Home > Article > Backend Development > Detailed explanation of CG and EG macros in PHP7 source code
When reading PHP source code, you will encounter many macros. If you don't understand the meaning of these macros, it will cause a lot of trouble in understanding the code. Now let's take a look at the meaning of the two macros CG and EG.
CG
Meaning
The meaning of CG is compiler_globals. Zend compiler related global variables.
Function
Everyone knows that PHP code is eventually converted into Opcode for execution. Some information needs to be saved during the conversion from PHP to Opcode. This information is stored in CG global variables.
If you want to know how Zend converts PHP code to Opcode and uses GC global variables to save that information, you can view the compile_file(zend_file_handle *file_handle, int type) method of the Zend/zend_language_scanner.c file
Code
There is the relevant code for this macro in the Zend/zend_globals_macros.h file. As follows:
/* Compiler */ #ifdef ZTS <h1>define CG(v) ZEND_TSRMG(compiler_globals_id, zend_compiler_globals *, v)</h1> #else <h1>define CG(v) (compiler_globals.v)</h1> extern ZEND_API struct _zend_compiler_globals compiler_globals; #endif
EG
Meaning
The meaning of EG is executor_globals. Global variables related to Zend executor.
Function
When the Zend engine executes Opcode, it needs to record some status during the execution. For example, the scope of the currently executing class, which files are currently loaded, etc.
Code
There is the relevant code for this macro in the Zend/zend_globals_macros.h file. As follows:
/* Executor */ #ifdef ZTS <h1>define EG(v) ZEND_TSRMG(executor_globals_id, zend_executor_globals *, v)</h1> #else <h1>define EG(v) (executor_globals.v)</h1> extern ZEND_API zend_executor_globals executor_globals;
Others
EG and CG share some data. For example, function_table (storage method information), class_table (storage class information).
The relevant code is found in the init_executor method of Zend/zend_execute_API.c as follows:
void init_executor(void) /* {{{ */ { zend_init_fpu(); ...... EG(function_table) = CG(function_table); EG(class_table) = CG(class_table); ...... }
The above is the detailed content of Detailed explanation of CG and EG macros in PHP7 source code. For more information, please follow other related articles on the PHP Chinese website!