Mercurial > cpython-lru_cache-weakref
annotate Python/peephole.c @ 56748:8eff759d080f
Merged revisions 75711 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r75711 | mark.dickinson | 2009-10-26 11:59:30 +0000 (Mon, 26 Oct 2009) | 1 line
Skip readline tests if readline module is not available.
........
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Mon, 26 Oct 2009 12:01:06 +0000 |
| parents | 27eaac07c5d6 |
| children | ed8b0ee1c531 |
| 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; |
|
56667
27eaac07c5d6
Peephole constant folding had missed UNARY_POSITIVE.
Raymond Hettinger <python@rcn.com>
parents:
55371
diff
changeset
|
200 case UNARY_POSITIVE: |
|
27eaac07c5d6
Peephole constant folding had missed UNARY_POSITIVE.
Raymond Hettinger <python@rcn.com>
parents:
55371
diff
changeset
|
201 newconst = PyNumber_Positive(v); |
|
27eaac07c5d6
Peephole constant folding had missed UNARY_POSITIVE.
Raymond Hettinger <python@rcn.com>
parents:
55371
diff
changeset
|
202 break; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
203 default: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
204 /* 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
|
205 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
|
206 "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
|
207 opcode); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
208 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
209 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
210 if (newconst == NULL) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
211 PyErr_Clear(); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
212 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
213 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
214 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
215 /* 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
|
216 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
|
217 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
|
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 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
220 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
221 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
222 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
223 /* 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
|
224 codestr[0] = NOP; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
225 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
|
226 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
|
227 return 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
228 } |
|
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 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
|
231 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
|
232 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
233 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
|
234 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
|
235 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
236 if (blocks == NULL) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
237 PyErr_NoMemory(); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
238 return NULL; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
239 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
240 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
|
241 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
242 /* 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
|
243 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
|
244 opcode = code[i]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
245 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
246 case FOR_ITER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
247 case JUMP_FORWARD: |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
248 case JUMP_IF_FALSE_OR_POP: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
249 case JUMP_IF_TRUE_OR_POP: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
250 case POP_JUMP_IF_FALSE: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
251 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
|
252 case JUMP_ABSOLUTE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
253 case CONTINUE_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
254 case SETUP_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
255 case SETUP_EXCEPT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
256 case SETUP_FINALLY: |
|
54889
3aaf2e065db0
Merged revisions 72912,72920,72940 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
51923
diff
changeset
|
257 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
|
258 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
|
259 blocks[j] = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
260 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
261 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
262 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
263 /* 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
|
264 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
|
265 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
|
266 blocks[i] = blockcnt; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
267 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
268 return blocks; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
269 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
270 |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
271 /* 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
|
272 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
|
273 static int |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
274 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
|
275 { |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
276 Py_ssize_t j; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
277 PyObject *obj; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
278 if (name == NULL) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
279 return 0; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
280 if (strcmp(name, "None") == 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
281 obj = Py_None; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
282 else if (strcmp(name, "True") == 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
283 obj = Py_True; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
284 else if (strcmp(name, "False") == 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
285 obj = Py_False; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
286 else |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
287 return 0; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
288 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
|
289 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
|
290 break; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
291 } |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
292 if (j == PyList_GET_SIZE(consts)) { |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
293 if (PyList_Append(consts, obj) < 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
294 return -1; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
295 } |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
296 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
|
297 codestr[i] = LOAD_CONST; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
298 SETARG(codestr, i, j); |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
299 return 1; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
300 } |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
301 |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
302 /* 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
|
303 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
|
304 to be appended. |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
305 |
|
40943
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
306 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
|
307 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
|
308 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
|
309 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
|
310 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
|
311 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
|
312 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
319 PyObject * |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
320 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
|
321 PyObject *lineno_obj) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
322 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
323 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
|
324 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
|
325 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
|
326 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
|
327 unsigned char *lineno; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
328 int *addrmap = NULL; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
329 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
|
330 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
|
331 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
|
332 char *name; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
333 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
334 /* 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
|
335 if (PyErr_Occurred()) |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
336 goto exitError; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
337 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
338 /* Bypass optimization when the lineno table is too complex */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
339 assert(PyBytes_Check(lineno_obj)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
340 lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
341 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
|
342 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
|
343 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
344 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
345 /* Avoid situations where jump retargeting could overflow */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
346 assert(PyBytes_Check(code)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
347 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
|
348 if (codelen > 32700) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
349 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
350 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
351 /* 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
|
352 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
|
353 if (codestr == NULL) |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
354 goto exitError; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
355 codestr = (unsigned char *)memcpy(codestr, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
356 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
|
357 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
358 /* 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
|
359 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
|
360 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
|
361 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
|
362 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
363 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
|
364 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
365 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
366 /* 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
|
367 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
|
368 if (addrmap == NULL) |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
369 goto exitError; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
370 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
371 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
|
372 if (blocks == NULL) |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
373 goto exitError; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
374 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
|
375 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
376 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
|
377 reoptimize_current: |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
378 opcode = codestr[i]; |
|
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 lastlc = cumlc; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
381 cumlc = 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
382 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
383 switch (opcode) { |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
384 /* Replace UNARY_NOT POP_JUMP_IF_FALSE |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
385 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
|
386 case UNARY_NOT: |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
387 if (codestr[i+1] != POP_JUMP_IF_FALSE |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
388 || !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
|
389 continue; |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
390 j = GETARG(codestr, i+1); |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
391 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
|
392 SETARG(codestr, i, j); |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
393 codestr[i+3] = NOP; |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
394 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
|
395 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
396 /* 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
|
397 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
|
398 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
|
399 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
|
400 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
401 case COMPARE_OP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
402 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
|
403 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
|
404 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
|
405 !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
|
406 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
407 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
|
408 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
|
409 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
410 |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
411 /* 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
|
412 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
|
413 case LOAD_NAME: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
414 case LOAD_GLOBAL: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
415 j = GETARG(codestr, i); |
|
48660
85b67fb5672b
Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
416 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
|
417 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
|
418 if (h < 0) |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
419 goto exitError; |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
420 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
|
421 continue; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40943
diff
changeset
|
422 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
|
423 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
424 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
425 /* Skip over LOAD_CONST trueconst |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
426 POP_JUMP_IF_FALSE xx. This improves |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
427 "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
|
428 case LOAD_CONST: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
429 cumlc = lastlc + 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
430 j = GETARG(codestr, i); |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
431 if (codestr[i+3] != POP_JUMP_IF_FALSE || |
|
51923
e1ce666d7405
Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51853
diff
changeset
|
432 !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
|
433 !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
|
434 continue; |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
435 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
|
436 cumlc = 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
437 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
438 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
439 /* 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
|
440 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
|
441 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
|
442 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
|
443 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
|
444 case BUILD_TUPLE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
445 case BUILD_LIST: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
446 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
|
447 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
|
448 if (h >= 0 && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
449 j <= lastlc && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
450 ((opcode == BUILD_TUPLE && |
|
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+1))) || |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
452 (opcode == BUILD_LIST && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
453 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
|
454 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
|
455 (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
|
456 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
|
457 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
|
458 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
|
459 cumlc = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
460 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
461 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
462 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
|
463 !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
|
464 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
|
465 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
466 if (j == 1) { |
|
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, NOP, 6); |
|
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 == 2) { |
|
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_TWO; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
470 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
|
471 } 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
|
472 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
|
473 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
|
474 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
|
475 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
476 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
477 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
478 /* 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
|
479 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
|
480 case BINARY_POWER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
481 case BINARY_MULTIPLY: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
482 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
|
483 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
|
484 case BINARY_MODULO: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
485 case BINARY_ADD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
486 case BINARY_SUBTRACT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
487 case BINARY_SUBSCR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
488 case BINARY_LSHIFT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
489 case BINARY_RSHIFT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
490 case BINARY_AND: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
491 case BINARY_XOR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
492 case BINARY_OR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
493 if (lastlc >= 2 && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
494 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
|
495 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
|
496 i -= 2; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
497 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
|
498 cumlc = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
499 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
500 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
501 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
502 /* 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
|
503 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
|
504 case UNARY_NEGATIVE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
505 case UNARY_INVERT: |
|
56667
27eaac07c5d6
Peephole constant folding had missed UNARY_POSITIVE.
Raymond Hettinger <python@rcn.com>
parents:
55371
diff
changeset
|
506 case UNARY_POSITIVE: |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
507 if (lastlc >= 1 && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
508 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
|
509 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
|
510 i -= 2; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
511 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
|
512 cumlc = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
513 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
514 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
515 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
516 /* 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
|
517 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
|
518 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
|
519 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
|
520 "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
|
521 "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
|
522 "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
|
523 "(a and b) and c" |
|
51923
e1ce666d7405
Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51853
diff
changeset
|
524 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
|
525 --> x:JUMP_IF_FALSE_OR_POP z |
|
e1ce666d7405
Fix 2 oversights from r69961.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
51853
diff
changeset
|
526 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
|
527 --> 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
|
528 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
|
529 */ |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
530 case JUMP_IF_FALSE_OR_POP: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
531 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
|
532 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
|
533 j = codestr[tgt]; |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
534 if (CONDITIONAL_JUMP(j)) { |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
535 /* NOTE: all possible jumps here are |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
536 absolute! */ |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
537 if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) { |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
538 /* The second jump will be |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
539 taken iff the first is. */ |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
540 tgttgt = GETJUMPTGT(codestr, tgt); |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
541 /* The current opcode inherits |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
542 its target's stack behaviour */ |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
543 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
|
544 SETARG(codestr, i, tgttgt); |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
545 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
|
546 } else { |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
547 /* The second jump is not taken |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
548 if the first is (so jump past |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
549 it), and all conditional |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
550 jumps pop their argument when |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
551 they're not taken (so change |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
552 the first jump to pop its |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
553 argument when it's taken). */ |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
554 if (JUMPS_ON_TRUE(opcode)) |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
555 codestr[i] = POP_JUMP_IF_TRUE; |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
556 else |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
557 codestr[i] = POP_JUMP_IF_FALSE; |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
558 SETARG(codestr, i, (tgt + 3)); |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
559 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
|
560 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
561 } |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
562 /* Intentional fallthrough */ |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
563 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
564 /* Replace jumps to unconditional jumps */ |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
565 case POP_JUMP_IF_FALSE: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
566 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
|
567 case FOR_ITER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
568 case JUMP_FORWARD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
569 case JUMP_ABSOLUTE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
570 case CONTINUE_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
571 case SETUP_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
572 case SETUP_EXCEPT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
573 case SETUP_FINALLY: |
|
54889
3aaf2e065db0
Merged revisions 72912,72920,72940 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
51923
diff
changeset
|
574 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
|
575 tgt = GETJUMPTGT(codestr, i); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
576 /* 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
|
577 if (UNCONDITIONAL_JUMP(opcode) && |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
578 codestr[tgt] == RETURN_VALUE) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
579 codestr[i] = RETURN_VALUE; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
580 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
|
581 continue; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
582 } |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
583 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
|
584 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
585 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
|
586 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
|
587 opcode = JUMP_ABSOLUTE; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
588 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
|
589 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
|
590 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
|
591 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
592 codestr[i] = opcode; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
593 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
|
594 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
595 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
596 case EXTENDED_ARG: |
|
40943
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
597 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
|
598 goto exitUnchanged; |
|
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
599 /* 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
|
600 i += 3; |
|
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
601 break; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
602 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
603 /* 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
|
604 /* 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
|
605 case RETURN_VALUE: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
606 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
|
607 continue; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
608 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
|
609 ISBASICBLOCK(blocks,i,5)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
610 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
|
611 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
|
612 ISBASICBLOCK(blocks,i,4)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
613 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
|
614 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
615 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
616 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
617 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
618 /* Fixup linenotab */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
619 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
|
620 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
|
621 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
|
622 nops++; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
623 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
624 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
|
625 last_line = 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
626 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
|
627 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
|
628 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
|
629 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
|
630 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
|
631 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
|
632 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
633 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
634 /* 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
|
635 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
|
636 opcode = codestr[i]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
637 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
638 case NOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
639 i++; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
640 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
641 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
642 case JUMP_ABSOLUTE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
643 case CONTINUE_LOOP: |
|
51853
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
644 case POP_JUMP_IF_FALSE: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
645 case POP_JUMP_IF_TRUE: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
646 case JUMP_IF_FALSE_OR_POP: |
|
643d8d4fc8eb
http://bugs.python.org/issue4715
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
49747
diff
changeset
|
647 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
|
648 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
|
649 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
|
650 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
651 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
652 case FOR_ITER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
653 case JUMP_FORWARD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
654 case SETUP_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
655 case SETUP_EXCEPT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
656 case SETUP_FINALLY: |
|
54889
3aaf2e065db0
Merged revisions 72912,72920,72940 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
51923
diff
changeset
|
657 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
|
658 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
|
659 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
|
660 break; |
|
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 adj = CODESIZE(opcode); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
663 while (adj--) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
664 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
|
665 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
666 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
|
667 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
668 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
|
669 PyMem_Free(addrmap); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
670 PyMem_Free(codestr); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
671 PyMem_Free(blocks); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
672 return code; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
673 |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
674 exitError: |
|
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
675 code = NULL; |
|
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
676 |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
677 exitUnchanged: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
678 if (blocks != 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(blocks); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
680 if (addrmap != NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
681 PyMem_Free(addrmap); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
682 if (codestr != NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
683 PyMem_Free(codestr); |
|
55371
3c4490ad01cf
Merged revisions 73683,73786 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
54889
diff
changeset
|
684 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
|
685 return code; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
686 } |
