Home > Backend Development > PHP Tutorial > Zend opcode是如何被执行的?是要编译为机器码再执行吗?

Zend opcode是如何被执行的?是要编译为机器码再执行吗?

WBOY
Release: 2016-06-06 16:43:22
Original
1905 people have browsed it

php先被Zend引擎编译为opcode,那么opcode又是如何被zend_execute执行的呢?最终是被编译为机器码了吗?具体过程是如何的?

回复内容:

既然题主提到了Zend那么这个问题是特指Zend Engine所实现的PHP,而不指任何其它实现(例如HHVM、Hippy、Quercus或JPHP)。

到PHP 7.0为止,Zend Engine的正式发行的版本从来都是通过解释器实现(*)。Zend的字节码指令一条条进入解释器,一条条字节码指令被其opcode对应的C语言写的函数所执行。就这么简单。

简易工作流程:
[ PHP源码 ]
=> 词法分析 / 语法分析 -> [ 抽象语法树(AST) ]
=> 字节码编译器 -> [ Zend字节码(指令集为 Zend opcodes) ]
=> 字节码解释器 -> [ 程序运行结果 ]

具体解释器是如何从opcode分派到其对应的函数,Zend Engine可以配置使用几种不同的方式:call threading、switch threading、computed goto threading。这些名字分别是什么意思,请参考这篇文章:Threaded Code

以最简单的switch-threading举例,这种解释器的基本形式就是这样的:
<code class="language-c"><span class="k">while</span> <span class="p">(</span><span class="n">TRUE</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">opcode</span> <span class="o">=</span> <span class="o">*</span><span class="n">program_counter</span><span class="p">;</span>
  <span class="k">switch</span> <span class="p">(</span><span class="n">opcode</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">case</span> <span class="nl">ZEND_ADD</span><span class="p">:</span>
    <span class="c1">// execute add ...</span>
    <span class="n">program_counter</span><span class="o">++</span><span class="p">;</span> <span class="c1">// next opcode</span>
    <span class="k">break</span><span class="p">;</span>
  <span class="k">case</span> <span class="nl">ZEND_SUB</span><span class="p">:</span>
    <span class="c1">// execute sub ...</span>
    <span class="n">program_counter</span><span class="o">++</span><span class="p">;</span> <span class="c1">// next opcode</span>
    <span class="k">break</span><span class="p">;</span>
  <span class="c1">// ...</span>
  <span class="p">}</span>
<span class="p">}</span>
</code>
Copy after login
PHP源码 ==(interpreter)==> opcode(PHP: Zend Engine 2 Opcodes,理解成汇编) ==(zend engine)==> 机器码 计算机只能执行机器码,但是并不是把PHP代码直接转为机器码执行,而是转化为一条条的opcode,其实opcode对应的是一个C函数,执行一条opcode就是执行这条opcode对应的C函数,而这个C函数已经被编译为机器码了
Related labels:
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