Phpopcode kernel implementation in PHP kernel learning tutorial, kernel opcode. Phpopcode kernel implementation in the PHP kernel learning tutorial. the kernel opcodeopcode is a part of the computer instructions used to specify the operations to be executed, the command format and specifications are implemented by the PHP kernel of the processor's instructions.
Opcode is a part of a computer instruction. it is used to specify the operation to be executed. the instruction format and specification are specified by the instruction specification of the processor. In addition to the instruction itself, there are usually operations required by the instruction. some commands may not need explicit operations. These operands may be values in registers, values in stacks, memory values, or values in I/O ports.
Opcode also has another title: byte codes ). For example, Java virtual machine (JVM) and. NET Common Intermeditate Language.
1. Opcode introduction
Opcode is a part of a computer instruction. it is used to specify the operation to be executed. the instruction format and specification are specified by the instruction specification of the processor. In addition to the instruction itself, there are usually operations required by the instruction. some commands may not need explicit operations. These operands may be values in registers, values in stacks, memory values, or values in I/O ports.
Opcode also has another title: byte codes ). For example, Java virtual machine (JVM) and. NET Common Intermeditate Language
The opcode in PHP belongs to the previous section. PHP is built on Zend VM (Zend VM. PHP opcode is the command in Zend virtual machine (based on Zend intermediate code)
Relevant Link:
Http://www.luocong.com/learningopcode/doc/1._%E4%BB%80%E4%B9%88%E6%98%AFOpCode%EF%BC%9F.htm
2. Opcode in PHP
0x1: Data structure
In PHP implementation, opcode is represented by the following struct:
Php-5.6.17 \ Zend \ zend_compile.h
Struct _ zend_op {opcode_handler_t handler; // The processing function znode_op op1 called when the opcode is executed; // The operand znode_op op2 operated by opcode; // The operand znode_op result operated by opcode; ulong extended_value; uint lineno; zend_uchar opcode; // opcode zend_uchar op1_type; zend_uchar op2_type; zend_uchar result_type ;};
Similar to CPU commands, there is an opcode field indicating commands and the operands operated by this opcode. PHP is not as underlying as assembly, when the script is actually executed, more information may be required. the extended_value field stores this type of information, and the result field stores the result after the command is executed.
For example, the following code is a function compiled when the compiler encounters a print statement.
Php-5.6.17 \ Zend \ zend_compile.c
Void merge (znode * result, const znode * arg TSRMLS_DC)/* {*/{// create a new zend_op * opline = get_next_op (CG (active_op_array) TSRMLS_CC ); // Set the returned value type of the newly created zend_op to a temporary variable (IS_TMP_VAR), because the memory in print is only for temporary output and does not need to save opline-> result_type = IS_TMP_VAR; // Apply for space opline for temporary variables-> result. var = get_temporary_variable (CG (active_op_array); // specify opcode as ZEND_PRINTopline-> opcode = ZEND_PRINT; // assign the passed parameter to SET_NODE (opline-> op1, arg), SET_UNUSED (opline-> op2), and GET_NODE (result, opline-> result );}
0x2: opcode type: zend_op-> zend_uchar opcode
Compared with the concept of assembly language, each opcode corresponds to a type, indicating the "operation command" of the opcpvdf. the opcode type is zend_uchar, and zend_uchar is actually unsigned char, the integer value stored in this field is the op number, which is used to distinguish different op types. the optional values of opcode are defined as macros.
/Zend/zend_vm_opcodes.h
#define ZEND_NOP 0#define ZEND_ADD 1#define ZEND_SUB 2#define ZEND_MUL 3#define ZEND_DIV 4#define ZEND_MOD 5#define ZEND_SL 6#define ZEND_SR 7#define ZEND_CONCAT 8#define ZEND_BW_OR 9#define ZEND_BW_AND 10#define ZEND_BW_XOR 11#define ZEND_BW_NOT 12#define ZEND_BOOL_NOT 13#define ZEND_BOOL_XOR 14#define ZEND_IS_IDENTICAL 15#define ZEND_IS_NOT_IDENTICAL 16#define ZEND_IS_EQUAL 17#define ZEND_IS_NOT_EQUAL 18#define ZEND_IS_SMALLER 19#define ZEND_IS_SMALLER_OR_EQUAL 20#define ZEND_CAST 21#define ZEND_QM_ASSIGN 22#define ZEND_ASSIGN_ADD 23#define ZEND_ASSIGN_SUB 24#define ZEND_ASSIGN_MUL 25#define ZEND_ASSIGN_DIV 26#define ZEND_ASSIGN_MOD 27#define ZEND_ASSIGN_SL 28#define ZEND_ASSIGN_SR 29#define ZEND_ASSIGN_CONCAT 30#define ZEND_ASSIGN_BW_OR 31#define ZEND_ASSIGN_BW_AND 32#define ZEND_ASSIGN_BW_XOR 33#define ZEND_PRE_INC 34#define ZEND_PRE_DEC 35#define ZEND_POST_INC 36#define ZEND_POST_DEC 37#define ZEND_ASSIGN 38#define ZEND_ASSIGN_REF 39#define ZEND_ECHO 40#define ZEND_PRINT 41#define ZEND_JMP 42#define ZEND_JMPZ 43#define ZEND_JMPNZ 44#define ZEND_JMPZNZ 45#define ZEND_JMPZ_EX 46#define ZEND_JMPNZ_EX 47#define ZEND_CASE 48#define ZEND_SWITCH_FREE 49#define ZEND_BRK 50#define ZEND_CONT 51#define ZEND_BOOL 52#define ZEND_INIT_STRING 53#define ZEND_ADD_CHAR 54#define ZEND_ADD_STRING 55#define ZEND_ADD_VAR 56#define ZEND_BEGIN_SILENCE 57#define ZEND_END_SILENCE 58#define ZEND_INIT_FCALL_BY_NAME 59#define ZEND_DO_FCALL 60#define ZEND_DO_FCALL_BY_NAME 61#define ZEND_RETURN 62#define ZEND_RECV 63#define ZEND_RECV_INIT 64#define ZEND_SEND_VAL 65#define ZEND_SEND_VAR 66#define ZEND_SEND_REF 67#define ZEND_NEW 68#define ZEND_INIT_NS_FCALL_BY_NAME 69#define ZEND_FREE 70#define ZEND_INIT_ARRAY 71#define ZEND_ADD_ARRAY_ELEMENT 72#define ZEND_INCLUDE_OR_EVAL 73#define ZEND_UNSET_VAR 74#define ZEND_UNSET_DIM 75#define ZEND_UNSET_OBJ 76#define ZEND_FE_RESET 77#define ZEND_FE_FETCH 78#define ZEND_EXIT 79#define ZEND_FETCH_R 80#define ZEND_FETCH_DIM_R 81#define ZEND_FETCH_OBJ_R 82#define ZEND_FETCH_W 83#define ZEND_FETCH_DIM_W 84#define ZEND_FETCH_OBJ_W 85#define ZEND_FETCH_RW 86#define ZEND_FETCH_DIM_RW 87#define ZEND_FETCH_OBJ_RW 88#define ZEND_FETCH_IS 89#define ZEND_FETCH_DIM_IS 90#define ZEND_FETCH_OBJ_IS 91#define ZEND_FETCH_FUNC_ARG 92#define ZEND_FETCH_DIM_FUNC_ARG 93#define ZEND_FETCH_OBJ_FUNC_ARG 94#define ZEND_FETCH_UNSET 95#define ZEND_FETCH_DIM_UNSET 96#define ZEND_FETCH_OBJ_UNSET 97#define ZEND_FETCH_DIM_TMP_VAR 98#define ZEND_FETCH_CONSTANT 99#define ZEND_GOTO 100#define ZEND_EXT_STMT 101#define ZEND_EXT_FCALL_BEGIN 102#define ZEND_EXT_FCALL_END 103#define ZEND_EXT_NOP 104#define ZEND_TICKS 105#define ZEND_SEND_VAR_NO_REF 106#define ZEND_CATCH 107#define ZEND_THROW 108#define ZEND_FETCH_CLASS 109#define ZEND_CLONE 110#define ZEND_RETURN_BY_REF 111#define ZEND_INIT_METHOD_CALL 112#define ZEND_INIT_STATIC_METHOD_CALL 113#define ZEND_ISSET_ISEMPTY_VAR 114#define ZEND_ISSET_ISEMPTY_DIM_OBJ 115#define ZEND_PRE_INC_OBJ 132#define ZEND_PRE_DEC_OBJ 133#define ZEND_POST_INC_OBJ 134#define ZEND_POST_DEC_OBJ 135#define ZEND_ASSIGN_OBJ 136#define ZEND_INSTANCEOF 138#define ZEND_DECLARE_CLASS 139#define ZEND_DECLARE_INHERITED_CLASS 140#define ZEND_DECLARE_FUNCTION 141#define ZEND_RAISE_ABSTRACT_ERROR 142#define ZEND_DECLARE_CONST 143#define ZEND_ADD_INTERFACE 144#define ZEND_DECLARE_INHERITED_CLASS_DELAYED 145#define ZEND_VERIFY_ABSTRACT_CLASS 146#define ZEND_ASSIGN_DIM 147#define ZEND_ISSET_ISEMPTY_PROP_OBJ 148#define ZEND_HANDLE_EXCEPTION 149#define ZEND_USER_OPCODE 150#define ZEND_JMP_SET 152#define ZEND_DECLARE_LAMBDA_FUNCTION 153#define ZEND_ADD_TRAIT 154#define ZEND_BIND_TRAITS 155#define ZEND_SEPARATE 156#define ZEND_QM_ASSIGN_VAR 157#define ZEND_JMP_SET_VAR 158#define ZEND_DISCARD_EXCEPTION 159#define ZEND_YIELD 160#define ZEND_GENERATOR_RETURN 161#define ZEND_FAST_CALL 162#define ZEND_FAST_RET 163#define ZEND_RECV_VARIADIC 164#define ZEND_SEND_UNPACK 165#define ZEND_POW 166#define ZEND_ASSIGN_POW 167
0x3: opcode execution handle: zend_op-> handler
Op execution handle, whose type is opcode_handler_t
Typedef int (ZEND_FASTCALL * opcode_handler_t) (ZEND_OPCODE_HANDLER_ARGS );
This function pointer defines the execution method for op. Each opcode field corresponds to a kind of handler. for example, if $ a = 1; such code generates op, the operands are const and cv. Finally, we can determine that handler is the ZEND_ASSIGN_SPEC_CV_CONST_HANDLER function.
/Zend/zend_vm_execute.h
void zend_init_opcodes_handlers(void){static const opcode_handler_t labels[] = {..ZEND_ASSIGN_SPEC_CV_CONST_HANDLER,..}}
0x4: opcpvdf operand znode
The operand field is an important part of the _ zend_op type. The three operands op1, op2, and result are defined as znode.
Php-5.6.17 \ Zend \ zend_compile.h
Typedef struct _ znode {/* used only during compilation * // * This int type field defines the type of the znode operand # define IS_CONST (1 <0) // represents a constant, for example, $ a = 123; $ B = "hello"; after the code is generated, both 123 and "hello" exist as constants. # define IS_TMP_VAR (1 <1) // indicates a temporary variable. the temporary variable is usually prefixed ~ This is an intermediate variable used during OP execution. for example, when initializing an array, a temporary variable is required to temporarily store the array zval, then assign the array value to the variable # define IS_VAR (1 <2) // variable in the general sense, expressed in $ development # define IS_UNUSED (1 <3) // Unused variable # define IS_CV (1 <4) // Compiled variable. the operands of this type are important. This type is in a later PHP version (about 5.1) in this example, CV indicates compiled variable, which is the compiled variable. all the variables are saved in a symbol table. this symbol table is a hash table, if you need to search in the hash table for each read/write variable, the efficiency will be affected. Therefore, some variables generated during compilation will be cached in the execution context environment. The operands of this type are generally! For example, $ a = 123; $ B = "hello", the corresponding operands of $ a and $ B may be! 0 and! 1, 0, and 1 are equivalent to an index number. The index number is used to obtain the corresponding value */int op_type from the cache./* This field is a consortium. according to op_type, u takes different values 1. when op_type = IS_CONST, constant in u stores the zval structure corresponding to the operand 2. for example, when $ a = 123, in the 123 operand, constant in u is an IS_LONG type zval, and its value of lval is 123 */union {znode_op op; zval constant; /* replaced by literal/zv */zend_op_array * op_array; zend_ast * ast;} u; zend_uint EA;/* extended attributes */} znode;
0x5: opcode compiled array op_array
In the first line of the zend_do_print function, we noticed the following line of code:
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
The opcode generated after the PHP script code is compiled is stored in op_array. its internal storage structure is as follows:
Php-5.6.17 \ Zend \ zend_compile.h
Struct _ zend_op_array {/* Common elements */zend_uchar type; const char * function_name; // if it is a user-defined function, the function name zend_class_entry * scope will be saved here; zend_uint fn_flags; union _ zend_function * prototype; zend_uint num_args; zend_uint limit; limit * arg_info;/* END of common elements */limit * refcount; zend_op * opcodes; // opcode array returns last; optional * vars; int last_var; zend_uint T; zend_uint values; zend_uint used_stack; optional * brk_cont_array; int values; optional * values; int values; zend_bool values; /* static variables support */HashTable * static_variables; zend_uint this_var; const char * filename; zend_uint line_start; zend_uint line_end; const char * doc_comment; zend_uint begin; zend_uint early_binding; /* the linked list of delayed declarations */zend_literal * literals; int last_literal; void ** run_time_cache; int last_cache_slot; void * reserved [region];};
The compiled opcodes of the PHP script code is stored here and executed by the following execute function during execution.
ZEND_API void execute (zend_op_array * op_array TSRMLS_DC) {//... cyclically execute opcode in op_array or execute opcode in other op_array}
Each opcode has an opcode_handler_t function pointer field for executing this opcode. PHP can process opcode in three ways.
1. CALL: PHP uses the CALL method by default, that is, the function CALL method.
2. SWITCH: because opcode execution is frequently required by every PHP program, you can use SWITCH or GOTO for distribution.
3. GOTO: Generally, the GOTO efficiency is relatively higher, but whether the efficiency is improved depends on different CPUs.
In fact, we will find that in/zend/zend_language_parser.c, the opcode translation and interpretation execution process of Zend contains three opcode execution methods: call, switch, and goto.
This is why PHP is called the kernel principle of interpreted language. after The Lex lexical parsing is completed, PHP will generate the syntax parsing, call the zend api directly through call, switch, and goto, even if the interpretation is executed
Relevant Link:
http://www.nowamagic.net/librarys/veda/detail/1325http://php.net/manual/zh/internals2.opcodes.list.phphttp://www.nowamagic.net/librarys/veda/detail/1543http://www.nowamagic.net/librarys/veda/detail/1324http://www.nowamagic.net/librarys/veda/detail/1543 http://www.laruence.com/2008/06/18/221.htmlhttp://www.php-internals.com/book/?p=chapt02/02-03-02-opcode
3. opcode translation execution (instant interpretation execution)
Relevant Link:
http://www.php-internals.com/book/?p=chapt02/02-03-03-from-opcode-to-handler
The above section introduces the PHP kernel learning tutorial and the php opcode kernel implementation. I hope it will be helpful to you.
Articles you may be interested in:
- PHP OPCode cache APC details
- Opcodes (Operation Code) that understands php principles)
- Use PHP extension vld to view PHP opcode operation steps
- Enable Zend OPCode caching for PHP5.4
- How to use PHP Embed SAPI to implement Opcodes viewer
Implements the opcode Kernel. the kernel opcode is a part of a computer instruction. it is used to specify the operation to be executed. the instruction format and specifications are the instruction of the processor...