PHP7 source code: Detailed analysis of PHP virtual machine

不言
Release: 2023-04-03 17:26:01
Original
4504 people have browsed it

The content of this article is about the PHP7 source code: a detailed analysis of the PHP virtual machine. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Starting from the physical machine

Virtual machines are also computers, and their design ideas have many similarities with physical machines;

1.1 Von Neumann Architecture

Von Neumann is the well-deserved father of digital computers. Current computers use the von Neumann architecture; the design ideas mainly include the following Several aspects:

  • Instructions and data are mixed and stored in the same memory without distinction. They are all data in memory. In the protected mode of modern CPUs, each memory segment has a segment descriptor. This descriptor records the access rights (readable, writable, and executable) of this memory segment. This specifies in disguise which memories store instructions and which are data);

  • The memory is a linearly addressed one-dimensional structure that is accessed by address. The number of bits in each unit is fixed;

  • data is expressed in binary;

  • instructions consist of opcodes and operands. The opcode specifies the operation type of this instruction, and the operand specifies the operand itself or the address of the operand. The operand itself has no data type, and its data type is determined by the opcode; any computer architecture will provide a set of instructions to the outside;

  • The operator directly sends out control signals to control the computer by executing instructions. various operations. The instruction counter indicates the memory address where the instruction to be executed is located. There is only one instruction counter, which generally increases sequentially, but the execution order may change due to the operation results or external conditions at the time;

PHP7 source code: Detailed analysis of PHP virtual machine

##1.2 Introduction to assembly language

Computers of any architecture will provide a set of instructions;

Instructions consist of opcodes and operands; opcodes are operation types. The operand can be an immediate number or a storage address; each instruction can have 0, 1 or 2 operands;

instruction is a string of binary; assembly language is the text form of binary instructions;

push   %ebx
mov    %eax, [%esp+8]
mov    %ebx, [%esp+12]
add    %eax, %ebx
pop    %ebx
Copy after login
push, mov, add, pop, etc. are the operation codes;

�x register; [%esp 12] memory address;
The operand is just a storage area where data can be accessed; the operand itself is not There is no data type, and its data type is determined by the operation code;
For example, movb transmits bytes, movw transmits words, movl transmits double words, etc.

1.3 Function call stack

The process (function) is an encapsulation of the code. What is exposed to the outside world is only a set of specified parameters and an optional return value; this function can be called at different places in the program; assuming that process P calls process Q, Q executes Then return to process P; in order to implement this function, three points need to be considered:

  • Instruction jump: When entering process Q, the program counter must be set to the start of Q's code address; when returning, the program counter needs to be set to the address of the instruction after calling Q in P;

  • Data transfer: P can provide one or more parameters to Q, and Q can Return a value to P;

  • Memory allocation and release: When Q starts execution, it may be necessary to allocate memory space for local variables, and before returning, these memory spaces need to be released;

Most language procedure calls use the memory management mechanism provided by the stack data structure; as shown in the following figure:

PHP7 source code: Detailed analysis of PHP virtual machine

The call and return of the function correspond to a series of push and pop operations;

When the function is executed, it will have its own private stack frame, and local variables are allocated on the function's private stack frame. ;
The stack overflow we usually encounter is caused by calling functions that are too deep into the stack;

2.PHP virtual machine

Virtual machine It is also a computer. Referring to the design of a physical machine, when designing a virtual machine, you should first consider three elements: instructions, data storage, and function stack frames;

The following is a detailed analysis of the design ideas of the PHP virtual machine from these three points;

2.1 refers to

##2.1.1 Instruction type

Computers of any architecture need to provide a set of external instructions. It represents a set of operation types supported by the computer;

The PHP virtual machine provides 186 instructions to the outside world, which are defined in the zend_vm_opcodes.h file;

//加、减、乘、除等
#define ZEND_ADD                               1
#define ZEND_SUB                               2
#define ZEND_MUL                               3
#define ZEND_p                               4
#define ZEND_MOD                               5
#define ZEND_SL                                6
#define ZEND_SR                                7
#define ZEND_CONCAT                            8
#define ZEND_BW_OR                             9
#define ZEND_BW_AND                           10
……………………
Copy after login

2.1.2 Instructions

2.1.2.1 Representation of the instruction The instruction consists of an operation code and an operand; the operation code specifies this instruction The operation type, the operand specifies the operand itself or the address of the operand;

PHP virtual machine defines the instruction format as: opcode operand 1 operand 2 return value; it uses the structure _zend_op to represent an instruction :

struct _zend_op {
    const void *handler;    //指针,指向当前指令的执行函数
    znode_op op1;           //操作数1         
    znode_op op2;           //操作数2
    znode_op result;        //返回值
    uint32_t extended_value;//扩展
    uint32_t lineno;        //行号
    zend_uchar opcode;      //指令类型
    zend_uchar op1_type;    //操作数1的类型(此类型并不代表字符串、数组等数据类型;其表示此操作数是常量,临时变量,编译变量等)
    zend_uchar op2_type;    //操作数2的类型
    zend_uchar result_type; //返回值的类型
};
Copy after login

2.1.2.2 Representation of operands

从上面可以看到,操作数使用结构体znode_op表示,定义如下:

constant、var、num等都是uint32_t类型的,这怎么表示一个操作数呢?(既不是指针不能代表地址,也无法表示所有数据类型);
其实,操作数大多情况采用的相对地址表示方式,constant等表示的是相对于执行栈帧首地址的偏移量;
另外,_znode_op结构体中有个zval *zv字段,其也可以表示一个操作数,这个字段是一个指针,指向的是zval结构体,PHP虚拟机支持的所有数据类型都使用zval结构体表示;

typedef union _znode_op {
        uint32_t      constant;
        uint32_t      var;
        uint32_t      num;
        uint32_t      opline_num;
    #if ZEND_USE_ABS_JMP_ADDR
        zend_op       *jmp_addr;
    #else
        uint32_t      jmp_offset;
    #endif
    #if ZEND_USE_ABS_CONST_ADDR
        zval          *zv;
    #endif
} znode_op;
Copy after login

2.2 数据存储

PHP虚拟机支持多种数据类型:整型、浮点型、字符串、数组,对象等;PHP虚拟机如何存储和表示多种数据类型?

2.1.2.2节指出结构体_znode_op代表一个操作数;操作数可以是一个偏移量(计算得到一个地址,即zval结构体的首地址),或者一个zval指针;PHP虚拟机使用zval结构体表示和存储多种数据;

struct _zval_struct {
    zend_value        value;            //存储实际的value值
    union {
        struct {                        //一些标志位
            ZEND_ENDIAN_LOHI_4(
                zend_uchar    type,         //重要;表示变量类型
                zend_uchar    type_flags,
                zend_uchar    const_flags,
                zend_uchar    reserved)     /* call info for EX(This) */
        } v;
        uint32_t type_info;
    } u1;
    union {                                 //其他有用信息
        uint32_t     next;                 /* hash collision chain */
        uint32_t     cache_slot;           /* literal cache slot */
        uint32_t     lineno;               /* line number (for ast nodes) */
        uint32_t     num_args;             /* arguments number for EX(This) */
        uint32_t     fe_pos;               /* foreach position */
        uint32_t     fe_iter_idx;          /* foreach iterator index */
        uint32_t     access_flags;         /* class constant access flags */
        uint32_t     property_guard;       /* single property guard */
    } u2;
};
Copy after login

zval.u1.type表示数据类型, zend_types.h文件定义了以下类型:

#define IS_UNDEF                    0
#define IS_NULL                     1
#define IS_FALSE                    2
#define IS_TRUE                     3
#define IS_LONG                     4
#define IS_DOUBLE                   5
#define IS_STRING                   6
#define IS_ARRAY                    7
#define IS_OBJECT                   8
#define IS_RESOURCE                 9
#define IS_REFERENCE                10
…………
Copy after login

zend_value存储具体的数据内容,结构体定义如下:

_zend_value占16字节内存;long、double类型会直接存储在结构体;引用、字符串、数组等类型使用指针存储;

代码中根据zval.u1.type字段,判断数据类型,以此决定操作_zend_value结构体哪个字段;

可以看出,字符串使用zend_string表示,数组使用zend_array表示…

typedef union _zend_value {
    zend_long         lval;            
    double            dval;            
    zend_refcounted  *counted;
    zend_string      *str;
    zend_array       *arr;
    zend_object      *obj;
    zend_resource    *res;
    zend_reference   *ref;
    zend_ast_ref     *ast;
    zval             *zv;
    void             *ptr;
    zend_class_entry *ce;
    zend_function    *func;
    struct {
        uint32_t w1;
        uint32_t w2;
    } ww;
} zend_value;
Copy after login

如下图为PHP7中字符串结构图:

PHP7 source code: Detailed analysis of PHP virtual machine

2.3 再谈指令

2.1.2.1指出,指令使用结构体_zend_op表示;其中最主要2个属性:操作函数,操作数(两个操作数和一个返回值);

操作数的类型(常量、临时变量等)不同,同一个指令对应的handler函数也会不同;操作数类型定义在 Zend/zend_compile.h文件:

//常量
#define IS_CONST    (1<p>操作函数命名规则为:ZEND_[opcode]_SPEC_(操作数1类型)_(操作数2类型)_(返回值类型)_HANDLER</p><p>比如赋值语句就有以下多种操作函数:</p><pre class="brush:php;toolbar:false">ZEND_ASSIGN_SPEC_VAR_CONST_RETVAL_UNUSED_HANDLER,
ZEND_ASSIGN_SPEC_VAR_TMP_RETVAL_UNUSED_HANDLER,
ZEND_ASSIGN_SPEC_VAR_VAR_RETVAL_UNUSED_HANDLER,
ZEND_ASSIGN_SPEC_VAR_CV_RETVAL_UNUSED_HANDLER,
…
Copy after login

对于$a=1,其操作函数为: ZEND_ASSIGN_SPEC_CV_CONST_RETVAL_UNUSED_HANDLER;函数实现为:

static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ASSIGN_SPEC_CV_CONST_RETVAL_UNUSED_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
    USE_OPLINE
 
    zval *value;
    zval *variable_ptr;
 
    SAVE_OPLINE();
    //获取op2对应的值,也就是1
    value = EX_CONSTANT(opline->op2);
    //在execute_data中获取op1的位置,也就是$a(execute_data类似函数栈帧,后面详细分析)
    variable_ptr = _get_zval_ptr_cv_undef_BP_VAR_W(execute_data, opline->op1.var);
     
    //赋值
    value = zend_assign_to_variable(variable_ptr, value, IS_CONST);
    if (UNEXPECTED(0)) {
        ZVAL_COPY(EX_VAR(opline->result.var), value);
    }
 
    ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION();
}
Copy after login

2.4 函数栈帧

2.4.1指令集

上面分析了指令的结构与表示,PHP虚拟机使用_zend_op_array表示指令的集合:

struct _zend_op_array {
    …………
    //last表示指令总数;opcodes为存储指令的数组;
    uint32_t last;
    zend_op *opcodes;
    //变量类型为IS_CV的个数
    int last_var;
    //变量类型为IS_VAR和IS_TEMP_VAR的个数
    uint32_t T;
    //存放IS_CV类型变量的数组
    zend_string **vars;
 
    …………
     
    //静态变量
    HashTable *static_variables;
 
    //常量个数;常量数组
    int last_literal;
    zval *literals;
 
    …
};
Copy after login

注意: last_var代表IS_CV类型变量的个数,这种类型变量存放在vars数组中;在整个编译过程中,每次遇到一个IS_CV类型的变量(类似于$something),就会去遍历vars数组,检查是否已经存在,如果不存在,则插入到vars中,并将last_var的值设置为该变量的操作数;如果存在,则使用之前分配的操作数

2.4.2 函数栈帧

PHP虚拟机实现了与1.3节物理机类似的函数栈帧结构;

使用 _zend_vm_stack表示栈结构;多个栈之间使用prev字段形成单向链表;top和end指向栈低和栈顶,分别为zval类型的指针;

struct _zend_vm_stack {
    zval *top;
    zval *end;
    zend_vm_stack prev;
};
Copy after login

考虑如何设计函数执行时候的帧结构:当前函数执行时,需要存储函数编译后的指令,需要存储函数内部的局部变量等(2.1.2.2节指出,操作数使用结构体znode_op表示,其内部使用uint32_t表示操作数,此时表示的就是当前zval变量相对于当前函数栈帧首地址的偏移量);

PHP虚拟机使用结构体_zend_execute_data存储当前函数执行所需数据;

struct _zend_execute_data {
    //当前指令指令
    const zend_op       *opline; 
    //当前函数执行栈帧
    zend_execute_data   *call; 
    //函数返回数据          
    zval                *return_value;
    zend_function       *func;            
    zval                 This;      /* this + call_info + num_args */
    //调用当前函数的栈帧       
    zend_execute_data   *prev_execute_data;
    //符号表
    zend_array          *symbol_table;
#if ZEND_EX_USE_RUN_TIME_CACHE
    void               **run_time_cache;  
#endif
#if ZEND_EX_USE_LITERALS
    //常量数组
    zval                *literals;        
#endif
};
Copy after login

函数开始执行时,需要为函数分配相应的函数栈帧并入栈,代码如下:

static zend_always_inline zend_execute_data *zend_vm_stack_push_call_frame(uint32_t call_info, zend_function *func, uint32_t num_args, zend_class_entry *called_scope, zend_object *object)
{
    //计算当前函数栈帧需要内存空间大小
    uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
 
    //根据栈帧大小分配空间,入栈
    return zend_vm_stack_push_call_frame_ex(used_stack, call_info,
        func, num_args, called_scope, object);
}
 
//计算函数栈帧大小
static zend_always_inline uint32_t zend_vm_calc_used_stack(uint32_t num_args, zend_function *func)
{
    //_zend_execute_data大小(80字节/16字节=5)+参数数目
    uint32_t used_stack = ZEND_CALL_FRAME_SLOT + num_args;
 
    if (EXPECTED(ZEND_USER_CODE(func->type))) {
        //当前函数临时变量等数目
        used_stack += func->op_array.last_var + func->op_array.T - MIN(func->op_array.num_args, num_args);
    }
 
    //乘以16字节
    return used_stack * sizeof(zval);
}
 
//入栈
static zend_always_inline zend_execute_data *zend_vm_stack_push_call_frame_ex(uint32_t used_stack, uint32_t call_info, zend_function *func, uint32_t num_args, zend_class_entry *called_scope, zend_object *object)
{
    //上一个函数栈帧地址
    zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
 
    //移动函数调用栈top指针
    EG(vm_stack_top) = (zval*)((char*)call + used_stack);
    //初始化当前函数栈帧
    zend_vm_init_call_frame(call, call_info, func, num_args, called_scope, object);
    //返回当前函数栈帧首地址
    return call;
}
Copy after login

从上面分析可以得到函数栈帧结构图如下所示:

PHP7 source code: Detailed analysis of PHP virtual machine

总结

PHP虚拟机也是计算机,有三点是我们需要重点关注的:指令集(包含指令处理函数)、数据存储(zval)、函数栈帧;

此时虚拟机已可以接受指令并执行指令代码;

但是,PHP虚拟机是专用执行PHP代码的,PHP代码如何能转换为PHP虚拟机可以识别的指令呢——编译;

PHP虚拟机同时提供了编译器,可以将PHP代码转换为其可以识别的指令集合;

理论上你可以自定义任何语言,只要实现编译器,能够将你自己的语言转换为PHP可以识别的指令代码,就能被PHP虚拟机执行;

Recommended related articles:

Summary of the new syntax features in PHP7.0 and php7.1

How to store session in the database in PHP And use (with code)

Explanation of the principle of the time function strtotime() function in PHP

The above is the detailed content of PHP7 source code: Detailed analysis of PHP virtual machine. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!