| OLD | NEW |
| 1 /* Peephole optimizations for bytecode compiler. */ | 1 /* Peephole optimizations for bytecode compiler. */ |
| 2 | 2 |
| 3 #include "Python.h" | 3 #include "Python.h" |
| 4 | 4 |
| 5 #include "Python-ast.h" | 5 #include "Python-ast.h" |
| 6 #include "node.h" | 6 #include "node.h" |
| 7 #include "pyarena.h" | 7 #include "pyarena.h" |
| 8 #include "ast.h" | 8 #include "ast.h" |
| 9 #include "code.h" | 9 #include "code.h" |
| 10 #include "compile.h" | 10 #include "compile.h" |
| 11 #include "symtable.h" | 11 #include "symtable.h" |
| 12 #include "opcode.h" | 12 #include "opcode.h" |
| 13 | 13 |
| 14 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) | 14 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) |
| 15 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) | 15 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) |
| 16 #define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ | 16 #define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ |
| 17 || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) | 17 || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) |
| 18 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \ | 18 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \ |
| 19 || op==FOR_ITER \ |
| 19 || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ | 20 || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \ |
| 20 || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) | 21 || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) |
| 21 #define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP) | 22 #define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP) |
| 22 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) | 23 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) |
| 23 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 | 24 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 |
| 24 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) | 25 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) |
| 25 #define ISBASICBLOCK(blocks, start, bytes) \ | 26 #define ISBASICBLOCK(blocks, start, bytes) \ |
| 26 (blocks[start]==blocks[start+bytes-1]) | 27 (blocks[start]==blocks[start+bytes-1]) |
| 27 | 28 |
| 28 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n | 29 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 | 608 |
| 608 /* Remove NOPs and fixup jump targets */ | 609 /* Remove NOPs and fixup jump targets */ |
| 609 for (i=0, h=0 ; i<codelen ; ) { | 610 for (i=0, h=0 ; i<codelen ; ) { |
| 610 opcode = codestr[i]; | 611 opcode = codestr[i]; |
| 611 switch (opcode) { | 612 switch (opcode) { |
| 612 case NOP: | 613 case NOP: |
| 613 i++; | 614 i++; |
| 614 continue; | 615 continue; |
| 615 | 616 |
| 616 case JUMP_ABSOLUTE: | 617 case JUMP_ABSOLUTE: |
| 617 case CONTINUE_LOOP: | |
| 618 case POP_JUMP_IF_FALSE: | 618 case POP_JUMP_IF_FALSE: |
| 619 case POP_JUMP_IF_TRUE: | 619 case POP_JUMP_IF_TRUE: |
| 620 case JUMP_IF_FALSE_OR_POP: | 620 case JUMP_IF_FALSE_OR_POP: |
| 621 case JUMP_IF_TRUE_OR_POP: | 621 case JUMP_IF_TRUE_OR_POP: |
| 622 case CONTINUE_LOOP: |
| 623 case FOR_ITER: |
| 622 j = addrmap[GETARG(codestr, i)]; | 624 j = addrmap[GETARG(codestr, i)]; |
| 623 SETARG(codestr, i, j); | 625 SETARG(codestr, i, j); |
| 624 break; | 626 break; |
| 625 | 627 |
| 626 case FOR_ITER: | |
| 627 case JUMP_FORWARD: | 628 case JUMP_FORWARD: |
| 628 case SETUP_LOOP: | 629 case SETUP_LOOP: |
| 629 case SETUP_EXCEPT: | 630 case SETUP_EXCEPT: |
| 630 case SETUP_FINALLY: | 631 case SETUP_FINALLY: |
| 631 j = addrmap[GETARG(codestr, i) + i + 3] - addrma
p[i] - 3; | 632 j = addrmap[GETARG(codestr, i) + i + 3] - addrma
p[i] - 3; |
| 632 SETARG(codestr, i, j); | 633 SETARG(codestr, i, j); |
| 633 break; | 634 break; |
| 634 } | 635 } |
| 635 adj = CODESIZE(opcode); | 636 adj = CODESIZE(opcode); |
| 636 while (adj--) | 637 while (adj--) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 647 exitUnchanged: | 648 exitUnchanged: |
| 648 if (blocks != NULL) | 649 if (blocks != NULL) |
| 649 PyMem_Free(blocks); | 650 PyMem_Free(blocks); |
| 650 if (addrmap != NULL) | 651 if (addrmap != NULL) |
| 651 PyMem_Free(addrmap); | 652 PyMem_Free(addrmap); |
| 652 if (codestr != NULL) | 653 if (codestr != NULL) |
| 653 PyMem_Free(codestr); | 654 PyMem_Free(codestr); |
| 654 Py_INCREF(code); | 655 Py_INCREF(code); |
| 655 return code; | 656 return code; |
| 656 } | 657 } |
| OLD | NEW |