PHP opcode kernel Implementation

Source: Internet
Author: User

Catalogue

1. OpCode Introduction 2. The Opcode3 in PHP. OpCode translation Execution (instant interpretation execution)

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

Within the PHP implementation, opcode is represented by the following structure \php-5.6.17\zend\zend_compile.h /c1>

struct _ZEND_OP {    opcode_handler_t handler;    The handler function called when executing the opcode    znode_op op1;                OpCode the operand    znode_op op2;                OpCode operation operand    znode_op result;    ULONG Extended_value;    UINT Lineno;    Zend_uchar opcode;            OpCode code    zend_uchar op1_type;    Zend_uchar Op2_type;    Zend_uchar result_type;};

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 (Active_op_array) tsrmls_cc);    Set the return 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 be saved    opline->result_type = Is_tmp_var;    Request space for temporary variables    opline->result.var = get_temporary_variable (CG (Active_op_array));    Specify opcode to zend_print    opline->opcode = zend_print;    Assign 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, opcode's desirable 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#def                                Ine 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_assig                       N_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#def                           Ine 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 ZE                          ND_JMPNZ 44#define zend_jmpznz 45#define zend_jmpz_ex 46#define zend_jmpnz_ex 47#define zend_case 48#define zend_s                             Witch_free 49#define zend_brk 50#define Zend_cont 51#define zend_bool 52#define zend_init_string 53#define Zend_add_c                          HAR 54#define zend_add_string 55#define Zend_add_var 56#define zend_begin_silence 57#define zend_end_silence 58#define zend_init_fcal L_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#d                         Efine 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#defin                E 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 ZE                         Nd_unset_dim 75#define zend_unset_obj 76#define zend_fe_reset 77#define zend_fe_fetch 78#define zend_exit 79#define zend_f                      Etch_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_o                     BJ_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_f Unc_arg 93#define zend_fetch_obj_func_arg 94#define zend_fetch_unset 95#d Efine zend_fetch_dim_unset 96#define zend_fetch_obj_unset 97#define zend_fetch_dim_tmp_v AR 98#define zend_fetch_constant 99#define zend_goto 100#defin                   E zend_ext_stmt 101#define zend_ext_fcall_begin 102#define zend_ext_fcall_end 103#defineZend_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_ISS                     Et_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_de C_obj 135#define zend_assign_obj 136#define zend_instanceof 1 38#define zend_declare_class 139#define zend_declare_inherited_class 140#define ZEND_DECLARE_FUN Ction 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#def                Ine 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_fas                   T_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 defines the type of Znode operand #define IS_CONST (1<& LT;0)//Represents a constant, such as $ A = 123; $b = "Hello"; After the code generates OP, both 123 and "Hello" are present in the constant type operand #define IS_TMP_VAR (1<<1)///represents a temporary variable, the temporary variable is generally represented by the preceding plus ~, which is some OP execution procedure Intermediate variables that need to be used, such as initializing an array, require a temporary variable to temporarily store the array zval, and then assign the array to the variable #define IS_VAR (1&LT;&LT;2)//general-purpose variables in the development representation #def Ine is_unused (1<<3)//UNUSED variable #define IS_CV (1<<4)//Compiled variable, this type of operation Number is more important, this type is in the later version of PHP (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 hashtable, If you need to retrieve a hash table every time you read and write a variable, it will have a certain effect on efficiency, so in the execution context, some of the variables generated during compilation will be cached.        This type of operand is usually expressed as a. Start, for example, the variable $a=123; $b = "Hello" code, $a and $b the corresponding operand may be!0 and! 1, 0, and 1 are equivalent to an index number, and the corresponding value is obtained from the cache by 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

Zend_op *opline = Get_next_op (CG (Active_op_array) tsrmls_cc);

The PHP script code is generated after the opcode is saved in Op_array, and 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, 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 method of function invocation 2. Switch: Because opcode execution is a frequently needed operation for each PHP program, you can distribute 3 using switch or goto. Goto: The efficiency of Goto is relatively higher, but whether efficiency is increased depends on different CPUs

In fact, we will find that in/zend/zend_language_parser.c is Zend's opcode translation interpretation execution process, which includes call, switch, goto three opcode execution methods This is why PHP is called the core principle of interpreted language, PHP after the completion of the lex lexical parsing, in the syntax parsing is generated when the production, the direct call, switch, goto Zend API, even if the interpretation of the execution

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

Copyright (c) Little5ann All rights reserved

  • Related Article

    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.