OLD | NEW |
1 /* | 1 /* |
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. | 2 * This file compiles an abstract syntax tree (AST) into Python bytecode. |
3 * | 3 * |
4 * The primary entry point is PyAST_Compile(), which returns a | 4 * The primary entry point is PyAST_Compile(), which returns a |
5 * PyCodeObject. The compiler makes several passes to build the code | 5 * PyCodeObject. The compiler makes several passes to build the code |
6 * object: | 6 * object: |
7 * 1. Checks for future statements. See future.c | 7 * 1. Checks for future statements. See future.c |
8 * 2. Builds a symbol table. See symtable.c. | 8 * 2. Builds a symbol table. See symtable.c. |
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. | 9 * 3. Generate code for basic blocks. See compiler_mod() in this file. |
10 * 4. Assemble the basic blocks into final code. See assemble() in | 10 * 4. Assemble the basic blocks into final code. See assemble() in |
(...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 case LOAD_ATTR: | 829 case LOAD_ATTR: |
830 return 0; | 830 return 0; |
831 case COMPARE_OP: | 831 case COMPARE_OP: |
832 return -1; | 832 return -1; |
833 case IMPORT_NAME: | 833 case IMPORT_NAME: |
834 return 0; | 834 return 0; |
835 case IMPORT_FROM: | 835 case IMPORT_FROM: |
836 return 1; | 836 return 1; |
837 | 837 |
838 case JUMP_FORWARD: | 838 case JUMP_FORWARD: |
839 » » case JUMP_IF_FALSE: | 839 » » case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */ |
840 » » case JUMP_IF_TRUE: | 840 » » case JUMP_IF_FALSE_OR_POP: /* "" */ |
841 case JUMP_ABSOLUTE: | 841 case JUMP_ABSOLUTE: |
842 return 0; | 842 return 0; |
843 | 843 |
| 844 case POP_JUMP_IF_FALSE: |
| 845 case POP_JUMP_IF_TRUE: |
| 846 return -1; |
| 847 |
844 case LOAD_GLOBAL: | 848 case LOAD_GLOBAL: |
845 return 1; | 849 return 1; |
846 | 850 |
847 case CONTINUE_LOOP: | 851 case CONTINUE_LOOP: |
848 return 0; | 852 return 0; |
849 case SETUP_LOOP: | 853 case SETUP_LOOP: |
850 return 0; | 854 return 0; |
851 case SETUP_EXCEPT: | 855 case SETUP_EXCEPT: |
852 case SETUP_FINALLY: | 856 case SETUP_FINALLY: |
853 return 3; /* actually pushed by an exception */ | 857 return 3; /* actually pushed by an exception */ |
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1492 basicblock *end, *next; | 1496 basicblock *end, *next; |
1493 ········ | 1497 ········ |
1494 assert(e->kind == IfExp_kind); | 1498 assert(e->kind == IfExp_kind); |
1495 end = compiler_new_block(c); | 1499 end = compiler_new_block(c); |
1496 if (end == NULL) | 1500 if (end == NULL) |
1497 return 0; | 1501 return 0; |
1498 next = compiler_new_block(c); | 1502 next = compiler_new_block(c); |
1499 if (next == NULL) | 1503 if (next == NULL) |
1500 return 0; | 1504 return 0; |
1501 VISIT(c, expr, e->v.IfExp.test); | 1505 VISIT(c, expr, e->v.IfExp.test); |
1502 » ADDOP_JREL(c, JUMP_IF_FALSE, next); | 1506 » ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); |
1503 » ADDOP(c, POP_TOP); | |
1504 VISIT(c, expr, e->v.IfExp.body); | 1507 VISIT(c, expr, e->v.IfExp.body); |
1505 ADDOP_JREL(c, JUMP_FORWARD, end); | 1508 ADDOP_JREL(c, JUMP_FORWARD, end); |
1506 compiler_use_next_block(c, next); | 1509 compiler_use_next_block(c, next); |
1507 ADDOP(c, POP_TOP); | |
1508 VISIT(c, expr, e->v.IfExp.orelse); | 1510 VISIT(c, expr, e->v.IfExp.orelse); |
1509 compiler_use_next_block(c, end); | 1511 compiler_use_next_block(c, end); |
1510 return 1; | 1512 return 1; |
1511 } | 1513 } |
1512 | 1514 |
1513 static int | 1515 static int |
1514 compiler_lambda(struct compiler *c, expr_ty e) | 1516 compiler_lambda(struct compiler *c, expr_ty e) |
1515 { | 1517 { |
1516 PyCodeObject *co; | 1518 PyCodeObject *co; |
1517 static identifier name; | 1519 static identifier name; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1590 | 1592 |
1591 static int | 1593 static int |
1592 compiler_if(struct compiler *c, stmt_ty s) | 1594 compiler_if(struct compiler *c, stmt_ty s) |
1593 { | 1595 { |
1594 basicblock *end, *next; | 1596 basicblock *end, *next; |
1595 int constant; | 1597 int constant; |
1596 assert(s->kind == If_kind); | 1598 assert(s->kind == If_kind); |
1597 end = compiler_new_block(c); | 1599 end = compiler_new_block(c); |
1598 if (end == NULL) | 1600 if (end == NULL) |
1599 return 0; | 1601 return 0; |
1600 next = compiler_new_block(c); | |
1601 if (next == NULL) | |
1602 return 0; | |
1603 ········ | 1602 ········ |
1604 constant = expr_constant(s->v.If.test); | 1603 constant = expr_constant(s->v.If.test); |
1605 /* constant = 0: "if 0" | 1604 /* constant = 0: "if 0" |
1606 * constant = 1: "if 1", "if 2", ... | 1605 * constant = 1: "if 1", "if 2", ... |
1607 * constant = -1: rest */ | 1606 * constant = -1: rest */ |
1608 if (constant == 0) { | 1607 if (constant == 0) { |
1609 if (s->v.If.orelse) | 1608 if (s->v.If.orelse) |
1610 VISIT_SEQ(c, stmt, s->v.If.orelse); | 1609 VISIT_SEQ(c, stmt, s->v.If.orelse); |
1611 } else if (constant == 1) { | 1610 } else if (constant == 1) { |
1612 VISIT_SEQ(c, stmt, s->v.If.body); | 1611 VISIT_SEQ(c, stmt, s->v.If.body); |
1613 } else { | 1612 } else { |
| 1613 if (s->v.If.orelse) { |
| 1614 next = compiler_new_block(c); |
| 1615 if (next == NULL) |
| 1616 return 0; |
| 1617 } |
| 1618 else |
| 1619 next = end; |
1614 VISIT(c, expr, s->v.If.test); | 1620 VISIT(c, expr, s->v.If.test); |
1615 » » ADDOP_JREL(c, JUMP_IF_FALSE, next); | 1621 » » ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); |
1616 » » ADDOP(c, POP_TOP); | |
1617 VISIT_SEQ(c, stmt, s->v.If.body); | 1622 VISIT_SEQ(c, stmt, s->v.If.body); |
1618 ADDOP_JREL(c, JUMP_FORWARD, end); | 1623 ADDOP_JREL(c, JUMP_FORWARD, end); |
1619 » » compiler_use_next_block(c, next); | 1624 » » if (s->v.If.orelse) { |
1620 » » ADDOP(c, POP_TOP); | 1625 » » » compiler_use_next_block(c, next); |
1621 » » if (s->v.If.orelse) | |
1622 VISIT_SEQ(c, stmt, s->v.If.orelse); | 1626 VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 1627 } |
1623 } | 1628 } |
1624 compiler_use_next_block(c, end); | 1629 compiler_use_next_block(c, end); |
1625 return 1; | 1630 return 1; |
1626 } | 1631 } |
1627 | 1632 |
1628 static int | 1633 static int |
1629 compiler_for(struct compiler *c, stmt_ty s) | 1634 compiler_for(struct compiler *c, stmt_ty s) |
1630 { | 1635 { |
1631 basicblock *start, *cleanup, *end; | 1636 basicblock *start, *cleanup, *end; |
1632 | 1637 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1686 | 1691 |
1687 ADDOP_JREL(c, SETUP_LOOP, end); | 1692 ADDOP_JREL(c, SETUP_LOOP, end); |
1688 compiler_use_next_block(c, loop); | 1693 compiler_use_next_block(c, loop); |
1689 if (!compiler_push_fblock(c, LOOP, loop)) | 1694 if (!compiler_push_fblock(c, LOOP, loop)) |
1690 return 0; | 1695 return 0; |
1691 if (constant == -1) { | 1696 if (constant == -1) { |
1692 /* while expressions must be traced on each iteration, | 1697 /* while expressions must be traced on each iteration, |
1693 so we need to set an extra line number. */ | 1698 so we need to set an extra line number. */ |
1694 c->u->u_lineno_set = false; | 1699 c->u->u_lineno_set = false; |
1695 VISIT(c, expr, s->v.While.test); | 1700 VISIT(c, expr, s->v.While.test); |
1696 » » ADDOP_JREL(c, JUMP_IF_FALSE, anchor); | 1701 » » ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor); |
1697 » » ADDOP(c, POP_TOP); | |
1698 } | 1702 } |
1699 VISIT_SEQ(c, stmt, s->v.While.body); | 1703 VISIT_SEQ(c, stmt, s->v.While.body); |
1700 ADDOP_JABS(c, JUMP_ABSOLUTE, loop); | 1704 ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
1701 | 1705 |
1702 /* XXX should the two POP instructions be in a separate block | 1706 /* XXX should the two POP instructions be in a separate block |
1703 if there is no else clause ? | 1707 if there is no else clause ? |
1704 */ | 1708 */ |
1705 | 1709 |
1706 if (constant == -1) { | 1710 if (constant == -1) { |
1707 compiler_use_next_block(c, anchor); | 1711 compiler_use_next_block(c, anchor); |
1708 ADDOP(c, POP_TOP); | |
1709 ADDOP(c, POP_BLOCK); | 1712 ADDOP(c, POP_BLOCK); |
1710 } | 1713 } |
1711 compiler_pop_fblock(c, LOOP, loop); | 1714 compiler_pop_fblock(c, LOOP, loop); |
1712 if (orelse != NULL) /* what if orelse is just pass? */ | 1715 if (orelse != NULL) /* what if orelse is just pass? */ |
1713 VISIT_SEQ(c, stmt, s->v.While.orelse); | 1716 VISIT_SEQ(c, stmt, s->v.While.orelse); |
1714 compiler_use_next_block(c, end); | 1717 compiler_use_next_block(c, end); |
1715 | 1718 |
1716 return 1; | 1719 return 1; |
1717 } | 1720 } |
1718 | 1721 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1819 ··· | 1822 ··· |
1820 Value stack Label Instruction Argument | 1823 Value stack Label Instruction Argument |
1821 [] SETUP_EXCEPT L1 | 1824 [] SETUP_EXCEPT L1 |
1822 [] <code for S> | 1825 [] <code for S> |
1823 [] POP_BLOCK | 1826 [] POP_BLOCK |
1824 [] JUMP_FORWARD L0 | 1827 [] JUMP_FORWARD L0 |
1825 ··· | 1828 ··· |
1826 [tb, val, exc] L1: DUP ) | 1829 [tb, val, exc] L1: DUP ) |
1827 [tb, val, exc, exc] <evaluate E1> ) | 1830 [tb, val, exc, exc] <evaluate E1> ) |
1828 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 | 1831 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
1829 [tb, val, exc, 1-or-0]» JUMP_IF_FALSE» L2» » ) | 1832 [tb, val, exc, 1-or-0]» POP_JUMP_IF_FALSE» L2» ) |
1830 [tb, val, exc, 1]» » POP» » » » ) | |
1831 [tb, val, exc] POP | 1833 [tb, val, exc] POP |
1832 [tb, val] <assign to V1> (or POP if no V1) | 1834 [tb, val] <assign to V1> (or POP if no V1) |
1833 [tb] POP | 1835 [tb] POP |
1834 [] <code for S1> | 1836 [] <code for S1> |
1835 JUMP_FORWARD L0 | 1837 JUMP_FORWARD L0 |
1836 ··· | 1838 ··· |
1837 [tb, val, exc, 0]» L2:» POP | 1839 [tb, val, exc]» L2:» DUP |
1838 [tb, val, exc]» » DUP | |
1839 .............................etc....................... | 1840 .............................etc....................... |
1840 | 1841 |
1841 [tb, val, exc, 0]» Ln+1:» POP | 1842 [tb, val, exc]» Ln+1:» END_FINALLY» # re-raise exception |
1842 [tb, val, exc]» » END_FINALLY» # re-raise exception | |
1843 ··· | 1843 ··· |
1844 [] L0: <next statement> | 1844 [] L0: <next statement> |
1845 ··· | 1845 ··· |
1846 Of course, parts are not generated if Vi or Ei is not present. | 1846 Of course, parts are not generated if Vi or Ei is not present. |
1847 */ | 1847 */ |
1848 static int | 1848 static int |
1849 compiler_try_except(struct compiler *c, stmt_ty s) | 1849 compiler_try_except(struct compiler *c, stmt_ty s) |
1850 { | 1850 { |
1851 basicblock *body, *orelse, *except, *end; | 1851 basicblock *body, *orelse, *except, *end; |
1852 int i, n; | 1852 int i, n; |
(...skipping 21 matching lines...) Expand all Loading... |
1874 return compiler_error(c, "default 'except:' must be last"); | 1874 return compiler_error(c, "default 'except:' must be last"); |
1875 c->u->u_lineno_set = false; | 1875 c->u->u_lineno_set = false; |
1876 c->u->u_lineno = handler->lineno; | 1876 c->u->u_lineno = handler->lineno; |
1877 except = compiler_new_block(c); | 1877 except = compiler_new_block(c); |
1878 if (except == NULL) | 1878 if (except == NULL) |
1879 return 0; | 1879 return 0; |
1880 if (handler->v.ExceptHandler.type) { | 1880 if (handler->v.ExceptHandler.type) { |
1881 ADDOP(c, DUP_TOP); | 1881 ADDOP(c, DUP_TOP); |
1882 VISIT(c, expr, handler->v.ExceptHandler.type); | 1882 VISIT(c, expr, handler->v.ExceptHandler.type); |
1883 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); | 1883 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
1884 » » » ADDOP_JREL(c, JUMP_IF_FALSE, except); | 1884 » » » ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
1885 » » » ADDOP(c, POP_TOP); | |
1886 } | 1885 } |
1887 ADDOP(c, POP_TOP); | 1886 ADDOP(c, POP_TOP); |
1888 if (handler->v.ExceptHandler.name) { | 1887 if (handler->v.ExceptHandler.name) { |
1889 VISIT(c, expr, handler->v.ExceptHandler.name); | 1888 VISIT(c, expr, handler->v.ExceptHandler.name); |
1890 } | 1889 } |
1891 else { | 1890 else { |
1892 ADDOP(c, POP_TOP); | 1891 ADDOP(c, POP_TOP); |
1893 } | 1892 } |
1894 ADDOP(c, POP_TOP); | 1893 ADDOP(c, POP_TOP); |
1895 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); | 1894 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
1896 ADDOP_JREL(c, JUMP_FORWARD, end); | 1895 ADDOP_JREL(c, JUMP_FORWARD, end); |
1897 compiler_use_next_block(c, except); | 1896 compiler_use_next_block(c, except); |
1898 if (handler->v.ExceptHandler.type) | |
1899 ADDOP(c, POP_TOP); | |
1900 } | 1897 } |
1901 ADDOP(c, END_FINALLY); | 1898 ADDOP(c, END_FINALLY); |
1902 compiler_use_next_block(c, orelse); | 1899 compiler_use_next_block(c, orelse); |
1903 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); | 1900 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); |
1904 compiler_use_next_block(c, end); | 1901 compiler_use_next_block(c, end); |
1905 return 1; | 1902 return 1; |
1906 } | 1903 } |
1907 | 1904 |
1908 static int | 1905 static int |
1909 compiler_import_as(struct compiler *c, identifier name, identifier asname) | 1906 compiler_import_as(struct compiler *c, identifier name, identifier asname) |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2077 const char* msg = | 2074 const char* msg = |
2078 "assertion is always true, perhaps remove parentheses?"; | 2075 "assertion is always true, perhaps remove parentheses?"; |
2079 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, | 2076 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, |
2080 c->u->u_lineno, NULL, NULL) == -1) | 2077 c->u->u_lineno, NULL, NULL) == -1) |
2081 return 0; | 2078 return 0; |
2082 } | 2079 } |
2083 VISIT(c, expr, s->v.Assert.test); | 2080 VISIT(c, expr, s->v.Assert.test); |
2084 end = compiler_new_block(c); | 2081 end = compiler_new_block(c); |
2085 if (end == NULL) | 2082 if (end == NULL) |
2086 return 0; | 2083 return 0; |
2087 » ADDOP_JREL(c, JUMP_IF_TRUE, end); | 2084 » ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); |
2088 » ADDOP(c, POP_TOP); | |
2089 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); | 2085 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
2090 if (s->v.Assert.msg) { | 2086 if (s->v.Assert.msg) { |
2091 VISIT(c, expr, s->v.Assert.msg); | 2087 VISIT(c, expr, s->v.Assert.msg); |
2092 ADDOP_I(c, RAISE_VARARGS, 2); | 2088 ADDOP_I(c, RAISE_VARARGS, 2); |
2093 } | 2089 } |
2094 else { | 2090 else { |
2095 ADDOP_I(c, RAISE_VARARGS, 1); | 2091 ADDOP_I(c, RAISE_VARARGS, 1); |
2096 } | 2092 } |
2097 compiler_use_next_block(c, end); | 2093 compiler_use_next_block(c, end); |
2098 ADDOP(c, POP_TOP); | |
2099 return 1; | 2094 return 1; |
2100 } | 2095 } |
2101 | 2096 |
2102 static int | 2097 static int |
2103 compiler_visit_stmt(struct compiler *c, stmt_ty s) | 2098 compiler_visit_stmt(struct compiler *c, stmt_ty s) |
2104 { | 2099 { |
2105 int i, n; | 2100 int i, n; |
2106 | 2101 |
2107 /* Always assign a lineno to the next instruction for a stmt. */ | 2102 /* Always assign a lineno to the next instruction for a stmt. */ |
2108 c->u->u_lineno = s->lineno; | 2103 c->u->u_lineno = s->lineno; |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2466 | 2461 |
2467 static int | 2462 static int |
2468 compiler_boolop(struct compiler *c, expr_ty e) | 2463 compiler_boolop(struct compiler *c, expr_ty e) |
2469 { | 2464 { |
2470 basicblock *end; | 2465 basicblock *end; |
2471 int jumpi, i, n; | 2466 int jumpi, i, n; |
2472 asdl_seq *s; | 2467 asdl_seq *s; |
2473 | 2468 |
2474 assert(e->kind == BoolOp_kind); | 2469 assert(e->kind == BoolOp_kind); |
2475 if (e->v.BoolOp.op == And) | 2470 if (e->v.BoolOp.op == And) |
2476 » » jumpi = JUMP_IF_FALSE; | 2471 » » jumpi = JUMP_IF_FALSE_OR_POP; |
2477 else | 2472 else |
2478 » » jumpi = JUMP_IF_TRUE; | 2473 » » jumpi = JUMP_IF_TRUE_OR_POP; |
2479 end = compiler_new_block(c); | 2474 end = compiler_new_block(c); |
2480 if (end == NULL) | 2475 if (end == NULL) |
2481 return 0; | 2476 return 0; |
2482 s = e->v.BoolOp.values; | 2477 s = e->v.BoolOp.values; |
2483 n = asdl_seq_LEN(s) - 1; | 2478 n = asdl_seq_LEN(s) - 1; |
2484 assert(n >= 0); | 2479 assert(n >= 0); |
2485 for (i = 0; i < n; ++i) { | 2480 for (i = 0; i < n; ++i) { |
2486 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); | 2481 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
2487 » » ADDOP_JREL(c, jumpi, end); | 2482 » » ADDOP_JABS(c, jumpi, end); |
2488 » » ADDOP(c, POP_TOP) | |
2489 } | 2483 } |
2490 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); | 2484 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
2491 compiler_use_next_block(c, end); | 2485 compiler_use_next_block(c, end); |
2492 return 1; | 2486 return 1; |
2493 } | 2487 } |
2494 | 2488 |
2495 static int | 2489 static int |
2496 compiler_list(struct compiler *c, expr_ty e) | 2490 compiler_list(struct compiler *c, expr_ty e) |
2497 { | 2491 { |
2498 int n = asdl_seq_LEN(e->v.List.elts); | 2492 int n = asdl_seq_LEN(e->v.List.elts); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2536 return 0; | 2530 return 0; |
2537 VISIT(c, expr,· | 2531 VISIT(c, expr,· |
2538 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); | 2532 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
2539 } | 2533 } |
2540 for (i = 1; i < n; i++) { | 2534 for (i = 1; i < n; i++) { |
2541 ADDOP(c, DUP_TOP); | 2535 ADDOP(c, DUP_TOP); |
2542 ADDOP(c, ROT_THREE); | 2536 ADDOP(c, ROT_THREE); |
2543 ADDOP_I(c, COMPARE_OP, | 2537 ADDOP_I(c, COMPARE_OP, |
2544 cmpop((cmpop_ty)(asdl_seq_GET( | 2538 cmpop((cmpop_ty)(asdl_seq_GET( |
2545 e->v.Compare.ops, i - 1)))); | 2539 e->v.Compare.ops, i - 1)))); |
2546 » » ADDOP_JREL(c, JUMP_IF_FALSE, cleanup); | 2540 » » ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
2547 NEXT_BLOCK(c); | 2541 NEXT_BLOCK(c); |
2548 ADDOP(c, POP_TOP); | |
2549 if (i < (n - 1)) | 2542 if (i < (n - 1)) |
2550 VISIT(c, expr,· | 2543 VISIT(c, expr,· |
2551 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); | 2544 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
2552 } | 2545 } |
2553 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1)); | 2546 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1)); |
2554 ADDOP_I(c, COMPARE_OP, | 2547 ADDOP_I(c, COMPARE_OP, |
2555 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1)))); | 2548 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1)))); |
2556 if (n > 1) { | 2549 if (n > 1) { |
2557 basicblock *end = compiler_new_block(c); | 2550 basicblock *end = compiler_new_block(c); |
2558 if (end == NULL) | 2551 if (end == NULL) |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2629 compiler_use_next_block(c, start); | 2622 compiler_use_next_block(c, start); |
2630 ADDOP_JREL(c, FOR_ITER, anchor); | 2623 ADDOP_JREL(c, FOR_ITER, anchor); |
2631 NEXT_BLOCK(c); | 2624 NEXT_BLOCK(c); |
2632 VISIT(c, expr, l->target); | 2625 VISIT(c, expr, l->target); |
2633 | 2626 |
2634 /* XXX this needs to be cleaned up...a lot! */ | 2627 /* XXX this needs to be cleaned up...a lot! */ |
2635 n = asdl_seq_LEN(l->ifs); | 2628 n = asdl_seq_LEN(l->ifs); |
2636 for (i = 0; i < n; i++) { | 2629 for (i = 0; i < n; i++) { |
2637 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i); | 2630 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i); |
2638 VISIT(c, expr, e); | 2631 VISIT(c, expr, e); |
2639 » » ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup); | 2632 » » ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); |
2640 NEXT_BLOCK(c); | 2633 NEXT_BLOCK(c); |
2641 ADDOP(c, POP_TOP); | |
2642 }· | 2634 }· |
2643 | 2635 |
2644 if (++gen_index < asdl_seq_LEN(generators)) | 2636 if (++gen_index < asdl_seq_LEN(generators)) |
2645 if (!compiler_listcomp_generator(c, generators, gen_index, elt)) | 2637 if (!compiler_listcomp_generator(c, generators, gen_index, elt)) |
2646 return 0; | 2638 return 0; |
2647 | 2639 |
2648 /* only append after the last for generator */ | 2640 /* only append after the last for generator */ |
2649 if (gen_index >= asdl_seq_LEN(generators)) { | 2641 if (gen_index >= asdl_seq_LEN(generators)) { |
2650 VISIT(c, expr, elt); | 2642 VISIT(c, expr, elt); |
2651 ADDOP_I(c, LIST_APPEND, gen_index+1); | 2643 ADDOP_I(c, LIST_APPEND, gen_index+1); |
2652 | 2644 |
2653 compiler_use_next_block(c, skip); | 2645 compiler_use_next_block(c, skip); |
2654 } | 2646 } |
2655 » for (i = 0; i < n; i++) { | 2647 » compiler_use_next_block(c, if_cleanup); |
2656 » » ADDOP_I(c, JUMP_FORWARD, 1); | |
2657 » » if (i == 0) | |
2658 » » compiler_use_next_block(c, if_cleanup); | |
2659 » » ADDOP(c, POP_TOP); | |
2660 » }· | |
2661 ADDOP_JABS(c, JUMP_ABSOLUTE, start); | 2648 ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
2662 compiler_use_next_block(c, anchor); | 2649 compiler_use_next_block(c, anchor); |
2663 ········ | 2650 ········ |
2664 return 1; | 2651 return 1; |
2665 } | 2652 } |
2666 | 2653 |
2667 static int | 2654 static int |
2668 compiler_listcomp(struct compiler *c, expr_ty e) | 2655 compiler_listcomp(struct compiler *c, expr_ty e) |
2669 { | 2656 { |
2670 assert(e->kind == ListComp_kind); | 2657 assert(e->kind == ListComp_kind); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2713 compiler_use_next_block(c, start); | 2700 compiler_use_next_block(c, start); |
2714 ADDOP_JREL(c, FOR_ITER, anchor); | 2701 ADDOP_JREL(c, FOR_ITER, anchor); |
2715 NEXT_BLOCK(c); | 2702 NEXT_BLOCK(c); |
2716 VISIT(c, expr, ge->target); | 2703 VISIT(c, expr, ge->target); |
2717 | 2704 |
2718 /* XXX this needs to be cleaned up...a lot! */ | 2705 /* XXX this needs to be cleaned up...a lot! */ |
2719 n = asdl_seq_LEN(ge->ifs); | 2706 n = asdl_seq_LEN(ge->ifs); |
2720 for (i = 0; i < n; i++) { | 2707 for (i = 0; i < n; i++) { |
2721 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i); | 2708 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i); |
2722 VISIT(c, expr, e); | 2709 VISIT(c, expr, e); |
2723 » » ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup); | 2710 » » ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); |
2724 NEXT_BLOCK(c); | 2711 NEXT_BLOCK(c); |
2725 » » ADDOP(c, POP_TOP); | 2712 » } |
2726 » } | |
2727 | 2713 |
2728 if (++gen_index < asdl_seq_LEN(generators)) | 2714 if (++gen_index < asdl_seq_LEN(generators)) |
2729 if (!compiler_genexp_generator(c, generators, gen_index, elt)) | 2715 if (!compiler_genexp_generator(c, generators, gen_index, elt)) |
2730 return 0; | 2716 return 0; |
2731 | 2717 |
2732 /* only append after the last 'for' generator */ | 2718 /* only append after the last 'for' generator */ |
2733 if (gen_index >= asdl_seq_LEN(generators)) { | 2719 if (gen_index >= asdl_seq_LEN(generators)) { |
2734 VISIT(c, expr, elt); | 2720 VISIT(c, expr, elt); |
2735 ADDOP(c, YIELD_VALUE); | 2721 ADDOP(c, YIELD_VALUE); |
2736 ADDOP(c, POP_TOP); | 2722 ADDOP(c, POP_TOP); |
2737 | 2723 |
2738 compiler_use_next_block(c, skip); | 2724 compiler_use_next_block(c, skip); |
2739 } | 2725 } |
2740 » for (i = 0; i < n; i++) { | 2726 » compiler_use_next_block(c, if_cleanup); |
2741 » » ADDOP_I(c, JUMP_FORWARD, 1); | |
2742 » » if (i == 0) | |
2743 » » » compiler_use_next_block(c, if_cleanup); | |
2744 | |
2745 » » ADDOP(c, POP_TOP); | |
2746 » }· | |
2747 ADDOP_JABS(c, JUMP_ABSOLUTE, start); | 2727 ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
2748 compiler_use_next_block(c, anchor); | 2728 compiler_use_next_block(c, anchor); |
2749 ADDOP(c, POP_BLOCK); | 2729 ADDOP(c, POP_BLOCK); |
2750 compiler_pop_fblock(c, LOOP, start); | 2730 compiler_pop_fblock(c, LOOP, start); |
2751 compiler_use_next_block(c, end); | 2731 compiler_use_next_block(c, end); |
2752 | 2732 |
2753 return 1; | 2733 return 1; |
2754 } | 2734 } |
2755 | 2735 |
2756 static int | 2736 static int |
(...skipping 1216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3973 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) | 3953 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
3974 goto error; | 3954 goto error; |
3975 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0) | 3955 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0) |
3976 goto error; | 3956 goto error; |
3977 | 3957 |
3978 co = makecode(c, &a); | 3958 co = makecode(c, &a); |
3979 error: | 3959 error: |
3980 assemble_free(&a); | 3960 assemble_free(&a); |
3981 return co; | 3961 return co; |
3982 } | 3962 } |
OLD | NEW |