php-src/ext/opcache/Optimizer/block_pass.c

2000 lines
65 KiB
C
Raw Normal View History

2014-08-15 08:47:54 +00:00
/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
2015-01-15 15:27:30 +00:00
| Copyright (c) 1998-2015 The PHP Group |
2014-08-15 08:47:54 +00:00
+----------------------------------------------------------------------+
| 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"
2013-02-13 12:26:47 +00:00
#define DEBUG_BLOCKPASS 0
2013-03-11 18:49:05 +00:00
/* Checks if a constant (like "true") may be replaced by its value */
2014-12-13 22:06:14 +00:00
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
2013-02-13 12:26:47 +00:00
{
zend_constant *c;
char *lookup_name;
int retval = 1;
ALLOCA_FLAG(use_heap);
2014-03-28 19:34:49 +00:00
if ((c = zend_hash_find_ptr(EG(zend_constants), name)) == NULL) {
lookup_name = DO_ALLOCA(ZSTR_LEN(name) + 1);
memcpy(lookup_name, ZSTR_VAL(name), ZSTR_LEN(name) + 1);
zend_str_tolower(lookup_name, ZSTR_LEN(name));
2013-02-13 12:26:47 +00:00
if ((c = zend_hash_str_find_ptr(EG(zend_constants), lookup_name, ZSTR_LEN(name))) != NULL) {
2013-02-13 12:26:47 +00:00
if (!(c->flags & CONST_CT_SUBST) || (c->flags & CONST_CS)) {
2013-02-22 06:56:05 +00:00
retval = 0;
2013-02-13 12:26:47 +00:00
}
} else {
2013-02-22 06:56:05 +00:00
retval = 0;
2013-02-13 12:26:47 +00:00
}
FREE_ALLOCA(lookup_name);
}
if (retval) {
2013-02-22 06:56:05 +00:00
if (c->flags & CONST_PERSISTENT) {
2014-03-31 19:38:37 +00:00
ZVAL_COPY_VALUE(result, &c->value);
2013-02-22 06:56:05 +00:00
if (copy) {
2013-02-13 12:26:47 +00:00
zval_copy_ctor(result);
}
} else {
retval = 0;
}
}
return retval;
}
#if DEBUG_BLOCKPASS
# define BLOCK_REF(b) b?op_array->opcodes-b->start_opline:-1
static inline void print_block(zend_code_block *block, zend_op *opcodes, char *txt)
{
2013-02-25 07:29:54 +00:00
fprintf(stderr, "%sBlock: %d-%d (%d)", txt, block->start_opline - opcodes, block->start_opline - opcodes + block->len - 1, block->len);
2013-02-22 06:56:05 +00:00
if (!block->access) {
2013-02-13 12:26:47 +00:00
fprintf(stderr, " unused");
}
2013-02-22 06:56:05 +00:00
if (block->op1_to) {
2013-02-13 12:26:47 +00:00
fprintf(stderr, " 1: %d", block->op1_to->start_opline - opcodes);
}
2013-02-22 06:56:05 +00:00
if (block->op2_to) {
2013-02-13 12:26:47 +00:00
fprintf(stderr, " 2: %d", block->op2_to->start_opline - opcodes);
}
2013-02-22 06:56:05 +00:00
if (block->ext_to) {
2013-02-13 12:26:47 +00:00
fprintf(stderr, " e: %d", block->ext_to->start_opline - opcodes);
}
2013-02-22 06:56:05 +00:00
if (block->follow_to) {
2013-02-13 12:26:47 +00:00
fprintf(stderr, " f: %d", block->follow_to->start_opline - opcodes);
}
2013-02-22 06:56:05 +00:00
if (block->sources) {
2013-02-13 12:26:47 +00:00
zend_block_source *bs = block->sources;
fprintf(stderr, " s:");
2013-02-22 06:56:05 +00:00
while (bs) {
2013-02-13 12:26:47 +00:00
fprintf(stderr, " %d", bs->from->start_opline - opcodes);
bs = bs->next;
}
}
fprintf(stderr, "\n");
fflush(stderr);
}
#else
#define print_block(a,b,c)
#endif
#define START_BLOCK_OP(opno) blocks[opno].start_opline = &op_array->opcodes[opno]; blocks[opno].start_opline_no = opno; blocks[opno].access = 1
/* find code blocks in op_array
code block is a set of opcodes with single flow of control, i.e. without jmps,
branches, etc. */
static int find_code_blocks(zend_op_array *op_array, zend_cfg *cfg, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
zend_op *opline;
2013-02-22 06:56:05 +00:00
zend_op *end = op_array->opcodes + op_array->last;
zend_code_block *blocks, *cur_block;
2014-08-25 17:28:33 +00:00
uint32_t opno = 0;
2013-02-13 12:26:47 +00:00
memset(cfg, 0, sizeof(zend_cfg));
blocks = cfg->blocks = zend_arena_calloc(&ctx->arena, op_array->last + 2, sizeof(zend_code_block));
2013-02-13 12:26:47 +00:00
opline = op_array->opcodes;
blocks[0].start_opline = opline;
blocks[0].start_opline_no = 0;
2013-02-22 06:56:05 +00:00
while (opline < end) {
2013-02-13 12:26:47 +00:00
switch((unsigned)opline->opcode) {
case ZEND_FAST_CALL:
START_BLOCK_OP(ZEND_OP1(opline).opline_num);
2013-04-09 13:48:45 +00:00
if (opline->extended_value) {
START_BLOCK_OP(ZEND_OP2(opline).opline_num);
}
START_BLOCK_OP(opno + 1);
break;
case ZEND_FAST_RET:
if (opline->extended_value) {
START_BLOCK_OP(ZEND_OP2(opline).opline_num);
}
START_BLOCK_OP(opno + 1);
2013-02-13 12:26:47 +00:00
break;
case ZEND_JMP:
2015-04-27 09:24:39 +00:00
case ZEND_DECLARE_ANON_CLASS:
case ZEND_DECLARE_ANON_INHERITED_CLASS:
2013-02-13 12:26:47 +00:00
START_BLOCK_OP(ZEND_OP1(opline).opline_num);
/* break missing intentionally */
case ZEND_RETURN:
case ZEND_RETURN_BY_REF:
2013-04-09 13:48:45 +00:00
case ZEND_GENERATOR_RETURN:
2013-02-13 12:26:47 +00:00
case ZEND_EXIT:
case ZEND_THROW:
/* start new block from this+1 */
2013-02-22 06:56:05 +00:00
START_BLOCK_OP(opno + 1);
2013-02-13 12:26:47 +00:00
break;
/* TODO: if conditional jmp depends on constant,
don't start block that won't be executed */
case ZEND_CATCH:
START_BLOCK_OP(opline->extended_value);
2013-02-22 06:56:05 +00:00
START_BLOCK_OP(opno + 1);
2013-02-13 12:26:47 +00:00
break;
case ZEND_JMPZNZ:
START_BLOCK_OP(opline->extended_value);
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach Squashed commit of the following: commit 1e41295097576dbce6c197ddb7507c07ccae3cbe Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Jan 31 07:28:58 2015 +0300 Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter commit 5406f21b11e563069d64045e599693b51c444b63 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:43 2015 +0300 Reduced alghorithms complexity commit b37f1d58d2a141b6e1d980a461ccb588d4317d2e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:30 2015 +0300 Fixed test name commit fb2d079645829b12ed4e55a461034df6400bc430 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:05 2015 +0300 API cleanup commit 08302c0d6d1cab279b9f2129df03a057baddf2ff Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 14:20:46 2015 +0300 Make array_splice() to preserve foreach hash position commit cc4b7be41e2e2b9b0d7a3c8e98466b8886692e6e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 12:24:31 2015 +0300 Make internal function, operation on array passed by reference, to preserve foreach hash position commit 5aa9712b0a30303aadfe3bdd8ae1f072ca3e6ba1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 09:49:35 2015 +0300 Implement consistent behavior for foreach by value over plain object commit 4c5b385ff53ae9f0b52572e98c4db801f56603b0 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 07:56:37 2015 +0300 More careful iterators update. commit 721fc9e80d2ee8f2cd79c8c3cdceffae2c72de92 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:43:28 2015 +0300 Added new test commit 15a23b1218b3e38630d677751a975907daa2cd54 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:05:02 2015 +0300 Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details) commit 10a3260b1f16b6075fd8140f673dfef4d5efea91 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:04:44 2015 +0300 New test commit eef80c583762d1e98d177cdbb27e3a8a6b0c4539 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 16:52:21 2015 +0300 Fixed foreach by reference iteration over constant array commit 61e739187391661e2d541947bec25d7dcc4479f3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 14:59:54 2015 +0300 Fixed temporary variable re-allocation pass commit 92e90c09f085c22707ff4a59201f016f56e0ef8b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 12:44:57 2015 +0300 Fixed operand destruction in case of exceptions in iterator commit dd2a36a2074bbb0cb31de00b66dcf2812d6d753f Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 10:02:34 2015 +0300 Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE) commit 4638f7b91407c48710007af82a68da0007c820f2 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 07:43:28 2015 +0300 Change "foreach" statement behavior (this is just a PoC yet) - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy. - it makes no difference if array given to "foreach by value" is reference itself - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 10:57:12 +00:00
case ZEND_FE_RESET_R:
case ZEND_FE_RESET_RW:
2013-02-13 12:26:47 +00:00
case ZEND_NEW:
case ZEND_JMP_SET:
case ZEND_COALESCE:
Improved assert() according to expectations RFC. See https://wiki.php.net/rfc/expectations Squashed commit of the following: commit 3f3651a7870738e35ec541e53b53069152135b24 Author: 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 commit ca8ecabf2a5f97f9116839c33396c9a7037e4368 Merge: 24328ac 9dac923 Author: 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()). commit 24328ac03f79a0f3b19be7411bf9e173f392abda Merge: 021fd94 1cdee9a Author: 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(). commit 021fd94ed1d692d212e6e30c6c1a9767c3f16f7f Merge: 49963eb ace1f82 Author: 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 commit 49963ebf9d2bcd6d2670203dd72884f6ba6c8a4b Merge: 07b1f92 6b77959 Author: 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 commit 07b1f92ed662f6fa9309e679b83aff328362c98b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 23:06:32 2015 +0300 fixed pretty-printer (support for "elseif") commit 5a976c8d85078502b48379996ab066e57533a0c3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 19:50:08 2015 +0300 Fixed vaeious ptetty-printer issues commit 69491e8e8e692030b0585aab485146906c0fedaf Merge: 8473157 3ddc246 Author: 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 commit 8473157fbb12d03fff8d5b602865a4b667522a4d Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 10:17:26 2015 +0300 Fixed typo and white spaces commit 96de5ffc8d604df9797d0141ae5ad9c15e1d6c32 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 00:28:39 2015 +0300 Fixed assert() in namesapaces commit 5eba069c28e7b6590618707e0b21cdb2dd62a192 Merge: 4a2d9c0 d428bf2 Author: 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 ... commit 4a2d9c0953dccd9e78ebee9291e1213419eb9136 Author: Dmitry Stogov <dmitry@zend.com> Date: Tue Feb 17 22:45:10 2015 +0300 Implemented AST pretty-printer to capture expression passed to assert() commit 7a059b66d51a65159801bd826346721325b89fec Merge: 9973df7 3892eba Author: 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 commit 3892eba2bf56a7699453855c995404106322718d Author: krakjoe <joe.watkins@live.co.uk> Date: Sun Feb 2 12:49:35 2014 +0000 import expect
2015-03-02 09:25:40 +00:00
case ZEND_ASSERT_CHECK:
2013-02-13 12:26:47 +00:00
START_BLOCK_OP(ZEND_OP2(opline).opline_num);
2013-02-22 06:56:05 +00:00
START_BLOCK_OP(opno + 1);
2013-02-13 12:26:47 +00:00
break;
Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach Squashed commit of the following: commit 1e41295097576dbce6c197ddb7507c07ccae3cbe Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Jan 31 07:28:58 2015 +0300 Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter commit 5406f21b11e563069d64045e599693b51c444b63 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:43 2015 +0300 Reduced alghorithms complexity commit b37f1d58d2a141b6e1d980a461ccb588d4317d2e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:30 2015 +0300 Fixed test name commit fb2d079645829b12ed4e55a461034df6400bc430 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:05 2015 +0300 API cleanup commit 08302c0d6d1cab279b9f2129df03a057baddf2ff Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 14:20:46 2015 +0300 Make array_splice() to preserve foreach hash position commit cc4b7be41e2e2b9b0d7a3c8e98466b8886692e6e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 12:24:31 2015 +0300 Make internal function, operation on array passed by reference, to preserve foreach hash position commit 5aa9712b0a30303aadfe3bdd8ae1f072ca3e6ba1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 09:49:35 2015 +0300 Implement consistent behavior for foreach by value over plain object commit 4c5b385ff53ae9f0b52572e98c4db801f56603b0 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 07:56:37 2015 +0300 More careful iterators update. commit 721fc9e80d2ee8f2cd79c8c3cdceffae2c72de92 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:43:28 2015 +0300 Added new test commit 15a23b1218b3e38630d677751a975907daa2cd54 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:05:02 2015 +0300 Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details) commit 10a3260b1f16b6075fd8140f673dfef4d5efea91 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:04:44 2015 +0300 New test commit eef80c583762d1e98d177cdbb27e3a8a6b0c4539 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 16:52:21 2015 +0300 Fixed foreach by reference iteration over constant array commit 61e739187391661e2d541947bec25d7dcc4479f3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 14:59:54 2015 +0300 Fixed temporary variable re-allocation pass commit 92e90c09f085c22707ff4a59201f016f56e0ef8b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 12:44:57 2015 +0300 Fixed operand destruction in case of exceptions in iterator commit dd2a36a2074bbb0cb31de00b66dcf2812d6d753f Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 10:02:34 2015 +0300 Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE) commit 4638f7b91407c48710007af82a68da0007c820f2 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 07:43:28 2015 +0300 Change "foreach" statement behavior (this is just a PoC yet) - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy. - it makes no difference if array given to "foreach by value" is reference itself - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 10:57:12 +00:00
case ZEND_FE_FETCH_R:
case ZEND_FE_FETCH_RW:
START_BLOCK_OP(opline->extended_value);
START_BLOCK_OP(opno + 1);
2013-04-09 13:48:45 +00:00
break;
2013-02-13 12:26:47 +00:00
}
opno++;
opline++;
}
/* first find block start points */
if (op_array->last_try_catch) {
int i;
cfg->try = zend_arena_calloc(&ctx->arena, op_array->last_try_catch, sizeof(zend_code_block *));
cfg->catch = zend_arena_calloc(&ctx->arena, op_array->last_try_catch, sizeof(zend_code_block *));
for (i = 0; i< op_array->last_try_catch; i++) {
cfg->try[i] = &blocks[op_array->try_catch_array[i].try_op];
cfg->catch[i] = &blocks[op_array->try_catch_array[i].catch_op];
START_BLOCK_OP(op_array->try_catch_array[i].try_op);
START_BLOCK_OP(op_array->try_catch_array[i].catch_op);
blocks[op_array->try_catch_array[i].try_op].protected = 1;
}
}
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
/* Currently, we don't optimize op_arrays with BRK/CONT/GOTO opcodes,
* but, we have to keep brk_cont_array to avoid memory leaks during
* exception handling */
if (op_array->last_brk_cont) {
int i, j;
j = 0;
for (i = 0; i< op_array->last_brk_cont; i++) {
if (op_array->brk_cont_array[i].start >= 0 &&
(op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_FREE ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_FE_FREE ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_ROPE_END ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_END_SILENCE)) {
int parent = op_array->brk_cont_array[i].parent;
while (parent >= 0 &&
op_array->brk_cont_array[parent].start < 0 &&
(op_array->opcodes[op_array->brk_cont_array[parent].brk].opcode != ZEND_FREE ||
op_array->opcodes[op_array->brk_cont_array[parent].brk].opcode != ZEND_FE_FREE ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode != ZEND_ROPE_END ||
op_array->opcodes[op_array->brk_cont_array[parent].brk].opcode != ZEND_END_SILENCE)) {
parent = op_array->brk_cont_array[parent].parent;
}
op_array->brk_cont_array[i].parent = parent;
j++;
}
}
if (j) {
cfg->loop_start = zend_arena_calloc(&ctx->arena, op_array->last_brk_cont, sizeof(zend_code_block *));
cfg->loop_cont = zend_arena_calloc(&ctx->arena, op_array->last_brk_cont, sizeof(zend_code_block *));
cfg->loop_brk = zend_arena_calloc(&ctx->arena, op_array->last_brk_cont, sizeof(zend_code_block *));
j = 0;
for (i = 0; i< op_array->last_brk_cont; i++) {
if (op_array->brk_cont_array[i].start >= 0 &&
(op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_FREE ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_FE_FREE ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_ROPE_END ||
op_array->opcodes[op_array->brk_cont_array[i].brk].opcode == ZEND_END_SILENCE)) {
if (i != j) {
op_array->brk_cont_array[j] = op_array->brk_cont_array[i];
}
cfg->loop_start[j] = &blocks[op_array->brk_cont_array[j].start];
cfg->loop_cont[j] = &blocks[op_array->brk_cont_array[j].cont];
cfg->loop_brk[j] = &blocks[op_array->brk_cont_array[j].brk];
START_BLOCK_OP(op_array->brk_cont_array[j].start);
START_BLOCK_OP(op_array->brk_cont_array[j].cont);
START_BLOCK_OP(op_array->brk_cont_array[j].brk);
blocks[op_array->brk_cont_array[j].start].protected = 1;
blocks[op_array->brk_cont_array[j].brk].protected = 1;
j++;
}
}
op_array->last_brk_cont = j;
} else {
efree(op_array->brk_cont_array);
op_array->brk_cont_array = NULL;
op_array->last_brk_cont = 0;
}
}
2013-02-13 12:26:47 +00:00
/* Build CFG (Control Flow Graph) */
cur_block = blocks;
2013-02-22 06:56:05 +00:00
for (opno = 1; opno < op_array->last; opno++) {
if (blocks[opno].start_opline) {
2013-02-13 12:26:47 +00:00
/* found new block start */
cur_block->len = blocks[opno].start_opline - cur_block->start_opline;
cur_block->next = &blocks[opno];
/* what is the last OP of previous block? */
2013-02-22 06:56:05 +00:00
opline = blocks[opno].start_opline - 1;
2013-04-09 13:48:45 +00:00
if (opline->opcode == ZEND_OP_DATA) {
opline--;
}
2013-02-13 12:26:47 +00:00
switch((unsigned)opline->opcode) {
case ZEND_RETURN:
case ZEND_RETURN_BY_REF:
2013-04-09 13:48:45 +00:00
case ZEND_GENERATOR_RETURN:
2013-02-13 12:26:47 +00:00
case ZEND_EXIT:
case ZEND_THROW:
break;
case ZEND_FAST_CALL:
2013-04-09 13:48:45 +00:00
if (opline->extended_value) {
cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
}
cur_block->op1_to = &blocks[ZEND_OP1(opline).opline_num];
break;
case ZEND_FAST_RET:
if (opline->extended_value) {
cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
}
break;
2013-02-13 12:26:47 +00:00
case ZEND_JMP:
cur_block->op1_to = &blocks[ZEND_OP1(opline).opline_num];
break;
2015-04-27 09:24:39 +00:00
case ZEND_DECLARE_ANON_CLASS:
case ZEND_DECLARE_ANON_INHERITED_CLASS:
cur_block->op1_to = &blocks[ZEND_OP1(opline).opline_num];
cur_block->follow_to = &blocks[opno];
break;
2013-02-13 12:26:47 +00:00
case ZEND_JMPZNZ:
cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
cur_block->ext_to = &blocks[opline->extended_value];
break;
case ZEND_CATCH:
cur_block->ext_to = &blocks[opline->extended_value];
cur_block->follow_to = &blocks[opno];
break;
case ZEND_FE_FETCH_R:
case ZEND_FE_FETCH_RW:
cur_block->ext_to = &blocks[opline->extended_value];
cur_block->follow_to = &blocks[opno];
break;
2013-02-13 12:26:47 +00:00
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach Squashed commit of the following: commit 1e41295097576dbce6c197ddb7507c07ccae3cbe Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Jan 31 07:28:58 2015 +0300 Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter commit 5406f21b11e563069d64045e599693b51c444b63 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:43 2015 +0300 Reduced alghorithms complexity commit b37f1d58d2a141b6e1d980a461ccb588d4317d2e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:30 2015 +0300 Fixed test name commit fb2d079645829b12ed4e55a461034df6400bc430 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:05 2015 +0300 API cleanup commit 08302c0d6d1cab279b9f2129df03a057baddf2ff Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 14:20:46 2015 +0300 Make array_splice() to preserve foreach hash position commit cc4b7be41e2e2b9b0d7a3c8e98466b8886692e6e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 12:24:31 2015 +0300 Make internal function, operation on array passed by reference, to preserve foreach hash position commit 5aa9712b0a30303aadfe3bdd8ae1f072ca3e6ba1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 09:49:35 2015 +0300 Implement consistent behavior for foreach by value over plain object commit 4c5b385ff53ae9f0b52572e98c4db801f56603b0 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 07:56:37 2015 +0300 More careful iterators update. commit 721fc9e80d2ee8f2cd79c8c3cdceffae2c72de92 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:43:28 2015 +0300 Added new test commit 15a23b1218b3e38630d677751a975907daa2cd54 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:05:02 2015 +0300 Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details) commit 10a3260b1f16b6075fd8140f673dfef4d5efea91 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:04:44 2015 +0300 New test commit eef80c583762d1e98d177cdbb27e3a8a6b0c4539 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 16:52:21 2015 +0300 Fixed foreach by reference iteration over constant array commit 61e739187391661e2d541947bec25d7dcc4479f3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 14:59:54 2015 +0300 Fixed temporary variable re-allocation pass commit 92e90c09f085c22707ff4a59201f016f56e0ef8b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 12:44:57 2015 +0300 Fixed operand destruction in case of exceptions in iterator commit dd2a36a2074bbb0cb31de00b66dcf2812d6d753f Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 10:02:34 2015 +0300 Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE) commit 4638f7b91407c48710007af82a68da0007c820f2 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 07:43:28 2015 +0300 Change "foreach" statement behavior (this is just a PoC yet) - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy. - it makes no difference if array given to "foreach by value" is reference itself - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 10:57:12 +00:00
case ZEND_FE_RESET_R:
case ZEND_FE_RESET_RW:
2013-02-13 12:26:47 +00:00
case ZEND_NEW:
case ZEND_JMP_SET:
case ZEND_COALESCE:
Improved assert() according to expectations RFC. See https://wiki.php.net/rfc/expectations Squashed commit of the following: commit 3f3651a7870738e35ec541e53b53069152135b24 Author: 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 commit ca8ecabf2a5f97f9116839c33396c9a7037e4368 Merge: 24328ac 9dac923 Author: 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()). commit 24328ac03f79a0f3b19be7411bf9e173f392abda Merge: 021fd94 1cdee9a Author: 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(). commit 021fd94ed1d692d212e6e30c6c1a9767c3f16f7f Merge: 49963eb ace1f82 Author: 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 commit 49963ebf9d2bcd6d2670203dd72884f6ba6c8a4b Merge: 07b1f92 6b77959 Author: 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 commit 07b1f92ed662f6fa9309e679b83aff328362c98b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 23:06:32 2015 +0300 fixed pretty-printer (support for "elseif") commit 5a976c8d85078502b48379996ab066e57533a0c3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 19:50:08 2015 +0300 Fixed vaeious ptetty-printer issues commit 69491e8e8e692030b0585aab485146906c0fedaf Merge: 8473157 3ddc246 Author: 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 commit 8473157fbb12d03fff8d5b602865a4b667522a4d Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 10:17:26 2015 +0300 Fixed typo and white spaces commit 96de5ffc8d604df9797d0141ae5ad9c15e1d6c32 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Feb 18 00:28:39 2015 +0300 Fixed assert() in namesapaces commit 5eba069c28e7b6590618707e0b21cdb2dd62a192 Merge: 4a2d9c0 d428bf2 Author: 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 ... commit 4a2d9c0953dccd9e78ebee9291e1213419eb9136 Author: Dmitry Stogov <dmitry@zend.com> Date: Tue Feb 17 22:45:10 2015 +0300 Implemented AST pretty-printer to capture expression passed to assert() commit 7a059b66d51a65159801bd826346721325b89fec Merge: 9973df7 3892eba Author: 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 commit 3892eba2bf56a7699453855c995404106322718d Author: krakjoe <joe.watkins@live.co.uk> Date: Sun Feb 2 12:49:35 2014 +0000 import expect
2015-03-02 09:25:40 +00:00
case ZEND_ASSERT_CHECK:
2013-02-13 12:26:47 +00:00
cur_block->op2_to = &blocks[ZEND_OP2(opline).opline_num];
/* break missing intentionally */
2013-04-09 13:48:45 +00:00
default:
/* next block follows this */
2013-02-13 12:26:47 +00:00
cur_block->follow_to = &blocks[opno];
break;
}
print_block(cur_block, op_array->opcodes, "");
cur_block = cur_block->next;
}
}
cur_block->len = end - cur_block->start_opline;
2013-02-25 07:29:54 +00:00
cur_block->next = &blocks[op_array->last + 1];
2013-02-13 12:26:47 +00:00
print_block(cur_block, op_array->opcodes, "");
return 1;
2013-02-13 12:26:47 +00:00
}
/* CFG back references management */
#define ADD_SOURCE(fromb, tob) { \
zend_block_source *__s = tob->sources; \
2013-02-22 06:56:05 +00:00
while (__s && __s->from != fromb) __s = __s->next; \
if (__s == NULL) { \
zend_block_source *__t = zend_arena_alloc(&ctx->arena, sizeof(zend_block_source)); \
2013-02-13 12:26:47 +00:00
__t->next = tob->sources; \
tob->sources = __t; \
__t->from = fromb; \
} \
}
#define DEL_SOURCE(cs) do { \
*(cs) = (*(cs))->next; \
} while (0)
2013-02-13 12:26:47 +00:00
static inline void replace_source(zend_block_source *list, zend_code_block *old, zend_code_block *new)
{
/* replace all references to 'old' in 'list' with 'new' */
zend_block_source **cs;
int found = 0;
2013-02-22 06:56:05 +00:00
for (cs = &list; *cs; cs = &((*cs)->next)) {
if ((*cs)->from == new) {
if (found) {
2013-02-13 12:26:47 +00:00
DEL_SOURCE(cs);
} else {
found = 1;
}
}
2013-02-22 06:56:05 +00:00
if ((*cs)->from == old) {
if (found) {
2013-02-13 12:26:47 +00:00
DEL_SOURCE(cs);
} else {
(*cs)->from = new;
found = 1;
}
}
}
}
static inline void del_source(zend_code_block *from, zend_code_block *to)
{
/* delete source 'from' from 'to'-s sources list */
zend_block_source **cs = &to->sources;
2013-02-22 06:56:05 +00:00
if (to->sources == NULL) {
2013-02-13 12:26:47 +00:00
to->access = 0;
return;
}
2013-02-22 06:56:05 +00:00
while (*cs) {
if ((*cs)->from == from) {
DEL_SOURCE(cs);
2013-02-13 12:26:47 +00:00
break;
}
cs = &((*cs)->next);
}
2013-02-22 06:56:05 +00:00
if (to->sources == NULL) {
2013-02-13 12:26:47 +00:00
/* 'to' has no more sources - it's unused, will be stripped */
to->access = 0;
return;
}
if (!to->protected && to->sources->next == NULL) {
2013-02-13 12:26:47 +00:00
/* source to only one block */
zend_code_block *from_block = to->sources->from;
2013-02-22 06:56:05 +00:00
if (from_block->access && from_block->follow_to == to &&
from_block->op1_to == NULL &&
from_block->op2_to == NULL &&
from_block->ext_to == NULL) {
2013-02-13 12:26:47 +00:00
/* this block follows it's only predecessor - we can join them */
zend_op *new_to = from_block->start_opline + from_block->len;
2013-02-22 06:56:05 +00:00
if (new_to != to->start_opline) {
2013-02-13 12:26:47 +00:00
/* move block to new location */
memmove(new_to, to->start_opline, sizeof(zend_op)*to->len);
}
2013-03-11 18:49:05 +00:00
/* join blocks' lengths */
2013-02-13 12:26:47 +00:00
from_block->len += to->len;
/* move 'to'`s references to 'from' */
to->start_opline = NULL;
to->access = 0;
to->sources = NULL;
from_block->follow_to = to->follow_to;
2013-02-22 06:56:05 +00:00
if (to->op1_to) {
2013-02-13 12:26:47 +00:00
from_block->op1_to = to->op1_to;
replace_source(to->op1_to->sources, to, from_block);
}
2013-02-22 06:56:05 +00:00
if (to->op2_to) {
2013-02-13 12:26:47 +00:00
from_block->op2_to = to->op2_to;
replace_source(to->op2_to->sources, to, from_block);
}
2013-02-22 06:56:05 +00:00
if (to->ext_to) {
2013-02-13 12:26:47 +00:00
from_block->ext_to = to->ext_to;
replace_source(to->ext_to->sources, to, from_block);
}
2013-02-22 06:56:05 +00:00
if (to->follow_to) {
2013-02-13 12:26:47 +00:00
replace_source(to->follow_to->sources, to, from_block);
}
/* remove "to" from list */
}
}
}
static void delete_code_block(zend_code_block *block, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
2013-03-06 18:32:21 +00:00
if (block->protected) {
return;
}
2013-02-22 06:56:05 +00:00
if (block->follow_to) {
2013-02-13 12:26:47 +00:00
zend_block_source *bs = block->sources;
2013-02-22 06:56:05 +00:00
while (bs) {
2013-02-13 12:26:47 +00:00
zend_code_block *from_block = bs->from;
zend_code_block *to = block->follow_to;
2013-02-22 06:56:05 +00:00
if (from_block->op1_to == block) {
2013-02-13 12:26:47 +00:00
from_block->op1_to = to;
ADD_SOURCE(from_block, to);
}
2013-02-22 06:56:05 +00:00
if (from_block->op2_to == block) {
2013-02-13 12:26:47 +00:00
from_block->op2_to = to;
ADD_SOURCE(from_block, to);
}
2013-02-22 06:56:05 +00:00
if (from_block->ext_to == block) {
2013-02-13 12:26:47 +00:00
from_block->ext_to = to;
ADD_SOURCE(from_block, to);
}
2013-02-22 06:56:05 +00:00
if (from_block->follow_to == block) {
2013-02-13 12:26:47 +00:00
from_block->follow_to = to;
ADD_SOURCE(from_block, to);
}
bs = bs->next;
}
}
block->access = 0;
}
static void zend_access_path(zend_code_block *block, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
2013-02-22 06:56:05 +00:00
if (block->access) {
2013-02-13 12:26:47 +00:00
return;
}
block->access = 1;
2013-02-22 06:56:05 +00:00
if (block->op1_to) {
zend_access_path(block->op1_to, ctx);
2013-02-13 12:26:47 +00:00
ADD_SOURCE(block, block->op1_to);
}
2013-02-22 06:56:05 +00:00
if (block->op2_to) {
zend_access_path(block->op2_to, ctx);
2013-02-13 12:26:47 +00:00
ADD_SOURCE(block, block->op2_to);
}
2013-02-22 06:56:05 +00:00
if (block->ext_to) {
zend_access_path(block->ext_to, ctx);
2013-02-13 12:26:47 +00:00
ADD_SOURCE(block, block->ext_to);
}
2013-02-22 06:56:05 +00:00
if (block->follow_to) {
zend_access_path(block->follow_to, ctx);
2013-02-13 12:26:47 +00:00
ADD_SOURCE(block, block->follow_to);
}
}
/* Traverse CFG, mark reachable basic blocks and build back references */
static void zend_rebuild_access_path(zend_cfg *cfg, zend_op_array *op_array, int find_start, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
zend_code_block *blocks = cfg->blocks;
2013-02-22 06:56:05 +00:00
zend_code_block *start = find_start? NULL : blocks;
2013-02-13 12:26:47 +00:00
zend_code_block *b;
2013-03-11 18:49:05 +00:00
/* Mark all blocks as unaccessible and destroy back references */
2013-02-13 12:26:47 +00:00
b = blocks;
while (b != NULL) {
if (!start && b->access) {
start = b;
}
b->access = 0;
b->sources = NULL;
b = b->next;
}
2013-03-11 18:49:05 +00:00
/* Walk thorough all paths */
zend_access_path(start, ctx);
2013-02-13 12:26:47 +00:00
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
/* Add brk/cont paths */
if (op_array->last_brk_cont) {
int i;
for (i=0; i< op_array->last_brk_cont; i++) {
zend_access_path(cfg->loop_start[i], ctx);
zend_access_path(cfg->loop_cont[i], ctx);
zend_access_path(cfg->loop_brk[i], ctx);
}
}
2013-03-11 18:49:05 +00:00
/* Add exception paths */
2013-02-13 12:26:47 +00:00
if (op_array->last_try_catch) {
int i;
2013-02-22 06:56:05 +00:00
for (i=0; i< op_array->last_try_catch; i++) {
if (!cfg->catch[i]->access) {
zend_access_path(cfg->catch[i], ctx);
2013-02-13 12:26:47 +00:00
}
}
}
}
/* Data dependencies macros */
2014-08-15 08:40:07 +00:00
#define VAR_NUM_EX(op) VAR_NUM((op).var)
2013-02-13 12:26:47 +00:00
2014-08-15 08:40:07 +00:00
#define VAR_SOURCE(op) Tsource[VAR_NUM(op.var)]
#define SET_VAR_SOURCE(opline) Tsource[VAR_NUM(opline->result.var)] = opline
2013-02-13 12:26:47 +00:00
2014-08-15 08:40:07 +00:00
#define VAR_UNSET(op) do { if (op ## _type & (IS_TMP_VAR|IS_VAR)) {VAR_SOURCE(op) = NULL;}} while (0)
2013-02-13 12:26:47 +00:00
#define convert_to_string_safe(v) \
2013-02-22 06:56:05 +00:00
if (Z_TYPE_P((v)) == IS_NULL) { \
2014-03-28 19:34:49 +00:00
ZVAL_STRINGL((v), "", 0); \
2013-02-13 12:26:47 +00:00
} else { \
convert_to_string((v)); \
}
static void strip_nop(zend_code_block *block, zend_optimizer_ctx *ctx)
{
zend_op *opline = block->start_opline;
zend_op *end, *new_end;
/* remove leading NOPs */
while (block->len > 0 && block->start_opline->opcode == ZEND_NOP) {
if (block->len == 1) {
/* this block is all NOPs, join with following block */
if (block->follow_to) {
delete_code_block(block, ctx);
}
return;
}
block->start_opline++;
block->start_opline_no++;
block->len--;
}
/* strip the inside NOPs */
opline = new_end = block->start_opline;
end = opline + block->len;
while (opline < end) {
zend_op *src;
int len = 0;
while (opline < end && opline->opcode == ZEND_NOP) {
opline++;
}
src = opline;
while (opline < end && opline->opcode != ZEND_NOP) {
opline++;
}
len = opline - src;
/* move up non-NOP opcodes */
memmove(new_end, src, len*sizeof(zend_op));
new_end += len;
}
block->len = new_end - block->start_opline;
}
2014-12-13 22:06:14 +00:00
static void zend_optimize_block(zend_code_block *block, zend_op_array *op_array, char *used_ext, zend_cfg *cfg, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
zend_op *opline = block->start_opline;
zend_op *end, *last_op = NULL;
zend_op **Tsource = cfg->Tsource;
2013-02-13 12:26:47 +00:00
print_block(block, op_array->opcodes, "Opt ");
/* remove leading NOPs */
2013-03-06 18:32:21 +00:00
while (block->len > 0 && block->start_opline->opcode == ZEND_NOP) {
2013-02-22 06:56:05 +00:00
if (block->len == 1) {
2013-02-13 12:26:47 +00:00
/* this block is all NOPs, join with following block */
2013-02-22 06:56:05 +00:00
if (block->follow_to) {
delete_code_block(block, ctx);
2013-02-13 12:26:47 +00:00
}
return;
}
block->start_opline++;
block->start_opline_no++;
block->len--;
}
/* we track data dependencies only insight a single basic block */
memset(Tsource, 0, (op_array->last_var + op_array->T) * sizeof(zend_op *));
2013-02-13 12:26:47 +00:00
opline = block->start_opline;
2013-02-22 06:56:05 +00:00
end = opline + block->len;
while ((op_array->T) && (opline < end)) {
2013-02-13 12:26:47 +00:00
/* strip X = QM_ASSIGN(const) */
if ((ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
2013-02-22 06:56:05 +00:00
VAR_SOURCE(opline->op1) &&
VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN &&
ZEND_OP1_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
opline->opcode != ZEND_CASE && /* CASE _always_ expects variable */
2014-10-20 09:52:28 +00:00
opline->opcode != ZEND_FETCH_LIST &&
Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach Squashed commit of the following: commit 1e41295097576dbce6c197ddb7507c07ccae3cbe Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Jan 31 07:28:58 2015 +0300 Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter commit 5406f21b11e563069d64045e599693b51c444b63 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:43 2015 +0300 Reduced alghorithms complexity commit b37f1d58d2a141b6e1d980a461ccb588d4317d2e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:30 2015 +0300 Fixed test name commit fb2d079645829b12ed4e55a461034df6400bc430 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:05 2015 +0300 API cleanup commit 08302c0d6d1cab279b9f2129df03a057baddf2ff Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 14:20:46 2015 +0300 Make array_splice() to preserve foreach hash position commit cc4b7be41e2e2b9b0d7a3c8e98466b8886692e6e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 12:24:31 2015 +0300 Make internal function, operation on array passed by reference, to preserve foreach hash position commit 5aa9712b0a30303aadfe3bdd8ae1f072ca3e6ba1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 09:49:35 2015 +0300 Implement consistent behavior for foreach by value over plain object commit 4c5b385ff53ae9f0b52572e98c4db801f56603b0 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 07:56:37 2015 +0300 More careful iterators update. commit 721fc9e80d2ee8f2cd79c8c3cdceffae2c72de92 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:43:28 2015 +0300 Added new test commit 15a23b1218b3e38630d677751a975907daa2cd54 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:05:02 2015 +0300 Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details) commit 10a3260b1f16b6075fd8140f673dfef4d5efea91 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:04:44 2015 +0300 New test commit eef80c583762d1e98d177cdbb27e3a8a6b0c4539 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 16:52:21 2015 +0300 Fixed foreach by reference iteration over constant array commit 61e739187391661e2d541947bec25d7dcc4479f3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 14:59:54 2015 +0300 Fixed temporary variable re-allocation pass commit 92e90c09f085c22707ff4a59201f016f56e0ef8b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 12:44:57 2015 +0300 Fixed operand destruction in case of exceptions in iterator commit dd2a36a2074bbb0cb31de00b66dcf2812d6d753f Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 10:02:34 2015 +0300 Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE) commit 4638f7b91407c48710007af82a68da0007c820f2 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 07:43:28 2015 +0300 Change "foreach" statement behavior (this is just a PoC yet) - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy. - it makes no difference if array given to "foreach by value" is reference itself - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 10:57:12 +00:00
(opline->opcode != ZEND_FE_RESET_R || opline->opcode != ZEND_FE_RESET_RW) &&
2013-02-22 06:56:05 +00:00
opline->opcode != ZEND_FREE
) {
2013-02-13 12:26:47 +00:00
zend_op *src = VAR_SOURCE(opline->op1);
zval c = ZEND_OP1_LITERAL(src);
2013-02-13 12:26:47 +00:00
VAR_UNSET(opline->op1);
zval_copy_ctor(&c);
2014-12-13 22:06:14 +00:00
zend_optimizer_update_op1_const(op_array, opline, &c);
literal_dtor(&ZEND_OP1_LITERAL(src));
2013-02-13 12:26:47 +00:00
MAKE_NOP(src);
}
/* T = QM_ASSIGN(C), F(T) => NOP, F(C) */
if ((ZEND_OP2_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
2013-02-22 06:56:05 +00:00
VAR_SOURCE(opline->op2) &&
VAR_SOURCE(opline->op2)->opcode == ZEND_QM_ASSIGN &&
ZEND_OP1_TYPE(VAR_SOURCE(opline->op2)) == IS_CONST) {
2013-02-13 12:26:47 +00:00
zend_op *src = VAR_SOURCE(opline->op2);
zval c = ZEND_OP1_LITERAL(src);
2013-02-13 12:26:47 +00:00
VAR_UNSET(opline->op2);
zval_copy_ctor(&c);
2014-12-13 22:06:14 +00:00
zend_optimizer_update_op2_const(op_array, opline, &c);
literal_dtor(&ZEND_OP1_LITERAL(src));
2013-02-13 12:26:47 +00:00
MAKE_NOP(src);
}
/* T = CAST(X, String), ECHO(T) => NOP, ECHO(X) */
if (opline->opcode == ZEND_ECHO &&
ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR) &&
2013-02-22 06:56:05 +00:00
VAR_SOURCE(opline->op1) &&
VAR_SOURCE(opline->op1)->opcode == ZEND_CAST &&
VAR_SOURCE(opline->op1)->extended_value == IS_STRING) {
2013-02-13 12:26:47 +00:00
zend_op *src = VAR_SOURCE(opline->op1);
COPY_NODE(opline->op1, src->op1);
MAKE_NOP(src);
}
/* T = BOOL(X), FREE(T) => NOP */
2013-02-22 06:56:05 +00:00
if (opline->opcode == ZEND_FREE &&
ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
VAR_SOURCE(opline->op1)) {
2013-02-13 12:26:47 +00:00
zend_op *src = VAR_SOURCE(opline->op1);
if (src->opcode == ZEND_BOOL) {
if (ZEND_OP1_TYPE(src) == IS_CONST) {
literal_dtor(&ZEND_OP1_LITERAL(src));
}
MAKE_NOP(src);
MAKE_NOP(opline);
}
}
#if 0
/* pre-evaluate functions:
constant(x)
defined(x)
function_exists(x)
extension_loaded(x)
BAD: interacts badly with Accelerator
*/
if((ZEND_OP1_TYPE(opline) & IS_VAR) &&
VAR_SOURCE(opline->op1) && VAR_SOURCE(opline->op1)->opcode == ZEND_DO_CF_FCALL &&
VAR_SOURCE(opline->op1)->extended_value == 1) {
zend_op *fcall = VAR_SOURCE(opline->op1);
zend_op *sv = fcall-1;
if(sv >= block->start_opline && sv->opcode == ZEND_SEND_VAL &&
ZEND_OP1_TYPE(sv) == IS_CONST && Z_TYPE(OPLINE_OP1_LITERAL(sv)) == IS_STRING &&
2014-08-25 17:24:55 +00:00
Z_LVAL(OPLINE_OP2_LITERAL(sv)) == 1
2013-02-13 12:26:47 +00:00
) {
zval *arg = &OPLINE_OP1_LITERAL(sv);
2014-08-25 17:24:55 +00:00
char *fname = FUNCTION_CACHE->funcs[Z_LVAL(ZEND_OP1_LITERAL(fcall))].function_name;
int flen = FUNCTION_CACHE->funcs[Z_LVAL(ZEND_OP1_LITERAL(fcall))].name_len;
2013-02-13 12:26:47 +00:00
if(flen == sizeof("defined")-1 && zend_binary_strcasecmp(fname, flen, "defined", sizeof("defined")-1) == 0) {
zval c;
2014-12-14 13:07:59 +00:00
if(zend_optimizer_get_persistent_constant(Z_STR_P(arg), &c, 0 ELS_CC) != 0) {
2013-02-13 12:26:47 +00:00
literal_dtor(arg);
MAKE_NOP(sv);
MAKE_NOP(fcall);
LITERAL_BOOL(opline->op1, 1);
ZEND_OP1_TYPE(opline) = IS_CONST;
}
} else if((flen == sizeof("function_exists")-1 && zend_binary_strcasecmp(fname, flen, "function_exists", sizeof("function_exists")-1) == 0) ||
(flen == sizeof("is_callable")-1 && zend_binary_strcasecmp(fname, flen, "is_callable", sizeof("is_callable")-1) == 0)
) {
zend_function *function;
if((function = zend_hash_find_ptr(EG(function_table), Z_STR_P(arg))) != NULL) {
2013-02-13 12:26:47 +00:00
literal_dtor(arg);
MAKE_NOP(sv);
MAKE_NOP(fcall);
LITERAL_BOOL(opline->op1, 1);
ZEND_OP1_TYPE(opline) = IS_CONST;
}
} else if(flen == sizeof("constant")-1 && zend_binary_strcasecmp(fname, flen, "constant", sizeof("constant")-1) == 0) {
zval c;
2014-12-14 13:07:59 +00:00
if(zend_optimizer_get_persistent_constant(Z_STR_P(arg), &c, 1 ELS_CC) != 0) {
2013-02-13 12:26:47 +00:00
literal_dtor(arg);
MAKE_NOP(sv);
MAKE_NOP(fcall);
2014-12-13 22:06:14 +00:00
ZEND_OP1_LITERAL(opline) = zend_optimizer_add_literal(op_array, &c);
2013-02-13 12:26:47 +00:00
/* no copy ctor - get already copied it */
ZEND_OP1_TYPE(opline) = IS_CONST;
}
} else if(flen == sizeof("extension_loaded")-1 && zend_binary_strcasecmp(fname, flen, "extension_loaded", sizeof("extension_loaded")-1) == 0) {
if(zend_hash_exists(&module_registry, Z_STR_P(arg))) {
2013-02-13 12:26:47 +00:00
literal_dtor(arg);
MAKE_NOP(sv);
MAKE_NOP(fcall);
LITERAL_BOOL(opline->op1, 1);
ZEND_OP1_TYPE(opline) = IS_CONST;
}
}
}
}
#endif
/* IS_EQ(TRUE, X) => BOOL(X)
* IS_EQ(FALSE, X) => BOOL_NOT(X)
* IS_NOT_EQ(TRUE, X) => BOOL_NOT(X)
* IS_NOT_EQ(FALSE, X) => BOOL(X)
* CASE(TRUE, X) => BOOL(X)
* CASE(FALSE, X) => BOOL_NOT(X)
2013-02-13 12:26:47 +00:00
*/
2013-02-22 06:56:05 +00:00
if (opline->opcode == ZEND_IS_EQUAL ||
opline->opcode == ZEND_IS_NOT_EQUAL ||
2015-07-06 13:52:38 +00:00
/* CASE variable will be deleted later by FREE, so we can't optimize it */
(opline->opcode == ZEND_CASE && (ZEND_OP1_TYPE(opline) & (IS_CONST|IS_CV)))) {
2013-02-13 12:26:47 +00:00
if (ZEND_OP1_TYPE(opline) == IS_CONST &&
(Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_FALSE ||
Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_TRUE)) {
/* T = IS_EQUAL(TRUE, X) => T = BOOL(X) */
/* T = IS_EQUAL(FALSE, X) => T = BOOL_NOT(X) */
/* T = IS_NOT_EQUAL(TRUE, X) => T = BOOL_NOT(X) */
/* T = IS_NOT_EQUAL(FALSE, X) => T = BOOL(X) */
/* Optimization of comparison with "null" is not safe,
* because ("0" == null) is not equal to !("0")
*/
2013-02-13 12:26:47 +00:00
opline->opcode =
((opline->opcode != ZEND_IS_NOT_EQUAL) == ((Z_TYPE(ZEND_OP1_LITERAL(opline))) == IS_TRUE)) ?
2013-02-22 06:56:05 +00:00
ZEND_BOOL : ZEND_BOOL_NOT;
2013-02-13 12:26:47 +00:00
COPY_NODE(opline->op1, opline->op2);
SET_UNUSED(opline->op2);
} else if (ZEND_OP2_TYPE(opline) == IS_CONST &&
(Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_FALSE ||
Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_TRUE)) {
/* T = IS_EQUAL(X, TRUE) => T = BOOL(X) */
/* T = IS_EQUAL(X, FALSE) => T = BOOL_NOT(X) */
/* T = IS_NOT_EQUAL(X, TRUE) => T = BOOL_NOT(X) */
/* T = IS_NOT_EQUAL(X, FALSE) => T = BOOL(X) */
/* Optimization of comparison with "null" is not safe,
* because ("0" == null) is not equal to !("0")
*/
2013-02-13 12:26:47 +00:00
opline->opcode =
((opline->opcode != ZEND_IS_NOT_EQUAL) == ((Z_TYPE(ZEND_OP2_LITERAL(opline))) == IS_TRUE)) ?
2013-02-22 06:56:05 +00:00
ZEND_BOOL : ZEND_BOOL_NOT;
2013-02-13 12:26:47 +00:00
SET_UNUSED(opline->op2);
}
}
2013-02-22 06:56:05 +00:00
if ((opline->opcode == ZEND_BOOL ||
2013-02-13 12:26:47 +00:00
opline->opcode == ZEND_BOOL_NOT ||
opline->opcode == ZEND_JMPZ ||
opline->opcode == ZEND_JMPNZ ||
opline->opcode == ZEND_JMPZNZ) &&
2013-02-22 06:56:05 +00:00
ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
VAR_SOURCE(opline->op1) != NULL &&
!used_ext[VAR_NUM(ZEND_OP1(opline).var)] &&
VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL_NOT) {
2013-02-13 12:26:47 +00:00
/* T = BOOL_NOT(X) + JMPZ(T) -> NOP, JMPNZ(X) */
zend_op *src = VAR_SOURCE(opline->op1);
COPY_NODE(opline->op1, src->op1);
2013-02-22 06:56:05 +00:00
switch (opline->opcode) {
2013-02-13 12:26:47 +00:00
case ZEND_BOOL:
/* T = BOOL_NOT(X) + BOOL(T) -> NOP, BOOL_NOT(X) */
opline->opcode = ZEND_BOOL_NOT;
break;
case ZEND_BOOL_NOT:
/* T = BOOL_NOT(X) + BOOL_BOOL(T) -> NOP, BOOL(X) */
opline->opcode = ZEND_BOOL;
break;
case ZEND_JMPZ:
/* T = BOOL_NOT(X) + JMPZ(T,L) -> NOP, JMPNZ(X,L) */
opline->opcode = ZEND_JMPNZ;
break;
case ZEND_JMPNZ:
/* T = BOOL_NOT(X) + JMPNZ(T,L) -> NOP, JMPZ(X,L) */
opline->opcode = ZEND_JMPZ;
break;
case ZEND_JMPZNZ:
{
/* T = BOOL_NOT(X) + JMPZNZ(T,L1,L2) -> NOP, JMPZNZ(X,L2,L1) */
int op_t;
zend_code_block *op_b;
op_t = opline->extended_value;
opline->extended_value = ZEND_OP2(opline).opline_num;
ZEND_OP2(opline).opline_num = op_t;
op_b = block->ext_to;
block->ext_to = block->op2_to;
block->op2_to = op_b;
}
break;
}
VAR_UNSET(opline->op1);
MAKE_NOP(src);
continue;
} else
#if 0
/* T = BOOL_NOT(X) + T = JMPZ_EX(T, X) -> T = BOOL_NOT(X), JMPNZ(X) */
if(0 && (opline->opcode == ZEND_JMPZ_EX ||
opline->opcode == ZEND_JMPNZ_EX) &&
ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
VAR_SOURCE(opline->op1) != NULL &&
VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL_NOT &&
ZEND_OP1(opline).var == ZEND_RESULT(opline).var
) {
zend_op *src = VAR_SOURCE(opline->op1);
if(opline->opcode == ZEND_JMPZ_EX) {
opline->opcode = ZEND_JMPNZ;
} else {
opline->opcode = ZEND_JMPZ;
}
COPY_NODE(opline->op1, src->op1);
SET_UNUSED(opline->result);
continue;
} else
#endif
/* T = BOOL(X) + JMPZ(T) -> NOP, JMPZ(X) */
2013-02-22 06:56:05 +00:00
if ((opline->opcode == ZEND_BOOL ||
2013-02-13 12:26:47 +00:00
opline->opcode == ZEND_BOOL_NOT ||
opline->opcode == ZEND_JMPZ ||
opline->opcode == ZEND_JMPZ_EX ||
opline->opcode == ZEND_JMPNZ_EX ||
opline->opcode == ZEND_JMPNZ ||
opline->opcode == ZEND_JMPZNZ) &&
(ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
2013-02-22 06:56:05 +00:00
VAR_SOURCE(opline->op1) != NULL &&
(!used_ext[VAR_NUM(ZEND_OP1(opline).var)] ||
((ZEND_RESULT_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
2013-02-13 12:26:47 +00:00
ZEND_RESULT(opline).var == ZEND_OP1(opline).var)) &&
2013-02-22 06:56:05 +00:00
(VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN)) {
2013-02-13 12:26:47 +00:00
zend_op *src = VAR_SOURCE(opline->op1);
COPY_NODE(opline->op1, src->op1);
VAR_UNSET(opline->op1);
MAKE_NOP(src);
continue;
2013-02-22 06:56:05 +00:00
} else if (last_op && opline->opcode == ZEND_ECHO &&
2013-02-13 12:26:47 +00:00
last_op->opcode == ZEND_ECHO &&
ZEND_OP1_TYPE(opline) == IS_CONST &&
Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_DOUBLE &&
ZEND_OP1_TYPE(last_op) == IS_CONST &&
Z_TYPE(ZEND_OP1_LITERAL(last_op)) != IS_DOUBLE) {
/* compress consecutive ECHO's.
* Float to string conversion may be affected by current
* locale setting.
*/
2014-03-31 19:38:37 +00:00
int l, old_len;
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
if (Z_TYPE(ZEND_OP1_LITERAL(opline)) != IS_STRING) {
2013-02-13 12:26:47 +00:00
convert_to_string_safe(&ZEND_OP1_LITERAL(opline));
}
2013-02-22 06:56:05 +00:00
if (Z_TYPE(ZEND_OP1_LITERAL(last_op)) != IS_STRING) {
2013-02-13 12:26:47 +00:00
convert_to_string_safe(&ZEND_OP1_LITERAL(last_op));
}
2014-08-25 17:24:55 +00:00
old_len = Z_STRLEN(ZEND_OP1_LITERAL(last_op));
l = old_len + Z_STRLEN(ZEND_OP1_LITERAL(opline));
if (!Z_REFCOUNTED(ZEND_OP1_LITERAL(last_op))) {
2014-08-25 17:24:55 +00:00
zend_string *tmp = zend_string_alloc(l, 0);
memcpy(ZSTR_VAL(tmp), Z_STRVAL(ZEND_OP1_LITERAL(last_op)), old_len);
2014-03-28 19:34:49 +00:00
Z_STR(ZEND_OP1_LITERAL(last_op)) = tmp;
2013-02-13 12:26:47 +00:00
} else {
Z_STR(ZEND_OP1_LITERAL(last_op)) = zend_string_extend(Z_STR(ZEND_OP1_LITERAL(last_op)), l, 0);
2013-02-13 12:26:47 +00:00
}
Z_TYPE_INFO(ZEND_OP1_LITERAL(last_op)) = IS_STRING_EX;
2014-08-25 17:24:55 +00:00
memcpy(Z_STRVAL(ZEND_OP1_LITERAL(last_op)) + old_len, Z_STRVAL(ZEND_OP1_LITERAL(opline)), Z_STRLEN(ZEND_OP1_LITERAL(opline)));
2013-02-13 12:26:47 +00:00
Z_STRVAL(ZEND_OP1_LITERAL(last_op))[l] = '\0';
zval_dtor(&ZEND_OP1_LITERAL(opline));
2014-12-13 22:06:14 +00:00
Z_STR(ZEND_OP1_LITERAL(opline)) = zend_new_interned_string(Z_STR(ZEND_OP1_LITERAL(last_op)));
if (!Z_REFCOUNTED(ZEND_OP1_LITERAL(opline))) {
Z_TYPE_FLAGS(ZEND_OP1_LITERAL(opline)) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
}
ZVAL_NULL(&ZEND_OP1_LITERAL(last_op));
2013-02-13 12:26:47 +00:00
MAKE_NOP(last_op);
} else if ((opline->opcode == ZEND_CONCAT) &&
2013-02-13 12:26:47 +00:00
ZEND_OP2_TYPE(opline) == IS_CONST &&
ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
VAR_SOURCE(opline->op1) &&
(VAR_SOURCE(opline->op1)->opcode == ZEND_CONCAT ||
VAR_SOURCE(opline->op1)->opcode == ZEND_FAST_CONCAT) &&
2013-02-13 12:26:47 +00:00
ZEND_OP2_TYPE(VAR_SOURCE(opline->op1)) == IS_CONST &&
2013-02-22 06:56:05 +00:00
ZEND_RESULT(VAR_SOURCE(opline->op1)).var == ZEND_OP1(opline).var) {
/* compress consecutive CONCAT/ADD_STRING/ADD_CHARs */
2013-02-13 12:26:47 +00:00
zend_op *src = VAR_SOURCE(opline->op1);
2014-03-31 19:38:37 +00:00
int l, old_len;
2013-02-13 12:26:47 +00:00
if (Z_TYPE(ZEND_OP2_LITERAL(opline)) != IS_STRING) {
2013-02-13 12:26:47 +00:00
convert_to_string_safe(&ZEND_OP2_LITERAL(opline));
}
if (Z_TYPE(ZEND_OP2_LITERAL(src)) != IS_STRING) {
2013-02-13 12:26:47 +00:00
convert_to_string_safe(&ZEND_OP2_LITERAL(src));
}
VAR_UNSET(opline->op1);
COPY_NODE(opline->op1, src->op1);
2014-08-25 17:24:55 +00:00
old_len = Z_STRLEN(ZEND_OP2_LITERAL(src));
l = old_len + Z_STRLEN(ZEND_OP2_LITERAL(opline));
if (!Z_REFCOUNTED(ZEND_OP2_LITERAL(src))) {
2014-08-25 17:24:55 +00:00
zend_string *tmp = zend_string_alloc(l, 0);
memcpy(ZSTR_VAL(tmp), Z_STRVAL(ZEND_OP2_LITERAL(src)), old_len);
2014-03-28 19:34:49 +00:00
Z_STR(ZEND_OP2_LITERAL(last_op)) = tmp;
2013-02-13 12:26:47 +00:00
} else {
Z_STR(ZEND_OP2_LITERAL(src)) = zend_string_extend(Z_STR(ZEND_OP2_LITERAL(src)), l, 0);
2013-02-13 12:26:47 +00:00
}
Z_TYPE_INFO(ZEND_OP2_LITERAL(last_op)) = IS_STRING_EX;
2014-08-25 17:24:55 +00:00
memcpy(Z_STRVAL(ZEND_OP2_LITERAL(src)) + old_len, Z_STRVAL(ZEND_OP2_LITERAL(opline)), Z_STRLEN(ZEND_OP2_LITERAL(opline)));
2013-02-13 12:26:47 +00:00
Z_STRVAL(ZEND_OP2_LITERAL(src))[l] = '\0';
2014-08-25 17:24:55 +00:00
zend_string_release(Z_STR(ZEND_OP2_LITERAL(opline)));
2014-12-13 22:06:14 +00:00
Z_STR(ZEND_OP2_LITERAL(opline)) = zend_new_interned_string(Z_STR(ZEND_OP2_LITERAL(src)));
if (!Z_REFCOUNTED(ZEND_OP2_LITERAL(opline))) {
Z_TYPE_FLAGS(ZEND_OP2_LITERAL(opline)) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
}
ZVAL_NULL(&ZEND_OP2_LITERAL(src));
2013-02-13 12:26:47 +00:00
MAKE_NOP(src);
2013-02-22 06:56:05 +00:00
} else if ((opline->opcode == ZEND_ADD ||
opline->opcode == ZEND_SUB ||
opline->opcode == ZEND_MUL ||
opline->opcode == ZEND_DIV ||
opline->opcode == ZEND_MOD ||
opline->opcode == ZEND_SL ||
opline->opcode == ZEND_SR ||
opline->opcode == ZEND_CONCAT ||
opline->opcode == ZEND_FAST_CONCAT ||
2013-02-22 06:56:05 +00:00
opline->opcode == ZEND_IS_EQUAL ||
opline->opcode == ZEND_IS_NOT_EQUAL ||
opline->opcode == ZEND_IS_SMALLER ||
opline->opcode == ZEND_IS_SMALLER_OR_EQUAL ||
opline->opcode == ZEND_IS_IDENTICAL ||
opline->opcode == ZEND_IS_NOT_IDENTICAL ||
opline->opcode == ZEND_BOOL_XOR ||
opline->opcode == ZEND_BW_OR ||
opline->opcode == ZEND_BW_AND ||
opline->opcode == ZEND_BW_XOR) &&
ZEND_OP1_TYPE(opline)==IS_CONST &&
ZEND_OP2_TYPE(opline)==IS_CONST) {
2013-02-13 12:26:47 +00:00
/* evaluate constant expressions */
binary_op_type binary_op = get_binary_op(opline->opcode);
2013-02-13 12:26:47 +00:00
zval result;
int er;
2013-02-22 06:56:05 +00:00
if ((opline->opcode == ZEND_DIV || opline->opcode == ZEND_MOD) &&
zval_get_long(&ZEND_OP2_LITERAL(opline)) == 0) {
if (RESULT_USED(opline)) {
SET_VAR_SOURCE(opline);
}
opline++;
continue;
} else if ((opline->opcode == ZEND_SL || opline->opcode == ZEND_SR) &&
zval_get_long(&ZEND_OP2_LITERAL(opline)) < 0) {
2013-02-22 06:56:05 +00:00
if (RESULT_USED(opline)) {
2013-02-13 12:26:47 +00:00
SET_VAR_SOURCE(opline);
}
opline++;
continue;
}
er = EG(error_reporting);
EG(error_reporting) = 0;
2014-12-13 22:06:14 +00:00
if (binary_op(&result, &ZEND_OP1_LITERAL(opline), &ZEND_OP2_LITERAL(opline)) == SUCCESS) {
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP1_LITERAL(opline));
literal_dtor(&ZEND_OP2_LITERAL(opline));
opline->opcode = ZEND_QM_ASSIGN;
SET_UNUSED(opline->op2);
2014-12-13 22:06:14 +00:00
zend_optimizer_update_op1_const(op_array, opline, &result);
2013-02-13 12:26:47 +00:00
}
EG(error_reporting) = er;
2013-02-22 06:56:05 +00:00
} else if ((opline->opcode == ZEND_BOOL ||
opline->opcode == ZEND_BOOL_NOT ||
opline->opcode == ZEND_BW_NOT) && ZEND_OP1_TYPE(opline) == IS_CONST) {
2013-02-13 12:26:47 +00:00
/* evaluate constant unary ops */
unary_op_type unary_op = get_unary_op(opline->opcode);
zval result;
2013-02-22 06:56:05 +00:00
if (unary_op) {
2014-12-13 22:06:14 +00:00
unary_op(&result, &ZEND_OP1_LITERAL(opline));
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP1_LITERAL(opline));
} else {
/* BOOL */
result = ZEND_OP1_LITERAL(opline);
convert_to_boolean(&result);
ZVAL_NULL(&ZEND_OP1_LITERAL(opline));
2013-02-13 12:26:47 +00:00
}
opline->opcode = ZEND_QM_ASSIGN;
2014-12-13 22:06:14 +00:00
zend_optimizer_update_op1_const(op_array, opline, &result);
2013-02-22 06:56:05 +00:00
} else if ((opline->opcode == ZEND_RETURN || opline->opcode == ZEND_EXIT) &&
(ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
2013-02-22 06:56:05 +00:00
VAR_SOURCE(opline->op1) &&
VAR_SOURCE(opline->op1)->opcode == ZEND_QM_ASSIGN) {
2013-02-13 12:26:47 +00:00
/* T = QM_ASSIGN(X), RETURN(T) to RETURN(X) */
zend_op *src = VAR_SOURCE(opline->op1);
VAR_UNSET(opline->op1);
COPY_NODE(opline->op1, src->op1);
MAKE_NOP(src);
} else if (opline->opcode == ZEND_CONCAT || opline->opcode == ZEND_FAST_CONCAT) {
2015-03-20 02:24:04 +00:00
if ((ZEND_OP1_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
VAR_SOURCE(opline->op1) &&
VAR_SOURCE(opline->op1)->opcode == ZEND_CAST &&
VAR_SOURCE(opline->op1)->extended_value == IS_STRING) {
/* convert T1 = CAST(STRING, X), T2 = CONCAT(T1, Y) to T2 = CONCAT(X,Y) */
zend_op *src = VAR_SOURCE(opline->op1);
VAR_UNSET(opline->op1);
COPY_NODE(opline->op1, src->op1);
MAKE_NOP(src);
2013-02-13 12:26:47 +00:00
}
2015-03-20 02:24:04 +00:00
if ((ZEND_OP2_TYPE(opline) & (IS_TMP_VAR|IS_VAR)) &&
VAR_SOURCE(opline->op2) &&
VAR_SOURCE(opline->op2)->opcode == ZEND_CAST &&
VAR_SOURCE(opline->op2)->extended_value == IS_STRING) {
/* convert T1 = CAST(STRING, X), T2 = CONCAT(Y, T1) to T2 = CONCAT(Y,X) */
zend_op *src = VAR_SOURCE(opline->op2);
VAR_UNSET(opline->op2);
COPY_NODE(opline->op2, src->op1);
MAKE_NOP(src);
}
if (ZEND_OP1_TYPE(opline) == IS_CONST &&
Z_TYPE(ZEND_OP1_LITERAL(opline)) == IS_STRING &&
Z_STRLEN(ZEND_OP1_LITERAL(opline)) == 0) {
/* convert CONCAT('', X) => CAST(STRING, X) */
literal_dtor(&ZEND_OP1_LITERAL(opline));
opline->opcode = ZEND_CAST;
opline->extended_value = IS_STRING;
COPY_NODE(opline->op1, opline->op2);
opline->op2_type = IS_UNUSED;
opline->op2.var = 0;
} else if (ZEND_OP2_TYPE(opline) == IS_CONST &&
Z_TYPE(ZEND_OP2_LITERAL(opline)) == IS_STRING &&
Z_STRLEN(ZEND_OP2_LITERAL(opline)) == 0) {
/* convert CONCAT(X, '') => CAST(STRING, X) */
literal_dtor(&ZEND_OP2_LITERAL(opline));
opline->opcode = ZEND_CAST;
opline->extended_value = IS_STRING;
opline->op2_type = IS_UNUSED;
opline->op2.var = 0;
} else if (opline->opcode == ZEND_CONCAT &&
(opline->op1_type == IS_CONST ||
(opline->op1_type == IS_TMP_VAR &&
VAR_SOURCE(opline->op1) &&
(VAR_SOURCE(opline->op1)->opcode == ZEND_FAST_CONCAT ||
VAR_SOURCE(opline->op1)->opcode == ZEND_ROPE_END ||
VAR_SOURCE(opline->op1)->opcode == ZEND_FETCH_CONSTANT))) &&
(opline->op2_type == IS_CONST ||
(opline->op2_type == IS_TMP_VAR &&
VAR_SOURCE(opline->op2) &&
(VAR_SOURCE(opline->op2)->opcode == ZEND_FAST_CONCAT ||
VAR_SOURCE(opline->op2)->opcode == ZEND_ROPE_END ||
VAR_SOURCE(opline->op2)->opcode == ZEND_FETCH_CONSTANT)))) {
opline->opcode = ZEND_FAST_CONCAT;
2013-02-13 12:26:47 +00:00
}
2013-02-22 06:56:05 +00:00
} else if (opline->opcode == ZEND_QM_ASSIGN &&
ZEND_OP1_TYPE(opline) == ZEND_RESULT_TYPE(opline) &&
2013-02-22 06:56:05 +00:00
ZEND_OP1(opline).var == ZEND_RESULT(opline).var) {
2013-02-13 12:26:47 +00:00
/* strip T = QM_ASSIGN(T) */
MAKE_NOP(opline);
2013-02-22 06:56:05 +00:00
} else if (opline->opcode == ZEND_BOOL &&
ZEND_OP1_TYPE(opline) == IS_TMP_VAR &&
VAR_SOURCE(opline->op1) &&
(VAR_SOURCE(opline->op1)->opcode == ZEND_IS_EQUAL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_IS_NOT_EQUAL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_IS_SMALLER ||
VAR_SOURCE(opline->op1)->opcode == ZEND_IS_SMALLER_OR_EQUAL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_BOOL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_IS_IDENTICAL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_IS_NOT_IDENTICAL ||
VAR_SOURCE(opline->op1)->opcode == ZEND_ISSET_ISEMPTY_VAR ||
VAR_SOURCE(opline->op1)->opcode == ZEND_ISSET_ISEMPTY_DIM_OBJ) &&
!used_ext[VAR_NUM(ZEND_OP1(opline).var)]) {
2013-02-13 12:26:47 +00:00
/* T = IS_SMALLER(X, Y), T1 = BOOL(T) => T = IS_SMALLER(X, Y), T1 = QM_ASSIGN(T) */
zend_op *src = VAR_SOURCE(opline->op1);
COPY_NODE(src->result, opline->result);
SET_VAR_SOURCE(src);
MAKE_NOP(opline);
}
/* get variable source */
2013-02-22 06:56:05 +00:00
if (RESULT_USED(opline)) {
2013-02-13 12:26:47 +00:00
SET_VAR_SOURCE(opline);
}
2013-02-22 06:56:05 +00:00
if (opline->opcode != ZEND_NOP) {
2013-02-13 12:26:47 +00:00
last_op = opline;
}
opline++;
}
strip_nop(block, ctx);
2013-02-13 12:26:47 +00:00
}
/* Rebuild plain (optimized) op_array from CFG */
static void assemble_code_blocks(zend_cfg *cfg, zend_op_array *op_array)
2013-02-13 12:26:47 +00:00
{
zend_code_block *blocks = cfg->blocks;
2013-02-25 07:29:54 +00:00
zend_op *new_opcodes = emalloc(op_array->last * sizeof(zend_op));
2013-02-13 12:26:47 +00:00
zend_op *opline = new_opcodes;
zend_code_block *cur_block = blocks;
/* Copy code of reachable blocks into a single buffer */
2013-02-22 06:56:05 +00:00
while (cur_block) {
if (cur_block->access) {
2013-02-25 07:29:54 +00:00
memcpy(opline, cur_block->start_opline, cur_block->len * sizeof(zend_op));
2013-02-13 12:26:47 +00:00
cur_block->start_opline = opline;
opline += cur_block->len;
2013-02-25 07:29:54 +00:00
if ((opline - 1)->opcode == ZEND_JMP) {
2013-02-13 12:26:47 +00:00
zend_code_block *next;
next = cur_block->next;
2013-02-22 06:56:05 +00:00
while (next && !next->access) {
2013-02-13 12:26:47 +00:00
next = next->next;
}
2013-02-22 06:56:05 +00:00
if (next && next == cur_block->op1_to) {
2013-02-13 12:26:47 +00:00
/* JMP to the next block - strip it */
cur_block->follow_to = cur_block->op1_to;
cur_block->op1_to = NULL;
2013-02-25 07:29:54 +00:00
MAKE_NOP((opline - 1));
2013-02-13 12:26:47 +00:00
opline--;
cur_block->len--;
}
}
} else {
/* this block will not be used, delete all constants there */
zend_op *_opl;
2013-02-25 07:29:54 +00:00
zend_op *end = cur_block->start_opline + cur_block->len;
2013-02-22 06:56:05 +00:00
for (_opl = cur_block->start_opline; _opl && _opl < end; _opl++) {
if (ZEND_OP1_TYPE(_opl) == IS_CONST) {
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP1_LITERAL(_opl));
}
2013-02-22 06:56:05 +00:00
if (ZEND_OP2_TYPE(_opl) == IS_CONST) {
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP2_LITERAL(_opl));
}
}
}
cur_block = cur_block->next;
}
2013-02-13 12:26:47 +00:00
op_array->last = opline-new_opcodes;
/* adjust exception jump targets */
2013-02-22 06:56:05 +00:00
if (op_array->last_try_catch) {
int i, j;
for (i = 0, j = 0; i< op_array->last_try_catch; i++) {
if (cfg->try[i]->access) {
op_array->try_catch_array[j].try_op = cfg->try[i]->start_opline - new_opcodes;
op_array->try_catch_array[j].catch_op = cfg->catch[i]->start_opline - new_opcodes;
j++;
}
2013-02-13 12:26:47 +00:00
}
op_array->last_try_catch = j;
}
Squashed commit of the following: commit 03cf871f1576f08b2348c141b209894a7bf17a86 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:31 2015 +0300 Revert "Fixed bug #62210 (Exceptions can leak temporary variables. As a part of the fix serious refactoring was done. op_array->brk_cont_array was removed, and replaced with more general and speed efficient op_array->T_liveliness. ZEND_GOTO opcode is always replaced by ZEND_JMP at compile time). (Bob, Dmitry, Laruence)" This reverts commit 5ee841325901a4b040cfea56292a24702fe224d9. commit 285a68227ce3d380e821a24fa389aa5239bd3fe1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:26 2015 +0300 Revert "Tuned off dubugging of live ranges" This reverts commit 404dc93d35f7061fc4b1b41ad6cb0721b9b52bcc. commit 93d9d11157301ee2ec99afb6f5744b126d17f637 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:17 2015 +0300 Revert "Remove loop_var_stack" This reverts commit b3a4c05071c3786e27e1326fa1b4d5acad62fccd. commit ede68ebbc284aec79e3f719f2c8dbf9da6907752 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:12 2015 +0300 Revert "ZEND_SEPARATE reuses temporaries" This reverts commit 1852f538b9f8d5e7d67fe5a4f6080396d8b10034. commit 96d8995dc1f517fb01b481736273767509f76c47 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:10 2015 +0300 Revert "Add assertion in liveliness computation" This reverts commit ed14019e8c0c852480eebc6fc552d8c3d939dce1. commit 0649d7bfef152e6cc8e67b922534e9946c634d9c Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:07 2015 +0300 Revert "Fixed invalid live-range detection" This reverts commit 54f367ee2a2e4cb7c952b17915c226fdc56038ab. commit dfe8f3851f6b04595eb089323e3492115a59363e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:04 2015 +0300 Revert "Add test guaranteeing that loop vars are only freed after potential return type exceptions" This reverts commit f5db5a558d550bf441373febebbb02f3884209d1. commit 52a94aad6f48a199358cc07f7e4f56bb73050504 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:45:01 2015 +0300 Revert "Fixed exception habdling on "return" statement." This reverts commit 17c5315bdf8f8087979aeb55f6d3a512ba197cf5. commit 6e90ad7331901711e89c2ceb2bcab5023e5cee60 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:58 2015 +0300 Revert "Fix too early terminated temporary range with break/cont/goto" This reverts commit cc876c04b420589cb1f62b650d0c0e24975dd4af. commit 7b766e44b1970e4031f75109c302c07ead2c05cb Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jul 10 02:44:55 2015 +0300 Revert "Fixed exception catching on break/continue" This reverts commit 8c3f701eebfa92d761bb368cfa8c2d1ccf821b9d.
2015-07-10 00:31:52 +00:00
/* adjust loop jump targets */
if (op_array->last_brk_cont) {
int i;
for (i = 0; i< op_array->last_brk_cont; i++) {
op_array->brk_cont_array[i].start = cfg->loop_start[i]->start_opline - new_opcodes;
op_array->brk_cont_array[i].cont = cfg->loop_cont[i]->start_opline - new_opcodes;
op_array->brk_cont_array[i].brk = cfg->loop_brk[i]->start_opline - new_opcodes;
}
}
2013-02-13 12:26:47 +00:00
/* adjust jump targets */
2013-02-22 06:56:05 +00:00
for (cur_block = blocks; cur_block; cur_block = cur_block->next) {
if (!cur_block->access) {
2013-02-13 12:26:47 +00:00
continue;
}
2013-04-09 13:48:45 +00:00
opline = cur_block->start_opline + cur_block->len - 1;
if (opline->opcode == ZEND_OP_DATA) {
opline--;
}
2013-02-22 06:56:05 +00:00
if (cur_block->op1_to) {
2013-04-09 13:48:45 +00:00
ZEND_OP1(opline).opline_num = cur_block->op1_to->start_opline - new_opcodes;
2013-02-13 12:26:47 +00:00
}
2013-02-22 06:56:05 +00:00
if (cur_block->op2_to) {
2013-04-09 13:48:45 +00:00
ZEND_OP2(opline).opline_num = cur_block->op2_to->start_opline - new_opcodes;
2013-02-13 12:26:47 +00:00
}
2013-02-22 06:56:05 +00:00
if (cur_block->ext_to) {
2013-04-09 13:48:45 +00:00
opline->extended_value = cur_block->ext_to->start_opline - new_opcodes;
2013-02-13 12:26:47 +00:00
}
print_block(cur_block, new_opcodes, "Out ");
}
efree(op_array->opcodes);
2013-02-25 07:29:54 +00:00
op_array->opcodes = erealloc(new_opcodes, op_array->last * sizeof(zend_op));
2013-02-13 12:26:47 +00:00
/* adjust early binding list */
2014-08-25 17:28:33 +00:00
if (op_array->early_binding != (uint32_t)-1) {
uint32_t *opline_num = &op_array->early_binding;
2013-02-13 12:26:47 +00:00
zend_op *end;
opline = op_array->opcodes;
end = opline + op_array->last;
while (opline < end) {
if (opline->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
*opline_num = opline - op_array->opcodes;
opline_num = &ZEND_RESULT(opline).opline_num;
}
++opline;
}
*opline_num = -1;
}
}
2014-12-13 22:06:14 +00:00
static void zend_jmp_optimization(zend_code_block *block, zend_op_array *op_array, zend_code_block *blocks, zend_cfg *cfg, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
/* last_op is the last opcode of the current block */
2013-02-22 06:56:05 +00:00
zend_op *last_op = (block->start_opline + block->len - 1);
2013-02-13 12:26:47 +00:00
2013-03-06 18:32:21 +00:00
if (!block->len) {
return;
}
2013-02-22 06:56:05 +00:00
switch (last_op->opcode) {
2013-02-13 12:26:47 +00:00
case ZEND_JMP:
{
zend_op *target = block->op1_to->start_opline;
zend_code_block *next = block->next;
while (next && !next->access) {
/* find used one */
next = next->next;
}
/* JMP(next) -> NOP */
2013-02-22 06:56:05 +00:00
if (block->op1_to == next) {
2013-02-13 12:26:47 +00:00
block->follow_to = block->op1_to;
block->op1_to = NULL;
MAKE_NOP(last_op);
block->len--;
2013-02-22 06:56:05 +00:00
if (block->len == 0) {
2013-02-13 12:26:47 +00:00
/* this block is nothing but NOP now */
delete_code_block(block, ctx);
2013-02-13 12:26:47 +00:00
}
break;
}
2013-02-22 06:56:05 +00:00
if (((target->opcode == ZEND_JMP &&
block->op1_to != block->op1_to->op1_to) ||
target->opcode == ZEND_JMPZNZ) &&
!block->op1_to->protected) {
2013-02-13 12:26:47 +00:00
/* JMP L, L: JMP L1 -> JMP L1 */
/* JMP L, L: JMPZNZ L1,L2 -> JMPZNZ L1,L2 */
*last_op = *target;
if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
zval zv = ZEND_OP1_LITERAL(last_op);
zval_copy_ctor(&zv);
2014-12-13 22:06:14 +00:00
last_op->op1.constant = zend_optimizer_add_literal(op_array, &zv);
}
2013-02-13 12:26:47 +00:00
del_source(block, block->op1_to);
if (block->op1_to->op2_to) {
block->op2_to = block->op1_to->op2_to;
ADD_SOURCE(block, block->op2_to);
}
if (block->op1_to->ext_to) {
block->ext_to = block->op1_to->ext_to;
ADD_SOURCE(block, block->ext_to);
}
if (block->op1_to->op1_to) {
block->op1_to = block->op1_to->op1_to;
ADD_SOURCE(block, block->op1_to);
} else {
block->op1_to = NULL;
}
2013-02-22 06:56:05 +00:00
} else if (target->opcode == ZEND_RETURN ||
2013-02-13 12:26:47 +00:00
target->opcode == ZEND_RETURN_BY_REF ||
target->opcode == ZEND_FAST_RET ||
target->opcode == ZEND_EXIT) {
/* JMP L, L: RETURN to immediate RETURN */
*last_op = *target;
if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
zval zv = ZEND_OP1_LITERAL(last_op);
zval_copy_ctor(&zv);
2014-12-13 22:06:14 +00:00
last_op->op1.constant = zend_optimizer_add_literal(op_array, &zv);
}
2013-02-13 12:26:47 +00:00
del_source(block, block->op1_to);
block->op1_to = NULL;
#if 0
/* Temporarily disabled - see bug #0025274 */
} else if (0&& block->op1_to != block &&
block->op1_to != blocks &&
op_array->last_try_catch == 0 &&
target->opcode != ZEND_FREE) {
2013-02-13 12:26:47 +00:00
/* Block Reordering (saves one JMP on each "for" loop iteration)
* It is disabled for some cases (ZEND_FREE)
2013-02-13 12:26:47 +00:00
* which may break register allocation.
*/
zend_bool can_reorder = 0;
zend_block_source *cs = block->op1_to->sources;
/* the "target" block doesn't had any followed block */
while(cs) {
if (cs->from->follow_to == block->op1_to) {
can_reorder = 0;
break;
}
cs = cs->next;
}
if (can_reorder) {
next = block->op1_to;
/* the "target" block is not followed by current "block" */
while (next->follow_to != NULL) {
if (next->follow_to == block) {
can_reorder = 0;
break;
}
next = next->follow_to;
}
if (can_reorder) {
zend_code_block *prev = blocks;
while (prev->next != block->op1_to) {
prev = prev->next;
}
prev->next = next->next;
next->next = block->next;
block->next = block->op1_to;
block->follow_to = block->op1_to;
block->op1_to = NULL;
MAKE_NOP(last_op);
block->len--;
if(block->len == 0) {
/* this block is nothing but NOP now */
delete_code_block(block, ctx);
2013-02-13 12:26:47 +00:00
}
break;
}
}
#endif
}
}
break;
case ZEND_JMPZ:
case ZEND_JMPNZ:
/* constant conditional JMPs */
2013-02-22 06:56:05 +00:00
if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
2014-12-13 22:06:14 +00:00
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(last_op));
2013-02-13 12:26:47 +00:00
if (last_op->opcode == ZEND_JMPZ) {
should_jmp = !should_jmp;
}
literal_dtor(&ZEND_OP1_LITERAL(last_op));
ZEND_OP1_TYPE(last_op) = IS_UNUSED;
if (should_jmp) {
/* JMPNZ(true) -> JMP */
last_op->opcode = ZEND_JMP;
COPY_NODE(last_op->op1, last_op->op2);
block->op1_to = block->op2_to;
del_source(block, block->follow_to);
block->op2_to = NULL;
block->follow_to = NULL;
} else {
/* JMPNZ(false) -> NOP */
MAKE_NOP(last_op);
del_source(block, block->op2_to);
block->op2_to = NULL;
}
break;
}
if (block->op2_to == block->follow_to) {
/* L: JMPZ(X, L+1) -> NOP or FREE(X) */
if (last_op->op1_type == IS_VAR) {
zend_op **Tsource = cfg->Tsource;
zend_op *src = VAR_SOURCE(last_op->op1);
if (src &&
src->opcode != ZEND_FETCH_R &&
src->opcode != ZEND_FETCH_DIM_R &&
src->opcode != ZEND_FETCH_OBJ_R) {
ZEND_RESULT_TYPE(src) |= EXT_TYPE_UNUSED;
MAKE_NOP(last_op);
block->op2_to = NULL;
break;
}
}
if (last_op->op1_type & (IS_VAR|IS_TMP_VAR)) {
last_op->opcode = ZEND_FREE;
last_op->op2.num = 0;
block->op2_to = NULL;
} else {
MAKE_NOP(last_op);
block->op2_to = NULL;
}
break;
}
2013-02-22 06:56:05 +00:00
if (block->op2_to) {
2013-02-13 12:26:47 +00:00
zend_uchar same_type = ZEND_OP1_TYPE(last_op);
2014-08-25 17:28:33 +00:00
uint32_t same_var = VAR_NUM_EX(last_op->op1);
2013-02-13 12:26:47 +00:00
zend_op *target;
zend_op *target_end;
zend_code_block *target_block = block->op2_to;;
next_target:
target = target_block->start_opline;
target_end = target_block->start_opline + target_block->len;
2013-02-22 06:56:05 +00:00
while (target < target_end && target->opcode == ZEND_NOP) {
2013-02-13 12:26:47 +00:00
target++;
}
/* next block is only NOP's */
2013-02-22 06:56:05 +00:00
if (target == target_end) {
2013-02-13 12:26:47 +00:00
target_block = target_block->follow_to;
goto next_target;
2013-02-22 06:56:05 +00:00
} else if (target->opcode == INV_COND(last_op->opcode) &&
/* JMPZ(X, L), L: JMPNZ(X, L2) -> JMPZ(X, L+1) */
2013-02-13 12:26:47 +00:00
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
same_type == ZEND_OP1_TYPE(target) &&
same_var == VAR_NUM_EX(target->op1) &&
target_block->follow_to &&
!target_block->protected
2013-02-13 12:26:47 +00:00
) {
del_source(block, block->op2_to);
block->op2_to = target_block->follow_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target->opcode == INV_COND_EX(last_op->opcode) &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
same_type == ZEND_OP1_TYPE(target) &&
same_var == VAR_NUM_EX(target->op1) &&
target_block->follow_to &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZ(X, L), L: X = JMPNZ_EX(X, L2) -> JMPZ(X, L+1) */
2013-02-13 12:26:47 +00:00
last_op->opcode += 3;
last_op->result = target->result;
del_source(block, block->op2_to);
block->op2_to = target_block->follow_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target->opcode == last_op->opcode &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
same_type == ZEND_OP1_TYPE(target) &&
same_var == VAR_NUM_EX(target->op1) &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZ(X, L), L: JMPZ(X, L2) -> JMPZ(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op2_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op1_to &&
target->opcode == ZEND_JMP &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZ(X, L), L: JMP(L2) -> JMPZ(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op1_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target_block->ext_to &&
target->opcode == ZEND_JMPZNZ &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
same_type == ZEND_OP1_TYPE(target) &&
same_var == VAR_NUM_EX(target->op1) &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZ(X, L), L: JMPZNZ(X, L2, L3) -> JMPZ(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
if (last_op->opcode == ZEND_JMPZ) {
block->op2_to = target_block->op2_to;
} else {
block->op2_to = target_block->ext_to;
}
ADD_SOURCE(block, block->op2_to);
}
}
if (block->follow_to &&
(last_op->opcode == ZEND_JMPZ || last_op->opcode == ZEND_JMPNZ)) {
zend_op *target;
zend_op *target_end;
while (1) {
target = block->follow_to->start_opline;
target_end = block->follow_to->start_opline + block->follow_to->len;
while (target < target_end && target->opcode == ZEND_NOP) {
target++;
}
/* next block is only NOP's */
if (target == target_end && ! block->follow_to->protected) {
2013-02-13 12:26:47 +00:00
del_source(block, block->follow_to);
block->follow_to = block->follow_to->follow_to;
ADD_SOURCE(block, block->follow_to);
} else {
break;
}
}
/* JMPZ(X,L1), JMP(L2) -> JMPZNZ(X,L1,L2) */
if (target->opcode == ZEND_JMP &&
block->follow_to->op1_to &&
!block->follow_to->protected) {
2013-02-13 12:26:47 +00:00
del_source(block, block->follow_to);
if (last_op->opcode == ZEND_JMPZ) {
block->ext_to = block->follow_to->op1_to;
ADD_SOURCE(block, block->ext_to);
} else {
block->ext_to = block->op2_to;
block->op2_to = block->follow_to->op1_to;
ADD_SOURCE(block, block->op2_to);
}
block->follow_to = NULL;
last_op->opcode = ZEND_JMPZNZ;
}
}
break;
case ZEND_JMPNZ_EX:
case ZEND_JMPZ_EX:
/* constant conditional JMPs */
2013-02-22 06:56:05 +00:00
if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
2014-12-13 22:06:14 +00:00
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(last_op));
2013-02-13 12:26:47 +00:00
if (last_op->opcode == ZEND_JMPZ_EX) {
should_jmp = !should_jmp;
}
if (!should_jmp) {
/* T = JMPZ_EX(true,L) -> T = QM_ASSIGN(true)
* T = JMPNZ_EX(false,L) -> T = QM_ASSIGN(false)
*/
last_op->opcode = ZEND_QM_ASSIGN;
SET_UNUSED(last_op->op2);
del_source(block, block->op2_to);
block->op2_to = NULL;
}
break;
}
2013-02-22 06:56:05 +00:00
if (block->op2_to) {
2013-02-13 12:26:47 +00:00
zend_op *target, *target_end;
char *same_t=NULL;
zend_code_block *target_block;
2014-03-31 14:13:16 +00:00
int var_num = op_array->last_var + op_array->T;
2015-01-03 09:22:58 +00:00
2013-02-22 06:56:05 +00:00
if (var_num <= 0) {
2013-02-13 12:26:47 +00:00
return;
}
same_t = cfg->same_t;
memset(same_t, 0, var_num);
2013-02-13 12:26:47 +00:00
same_t[VAR_NUM_EX(last_op->op1)] |= ZEND_OP1_TYPE(last_op);
same_t[VAR_NUM_EX(last_op->result)] |= ZEND_RESULT_TYPE(last_op);
target_block = block->op2_to;
next_target_ex:
target = target_block->start_opline;
target_end = target_block->start_opline + target_block->len;
2013-02-22 06:56:05 +00:00
while (target < target_end && target->opcode == ZEND_NOP) {
2013-02-13 12:26:47 +00:00
target++;
}
/* next block is only NOP's */
2013-02-22 06:56:05 +00:00
if (target == target_end) {
2013-02-13 12:26:47 +00:00
target_block = target_block->follow_to;
goto next_target_ex;
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target->opcode == last_op->opcode-3 &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
(same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* T = JMPZ_EX(X, L1), L1: JMPZ({X|T}, L2) -> T = JMPZ_EX(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op2_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target->opcode == INV_EX_COND(last_op->opcode) &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
(same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
!target_block->protected) {
2013-02-13 12:26:47 +00:00
/* T = JMPZ_EX(X, L1), L1: JMPNZ({X|T1}, L2) -> T = JMPZ_EX(X, L1+1) */
del_source(block, block->op2_to);
block->op2_to = target_block->follow_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target->opcode == INV_EX_COND_EX(last_op->opcode) &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
(same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
(same_t[VAR_NUM_EX(target->result)] & ZEND_RESULT_TYPE(target)) != 0 &&
!target_block->protected) {
2013-02-13 12:26:47 +00:00
/* T = JMPZ_EX(X, L1), L1: T = JMPNZ_EX(T, L2) -> T = JMPZ_EX(X, L1+1) */
del_source(block, block->op2_to);
block->op2_to = target_block->follow_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target->opcode == last_op->opcode &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
(same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
(same_t[VAR_NUM_EX(target->result)] & ZEND_RESULT_TYPE(target)) != 0 &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* T = JMPZ_EX(X, L1), L1: T = JMPZ({X|T}, L2) -> T = JMPZ_EX(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op2_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op1_to &&
target->opcode == ZEND_JMP &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* T = JMPZ_EX(X, L), L: JMP(L2) -> T = JMPZ(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op1_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
target_block->ext_to &&
target->opcode == ZEND_JMPZNZ &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
(same_t[VAR_NUM_EX(target->op1)] & ZEND_OP1_TYPE(target)) != 0 &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* T = JMPZ_EX(X, L), L: JMPZNZ({X|T}, L2, L3) -> T = JMPZ_EX(X, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
if (last_op->opcode == ZEND_JMPZ_EX) {
block->op2_to = target_block->op2_to;
} else {
block->op2_to = target_block->ext_to;
}
ADD_SOURCE(block, block->op2_to);
}
}
break;
case ZEND_JMPZNZ: {
zend_code_block *next = block->next;
while (next && !next->access) {
/* find first accessed one */
next = next->next;
}
2013-02-22 06:56:05 +00:00
if (ZEND_OP1_TYPE(last_op) == IS_CONST) {
2014-12-13 22:06:14 +00:00
if (!zend_is_true(&ZEND_OP1_LITERAL(last_op))) {
2013-02-13 12:26:47 +00:00
/* JMPZNZ(false,L1,L2) -> JMP(L1) */
zend_code_block *todel;
2013-02-22 06:56:05 +00:00
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP1_LITERAL(last_op));
last_op->opcode = ZEND_JMP;
SET_UNUSED(last_op->op1);
SET_UNUSED(last_op->op2);
block->op1_to = block->op2_to;
todel = block->ext_to;
block->op2_to = NULL;
block->ext_to = NULL;
del_source(block, todel);
} else {
/* JMPZNZ(true,L1,L2) -> JMP(L2) */
zend_code_block *todel;
2013-02-22 06:56:05 +00:00
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP1_LITERAL(last_op));
last_op->opcode = ZEND_JMP;
SET_UNUSED(last_op->op1);
SET_UNUSED(last_op->op2);
block->op1_to = block->ext_to;
todel = block->op2_to;
block->op2_to = NULL;
block->ext_to = NULL;
del_source(block, todel);
}
} else if (block->op2_to == block->ext_to) {
/* both goto the same one - it's JMP */
if (!(last_op->op1_type & (IS_VAR|IS_TMP_VAR))) {
/* JMPZNZ(?,L,L) -> JMP(L) */
last_op->opcode = ZEND_JMP;
SET_UNUSED(last_op->op1);
SET_UNUSED(last_op->op2);
block->op1_to = block->op2_to;
block->op2_to = NULL;
block->ext_to = NULL;
}
2013-02-13 12:26:47 +00:00
} else if (block->op2_to == next) {
/* jumping to next on Z - can follow to it and jump only on NZ */
/* JMPZNZ(X,L1,L2) L1: -> JMPNZ(X,L2) */
last_op->opcode = ZEND_JMPNZ;
block->op2_to = block->ext_to;
block->follow_to = next;
block->ext_to = NULL;
/* no need to add source - it's block->op2_to */
} else if (block->ext_to == next) {
/* jumping to next on NZ - can follow to it and jump only on Z */
/* JMPZNZ(X,L1,L2) L2: -> JMPZ(X,L1) */
last_op->opcode = ZEND_JMPZ;
block->follow_to = next;
block->ext_to = NULL;
/* no need to add source - it's block->ext_to */
}
2013-02-22 06:56:05 +00:00
if (last_op->opcode == ZEND_JMPZNZ && block->op2_to) {
2013-02-13 12:26:47 +00:00
zend_uchar same_type = ZEND_OP1_TYPE(last_op);
zend_uchar same_var = VAR_NUM_EX(last_op->op1);
zend_op *target;
zend_op *target_end;
zend_code_block *target_block = block->op2_to;
next_target_znz:
target = target_block->start_opline;
target_end = target_block->start_opline + target_block->len;
2013-02-22 06:56:05 +00:00
while (target < target_end && target->opcode == ZEND_NOP) {
2013-02-13 12:26:47 +00:00
target++;
}
/* next block is only NOP's */
2013-02-22 06:56:05 +00:00
if (target == target_end) {
2013-02-13 12:26:47 +00:00
target_block = target_block->follow_to;
goto next_target_znz;
2013-02-22 06:56:05 +00:00
} else if (target_block->op2_to &&
(target->opcode == ZEND_JMPZ || target->opcode == ZEND_JMPZNZ) &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
same_type == ZEND_OP1_TYPE(target) &&
same_var == VAR_NUM_EX(target->op1) &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZNZ(X, L1, L2), L1: JMPZ(X, L3) -> JMPZNZ(X, L3, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op2_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target->opcode == ZEND_JMPNZ &&
(ZEND_OP1_TYPE(target) & (IS_TMP_VAR|IS_CV)) &&
same_type == ZEND_OP1_TYPE(target) &&
same_var == VAR_NUM_EX(target->op1) &&
target_block->follow_to &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZNZ(X, L1, L2), L1: X = JMPNZ(X, L3) -> JMPZNZ(X, L1+1, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->follow_to;
ADD_SOURCE(block, block->op2_to);
2013-02-22 06:56:05 +00:00
} else if (target_block->op1_to &&
target->opcode == ZEND_JMP &&
!target_block->protected) {
2013-02-22 06:56:05 +00:00
/* JMPZNZ(X, L1, L2), L1: JMP(L3) -> JMPZNZ(X, L3, L2) */
2013-02-13 12:26:47 +00:00
del_source(block, block->op2_to);
block->op2_to = target_block->op1_to;
ADD_SOURCE(block, block->op2_to);
}
}
break;
}
}
}
/* Global data dependencies */
2014-08-15 08:40:07 +00:00
#define T_USAGE(op) do { \
2013-02-22 06:56:05 +00:00
if ((op ## _type & (IS_VAR | IS_TMP_VAR)) && \
2013-02-13 12:26:47 +00:00
!defined_here[VAR_NUM(op.var)] && !used_ext[VAR_NUM(op.var)]) { \
used_ext[VAR_NUM(op.var)] = 1; \
} \
} while (0)
2014-08-15 08:40:07 +00:00
#define NEVER_USED(op) ((op ## _type & (IS_VAR | IS_TMP_VAR)) && !usage[VAR_NUM(op.var)]) /* !used_ext[op.var] && */
#define RES_NEVER_USED(opline) (opline->result_type == IS_UNUSED || NEVER_USED(opline->result))
2013-02-13 12:26:47 +00:00
/* Find a set of variables which are used outside of the block where they are
* defined. We won't apply some optimization patterns for such variables. */
static void zend_t_usage(zend_code_block *block, zend_op_array *op_array, char *used_ext, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
zend_code_block *next_block = block->next;
char *usage;
char *defined_here;
void *checkpoint;
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
if (op_array->T == 0) {
2013-02-13 12:26:47 +00:00
/* shortcut - if no Ts, nothing to do */
return;
}
checkpoint = zend_arena_checkpoint(ctx->arena);
usage = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
memset(usage, 0, op_array->last_var + op_array->T);
defined_here = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
while (next_block) {
2013-02-13 12:26:47 +00:00
zend_op *opline = next_block->start_opline;
2013-02-25 07:29:54 +00:00
zend_op *end = opline + next_block->len;
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
if (!next_block->access) {
2013-02-13 12:26:47 +00:00
next_block = next_block->next;
continue;
}
2014-03-28 19:34:49 +00:00
memset(defined_here, 0, op_array->last_var + op_array->T);
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
while (opline<end) {
2013-02-13 12:26:47 +00:00
T_USAGE(opline->op1);
if (opline->op2_type & (IS_VAR | IS_TMP_VAR)) {
if (opline->opcode == ZEND_FE_FETCH_R || opline->opcode == ZEND_FE_FETCH_RW) {
/* these opcode use the op2 as result */
defined_here[VAR_NUM(ZEND_OP2(opline).var)] = 1;
} else {
T_USAGE(opline->op2);
}
}
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
if (RESULT_USED(opline)) {
if (!defined_here[VAR_NUM(ZEND_RESULT(opline).var)] && !used_ext[VAR_NUM(ZEND_RESULT(opline).var)] &&
2015-05-13 07:09:06 +00:00
opline->opcode == ZEND_ADD_ARRAY_ELEMENT) {
/* these opcode use the result as argument */
2013-02-13 12:26:47 +00:00
used_ext[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
}
defined_here[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
}
opline++;
}
next_block = next_block->next;
}
#if DEBUG_BLOCKPASS
{
int i;
2014-03-28 19:34:49 +00:00
for (i = op_array->last_var; i< op_array->T; i++) {
2013-02-22 06:56:05 +00:00
fprintf(stderr, "T%d: %c\n", i, used_ext[i] + '0');
2013-02-13 12:26:47 +00:00
}
}
#endif
2013-02-22 06:56:05 +00:00
while (block) {
2013-02-25 07:29:54 +00:00
zend_op *opline = block->start_opline + block->len - 1;
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
if (!block->access) {
2013-02-13 12:26:47 +00:00
block = block->next;
continue;
}
2014-03-28 19:34:49 +00:00
memcpy(usage, used_ext, op_array->last_var + op_array->T);
2013-02-13 12:26:47 +00:00
2013-02-22 06:56:05 +00:00
while (opline >= block->start_opline) {
2013-02-13 12:26:47 +00:00
/* usage checks */
2013-02-22 06:56:05 +00:00
if (RES_NEVER_USED(opline)) {
switch (opline->opcode) {
2013-02-13 12:26:47 +00:00
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:
case ZEND_PRE_INC:
case ZEND_PRE_DEC:
case ZEND_POST_INC:
case ZEND_POST_DEC:
case ZEND_ASSIGN:
case ZEND_ASSIGN_REF:
case ZEND_DO_FCALL:
case ZEND_DO_ICALL:
case ZEND_DO_UCALL:
case ZEND_DO_FCALL_BY_NAME:
2013-02-22 06:56:05 +00:00
if (ZEND_RESULT_TYPE(opline) == IS_VAR) {
2013-02-13 12:26:47 +00:00
ZEND_RESULT_TYPE(opline) |= EXT_TYPE_UNUSED;
}
break;
case ZEND_QM_ASSIGN:
case ZEND_BOOL:
case ZEND_BOOL_NOT:
2013-02-22 06:56:05 +00:00
if (ZEND_OP1_TYPE(opline) == IS_CONST) {
2013-02-13 12:26:47 +00:00
literal_dtor(&ZEND_OP1_LITERAL(opline));
}
MAKE_NOP(opline);
break;
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
opline->opcode -= 3;
SET_UNUSED(opline->result);
break;
}
}
2015-05-13 07:09:06 +00:00
if (opline->opcode == ZEND_ADD_ARRAY_ELEMENT) {
2013-02-22 06:56:05 +00:00
if (ZEND_OP1_TYPE(opline) == IS_VAR || ZEND_OP1_TYPE(opline) == IS_TMP_VAR) {
2013-02-13 12:26:47 +00:00
usage[VAR_NUM(ZEND_RESULT(opline).var)] = 1;
}
} else {
2013-02-22 06:56:05 +00:00
if (RESULT_USED(opline)) {
2013-02-13 12:26:47 +00:00
usage[VAR_NUM(ZEND_RESULT(opline).var)] = 0;
}
}
2013-02-22 06:56:05 +00:00
if (ZEND_OP1_TYPE(opline) == IS_VAR || ZEND_OP1_TYPE(opline) == IS_TMP_VAR) {
2013-02-13 12:26:47 +00:00
usage[VAR_NUM(ZEND_OP1(opline).var)] = 1;
}
2014-08-15 08:40:07 +00:00
2013-02-22 06:56:05 +00:00
if (ZEND_OP2_TYPE(opline) == IS_VAR || ZEND_OP2_TYPE(opline) == IS_TMP_VAR) {
2013-02-13 12:26:47 +00:00
usage[VAR_NUM(ZEND_OP2(opline).var)] = 1;
}
2013-02-22 06:56:05 +00:00
if ((ZEND_RESULT_TYPE(opline) & IS_VAR) &&
(ZEND_RESULT_TYPE(opline) & EXT_TYPE_UNUSED) &&
usage[VAR_NUM(ZEND_RESULT(opline).var)]) {
2013-02-13 12:26:47 +00:00
ZEND_RESULT_TYPE(opline) &= ~EXT_TYPE_UNUSED;
}
opline--;
}
block = block->next;
} /* end blocks */
2013-02-22 06:56:05 +00:00
zend_arena_release(&ctx->arena, checkpoint);
2013-02-13 12:26:47 +00:00
}
#define PASSES 3
2014-12-13 22:06:14 +00:00
void optimize_cfg(zend_op_array *op_array, zend_optimizer_ctx *ctx)
2013-02-13 12:26:47 +00:00
{
zend_cfg cfg;
zend_code_block *cur_block;
2013-02-13 12:26:47 +00:00
int pass;
char *usage;
void *checkpoint;
2013-02-13 12:26:47 +00:00
#if DEBUG_BLOCKPASS
2013-02-22 06:56:05 +00:00
fprintf(stderr, "File %s func %s\n", op_array->filename, op_array->function_name? op_array->function_name : "main");
2013-02-13 12:26:47 +00:00
fflush(stderr);
#endif
if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
2013-02-13 12:26:47 +00:00
return;
}
/* Build CFG */
checkpoint = zend_arena_checkpoint(ctx->arena);
if (!find_code_blocks(op_array, &cfg, ctx)) {
zend_arena_release(&ctx->arena, checkpoint);
2013-02-13 12:26:47 +00:00
return;
}
zend_rebuild_access_path(&cfg, op_array, 0, ctx);
2013-02-13 12:26:47 +00:00
/* full rebuild here to produce correct sources! */
if (op_array->last_var || op_array->T) {
cfg.Tsource = zend_arena_calloc(&ctx->arena, op_array->last_var + op_array->T, sizeof(zend_op *));
cfg.same_t = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
usage = zend_arena_alloc(&ctx->arena, op_array->last_var + op_array->T);
} else {
cfg.Tsource = NULL;
cfg.same_t = NULL;
usage = NULL;
}
2013-02-22 06:56:05 +00:00
for (pass = 0; pass < PASSES; pass++) {
2013-02-13 12:26:47 +00:00
/* Compute data dependencies */
2014-03-28 19:34:49 +00:00
memset(usage, 0, op_array->last_var + op_array->T);
zend_t_usage(cfg.blocks, op_array, usage, ctx);
2013-02-13 12:26:47 +00:00
/* optimize each basic block separately */
for (cur_block = cfg.blocks; cur_block; cur_block = cur_block->next) {
2013-02-22 06:56:05 +00:00
if (!cur_block->access) {
2013-02-13 12:26:47 +00:00
continue;
}
2014-12-13 22:06:14 +00:00
zend_optimize_block(cur_block, op_array, usage, &cfg, ctx);
2013-02-13 12:26:47 +00:00
}
/* Jump optimization for each block */
for (cur_block = cfg.blocks; cur_block; cur_block = cur_block->next) {
2013-02-22 06:56:05 +00:00
if (!cur_block->access) {
2013-02-13 12:26:47 +00:00
continue;
}
2014-12-13 22:06:14 +00:00
zend_jmp_optimization(cur_block, op_array, cfg.blocks, &cfg, ctx);
2013-02-13 12:26:47 +00:00
}
/* Eliminate unreachable basic blocks */
zend_rebuild_access_path(&cfg, op_array, 1, ctx);
2013-02-13 12:26:47 +00:00
}
2014-03-28 19:34:49 +00:00
memset(usage, 0, op_array->last_var + op_array->T);
zend_t_usage(cfg.blocks, op_array, usage, ctx);
assemble_code_blocks(&cfg, op_array);
2013-02-13 12:26:47 +00:00
/* Destroy CFG */
zend_arena_release(&ctx->arena, checkpoint);
2013-02-13 12:26:47 +00:00
}