com php-src: Fixed temporary variable re-allocation pass: Zend/tests/foreach_005.phpt ext/opcache/Optimizer/o ptimize_temp_vars_5.c

[email protected] (Rasmus Lerdorf)
Newsgroups php.cvs
Message-ID <[email protected]>
Commit:    61e739187391661e2d541947bec25d7dcc4479f3
Author:    Dmitry Stogov <[email protected]>         Wed, 28 Jan 2015 14:59:54 +0300
Parents:   92e90c09f085c22707ff4a59201f016f56e0ef8b
Branches:  dstogov-foreach

Link:       http://git.php.net/?p=php-src.git;a=commitdiff;h=61e739187391661e2d541947bec25d7dcc4479f3

Log:
Fixed temporary variable re-allocation pass

Changed paths:
  A  Zend/tests/foreach_005.phpt
  M  ext/opcache/Optimizer/optimize_temp_vars_5.c


Diff:
diff --git a/Zend/tests/foreach_005.phpt b/Zend/tests/foreach_005.phpt
new file mode 100644
index 0000000..6ed9fe9
--- /dev/null
+++ b/Zend/tests/foreach_005.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Nested foreach by reference on the same array
+--FILE--
+<?php
+$a = [1,2,3];
+foreach($a as &$x) {
+	foreach($a as &$y) {
+		echo "$x-$y\n";
+		$y++; 
+	}
+}
+?>
+--EXPECT--
+1-1
+2-2
+2-3
+3-2
+3-3
+4-4
+5-3
+5-4
+5-5
diff --git a/ext/opcache/Optimizer/optimize_temp_vars_5.c b/ext/opcache/Optimizer/optimize_temp_vars_5.c
index ac5389e..7ff94dd 100644
--- a/ext/opcache/Optimizer/optimize_temp_vars_5.c
+++ b/ext/opcache/Optimizer/optimize_temp_vars_5.c
@@ -69,9 +69,8 @@ void optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *c
 		/* special puprose variable to keep HashTable* on VM stack */
 		if (opline->opcode == ZEND_OP_DATA &&
 		    (opline-1)->opcode == ZEND_FE_FETCH_RW &&
-		    (opline-2)->opcode == ZEND_FE_RESET_RW &&
 		    opline->op1_type == IS_TMP_VAR) {
-			start_of_T[VAR_NUM(ZEND_OP1(opline).var) - offset] = opline - 2;
+			start_of_T[VAR_NUM(ZEND_OP1(opline).var) - offset] = opline;
 		}
 		opline--;
 	}
@@ -85,13 +84,21 @@ void optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *c
     while (opline >= end) {
 		if ((ZEND_OP1_TYPE(opline) & (IS_VAR | IS_TMP_VAR))) {
 
-			currT = VAR_NUM(ZEND_OP1(opline).var) - offset;
-			if (!valid_T[currT]) {
-				GET_AVAILABLE_T();
-				map_T[currT] = i;
-				valid_T[currT] = 1;
+			/* special puprose variable to keep HashPointer on VM stack */
+			if (opline->opcode == ZEND_OP_DATA &&
+			    (opline-1)->opcode == ZEND_FE_FETCH_RW &&
+				opline->op1_type == IS_TMP_VAR) {
+				max++;
+				ZEND_OP1(opline).var = NUM_VAR(max + offset);
+			} else {
+				currT = VAR_NUM(ZEND_OP1(opline).var) - offset;
+				if (!valid_T[currT]) {
+					GET_AVAILABLE_T();
+					map_T[currT] = i;
+					valid_T[currT] = 1;
+				}
+				ZEND_OP1(opline).var = NUM_VAR(map_T[currT] + offset);
 			}
-			ZEND_OP1(opline).var = NUM_VAR(map_T[currT] + offset);
 		}
 
 		/* Skip OP_DATA */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.