diff -urw cpython-8122deb904e1-old/Include/opcode.h cpython-8122deb904e1/Include/opcode.h --- cpython-8122deb904e1-old/Include/opcode.h 2010-07-04 13:07:10.668582342 +0000 +++ cpython-8122deb904e1/Include/opcode.h 2010-07-04 19:02:52.560783376 +0000 @@ -18,13 +18,15 @@ #define UNARY_POSITIVE 10 #define UNARY_NEGATIVE 11 #define UNARY_NOT 12 - -#define UNARY_INVERT 15 - -#define BINARY_POWER 19 - -#define BINARY_MULTIPLY 20 - +#define UNARY_INVERT 13 +#define BINARY_IN 14 /* peephole relies on NOT x == x^1 */ +#define BINARY_NOT_IN 15 /* "" */ +#define BINARY_IS 16 /* "" */ +#define BINARY_IS_NOT 17 /* "" */ + +#define BINARY_EXC_MATCH 19 +#define BINARY_POWER 20 +#define BINARY_MULTIPLY 21 #define BINARY_MODULO 22 #define BINARY_ADD 23 #define BINARY_SUBTRACT 24 @@ -42,7 +44,6 @@ #define INPLACE_MODULO 59 #define STORE_SUBSCR 60 #define DELETE_SUBSCR 61 - #define BINARY_LSHIFT 62 #define BINARY_RSHIFT 63 #define BINARY_AND 64 @@ -78,7 +79,6 @@ #define FOR_ITER 93 #define UNPACK_EX 94 /* Num items before variable part + (Num items after variable part << 8) */ - #define STORE_ATTR 95 /* Index in name list */ #define DELETE_ATTR 96 /* "" */ #define STORE_GLOBAL 97 /* "" */ @@ -147,8 +147,7 @@ #define EXCEPT_HANDLER 257 -enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE, PyCmp_GT=Py_GT, PyCmp_GE=Py_GE, - PyCmp_IN, PyCmp_NOT_IN, PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD}; +enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE, PyCmp_GT=Py_GT, PyCmp_GE=Py_GE, PyCmp_BAD}; #define HAS_ARG(op) ((op) >= HAVE_ARGUMENT) diff -urw cpython-8122deb904e1-old/Lib/opcode.py cpython-8122deb904e1/Lib/opcode.py --- cpython-8122deb904e1-old/Lib/opcode.py 2010-07-04 13:07:10.947457526 +0000 +++ cpython-8122deb904e1/Lib/opcode.py 2010-07-04 19:42:42.546051325 +0000 @@ -8,8 +8,7 @@ "haslocal", "hascompare", "hasfree", "opname", "opmap", "HAVE_ARGUMENT", "EXTENDED_ARG"] -cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', - 'is not', 'exception match', 'BAD') +cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'BAD') hasconst = [] hasname = [] @@ -54,12 +53,15 @@ def_op('UNARY_POSITIVE', 10) def_op('UNARY_NEGATIVE', 11) def_op('UNARY_NOT', 12) - -def_op('UNARY_INVERT', 15) - -def_op('BINARY_POWER', 19) -def_op('BINARY_MULTIPLY', 20) - +def_op('UNARY_INVERT', 13) +def_op('BINARY_IN', 14) # peephole relies on NOT x == x^1 +def_op('BINARY_NOT_IN', 15) # "" +def_op('BINARY_IS', 16) # "" +def_op('BINARY_IS_NOT', 17) # "" + +def_op('BINARY_EXC_MATCH', 19) +def_op('BINARY_POWER', 20) +def_op('BINARY_MULTIPLY', 21) def_op('BINARY_MODULO', 22) def_op('BINARY_ADD', 23) def_op('BINARY_SUBTRACT', 24) diff -urw cpython-8122deb904e1-old/Python/ceval.c cpython-8122deb904e1/Python/ceval.c --- cpython-8122deb904e1-old/Python/ceval.c 2010-07-04 13:07:10.989840439 +0000 +++ cpython-8122deb904e1/Python/ceval.c 2010-07-04 19:07:21.769841491 +0000 @@ -131,7 +131,6 @@ static int maybe_call_line_trace(Py_tracefunc, PyObject *, PyFrameObject *, int *, int *, int *); -static PyObject * cmp_outcome(int, PyObject *, PyObject *); static PyObject * import_from(PyObject *, PyObject *); static int import_all_from(PyObject *, PyObject *); static void format_exc_check_arg(PyObject *, const char *, PyObject *); @@ -1489,6 +1488,108 @@ if (x != NULL) DISPATCH(); break; + TARGET(BINARY_IN) + { + w = POP(); + v = TOP(); + int res = PySequence_Contains(w, v); + if (res < 0){ + Py_DECREF(v); + Py_DECREF(w); + SET_TOP(NULL); + break; + } + x = res ? Py_True : Py_False; + Py_DECREF(v); + Py_DECREF(w); + Py_INCREF(x); + SET_TOP(x); + PREDICT(POP_JUMP_IF_FALSE); + PREDICT(POP_JUMP_IF_TRUE); + DISPATCH(); + } + + TARGET(BINARY_NOT_IN) + { + w = POP(); + v = TOP(); + int res = PySequence_Contains(w, v); + if (res < 0){ + Py_DECREF(v); + Py_DECREF(w); + SET_TOP(NULL); + break; + } + x = res ? Py_False : Py_True; + Py_DECREF(v); + Py_DECREF(w); + Py_INCREF(x); + SET_TOP(x); + PREDICT(POP_JUMP_IF_FALSE); + PREDICT(POP_JUMP_IF_TRUE); + DISPATCH(); + } + + TARGET(BINARY_IS) + w = POP(); + v = TOP(); + x = v == w ? Py_True : Py_False; + Py_DECREF(v); + Py_DECREF(w); + Py_INCREF(x); + SET_TOP(x); + PREDICT(POP_JUMP_IF_FALSE); + PREDICT(POP_JUMP_IF_TRUE); + DISPATCH(); + + TARGET(BINARY_IS_NOT) + w = POP(); + v = TOP(); + x = v != w ? Py_True : Py_False; + Py_DECREF(v); + Py_DECREF(w); + Py_INCREF(x); + SET_TOP(x); + PREDICT(POP_JUMP_IF_FALSE); + PREDICT(POP_JUMP_IF_TRUE); + DISPATCH(); + + TARGET(BINARY_EXC_MATCH) + w = POP(); + v = TOP(); + if (PyTuple_Check(w)) { + Py_ssize_t i, length; + length = PyTuple_Size(w); + for (i = 0; i < length; i += 1) { + PyObject *exc = PyTuple_GET_ITEM(w, i); + if (!PyExceptionClass_Check(exc)) { + PyErr_SetString(PyExc_TypeError, + "catching classes that do not inherit from BaseException is not allowed"); + Py_DECREF(v); + Py_DECREF(w); + SET_TOP(NULL); + break; + } + } + } + else { + if (!PyExceptionClass_Check(w)) { + PyErr_SetString(PyExc_TypeError, + "catching classes that do not inherit from BaseException is not allowed"); + Py_DECREF(v); + Py_DECREF(w); + SET_TOP(NULL); + break; + } + } + x = PyErr_GivenExceptionMatches(v, w)? Py_True : Py_False; + Py_DECREF(v); + Py_DECREF(w); + Py_INCREF(x); + SET_TOP(x); + PREDICT(POP_JUMP_IF_FALSE); + DISPATCH(); + TARGET(BINARY_POWER) w = POP(); v = TOP(); @@ -2281,7 +2382,7 @@ TARGET(COMPARE_OP) w = POP(); v = TOP(); - x = cmp_outcome(oparg, v, w); + x = PyObject_RichCompare(v, w, oparg); Py_DECREF(v); Py_DECREF(w); SET_TOP(x); @@ -4221,61 +4322,6 @@ return 1; } -#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ - "BaseException is not allowed" - -static PyObject * -cmp_outcome(int op, register PyObject *v, register PyObject *w) -{ - int res = 0; - switch (op) { - case PyCmp_IS: - res = (v == w); - break; - case PyCmp_IS_NOT: - res = (v != w); - break; - case PyCmp_IN: - res = PySequence_Contains(w, v); - if (res < 0) - return NULL; - break; - case PyCmp_NOT_IN: - res = PySequence_Contains(w, v); - if (res < 0) - return NULL; - res = !res; - break; - case PyCmp_EXC_MATCH: - if (PyTuple_Check(w)) { - Py_ssize_t i, length; - length = PyTuple_Size(w); - for (i = 0; i < length; i += 1) { - PyObject *exc = PyTuple_GET_ITEM(w, i); - if (!PyExceptionClass_Check(exc)) { - PyErr_SetString(PyExc_TypeError, - CANNOT_CATCH_MSG); - return NULL; - } - } - } - else { - if (!PyExceptionClass_Check(w)) { - PyErr_SetString(PyExc_TypeError, - CANNOT_CATCH_MSG); - return NULL; - } - } - res = PyErr_GivenExceptionMatches(v, w); - break; - default: - return PyObject_RichCompare(v, w, op); - } - v = res ? Py_True : Py_False; - Py_INCREF(v); - return v; -} - static PyObject * import_from(PyObject *v, PyObject *name) { diff -urw cpython-8122deb904e1-old/Python/compile.c cpython-8122deb904e1/Python/compile.c --- cpython-8122deb904e1-old/Python/compile.c 2010-07-04 13:07:10.979840451 +0000 +++ cpython-8122deb904e1/Python/compile.c 2010-07-04 18:41:44.187864049 +0000 @@ -694,7 +694,11 @@ return -1; case MAP_ADD: return -2; - + case BINARY_IN: + case BINARY_NOT_IN: + case BINARY_IS: + case BINARY_IS_NOT: + case BINARY_EXC_MATCH: case BINARY_POWER: case BINARY_MULTIPLY: case BINARY_MODULO: @@ -1919,7 +1923,7 @@ [tb, val, exc] L1: DUP ) [tb, val, exc, exc] ) - [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 + [tb, val, exc, exc, E1] BINARY_EXC_MATCH ) only if E1 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) [tb, val, exc] POP [tb, val] (or POP if no V1) @@ -1971,7 +1975,7 @@ if (handler->v.ExceptHandler.type) { ADDOP(c, DUP_TOP); VISIT(c, expr, handler->v.ExceptHandler.type); - ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); + ADDOP(c, BINARY_EXC_MATCH); ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); } ADDOP(c, POP_TOP); @@ -2407,13 +2411,13 @@ case GtE: return PyCmp_GE; case Is: - return PyCmp_IS; + return -BINARY_IS; case IsNot: - return PyCmp_IS_NOT; + return -BINARY_IS_NOT; case In: - return PyCmp_IN; + return -BINARY_IN; case NotIn: - return PyCmp_NOT_IN; + return -BINARY_NOT_IN; default: return PyCmp_BAD; } @@ -2675,7 +2679,7 @@ static int compiler_compare(struct compiler *c, expr_ty e) { - int i, n; + int i, n, t; basicblock *cleanup = NULL; /* XXX the logic can be cleaned up for 1 or multiple comparisons */ @@ -2692,9 +2696,12 @@ for (i = 1; i < n; i++) { ADDOP(c, DUP_TOP); ADDOP(c, ROT_THREE); - ADDOP_I(c, COMPARE_OP, - cmpop((cmpop_ty)(asdl_seq_GET( - e->v.Compare.ops, i - 1)))); + t = cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i - 1))); + if (t >= 0) { + ADDOP_I(c, COMPARE_OP, t); + } else { + ADDOP(c, -t); + } ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); NEXT_BLOCK(c); if (i < (n - 1)) @@ -2702,8 +2709,12 @@ (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); } VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1)); - ADDOP_I(c, COMPARE_OP, - cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1)))); + t = cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))); + if(t >= 0) { + ADDOP_I(c, COMPARE_OP, t); + } else { + ADDOP(c, -t); + } if (n > 1) { basicblock *end = compiler_new_block(c); if (end == NULL) diff -urw cpython-8122deb904e1-old/Python/opcode_targets.h cpython-8122deb904e1/Python/opcode_targets.h --- cpython-8122deb904e1-old/Python/opcode_targets.h 2010-07-04 15:29:09.429845926 +0000 +++ cpython-8122deb904e1/Python/opcode_targets.h 2010-07-04 18:53:15.142544423 +0000 @@ -12,15 +12,15 @@ &&TARGET_UNARY_POSITIVE, &&TARGET_UNARY_NEGATIVE, &&TARGET_UNARY_NOT, - &&_unknown_opcode, - &&_unknown_opcode, &&TARGET_UNARY_INVERT, + &&TARGET_BINARY_IN, + &&TARGET_BINARY_NOT_IN, + &&TARGET_BINARY_IS, + &&TARGET_BINARY_IS_NOT, &&_unknown_opcode, - &&_unknown_opcode, - &&_unknown_opcode, + &&TARGET_BINARY_EXC_MATCH, &&TARGET_BINARY_POWER, &&TARGET_BINARY_MULTIPLY, - &&_unknown_opcode, &&TARGET_BINARY_MODULO, &&TARGET_BINARY_ADD, &&TARGET_BINARY_SUBTRACT, diff -urw cpython-8122deb904e1-old/Python/peephole.c cpython-8122deb904e1/Python/peephole.c --- cpython-8122deb904e1-old/Python/peephole.c 2010-07-04 13:07:10.989840439 +0000 +++ cpython-8122deb904e1/Python/peephole.c 2010-07-04 18:28:56.680887392 +0000 @@ -409,14 +409,15 @@ not a is not b --> a is b not a not in b --> a in b */ - case COMPARE_OP: - j = GETARG(codestr, i); - if (j < 6 || j > 9 || - codestr[i+3] != UNARY_NOT || - !ISBASICBLOCK(blocks,i,4)) + case BINARY_IN: + case BINARY_NOT_IN: + case BINARY_IS: + case BINARY_IS_NOT: + if (!ISBASICBLOCK(blocks,i,2) || + codestr[i+1]!=UNARY_NOT) continue; - SETARG(codestr, i, (j^1)); - codestr[i+3] = NOP; + codestr[i] ^= 1; + codestr[i+1] = NOP; break; /* Replace LOAD_GLOBAL/LOAD_NAME None/True/False @@ -462,10 +463,8 @@ ((opcode == BUILD_TUPLE && ISBASICBLOCK(blocks, h, 3*(j+1))) || ((opcode == BUILD_LIST || opcode == BUILD_SET) && - codestr[i+3]==COMPARE_OP && - ISBASICBLOCK(blocks, h, 3*(j+2)) && - (GETARG(codestr,i+3)==6 || - GETARG(codestr,i+3)==7))) && + (codestr[i+3]==BINARY_IN || codestr[i+3]==BINARY_NOT_IN) && + ISBASICBLOCK(blocks, h, 3*j+4))) && tuple_of_constants(&codestr[h], j, consts)) { assert(codestr[i] == LOAD_CONST); cumlc = 1;