How can PHP code be executed?

Source: Internet
Author: User
As we all know, the CPU of a computer can only execute binary machine codes. Each type of CPU has a corresponding assembly language. The Assembly Language compiler translates the assembly language into a binary machine language, and then the CPU starts to execute these machine codes. Assembly language, as a layer between machine language and program designers, brings us a lot of convenience. programmers do not need to use obscure 01 numbers.

As we all know, the CPU of a computer can only execute binary machine codes. Each type of CPU has a corresponding assembly language. The Assembly Language compiler translates the assembly language into a binary machine language, and then the CPU starts to execute these machine codes. Assembly language, as a layer between machine language and program designers, brings us a lot of convenience. programmers do not need to use obscure 01 numbers.

As we all know, the CPU of a computer can only execute binary machine codes. Each type of CPU has a corresponding assembly language. The Assembly Language compiler translates the assembly language into a binary machine language, and then the CPU starts to execute these machine codes. Assembly language, as a layer between machine language and program designers, brings us a lot of convenience. programmers do not need to use obscure 01 numbers to write programs, of course, people are not satisfied with this kind of progress, so there is another layer-C language on the assembly language, and the C language is closer to the familiar "natural language ", the program designer can compile the C source code file into the target file through the C language compiler (the binary file is first translated into the assembly language, and then the machine code is generated by the Assembly Language ), then, the target files are connected together to form an executable file. As someone has said, "Any problem in the computer science field can be solved by adding an indirect middle layer." ("Any problem in computer science can be solved by another layer of indirection. ") the PHP language is a layer above the C language, and the PHP engine is implemented by the C language, therefore, the PHP language abstraction layer on top of C is easier to use than C, and the entry threshold is lower.

So how can PHP be executed?

If the word "Translation" is used for conversion from the PHP language to the C language, it is not accurate because the engine does not convert the PHP language to the C language, and then runs the compiled link of the converted C language. When parsing PHP code, the engine is usually divided into two parts: Compile and execute:

Compilation phase: the engine converts PHP code to the intermediate code of op code.
Execution phase: the engine interprets and executes the op code generated during the compilation phase.

There will be a special article on op code, and there are already many articles on the Internet. In short, PHP code will be compiled into the form of _ zend_op_array, which is a struct, it includes many related attributes and the most important member zend_op * opcodes, that is, the opcode array. The execution stage engine executes each opcode in sequence.

Currently, there are 154 types of opcode in PHP 5.3.2, which can be viewed in {PHPSRC}/Zend/zend_vm_opcodes.h. The op structure is defined:

Struct _ zend_op {
Opcode_handler_t handler;
Znode result;
Znode op1;
Znode op2;
Ulong extended_value;
Uint lineno;
Zend_uchar opcode;
};

The opcode member corresponds to one of the 154 opcode macro definitions. Each op corresponds to a related execution handle (opcode_handler_t handler) based on the opcode and operand types ), the execution handle is a function pointer, And the execution handles of op are defined in {PHPSRC}/Zend/zend_vm_execute.h. This file can be run using a PHP script ({PHPSRC}/Zend/zend_vm_gen.php) this PHP script is used to generate two files, zend_vm_opcodes.h and zend_vm_execute.h. The content of zend_vm_execute.h varies according to the generated parameters. Here, the zend engine can be used to distribute the op, for example, CALL, SWITCH, and GOTO are used by default, that is, function CALL. So here we will use function CALL to briefly introduce the functions of this file (the file is very large, there are nearly 36000 rows, so don't take it seriously), here All the functions defined as static int ZEND_FASTCALL and started with ZEND _ * are the op handle. In this file, the first function execute is the main method for executing op, use this as the entry to execute a series of op operations. It can be said that the functions and features of the entire PHP are completed through these op handles (of course these handles will indirectly call the functions in other modules ), how does the 154 Opcodes correspond to the execution handles of these static int ZEND_FASTCALL ZEND _ * statements? In the same file, we can see the zend_init_opcodes_handlers function. This function initializes a static const opcode_handler_t labels [] array. This labels array is a table of handlers, which has nearly 4000 items, an algorithm maps an opcode to an element in this table. The algorithm can also be found in zend_vm_execute.h. zend_vm_set_opcode_handler and zend_vm_get_opcode_handler near the end of the file are implemented by this algorithm.

So how does the engine implement the features of the PHP language through these op handler? Here is the simplest example: there is only one line of PHP code.

$ A = 123;

?>

Through some method (we will introduce these methods later), we can know that this line of code mainly generates a zend_op, and its main member value is:

Opcode = 38 (corresponding to # define ZEND_ASSIGN 38)

Op1 = $ a ($ a variable actually exists in the form of cv, which will be introduced later)

Op2 = 123 (const constant)

Handler = ZEND_ASSIGN_SPEC_CV_CONST_HANDLER)

Opcode ZEND_ASSIGN means to assign a constant to a cv (compiled variable). This cv is actually an existing form of the $ a variable. The ZEND_ASSIGN_SPEC_CV_CONST_HANDLER definition is found in zend_vm_execute.h. Its main function is to assign the value of op2 to the variable of op1, which is more complex than imagined, there will be variable initialization, variable write-time assignment, and other processes, will introduce each process in the future. This completes the functionality of the PHP statement. It can be seen that op handler only operates op1 op2 (possibly result) in some fixed ways. handler ignores the specific values in these operands, these values are determined when op is generated in the compilation phase. For example, if $ a = 123 is changed to $ a = 456, op2 is 456 in the generated op, handler is always handled in a fixed way.

Therefore, we can know that the PHP Execution Process is to compile the PHP code into op code through the compiler, and then the zend virtual machine executes the opcode in a certain order, specifically, each opcode is distributed to a specific op code handler.

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.