PHP kernel learning tutorial PHP opcode kernel Implementation _php instance

Source: Internet
Author: User
OpCode is a part of the computer directive that specifies the action to be performed, and the format and specification of the instruction is specified by the directive specification of the processor. In addition to the instruction itself, there is usually an operand required by the instruction, and there may be instructions that do not require an explicit operand. These operands may be the value in the register, the value in the stack, the value of a block of memory, or the value in the IO port, and so on.

Usually opcode has another appellation: bytecode (byte codes). For example, Java Virtual Machine (JVM),. NET Common Intermediate language (Cil:common intermeditate Language) and so on.

1. OpCode Introduction

OpCode is a part of the computer directive that specifies the action to be performed, and the format and specification of the instruction is specified by the directive specification of the processor. In addition to the instruction itself, there is usually an operand required by the instruction, and there may be instructions that do not require an explicit operand. These operands may be values in registers, values in stacks, values of a block of memory, or values in IO ports, etc.

Usually opcode has another appellation: bytecode (byte codes). For example, Java Virtual Machine (JVM),. NET Common Intermediate language (Cil:common intermeditate Language) and so on

php is opcode in the previous introduction, PHP is built on the Zend virtual machine (Zend VM). php's opcode is the instruction in the Zend virtual machine (Zend-based 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

Inside the PHP implementation, opcode is represented by the following struct

\php-5.6.17\zend\zend_compile.h

Similar to the instructions for the CPU, there is a opcode field that indicates the instruction, and the operand that the opcode operates, PHP is not as low-level as the assembly, and may require additional information when the script is actually executed, and the Extended_value field holds that information. Where the result field is saved after the execution of the instruction is completed

For example, the following code is a function that compiles when the compiler encounters a print statement

\php-5.6.17\zend\zend_compile.c

void Zend_do_print (Znode *result, const znode *ARG tsrmls_dc)/* {{{*/{//new Create a zend_op zend_op *opline = Get_next_op (CG (A Ctive_op_array) tsrmls_cc);//Set the return value type of the new Zend_op to temporary (Is_tmp_var), because the memory in print is only for temporary output and does not need to be saved Opline->result_ Type = is_tmp_var;//request space for temporary variable Opline->result.var = get_temporary_variable (CG (Active_op_array));//Specify OpCode as Zend_ Printopline->opcode = zend_print;//assigns the passed in parameter to the first operand of the opcode Set_node (OPLINE->OP1, ARG); Set_unused (OPLINE->OP2); Get_node (result, opline->result);}

0x2:opcode type: Zend_op->zend_uchar opcode

Compared to the concept of assembly language, each opcode corresponds to a type, indicating that the Opcpde "operation instructions", opcode the type of Zend_uchar,zend_uchar is actually unsigned char, this field holds the shaping value is the OP number, To distinguish between different op types, the opcode values 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_S L 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_N OT 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_IN It_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 8 0#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

The execution handle of the OP, which is of type opcode_handler_t

typedef int (Zend_fastcall *opcode_handler_t) (Zend_opcode_handler_args);
This function pointer defines the execution method for the OP, and each opcode field corresponds to a kind of handler, such as if $ A = 1; The code generates the OP, the operands are const and CV, and finally the handler function is determined ZEND_ASSIGN_SPEC_ Cv_const_handler

/zend/zend_vm_execute.h

void Zend_init_opcodes_handlers (void) {static const opcode_handler_t labels[] = {: Zend_assign_spec_cv_const_handler,..}}

0x4:opcpde operand Znode

The Operand field is a more important part of the _zend_op type, where Op1,op2,result three operands are defined as Znode types

\php-5.6.17\zend\zend_compile.h

typedef struct _ZNODE {/* used only during compilation *//* this type of int field defines the type of the Znode operand-# is_const (1<<0)//Represents a constant, for example $a = 123; $b = "Hello"; After the code generates OP, both 123 and "Hello" are in constant type operands with a # define IS_TMP_VAR (1<<1)///For temporary variables, and temporary variables are generally represented by the preceding plus ~. This is a number of intermediate variables that need to be used during OP execution, such as initializing an array, a temporary variable is required to temporarily store the array zval, and then the array is assigned to the variable # define IS_VAR (1&LT;&LT;2)//general variables in the development representation # Define is_unused (1<<3)//UNUSED variable #define IS_CV (1<<4)//Compiled variable, this type of operand is more important, this type is later in PHP Version (about 5.1) appears, CV means compiled variable, that is, the compiled variable, the variables are stored in a symbol table, the symbol table is a hash table, if each read and write variables need to go to the hash tables to retrieve, will have a certain effect on efficiency, Therefore, in the execution context, some variables generated during compilation are cached. This type of operand is usually given at the beginning, such as the variable $a=123; $b = "Hello" code, $a and $b corresponding operands may be!0 and! 1, 0, and 1 are equivalent to an index number, which is obtained from the cache by the index number */int op_type;/* This field is a union, depending on the op_type, you take a different value of 1. When Op_type=is_const, the constant in U is the zval structure that corresponds to the operand 2. For example $a=123, in 123 of this operand, the constant in U is a zval of is_long type with a value Lval 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 post-compilation array Op_array

In the first line of the Zend_do_print function, we notice that the following line of code


PHP script code is compiled after the opcode is saved in Op_array, the structure of its internal storage 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, this will save the name of the function Zend_ Class_entry *scope;zend_uint fn_flags;union _zend_function *prototype;zend_uint num_args;zend_uint Required_num_args ; Zend_arg_info *arg_info;/* END of common elements */zend_uint *refcount;zend_op *opcodes; OpCode array zend_uint last;zend_compiled_variable *vars;int last_var;zend_uint t;zend_uint Nested_calls;zend_uint used _stack;zend_brk_cont_element *brk_cont_array;int last_brk_cont;zend_try_catch_element *try_catch_array;int last_ Try_catch;zend_bool has_finally_block;/* 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 doc_comment_len;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[zend_max_reserved_resources];};

The entire PHP script code is saved here by the compiled opcodes and executed by the following execute function

Zend_api void Execute (zend_op_array *op_array tsrmls_dc) {//... Loop through opcode in op_array or perform opcode in other op_array}

Each opcode has a opcode_handler_t function pointer field, which is used to perform the opcode,php in three ways for opcode processing

1. call:php uses the call method by default, that is, the way the function is called
2. Switch: Because opcode execution is a frequently required operation for each PHP program, you can use switch or goto to distribute
3. Goto: The efficiency of Goto is relatively higher, but whether efficiency is increased depends on different CPU
In fact, we will find that in/zend/zend_language_parser.c is the Zend opcode interpretation of the execution process, which includes call, switch, goto three kinds of opcode execution method

This is why PHP is referred to as the core principle of interpreted language, PHP after the completion of the lex lexical parsing, in the syntax parsing is generated when the production, directly through call, switch, Goto calls the Zend API, even if the interpretation of the execution

Relevant Link:

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 mentioned in this article to introduce the PHP kernel learning tutorial PHP opcode kernel Implementation of the relevant knowledge, I hope to be helpful to everyone.

  • 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.