mirror of
https://github.com/php/php-src.git
synced 2026-03-26 17:22:15 +01:00
See https://wiki.php.net/rfc/expectations Squashed commit of the following: commit3f3651a787Author: Dmitry Stogov <dmitry@zend.com> Date: Mon Mar 2 11:56:33 2015 +0300 opcode 137 is used for ZEND_OP_DATA and con't be reused for ZEND_ASSERT_CHECK commitca8ecabf2aMerge:24328ac9dac923Author: Dmitry Stogov <dmitry@zend.com> Date: Mon Mar 2 10:49:23 2015 +0300 Merge branch 'master' into assert * master: Update NEWS Fixed bug #69139 (Crash in gc_zval_possible_root on unserialize) windows only test Align entries format Align entries format for 5.6.7 Align entries format for 5.5.23 Bump header year Fixed bug #69144 (strtr not replacing with partly matching replace pairs) Fixed test? Revert mktime()/gmmktime()'s arginfo Update NEWS Fixed bug #69141 Missing arguments in reflection info for some builtin functions Add NEWS entry Remove useless date warning Fix ARG_INFO for levenshtein Fix ARG_INFO for levenshtein fix dir separator in tests Update NEWS Fixed bug #69085 (SoapClient's __call() type confusion through unserialize()). commit24328ac03fMerge:021fd941cdee9aAuthor: Dmitry Stogov <dmitry@zend.com> Date: Fri Feb 27 15:57:13 2015 +0300 Merge branch 'master' into assert * master: Fixed C++ support Fixed bug #69115 crash in mail Reorder Update NEWs Fixed bug #69121 (Segfault in get_current_user when script owner is not in passwd with ZTS build) Update News Fixed bug #69125 (Array numeric string as key) fix bug#68942's patch Fixed ability to build unspecialized executor Fixed bug #69124 (method name could not be used when by ref) Fixed a bug that header value is not terminated by '\0' when accessed through getenv(). Fixed a bug that header value is not terminated by '\0' when accessed through getenv(). commit021fd94ed1Merge:49963ebace1f82Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Feb 26 11:26:03 2015 +0300 Merge branch 'master' into assert * master: (59 commits) Improved ASSIGN_DIM handler Don't inline slow path Revert a part committted by mistake Fixed compilation warnings Fixed a bug that header value is not terminated by '\0' when accessed through getenv(). better name Improve fix for #69038 Update NEWs Fixed bug #69108 ("Segmentation fault" when (de)serializing SplObjectStorage) Added specialized versions of DO_FCALL handler: DO_ICALL - for internal functions DO_UCALL - for user functions DO_FCALL_BY_NAME - plain, most probably user, funcstions (not methods) Use cache_slot offsets instead of indexes (simplify run-time instructions) Split INIT_FCALL_BY_NAME inti INIT_FCALL_BY_NAME(CONST+STRING) and INIT_DYNAMIC_CALL(CONST-STRING|TMPVAR|CV) Support list($a, $b) = $a Avoid unnecassary check Make zend_array_destroy() to free the corresponding zend_array Eliminate check on the fast path Make current() and key() receive argument by value. Use Firebird default home folder, replace Interbase with Firebird Updated NEWS updated NEWS ... Conflicts: Zend/zend_vm_execute.h Zend/zend_vm_opcodes.c Zend/zend_vm_opcodes.h commit49963ebf9dMerge:07b1f926b77959Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Feb 19 11:13:08 2015 +0300 Merge branch 'master' into assert * master: Implemented AST pretty-printer update NEWS to match the actual stuff in 5.6.6 update NEWS to match the actual stuff in 5.5.22 update NEWS(add missing entry for the enchant fix, and reorder the entries a bit) fix typo in bug# update NEWS fix email format update NEWS update 5.6.6 release date in NEWS Fix bug #69033 (Request may get env. variables from previous requests if PHP works as FastCGI) BFN fix test fix test fix test Fixed bug #65593 (Segfault when calling ob_start from output buffering callback) Updated NEWS add CVE 5.4.39 next Fix associativity to match Perl Blast off to space. Conflicts: Zend/zend_ast.c commit07b1f92ed6Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 23:06:32 2015 +0300 fixed pretty-printer (support for "elseif") commit5a976c8d85Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 19:50:08 2015 +0300 Fixed vaeious ptetty-printer issues commit69491e8e8eMerge:84731573ddc246Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 10:18:32 2015 +0300 Merge branch 'master' into assert * master: Set PHP_JSON_VERSION to 1.4.0 Remove unnecessary resource checks in openssl ext JSON is now maintained commit8473157fbbAuthor: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 10:17:26 2015 +0300 Fixed typo and white spaces commit96de5ffc8dAuthor: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 00:28:39 2015 +0300 Fixed assert() in namesapaces commit5eba069c28Merge:4a2d9c0d428bf2Author: Dmitry Stogov <dmitry@zend.com> Date: Tue Feb 17 22:45:55 2015 +0300 Merge branch 'master' into assert * master: (25 commits) improve debugability in TS debug builds More UPGRADING, in particular on foreach Fixed bug #69038 (switch(SOMECONSTANT) misbehaves) for master Replace var is introduced abstain from using xmlCleanupParser fix TS build Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone) update news Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone) Port for for bug #68552 Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone) Update NEWS Fixed bug #69038 (switch(SOMECONSTANT) misbehaves) - BFN Don't read the local php.ini when Generating Phar When building phar shared, you can end up loading a previous phar.so that isn't compatible with the php cli being used to generate Phar here. - Fixed bug #67827 (broken detection of system crypt sha256/sha512 support) Delete json outdated package.xml made ZEND_TSRMLS_CACHE_* macros look like function calls - Fix merge - Fixed bug #67427 (SoapServer cannot handle large messages) patch by: brandt at docoloc dot de ... commit4a2d9c0953Author: Dmitry Stogov <dmitry@zend.com> Date: Tue Feb 17 22:45:10 2015 +0300 Implemented AST pretty-printer to capture expression passed to assert() commit7a059b66d5Merge:9973df73892ebaAuthor: Dmitry Stogov <dmitry@zend.com> Date: Mon Feb 16 18:42:28 2015 +0300 Merge branch 'expect' of github.com:krakjoe/php-src into assert * 'expect' of github.com:krakjoe/php-src: import expect Conflicts: Zend/zend_compile.c Zend/zend_execute_API.c Zend/zend_globals.h Zend/zend_vm_def.h Zend/zend_vm_execute.h Zend/zend_vm_opcodes.c Zend/zend_vm_opcodes.h ext/opcache/Optimizer/block_pass.c ext/opcache/Optimizer/pass1_5.c ext/standard/assert.c ext/standard/tests/assert/assert_error3.phpt commit3892eba2bfAuthor: krakjoe <joe.watkins@live.co.uk> Date: Sun Feb 2 12:49:35 2014 +0000 import expect
649 lines
20 KiB
C
649 lines
20 KiB
C
/*
|
|
+----------------------------------------------------------------------+
|
|
| Zend OPcache |
|
|
+----------------------------------------------------------------------+
|
|
| Copyright (c) 1998-2015 The PHP Group |
|
|
+----------------------------------------------------------------------+
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
| available through the world-wide-web at the following url: |
|
|
| http://www.php.net/license/3_01.txt |
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
+----------------------------------------------------------------------+
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
| Stanislav Malyshev <stas@zend.com> |
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
|
+----------------------------------------------------------------------+
|
|
*/
|
|
|
|
#include "php.h"
|
|
#include "Optimizer/zend_optimizer.h"
|
|
#include "Optimizer/zend_optimizer_internal.h"
|
|
#include "zend_API.h"
|
|
#include "zend_constants.h"
|
|
#include "zend_execute.h"
|
|
#include "zend_vm.h"
|
|
|
|
static void zend_optimizer_zval_dtor_wrapper(zval *zvalue)
|
|
{
|
|
zval_dtor(zvalue);
|
|
}
|
|
|
|
void zend_optimizer_collect_constant(zend_optimizer_ctx *ctx, zval *name, zval* value)
|
|
{
|
|
zval val;
|
|
|
|
if (!ctx->constants) {
|
|
ctx->constants = zend_arena_alloc(&ctx->arena, sizeof(HashTable));
|
|
zend_hash_init(ctx->constants, 16, NULL, zend_optimizer_zval_dtor_wrapper, 0);
|
|
}
|
|
ZVAL_DUP(&val, value);
|
|
zend_hash_add(ctx->constants, Z_STR_P(name), &val);
|
|
}
|
|
|
|
int zend_optimizer_get_collected_constant(HashTable *constants, zval *name, zval* value)
|
|
{
|
|
zval *val;
|
|
|
|
if ((val = zend_hash_find(constants, Z_STR_P(name))) != NULL) {
|
|
ZVAL_DUP(value, val);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int zend_optimizer_lookup_cv(zend_op_array *op_array, zend_string* name)
|
|
{
|
|
int i = 0;
|
|
zend_ulong hash_value = zend_string_hash_val(name);
|
|
|
|
while (i < op_array->last_var) {
|
|
if (op_array->vars[i] == name ||
|
|
(op_array->vars[i]->h == hash_value &&
|
|
op_array->vars[i]->len == name->len &&
|
|
memcmp(op_array->vars[i]->val, name->val, name->len) == 0)) {
|
|
return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
|
|
}
|
|
i++;
|
|
}
|
|
i = op_array->last_var;
|
|
op_array->last_var++;
|
|
op_array->vars = erealloc(op_array->vars, op_array->last_var * sizeof(zend_string*));
|
|
op_array->vars[i] = zend_string_dup(name, 0);
|
|
|
|
/* all IS_TMP_VAR and IS_VAR variable numbers have to be adjusted */
|
|
{
|
|
zend_op *opline = op_array->opcodes;
|
|
zend_op *end = opline + op_array->last;
|
|
while (opline < end) {
|
|
if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
|
|
opline->op1.var += sizeof(zval);
|
|
}
|
|
if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
|
|
opline->op2.var += sizeof(zval);
|
|
}
|
|
if (opline->result_type & (IS_TMP_VAR|IS_VAR)) {
|
|
opline->result.var += sizeof(zval);
|
|
}
|
|
if (opline->opcode == ZEND_DECLARE_INHERITED_CLASS ||
|
|
opline->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
|
|
opline->extended_value += sizeof(zval);
|
|
}
|
|
opline++;
|
|
}
|
|
}
|
|
|
|
return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
|
|
}
|
|
|
|
int zend_optimizer_add_literal(zend_op_array *op_array, zval *zv)
|
|
{
|
|
int i = op_array->last_literal;
|
|
op_array->last_literal++;
|
|
op_array->literals = (zval*)erealloc(op_array->literals, op_array->last_literal * sizeof(zval));
|
|
ZVAL_COPY_VALUE(&op_array->literals[i], zv);
|
|
Z_CACHE_SLOT(op_array->literals[i]) = -1;
|
|
//??? Z_SET_REFCOUNT(op_array->literals[i].constant, 2);
|
|
//??? Z_SET_ISREF(op_array->literals[i].constant);
|
|
return i;
|
|
}
|
|
|
|
void zend_optimizer_update_op1_const(zend_op_array *op_array,
|
|
zend_op *opline,
|
|
zval *val)
|
|
{
|
|
if (opline->opcode == ZEND_FREE) {
|
|
MAKE_NOP(opline);
|
|
zval_dtor(val);
|
|
} else {
|
|
ZEND_OP1_TYPE(opline) = IS_CONST;
|
|
if (Z_TYPE_P(val) == IS_STRING) {
|
|
switch (opline->opcode) {
|
|
case ZEND_INIT_STATIC_METHOD_CALL:
|
|
case ZEND_CATCH:
|
|
case ZEND_FETCH_CONSTANT:
|
|
case ZEND_DEFINED:
|
|
case ZEND_NEW:
|
|
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline)));
|
|
Z_CACHE_SLOT(op_array->literals[opline->op1.constant]) = op_array->cache_size;
|
|
op_array->cache_size += sizeof(void*);
|
|
zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
|
|
zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(op_array->literals[opline->op1.constant+1]));
|
|
break;
|
|
default:
|
|
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(ZEND_OP1_LITERAL(opline)));
|
|
break;
|
|
}
|
|
} else {
|
|
opline->op1.constant = zend_optimizer_add_literal(op_array, val);
|
|
}
|
|
}
|
|
}
|
|
|
|
void zend_optimizer_update_op2_const(zend_op_array *op_array,
|
|
zend_op *opline,
|
|
zval *val)
|
|
{
|
|
ZEND_OP2_TYPE(opline) = IS_CONST;
|
|
if (opline->opcode == ZEND_INIT_FCALL) {
|
|
zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
|
|
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline)));
|
|
Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size;
|
|
op_array->cache_size += sizeof(void*);
|
|
return;
|
|
}
|
|
opline->op2.constant = zend_optimizer_add_literal(op_array, val);
|
|
if (Z_TYPE_P(val) == IS_STRING) {
|
|
zend_string_hash_val(Z_STR(ZEND_OP2_LITERAL(opline)));
|
|
switch (opline->opcode) {
|
|
case ZEND_FETCH_R:
|
|
case ZEND_FETCH_W:
|
|
case ZEND_FETCH_RW:
|
|
case ZEND_FETCH_IS:
|
|
case ZEND_FETCH_UNSET:
|
|
case ZEND_FETCH_FUNC_ARG:
|
|
case ZEND_FETCH_CLASS:
|
|
case ZEND_INIT_FCALL_BY_NAME:
|
|
/*case ZEND_INIT_NS_FCALL_BY_NAME:*/
|
|
case ZEND_UNSET_VAR:
|
|
case ZEND_ISSET_ISEMPTY_VAR:
|
|
case ZEND_ADD_INTERFACE:
|
|
case ZEND_ADD_TRAIT:
|
|
case ZEND_INSTANCEOF:
|
|
Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size;
|
|
op_array->cache_size += sizeof(void*);
|
|
zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
|
|
zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(op_array->literals[opline->op2.constant+1]));
|
|
break;
|
|
case ZEND_INIT_DYNAMIC_CALL:
|
|
opline->opcode = ZEND_INIT_FCALL_BY_NAME;
|
|
Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size;
|
|
op_array->cache_size += sizeof(void*);
|
|
zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
|
|
zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(op_array->literals[opline->op2.constant+1]));
|
|
break;
|
|
case ZEND_INIT_METHOD_CALL:
|
|
case ZEND_INIT_STATIC_METHOD_CALL:
|
|
zend_str_tolower(Z_STRVAL_P(val), Z_STRLEN_P(val));
|
|
zend_optimizer_add_literal(op_array, val);
|
|
zend_string_hash_val(Z_STR(op_array->literals[opline->op2.constant+1]));
|
|
/* break missing intentionally */
|
|
/*case ZEND_FETCH_CONSTANT:*/
|
|
case ZEND_ASSIGN_OBJ:
|
|
case ZEND_FETCH_OBJ_R:
|
|
case ZEND_FETCH_OBJ_W:
|
|
case ZEND_FETCH_OBJ_RW:
|
|
case ZEND_FETCH_OBJ_IS:
|
|
case ZEND_FETCH_OBJ_UNSET:
|
|
case ZEND_FETCH_OBJ_FUNC_ARG:
|
|
case ZEND_UNSET_OBJ:
|
|
case ZEND_PRE_INC_OBJ:
|
|
case ZEND_PRE_DEC_OBJ:
|
|
case ZEND_POST_INC_OBJ:
|
|
case ZEND_POST_DEC_OBJ:
|
|
case ZEND_ISSET_ISEMPTY_PROP_OBJ:
|
|
Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size;
|
|
op_array->cache_size += 2 * sizeof(void*);
|
|
break;
|
|
case ZEND_ASSIGN_ADD:
|
|
case ZEND_ASSIGN_SUB:
|
|
case ZEND_ASSIGN_MUL:
|
|
case ZEND_ASSIGN_DIV:
|
|
case ZEND_ASSIGN_MOD:
|
|
case ZEND_ASSIGN_SL:
|
|
case ZEND_ASSIGN_SR:
|
|
case ZEND_ASSIGN_CONCAT:
|
|
case ZEND_ASSIGN_BW_OR:
|
|
case ZEND_ASSIGN_BW_AND:
|
|
case ZEND_ASSIGN_BW_XOR:
|
|
if (opline->extended_value == ZEND_ASSIGN_OBJ) {
|
|
Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = op_array->cache_size;
|
|
op_array->cache_size += 2 * sizeof(void*);
|
|
}
|
|
break;
|
|
case ZEND_OP_DATA:
|
|
if ((opline-1)->opcode == ZEND_ASSIGN_DIM ||
|
|
((opline-1)->extended_value == ZEND_ASSIGN_DIM &&
|
|
((opline-1)->opcode == ZEND_ASSIGN_ADD ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_SUB ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_MUL ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_DIV ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_MOD ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_SL ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_SR ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_CONCAT ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_BW_OR ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_BW_AND ||
|
|
(opline-1)->opcode == ZEND_ASSIGN_BW_XOR))) {
|
|
goto check_numeric;
|
|
}
|
|
break;
|
|
case ZEND_ISSET_ISEMPTY_DIM_OBJ:
|
|
case ZEND_ADD_ARRAY_ELEMENT:
|
|
case ZEND_INIT_ARRAY:
|
|
case ZEND_ASSIGN_DIM:
|
|
case ZEND_UNSET_DIM:
|
|
case ZEND_FETCH_DIM_R:
|
|
case ZEND_FETCH_DIM_W:
|
|
case ZEND_FETCH_DIM_RW:
|
|
case ZEND_FETCH_DIM_IS:
|
|
case ZEND_FETCH_DIM_FUNC_ARG:
|
|
case ZEND_FETCH_DIM_UNSET:
|
|
case ZEND_FETCH_LIST:
|
|
check_numeric:
|
|
{
|
|
zend_ulong index;
|
|
|
|
if (ZEND_HANDLE_NUMERIC(Z_STR_P(val), index)) {
|
|
zval_dtor(val);
|
|
ZVAL_LONG(val, index);
|
|
op_array->literals[opline->op2.constant] = *val;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int zend_optimizer_replace_by_const(zend_op_array *op_array,
|
|
zend_op *opline,
|
|
zend_uchar type,
|
|
uint32_t var,
|
|
zval *val)
|
|
{
|
|
zend_op *end = op_array->opcodes + op_array->last;
|
|
|
|
while (opline < end) {
|
|
if (ZEND_OP1_TYPE(opline) == type &&
|
|
ZEND_OP1(opline).var == var) {
|
|
switch (opline->opcode) {
|
|
case ZEND_FETCH_DIM_W:
|
|
case ZEND_FETCH_DIM_RW:
|
|
case ZEND_FETCH_DIM_FUNC_ARG:
|
|
case ZEND_FETCH_DIM_UNSET:
|
|
case ZEND_ASSIGN_DIM:
|
|
case ZEND_SEPARATE:
|
|
return 0;
|
|
case ZEND_SEND_VAR:
|
|
opline->extended_value = 0;
|
|
opline->opcode = ZEND_SEND_VAL;
|
|
break;
|
|
case ZEND_SEND_VAR_EX:
|
|
opline->extended_value = 0;
|
|
opline->opcode = ZEND_SEND_VAL_EX;
|
|
break;
|
|
case ZEND_SEND_VAR_NO_REF:
|
|
if (opline->extended_value & ZEND_ARG_COMPILE_TIME_BOUND) {
|
|
if (opline->extended_value & ZEND_ARG_SEND_BY_REF) {
|
|
return 0;
|
|
}
|
|
opline->extended_value = 0;
|
|
opline->opcode = ZEND_SEND_VAL_EX;
|
|
} else {
|
|
opline->extended_value = 0;
|
|
opline->opcode = ZEND_SEND_VAL;
|
|
}
|
|
break;
|
|
/* In most cases IS_TMP_VAR operand may be used only once.
|
|
* The operands are usually destroyed by the opcode handler.
|
|
* ZEND_CASE is an exception, that keeps operand unchanged,
|
|
* and allows its reuse. The number of ZEND_CASE instructions
|
|
* usually terminated by ZEND_FREE that finally kills the value.
|
|
*/
|
|
case ZEND_FREE:
|
|
case ZEND_CASE: {
|
|
zend_op *m, *n;
|
|
int brk = op_array->last_brk_cont;
|
|
zend_bool in_switch = 0;
|
|
while (brk--) {
|
|
if (op_array->brk_cont_array[brk].start <= (opline - op_array->opcodes) &&
|
|
op_array->brk_cont_array[brk].brk > (opline - op_array->opcodes)) {
|
|
in_switch = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!in_switch) {
|
|
ZEND_ASSERT(opline->opcode == ZEND_FREE);
|
|
MAKE_NOP(opline);
|
|
zval_dtor(val);
|
|
return 1;
|
|
}
|
|
|
|
m = opline;
|
|
n = op_array->opcodes + op_array->brk_cont_array[brk].brk + 1;
|
|
while (m < n) {
|
|
if (ZEND_OP1_TYPE(m) == type &&
|
|
ZEND_OP1(m).var == var) {
|
|
if (m->opcode == ZEND_CASE) {
|
|
zval old_val;
|
|
ZVAL_COPY_VALUE(&old_val, val);
|
|
zval_copy_ctor(val);
|
|
zend_optimizer_update_op1_const(op_array, m, val);
|
|
ZVAL_COPY_VALUE(val, &old_val);
|
|
} else if (m->opcode == ZEND_FREE) {
|
|
MAKE_NOP(m);
|
|
} else {
|
|
ZEND_ASSERT(0);
|
|
}
|
|
}
|
|
m++;
|
|
}
|
|
zval_dtor(val);
|
|
return 1;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
zend_optimizer_update_op1_const(op_array, opline, val);
|
|
break;
|
|
}
|
|
|
|
if (ZEND_OP2_TYPE(opline) == type &&
|
|
ZEND_OP2(opline).var == var) {
|
|
switch (opline->opcode) {
|
|
case ZEND_ASSIGN_REF:
|
|
return 0;
|
|
default:
|
|
break;
|
|
}
|
|
zend_optimizer_update_op2_const(op_array, opline, val);
|
|
break;
|
|
}
|
|
opline++;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
static void zend_optimize(zend_op_array *op_array,
|
|
zend_optimizer_ctx *ctx)
|
|
{
|
|
if (op_array->type == ZEND_EVAL_CODE) {
|
|
return;
|
|
}
|
|
|
|
/* pass 1
|
|
* - substitute persistent constants (true, false, null, etc)
|
|
* - perform compile-time evaluation of constant binary and unary operations
|
|
* - optimize series of ADD_STRING and/or ADD_CHAR
|
|
* - convert CAST(IS_BOOL,x) into BOOL(x)
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_1 & OPTIMIZATION_LEVEL) {
|
|
zend_optimizer_pass1(op_array, ctx);
|
|
}
|
|
|
|
/* pass 2:
|
|
* - convert non-numeric constants to numeric constants in numeric operators
|
|
* - optimize constant conditional JMPs
|
|
* - optimize static BRKs and CONTs
|
|
* - pre-evaluate constant function calls
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_2 & OPTIMIZATION_LEVEL) {
|
|
zend_optimizer_pass2(op_array);
|
|
}
|
|
|
|
/* pass 3:
|
|
* - optimize $i = $i+expr to $i+=expr
|
|
* - optimize series of JMPs
|
|
* - change $i++ to ++$i where possible
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_3 & OPTIMIZATION_LEVEL) {
|
|
zend_optimizer_pass3(op_array);
|
|
}
|
|
|
|
/* pass 4:
|
|
* - INIT_FCALL_BY_NAME -> DO_FCALL
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_4 & OPTIMIZATION_LEVEL) {
|
|
optimize_func_calls(op_array, ctx);
|
|
}
|
|
|
|
/* pass 5:
|
|
* - CFG optimization
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_5 & OPTIMIZATION_LEVEL) {
|
|
optimize_cfg(op_array, ctx);
|
|
}
|
|
|
|
/* pass 9:
|
|
* - Optimize temp variables usage
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_9 & OPTIMIZATION_LEVEL) {
|
|
optimize_temporary_variables(op_array, ctx);
|
|
}
|
|
|
|
/* pass 10:
|
|
* - remove NOPs
|
|
*/
|
|
if (((ZEND_OPTIMIZER_PASS_10|ZEND_OPTIMIZER_PASS_5) & OPTIMIZATION_LEVEL) == ZEND_OPTIMIZER_PASS_10) {
|
|
zend_optimizer_nop_removal(op_array);
|
|
}
|
|
|
|
/* pass 11:
|
|
* - Compact literals table
|
|
*/
|
|
if (ZEND_OPTIMIZER_PASS_11 & OPTIMIZATION_LEVEL) {
|
|
zend_optimizer_compact_literals(op_array, ctx);
|
|
}
|
|
}
|
|
|
|
static void zend_accel_optimize(zend_op_array *op_array,
|
|
zend_optimizer_ctx *ctx)
|
|
{
|
|
zend_op *opline, *end;
|
|
|
|
/* Revert pass_two() */
|
|
opline = op_array->opcodes;
|
|
end = opline + op_array->last;
|
|
while (opline < end) {
|
|
if (opline->op1_type == IS_CONST) {
|
|
ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline->op1);
|
|
}
|
|
if (opline->op2_type == IS_CONST) {
|
|
ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline->op2);
|
|
}
|
|
switch (opline->opcode) {
|
|
case ZEND_JMP:
|
|
case ZEND_GOTO:
|
|
case ZEND_FAST_CALL:
|
|
ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, ZEND_OP1(opline));
|
|
break;
|
|
case ZEND_JMPZNZ:
|
|
/* relative offset into absolute index */
|
|
opline->extended_value = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value);
|
|
/* break omitted intentionally */
|
|
case ZEND_JMPZ:
|
|
case ZEND_JMPNZ:
|
|
case ZEND_JMPZ_EX:
|
|
case ZEND_JMPNZ_EX:
|
|
case ZEND_JMP_SET:
|
|
case ZEND_COALESCE:
|
|
case ZEND_NEW:
|
|
case ZEND_FE_RESET_R:
|
|
case ZEND_FE_RESET_RW:
|
|
case ZEND_FE_FETCH_R:
|
|
case ZEND_FE_FETCH_RW:
|
|
case ZEND_ASSERT_CHECK:
|
|
ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, ZEND_OP2(opline));
|
|
break;
|
|
}
|
|
opline++;
|
|
}
|
|
|
|
/* Do actual optimizations */
|
|
zend_optimize(op_array, ctx);
|
|
|
|
/* Redo pass_two() */
|
|
opline = op_array->opcodes;
|
|
end = opline + op_array->last;
|
|
while (opline < end) {
|
|
if (opline->op1_type == IS_CONST) {
|
|
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
|
|
}
|
|
if (opline->op2_type == IS_CONST) {
|
|
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
|
|
}
|
|
switch (opline->opcode) {
|
|
case ZEND_JMP:
|
|
case ZEND_GOTO:
|
|
case ZEND_FAST_CALL:
|
|
ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, ZEND_OP1(opline));
|
|
break;
|
|
case ZEND_JMPZNZ:
|
|
/* absolute index to relative offset */
|
|
opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
|
|
/* break omitted intentionally */
|
|
case ZEND_JMPZ:
|
|
case ZEND_JMPNZ:
|
|
case ZEND_JMPZ_EX:
|
|
case ZEND_JMPNZ_EX:
|
|
case ZEND_JMP_SET:
|
|
case ZEND_COALESCE:
|
|
case ZEND_NEW:
|
|
case ZEND_FE_RESET_R:
|
|
case ZEND_FE_RESET_RW:
|
|
case ZEND_FE_FETCH_R:
|
|
case ZEND_FE_FETCH_RW:
|
|
case ZEND_ASSERT_CHECK:
|
|
ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, ZEND_OP2(opline));
|
|
break;
|
|
}
|
|
ZEND_VM_SET_OPCODE_HANDLER(opline);
|
|
opline++;
|
|
}
|
|
}
|
|
|
|
static void zend_accel_adjust_fcall_stack_size(zend_op_array *op_array, zend_optimizer_ctx *ctx)
|
|
{
|
|
zend_function *func;
|
|
zend_op *opline, *end;
|
|
|
|
opline = op_array->opcodes;
|
|
end = opline + op_array->last;
|
|
while (opline < end) {
|
|
if (opline->opcode == ZEND_INIT_FCALL) {
|
|
func = zend_hash_find_ptr(
|
|
&ctx->script->function_table,
|
|
Z_STR_P(RT_CONSTANT(op_array, opline->op2)));
|
|
if (func) {
|
|
opline->op1.num = zend_vm_calc_used_stack(opline->extended_value, func);
|
|
}
|
|
}
|
|
opline++;
|
|
}
|
|
}
|
|
|
|
int zend_accel_script_optimize(zend_persistent_script *script)
|
|
{
|
|
uint idx, j;
|
|
Bucket *p, *q;
|
|
zend_class_entry *ce;
|
|
zend_op_array *op_array;
|
|
zend_optimizer_ctx ctx;
|
|
|
|
ctx.arena = zend_arena_create(64 * 1024);
|
|
ctx.script = script;
|
|
ctx.constants = NULL;
|
|
|
|
zend_accel_optimize(&script->main_op_array, &ctx);
|
|
|
|
for (idx = 0; idx < script->function_table.nNumUsed; idx++) {
|
|
p = script->function_table.arData + idx;
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
op_array = (zend_op_array*)Z_PTR(p->val);
|
|
zend_accel_optimize(op_array, &ctx);
|
|
}
|
|
|
|
for (idx = 0; idx < script->class_table.nNumUsed; idx++) {
|
|
p = script->class_table.arData + idx;
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
ce = (zend_class_entry*)Z_PTR(p->val);
|
|
for (j = 0; j < ce->function_table.nNumUsed; j++) {
|
|
q = ce->function_table.arData + j;
|
|
if (Z_TYPE(q->val) == IS_UNDEF) continue;
|
|
op_array = (zend_op_array*)Z_PTR(q->val);
|
|
if (op_array->scope == ce) {
|
|
zend_accel_optimize(op_array, &ctx);
|
|
} else if (op_array->type == ZEND_USER_FUNCTION) {
|
|
zend_op_array *orig_op_array;
|
|
if ((orig_op_array = zend_hash_find_ptr(&op_array->scope->function_table, q->key)) != NULL) {
|
|
HashTable *ht = op_array->static_variables;
|
|
*op_array = *orig_op_array;
|
|
op_array->static_variables = ht;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ZEND_OPTIMIZER_PASS_12 & OPTIMIZATION_LEVEL) {
|
|
zend_accel_adjust_fcall_stack_size(&script->main_op_array, &ctx);
|
|
|
|
for (idx = 0; idx < script->function_table.nNumUsed; idx++) {
|
|
p = script->function_table.arData + idx;
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
op_array = (zend_op_array*)Z_PTR(p->val);
|
|
zend_accel_adjust_fcall_stack_size(op_array, &ctx);
|
|
}
|
|
|
|
for (idx = 0; idx < script->class_table.nNumUsed; idx++) {
|
|
p = script->class_table.arData + idx;
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
ce = (zend_class_entry*)Z_PTR(p->val);
|
|
for (j = 0; j < ce->function_table.nNumUsed; j++) {
|
|
q = ce->function_table.arData + j;
|
|
if (Z_TYPE(q->val) == IS_UNDEF) continue;
|
|
op_array = (zend_op_array*)Z_PTR(q->val);
|
|
if (op_array->scope == ce) {
|
|
zend_accel_adjust_fcall_stack_size(op_array, &ctx);
|
|
} else if (op_array->type == ZEND_USER_FUNCTION) {
|
|
zend_op_array *orig_op_array;
|
|
if ((orig_op_array = zend_hash_find_ptr(&op_array->scope->function_table, q->key)) != NULL) {
|
|
HashTable *ht = op_array->static_variables;
|
|
*op_array = *orig_op_array;
|
|
op_array->static_variables = ht;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ctx.constants) {
|
|
zend_hash_destroy(ctx.constants);
|
|
}
|
|
zend_arena_destroy(ctx.arena);
|
|
|
|
return 1;
|
|
}
|