PHP 7 Virtual Machine - Nikita Popov

Planet PHP

Guest
This article aims to provide an overview of the Zend Virtual Machine, as it is found in PHP 7. This is not a comprehensive description, but I try to cover most of the important parts, as well as some of the finer details.

This description targets PHP version 7.2 (currently in development), but nearly everything also applies to PHP 7.0/7.1. However, the differences to the PHP 5.x series VM are significant and I will generally not bother to draw parallels.

Most of this post will consider things at the level of instruction listings and only a few sections at the end deal with the actual C level implementation of the VM. However, I do want to provide some links to the main files that make up the VM upfront:

Opcodes


In the beginning, there was the opcode. “Opcode” is how we refer to a full VM instruction (including operands), but may also designate only the “actual” operation code, which is a small integer determining the type of instruction. The intended meaning should be clear from context. In source code, full instructions are usually called “oplines”.

An individual instruction conforms to the following zend_op structure:


struct _zend_op {
const void *handler;
znode_op op1;
znode_op op2;
znode_op result;
uint32_t extended_value;
uint32_t lineno;
zend_uchar opcode;
zend_uchar op1_type;
zend_uchar op2_type;
zend_uchar result_type;
};

As such, opcodes are essentially a “three-address code” instruction format. There is an opcode determining the instruction type, there are two input operands op1 and op2 and one output operand result.

Not all instructions use all operands. An ADD instruction (representing the + operator) will use all three. A BOOL_NOT instruction (representing the ! operator) uses only op1 and result. An ECHO instruction uses only op1. Some instructions may either use or not use an operand. For example DO_FCALL may or may not have a result operand, depending on whether the return value of the function call is used. Some instructions require more than two input operands, in which case they will simply use a second dummy instruction (OP_DATA) to carry additional operands.

Next to these three standard operands, there exists an additional numeric extended_value field, which can be used to hold additional instruction modifiers. For example for a CAST it might contain the target type to cast to.

Each operand has a type, stored in op1_type, op2_type and result_type respectively. The possible types are IS_UNUSED, IS_CONST, IS_TMPVAR, IS_VAR and IS_CV.

The three latter types

Truncated by Planet PHP, read more at the original (another 81113 bytes)

Читать дальше...
 
Сверху