diff -r aa480ecc8d3a Include/opcode.h --- a/Include/opcode.h Sat Mar 22 14:07:06 2008 +0100 +++ b/Include/opcode.h Sun Mar 23 00:35:48 2008 +0100 @@ -89,6 +89,7 @@ extern "C" { #define DELETE_NAME 91 /* "" */ #define UNPACK_SEQUENCE 92 /* Number of sequence items */ #define FOR_ITER 93 +#define FOR_ITER2 94 #define STORE_ATTR 95 /* Index in name list */ #define DELETE_ATTR 96 /* "" */ @@ -109,6 +110,7 @@ extern "C" { #define JUMP_IF_FALSE 111 /* "" */ #define JUMP_IF_TRUE 112 /* "" */ #define JUMP_ABSOLUTE 113 /* Target byte offset from beginning of code */ +#define JUMP_ABS_IF_TRUE 114 /* "" */ #define LOAD_GLOBAL 116 /* Index in name list */ diff -r aa480ecc8d3a Lib/opcode.py --- a/Lib/opcode.py Sat Mar 22 14:07:06 2008 +0100 +++ b/Lib/opcode.py Sun Mar 23 00:35:48 2008 +0100 @@ -128,6 +128,7 @@ name_op('DELETE_NAME', 91) # "" name_op('DELETE_NAME', 91) # "" def_op('UNPACK_SEQUENCE', 92) # Number of tuple items jrel_op('FOR_ITER', 93) +jabs_op('FOR_ITER2', 94) name_op('STORE_ATTR', 95) # Index in name list name_op('DELETE_ATTR', 96) # "" @@ -150,6 +151,7 @@ jrel_op('JUMP_IF_FALSE', 111) # "" jrel_op('JUMP_IF_FALSE', 111) # "" jrel_op('JUMP_IF_TRUE', 112) # "" jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code +jabs_op('JUMP_ABS_IF_TRUE', 114) # "" name_op('LOAD_GLOBAL', 116) # Index in name list diff -r aa480ecc8d3a Python/ceval.c --- a/Python/ceval.c Sat Mar 22 14:07:06 2008 +0100 +++ b/Python/ceval.c Sun Mar 23 00:35:48 2008 +0100 @@ -2175,6 +2175,26 @@ PyEval_EvalFrameEx(PyFrameObject *f, int break; continue; + case JUMP_ABS_IF_TRUE: + w = POP(); + if (w == Py_False) { + goto fast_next_opcode; + } + if (w == Py_True) { + JUMPTO(oparg); + goto fast_next_opcode; + } + err = PyObject_IsTrue(w); + if (err > 0) { + err = 0; + JUMPTO(oparg); + } + else if (err == 0) + ; + else + break; + continue; + PREDICTED_WITH_ARG(JUMP_ABSOLUTE); case JUMP_ABSOLUTE: JUMPTO(oparg); @@ -2225,6 +2245,28 @@ PyEval_EvalFrameEx(PyFrameObject *f, int x = v = POP(); Py_DECREF(v); JUMPBY(oparg); + continue; + + case FOR_ITER2: + /* before: [iter]; after: [iter, iter()] *or* [] */ + v = TOP(); + x = (*v->ob_type->tp_iternext)(v); + if (x != NULL) { + PUSH(x); + JUMPTO(oparg); + PREDICT(STORE_FAST); + PREDICT(UNPACK_SEQUENCE); + continue; + } + if (PyErr_Occurred()) { + if (!PyErr_ExceptionMatches( + PyExc_StopIteration)) + break; + PyErr_Clear(); + } + /* iterator ended normally */ + x = v = POP(); + Py_DECREF(v); continue; case BREAK_LOOP: diff -r aa480ecc8d3a Python/compile.c --- a/Python/compile.c Sat Mar 22 14:07:06 2008 +0100 +++ b/Python/compile.c Sun Mar 23 00:35:48 2008 +0100 @@ -796,6 +796,8 @@ opcode_stack_effect(int opcode, int opar return oparg-1; case FOR_ITER: return 1; + case FOR_ITER2: + return 1; case STORE_ATTR: return -2; @@ -830,6 +832,8 @@ opcode_stack_effect(int opcode, int opar case JUMP_IF_TRUE: case JUMP_ABSOLUTE: return 0; + case JUMP_ABS_IF_TRUE: + return -1; case LOAD_GLOBAL: return 1; @@ -1612,12 +1616,15 @@ static int static int compiler_for(struct compiler *c, stmt_ty s) { - basicblock *start, *cleanup, *end; + basicblock *start, *body, *tail, *cleanup, *end; start = compiler_new_block(c); + body = compiler_new_block(c); + tail = compiler_new_block(c); cleanup = compiler_new_block(c); end = compiler_new_block(c); - if (start == NULL || end == NULL || cleanup == NULL) + if (start == NULL || body == NULL || tail == NULL + || end == NULL || cleanup == NULL) return 0; ADDOP_JREL(c, SETUP_LOOP, end); if (!compiler_push_fblock(c, LOOP, start)) @@ -1628,10 +1635,12 @@ compiler_for(struct compiler *c, stmt_ty /* for expressions must be traced on each iteration, so we need to set an extra line number. */ c->u->u_lineno_set = false; - ADDOP_JREL(c, FOR_ITER, cleanup); + ADDOP_JABS(c, JUMP_ABSOLUTE, tail); + compiler_use_next_block(c, body); VISIT(c, expr, s->v.For.target); VISIT_SEQ(c, stmt, s->v.For.body); - ADDOP_JABS(c, JUMP_ABSOLUTE, start); + compiler_use_next_block(c, tail); + ADDOP_JABS(c, FOR_ITER2, body); compiler_use_next_block(c, cleanup); ADDOP(c, POP_BLOCK); compiler_pop_fblock(c, LOOP, start); @@ -1643,7 +1652,7 @@ static int static int compiler_while(struct compiler *c, stmt_ty s) { - basicblock *loop, *orelse, *end, *anchor = NULL; + basicblock *loop, *body, *tail, *orelse, *end, *anchor = NULL; int constant = expr_constant(s->v.While.test); if (constant == 0) { @@ -1654,8 +1663,10 @@ compiler_while(struct compiler *c, stmt_ loop = compiler_new_block(c); end = compiler_new_block(c); if (constant == -1) { + body = compiler_new_block(c); + tail = compiler_new_block(c); anchor = compiler_new_block(c); - if (anchor == NULL) + if (body == NULL || tail == NULL || anchor == NULL) return 0; } if (loop == NULL || end == NULL) @@ -1676,12 +1687,18 @@ compiler_while(struct compiler *c, stmt_ /* while expressions must be traced on each iteration, so we need to set an extra line number. */ c->u->u_lineno_set = false; - VISIT(c, expr, s->v.While.test); - ADDOP_JREL(c, JUMP_IF_FALSE, anchor); - ADDOP(c, POP_TOP); + ADDOP_JABS(c, JUMP_ABSOLUTE, tail); + compiler_use_next_block(c, body); } VISIT_SEQ(c, stmt, s->v.While.body); - ADDOP_JABS(c, JUMP_ABSOLUTE, loop); + if (constant == -1) { + compiler_use_next_block(c, tail); + VISIT(c, expr, s->v.While.test); + ADDOP_JABS(c, JUMP_ABS_IF_TRUE, body); + } + else { + ADDOP_JABS(c, JUMP_ABSOLUTE, loop); + } /* XXX should the two POP instructions be in a separate block if there is no else clause ? @@ -1689,7 +1706,6 @@ compiler_while(struct compiler *c, stmt_ if (constant == -1) { compiler_use_next_block(c, anchor); - ADDOP(c, POP_TOP); ADDOP(c, POP_BLOCK); } compiler_pop_fblock(c, LOOP, loop); diff -r aa480ecc8d3a Python/import.c --- a/Python/import.c Sat Mar 22 14:07:06 2008 +0100 +++ b/Python/import.c Sun Mar 23 00:35:48 2008 +0100 @@ -73,9 +73,10 @@ extern time_t PyOS_GetLastModificationTi Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp) Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode) Python 2.6a1: 62161 (WITH_CLEANUP optimization) + Python 2.6a1: 62171 (FOR_ITER2, JUMP_ABS_IF_TRUE) . */ -#define MAGIC (62161 | ((long)'\r'<<16) | ((long)'\n'<<24)) +#define MAGIC (62171 | ((long)'\r'<<16) | ((long)'\n'<<24)) /* Magic word as global; note that _PyImport_Init() can change the value of this global to accommodate for alterations of how the diff -r aa480ecc8d3a Python/peephole.c --- a/Python/peephole.c Sat Mar 22 14:07:06 2008 +0100 +++ b/Python/peephole.c Sun Mar 23 00:35:48 2008 +0100 @@ -13,7 +13,8 @@ #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) -#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) +#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP || \ + op==FOR_ITER2 || op==JUMP_ABS_IF_TRUE) #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) @@ -244,10 +245,12 @@ markblocks(unsigned char *code, int len) opcode = code[i]; switch (opcode) { case FOR_ITER: + case FOR_ITER2: case JUMP_FORWARD: case JUMP_IF_FALSE: case JUMP_IF_TRUE: case JUMP_ABSOLUTE: + case JUMP_ABS_IF_TRUE: case CONTINUE_LOOP: case SETUP_LOOP: case SETUP_EXCEPT: @@ -590,7 +593,9 @@ PyCode_Optimize(PyObject *code, PyObject continue; case JUMP_ABSOLUTE: + case JUMP_ABS_IF_TRUE: case CONTINUE_LOOP: + case FOR_ITER2: j = addrmap[GETARG(codestr, i)]; SETARG(codestr, i, j); break;