UnderstandingPHPOpcode (Forward)

Source: Internet
Author: User
"UnderstandingOPcode" Opcode is an intermediate language after PHP script compilation, just like the ByteCode of Java, or. net msl. For example, you wrote the following PHP code :? PhpechoHelloWorld; $ a1 + 1; echo $ ;? PHP performs the following four steps to execute this Code:

"Understanding OPcode" Opcode is an intermediate language after PHP script compilation, just like the ByteCode of Java, or. net msl. For example, you wrote the following PHP code :? Php echo "Hello World"; $ a = 1 + 1; echo $ ;? PHP performs the following four steps to execute this Code:

Understanding OPcode
Opcode is an intermediate language after PHP script compilation. It is like ByteCode of Java or MSL of. NET. For example, you wrote the following PHP code:

 
  1. echo "Hello World";
  2. $a = 1 + 1;
  3. echo $a;
  4. ?>

PHP executes the Code in the following four steps (it should be the PHP language engine Zend)

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.

Digression: 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.

So what is Lexing? Those who have learned the compilation principles should have some knowledge about the lexical analysis steps in the compilation principles. Lex is a basis table for lexical analysis. 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". PHP4.2 starts to provide a function called token_get_all, this function can introduce a piece of PHP code Scanning into Tokens;
If you use this function to process the PHP code we mentioned at the beginning, the following results will be obtained:

 
  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [0] => 367
  6. [1] => Array
  7. (
  8. [0] => 316
  9. [1] => echo
  10. )
  11. [2] => Array
  12. (
  13. [0] => 370
  14. [1] =>
  15. )
  16. [3] => Array
  17. (
  18. [0] => 315
  19. [1] => "Hello World"
  20. )
  21. [4] => ;
  22. [5] => Array
  23. (
  24. [0] => 370
  25. [1] =>
  26. )
  27. [6] => =
  28. [7] => Array
  29. (
  30. [0] => 370
  31. [1] =>
  32. )
  33. [8] => Array
  34. (
  35. [0] => 305
  36. [1] => 1
  37. )
  38. [9] => Array
  39. (
  40. [0] => 370
  41. [1] =>
  42. )
  43. [10] => +
  44. [11] => Array
  45. (
  46. [0] => 370
  47. [1] =>
  48. )
  49. [12] => Array
  50. (
  51. [0] => 305
  52. [1] => 1
  53. )
  54. [13] => ;
  55. [14] => Array
  56. (
  57. [0] => 370
  58. [1] =>
  59. )
  60. [15] => Array
  61. (
  62. [0] => 316
  63. [1] => echo
  64. )
  65. [16] => Array
  66. (
  67. [0] => 370
  68. [1] =>
  69. )
  70. [17] => ;
  71. )


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, our PHP code will be Parsing:

* ZEND_ECHO 'Hello world'
* ZEND_ADD ~ 0 1 1
* ZEND_ASSIGN! 0 ~ 0
* ZEND_ECHO! 0

You may ask, where is our $?

Well, we will introduce the operands. each operand consists 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 the temporary variable of the variable table No. 0 unknown

IS_VAR is a variable in the general sense. They start with $.

IS_CV indicates 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, the variable will be used up by the CV, and you do not need to search for the active symbol table again for future reference of this variable. The CV variable starts! .
In this case, our $ a 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.