How is Zend opcode executed? Do you want to compile the machine code and execute it?

Source: Internet
Author: User
Keywords opcode zend execute php
PHP was first compiled by the Zend engine to opcode, then how is opcode executed by zend_execute? Is it finally compiled into machine code? How is the process?

Reply content:

Now that the title mentions Zend, the problem is specifically PHP Zend engine implementation, not any other implementations (such as HHVM, hippy, Quercus, or jphp).

By the end of PHP 7.0, the official release of Zend engine was always implemented via an interpreter (*). Zend Bytecode instructions are entered into the interpreter, and a byte code instruction is executed by its opcode corresponding C-language function. It's so simple.

Simple Work Flow:
[PHP Source code]
= = Lexical Analysis /Syntax Analysis --[Abstract Syntax tree (AST) ]
= = byte code compiler , [Zend bytecode (instruction set is Zend opcodes ) ]
= = byte Code interpreter --[Program run result]

How the specific interpreter is dispatched from opcode to its corresponding function, Zend engine can be configured to use several different ways: call threading, switch threading, computed goto threading. What do these names mean, please refer to this article: threaded Code

With the simplest switch-threading example, the basic form of this interpreter is this:
 while (TRUE) {  int opcode = *Program_counter;  Switch (opcode) {   Case Zend_add:    //Execute add ...    Program_counter++; //Next opcode     Break;   Case zend_sub:    //execute sub ...    Program_counter++; //Next opcode     Break;  // ...  }}
PHP Source = = (interpreter) ==> opcode (php:zend Engine 2 opcodes , understood as a compilation) = = (Zend Engine) ==> machine code computer can only execute machine code, but not the PHP code directly to the machine code execution, but instead into a line of opcode, in fact, opcode corresponds to a C function, Executing a opcode is to execute this opcode corresponding C function, and this C function has been compiled into machine code.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.