php内核分析(三)-全局变量

本文详细介绍了PHP-7.1.0RC3版本中编译器全局变量CG的具体结构与内容,包括循环变量堆栈、活动类入口、编译文件名等,并概述了全局变量SG与EG的作用及部分字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux

CG

CG是从全局的compiler_global中获取属性值,里面存储的就是编译过程使用到的全局变量。

01 struct _zend_compiler_globals {
02     zend_stack loop_var_stack;
03  
04     zend_class_entry *active_class_entry;
05  
06     zend_string *compiled_filename;
07  
08     int zend_lineno;
09  
10     zend_op_array *active_op_array;
11  
12     HashTable *function_table;    /* function symbol table */
13     HashTable *class_table;        /* class table */
14  
15     HashTable filenames_table;
16  
17     HashTable *auto_globals;
18  
19     zend_bool parse_error;
20     zend_bool in_compilation;
21     zend_bool short_tags;
22  
23     zend_bool unclean_shutdown;
24  
25     zend_bool ini_parser_unbuffered_errors;
26  
27     zend_llist open_files;
28  
29     struct _zend_ini_parser_param *ini_parser_param;
30  
31     uint32_t start_lineno; // 执行文件开始执行的行号
32     zend_bool increment_lineno;
33  
34     zend_string *doc_comment;
35     uint32_t extra_fn_flags;
36  
37     uint32_t compiler_options; /* set of ZEND_COMPILE_* constants */
38  
39     HashTable const_filenames;
40  
41     zend_oparray_context context;
42     zend_file_context file_context;
43  
44     zend_arena *arena;
45  
46     zend_string *empty_string;
47     zend_string *one_char_string[256];
48     zend_string **known_strings;
49     uint32_t    known_strings_count;
50  
51     HashTable interned_strings;
52  
53     const zend_encoding **script_encoding_list;
54     size_t script_encoding_list_size;
55     zend_bool multibyte;
56     zend_bool detect_unicode;
57     zend_bool encoding_declared;
58  
59     zend_ast *ast;
60     zend_arena *ast_arena;
61  
62     zend_stack delayed_oplines_stack;
63  
64 #ifdef ZTS
65     zval **static_members_table;
66     int last_static_member;
67 #endif
68 };

SG

01 SG是从全局的sapi_global中获取属性值
02  
03 // TODO:更新
04 typedef struct _sapi_globals_struct {
05     void *server_context;
06     sapi_request_info request_info; // 请求信息
07     sapi_headers_struct sapi_headers;
08     int64_t read_post_bytes;
09     unsigned char post_read;
10     unsigned char headers_sent;
11     zend_stat_t global_stat;
12     char *default_mimetype;
13     char *default_charset;
14     HashTable *rfc1867_uploaded_files;
15     zend_long post_max_size;
16     int options;
17     zend_bool sapi_started;
18     double global_request_time;
19     HashTable known_post_content_types;
20     zval callback_func;
21     zend_fcall_info_cache fci_cache;
22 } sapi_globals_struct;

EG

EG是从executor_globals中获取变量

01 // TODO: 更新
02 struct _zend_executor_globals {
03     zval uninitialized_zval;
04     zval error_zval;
05  
06     /* symbol table cache */
07     zend_array *symtable_cache[SYMTABLE_CACHE_SIZE];
08     zend_array **symtable_cache_limit;
09     zend_array **symtable_cache_ptr;
10  
11     zend_array symbol_table;        /* main symbol table */
12  
13     HashTable included_files;    /* files already included */
14  
15     JMP_BUF *bailout;
16  
17     int error_reporting;
18     int exit_status;
19  
20     HashTable *function_table;    /* function symbol table */
21     HashTable *class_table;        /* class table */
22     HashTable *zend_constants;    /* constants table */
23  
24     zval          *vm_stack_top;
25     zval          *vm_stack_end;
26     zend_vm_stack  vm_stack;
27  
28     struct _zend_execute_data *current_execute_data;
29     zend_class_entry *fake_scope; /* used to avoid checks accessing properties */
30  
31     zend_long precision;
32  
33     int ticks_count;
34  
35     HashTable *in_autoload;
36     zend_function *autoload_func;
37     zend_bool full_tables_cleanup;
38  
39     /* for extended information support */
40     zend_bool no_extensions;
41  
42     zend_bool vm_interrupt;
43     zend_bool timed_out;
44     zend_long hard_timeout;
45  
46 #ifdef ZEND_WIN32
47     OSVERSIONINFOEX windows_version_info;
48 #endif
49  
50     HashTable regular_list;
51     HashTable persistent_list;
52  
53     int user_error_handler_error_reporting;
54     zval user_error_handler;
55     zval user_exception_handler;
56     zend_stack user_error_handlers_error_reporting;
57     zend_stack user_error_handlers;
58     zend_stack user_exception_handlers;
59  
60     zend_error_handling_t  error_handling;
61     zend_class_entry      *exception_class;
62  
63     /* timeout support */
64     zend_long timeout_seconds;
65  
66     int lambda_count;
67  
68     HashTable *ini_directives;
69     HashTable *modified_ini_directives;
70     zend_ini_entry *error_reporting_ini_entry;
71  
72     zend_objects_store objects_store;
73     zend_object *exception, *prev_exception;
74     const zend_op *opline_before_exception;
75     zend_op exception_op[3];
76  
77     struct _zend_module_entry *current_module;
78  
79     zend_bool active;
80     zend_bool valid_symbol_table;
81  
82     zend_long assertions;
83  
84     uint32_t           ht_iterators_count;     /* number of allocatd slots */
85     uint32_t           ht_iterators_used;      /* number of used slots */
86     HashTableIterator *ht_iterators;
87     HashTableIterator  ht_iterators_slots[16];
88  
89     void *saved_fpu_cw_ptr;
90 #if XPFPA_HAVE_CW
91     XPFPA_CW_DATATYPE saved_fpu_cw;
92 #endif
93  
94     zend_function trampoline;
95     zend_op       call_trampoline_op;
96  
97     void *reserved[ZEND_MAX_RESERVED_RESOURCES];
98 };
资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 行列式是线性代数的核心概念,在求解线性方程组、分析矩阵特性以及几何计算中都极为关键。本教程将讲解如何用C++实现行列式的计算,重点在于如何输出分数形式的结果。 行列式定义如下:对于n阶方阵A=(a_ij),其行列式由主对角线元素的乘积,按行或列的奇偶性赋予正负号后求和得到,记作det(A)。例如,2×2矩阵的行列式为det(A)=a11×a22-a12×a21,而更高阶矩阵的行列式可通过Laplace展开或Sarrus规则递归计算。 在C++中实现行列式计算时,首先需定义矩阵类或结构体,用二维数组存储矩阵元素,并实现初始化、加法、乘法、转置等操作。为支持分数形式输出,需引入分数类,包含分子和分母两个整数,并提供与整数、浮点数的转换以及加、减、乘、除等运算。C++中可借助std::pair表示分数,或自定义结构体并重载运算符。 计算行列式的函数实现上,3×3及以下矩阵可直接按定义计算,更大矩阵可采用Laplace展开或高斯 - 约旦消元法。Laplace展开是沿某行或列展开,将矩阵分解为多个小矩阵的行列式乘积,再递归计算。在处理分数输出时,需注意避免无限循环和除零错误,如在分数运算前先约简,确保分子分母互质,且所有计算基于整数进行,最后再转为浮点数,以避免浮点数误差。 为提升代码可读性和可维护性,建议采用面向对象编程,将矩阵类和分数类封装,每个类有明确功能和接口,便于后续扩展如矩阵求逆、计算特征值等功能。 总结C++实现行列式计算的关键步骤:一是定义矩阵类和分数类;二是实现矩阵基本操作;是设计行列式计算函数;四是用分数类处理精确计算;五是编写测试用例验证程序正确性。通过这些步骤,可构建一个高效准确的行列式计算程序,支持分数形式计算,为C++编程和线性代数应用奠定基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值