README.ZEND_VM 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ZEND_VM
  2. =======
  3. ZEND_VM architecture allows specializing opcode handlers according to op_type
  4. fields and using different execution methods (call threading, switch threading
  5. and direct threading). As a result ZE2 got more than 20% speedup on raw PHP
  6. code execution (with specialized executor and direct threading execution
  7. method). As in most PHP applications raw execution speed isn't the limiting
  8. factor but system calls and database calls are, your mileage with this patch
  9. will vary.
  10. Most parts of the old zend_execute.c go into zend_vm_def.h. Here you can
  11. find opcode handlers and helpers. The typical opcode handler template looks
  12. like this:
  13. ZEND_VM_HANDLER(<OPCODE-NUMBER>, <OPCODE>, <OP1_TYPES>, <OP2_TYPES>)
  14. {
  15. <HANDLER'S CODE>
  16. }
  17. <OPCODE-NUMBER> is a opcode number (0, 1, ...)
  18. <OPCODE> is an opcode name (ZEN_NOP, ZEND_ADD, :)
  19. <OP1_TYPES> & <OP2_TYPES> are masks for allowed operand op_types. Specializer
  20. will generate code only for defined combination of types. You can use any
  21. combination of the following op_types UNUSED, CONST, VAR, TMP and CV also
  22. you can use ANY mask to disable specialization according operand's op_type.
  23. <HANDLER'S CODE> is a handler's code itself. For most handlers it stills the
  24. same as in old zend_execute.c, but now it uses macros to access opcode operands
  25. and some internal executor data.
  26. You can see the conformity of new macros to old code in the following list:
  27. EXECUTE_DATA
  28. execute_data
  29. ZEND_VM_DISPATCH_TO_HANDLER(<OP>)
  30. return <OP>_helper(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)
  31. ZEND_VM_DISPATCH_TO_HELPER(<NAME>)
  32. return <NAME>(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)
  33. ZEND_VM_DISPATCH_TO_HELPER_EX(<NAME>,<PARAM>,<VAL>)
  34. return <NAME>(<VAL>, ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)
  35. ZEND_VM_CONTINUE()
  36. return 0
  37. ZEND_VM_NEXT_OPCODE()
  38. NEXT_OPCODE()
  39. ZEND_VM_SET_OPCODE(<TARGET>
  40. SET_OPCODE(<TARGET>
  41. ZEND_VM_INC_OPCODE()
  42. INC_OPCOD()
  43. ZEND_VM_RETURN_FROM_EXECUTE_LOOP()
  44. RETURN_FROM_EXECUTE_LOOP()
  45. ZEND_VM_C_LABEL(<LABEL>):
  46. <LABEL>:
  47. ZEND_VM_C_GOTO(<LABEL>)
  48. goto <LABEL>
  49. OP<X>_TYPE
  50. opline->op<X>.op_type
  51. GET_OP<X>_ZVAL_PTR(<TYPE>)
  52. get_zval_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
  53. GET_OP<X>_ZVAL_PTR_PTR(<TYPE>)
  54. get_zval_ptr_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
  55. GET_OP<X>_OBJ_ZVAL_PTR(<TYPE>)
  56. get_obj_zval_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
  57. GET_OP<X>_OBJ_ZVAL_PTR_PTR(<TYPE>)
  58. get_obj_zval_ptr_ptr(&opline->op<X>, EX(Ts), &free_op<X>, <TYPE>)
  59. IS_OP<X>_TMP_FREE()
  60. IS_TMP_FREE(free_op<X>)
  61. FREE_OP<X>()
  62. FREE_OP(free_op<X>)
  63. FREE_OP<X>_IF_VAR()
  64. FREE_VAR(free_op<X>)
  65. FREE_OP<X>_VAR_PTR()
  66. FREE_VAR_PTR(free_op<X>)
  67. Executor's helpers can be defined without parameters or with one parameter.
  68. This is done with the following constructs:
  69. ZEND_VM_HELPER(<HELPER-NAME>, <OP1_TYPES>, <OP2_TYPES>)
  70. {
  71. <HELPER'S CODE>
  72. }
  73. ZEND_VM_HELPER_EX(<HELPER-NAME>, <OP1_TYPES>, <OP2_TYPES>, <PARAM_SPEC>)
  74. {
  75. <HELPER'S CODE>
  76. }
  77. Executor's code is generated by PHP script zend_vm_gen.php it uses zend_vm_def.h
  78. and zend_vm_execute.skl as input and produces zend_vm_opcodes.h and
  79. zend_vm_execute.h. The first file is a list of opcode definitions. It is
  80. included from zend_compile.h. The second one is an executor code itself. It is
  81. included from zend_execute.c.
  82. zend_vm_gen.php can produce different kind of executors. You can select
  83. different opcode threading model using --with-vm-kind=CALL|SWITCH|GOTO. You can
  84. disable opcode specialization using --without-specializer. You can include or
  85. exclude old executor together with specialized one using --without-old-executor.
  86. At last you can debug executor using original zend_vm_def.h or generated file
  87. zend_vm_execute.h. Debugging with original file requires --with-lines
  88. option. By default ZE2 uses the following command to generate executor:
  89. $ php zend_vm_gen.php --with-vm-kind=CALL
  90. Zend Engine II currently includes two executors during the build process, one
  91. is the specialized version and the other is the old one non-specialized with
  92. function handlers. By default Zend Engine II uses the specialized one but you
  93. can switch to the old executor at runtime by calling zend_vm_use_old_executor().