php 抽象语法树,【PHP7源码学习】抽象语法树编译过程的验证

grape

引入

此篇文章是针对于$a=1进行了gdb实战调试,以验证 【PHP源码学习】2019-03-22 AST的遍历 中的一些论证。

实验代码:

$a =1;

基本流程:

- zend_compile_top_stmt(CG(ast))

- zend_compile_stmt(ast)

- zend_compile_expr(&result, ast);

- zend_compile_assign(result, ast);

- zend_delayed_compile_begin();

- zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W);

- zend_compile_expr(&expr_node, expr_ast);

- zend_delayed_compile_end(offset);

- zend_emit_op(result, ZEND_ASSIGN, &var_node, &expr_node);

GDB过程

我们来gdb一下整个过程,首先,在zend_compile_top_stmt入口处打断点:

bVbsLsM?w=1099&h=212

gdb下来我们进入:

bVbsLtG?w=884&h=341

在zend_compile_stmt我们进入default的zend_compile_expr函数:

bVbsLuR?w=816&h=106

bVbsLu7?w=793&h=174

因为我们是赋值运算,我们此时走到了assign函数,接下来就是整个编译过程中的重点部分:

bVbsLvO?w=979&h=203

打印var_ast->kind:

bVbsLv2?w=509&h=67

可以看出我们接下来要走的就是上图的几个函数,那么,这几个函数又是怎么走的呢?我们接着看,首先,进入zend_delayed_compile_begin():

bVbsLwW?w=905&h=112

这个函数的作用是取栈顶,然后在函数结束后赋值给offest,那我们看看这个offest是什么?

等号左边$a的编译

接下来进入$a的处理函数:

bVbsLye?w=975&h=64

在这里记录下我们处理$a过程中调用的函数:

zend_delayed_compile_var (result=0x7fffffffa580, ast=0x7ffff5e7f060, type=1)

zend_compile_simple_var (result=0x7fffffffa580, ast=0x7ffff5e7f060, type=1, delayed=1)

zend_try_compile_cv (result=0x7fffffffa580, ast=0x7ffff5e7f060)

lookup_cv (op_array=0x7ffff5e75460, name=0x7ffff5e5e4e0)

lookup_cv函数的作用是什么呢?首先我们先看lookcv的源代码:

static int lookup_cv(zend_op_array *op_array, zend_string* name) /* {{{ */{

int i = 0;

zend_ulong hash_value = zend_string_hash_val(name);

while (i < op_array->last_var) {

if (ZSTR_VAL(op_array->vars[i]) == ZSTR_VAL(name) ||

(ZSTR_H(op_array->vars[i]) == hash_value &&

ZSTR_LEN(op_array->vars[i]) == ZSTR_LEN(name) &&

memcmp(ZSTR_VAL(op_array->vars[i]), ZSTR_VAL(name), ZSTR_LEN(name)) == 0)) {

zend_string_release(name);

return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);

}

i++;

}

i = op_array->last_var;

op_array->last_var++;

if (op_array->last_var > CG(context).vars_size) {

CG(context).vars_size += 16; /* FIXME */

op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));

}

op_array->vars[i] = zend_new_interned_string(name);

return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);

}

我们发现,lookup_cv()函数它返回一个int类型的地址,是sizeof(zval)的整数倍,通过它可以得到每个变量的偏移量(80 + 16 * i),i是变量的编号。这样就规定了运行时在栈上相对于zend_execute_data的偏移量,从而在栈上方便地存储了$a这个变量。而$a在zend_op_array的vars数组上也存了一份,这样如果后面又用到了$a的话,直接去zend_op_array的vars数组中查找找,如果存在,那么直接使用之前的编号i,如果不存在则按序分配一个编号,然后再插入zend_op_array的vars数组,节省了分配编号的时间。

另外,在zend_try_compile_cv这个函数中对于result进行赋值,那么我们打印下result的值:

bVbsLG5?w=1087&h=208

我们发现,op_type=16(IS_CV),u.op.var=80

到此我们总结一下$a这个过程,核心函数lookupcv,在lookupcv中我们将变量存储在op_array->vars中,并且返回一个int型整数,代表着偏移量。随后在zend_try_compile_cv中将op_type和u.op.var赋值给znode *result,具体编译示例图如下图所示:

bVbsLIV?w=800&h=534

到此,$a即左子节点就结束了。

等号右边1的编译

接下来我们来进行右子树的处理,gdb如图:

bVbsLJG?w=793&h=91

右子树的处理比较简单,其调用函数为:

zend_compile_expr

ZVAL_COPY(z, v)

重点函数在于ZVAL_COPY这个宏,首先我们看gdb中到达了这个宏:

bVbsLKr?w=975&h=71

然后我们继续分析这个宏的作用,老规矩,先贴源码:

#define ZVAL_COPY(z, v) \

do { \

zval *_z1 = (z); \

const zval *_z2 = (v); \

zend_refcounted *_gc = Z_COUNTED_P(_z2); \

uint32_t _t = Z_TYPE_INFO_P(_z2); \

ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t); \

if ((_t & (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT)) != 0) { \

GC_REFCOUNT(_gc)++; \

} \

} while (0)

它的功能是把一个zval(v)拷贝到另外一个zval(z)中,具体的一些分析请查看上一篇文章:【PHP源码学习】2019-03-22 AST的遍历-1

在进行复制完之后我们对于result进行打印:

bVbsLL9?w=1049&h=154

我们可以看到已经完成了赋值。

最后进行了 result->op_type = IS_CONST,op_type的赋值:

bVbsLMs?w=844&h=28

重新打印result即最终的结果:

bVbsLMM?w=1077&h=196

至此,$a和1都分别存在了两个znode中。下边开始生成指令。

根据assign以及op1,op2生成opline

我们进入到zend_emit_op函数中,这个函数中会生成opcode:

bVbsLNN?w=971&h=73

首先我们看这个函数所执行的所有指令:

bVbsLOi?w=982&h=521

其中,set_node出现的频次很高,我们来看一下它究竟有什么用:

#define SET_NODE(target, src) do { \

target ## _type = (src)->op_type; \

if ((src)->op_type == IS_CONST) { \

target.constant = zend_add_literal(CG(active_op_array), &(src)->u.constant); \

} else { \

target = (src)->u.op; \

} \

} while (0)

从代码中可以看出,对于操作数1,会将编译过程中临时的结构znode传递给zend_op中,对于操作数2,因为是常量(IS_CONST),会调用zend_add_literal将其插入到op_array->literals中。

接下来我们进行返回值的设置,此时会调用zend_make_var_result这个函数:

static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */

{

opline->result_type = IS_VAR; //返回值的类型设置为IS_VAR

opline->result.var = get_temporary_variable(CG(active_op_array)); //这个是返回值的编号,对应T位置

GET_NODE(result, opline->result);

}

static uint32_t get_temporary_variable(zend_op_array *op_array) /* {{{ */

{

return (uint32_t)op_array->T++;

}

返回值的类型为IS_VAR,result.var为T的值

最后打印opline看一下最终的结果:

bVbsLQ0?w=1113&h=118

,下面我们给出Assign操作对应的指令图,如图所示:

bVbsLQ4?w=800&h=549

从图中可以看出,生成的opline中opcode等于38;op1的类型为IS_CV,op1.var对应的是vm_stack上的偏移量;op2的类型为IS_CONST,op2.constant对应的是op_array中literals数组的下标;result的类型为IS_VAR,result.var对应的是T的值;此时handler的值为空。

到此,编译阶段告一段落。

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值