Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | /* Peephole optimizations for bytecode compiler. */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2 | |
| 3 | #include "Python.h" |
| 4 | |
| 5 | #include "Python-ast.h" |
| 6 | #include "node.h" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7 | #include "ast.h" |
| 8 | #include "code.h" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9 | #include "symtable.h" |
| 10 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 11 | #include "wordcode_helpers.h" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 12 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 14 | #define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 16 | #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ |
| 18 | || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 19 | #define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP) |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 20 | #define GETJUMPTGT(arr, i) (get_arg(arr, i) / sizeof(_Py_CODEUNIT) + \ |
| 21 | (ABSOLUTE_JUMP(_Py_OPCODE(arr[i])) ? 0 : i+1)) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 22 | #define ISBASICBLOCK(blocks, start, end) \ |
| 23 | (blocks[start]==blocks[end]) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 25 | |
| 26 | #define CONST_STACK_CREATE() { \ |
| 27 | const_stack_size = 256; \ |
| 28 | const_stack = PyMem_New(PyObject *, const_stack_size); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 29 | if (!const_stack) { \ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 30 | PyErr_NoMemory(); \ |
| 31 | goto exitError; \ |
| 32 | } \ |
| 33 | } |
| 34 | |
| 35 | #define CONST_STACK_DELETE() do { \ |
| 36 | if (const_stack) \ |
| 37 | PyMem_Free(const_stack); \ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 38 | } while(0) |
| 39 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 40 | #define CONST_STACK_LEN() ((unsigned)(const_stack_top + 1)) |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 41 | |
| 42 | #define CONST_STACK_PUSH_OP(i) do { \ |
| 43 | PyObject *_x; \ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 44 | assert(_Py_OPCODE(codestr[i]) == LOAD_CONST); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 45 | assert(PyList_GET_SIZE(consts) > (Py_ssize_t)get_arg(codestr, i)); \ |
| 46 | _x = PyList_GET_ITEM(consts, get_arg(codestr, i)); \ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 47 | if (++const_stack_top >= const_stack_size) { \ |
| 48 | const_stack_size *= 2; \ |
| 49 | PyMem_Resize(const_stack, PyObject *, const_stack_size); \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 50 | if (!const_stack) { \ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 51 | PyErr_NoMemory(); \ |
| 52 | goto exitError; \ |
| 53 | } \ |
| 54 | } \ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 55 | const_stack[const_stack_top] = _x; \ |
| 56 | in_consts = 1; \ |
| 57 | } while(0) |
| 58 | |
| 59 | #define CONST_STACK_RESET() do { \ |
| 60 | const_stack_top = -1; \ |
| 61 | } while(0) |
| 62 | |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 63 | #define CONST_STACK_LASTN(i) \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 64 | &const_stack[CONST_STACK_LEN() - i] |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 65 | |
| 66 | #define CONST_STACK_POP(i) do { \ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 67 | assert(CONST_STACK_LEN() >= i); \ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 68 | const_stack_top -= i; \ |
| 69 | } while(0) |
| 70 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 71 | /* Scans back N consecutive LOAD_CONST instructions, skipping NOPs, |
| 72 | returns index of the Nth last's LOAD_CONST's EXTENDED_ARG prefix. |
| 73 | Callers are responsible to check CONST_STACK_LEN beforehand. |
| 74 | */ |
| 75 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 76 | lastn_const_start(const _Py_CODEUNIT *codestr, Py_ssize_t i, Py_ssize_t n) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 77 | { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 78 | assert(n > 0); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 79 | for (;;) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 80 | i--; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 81 | assert(i >= 0); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 82 | if (_Py_OPCODE(codestr[i]) == LOAD_CONST) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 83 | if (!--n) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 84 | while (i > 0 && _Py_OPCODE(codestr[i-1]) == EXTENDED_ARG) { |
| 85 | i--; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 86 | } |
| 87 | return i; |
| 88 | } |
| 89 | } |
| 90 | else { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 91 | assert(_Py_OPCODE(codestr[i]) == NOP || |
| 92 | _Py_OPCODE(codestr[i]) == EXTENDED_ARG); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 96 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 97 | /* Scans through EXTENDED ARGs, seeking the index of the effective opcode */ |
| 98 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 99 | find_op(const _Py_CODEUNIT *codestr, Py_ssize_t i) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 100 | { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 101 | while (_Py_OPCODE(codestr[i]) == EXTENDED_ARG) { |
| 102 | i++; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 103 | } |
| 104 | return i; |
| 105 | } |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 106 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 107 | /* Given the index of the effective opcode, |
| 108 | scan back to construct the oparg with EXTENDED_ARG */ |
| 109 | static unsigned int |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 110 | get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 111 | { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 112 | _Py_CODEUNIT word; |
| 113 | unsigned int oparg = _Py_OPARG(codestr[i]); |
| 114 | if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) { |
| 115 | oparg |= _Py_OPARG(word) << 8; |
| 116 | if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) { |
| 117 | oparg |= _Py_OPARG(word) << 16; |
| 118 | if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) { |
| 119 | oparg |= _Py_OPARG(word) << 24; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | return oparg; |
| 124 | } |
| 125 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 126 | /* Fill the region with NOPs. */ |
| 127 | static void |
| 128 | fill_nops(_Py_CODEUNIT *codestr, Py_ssize_t start, Py_ssize_t end) |
| 129 | { |
| 130 | memset(codestr + start, NOP, (end - start) * sizeof(_Py_CODEUNIT)); |
| 131 | } |
| 132 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 133 | /* Given the index of the effective opcode, |
| 134 | attempt to replace the argument, taking into account EXTENDED_ARG. |
| 135 | Returns -1 on failure, or the new op index on success */ |
| 136 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 137 | set_arg(_Py_CODEUNIT *codestr, Py_ssize_t i, unsigned int oparg) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 138 | { |
| 139 | unsigned int curarg = get_arg(codestr, i); |
| 140 | int curilen, newilen; |
| 141 | if (curarg == oparg) |
| 142 | return i; |
| 143 | curilen = instrsize(curarg); |
| 144 | newilen = instrsize(oparg); |
| 145 | if (curilen < newilen) { |
| 146 | return -1; |
| 147 | } |
| 148 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 149 | write_op_arg(codestr + i + 1 - curilen, _Py_OPCODE(codestr[i]), oparg, newilen); |
| 150 | fill_nops(codestr, i + 1 - curilen + newilen, i + 1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 151 | return i-curilen+newilen; |
| 152 | } |
| 153 | |
| 154 | /* Attempt to write op/arg at end of specified region of memory. |
| 155 | Preceding memory in the region is overwritten with NOPs. |
| 156 | Returns -1 on failure, op index on success */ |
| 157 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 158 | copy_op_arg(_Py_CODEUNIT *codestr, Py_ssize_t i, unsigned char op, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 159 | unsigned int oparg, Py_ssize_t maxi) |
| 160 | { |
| 161 | int ilen = instrsize(oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 162 | if (i + ilen > maxi) { |
| 163 | return -1; |
| 164 | } |
| 165 | write_op_arg(codestr + maxi - ilen, op, oparg, ilen); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 166 | fill_nops(codestr, i, maxi - ilen); |
| 167 | return maxi - 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | with LOAD_CONST (c1, c2, ... cn). |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 172 | The consts table must still be in list form so that the |
| 173 | new constant (c1, c2, ... cn) can be appended. |
| 174 | Called with codestr pointing to the first LOAD_CONST. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | Bails out with no change if one or more of the LOAD_CONSTs is missing. |
Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 176 | Also works for BUILD_LIST and BUILT_SET when followed by an "in" or "not in" |
| 177 | test; for BUILD_SET it assembles a frozenset rather than a tuple. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 178 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 179 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 180 | fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 181 | Py_ssize_t opcode_end, unsigned char opcode, |
| 182 | PyObject *consts, PyObject **objs, int n) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | PyObject *newconst, *constant; |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 185 | Py_ssize_t i, len_consts; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | /* Pre-conditions */ |
| 188 | assert(PyList_CheckExact(consts)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | /* Buildup new tuple of constants */ |
| 191 | newconst = PyTuple_New(n); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 192 | if (newconst == NULL) { |
| 193 | return -1; |
| 194 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | for (i=0 ; i<n ; i++) { |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 196 | constant = objs[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | Py_INCREF(constant); |
| 198 | PyTuple_SET_ITEM(newconst, i, constant); |
| 199 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | /* If it's a BUILD_SET, use the PyTuple we just built to create a |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 202 | PyFrozenSet, and use that as the constant instead: */ |
| 203 | if (opcode == BUILD_SET) { |
| 204 | Py_SETREF(newconst, PyFrozenSet_New(newconst)); |
| 205 | if (newconst == NULL) { |
| 206 | return -1; |
| 207 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | } |
Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | /* Append folded constant onto consts */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 211 | len_consts = PyList_GET_SIZE(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | if (PyList_Append(consts, newconst)) { |
| 213 | Py_DECREF(newconst); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 214 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | } |
| 216 | Py_DECREF(newconst); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 217 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 218 | return copy_op_arg(codestr, c_start, LOAD_CONST, len_consts, opcode_end); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 221 | /* Replace LOAD_CONST c1, LOAD_CONST c2, BINOP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | with LOAD_CONST binop(c1,c2) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 223 | The consts table must still be in list form so that the |
| 224 | new constant can be appended. |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 225 | Called with codestr pointing to the BINOP. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | Abandons the transformation if the folding fails (i.e. 1+'a'). |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 227 | If the new constant is a sequence, only folds when the size |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | is below a threshold value. That keeps pyc files from |
| 229 | becoming large in the presence of code like: (None,)*1000. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 230 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 231 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 232 | fold_binops_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 233 | Py_ssize_t opcode_end, unsigned char opcode, |
| 234 | PyObject *consts, PyObject **objs) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | PyObject *newconst, *v, *w; |
| 237 | Py_ssize_t len_consts, size; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | /* Pre-conditions */ |
| 240 | assert(PyList_CheckExact(consts)); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 241 | len_consts = PyList_GET_SIZE(consts); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | /* Create new constant */ |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 244 | v = objs[0]; |
| 245 | w = objs[1]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | switch (opcode) { |
| 247 | case BINARY_POWER: |
| 248 | newconst = PyNumber_Power(v, w, Py_None); |
| 249 | break; |
| 250 | case BINARY_MULTIPLY: |
| 251 | newconst = PyNumber_Multiply(v, w); |
| 252 | break; |
| 253 | case BINARY_TRUE_DIVIDE: |
| 254 | newconst = PyNumber_TrueDivide(v, w); |
| 255 | break; |
| 256 | case BINARY_FLOOR_DIVIDE: |
| 257 | newconst = PyNumber_FloorDivide(v, w); |
| 258 | break; |
| 259 | case BINARY_MODULO: |
| 260 | newconst = PyNumber_Remainder(v, w); |
| 261 | break; |
| 262 | case BINARY_ADD: |
| 263 | newconst = PyNumber_Add(v, w); |
| 264 | break; |
| 265 | case BINARY_SUBTRACT: |
| 266 | newconst = PyNumber_Subtract(v, w); |
| 267 | break; |
| 268 | case BINARY_SUBSCR: |
| 269 | newconst = PyObject_GetItem(v, w); |
| 270 | break; |
| 271 | case BINARY_LSHIFT: |
| 272 | newconst = PyNumber_Lshift(v, w); |
| 273 | break; |
| 274 | case BINARY_RSHIFT: |
| 275 | newconst = PyNumber_Rshift(v, w); |
| 276 | break; |
| 277 | case BINARY_AND: |
| 278 | newconst = PyNumber_And(v, w); |
| 279 | break; |
| 280 | case BINARY_XOR: |
| 281 | newconst = PyNumber_Xor(v, w); |
| 282 | break; |
| 283 | case BINARY_OR: |
| 284 | newconst = PyNumber_Or(v, w); |
| 285 | break; |
| 286 | default: |
| 287 | /* Called with an unknown opcode */ |
| 288 | PyErr_Format(PyExc_SystemError, |
| 289 | "unexpected binary operation %d on a constant", |
| 290 | opcode); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 291 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | } |
| 293 | if (newconst == NULL) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 294 | if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) { |
Raymond Hettinger | 819a064 | 2010-08-22 08:39:49 +0000 | [diff] [blame] | 295 | PyErr_Clear(); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 296 | } |
| 297 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | } |
| 299 | size = PyObject_Size(newconst); |
Raymond Hettinger | 819a064 | 2010-08-22 08:39:49 +0000 | [diff] [blame] | 300 | if (size == -1) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 301 | if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) { |
| 302 | return -1; |
| 303 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | PyErr_Clear(); |
Raymond Hettinger | 819a064 | 2010-08-22 08:39:49 +0000 | [diff] [blame] | 305 | } else if (size > 20) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | Py_DECREF(newconst); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 307 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | /* Append folded constant into consts table */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | if (PyList_Append(consts, newconst)) { |
| 312 | Py_DECREF(newconst); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 313 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | } |
| 315 | Py_DECREF(newconst); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 316 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 317 | return copy_op_arg(codestr, c_start, LOAD_CONST, len_consts, opcode_end); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 320 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 321 | fold_unaryops_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 322 | Py_ssize_t opcode_end, unsigned char opcode, |
| 323 | PyObject *consts, PyObject *v) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 324 | { |
Mark Dickinson | 7c9e803 | 2011-03-23 17:59:37 +0000 | [diff] [blame] | 325 | PyObject *newconst; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | Py_ssize_t len_consts; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | /* Pre-conditions */ |
| 329 | assert(PyList_CheckExact(consts)); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 330 | len_consts = PyList_GET_SIZE(consts); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | /* Create new constant */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | switch (opcode) { |
| 334 | case UNARY_NEGATIVE: |
Mark Dickinson | 7c9e803 | 2011-03-23 17:59:37 +0000 | [diff] [blame] | 335 | newconst = PyNumber_Negative(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | break; |
| 337 | case UNARY_INVERT: |
| 338 | newconst = PyNumber_Invert(v); |
| 339 | break; |
| 340 | case UNARY_POSITIVE: |
| 341 | newconst = PyNumber_Positive(v); |
| 342 | break; |
| 343 | default: |
| 344 | /* Called with an unknown opcode */ |
| 345 | PyErr_Format(PyExc_SystemError, |
| 346 | "unexpected unary operation %d on a constant", |
| 347 | opcode); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 348 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | } |
| 350 | if (newconst == NULL) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 351 | if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) { |
Raymond Hettinger | 819a064 | 2010-08-22 08:39:49 +0000 | [diff] [blame] | 352 | PyErr_Clear(); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 353 | } |
| 354 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | /* Append folded constant into consts table */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | if (PyList_Append(consts, newconst)) { |
| 359 | Py_DECREF(newconst); |
Victor Stinner | c82729e | 2013-11-14 01:21:00 +0100 | [diff] [blame] | 360 | PyErr_Clear(); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 361 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | } |
| 363 | Py_DECREF(newconst); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 364 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 365 | return copy_op_arg(codestr, c_start, LOAD_CONST, len_consts, opcode_end); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static unsigned int * |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 369 | markblocks(_Py_CODEUNIT *code, Py_ssize_t len) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 370 | { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 371 | unsigned int *blocks = PyMem_New(unsigned int, len); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 372 | int i, j, opcode, blockcnt = 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | if (blocks == NULL) { |
| 375 | PyErr_NoMemory(); |
| 376 | return NULL; |
| 377 | } |
| 378 | memset(blocks, 0, len*sizeof(int)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | /* Mark labels in the first pass */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 381 | for (i = 0; i < len; i++) { |
| 382 | opcode = _Py_OPCODE(code[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | switch (opcode) { |
| 384 | case FOR_ITER: |
| 385 | case JUMP_FORWARD: |
| 386 | case JUMP_IF_FALSE_OR_POP: |
| 387 | case JUMP_IF_TRUE_OR_POP: |
| 388 | case POP_JUMP_IF_FALSE: |
| 389 | case POP_JUMP_IF_TRUE: |
| 390 | case JUMP_ABSOLUTE: |
| 391 | case CONTINUE_LOOP: |
| 392 | case SETUP_LOOP: |
| 393 | case SETUP_EXCEPT: |
| 394 | case SETUP_FINALLY: |
| 395 | case SETUP_WITH: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 396 | case SETUP_ASYNC_WITH: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | j = GETJUMPTGT(code, i); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 398 | assert(j < len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | blocks[j] = 1; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | /* Build block numbers in the second pass */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 404 | for (i = 0; i < len; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | blockcnt += blocks[i]; /* increment blockcnt over labels */ |
| 406 | blocks[i] = blockcnt; |
| 407 | } |
| 408 | return blocks; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Perform basic peephole optimizations to components of a code object. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | The consts object should still be in list form to allow new constants |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 413 | to be appended. |
| 414 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 415 | To keep the optimizer simple, it bails when the lineno table has complex |
| 416 | encoding for gaps >= 255. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 417 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 418 | Optimizations are restricted to simple transformations occurring within a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | single basic block. All transformations keep the code size the same or |
| 420 | smaller. For those that reduce size, the gaps are initially filled with |
| 421 | NOPs. Later those NOPs are removed and the jump addresses retargeted in |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 422 | a single pass. */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 423 | |
| 424 | PyObject * |
| 425 | PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 426 | PyObject *lnotab_obj) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 427 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 428 | Py_ssize_t h, i, nexti, op_start, codelen, tgt; |
| 429 | unsigned int j, nops; |
| 430 | unsigned char opcode, nextop; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 431 | _Py_CODEUNIT *codestr = NULL; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 432 | unsigned char *lnotab; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 433 | unsigned int cum_orig_offset, last_offset; |
Serhiy Storchaka | 67559bf | 2015-02-16 21:13:24 +0200 | [diff] [blame] | 434 | Py_ssize_t tabsiz; |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 435 | PyObject **const_stack = NULL; |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 436 | Py_ssize_t const_stack_top = -1; |
| 437 | Py_ssize_t const_stack_size = 0; |
| 438 | int in_consts = 0; /* whether we are in a LOAD_CONST sequence */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | unsigned int *blocks = NULL; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | /* Bail out if an exception is set */ |
| 442 | if (PyErr_Occurred()) |
| 443 | goto exitError; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 444 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 445 | /* Bypass optimization when the lnotab table is too complex */ |
| 446 | assert(PyBytes_Check(lnotab_obj)); |
| 447 | lnotab = (unsigned char*)PyBytes_AS_STRING(lnotab_obj); |
| 448 | tabsiz = PyBytes_GET_SIZE(lnotab_obj); |
| 449 | assert(tabsiz == 0 || Py_REFCNT(lnotab_obj) == 1); |
| 450 | if (memchr(lnotab, 255, tabsiz) != NULL) { |
| 451 | /* 255 value are used for multibyte bytecode instructions */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | goto exitUnchanged; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 453 | } |
| 454 | /* Note: -128 and 127 special values for line number delta are ok, |
| 455 | the peephole optimizer doesn't modify line numbers. */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | assert(PyBytes_Check(code)); |
| 458 | codelen = PyBytes_GET_SIZE(code); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 459 | assert(codelen % sizeof(_Py_CODEUNIT) == 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | /* Make a modifiable copy of the code string */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 462 | codestr = (_Py_CODEUNIT *)PyMem_Malloc(codelen); |
Victor Stinner | e0af3a8 | 2013-07-09 00:32:04 +0200 | [diff] [blame] | 463 | if (codestr == NULL) { |
| 464 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | goto exitError; |
Victor Stinner | e0af3a8 | 2013-07-09 00:32:04 +0200 | [diff] [blame] | 466 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 467 | memcpy(codestr, PyBytes_AS_STRING(code), codelen); |
| 468 | codelen /= sizeof(_Py_CODEUNIT); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | blocks = markblocks(codestr, codelen); |
| 471 | if (blocks == NULL) |
| 472 | goto exitError; |
| 473 | assert(PyList_Check(consts)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 475 | CONST_STACK_CREATE(); |
| 476 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 477 | for (i=find_op(codestr, 0) ; i<codelen ; i=nexti) { |
Serhiy Storchaka | a1e9ab3 | 2016-09-11 15:19:12 +0300 | [diff] [blame] | 478 | opcode = _Py_OPCODE(codestr[i]); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 479 | op_start = i; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 480 | while (op_start >= 1 && _Py_OPCODE(codestr[op_start-1]) == EXTENDED_ARG) { |
| 481 | op_start--; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 482 | } |
| 483 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 484 | nexti = i + 1; |
| 485 | while (nexti < codelen && _Py_OPCODE(codestr[nexti]) == EXTENDED_ARG) |
| 486 | nexti++; |
| 487 | nextop = nexti < codelen ? _Py_OPCODE(codestr[nexti]) : 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 488 | |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 489 | if (!in_consts) { |
| 490 | CONST_STACK_RESET(); |
| 491 | } |
| 492 | in_consts = 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | switch (opcode) { |
| 495 | /* Replace UNARY_NOT POP_JUMP_IF_FALSE |
| 496 | with POP_JUMP_IF_TRUE */ |
| 497 | case UNARY_NOT: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 498 | if (nextop != POP_JUMP_IF_FALSE |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 499 | || !ISBASICBLOCK(blocks, op_start, i + 1)) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 500 | break; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 501 | fill_nops(codestr, op_start, i + 1); |
| 502 | codestr[nexti] = PACKOPARG(POP_JUMP_IF_TRUE, _Py_OPARG(codestr[nexti])); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 503 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | /* not a is b --> a is not b |
| 506 | not a in b --> a not in b |
| 507 | not a is not b --> a is b |
| 508 | not a not in b --> a in b |
| 509 | */ |
| 510 | case COMPARE_OP: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 511 | j = get_arg(codestr, i); |
| 512 | if (j < 6 || j > 9 || |
| 513 | nextop != UNARY_NOT || |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 514 | !ISBASICBLOCK(blocks, op_start, i + 1)) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 515 | break; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 516 | codestr[i] = PACKOPARG(opcode, j^1); |
| 517 | fill_nops(codestr, i + 1, nexti + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | /* Skip over LOAD_CONST trueconst |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 521 | POP_JUMP_IF_FALSE xx. This improves |
| 522 | "while 1" performance. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | case LOAD_CONST: |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 524 | CONST_STACK_PUSH_OP(i); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 525 | if (nextop != POP_JUMP_IF_FALSE || |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 526 | !ISBASICBLOCK(blocks, op_start, i + 1) || |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 527 | !PyObject_IsTrue(PyList_GET_ITEM(consts, get_arg(codestr, i)))) |
| 528 | break; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 529 | fill_nops(codestr, op_start, nexti + 1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 530 | CONST_STACK_POP(1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 532 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 533 | /* Try to fold tuples of constants (includes a case for lists |
| 534 | and sets which are only used for "in" and "not in" tests). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 536 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 537 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 538 | case BUILD_TUPLE: |
| 539 | case BUILD_LIST: |
| 540 | case BUILD_SET: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 541 | j = get_arg(codestr, i); |
| 542 | if (j > 0 && CONST_STACK_LEN() >= j) { |
| 543 | h = lastn_const_start(codestr, op_start, j); |
| 544 | if ((opcode == BUILD_TUPLE && |
| 545 | ISBASICBLOCK(blocks, h, op_start)) || |
| 546 | ((opcode == BUILD_LIST || opcode == BUILD_SET) && |
| 547 | ((nextop==COMPARE_OP && |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 548 | (_Py_OPARG(codestr[nexti]) == PyCmp_IN || |
| 549 | _Py_OPARG(codestr[nexti]) == PyCmp_NOT_IN)) || |
| 550 | nextop == GET_ITER) && ISBASICBLOCK(blocks, h, i + 1))) { |
| 551 | h = fold_tuple_on_constants(codestr, h, i + 1, opcode, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 552 | consts, CONST_STACK_LASTN(j), j); |
| 553 | if (h >= 0) { |
| 554 | CONST_STACK_POP(j); |
| 555 | CONST_STACK_PUSH_OP(h); |
| 556 | } |
| 557 | break; |
| 558 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 560 | if (nextop != UNPACK_SEQUENCE || |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 561 | !ISBASICBLOCK(blocks, op_start, i + 1) || |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 562 | j != get_arg(codestr, nexti) || |
Raymond Hettinger | 0661e91 | 2011-03-15 15:03:36 -0700 | [diff] [blame] | 563 | opcode == BUILD_SET) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 564 | break; |
| 565 | if (j < 2) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 566 | fill_nops(codestr, op_start, nexti + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | } else if (j == 2) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 568 | codestr[op_start] = PACKOPARG(ROT_TWO, 0); |
| 569 | fill_nops(codestr, op_start + 1, nexti + 1); |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 570 | CONST_STACK_RESET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | } else if (j == 3) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 572 | codestr[op_start] = PACKOPARG(ROT_THREE, 0); |
| 573 | codestr[op_start + 1] = PACKOPARG(ROT_TWO, 0); |
| 574 | fill_nops(codestr, op_start + 2, nexti + 1); |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 575 | CONST_STACK_RESET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | } |
| 577 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | /* Fold binary ops on constants. |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 580 | LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | case BINARY_POWER: |
| 582 | case BINARY_MULTIPLY: |
| 583 | case BINARY_TRUE_DIVIDE: |
| 584 | case BINARY_FLOOR_DIVIDE: |
| 585 | case BINARY_MODULO: |
| 586 | case BINARY_ADD: |
| 587 | case BINARY_SUBTRACT: |
| 588 | case BINARY_SUBSCR: |
| 589 | case BINARY_LSHIFT: |
| 590 | case BINARY_RSHIFT: |
| 591 | case BINARY_AND: |
| 592 | case BINARY_XOR: |
| 593 | case BINARY_OR: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 594 | if (CONST_STACK_LEN() < 2) |
| 595 | break; |
| 596 | h = lastn_const_start(codestr, op_start, 2); |
| 597 | if (ISBASICBLOCK(blocks, h, op_start)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 598 | h = fold_binops_on_constants(codestr, h, i + 1, opcode, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 599 | consts, CONST_STACK_LASTN(2)); |
| 600 | if (h >= 0) { |
| 601 | CONST_STACK_POP(2); |
| 602 | CONST_STACK_PUSH_OP(h); |
| 603 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | } |
| 605 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | /* Fold unary ops on constants. |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 608 | LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | case UNARY_NEGATIVE: |
| 610 | case UNARY_INVERT: |
| 611 | case UNARY_POSITIVE: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 612 | if (CONST_STACK_LEN() < 1) |
| 613 | break; |
| 614 | h = lastn_const_start(codestr, op_start, 1); |
| 615 | if (ISBASICBLOCK(blocks, h, op_start)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 616 | h = fold_unaryops_on_constants(codestr, h, i + 1, opcode, |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 617 | consts, *CONST_STACK_LASTN(1)); |
| 618 | if (h >= 0) { |
| 619 | CONST_STACK_POP(1); |
| 620 | CONST_STACK_PUSH_OP(h); |
| 621 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | } |
| 623 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | /* Simplify conditional jump to conditional jump where the |
| 626 | result of the first test implies the success of a similar |
| 627 | test or the failure of the opposite test. |
| 628 | Arises in code like: |
| 629 | "if a and b:" |
| 630 | "if a or b:" |
| 631 | "a and b or c" |
| 632 | "(a and b) and c" |
| 633 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 634 | --> x:JUMP_IF_FALSE_OR_POP z |
| 635 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 636 | --> x:POP_JUMP_IF_FALSE y+1 |
| 637 | where y+1 is the instruction following the second test. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | */ |
| 639 | case JUMP_IF_FALSE_OR_POP: |
| 640 | case JUMP_IF_TRUE_OR_POP: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 641 | h = get_arg(codestr, i) / sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 642 | tgt = find_op(codestr, h); |
| 643 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 644 | j = _Py_OPCODE(codestr[tgt]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | if (CONDITIONAL_JUMP(j)) { |
Raymond Hettinger | 08eef3f | 2016-08-07 20:20:33 -0700 | [diff] [blame] | 646 | /* NOTE: all possible jumps here are absolute. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) { |
Raymond Hettinger | 08eef3f | 2016-08-07 20:20:33 -0700 | [diff] [blame] | 648 | /* The second jump will be taken iff the first is. |
| 649 | The current opcode inherits its target's |
| 650 | stack effect */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 651 | h = set_arg(codestr, i, get_arg(codestr, tgt)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | } else { |
Raymond Hettinger | 08eef3f | 2016-08-07 20:20:33 -0700 | [diff] [blame] | 653 | /* The second jump is not taken if the first is (so |
| 654 | jump past it), and all conditional jumps pop their |
| 655 | argument when they're not taken (so change the |
| 656 | first jump to pop its argument when it's taken). */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 657 | h = set_arg(codestr, i, (tgt + 1) * sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 658 | j = opcode == JUMP_IF_TRUE_OR_POP ? |
| 659 | POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE; |
| 660 | } |
| 661 | |
| 662 | if (h >= 0) { |
| 663 | nexti = h; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 664 | codestr[nexti] = PACKOPARG(j, _Py_OPARG(codestr[nexti])); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 665 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | /* Intentional fallthrough */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | /* Replace jumps to unconditional jumps */ |
| 671 | case POP_JUMP_IF_FALSE: |
| 672 | case POP_JUMP_IF_TRUE: |
| 673 | case FOR_ITER: |
| 674 | case JUMP_FORWARD: |
| 675 | case JUMP_ABSOLUTE: |
| 676 | case CONTINUE_LOOP: |
| 677 | case SETUP_LOOP: |
| 678 | case SETUP_EXCEPT: |
| 679 | case SETUP_FINALLY: |
| 680 | case SETUP_WITH: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 681 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 682 | h = GETJUMPTGT(codestr, i); |
| 683 | tgt = find_op(codestr, h); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | /* Replace JUMP_* to a RETURN into just a RETURN */ |
| 685 | if (UNCONDITIONAL_JUMP(opcode) && |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 686 | _Py_OPCODE(codestr[tgt]) == RETURN_VALUE) { |
| 687 | codestr[op_start] = PACKOPARG(RETURN_VALUE, 0); |
| 688 | fill_nops(codestr, op_start + 1, i + 1); |
| 689 | } else if (UNCONDITIONAL_JUMP(_Py_OPCODE(codestr[tgt]))) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 690 | j = GETJUMPTGT(codestr, tgt); |
| 691 | if (opcode == JUMP_FORWARD) { /* JMP_ABS can go backwards */ |
| 692 | opcode = JUMP_ABSOLUTE; |
| 693 | } else if (!ABSOLUTE_JUMP(opcode)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 694 | if ((Py_ssize_t)j < i + 1) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 695 | break; /* No backward relative jumps */ |
| 696 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 697 | j -= i + 1; /* Calc relative jump addr */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 698 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 699 | j *= sizeof(_Py_CODEUNIT); |
| 700 | copy_op_arg(codestr, op_start, opcode, j, i + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 703 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 704 | /* Remove unreachable ops after RETURN */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | case RETURN_VALUE: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 706 | h = i + 1; |
Serhiy Storchaka | 7db3c48 | 2016-10-25 09:30:43 +0300 | [diff] [blame] | 707 | while (h < codelen && ISBASICBLOCK(blocks, i, h)) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 708 | h++; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 709 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 710 | if (h > i + 1) { |
Serhiy Storchaka | 7db3c48 | 2016-10-25 09:30:43 +0300 | [diff] [blame] | 711 | fill_nops(codestr, i + 1, h); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 712 | nexti = find_op(codestr, h); |
| 713 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | break; |
| 715 | } |
| 716 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 717 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 718 | /* Fixup lnotab */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 719 | for (i = 0, nops = 0; i < codelen; i++) { |
Serhiy Storchaka | 67559bf | 2015-02-16 21:13:24 +0200 | [diff] [blame] | 720 | assert(i - nops <= INT_MAX); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 721 | /* original code offset => new code offset */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 722 | blocks[i] = i - nops; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 723 | if (_Py_OPCODE(codestr[i]) == NOP) |
| 724 | nops++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 726 | cum_orig_offset = 0; |
| 727 | last_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | for (i=0 ; i < tabsiz ; i+=2) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 729 | unsigned int offset_delta, new_offset; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 730 | cum_orig_offset += lnotab[i]; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 731 | assert(cum_orig_offset % sizeof(_Py_CODEUNIT) == 0); |
| 732 | new_offset = blocks[cum_orig_offset / sizeof(_Py_CODEUNIT)] * |
| 733 | sizeof(_Py_CODEUNIT); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 734 | offset_delta = new_offset - last_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 735 | assert(offset_delta <= 255); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 736 | lnotab[i] = (unsigned char)offset_delta; |
| 737 | last_offset = new_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | /* Remove NOPs and fixup jump targets */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 741 | for (op_start = i = h = 0; i < codelen; i++, op_start = i) { |
| 742 | j = _Py_OPARG(codestr[i]); |
| 743 | while (_Py_OPCODE(codestr[i]) == EXTENDED_ARG) { |
| 744 | i++; |
| 745 | j = j<<8 | _Py_OPARG(codestr[i]); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 746 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 747 | opcode = _Py_OPCODE(codestr[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | switch (opcode) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 749 | case NOP:continue; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | case JUMP_ABSOLUTE: |
| 752 | case CONTINUE_LOOP: |
| 753 | case POP_JUMP_IF_FALSE: |
| 754 | case POP_JUMP_IF_TRUE: |
| 755 | case JUMP_IF_FALSE_OR_POP: |
| 756 | case JUMP_IF_TRUE_OR_POP: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 757 | j = blocks[j / sizeof(_Py_CODEUNIT)] * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | case FOR_ITER: |
| 761 | case JUMP_FORWARD: |
| 762 | case SETUP_LOOP: |
| 763 | case SETUP_EXCEPT: |
| 764 | case SETUP_FINALLY: |
| 765 | case SETUP_WITH: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 766 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 767 | j = blocks[j / sizeof(_Py_CODEUNIT) + i + 1] - blocks[i] - 1; |
| 768 | j *= sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | break; |
| 770 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 771 | nexti = i - op_start + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 772 | if (instrsize(j) > nexti) |
| 773 | goto exitUnchanged; |
| 774 | /* If instrsize(j) < nexti, we'll emit EXTENDED_ARG 0 */ |
| 775 | write_op_arg(codestr + h, opcode, j, nexti); |
| 776 | h += nexti; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 778 | assert(h + (Py_ssize_t)nops == codelen); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 780 | CONST_STACK_DELETE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | PyMem_Free(blocks); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 782 | code = PyBytes_FromStringAndSize((char *)codestr, h * sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 783 | PyMem_Free(codestr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | return code; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 785 | |
Alexandre Vassalotti | 6f82818 | 2009-07-21 02:51:58 +0000 | [diff] [blame] | 786 | exitError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 787 | code = NULL; |
Alexandre Vassalotti | 6f82818 | 2009-07-21 02:51:58 +0000 | [diff] [blame] | 788 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 789 | exitUnchanged: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 790 | Py_XINCREF(code); |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 791 | CONST_STACK_DELETE(); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 792 | PyMem_Free(blocks); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 793 | PyMem_Free(codestr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | return code; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 795 | } |