In-depth understanding of the OpCode principles of PHP, in-depth understanding of opcode

Source: Internet
Author: User

In-depth understanding of the OpCode principles of PHP, in-depth understanding of opcode

This article describes the principles of OpCode in PHP. We will share this with you for your reference. The details are as follows:

OpCode is an intermediate language after PHP script compilation, like ByteCode of Java or MSL of. NET. This article is mainly based on Understanding OPcode and the network. According to my personal Understanding and modification, it is recorded as follows:

PHP code:

<?php  echo "Hello World";  $a = 1 + 1;  echo $a;?>

PHP performs the following four steps to execute this Code:

1. Scanning (Lexing) converts PHP code into a language snippet (Tokens)
2. Parsing: converts Tokens into a simple and meaningful expression.
3. Compilation: Compile the expression into Opocdes
4. Execution: Execute Opcodes one by one at a time to implement the PHP script function.

Note: The current Cache, such as APC, enables PHP to Cache Opcodes. In this way, when a request comes, you do not need to repeat the previous three steps, this greatly improves the execution speed of PHP.

First, Zend/zend_language_scanner.c will perform lexical analysis on the entered PHP code based on Zend/zend_language_scanner.l (Lex file) to get a "word" one by one ", PHP4.2 + started to provide a function named token_get_all. This function can introduce a piece of PHP code Scanning into Tokens;

<?php$tokens = token_get_all('<?php  echo "Hello World";  $a = 1 + 1;  echo $a;?>');print_r($tokens);?>

The following result is displayed:

Array(  [0] => Array    (      [0] => 367      [1] => <?php      [2] => 1    )  [1] => Array    (      [0] => 370      [1] =>      [2] => 2    )  [2] => Array    (      [0] => 316      [1] => echo      [2] => 2    )  [3] => Array    (      [0] => 370      [1] =>      [2] => 2    )  [4] => Array    (      [0] => 315      [1] => "Hello World"      [2] => 2    )  [5] => ;  [6] => Array    (      [0] => 370      [1] =>      [2] => 2    )  [7] => Array    (      [0] => 309      [1] => $a      [2] => 3    )  [8] => Array    (      [0] => 370      [1] =>      [2] => 3    )  [9] => =  [10] => Array    (      [0] => 370      [1] =>      [2] => 3    )  [11] => Array    (      [0] => 305      [1] => 1      [2] => 3    )  [12] => Array    (      [0] => 370      [1] =>      [2] => 3    )  [13] => +  [14] => Array    (      [0] => 370      [1] =>      [2] => 3    )  [15] => Array    (      [0] => 305      [1] => 1      [2] => 3    )  [16] => ;  [17] => Array    (      [0] => 370      [1] =>      [2] => 3    )  [18] => Array    (      [0] => 316      [1] => echo      [2] => 4    )  [19] => Array    (      [0] => 370      [1] =>      [2] => 4    )  [20] => Array    (      [0] => 309      [1] => $a      [2] => 4    )  [21] => ;  [22] => Array    (      [0] => 370      [1] =>      [2] => 4    )  [23] => Array    (      [0] => 369      [1] => ?>      [2] => 5    ))

The returned results, strings, characters, and spaces in the source code are all returned as they are. The characters in each source code appear in the corresponding sequence. Other statements, such as tags, operators, and statements, are converted into an Array containing two parts: Token ID (that is, the corresponding code for changing Token within Zend, for example, t_ECHO, T_STRING), and the original content in the source code.

Next, it is the Parsing phase. Parsing first discards more than spaces in the Tokens Array, and then converts the remaining Tokens into a simple expression.

1. echo a constant string
2. add two numbers together
3. store the result of the prior expression to a variable
4. echo a variable

Then, the Compilation phase is changed. It will compile Tokens into op_array, and each op_arrayd contains the following five parts:

1. indicates the operation type of each op_array, such as add and echo.
2. Store the Opcode results.
3. operands 1 to Opcode
4. operand 2
5. The extended value is an integer to distinguish the overloaded operator.

For example, the PHP code is Parsing:

[root@localhost html]# /usr/local/php/bin/php -dvld.active=1 hello.phpBranch analysis from position: 0Return foundfilename:    /var/www/html/hello.phpfunction name: (null)number of ops: 6compiled vars: !0 = $aline   # op              fetch     ext return operands-------------------------------------------------------------------------------  2   0 ECHO                           'Hello+world'  3   1 ADD                       ~0   1, 1     2 ASSIGN                          !0, ~0  4   3 ECHO                           !0  6   4 RETURN                          1     5* ZEND_HANDLE_EXCEPTIONHello world2

Each operand is composed of the following two parts:

A) op_type: IS_CONST, IS_TMP_VAR, IS_VAR, IS_UNUSED, or IS_CV

B) u, a consortium, stores the value (const) or left value (var) of this operand with different types according to op_type)

For var, each var is different. IS_TMP_VAR, as the name suggests, is a temporary variable that stores the results of some op_array for the next op_array. The u of this operand stores a handle (integer) pointing to the variable table ), this type of operand is generally used ~ For example ~ 0 indicates that the temporary variable IS_VAR, which is unknown to the variable table No. 0, is a variable in the general sense, they start with $ and represent IS_CV, indicating a cache mechanism used by the compiler after ZE2.1/PHP5.1. This variable stores the address of the variable referenced by it, when a variable is referenced for the first time, it will be used up by the CV. For reference to this variable in the future, you do not need to search for the active symbol table again. The CV variable starts! .

$ A variable is optimized! 0.

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.