Introduction to the Python runtime environment in the Python Virtual Machine

Source: Internet
Author: User

In fact, the Python runtime environment is a global concept, while the execution environment is actually a stack frame, which corresponds to the Code Block. there is a fundamental difference between the two, in the future operation process, we can understand the differences between the two.

The initialization process of the runtime environment is very complex. We will use a separate chapter to analyze the initialization process. Here, we assume that the initialization action has been completed and we have stood at the threshold of the Python virtual machine, you just need to push the first card, and the whole execution process is like a domino card.

In a function called PyEval_EvalFramEx, where the first card is pushed, this function is actually the specific implementation of the Python virtual machine. It is a very huge function, therefore, the source code in the list is somewhat different from the previous one.

PyEval_EvalFrameEx initializes some variables first, and important information contained in the PyCodeObject object is taken care. Of course, another important action is to initialize the stack top pointer of the stack and point it to f-> f_stacktop:

 
 
  1. [PyEval_EvalFrameEx in ceval.c]      
  2.  
  3.     co = f->f_code;  
  4.  
  5.     names = co->co_names;  
  6.  
  7.     coconsts = co->co_consts;  
  8.  
  9.     ffastlocals = f->f_localsplus;  
  10.  
  11.     ffreevars = f->f_localsplus + co->co_nlocals;  
  12.  
  13.     first_instr = (unsigned char*)PyString_AS_STRING(co->co_code);  
  14.  
  15.     next_instr = first_instr + f->f_lasti + 1;  
  16.  
  17.     stack_pointer = f->f_stacktop;  
  18.  
  19.     f->f_stacktop = NULL;   /* remains NULL unless yield suspends frame */  

As we have mentioned earlier, the co_code domain of the PyCodeObject object stores the parameters of bytecode instructions and bytecode instructions, the Python virtual machine executes the bytecode instruction sequence from start to end, traversing the entire co_code and executing the bytecode instruction sequence in sequence.

In a virtual machine running in Python, three variables are used to complete the entire traversal process. Co_code is actually a PyStringObject object, and the character array is truly meaningful. That is to say, the entire bytecode instruction sequence is actually a common character array in C. Therefore, all the three variables used in the traversal process are char * type variables: first_instr always points to the starting position of the bytecode instruction sequence;

Next_instr always points to the location of the next bytecode instruction to be executed; f_lasti points to the location of the previous bytecode instruction that has been executed. It shows the situations in which the three variables are traversed at a specific time point:

 
 
  1. [Ceval. c]
  2.  
  3. /* Interpreter main loop */
  4.  
  5. PyObject * PyEval_EvalFrameEx (PyFrameObject * f, int throwflag)
  6.  
  7. {
  8.  
  9. ......
  10.  
  11. Why=WHY_NOT;
  12.  
  13. ......
  14.  
  15. For (;;){
  16.  
  17. ......
  18.  
  19. Fast_next_opcode:
  20.  
  21. F->F_lasti=Pai_offset();
  22.  
  23. // Obtain the bytecode command
  24.  
  25. Opcode=NEXTOP();
  26.  
  27. Oparg=0;
  28.  
  29. // Obtain the command parameters if the Command requires Parameters
  30.  
  31. If (HAS_ARG (opcode ))
  32.  
  33. Oparg=NEXTARG();
  34.  
  35. Dispatch_opcode:
  36.  
  37. Switch (opcode ){
  38.  
  39. Case NOP:
  40.  
  41. Goto fast_next_opcode;
  42.  
  43. Case LOAD_FAST:
  44.  
  45. ......
  46.  
  47. }
  48.  
  49. }

So how is this step-by-step action done? Let's take a look at the overall architecture of the Python runtime environment for executing bytecode commands, which is actually a for loop with a huge switch/case structure, if you are familiar with Windows SDK programming, you can imagine that the huge message loop in Windows is in that structure. In the analysis of the PyCodeObject object, we have said that some Python bytecode has parameters, some have no parameters, and whether the bytecode with parameters is implemented through the macro HAS_ARG.

Note: For different bytecode commands, the next_instr displacement may be different due to whether the command parameters are required. However, next_instr always points to the next bytecode to be executed in Python, much like the PC register on the x86 platform.

After obtaining a bytecode instruction and the required command parameters, Python uses switch to determine the bytecode instruction and selects different case statements based on the judgment result, each bytecode command corresponds to a case statement. In the case statement, Python implements bytecode commands.

After a bytecode command is successfully executed, the execution process in the Python runtime environment will jump to the fast_next_opcode or for loop, the next action of Python is to obtain the next bytecode instruction and instruction parameter to complete the execution of the next instruction. In this way, all the bytecode commands contained in co_code are traversed one by one, and the execution of the Python program is completed.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.