Home> php教程> PHP开发> body text

PHP kernel analysis (8) - zend_compile

黄舟
Release: 2016-12-19 11:30:57
Original
1668 people have browsed it

Abstract: The php version read here is PHP-7.1.0 RC3, and the platform for reading the code is linux. Go back to the zend_eval_stringlZEND_API that we read before int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name) /* {{ { */ { ... new_o ...

The php version read here is PHP-7.1.0 RC3, and the platform for reading the code is linux

Go back to the zend_eval_stringl we looked at before

01 ZEND_API int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name) /* {{{ */ 02 { 03 ... 04 new_op_array = zend_compile_string(&pv, string_name); // 这个是把php代码编译成为opcode的过程 05 ... 06 zend_execute(new_op_array, &local_retval); // 这个是具体的执行过程,执行opcode,把结果存储到local_retval中 07 ... 08 retval = SUCCESS; 09 return retval; 10 }
Copy after login

The zend_execute here performs two steps , the first step is the process of compiling and parsing php into opcode, we will look at this first.

zend_compile_string

zend_compile_string function can be traced to compile_string

01 // 将一个字符串解析成为op_array 02 zend_op_array *compile_string(zval *source_string, char *filename) 03 { 04 zend_lex_state original_lex_state; 05 zend_op_array *op_array = NULL; 06 zval tmp; 07 08 // 如果传进来要解析的字符为空,则返回null 09 if (Z_STRLEN_P(source_string)==0) { 10 return NULL; 11 } 12 13 ZVAL_DUP(&tmp, source_string); // 复制source_string到zval中 14 convert_to_string(&tmp); // 如果不是字符类型就转换为字符类型 15 source_string = &tmp; 16 17 zend_save_lexical_state(&original_lex_state); // 保存lex上下文 18 if (zend_prepare_string_for_scanning(source_string, filename) == SUCCESS) { // 做编译前的准备 19 BEGIN(ST_IN_SCRIPTING); // 设置状态为正在编译 20 op_array = zend_compile(ZEND_EVAL_CODE); // 进行编译,并把生成结果放在op_array中 21 } 22 23 zend_restore_lexical_state(&original_lex_state); // 恢复lex上下文 24 zval_dtor(&tmp); // 释放tmp 25 26 return op_array; 27 }
Copy after login

The core one is zend_compile. Here are a few points to look at:

1 To do type conversion, refer to convert_to_string. This function is to convert any type of value into the string type of zval.
2 zval_dtor, this function can recycle any zval variable. perfectly worked.
3 ZVAL_DUP is for copying. The difference between it and ZVAL_COPY is whether to increase the reference count of gc.

The above is the content of PHP kernel analysis (8) - zend_compile. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


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 Recommendations
    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!