annotate Python/peephole.c @ 56225:00a5cf374314

Andrew just committed a patch for curses, so since he's been the maintainer he's on the hook for it in maintainers unless he says otherwise.
author R. David Murray <rdmurray@bitdance.com>
date Sat, 26 Sep 2009 02:42:26 +0000
parents 3c4490ad01cf
children 27eaac07c5d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
1 /* Peephole optimizations for bytecode compiler. */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
2
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
3 #include "Python.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
4
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
5 #include "Python-ast.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
6 #include "node.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
7 #include "pyarena.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
8 #include "ast.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
9 #include "code.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
10 #include "compile.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
11 #include "symtable.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
12 #include "opcode.h"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
13
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
14 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
15 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
16 #define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
17 || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
18 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
19 || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
20 || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
21 #define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
23 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
24 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
25 #define ISBASICBLOCK(blocks, start, bytes) \
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
26 (blocks[start]==blocks[start+bytes-1])
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
27
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
28 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
29 with LOAD_CONST (c1, c2, ... cn).
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
30 The consts table must still be in list form so that the
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
31 new constant (c1, c2, ... cn) can be appended.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
32 Called with codestr pointing to the first LOAD_CONST.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
33 Bails out with no change if one or more of the LOAD_CONSTs is missing.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
34 Also works for BUILD_LIST when followed by an "in" or "not in" test.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
35 */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
36 static int
46084
6bea2f1351b1 Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45515
diff changeset
37 tuple_of_constants(unsigned char *codestr, Py_ssize_t n, PyObject *consts)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
38 {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
39 PyObject *newconst, *constant;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
40 Py_ssize_t i, arg, len_consts;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
41
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
42 /* Pre-conditions */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
43 assert(PyList_CheckExact(consts));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
44 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
45 assert(GETARG(codestr, (n*3)) == n);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
46 for (i=0 ; i<n ; i++)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
47 assert(codestr[i*3] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
48
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
49 /* Buildup new tuple of constants */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
50 newconst = PyTuple_New(n);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
51 if (newconst == NULL)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
52 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
53 len_consts = PyList_GET_SIZE(consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
54 for (i=0 ; i<n ; i++) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
55 arg = GETARG(codestr, (i*3));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
56 assert(arg < len_consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
57 constant = PyList_GET_ITEM(consts, arg);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
58 Py_INCREF(constant);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
59 PyTuple_SET_ITEM(newconst, i, constant);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
60 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
61
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
62 /* Append folded constant onto consts */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
63 if (PyList_Append(consts, newconst)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
64 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
66 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
67 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
68
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
69 /* Write NOPs over old LOAD_CONSTS and
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
70 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
71 memset(codestr, NOP, n*3);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
72 codestr[n*3] = LOAD_CONST;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
73 SETARG(codestr, (n*3), len_consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
74 return 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
75 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
76
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
77 /* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
78 with LOAD_CONST binop(c1,c2)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
79 The consts table must still be in list form so that the
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
80 new constant can be appended.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
81 Called with codestr pointing to the first LOAD_CONST.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
82 Abandons the transformation if the folding fails (i.e. 1+'a').
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
83 If the new constant is a sequence, only folds when the size
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
84 is below a threshold value. That keeps pyc files from
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
85 becoming large in the presence of code like: (None,)*1000.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
86 */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
87 static int
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
88 fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
89 {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
90 PyObject *newconst, *v, *w;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
91 Py_ssize_t len_consts, size;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
92 int opcode;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
93
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
94 /* Pre-conditions */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
95 assert(PyList_CheckExact(consts));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
96 assert(codestr[0] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
97 assert(codestr[3] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
98
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
99 /* Create new constant */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
100 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
101 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
102 opcode = codestr[6];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
103 switch (opcode) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
104 case BINARY_POWER:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
105 newconst = PyNumber_Power(v, w, Py_None);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
106 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
107 case BINARY_MULTIPLY:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
108 newconst = PyNumber_Multiply(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
109 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
110 case BINARY_TRUE_DIVIDE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
111 newconst = PyNumber_TrueDivide(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
112 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
113 case BINARY_FLOOR_DIVIDE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
114 newconst = PyNumber_FloorDivide(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
115 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
116 case BINARY_MODULO:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
117 newconst = PyNumber_Remainder(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
118 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
119 case BINARY_ADD:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
120 newconst = PyNumber_Add(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
121 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
122 case BINARY_SUBTRACT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
123 newconst = PyNumber_Subtract(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
124 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
125 case BINARY_SUBSCR:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
126 newconst = PyObject_GetItem(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
127 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
128 case BINARY_LSHIFT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
129 newconst = PyNumber_Lshift(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
130 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
131 case BINARY_RSHIFT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
132 newconst = PyNumber_Rshift(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
133 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
134 case BINARY_AND:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
135 newconst = PyNumber_And(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
136 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
137 case BINARY_XOR:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
138 newconst = PyNumber_Xor(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
139 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
140 case BINARY_OR:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
141 newconst = PyNumber_Or(v, w);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
142 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
143 default:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
144 /* Called with an unknown opcode */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
145 PyErr_Format(PyExc_SystemError,
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
146 "unexpected binary operation %d on a constant",
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
147 opcode);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
148 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
149 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
150 if (newconst == NULL) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
151 PyErr_Clear();
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
152 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
153 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
154 size = PyObject_Size(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
155 if (size == -1)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
156 PyErr_Clear();
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
157 else if (size > 20) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
158 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
159 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
160 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
161
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
162 /* Append folded constant into consts table */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
163 len_consts = PyList_GET_SIZE(consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
164 if (PyList_Append(consts, newconst)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
165 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
166 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
167 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
168 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
169
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
170 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
171 memset(codestr, NOP, 4);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
172 codestr[4] = LOAD_CONST;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
173 SETARG(codestr, 4, len_consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
174 return 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
175 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
176
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
177 static int
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
178 fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
179 {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
180 PyObject *newconst=NULL, *v;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
181 Py_ssize_t len_consts;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
182 int opcode;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
183
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
184 /* Pre-conditions */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
185 assert(PyList_CheckExact(consts));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
186 assert(codestr[0] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
187
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
188 /* Create new constant */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
189 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
190 opcode = codestr[3];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
191 switch (opcode) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
192 case UNARY_NEGATIVE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
193 /* Preserve the sign of -0.0 */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
194 if (PyObject_IsTrue(v) == 1)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
195 newconst = PyNumber_Negative(v);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
196 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
197 case UNARY_INVERT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
198 newconst = PyNumber_Invert(v);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
199 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
200 default:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
201 /* Called with an unknown opcode */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
202 PyErr_Format(PyExc_SystemError,
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
203 "unexpected unary operation %d on a constant",
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
204 opcode);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
205 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
206 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
207 if (newconst == NULL) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
208 PyErr_Clear();
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
209 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
210 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
211
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
212 /* Append folded constant into consts table */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
213 len_consts = PyList_GET_SIZE(consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
214 if (PyList_Append(consts, newconst)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
215 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
216 return 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
217 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
218 Py_DECREF(newconst);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
219
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
220 /* Write NOP LOAD_CONST newconst */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
221 codestr[0] = NOP;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
222 codestr[1] = LOAD_CONST;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
223 SETARG(codestr, 1, len_consts);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
224 return 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
225 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
226
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
227 static unsigned int *
46084
6bea2f1351b1 Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45515
diff changeset
228 markblocks(unsigned char *code, Py_ssize_t len)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
229 {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
230 unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
231 int i,j, opcode, blockcnt = 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
232
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
233 if (blocks == NULL) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
234 PyErr_NoMemory();
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
235 return NULL;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
236 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
237 memset(blocks, 0, len*sizeof(int));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
238
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
239 /* Mark labels in the first pass */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
240 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
241 opcode = code[i];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
242 switch (opcode) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
243 case FOR_ITER:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
244 case JUMP_FORWARD:
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
245 case JUMP_IF_FALSE_OR_POP:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
246 case JUMP_IF_TRUE_OR_POP:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
247 case POP_JUMP_IF_FALSE:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
248 case POP_JUMP_IF_TRUE:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
249 case JUMP_ABSOLUTE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
250 case CONTINUE_LOOP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
251 case SETUP_LOOP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
252 case SETUP_EXCEPT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
253 case SETUP_FINALLY:
54889
3aaf2e065db0 Merged revisions 72912,72920,72940 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51923
diff changeset
254 case SETUP_WITH:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
255 j = GETJUMPTGT(code, i);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
256 blocks[j] = 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
257 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
258 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
259 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
260 /* Build block numbers in the second pass */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
261 for (i=0 ; i<len ; i++) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
262 blockcnt += blocks[i]; /* increment blockcnt over labels */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
263 blocks[i] = blockcnt;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
264 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
265 return blocks;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
266 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
267
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
268 /* Helper to replace LOAD_NAME None/True/False with LOAD_CONST
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
269 Returns: 0 if no change, 1 if change, -1 if error */
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
270 static int
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
271 load_global(unsigned char *codestr, Py_ssize_t i, char *name, PyObject *consts)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
272 {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
273 Py_ssize_t j;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
274 PyObject *obj;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
275 if (name == NULL)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
276 return 0;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
277 if (strcmp(name, "None") == 0)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
278 obj = Py_None;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
279 else if (strcmp(name, "True") == 0)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
280 obj = Py_True;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
281 else if (strcmp(name, "False") == 0)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
282 obj = Py_False;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
283 else
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
284 return 0;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
285 for (j = 0; j < PyList_GET_SIZE(consts); j++) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
286 if (PyList_GET_ITEM(consts, j) == obj)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
287 break;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
288 }
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
289 if (j == PyList_GET_SIZE(consts)) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
290 if (PyList_Append(consts, obj) < 0)
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
291 return -1;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
292 }
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
293 assert(PyList_GET_ITEM(consts, j) == obj);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
294 codestr[i] = LOAD_CONST;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
295 SETARG(codestr, i, j);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
296 return 1;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
297 }
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
298
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
299 /* Perform basic peephole optimizations to components of a code object.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
300 The consts object should still be in list form to allow new constants
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
301 to be appended.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
302
40943
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
303 To keep the optimizer simple, it bails out (does nothing) for code that
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
304 has a length over 32,700, and does not calculate extended arguments.
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
305 That allows us to avoid overflow and sign issues. Likewise, it bails when
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
306 the lineno table has complex encoding for gaps >= 255. EXTENDED_ARG can
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
307 appear before MAKE_FUNCTION; in this case both opcodes are skipped.
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
308 EXTENDED_ARG preceding any other opcode causes the optimizer to bail.
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
309
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
310 Optimizations are restricted to simple transformations occuring within a
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
311 single basic block. All transformations keep the code size the same or
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
312 smaller. For those that reduce size, the gaps are initially filled with
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
313 NOPs. Later those NOPs are removed and the jump addresses retargeted in
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
314 a single pass. Line numbering is adjusted accordingly. */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
315
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
316 PyObject *
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
317 PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
318 PyObject *lineno_obj)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
319 {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
320 Py_ssize_t i, j, codelen;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
321 int nops, h, adj;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
322 int tgt, tgttgt, opcode;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
323 unsigned char *codestr = NULL;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
324 unsigned char *lineno;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
325 int *addrmap = NULL;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
326 int new_line, cum_orig_line, last_line, tabsiz;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
327 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
328 unsigned int *blocks = NULL;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
329 char *name;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
330
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
331 /* Bail out if an exception is set */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
332 if (PyErr_Occurred())
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
333 goto exitError;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
334
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
335 /* Bypass optimization when the lineno table is too complex */
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
336 assert(PyBytes_Check(lineno_obj));
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
337 lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj);
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
338 tabsiz = PyBytes_GET_SIZE(lineno_obj);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
339 if (memchr(lineno, 255, tabsiz) != NULL)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
340 goto exitUnchanged;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
341
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
342 /* Avoid situations where jump retargeting could overflow */
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
343 assert(PyBytes_Check(code));
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
344 codelen = PyBytes_GET_SIZE(code);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
345 if (codelen > 32700)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
346 goto exitUnchanged;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
347
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
348 /* Make a modifiable copy of the code string */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
349 codestr = (unsigned char *)PyMem_Malloc(codelen);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
350 if (codestr == NULL)
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
351 goto exitError;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
352 codestr = (unsigned char *)memcpy(codestr,
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
353 PyBytes_AS_STRING(code), codelen);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
354
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
355 /* Verify that RETURN_VALUE terminates the codestring. This allows
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
356 the various transformation patterns to look ahead several
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
357 instructions without additional checks to make sure they are not
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
358 looking beyond the end of the code string.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
359 */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
360 if (codestr[codelen-1] != RETURN_VALUE)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
361 goto exitUnchanged;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
362
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
363 /* Mapping to new jump targets after NOPs are removed */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
364 addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
365 if (addrmap == NULL)
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
366 goto exitError;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
367
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
368 blocks = markblocks(codestr, codelen);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
369 if (blocks == NULL)
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
370 goto exitError;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
371 assert(PyList_Check(consts));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
372
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
373 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
374 reoptimize_current:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
375 opcode = codestr[i];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
376
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
377 lastlc = cumlc;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
378 cumlc = 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
379
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
380 switch (opcode) {
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
381 /* Replace UNARY_NOT POP_JUMP_IF_FALSE
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
382 with POP_JUMP_IF_TRUE */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
383 case UNARY_NOT:
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
384 if (codestr[i+1] != POP_JUMP_IF_FALSE
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
385 || !ISBASICBLOCK(blocks,i,4))
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
386 continue;
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
387 j = GETARG(codestr, i+1);
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
388 codestr[i] = POP_JUMP_IF_TRUE;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
389 SETARG(codestr, i, j);
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
390 codestr[i+3] = NOP;
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
391 goto reoptimize_current;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
392
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
393 /* not a is b --> a is not b
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
394 not a in b --> a not in b
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
395 not a is not b --> a is b
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
396 not a not in b --> a in b
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
397 */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
398 case COMPARE_OP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
399 j = GETARG(codestr, i);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
400 if (j < 6 || j > 9 ||
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
401 codestr[i+3] != UNARY_NOT ||
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
402 !ISBASICBLOCK(blocks,i,4))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
403 continue;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
404 SETARG(codestr, i, (j^1));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
405 codestr[i+3] = NOP;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
406 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
407
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
408 /* Replace LOAD_GLOBAL/LOAD_NAME None/True/False
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
409 with LOAD_CONST None/True/False */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
410 case LOAD_NAME:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
411 case LOAD_GLOBAL:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
412 j = GETARG(codestr, i);
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 47301
diff changeset
413 name = _PyUnicode_AsString(PyTuple_GET_ITEM(names, j));
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
414 h = load_global(codestr, i, name, consts);
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
415 if (h < 0)
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
416 goto exitError;
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
417 else if (h == 0)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
418 continue;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40943
diff changeset
419 cumlc = lastlc + 1;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
420 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
421
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
422 /* Skip over LOAD_CONST trueconst
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
423 POP_JUMP_IF_FALSE xx. This improves
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
424 "while 1" performance. */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
425 case LOAD_CONST:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
426 cumlc = lastlc + 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
427 j = GETARG(codestr, i);
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
428 if (codestr[i+3] != POP_JUMP_IF_FALSE ||
51923
e1ce666d7405 Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51853
diff changeset
429 !ISBASICBLOCK(blocks,i,6) ||
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
430 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
431 continue;
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
432 memset(codestr+i, NOP, 6);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
433 cumlc = 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
434 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
435
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
436 /* Try to fold tuples of constants (includes a case for lists
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
437 which are only used for "in" and "not in" tests).
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
438 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
439 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
440 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
441 case BUILD_TUPLE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
442 case BUILD_LIST:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
443 j = GETARG(codestr, i);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
444 h = i - 3 * j;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
445 if (h >= 0 &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
446 j <= lastlc &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
447 ((opcode == BUILD_TUPLE &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
448 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
449 (opcode == BUILD_LIST &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
450 codestr[i+3]==COMPARE_OP &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
451 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
452 (GETARG(codestr,i+3)==6 ||
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
453 GETARG(codestr,i+3)==7))) &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
454 tuple_of_constants(&codestr[h], j, consts)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
455 assert(codestr[i] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
456 cumlc = 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
457 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
458 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
459 if (codestr[i+3] != UNPACK_SEQUENCE ||
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
460 !ISBASICBLOCK(blocks,i,6) ||
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
461 j != GETARG(codestr, i+3))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
462 continue;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
463 if (j == 1) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
464 memset(codestr+i, NOP, 6);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
465 } else if (j == 2) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
466 codestr[i] = ROT_TWO;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
467 memset(codestr+i+1, NOP, 5);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
468 } else if (j == 3) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
469 codestr[i] = ROT_THREE;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
470 codestr[i+1] = ROT_TWO;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
471 memset(codestr+i+2, NOP, 4);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
472 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
473 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
474
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
475 /* Fold binary ops on constants.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
476 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
477 case BINARY_POWER:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
478 case BINARY_MULTIPLY:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
479 case BINARY_TRUE_DIVIDE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
480 case BINARY_FLOOR_DIVIDE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
481 case BINARY_MODULO:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
482 case BINARY_ADD:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
483 case BINARY_SUBTRACT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
484 case BINARY_SUBSCR:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
485 case BINARY_LSHIFT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
486 case BINARY_RSHIFT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
487 case BINARY_AND:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
488 case BINARY_XOR:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
489 case BINARY_OR:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
490 if (lastlc >= 2 &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
491 ISBASICBLOCK(blocks, i-6, 7) &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
492 fold_binops_on_constants(&codestr[i-6], consts)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
493 i -= 2;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
494 assert(codestr[i] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
495 cumlc = 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
496 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
497 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
498
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
499 /* Fold unary ops on constants.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
500 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
501 case UNARY_NEGATIVE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
502 case UNARY_INVERT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
503 if (lastlc >= 1 &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
504 ISBASICBLOCK(blocks, i-3, 4) &&
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
505 fold_unaryops_on_constants(&codestr[i-3], consts)) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
506 i -= 2;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
507 assert(codestr[i] == LOAD_CONST);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
508 cumlc = 1;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
509 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
510 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
511
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
512 /* Simplify conditional jump to conditional jump where the
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
513 result of the first test implies the success of a similar
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
514 test or the failure of the opposite test.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
515 Arises in code like:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
516 "if a and b:"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
517 "if a or b:"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
518 "a and b or c"
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
519 "(a and b) and c"
51923
e1ce666d7405 Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51853
diff changeset
520 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
e1ce666d7405 Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51853
diff changeset
521 --> x:JUMP_IF_FALSE_OR_POP z
e1ce666d7405 Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51853
diff changeset
522 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
e1ce666d7405 Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 51853
diff changeset
523 --> x:POP_JUMP_IF_FALSE y+3
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
524 where y+3 is the instruction following the second test.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
525 */
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
526 case JUMP_IF_FALSE_OR_POP:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
527 case JUMP_IF_TRUE_OR_POP:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
528 tgt = GETJUMPTGT(codestr, i);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
529 j = codestr[tgt];
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
530 if (CONDITIONAL_JUMP(j)) {
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
531 /* NOTE: all possible jumps here are
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
532 absolute! */
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
533 if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) {
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
534 /* The second jump will be
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
535 taken iff the first is. */
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
536 tgttgt = GETJUMPTGT(codestr, tgt);
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
537 /* The current opcode inherits
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
538 its target's stack behaviour */
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
539 codestr[i] = j;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
540 SETARG(codestr, i, tgttgt);
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
541 goto reoptimize_current;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
542 } else {
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
543 /* The second jump is not taken
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
544 if the first is (so jump past
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
545 it), and all conditional
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
546 jumps pop their argument when
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
547 they're not taken (so change
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
548 the first jump to pop its
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
549 argument when it's taken). */
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
550 if (JUMPS_ON_TRUE(opcode))
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
551 codestr[i] = POP_JUMP_IF_TRUE;
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
552 else
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
553 codestr[i] = POP_JUMP_IF_FALSE;
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
554 SETARG(codestr, i, (tgt + 3));
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
555 goto reoptimize_current;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
556 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
557 }
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
558 /* Intentional fallthrough */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
559
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
560 /* Replace jumps to unconditional jumps */
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
561 case POP_JUMP_IF_FALSE:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
562 case POP_JUMP_IF_TRUE:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
563 case FOR_ITER:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
564 case JUMP_FORWARD:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
565 case JUMP_ABSOLUTE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
566 case CONTINUE_LOOP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
567 case SETUP_LOOP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
568 case SETUP_EXCEPT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
569 case SETUP_FINALLY:
54889
3aaf2e065db0 Merged revisions 72912,72920,72940 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51923
diff changeset
570 case SETUP_WITH:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
571 tgt = GETJUMPTGT(codestr, i);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
572 /* Replace JUMP_* to a RETURN into just a RETURN */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
573 if (UNCONDITIONAL_JUMP(opcode) &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
574 codestr[tgt] == RETURN_VALUE) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
575 codestr[i] = RETURN_VALUE;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
576 memset(codestr+i+1, NOP, 2);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
577 continue;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
578 }
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
579 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
580 continue;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
581 tgttgt = GETJUMPTGT(codestr, tgt);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
582 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
583 opcode = JUMP_ABSOLUTE;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
584 if (!ABSOLUTE_JUMP(opcode))
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
585 tgttgt -= i + 3; /* Calc relative jump addr */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
586 if (tgttgt < 0) /* No backward relative jumps */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
587 continue;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
588 codestr[i] = opcode;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
589 SETARG(codestr, i, tgttgt);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
590 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
591
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
592 case EXTENDED_ARG:
40943
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
593 if (codestr[i+3] != MAKE_FUNCTION)
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
594 goto exitUnchanged;
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
595 /* don't visit MAKE_FUNCTION as GETARG will be wrong */
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
596 i += 3;
c79e6234fb19 Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
597 break;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
598
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
599 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
600 /* Remove unreachable JUMPs after RETURN */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
601 case RETURN_VALUE:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
602 if (i+4 >= codelen)
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
603 continue;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
604 if (codestr[i+4] == RETURN_VALUE &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
605 ISBASICBLOCK(blocks,i,5))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
606 memset(codestr+i+1, NOP, 4);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
607 else if (UNCONDITIONAL_JUMP(codestr[i+1]) &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
608 ISBASICBLOCK(blocks,i,4))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39678
diff changeset
609 memset(codestr+i+1, NOP, 3);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
610 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
611 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
612 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
613
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
614 /* Fixup linenotab */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
615 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
616 addrmap[i] = i - nops;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
617 if (codestr[i] == NOP)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
618 nops++;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
619 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
620 cum_orig_line = 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
621 last_line = 0;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
622 for (i=0 ; i < tabsiz ; i+=2) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
623 cum_orig_line += lineno[i];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
624 new_line = addrmap[cum_orig_line];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
625 assert (new_line - last_line < 255);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
626 lineno[i] =((unsigned char)(new_line - last_line));
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
627 last_line = new_line;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
628 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
629
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
630 /* Remove NOPs and fixup jump targets */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
631 for (i=0, h=0 ; i<codelen ; ) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
632 opcode = codestr[i];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
633 switch (opcode) {
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
634 case NOP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
635 i++;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
636 continue;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
637
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
638 case JUMP_ABSOLUTE:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
639 case CONTINUE_LOOP:
51853
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
640 case POP_JUMP_IF_FALSE:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
641 case POP_JUMP_IF_TRUE:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
642 case JUMP_IF_FALSE_OR_POP:
643d8d4fc8eb http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49747
diff changeset
643 case JUMP_IF_TRUE_OR_POP:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
644 j = addrmap[GETARG(codestr, i)];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
645 SETARG(codestr, i, j);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
646 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
647
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
648 case FOR_ITER:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
649 case JUMP_FORWARD:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
650 case SETUP_LOOP:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
651 case SETUP_EXCEPT:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
652 case SETUP_FINALLY:
54889
3aaf2e065db0 Merged revisions 72912,72920,72940 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51923
diff changeset
653 case SETUP_WITH:
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
654 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
655 SETARG(codestr, i, j);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
656 break;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
657 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
658 adj = CODESIZE(opcode);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
659 while (adj--)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
660 codestr[h++] = codestr[i++];
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
661 }
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
662 assert(h + nops == codelen);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
663
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46084
diff changeset
664 code = PyBytes_FromStringAndSize((char *)codestr, h);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
665 PyMem_Free(addrmap);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
666 PyMem_Free(codestr);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
667 PyMem_Free(blocks);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
668 return code;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
669
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
670 exitError:
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
671 code = NULL;
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
672
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
673 exitUnchanged:
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
674 if (blocks != NULL)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
675 PyMem_Free(blocks);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
676 if (addrmap != NULL)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
677 PyMem_Free(addrmap);
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
678 if (codestr != NULL)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
679 PyMem_Free(codestr);
55371
3c4490ad01cf Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54889
diff changeset
680 Py_XINCREF(code);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
681 return code;
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff changeset
682 }