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) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 16 | #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE \ |
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 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 26 | /* Scans back N consecutive LOAD_CONST instructions, skipping NOPs, |
| 27 | returns index of the Nth last's LOAD_CONST's EXTENDED_ARG prefix. |
| 28 | Callers are responsible to check CONST_STACK_LEN beforehand. |
| 29 | */ |
| 30 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 31 | 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] | 32 | { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 33 | assert(n > 0); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 34 | for (;;) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 35 | i--; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 36 | assert(i >= 0); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 37 | if (_Py_OPCODE(codestr[i]) == LOAD_CONST) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 38 | if (!--n) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 39 | while (i > 0 && _Py_OPCODE(codestr[i-1]) == EXTENDED_ARG) { |
| 40 | i--; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 41 | } |
| 42 | return i; |
| 43 | } |
| 44 | } |
| 45 | else { |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 46 | assert(_Py_OPCODE(codestr[i]) == EXTENDED_ARG); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 50 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 51 | /* Scans through EXTENDED ARGs, seeking the index of the effective opcode */ |
| 52 | static Py_ssize_t |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 53 | find_op(const _Py_CODEUNIT *codestr, Py_ssize_t codelen, Py_ssize_t i) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 54 | { |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 55 | while (i < codelen && _Py_OPCODE(codestr[i]) == EXTENDED_ARG) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 56 | i++; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 57 | } |
| 58 | return i; |
| 59 | } |
Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 60 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 61 | /* Given the index of the effective opcode, |
| 62 | scan back to construct the oparg with EXTENDED_ARG */ |
| 63 | static unsigned int |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 64 | get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 65 | { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 66 | _Py_CODEUNIT word; |
| 67 | unsigned int oparg = _Py_OPARG(codestr[i]); |
| 68 | if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) { |
| 69 | oparg |= _Py_OPARG(word) << 8; |
| 70 | if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) { |
| 71 | oparg |= _Py_OPARG(word) << 16; |
| 72 | if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) { |
| 73 | oparg |= _Py_OPARG(word) << 24; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | return oparg; |
| 78 | } |
| 79 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 80 | /* Fill the region with NOPs. */ |
| 81 | static void |
| 82 | fill_nops(_Py_CODEUNIT *codestr, Py_ssize_t start, Py_ssize_t end) |
| 83 | { |
| 84 | memset(codestr + start, NOP, (end - start) * sizeof(_Py_CODEUNIT)); |
| 85 | } |
| 86 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 87 | /* Given the index of the effective opcode, |
| 88 | attempt to replace the argument, taking into account EXTENDED_ARG. |
| 89 | Returns -1 on failure, or the new op index on success */ |
| 90 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 91 | set_arg(_Py_CODEUNIT *codestr, Py_ssize_t i, unsigned int oparg) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 92 | { |
| 93 | unsigned int curarg = get_arg(codestr, i); |
| 94 | int curilen, newilen; |
| 95 | if (curarg == oparg) |
| 96 | return i; |
| 97 | curilen = instrsize(curarg); |
| 98 | newilen = instrsize(oparg); |
| 99 | if (curilen < newilen) { |
| 100 | return -1; |
| 101 | } |
| 102 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 103 | write_op_arg(codestr + i + 1 - curilen, _Py_OPCODE(codestr[i]), oparg, newilen); |
| 104 | fill_nops(codestr, i + 1 - curilen + newilen, i + 1); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 105 | return i-curilen+newilen; |
| 106 | } |
| 107 | |
| 108 | /* Attempt to write op/arg at end of specified region of memory. |
| 109 | Preceding memory in the region is overwritten with NOPs. |
| 110 | Returns -1 on failure, op index on success */ |
| 111 | static Py_ssize_t |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 112 | 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] | 113 | unsigned int oparg, Py_ssize_t maxi) |
| 114 | { |
| 115 | int ilen = instrsize(oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 116 | if (i + ilen > maxi) { |
| 117 | return -1; |
| 118 | } |
| 119 | write_op_arg(codestr + maxi - ilen, op, oparg, ilen); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 120 | fill_nops(codestr, i, maxi - ilen); |
| 121 | return maxi - 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* 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] | 125 | with LOAD_CONST (c1, c2, ... cn). |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 126 | The consts table must still be in list form so that the |
| 127 | new constant (c1, c2, ... cn) can be appended. |
| 128 | Called with codestr pointing to the first LOAD_CONST. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 129 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 130 | static Py_ssize_t |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 131 | fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t codelen, |
| 132 | Py_ssize_t c_start, Py_ssize_t opcode_end, |
| 133 | PyObject *consts, int n) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | /* Pre-conditions */ |
| 136 | assert(PyList_CheckExact(consts)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | /* Buildup new tuple of constants */ |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 139 | PyObject *newconst = PyTuple_New(n); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 140 | if (newconst == NULL) { |
| 141 | return -1; |
| 142 | } |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 143 | |
| 144 | for (Py_ssize_t i = 0, pos = c_start; i < n; i++, pos++) { |
| 145 | assert(pos < opcode_end); |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 146 | pos = find_op(codestr, codelen, pos); |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 147 | assert(_Py_OPCODE(codestr[pos]) == LOAD_CONST); |
| 148 | |
| 149 | unsigned int arg = get_arg(codestr, pos); |
| 150 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | Py_INCREF(constant); |
| 152 | PyTuple_SET_ITEM(newconst, i, constant); |
| 153 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 154 | |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 155 | Py_ssize_t index = PyList_GET_SIZE(consts); |
| 156 | #if SIZEOF_SIZE_T > SIZEOF_INT |
| 157 | if ((size_t)index >= UINT_MAX - 1) { |
| 158 | Py_DECREF(newconst); |
| 159 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 160 | return -1; |
| 161 | } |
| 162 | #endif |
| 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | /* Append folded constant onto consts */ |
| 165 | if (PyList_Append(consts, newconst)) { |
| 166 | Py_DECREF(newconst); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 167 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | } |
| 169 | Py_DECREF(newconst); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 170 | |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 171 | return copy_op_arg(codestr, c_start, LOAD_CONST, |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 172 | (unsigned int)index, opcode_end); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 175 | static unsigned int * |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 176 | markblocks(_Py_CODEUNIT *code, Py_ssize_t len) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 177 | { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 178 | unsigned int *blocks = PyMem_New(unsigned int, len); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 179 | int i, j, opcode, blockcnt = 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | if (blocks == NULL) { |
| 182 | PyErr_NoMemory(); |
| 183 | return NULL; |
| 184 | } |
| 185 | memset(blocks, 0, len*sizeof(int)); |
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 | /* Mark labels in the first pass */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 188 | for (i = 0; i < len; i++) { |
| 189 | opcode = _Py_OPCODE(code[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | switch (opcode) { |
| 191 | case FOR_ITER: |
| 192 | case JUMP_FORWARD: |
| 193 | case JUMP_IF_FALSE_OR_POP: |
| 194 | case JUMP_IF_TRUE_OR_POP: |
| 195 | case POP_JUMP_IF_FALSE: |
| 196 | case POP_JUMP_IF_TRUE: |
| 197 | case JUMP_ABSOLUTE: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | case SETUP_FINALLY: |
| 199 | case SETUP_WITH: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 200 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 201 | case CALL_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | j = GETJUMPTGT(code, i); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 203 | assert(j < len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | blocks[j] = 1; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | /* Build block numbers in the second pass */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 209 | for (i = 0; i < len; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | blockcnt += blocks[i]; /* increment blockcnt over labels */ |
| 211 | blocks[i] = blockcnt; |
| 212 | } |
| 213 | return blocks; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Perform basic peephole optimizations to components of a code object. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | 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] | 218 | to be appended. |
| 219 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 220 | To keep the optimizer simple, it bails when the lineno table has complex |
| 221 | encoding for gaps >= 255. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 222 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 223 | Optimizations are restricted to simple transformations occurring within a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | single basic block. All transformations keep the code size the same or |
| 225 | smaller. For those that reduce size, the gaps are initially filled with |
| 226 | NOPs. Later those NOPs are removed and the jump addresses retargeted in |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 227 | a single pass. */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 228 | |
| 229 | PyObject * |
| 230 | PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 231 | PyObject *lnotab_obj) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 232 | { |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 233 | Py_ssize_t h, i, nexti, op_start, tgt; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 234 | unsigned int j, nops; |
| 235 | unsigned char opcode, nextop; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 236 | _Py_CODEUNIT *codestr = NULL; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 237 | unsigned char *lnotab; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 238 | unsigned int cum_orig_offset, last_offset; |
Serhiy Storchaka | 67559bf | 2015-02-16 21:13:24 +0200 | [diff] [blame] | 239 | Py_ssize_t tabsiz; |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 240 | // Count runs of consecutive LOAD_CONSTs |
| 241 | unsigned int cumlc = 0, lastlc = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | unsigned int *blocks = NULL; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | /* Bail out if an exception is set */ |
| 245 | if (PyErr_Occurred()) |
| 246 | goto exitError; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 247 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 248 | /* Bypass optimization when the lnotab table is too complex */ |
| 249 | assert(PyBytes_Check(lnotab_obj)); |
| 250 | lnotab = (unsigned char*)PyBytes_AS_STRING(lnotab_obj); |
| 251 | tabsiz = PyBytes_GET_SIZE(lnotab_obj); |
| 252 | assert(tabsiz == 0 || Py_REFCNT(lnotab_obj) == 1); |
Miss Islington (bot) | 5282b3b | 2019-06-13 11:35:40 -0700 | [diff] [blame] | 253 | |
| 254 | /* Don't optimize if lnotab contains instruction pointer delta larger |
| 255 | than +255 (encoded as multiple bytes), just to keep the peephole optimizer |
| 256 | simple. The optimizer leaves line number deltas unchanged. */ |
| 257 | |
Miss Islington (bot) | d5ba8bb | 2019-08-27 10:13:52 -0700 | [diff] [blame] | 258 | for (i = 0; i < tabsiz; i += 2) { |
| 259 | if (lnotab[i] == 255) { |
Miss Islington (bot) | 5282b3b | 2019-06-13 11:35:40 -0700 | [diff] [blame] | 260 | goto exitUnchanged; |
| 261 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 262 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | assert(PyBytes_Check(code)); |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 265 | Py_ssize_t codesize = PyBytes_GET_SIZE(code); |
| 266 | assert(codesize % sizeof(_Py_CODEUNIT) == 0); |
| 267 | Py_ssize_t codelen = codesize / sizeof(_Py_CODEUNIT); |
| 268 | if (codelen > INT_MAX) { |
| 269 | /* Python assembler is limited to INT_MAX: see assembler.a_offset in |
| 270 | compile.c. */ |
| 271 | goto exitUnchanged; |
| 272 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | /* Make a modifiable copy of the code string */ |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 275 | codestr = (_Py_CODEUNIT *)PyMem_Malloc(codesize); |
Victor Stinner | e0af3a8 | 2013-07-09 00:32:04 +0200 | [diff] [blame] | 276 | if (codestr == NULL) { |
| 277 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | goto exitError; |
Victor Stinner | e0af3a8 | 2013-07-09 00:32:04 +0200 | [diff] [blame] | 279 | } |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 280 | memcpy(codestr, PyBytes_AS_STRING(code), codesize); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | blocks = markblocks(codestr, codelen); |
| 283 | if (blocks == NULL) |
| 284 | goto exitError; |
| 285 | assert(PyList_Check(consts)); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 286 | |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 287 | for (i=find_op(codestr, codelen, 0) ; i<codelen ; i=nexti) { |
Serhiy Storchaka | a1e9ab3 | 2016-09-11 15:19:12 +0300 | [diff] [blame] | 288 | opcode = _Py_OPCODE(codestr[i]); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 289 | op_start = i; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 290 | while (op_start >= 1 && _Py_OPCODE(codestr[op_start-1]) == EXTENDED_ARG) { |
| 291 | op_start--; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 292 | } |
| 293 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 294 | nexti = i + 1; |
| 295 | while (nexti < codelen && _Py_OPCODE(codestr[nexti]) == EXTENDED_ARG) |
| 296 | nexti++; |
| 297 | nextop = nexti < codelen ? _Py_OPCODE(codestr[nexti]) : 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 298 | |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 299 | lastlc = cumlc; |
| 300 | cumlc = 0; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | switch (opcode) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | /* Skip over LOAD_CONST trueconst |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 304 | POP_JUMP_IF_FALSE xx. This improves |
| 305 | "while 1" performance. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | case LOAD_CONST: |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 307 | cumlc = lastlc + 1; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 308 | if (nextop != POP_JUMP_IF_FALSE || |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 309 | !ISBASICBLOCK(blocks, op_start, i + 1)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 310 | break; |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 311 | } |
| 312 | PyObject* cnt = PyList_GET_ITEM(consts, get_arg(codestr, i)); |
| 313 | int is_true = PyObject_IsTrue(cnt); |
Miss Islington (bot) | 284daea | 2019-06-15 08:22:08 -0700 | [diff] [blame] | 314 | if (is_true == -1) { |
| 315 | goto exitError; |
| 316 | } |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 317 | if (is_true == 1) { |
| 318 | fill_nops(codestr, op_start, nexti + 1); |
| 319 | cumlc = 0; |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 320 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 322 | |
Serhiy Storchaka | 15a8728 | 2017-12-14 20:24:31 +0200 | [diff] [blame] | 323 | /* Try to fold tuples of constants. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 325 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 326 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 327 | case BUILD_TUPLE: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 328 | j = get_arg(codestr, i); |
INADA Naoki | 87010e8 | 2017-12-18 15:52:54 +0900 | [diff] [blame] | 329 | if (j > 0 && lastlc >= j) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 330 | h = lastn_const_start(codestr, op_start, j); |
Serhiy Storchaka | 15a8728 | 2017-12-14 20:24:31 +0200 | [diff] [blame] | 331 | if (ISBASICBLOCK(blocks, h, op_start)) { |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 332 | h = fold_tuple_on_constants(codestr, codelen, |
| 333 | h, i+1, consts, j); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 334 | break; |
| 335 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 337 | if (nextop != UNPACK_SEQUENCE || |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 338 | !ISBASICBLOCK(blocks, op_start, i + 1) || |
Serhiy Storchaka | 15a8728 | 2017-12-14 20:24:31 +0200 | [diff] [blame] | 339 | j != get_arg(codestr, nexti)) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 340 | break; |
| 341 | if (j < 2) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 342 | fill_nops(codestr, op_start, nexti + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | } else if (j == 2) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 344 | codestr[op_start] = PACKOPARG(ROT_TWO, 0); |
| 345 | fill_nops(codestr, op_start + 1, nexti + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | } else if (j == 3) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 347 | codestr[op_start] = PACKOPARG(ROT_THREE, 0); |
| 348 | codestr[op_start + 1] = PACKOPARG(ROT_TWO, 0); |
| 349 | fill_nops(codestr, op_start + 2, nexti + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | } |
| 351 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | /* Simplify conditional jump to conditional jump where the |
| 354 | result of the first test implies the success of a similar |
| 355 | test or the failure of the opposite test. |
| 356 | Arises in code like: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | "a and b or c" |
| 358 | "(a and b) and c" |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 359 | "(a or b) or c" |
| 360 | "(a or b) and c" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 362 | --> x:JUMP_IF_FALSE_OR_POP z |
| 363 | 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] | 364 | --> x:POP_JUMP_IF_FALSE y+1 |
| 365 | where y+1 is the instruction following the second test. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | */ |
| 367 | case JUMP_IF_FALSE_OR_POP: |
| 368 | case JUMP_IF_TRUE_OR_POP: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 369 | h = get_arg(codestr, i) / sizeof(_Py_CODEUNIT); |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 370 | tgt = find_op(codestr, codelen, h); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 371 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 372 | j = _Py_OPCODE(codestr[tgt]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | if (CONDITIONAL_JUMP(j)) { |
Raymond Hettinger | 08eef3f | 2016-08-07 20:20:33 -0700 | [diff] [blame] | 374 | /* NOTE: all possible jumps here are absolute. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) { |
Raymond Hettinger | 08eef3f | 2016-08-07 20:20:33 -0700 | [diff] [blame] | 376 | /* The second jump will be taken iff the first is. |
| 377 | The current opcode inherits its target's |
| 378 | stack effect */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 379 | h = set_arg(codestr, i, get_arg(codestr, tgt)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | } else { |
Raymond Hettinger | 08eef3f | 2016-08-07 20:20:33 -0700 | [diff] [blame] | 381 | /* The second jump is not taken if the first is (so |
| 382 | jump past it), and all conditional jumps pop their |
| 383 | argument when they're not taken (so change the |
| 384 | first jump to pop its argument when it's taken). */ |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 385 | Py_ssize_t arg = (tgt + 1); |
| 386 | /* cannot overflow: codelen <= INT_MAX */ |
| 387 | assert((size_t)arg <= UINT_MAX / sizeof(_Py_CODEUNIT)); |
| 388 | arg *= sizeof(_Py_CODEUNIT); |
| 389 | h = set_arg(codestr, i, (unsigned int)arg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 390 | j = opcode == JUMP_IF_TRUE_OR_POP ? |
| 391 | POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE; |
| 392 | } |
| 393 | |
| 394 | if (h >= 0) { |
| 395 | nexti = h; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 396 | codestr[nexti] = PACKOPARG(j, _Py_OPARG(codestr[nexti])); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 397 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | /* Intentional fallthrough */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | /* Replace jumps to unconditional jumps */ |
| 403 | case POP_JUMP_IF_FALSE: |
| 404 | case POP_JUMP_IF_TRUE: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | case JUMP_FORWARD: |
| 406 | case JUMP_ABSOLUTE: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 407 | h = GETJUMPTGT(codestr, i); |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 408 | tgt = find_op(codestr, codelen, h); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | /* Replace JUMP_* to a RETURN into just a RETURN */ |
| 410 | if (UNCONDITIONAL_JUMP(opcode) && |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 411 | _Py_OPCODE(codestr[tgt]) == RETURN_VALUE) { |
| 412 | codestr[op_start] = PACKOPARG(RETURN_VALUE, 0); |
| 413 | fill_nops(codestr, op_start + 1, i + 1); |
| 414 | } else if (UNCONDITIONAL_JUMP(_Py_OPCODE(codestr[tgt]))) { |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 415 | size_t arg = GETJUMPTGT(codestr, tgt); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 416 | if (opcode == JUMP_FORWARD) { /* JMP_ABS can go backwards */ |
| 417 | opcode = JUMP_ABSOLUTE; |
| 418 | } else if (!ABSOLUTE_JUMP(opcode)) { |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 419 | if (arg < (size_t)(i + 1)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 420 | break; /* No backward relative jumps */ |
| 421 | } |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 422 | arg -= i + 1; /* Calc relative jump addr */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 423 | } |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 424 | /* cannot overflow: codelen <= INT_MAX */ |
| 425 | assert(arg <= (UINT_MAX / sizeof(_Py_CODEUNIT))); |
| 426 | arg *= sizeof(_Py_CODEUNIT); |
| 427 | copy_op_arg(codestr, op_start, opcode, |
| 428 | (unsigned int)arg, i + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 431 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 432 | /* Remove unreachable ops after RETURN */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | case RETURN_VALUE: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 434 | h = i + 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 435 | /* END_FINALLY should be kept since it denotes the end of |
| 436 | the 'finally' block in frame_setlineno() in frameobject.c. |
| 437 | SETUP_FINALLY should be kept for balancing. |
| 438 | */ |
| 439 | while (h < codelen && ISBASICBLOCK(blocks, i, h) && |
| 440 | _Py_OPCODE(codestr[h]) != END_FINALLY) |
| 441 | { |
| 442 | if (_Py_OPCODE(codestr[h]) == SETUP_FINALLY) { |
| 443 | while (h > i + 1 && |
| 444 | _Py_OPCODE(codestr[h - 1]) == EXTENDED_ARG) |
| 445 | { |
| 446 | h--; |
| 447 | } |
| 448 | break; |
| 449 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 450 | h++; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 451 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 452 | if (h > i + 1) { |
Serhiy Storchaka | 7db3c48 | 2016-10-25 09:30:43 +0300 | [diff] [blame] | 453 | fill_nops(codestr, i + 1, h); |
Gregory P. Smith | 49fa4a9 | 2018-11-08 17:55:07 -0800 | [diff] [blame] | 454 | nexti = find_op(codestr, codelen, h); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 455 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 459 | |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 460 | /* Fixup lnotab */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 461 | for (i = 0, nops = 0; i < codelen; i++) { |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 462 | size_t block = (size_t)i - nops; |
| 463 | /* cannot overflow: codelen <= INT_MAX */ |
| 464 | assert(block <= UINT_MAX); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 465 | /* original code offset => new code offset */ |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 466 | blocks[i] = (unsigned int)block; |
| 467 | if (_Py_OPCODE(codestr[i]) == NOP) { |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 468 | nops++; |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 469 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 471 | cum_orig_offset = 0; |
| 472 | last_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | for (i=0 ; i < tabsiz ; i+=2) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 474 | unsigned int offset_delta, new_offset; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 475 | cum_orig_offset += lnotab[i]; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 476 | assert(cum_orig_offset % sizeof(_Py_CODEUNIT) == 0); |
| 477 | new_offset = blocks[cum_orig_offset / sizeof(_Py_CODEUNIT)] * |
| 478 | sizeof(_Py_CODEUNIT); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 479 | offset_delta = new_offset - last_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 480 | assert(offset_delta <= 255); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 481 | lnotab[i] = (unsigned char)offset_delta; |
| 482 | last_offset = new_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | /* Remove NOPs and fixup jump targets */ |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 486 | for (op_start = i = h = 0; i < codelen; i++, op_start = i) { |
| 487 | j = _Py_OPARG(codestr[i]); |
| 488 | while (_Py_OPCODE(codestr[i]) == EXTENDED_ARG) { |
| 489 | i++; |
| 490 | j = j<<8 | _Py_OPARG(codestr[i]); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 491 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 492 | opcode = _Py_OPCODE(codestr[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | switch (opcode) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 494 | case NOP:continue; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | case JUMP_ABSOLUTE: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | case POP_JUMP_IF_FALSE: |
| 498 | case POP_JUMP_IF_TRUE: |
| 499 | case JUMP_IF_FALSE_OR_POP: |
| 500 | case JUMP_IF_TRUE_OR_POP: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 501 | j = blocks[j / sizeof(_Py_CODEUNIT)] * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | break; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | case FOR_ITER: |
| 505 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | case SETUP_FINALLY: |
| 507 | case SETUP_WITH: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 508 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 509 | case CALL_FINALLY: |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 510 | j = blocks[j / sizeof(_Py_CODEUNIT) + i + 1] - blocks[i] - 1; |
| 511 | j *= sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | break; |
| 513 | } |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 514 | Py_ssize_t ilen = i - op_start + 1; |
| 515 | if (instrsize(j) > ilen) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 516 | goto exitUnchanged; |
Victor Stinner | 028f0ef | 2018-12-07 17:54:18 +0100 | [diff] [blame] | 517 | } |
| 518 | assert(ilen <= INT_MAX); |
| 519 | /* If instrsize(j) < ilen, we'll emit EXTENDED_ARG 0 */ |
| 520 | write_op_arg(codestr + h, opcode, j, (int)ilen); |
| 521 | h += ilen; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 523 | assert(h + (Py_ssize_t)nops == codelen); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | PyMem_Free(blocks); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 526 | code = PyBytes_FromStringAndSize((char *)codestr, h * sizeof(_Py_CODEUNIT)); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 527 | PyMem_Free(codestr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | return code; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 529 | |
Alexandre Vassalotti | 6f82818 | 2009-07-21 02:51:58 +0000 | [diff] [blame] | 530 | exitError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | code = NULL; |
Alexandre Vassalotti | 6f82818 | 2009-07-21 02:51:58 +0000 | [diff] [blame] | 532 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 533 | exitUnchanged: |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 534 | Py_XINCREF(code); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 535 | PyMem_Free(blocks); |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 536 | PyMem_Free(codestr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | return code; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 538 | } |