Mercurial > cpython-lru_cache-weakref
annotate Python/peephole.c @ 48632:5f3ef6400a96
Blocked revisions 65444 via svnmerge
........
r65444 | brett.cannon | 2008-08-03 15:57:23 -0700 (Sun, 03 Aug 2008) | 3 lines
Remove a dict.has_key() and callable() usage in SimpleXMLRPCServer as triggered
under -3 through test_xmlrpc.
........
| author | Brett Cannon <bcannon@gmail.com> |
|---|---|
| date | Sun, 03 Aug 2008 22:58:03 +0000 |
| parents | 23ab586c427a |
| children | 85b67fb5672b |
| 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) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
16 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
17 #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
|
18 #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
|
19 #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
|
20 #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
|
21 (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
|
22 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
23 /* 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
31 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
|
32 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
|
33 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
34 PyObject *newconst, *constant; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
35 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
|
36 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
37 /* Pre-conditions */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
44 /* 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
|
45 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
|
46 if (newconst == NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
47 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 Py_INCREF(constant); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
54 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
|
55 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
56 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
57 /* 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
|
58 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
|
59 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
60 return 0; |
|
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 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
63 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
64 /* 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 return 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
70 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
71 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
72 /* 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
82 static int |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
83 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
|
84 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
85 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
|
86 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
|
87 int opcode; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
88 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
89 /* Pre-conditions */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
90 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
|
91 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
|
92 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
|
93 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
94 /* Create new constant */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
95 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
|
96 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
|
97 opcode = codestr[6]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
98 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
99 case BINARY_POWER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
100 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
|
101 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
102 case BINARY_MULTIPLY: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
103 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
|
104 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
105 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
|
106 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
|
107 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
108 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
|
109 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
|
110 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
111 case BINARY_MODULO: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
112 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
|
113 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
114 case BINARY_ADD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
115 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
|
116 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
117 case BINARY_SUBTRACT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
118 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
|
119 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
120 case BINARY_SUBSCR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
121 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
|
122 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
123 case BINARY_LSHIFT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
124 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
|
125 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
126 case BINARY_RSHIFT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
127 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
|
128 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
129 case BINARY_AND: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
130 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
|
131 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
132 case BINARY_XOR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
133 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
|
134 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
135 case BINARY_OR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
136 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
|
137 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
138 default: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
139 /* 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
|
140 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
|
141 "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
|
142 opcode); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
143 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
144 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
145 if (newconst == NULL) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
146 PyErr_Clear(); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
147 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
148 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
149 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
|
150 if (size == -1) |
|
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 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
|
153 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
154 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
155 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
156 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
157 /* 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
|
158 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
|
159 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
|
160 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
161 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
162 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
163 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
164 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
165 /* 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
|
166 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
|
167 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
|
168 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
|
169 return 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
170 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
171 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
172 static int |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
173 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
|
174 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
175 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
|
176 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
|
177 int opcode; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
178 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
179 /* Pre-conditions */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
180 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
|
181 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
|
182 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
183 /* Create new constant */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
184 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
|
185 opcode = codestr[3]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
186 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
187 case UNARY_NEGATIVE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
188 /* 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
|
189 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
|
190 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
|
191 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
192 case UNARY_INVERT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
193 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
|
194 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
195 default: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
196 /* 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
|
197 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
|
198 "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
|
199 opcode); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
200 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
201 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
202 if (newconst == NULL) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
203 PyErr_Clear(); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
204 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
205 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
206 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
207 /* 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
|
208 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
|
209 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
|
210 Py_DECREF(newconst); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
211 return 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
212 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
213 Py_DECREF(newconst); |
|
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 /* 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
|
216 codestr[0] = NOP; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
217 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
|
218 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
|
219 return 1; |
|
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 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
222 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
|
223 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
|
224 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
225 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
|
226 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
|
227 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
228 if (blocks == NULL) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
229 PyErr_NoMemory(); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
230 return NULL; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
231 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
232 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
|
233 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
234 /* 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
|
235 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
|
236 opcode = code[i]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
237 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
238 case FOR_ITER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
239 case JUMP_FORWARD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
240 case JUMP_IF_FALSE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
241 case JUMP_IF_TRUE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
242 case JUMP_ABSOLUTE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
243 case CONTINUE_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
244 case SETUP_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
245 case SETUP_EXCEPT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
246 case SETUP_FINALLY: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
247 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
|
248 blocks[j] = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
249 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
250 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
251 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
252 /* 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
|
253 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
|
254 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
|
255 blocks[i] = blockcnt; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
256 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
257 return blocks; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
258 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
259 |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
260 /* 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
|
261 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
|
262 static int |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
263 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
|
264 { |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
265 Py_ssize_t j; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
266 PyObject *obj; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
267 if (name == NULL) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
268 return 0; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
269 if (strcmp(name, "None") == 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
270 obj = Py_None; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
271 else if (strcmp(name, "True") == 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
272 obj = Py_True; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
273 else if (strcmp(name, "False") == 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
274 obj = Py_False; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
275 else |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
276 return 0; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
277 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
|
278 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
|
279 break; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
280 } |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
281 if (j == PyList_GET_SIZE(consts)) { |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
282 if (PyList_Append(consts, obj) < 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
283 return -1; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
284 } |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
285 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
|
286 codestr[i] = LOAD_CONST; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
287 SETARG(codestr, i, j); |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
288 return 1; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
289 } |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
290 |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
291 /* 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
|
292 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
|
293 to be appended. |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
294 |
|
40943
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
295 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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 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
|
301 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
308 PyObject * |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
309 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
|
310 PyObject *lineno_obj) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
311 { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
312 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
|
313 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
|
314 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
|
315 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
|
316 unsigned char *lineno; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
317 int *addrmap = NULL; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
318 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
|
319 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
|
320 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
|
321 char *name; |
|
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 /* 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
|
324 if (PyErr_Occurred()) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
325 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
326 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
327 /* Bypass optimization when the lineno table is too complex */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
328 assert(PyBytes_Check(lineno_obj)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
329 lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
330 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
|
331 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
|
332 goto exitUnchanged; |
|
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 /* Avoid situations where jump retargeting could overflow */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
335 assert(PyBytes_Check(code)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
336 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
|
337 if (codelen > 32700) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
338 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
339 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
340 /* 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
|
341 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
|
342 if (codestr == 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 codestr = (unsigned char *)memcpy(codestr, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
345 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
|
346 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
347 /* 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
|
348 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
|
349 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
|
350 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
|
351 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
352 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
|
353 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
354 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
355 /* 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
|
356 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
|
357 if (addrmap == NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
358 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
359 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
360 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
|
361 if (blocks == NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
362 goto exitUnchanged; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
363 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
|
364 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
365 for (i=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
|
366 opcode = codestr[i]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
367 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
368 lastlc = cumlc; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
369 cumlc = 0; |
|
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 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
372 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
373 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
374 with JUMP_IF_TRUE POP_TOP */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
375 case UNARY_NOT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
376 if (codestr[i+1] != JUMP_IF_FALSE || |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
377 codestr[i+4] != POP_TOP || |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
378 !ISBASICBLOCK(blocks,i,5)) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
379 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
380 tgt = GETJUMPTGT(codestr, (i+1)); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
381 if (codestr[tgt] != POP_TOP) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
382 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
383 j = GETARG(codestr, i+1) + 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
384 codestr[i] = JUMP_IF_TRUE; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
385 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
|
386 codestr[i+3] = POP_TOP; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
387 codestr[i+4] = NOP; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
388 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
389 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
390 /* 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
|
391 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
|
392 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
|
393 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
|
394 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
395 case COMPARE_OP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
396 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
|
397 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
|
398 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
|
399 !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
|
400 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
401 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
|
402 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
|
403 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
404 |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
405 /* 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
|
406 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
|
407 case LOAD_NAME: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
408 case LOAD_GLOBAL: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
409 j = GETARG(codestr, i); |
|
43961
b231bda653a4
PyString_AsString is permissive and accepts unicode strings.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42084
diff
changeset
|
410 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
|
411 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
|
412 if (h < 0) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
413 goto exitUnchanged; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
414 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
|
415 continue; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40943
diff
changeset
|
416 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
|
417 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
418 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
419 /* Skip over LOAD_CONST trueconst |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
420 JUMP_IF_FALSE xx POP_TOP */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
421 case LOAD_CONST: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
422 cumlc = lastlc + 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
423 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
|
424 if (codestr[i+3] != JUMP_IF_FALSE || |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
425 codestr[i+6] != POP_TOP || |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
426 !ISBASICBLOCK(blocks,i,7) || |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
427 !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
|
428 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
429 memset(codestr+i, NOP, 7); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
430 cumlc = 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
431 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
432 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
433 /* 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
|
434 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
|
435 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
|
436 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
|
437 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
|
438 case BUILD_TUPLE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
439 case BUILD_LIST: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
440 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
|
441 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
|
442 if (h >= 0 && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
443 j <= lastlc && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
444 ((opcode == BUILD_TUPLE && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
445 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
|
446 (opcode == BUILD_LIST && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
447 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
|
448 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
|
449 (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
|
450 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
|
451 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
|
452 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
|
453 cumlc = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
454 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
455 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
456 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
|
457 !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
|
458 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
|
459 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
460 if (j == 1) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
461 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
|
462 } 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
|
463 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
|
464 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
|
465 } 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
|
466 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
|
467 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
|
468 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
|
469 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
470 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
471 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
472 /* 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
|
473 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
|
474 case BINARY_POWER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
475 case BINARY_MULTIPLY: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
476 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
|
477 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
|
478 case BINARY_MODULO: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
479 case BINARY_ADD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
480 case BINARY_SUBTRACT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
481 case BINARY_SUBSCR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
482 case BINARY_LSHIFT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
483 case BINARY_RSHIFT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
484 case BINARY_AND: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
485 case BINARY_XOR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
486 case BINARY_OR: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
487 if (lastlc >= 2 && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
488 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
|
489 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
|
490 i -= 2; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
491 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
|
492 cumlc = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
493 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
494 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
495 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
496 /* 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
|
497 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
|
498 case UNARY_NEGATIVE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
499 case UNARY_INVERT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
500 if (lastlc >= 1 && |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
501 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
|
502 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
|
503 i -= 2; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
504 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
|
505 cumlc = 1; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
506 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
507 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
508 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
509 /* 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
|
510 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
|
511 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
|
512 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
|
513 "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
|
514 "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
|
515 "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
|
516 "(a and b) and c" |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
517 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
518 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
519 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
|
520 */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
521 case JUMP_IF_FALSE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
522 case JUMP_IF_TRUE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
523 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
|
524 j = codestr[tgt]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
525 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
526 if (j == opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
527 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
528 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
|
529 } else { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
530 tgt -= i; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
531 SETARG(codestr, i, tgt); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
532 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
533 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
534 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
535 /* Intentional fallthrough */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
536 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
537 /* Replace jumps to unconditional jumps */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
538 case FOR_ITER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
539 case JUMP_FORWARD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
540 case JUMP_ABSOLUTE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
541 case CONTINUE_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
542 case SETUP_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
543 case SETUP_EXCEPT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
544 case SETUP_FINALLY: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
545 tgt = GETJUMPTGT(codestr, i); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
546 /* 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
|
547 if (UNCONDITIONAL_JUMP(opcode) && |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
548 codestr[tgt] == RETURN_VALUE) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
549 codestr[i] = RETURN_VALUE; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
550 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
|
551 continue; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
552 } |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
553 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
|
554 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
555 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
|
556 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
|
557 opcode = JUMP_ABSOLUTE; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
558 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
|
559 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
|
560 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
|
561 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
562 codestr[i] = opcode; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
563 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
|
564 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
565 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
566 case EXTENDED_ARG: |
|
40943
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
567 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
|
568 goto exitUnchanged; |
|
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
569 /* 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
|
570 i += 3; |
|
c79e6234fb19
Two more patches by Tony Lownds (SF# 1607548).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
571 break; |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
572 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
573 /* 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
|
574 /* 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
|
575 case RETURN_VALUE: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
576 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
|
577 continue; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
578 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
|
579 ISBASICBLOCK(blocks,i,5)) |
|
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, 4); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
581 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
|
582 ISBASICBLOCK(blocks,i,4)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39678
diff
changeset
|
583 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
|
584 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
585 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
586 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
587 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
588 /* Fixup linenotab */ |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 nops++; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
593 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
594 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
|
595 last_line = 0; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
596 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
|
597 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
|
598 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
|
599 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
|
600 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
|
601 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
|
602 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
603 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
604 /* 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
|
605 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
|
606 opcode = codestr[i]; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
607 switch (opcode) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
608 case NOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
609 i++; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
610 continue; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
611 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
612 case JUMP_ABSOLUTE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
613 case CONTINUE_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
614 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
|
615 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
|
616 break; |
|
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 case FOR_ITER: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
619 case JUMP_FORWARD: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
620 case JUMP_IF_FALSE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
621 case JUMP_IF_TRUE: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
622 case SETUP_LOOP: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
623 case SETUP_EXCEPT: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
624 case SETUP_FINALLY: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
625 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
|
626 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
|
627 break; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
628 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
629 adj = CODESIZE(opcode); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
630 while (adj--) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
631 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
|
632 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
633 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
|
634 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46084
diff
changeset
|
635 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
|
636 PyMem_Free(addrmap); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
637 PyMem_Free(codestr); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
638 PyMem_Free(blocks); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
639 return code; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
640 |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
641 exitUnchanged: |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
642 if (blocks != NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
643 PyMem_Free(blocks); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
644 if (addrmap != NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
645 PyMem_Free(addrmap); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
646 if (codestr != NULL) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
647 PyMem_Free(codestr); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
648 Py_INCREF(code); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
649 return code; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
diff
changeset
|
650 } |
