The lexical analysis and grammatical analysis of PHP are mostly handled by Flex/bison.
When parsing is complete, the Zend engine generates intermediate code (which can be omitted by using Opcache) opcode,php is built on Zend virtual machines (Zend VMs). PHP opcode is the instruction in the Zend virtual machine.
Inside the PHP implementation, opcode is represented by the following structure:
struct _ZEND_OP {
opcode_handler_t handler; handler function to invoke when executing the opcode
Znode result;
Znode OP1;
Znode OP2;
ULONG Extended_value;
UINT Lineno;
Zend_uchar opcode; OpCode code
};
It is known that the so-called opcode is the Zend of a fictional C structure with a set of instructions (about more than 130)
For example, the following code is a function that compiles when the compiler encounters a print statement:
void Zend_do_print (Znode *result,const znode *arg tsrmls_dc)
{
Zend_op *opline = Get_next_op (CG (Active_op_array) tsrmls_cc);
Opline->result.op_type = Is_tmp_var;
Opline->result.u.var = get_temporary_variable (CG (Active_op_array));
Opline->opcode = Zend_print;
OPLINE->OP1 = *arg;
Set_unused (OPLINE->OP2);
*result = opline->result;
}
This function creates a new ZEND_OP, sets the type of the return value to a temporary variable (Is_tmp_var), and applies for the temporary variable
Space, then specifies opcode as Zend_print, and assigns the passed in parameter to the first operand of the opcode. Such
At the end of this opcode, the Zend engine can get enough information to output the content. (Essentially, Zend parses the print statement by Zend_do_print function to generate the intermediate code opcode)
The opcode is stored in the op_array structure and executed opcode code in the order of the Execute function.
PHP Intermediate Code opcode