Home > Backend Development > PHP Tutorial > PHP kernel learning tutorial: php opcode kernel implementation, kernel opcode_PHP tutorial

PHP kernel learning tutorial: php opcode kernel implementation, kernel opcode_PHP tutorial

WBOY
Release: 2016-07-12 08:59:55
Original
1077 people have browsed it

PHP kernel learning tutorial PHP opcode kernel implementation, kernel opcode

opcode is part of the computer instructions, used to specify the operations to be performed, the format and specifications of the instructions are processed by specified by the instruction specification of the device. In addition to the instruction itself, there are usually operands required by the instruction. Some instructions may not require explicit operands. These operands may be the value in the register, the value in the stack, the value in a certain memory or the value in the IO port, etc.

Usually opcode has another name: byte codes. For example, Java Virtual Machine (JVM), .NET's Common Intermediate Language (CIL: Common Intermediate Language), etc.

1. Introduction to Opcode

Opcode is a part of the computer instruction that is used to specify the operation to be performed. The format and specification of the instruction are specified by the instruction specification of the processor. In addition to the instruction itself, there are usually operands required by the instruction. Some instructions may not require explicit operands. These operands may be the value in the register, the value in the stack, the value in a certain memory or the value in the IO port, etc.

Usually opcode has another name: byte codes. For example, Java Virtual Machine (JVM), .NET’s Common Intermediate Language (CIL: Common Intermediate Language), etc.

The opcode in PHP belongs to the latter part of the previous introduction. PHP is built on the Zend Virtual Machine (Zend VM). PHP's opcode is the instruction in the Zend virtual machine (based on Zend's intermediate code)

Relevant Link:

http://www.luocong.com/learningopcode/doc/1._What is OpCode? .htm

2. Opcode in PHP

0x1: Data structure

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

php-5.6.17Zendzend_compile.h

struct _zend_op 
{
opcode_handler_t handler; // 执行该opcode时调用的处理函数
znode_op op1; // opcode所操作的操作数
znode_op op2; // opcode所操作的操作数
znode_op result;
ulong extended_value;
uint lineno;
zend_uchar opcode; // opcode代码
zend_uchar op1_type;
zend_uchar op2_type;
zend_uchar result_type;
}; 
Copy after login

Similar to CPU instructions, there is an opcode field that indicates the instruction, and the operands operated by this opcode. PHP is not as low-level as assembly. When the script is actually executed, more information may be needed, such as the extended_value field. This type of information is saved, and the result field saves the result after the instruction is executed

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

php-5.6.17Zendzend_compile.c

void zend_do_print(znode *result, const znode *arg TSRMLS_DC) /* {{{ */
{ 
//新创建一条zend_op 
zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

//将新建的zend_op的返回值类型设置为临时变量(IS_TMP_VAR),因为print中的内存仅仅为了临时输出,并不需要保存
opline->result_type = IS_TMP_VAR;
//为临时变量申请空间
opline->result.var = get_temporary_variable(CG(active_op_array));
//指定opcode为ZEND_PRINT
opline->opcode = ZEND_PRINT;
//将传递进来的参数赋值给这条opcode的第一个操作数
SET_NODE(opline->op1, arg);
SET_UNUSED(opline->op2);
GET_NODE(result, opline->result);
}
Copy after login

0x2: opcode type: zend_op->zend_uchar opcode

Comparing the concept of assembly language, each opcode corresponds to a type, indicating the "operation instruction" of the opcpde. The type of opcode is zend_uchar. zend_uchar is actually unsigned char. The integer value saved in this field is op. The number is used to distinguish different op types. The possible 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 
Copy after login

0x3: opcode execution handle: zend_op->handler

The execution handle of op, its type is opcode_handler_t

typedef int (ZEND_FASTCALL *opcode_handler_t) (ZEND_OPCODE_HANDLER_ARGS);
This function pointer defines the execution method for the op. Each opcode field corresponds to a type of handler. For example, if $a = 1; the op generated by such code has the operands const and cv, and finally the handler can be determined to be a function. 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,
..
}
}
Copy after login

0x4: opcpde operand znode

The operand field is an important part of the _zend_op type, among which the three operands op1, op2, and result are defined as znode types

php-5.6.17Zendzend_compile.h

typedef struct _znode { /* used only during compilation */
/*
这个int类型的字段定义znode操作数的类型
#define IS_CONST (1<<0) //表示常量,例如$a = 123; $b = "hello";这些代码生成OP后,123和"hello"都是以常量类型操作数存在
#define IS_TMP_VAR (1<<1) //表示临时变量,临时变量一般在前面加~来表示,这是一些OP执行过程中需要用到的中间变量,例如初始化一个数组的时候,就需要一个临时变量来暂时存储数组zval,然后将数组赋值给变量
#define IS_VAR (1<<2) //一般意义上的变量,以$开发表示
#define IS_UNUSED (1<<3) // Unused variable 
#define IS_CV (1<<4) // Compiled variable,这种类型的操作数比较重要,此类型是在PHP后来的版本中(大概5.1)中才出现,CV的意思是compiled variable,即编译后的变量,变量都是保存在一个符号表中,这个符号表是一个哈希表,如果每次读写变量的时候都需要到哈希表中去检索,会对效率有一定的影响,因此在执行上下文环境中,会将一些编译期间生成的变量缓存起来。此类型操作数一般以!开头表示,比如变量$a=123;$b="hello"这段代码,$a和$b对应的操作数可能就是!0和!1, 0和1相当于一个索引号,通过索引号从缓存中取得相应的值
*/
int op_type;
/*
此字段为一个联合体,根据op_type的不同,u取不同的值
1. op_type=IS_CONST的时候,u中的constant保存的就是操作数对应的zval结构
2. 例如$a=123时,123这个操作数中,u中的constant是一个IS_LONG类型的zval,其值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; 
Copy after login

0x5: array op_array after opcode compilation

In the first line of the zend_do_print function, we notice the following line of code

zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); 
Copy after login

The opcode generated after the PHP script code is compiled is stored in op_array, and its internal storage structure is as follows

php-5.6.17Zendzend_compile.h

struct _zend_op_array 
{
/* Common elements */
zend_uchar type;
const char *function_name; // 如果是用户定义的函数则,这里将保存函数的名字
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数组
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];
}; 
Copy after login

The compiled opcodes of the entire PHP script code are saved here, and are executed by the following execute function during execution

ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
{
// ... 循环执行op_array中的opcode或者执行其他op_array中的opcode
}
Copy after login

Each opcode has a function pointer field of opcode_handler_t, which is used to execute the opcode. PHP has three ways to process opcode

1. CALL: PHP uses CALL by default, which is the method of function calling
2. SWITCH: Since opcode execution is a frequently required operation for every PHP program, it can be distributed using SWITCH or GOTO
3. GOTO: Usually the efficiency of GOTO is relatively high, but whether the efficiency is improved depends on different CPUs
In fact, we will find that in /zend/zend_language_parser.c is Zend’s opcode translation and interpretation execution process, which includes three opcode execution methods: call, switch, and goto

This is the core principle of why PHP is called an interpreted language. After PHP completes Lex lexical parsing, when the grammar parsing generates productions, it directly calls the zend api through call, switch, and goto for immediate interpretation. Execute

Relevant Link:

http://www.nowamagic.net/librarys/veda/detail/1325
http://php.net/manual/zh/internals2.opcodes.list.php
http://www.nowamagic.net/librarys/veda/detail/1543
http://www.nowamagic.net/librarys/veda/detail/1324
http://www.nowamagic.net/librarys/veda/detail/1543 
http://www.laruence.com/2008/06/18/221.html
http://www.php-internals.com/book/&#63;p=chapt02/02-03-02-opcode 
Copy after login

3. opcode翻译执行(即时解释执行)

Relevant Link:

http://www.php-internals.com/book/&#63;p=chapt02/02-03-03-from-opcode-to-handler
Copy after login

以上所述本文给大家介绍的PHP内核学习教程之php opcode内核实现的相关知识,希望对大家有所帮助。

您可能感兴趣的文章:

  • PHP OPCode缓存 APC详细介绍
  • 理解php原理的opcodes(操作码)
  • 利用PHP扩展vld查看PHP opcode操作步骤
  • 为PHP5.4开启Zend OPCode缓存
  • 如何使用PHP Embed SAPI实现Opcodes查看器

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1096143.htmlTechArticlePHP内核学习教程之php opcode内核实现,内核opcode opcode是计算机指令中的一部分,用于指定要执行的操作, 指令的格式和规范由处理器的指令...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template