php 字节码 虚拟机,PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述...

前言

本文简要介绍 zend 虚拟机解释执行字节码的基本逻辑以及相关的数据结构,关于 PHP 源代码的下载,编译,调试可以参考之前的系列文章

execute_ex

我们来看看执行一个简单的脚本 test.php 的调用栈

execute_ex @ zend_vm_execute.h : 411

zend_execute @ zend_vm_execute.h : 474

php_execute_script @ zend.c : 1474

do_cli @ php_cli.c : 993

main @ php_cli.c : 1381

由于是执行脚本文件,所以 do_cli 调用了 php_execute_script 函数,最终调用 execute_ex 函数:

ZEND_API void execute_ex(zend_execute_data *ex)

{

DCL_OPLINE

#ifdef ZEND_VM_IP_GLOBAL_REG

const zend_op *orig_opline = opline;

#endif

#ifdef ZEND_VM_FP_GLOBAL_REG

zend_execute_data *orig_execute_data = execute_data;

execute_data = ex;

#else

zend_execute_data *execute_data = ex;

#endif

LOAD_OPLINE();

ZEND_VM_LOOP_INTERRUPT_CHECK();

while (1) {

#if !defined(ZEND_VM_FP_GLOBAL_REG) || !defined(ZEND_VM_IP_GLOBAL_REG)

int ret;

#endif

#if defined(ZEND_VM_FP_GLOBAL_REG) && defined(ZEND_VM_IP_GLOBAL_REG)

((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);

if (UNEXPECTED(!OPLINE)) {

#else

if (UNEXPECTED((ret = ((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)) != 0)) {

#endif

#ifdef ZEND_VM_FP_GLOBAL_REG

execute_data = orig_execute_data;

# ifdef ZEND_VM_IP_GLOBAL_REG

opline = orig_opline;

# endif

return;

#else

if (EXPECTED(ret > 0)) {

execute_data = EG(current_execute_data);

ZEND_VM_LOOP_INTERRUPT_CHECK();

} else {

# ifdef ZEND_VM_IP_GLOBAL_REG

opline = orig_opline;

# endif

return;

}

#endif

}

}

zend_error_noreturn(E_CORE_ERROR, "Arrived at end of main loop which shouldn't happen");

}

和其它 C 语言编写的系统软件类似,函数中使用了大量的宏定义,通过宏定义的名字还是能大概看出其用途

DCL_OPLINE,变量声明

LOAD_OPLINE(),加载指令字节码

ZEND_VM_LOOP_INTERRUPT_CHECK(),interrupt 检测

while (1) 循环,调用指令的处理函数 OPLINE->handler

op_code_handler

总结

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值