您的位置:首页 > 编程语言 > PHP开发

PHP内核之opcode解读

2016-08-26 13:33 106 查看
我们通过示例来说吧

<?php

echo '1'.print(2)+3;
exit;

?>
得出的opcode如图:



行号、指令编号、脚本开始标记、结束标记、ZEND VM指令、返回值、ZEND VM指令对应的参数。

ZEND VM执行opcode

struct _zend_op {
opcode_handler_t handler; // 执行该opcode时调用的处理函数
znode result;
znode op1;
znode op2;
ulong extended_value;
uint lineno;
zend_uchar opcode; // opcode代码
};

struct _zend_op_array {
/* Common elements */
zend_uchar type;
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;
zend_bool pass_rest_by_reference;
unsigned char return_reference;
/* END of common elements */

zend_bool done_pass_two;

zend_uint *refcount;

zend_op *opcodes;
zend_uint last, size;

zend_compiled_variable *vars;
int last_var, size_var;

zend_uint T;

zend_brk_cont_element *brk_cont_array;
int last_brk_cont;
int current_brk_cont;

zend_try_catch_element *try_catch_array;
int last_try_catch;

/* static variables support */
HashTable *static_variables;

zend_op *start_op;
int backpatch_count;

zend_uint this_var;

char *filename;
zend_uint line_start;
zend_uint line_end;
char *doc_comment;
zend_uint doc_comment_len;
zend_uint early_binding; /* the linked list of delayed declarations */

void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};

ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
{
// ... 循环执行op_array中的opcode或者执行其他op_array中的opcode
}
实际上我们编写的PHP,最终解析成ZEND VM中的指令集,最终通过ZEND VM返回结果。

每一条指令,都可以找到对用的函数执行,例如ECHO指令对应zend_do_echo。更多的可以查看Zend/compile.h

更多指令参见:http://php.net/manual/en/internals2.opcodes.list.php,同时列出了每条指令的案例
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opcode