You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

678 lines
23 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /* Peephole optimizations for bytecode compiler. */
  2. #include "Python.h"
  3. #include "Python-ast.h"
  4. #include "node.h"
  5. #include "pyarena.h"
  6. #include "ast.h"
  7. #include "code.h"
  8. #include "compile.h"
  9. #include "symtable.h"
  10. #include "opcode.h"
  11. #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
  12. #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
  13. #define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
  14. || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
  15. #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \
  16. || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
  17. || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
  18. #define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP)
  19. #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
  20. #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
  21. #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
  22. #define ISBASICBLOCK(blocks, start, bytes) \
  23. (blocks[start]==blocks[start+bytes-1])
  24. /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
  25. with LOAD_CONST (c1, c2, ... cn).
  26. The consts table must still be in list form so that the
  27. new constant (c1, c2, ... cn) can be appended.
  28. Called with codestr pointing to the first LOAD_CONST.
  29. Bails out with no change if one or more of the LOAD_CONSTs is missing.
  30. Also works for BUILD_LIST when followed by an "in" or "not in" test.
  31. */
  32. static int
  33. tuple_of_constants(unsigned char *codestr, Py_ssize_t n, PyObject *consts)
  34. {
  35. PyObject *newconst, *constant;
  36. Py_ssize_t i, arg, len_consts;
  37. /* Pre-conditions */
  38. assert(PyList_CheckExact(consts));
  39. assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
  40. assert(GETARG(codestr, (n*3)) == n);
  41. for (i=0 ; i<n ; i++)
  42. assert(codestr[i*3] == LOAD_CONST);
  43. /* Buildup new tuple of constants */
  44. newconst = PyTuple_New(n);
  45. if (newconst == NULL)
  46. return 0;
  47. len_consts = PyList_GET_SIZE(consts);
  48. for (i=0 ; i<n ; i++) {
  49. arg = GETARG(codestr, (i*3));
  50. assert(arg < len_consts);
  51. constant = PyList_GET_ITEM(consts, arg);
  52. Py_INCREF(constant);
  53. PyTuple_SET_ITEM(newconst, i, constant);
  54. }
  55. /* Append folded constant onto consts */
  56. if (PyList_Append(consts, newconst)) {
  57. Py_DECREF(newconst);
  58. return 0;
  59. }
  60. Py_DECREF(newconst);
  61. /* Write NOPs over old LOAD_CONSTS and
  62. add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
  63. memset(codestr, NOP, n*3);
  64. codestr[n*3] = LOAD_CONST;
  65. SETARG(codestr, (n*3), len_consts);
  66. return 1;
  67. }
  68. /* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
  69. with LOAD_CONST binop(c1,c2)
  70. The consts table must still be in list form so that the
  71. new constant can be appended.
  72. Called with codestr pointing to the first LOAD_CONST.
  73. Abandons the transformation if the folding fails (i.e. 1+'a').
  74. If the new constant is a sequence, only folds when the size
  75. is below a threshold value. That keeps pyc files from
  76. becoming large in the presence of code like: (None,)*1000.
  77. */
  78. static int
  79. fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
  80. {
  81. PyObject *newconst, *v, *w;
  82. Py_ssize_t len_consts, size;
  83. int opcode;
  84. /* Pre-conditions */
  85. assert(PyList_CheckExact(consts));
  86. assert(codestr[0] == LOAD_CONST);
  87. assert(codestr[3] == LOAD_CONST);
  88. /* Create new constant */
  89. v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
  90. w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
  91. opcode = codestr[6];
  92. switch (opcode) {
  93. case BINARY_POWER:
  94. newconst = PyNumber_Power(v, w, Py_None);
  95. break;
  96. case BINARY_MULTIPLY:
  97. newconst = PyNumber_Multiply(v, w);
  98. break;
  99. case BINARY_DIVIDE:
  100. /* Cannot fold this operation statically since
  101. the result can depend on the run-time presence
  102. of the -Qnew flag */
  103. return 0;
  104. case BINARY_TRUE_DIVIDE:
  105. newconst = PyNumber_TrueDivide(v, w);
  106. break;
  107. case BINARY_FLOOR_DIVIDE:
  108. newconst = PyNumber_FloorDivide(v, w);
  109. break;
  110. case BINARY_MODULO:
  111. newconst = PyNumber_Remainder(v, w);
  112. break;
  113. case BINARY_ADD:
  114. newconst = PyNumber_Add(v, w);
  115. break;
  116. case BINARY_SUBTRACT:
  117. newconst = PyNumber_Subtract(v, w);
  118. break;
  119. case BINARY_SUBSCR:
  120. newconst = PyObject_GetItem(v, w);
  121. /* #5057: if v is unicode, there might be differences between
  122. wide and narrow builds in cases like u'\U00012345'[0].
  123. Wide builds will return a non-BMP char, whereas narrow builds
  124. will return a surrogate. In both the cases skip the
  125. optimization in order to produce compatible pycs.
  126. */
  127. #ifdef Py_USING_UNICODE
  128. if (newconst != NULL &&
  129. PyUnicode_Check(v) && PyUnicode_Check(newconst)) {
  130. Py_UNICODE ch = PyUnicode_AS_UNICODE(newconst)[0];
  131. #ifdef Py_UNICODE_WIDE
  132. if (ch > 0xFFFF) {
  133. #else
  134. if (ch >= 0xD800 && ch <= 0xDFFF) {
  135. #endif
  136. Py_DECREF(newconst);
  137. return 0;
  138. }
  139. }
  140. #endif
  141. break;
  142. case BINARY_LSHIFT:
  143. newconst = PyNumber_Lshift(v, w);
  144. break;
  145. case BINARY_RSHIFT:
  146. newconst = PyNumber_Rshift(v, w);
  147. break;
  148. case BINARY_AND:
  149. newconst = PyNumber_And(v, w);
  150. break;
  151. case BINARY_XOR:
  152. newconst = PyNumber_Xor(v, w);
  153. break;
  154. case BINARY_OR:
  155. newconst = PyNumber_Or(v, w);
  156. break;
  157. default:
  158. /* Called with an unknown opcode */
  159. PyErr_Format(PyExc_SystemError,
  160. "unexpected binary operation %d on a constant",
  161. opcode);
  162. return 0;
  163. }
  164. if (newconst == NULL) {
  165. PyErr_Clear();
  166. return 0;
  167. }
  168. size = PyObject_Size(newconst);
  169. if (size == -1)
  170. PyErr_Clear();
  171. else if (size > 20) {
  172. Py_DECREF(newconst);
  173. return 0;
  174. }
  175. /* Append folded constant into consts table */
  176. len_consts = PyList_GET_SIZE(consts);
  177. if (PyList_Append(consts, newconst)) {
  178. Py_DECREF(newconst);
  179. return 0;
  180. }
  181. Py_DECREF(newconst);
  182. /* Write NOP NOP NOP NOP LOAD_CONST newconst */
  183. memset(codestr, NOP, 4);
  184. codestr[4] = LOAD_CONST;
  185. SETARG(codestr, 4, len_consts);
  186. return 1;
  187. }
  188. static int
  189. fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
  190. {
  191. PyObject *newconst=NULL, *v;
  192. Py_ssize_t len_consts;
  193. int opcode;
  194. /* Pre-conditions */
  195. assert(PyList_CheckExact(consts));
  196. assert(codestr[0] == LOAD_CONST);
  197. /* Create new constant */
  198. v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
  199. opcode = codestr[3];
  200. switch (opcode) {
  201. case UNARY_NEGATIVE:
  202. /* Preserve the sign of -0.0 */
  203. if (PyObject_IsTrue(v) == 1)
  204. newconst = PyNumber_Negative(v);
  205. break;
  206. case UNARY_CONVERT:
  207. newconst = PyObject_Repr(v);
  208. break;
  209. case UNARY_INVERT:
  210. newconst = PyNumber_Invert(v);
  211. break;
  212. default:
  213. /* Called with an unknown opcode */
  214. PyErr_Format(PyExc_SystemError,
  215. "unexpected unary operation %d on a constant",
  216. opcode);
  217. return 0;
  218. }
  219. if (newconst == NULL) {
  220. PyErr_Clear();
  221. return 0;
  222. }
  223. /* Append folded constant into consts table */
  224. len_consts = PyList_GET_SIZE(consts);
  225. if (PyList_Append(consts, newconst)) {
  226. Py_DECREF(newconst);
  227. return 0;
  228. }
  229. Py_DECREF(newconst);
  230. /* Write NOP LOAD_CONST newconst */
  231. codestr[0] = NOP;
  232. codestr[1] = LOAD_CONST;
  233. SETARG(codestr, 1, len_consts);
  234. return 1;
  235. }
  236. static unsigned int *
  237. markblocks(unsigned char *code, Py_ssize_t len)
  238. {
  239. unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
  240. int i,j, opcode, blockcnt = 0;
  241. if (blocks == NULL) {
  242. PyErr_NoMemory();
  243. return NULL;
  244. }
  245. memset(blocks, 0, len*sizeof(int));
  246. /* Mark labels in the first pass */
  247. for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
  248. opcode = code[i];
  249. switch (opcode) {
  250. case FOR_ITER:
  251. case JUMP_FORWARD:
  252. case JUMP_IF_FALSE_OR_POP:
  253. case JUMP_IF_TRUE_OR_POP:
  254. case POP_JUMP_IF_FALSE:
  255. case POP_JUMP_IF_TRUE:
  256. case JUMP_ABSOLUTE:
  257. case CONTINUE_LOOP:
  258. case SETUP_LOOP:
  259. case SETUP_EXCEPT:
  260. case SETUP_FINALLY:
  261. case SETUP_WITH:
  262. j = GETJUMPTGT(code, i);
  263. blocks[j] = 1;
  264. break;
  265. }
  266. }
  267. /* Build block numbers in the second pass */
  268. for (i=0 ; i<len ; i++) {
  269. blockcnt += blocks[i]; /* increment blockcnt over labels */
  270. blocks[i] = blockcnt;
  271. }
  272. return blocks;
  273. }
  274. /* Perform basic peephole optimizations to components of a code object.
  275. The consts object should still be in list form to allow new constants
  276. to be appended.
  277. To keep the optimizer simple, it bails out (does nothing) for code
  278. containing extended arguments or that has a length over 32,700. That
  279. allows us to avoid overflow and sign issues. Likewise, it bails when
  280. the lineno table has complex encoding for gaps >= 255.
  281. Optimizations are restricted to simple transformations occuring within a
  282. single basic block. All transformations keep the code size the same or
  283. smaller. For those that reduce size, the gaps are initially filled with
  284. NOPs. Later those NOPs are removed and the jump addresses retargeted in
  285. a single pass. Line numbering is adjusted accordingly. */
  286. PyObject *
  287. PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
  288. PyObject *lineno_obj)
  289. {
  290. Py_ssize_t i, j, codelen;
  291. int nops, h, adj;
  292. int tgt, tgttgt, opcode;
  293. unsigned char *codestr = NULL;
  294. unsigned char *lineno;
  295. int *addrmap = NULL;
  296. int new_line, cum_orig_line, last_line, tabsiz;
  297. int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */
  298. unsigned int *blocks = NULL;
  299. char *name;
  300. /* Bail out if an exception is set */
  301. if (PyErr_Occurred())
  302. goto exitError;
  303. /* Bypass optimization when the lineno table is too complex */
  304. assert(PyString_Check(lineno_obj));
  305. lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
  306. tabsiz = PyString_GET_SIZE(lineno_obj);
  307. if (memchr(lineno, 255, tabsiz) != NULL)
  308. goto exitUnchanged;
  309. /* Avoid situations where jump retargeting could overflow */
  310. assert(PyString_Check(code));
  311. codelen = PyString_GET_SIZE(code);
  312. if (codelen > 32700)
  313. goto exitUnchanged;
  314. /* Make a modifiable copy of the code string */
  315. codestr = (unsigned char *)PyMem_Malloc(codelen);
  316. if (codestr == NULL)
  317. goto exitError;
  318. codestr = (unsigned char *)memcpy(codestr,
  319. PyString_AS_STRING(code), codelen);
  320. /* Verify that RETURN_VALUE terminates the codestring. This allows
  321. the various transformation patterns to look ahead several
  322. instructions without additional checks to make sure they are not
  323. looking beyond the end of the code string.
  324. */
  325. if (codestr[codelen-1] != RETURN_VALUE)
  326. goto exitUnchanged;
  327. /* Mapping to new jump targets after NOPs are removed */
  328. addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
  329. if (addrmap == NULL)
  330. goto exitError;
  331. blocks = markblocks(codestr, codelen);
  332. if (blocks == NULL)
  333. goto exitError;
  334. assert(PyList_Check(consts));
  335. for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
  336. reoptimize_current:
  337. opcode = codestr[i];
  338. lastlc = cumlc;
  339. cumlc = 0;
  340. switch (opcode) {
  341. /* Replace UNARY_NOT POP_JUMP_IF_FALSE
  342. with POP_JUMP_IF_TRUE */
  343. case UNARY_NOT:
  344. if (codestr[i+1] != POP_JUMP_IF_FALSE
  345. || !ISBASICBLOCK(blocks,i,4))
  346. continue;
  347. j = GETARG(codestr, i+1);
  348. codestr[i] = POP_JUMP_IF_TRUE;
  349. SETARG(codestr, i, j);
  350. codestr[i+3] = NOP;
  351. goto reoptimize_current;
  352. /* not a is b --> a is not b
  353. not a in b --> a not in b
  354. not a is not b --> a is b
  355. not a not in b --> a in b
  356. */
  357. case COMPARE_OP:
  358. j = GETARG(codestr, i);
  359. if (j < 6 || j > 9 ||
  360. codestr[i+3] != UNARY_NOT ||
  361. !ISBASICBLOCK(blocks,i,4))
  362. continue;
  363. SETARG(codestr, i, (j^1));
  364. codestr[i+3] = NOP;
  365. break;
  366. /* Replace LOAD_GLOBAL/LOAD_NAME None
  367. with LOAD_CONST None */
  368. case LOAD_NAME:
  369. case LOAD_GLOBAL:
  370. j = GETARG(codestr, i);
  371. name = PyString_AsString(PyTuple_GET_ITEM(names, j));
  372. if (name == NULL || strcmp(name, "None") != 0)
  373. continue;
  374. for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
  375. if (PyList_GET_ITEM(consts, j) == Py_None)
  376. break;
  377. }
  378. if (j == PyList_GET_SIZE(consts)) {
  379. if (PyList_Append(consts, Py_None) == -1)
  380. goto exitError;
  381. }
  382. assert(PyList_GET_ITEM(consts, j) == Py_None);
  383. codestr[i] = LOAD_CONST;
  384. SETARG(codestr, i, j);
  385. cumlc = lastlc + 1;
  386. break;
  387. /* Skip over LOAD_CONST trueconst
  388. POP_JUMP_IF_FALSE xx. This improves
  389. "while 1" performance. */
  390. case LOAD_CONST:
  391. cumlc = lastlc + 1;
  392. j = GETARG(codestr, i);
  393. if (codestr[i+3] != POP_JUMP_IF_FALSE ||
  394. !ISBASICBLOCK(blocks,i,6) ||
  395. !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
  396. continue;
  397. memset(codestr+i, NOP, 6);
  398. cumlc = 0;
  399. break;
  400. /* Try to fold tuples of constants (includes a case for lists
  401. which are only used for "in" and "not in" tests).
  402. Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
  403. Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
  404. Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
  405. case BUILD_TUPLE:
  406. case BUILD_LIST:
  407. j = GETARG(codestr, i);
  408. h = i - 3 * j;
  409. if (h >= 0 &&
  410. j <= lastlc &&
  411. ((opcode == BUILD_TUPLE &&
  412. ISBASICBLOCK(blocks, h, 3*(j+1))) ||
  413. (opcode == BUILD_LIST &&
  414. codestr[i+3]==COMPARE_OP &&
  415. ISBASICBLOCK(blocks, h, 3*(j+2)) &&
  416. (GETARG(codestr,i+3)==6 ||
  417. GETARG(codestr,i+3)==7))) &&
  418. tuple_of_constants(&codestr[h], j, consts)) {
  419. assert(codestr[i] == LOAD_CONST);
  420. cumlc = 1;
  421. break;
  422. }
  423. if (codestr[i+3] != UNPACK_SEQUENCE ||
  424. !ISBASICBLOCK(blocks,i,6) ||
  425. j != GETARG(codestr, i+3))
  426. continue;
  427. if (j == 1) {
  428. memset(codestr+i, NOP, 6);
  429. } else if (j == 2) {
  430. codestr[i] = ROT_TWO;
  431. memset(codestr+i+1, NOP, 5);
  432. } else if (j == 3) {
  433. codestr[i] = ROT_THREE;
  434. codestr[i+1] = ROT_TWO;
  435. memset(codestr+i+2, NOP, 4);
  436. }
  437. break;
  438. /* Fold binary ops on constants.
  439. LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
  440. case BINARY_POWER:
  441. case BINARY_MULTIPLY:
  442. case BINARY_TRUE_DIVIDE:
  443. case BINARY_FLOOR_DIVIDE:
  444. case BINARY_MODULO:
  445. case BINARY_ADD:
  446. case BINARY_SUBTRACT:
  447. case BINARY_SUBSCR:
  448. case BINARY_LSHIFT:
  449. case BINARY_RSHIFT:
  450. case BINARY_AND:
  451. case BINARY_XOR:
  452. case BINARY_OR:
  453. if (lastlc >= 2 &&
  454. ISBASICBLOCK(blocks, i-6, 7) &&
  455. fold_binops_on_constants(&codestr[i-6], consts)) {
  456. i -= 2;
  457. assert(codestr[i] == LOAD_CONST);
  458. cumlc = 1;
  459. }
  460. break;
  461. /* Fold unary ops on constants.
  462. LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
  463. case UNARY_NEGATIVE:
  464. case UNARY_CONVERT:
  465. case UNARY_INVERT:
  466. if (lastlc >= 1 &&
  467. ISBASICBLOCK(blocks, i-3, 4) &&
  468. fold_unaryops_on_constants(&codestr[i-3], consts)) {
  469. i -= 2;
  470. assert(codestr[i] == LOAD_CONST);
  471. cumlc = 1;
  472. }
  473. break;
  474. /* Simplify conditional jump to conditional jump where the
  475. result of the first test implies the success of a similar
  476. test or the failure of the opposite test.
  477. Arises in code like:
  478. "if a and b:"
  479. "if a or b:"
  480. "a and b or c"
  481. "(a and b) and c"
  482. x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
  483. --> x:JUMP_IF_FALSE_OR_POP z
  484. x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
  485. --> x:POP_JUMP_IF_FALSE y+3
  486. where y+3 is the instruction following the second test.
  487. */
  488. case JUMP_IF_FALSE_OR_POP:
  489. case JUMP_IF_TRUE_OR_POP:
  490. tgt = GETJUMPTGT(codestr, i);
  491. j = codestr[tgt];
  492. if (CONDITIONAL_JUMP(j)) {
  493. /* NOTE: all possible jumps here are absolute! */
  494. if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) {
  495. /* The second jump will be
  496. taken iff the first is. */
  497. tgttgt = GETJUMPTGT(codestr, tgt);
  498. /* The current opcode inherits
  499. its target's stack behaviour */
  500. codestr[i] = j;
  501. SETARG(codestr, i, tgttgt);
  502. goto reoptimize_current;
  503. } else {
  504. /* The second jump is not taken if the first is (so
  505. jump past it), and all conditional jumps pop their
  506. argument when they're not taken (so change the
  507. first jump to pop its argument when it's taken). */
  508. if (JUMPS_ON_TRUE(opcode))
  509. codestr[i] = POP_JUMP_IF_TRUE;
  510. else
  511. codestr[i] = POP_JUMP_IF_FALSE;
  512. SETARG(codestr, i, (tgt + 3));
  513. goto reoptimize_current;
  514. }
  515. }
  516. /* Intentional fallthrough */
  517. /* Replace jumps to unconditional jumps */
  518. case POP_JUMP_IF_FALSE:
  519. case POP_JUMP_IF_TRUE:
  520. case FOR_ITER:
  521. case JUMP_FORWARD:
  522. case JUMP_ABSOLUTE:
  523. case CONTINUE_LOOP:
  524. case SETUP_LOOP:
  525. case SETUP_EXCEPT:
  526. case SETUP_FINALLY:
  527. case SETUP_WITH:
  528. tgt = GETJUMPTGT(codestr, i);
  529. /* Replace JUMP_* to a RETURN into just a RETURN */
  530. if (UNCONDITIONAL_JUMP(opcode) &&
  531. codestr[tgt] == RETURN_VALUE) {
  532. codestr[i] = RETURN_VALUE;
  533. memset(codestr+i+1, NOP, 2);
  534. continue;
  535. }
  536. if (!UNCONDITIONAL_JUMP(codestr[tgt]))
  537. continue;
  538. tgttgt = GETJUMPTGT(codestr, tgt);
  539. if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
  540. opcode = JUMP_ABSOLUTE;
  541. if (!ABSOLUTE_JUMP(opcode))
  542. tgttgt -= i + 3; /* Calc relative jump addr */
  543. if (tgttgt < 0) /* No backward relative jumps */
  544. continue;
  545. codestr[i] = opcode;
  546. SETARG(codestr, i, tgttgt);
  547. break;
  548. case EXTENDED_ARG:
  549. goto exitUnchanged;
  550. /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
  551. /* Remove unreachable JUMPs after RETURN */
  552. case RETURN_VALUE:
  553. if (i+4 >= codelen)
  554. continue;
  555. if (codestr[i+4] == RETURN_VALUE &&
  556. ISBASICBLOCK(blocks,i,5))
  557. memset(codestr+i+1, NOP, 4);
  558. else if (UNCONDITIONAL_JUMP(codestr[i+1]) &&
  559. ISBASICBLOCK(blocks,i,4))
  560. memset(codestr+i+1, NOP, 3);
  561. break;
  562. }
  563. }
  564. /* Fixup linenotab */
  565. for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
  566. addrmap[i] = i - nops;
  567. if (codestr[i] == NOP)
  568. nops++;
  569. }
  570. cum_orig_line = 0;
  571. last_line = 0;
  572. for (i=0 ; i < tabsiz ; i+=2) {
  573. cum_orig_line += lineno[i];
  574. new_line = addrmap[cum_orig_line];
  575. assert (new_line - last_line < 255);
  576. lineno[i] =((unsigned char)(new_line - last_line));
  577. last_line = new_line;
  578. }
  579. /* Remove NOPs and fixup jump targets */
  580. for (i=0, h=0 ; i<codelen ; ) {
  581. opcode = codestr[i];
  582. switch (opcode) {
  583. case NOP:
  584. i++;
  585. continue;
  586. case JUMP_ABSOLUTE:
  587. case CONTINUE_LOOP:
  588. case POP_JUMP_IF_FALSE:
  589. case POP_JUMP_IF_TRUE:
  590. case JUMP_IF_FALSE_OR_POP:
  591. case JUMP_IF_TRUE_OR_POP:
  592. j = addrmap[GETARG(codestr, i)];
  593. SETARG(codestr, i, j);
  594. break;
  595. case FOR_ITER:
  596. case JUMP_FORWARD:
  597. case SETUP_LOOP:
  598. case SETUP_EXCEPT:
  599. case SETUP_FINALLY:
  600. case SETUP_WITH:
  601. j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
  602. SETARG(codestr, i, j);
  603. break;
  604. }
  605. adj = CODESIZE(opcode);
  606. while (adj--)
  607. codestr[h++] = codestr[i++];
  608. }
  609. assert(h + nops == codelen);
  610. code = PyString_FromStringAndSize((char *)codestr, h);
  611. PyMem_Free(addrmap);
  612. PyMem_Free(codestr);
  613. PyMem_Free(blocks);
  614. return code;
  615. exitError:
  616. code = NULL;
  617. exitUnchanged:
  618. if (blocks != NULL)
  619. PyMem_Free(blocks);
  620. if (addrmap != NULL)
  621. PyMem_Free(addrmap);
  622. if (codestr != NULL)
  623. PyMem_Free(codestr);
  624. Py_XINCREF(code);
  625. return code;
  626. }