2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c.
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
13 * Note that compiler_mod() suggests module, but the module ast type
14 * (mod_ty) has cases for expressions and interactive statements.
16 * CAUTION: The VISIT_* macros abort the current function when they
17 * encounter a problem. So don't invoke them when there is memory
18 * which needs to be released. Code blocks are OK, as the compiler
19 * structure takes care of releasing those.
24 #include "Python-ast.h"
33 int Py_OptimizeFlag
= 0;
38 opcode_stack_effect() function should be reviewed since stack depth bugs
39 could be really hard to find later.
41 Dead code is being generated (i.e. after unconditional jumps).
42 XXX(nnorwitz): not sure this is still true
45 #define DEFAULT_BLOCK_SIZE 16
46 #define DEFAULT_BLOCKS 8
47 #define DEFAULT_CODE_SIZE 128
48 #define DEFAULT_LNOTAB_SIZE 16
53 unsigned i_hasarg
: 1;
54 unsigned char i_opcode
;
56 struct basicblock_
*i_target
; /* target block (if jump instruction) */
60 typedef struct basicblock_
{
61 /* next block in the list of blocks for a unit (don't confuse with
63 struct basicblock_
*b_list
;
64 /* number of instructions used */
66 /* length of instruction array (b_instr) */
68 /* pointer to an array of instructions, initially NULL */
69 struct instr
*b_instr
;
70 /* If b_next is non-NULL, it is a pointer to the next
71 block reached by normal control flow. */
72 struct basicblock_
*b_next
;
73 /* b_seen is used to perform a DFS of basicblocks. */
75 /* b_return is true if a RETURN_VALUE opcode is inserted. */
76 unsigned b_return
: 1;
77 /* depth of stack upon entry of block, computed by stackdepth() */
79 /* instruction offset for block, computed by assemble_jump_offsets() */
83 /* fblockinfo tracks the current frame block.
85 A frame block is used to handle loops, try/except, and try/finally.
86 It's called a frame block to distinguish it from a basic block in the
90 enum fblocktype
{ LOOP
, EXCEPT
, FINALLY_TRY
, FINALLY_END
};
93 enum fblocktype fb_type
;
97 /* The following items change on entry and exit of code blocks.
98 They must be saved and restored when returning to a block.
100 struct compiler_unit
{
101 PySTEntryObject
*u_ste
;
104 /* The following fields are dicts that map objects to
105 the index of them in co_XXX. The index is used as
106 the argument for opcodes that refer to those collections.
108 PyObject
*u_consts
; /* all constants */
109 PyObject
*u_names
; /* all names */
110 PyObject
*u_varnames
; /* local variables */
111 PyObject
*u_cellvars
; /* cell variables */
112 PyObject
*u_freevars
; /* free variables */
114 PyObject
*u_private
; /* for private name mangling */
116 int u_argcount
; /* number of arguments for block */
117 basicblock
*u_blocks
; /* pointer to list of blocks */
118 basicblock
*u_curblock
; /* pointer to current block */
119 int u_tmpname
; /* temporary variables for list comps */
122 struct fblockinfo u_fblock
[CO_MAXBLOCKS
];
124 int u_firstlineno
; /* the first lineno of the block */
125 int u_lineno
; /* the lineno for the current stmt */
126 bool u_lineno_set
; /* boolean to indicate whether instr
127 has been generated with current lineno */
130 /* This struct captures the global state of a compilation.
132 The u pointer points to the current compilation unit, while units
133 for enclosing blocks are stored in c_stack. The u and c_stack are
134 managed by compiler_enter_scope() and compiler_exit_scope().
138 const char *c_filename
;
139 struct symtable
*c_st
;
140 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
141 PyCompilerFlags
*c_flags
;
146 struct compiler_unit
*u
; /* compiler state for current block */
147 PyObject
*c_stack
; /* Python list holding compiler_unit ptrs */
148 char *c_encoding
; /* source encoding (a borrowed reference) */
149 PyArena
*c_arena
; /* pointer to memory allocation arena */
153 PyObject
*a_bytecode
; /* string containing bytecode */
154 int a_offset
; /* offset into bytecode */
155 int a_nblocks
; /* number of reachable blocks */
156 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
157 PyObject
*a_lnotab
; /* string containing lnotab */
158 int a_lnotab_off
; /* offset into lnotab */
159 int a_lineno
; /* last lineno of emitted instruction */
160 int a_lineno_off
; /* bytecode offset of last lineno */
163 static int compiler_enter_scope(struct compiler
*, identifier
, void *, int);
164 static void compiler_free(struct compiler
*);
165 static basicblock
*compiler_new_block(struct compiler
*);
166 static int compiler_next_instr(struct compiler
*, basicblock
*);
167 static int compiler_addop(struct compiler
*, int);
168 static int compiler_addop_o(struct compiler
*, int, PyObject
*, PyObject
*);
169 static int compiler_addop_i(struct compiler
*, int, int);
170 static int compiler_addop_j(struct compiler
*, int, basicblock
*, int);
171 static basicblock
*compiler_use_new_block(struct compiler
*);
172 static int compiler_error(struct compiler
*, const char *);
173 static int compiler_nameop(struct compiler
*, identifier
, expr_context_ty
);
175 static PyCodeObject
*compiler_mod(struct compiler
*, mod_ty
);
176 static int compiler_visit_stmt(struct compiler
*, stmt_ty
);
177 static int compiler_visit_keyword(struct compiler
*, keyword_ty
);
178 static int compiler_visit_expr(struct compiler
*, expr_ty
);
179 static int compiler_augassign(struct compiler
*, stmt_ty
);
180 static int compiler_visit_slice(struct compiler
*, slice_ty
,
183 static int compiler_push_fblock(struct compiler
*, enum fblocktype
,
185 static void compiler_pop_fblock(struct compiler
*, enum fblocktype
,
188 static int inplace_binop(struct compiler
*, operator_ty
);
189 static int expr_constant(expr_ty e
);
191 static int compiler_with(struct compiler
*, stmt_ty
);
193 static PyCodeObject
*assemble(struct compiler
*, int addNone
);
194 static PyObject
*__doc__
;
197 _Py_Mangle(PyObject
*private, PyObject
*ident
)
199 /* Name mangling: __private becomes _classname__private.
200 This is independent from how the name is used. */
201 const char *p
, *name
= PyString_AsString(ident
);
204 if (private == NULL
|| name
== NULL
|| name
[0] != '_' ||
209 p
= PyString_AsString(private);
211 if (name
[nlen
-1] == '_' && name
[nlen
-2] == '_') {
213 return ident
; /* Don't mangle __whatever__ */
215 /* Strip leading underscores from class name */
220 return ident
; /* Don't mangle if class is just underscores */
223 ident
= PyString_FromStringAndSize(NULL
, 1 + nlen
+ plen
);
226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
227 buffer
= PyString_AS_STRING(ident
);
229 strncpy(buffer
+1, p
, plen
);
230 strcpy(buffer
+1+plen
, name
);
235 compiler_init(struct compiler
*c
)
237 memset(c
, 0, sizeof(struct compiler
));
239 c
->c_stack
= PyList_New(0);
247 PyAST_Compile(mod_ty mod
, const char *filename
, PyCompilerFlags
*flags
,
251 PyCodeObject
*co
= NULL
;
252 PyCompilerFlags local_flags
;
256 __doc__
= PyString_InternFromString("__doc__");
261 if (!compiler_init(&c
))
263 c
.c_filename
= filename
;
265 c
.c_future
= PyFuture_FromAST(mod
, filename
);
266 if (c
.c_future
== NULL
)
269 local_flags
.cf_flags
= 0;
270 flags
= &local_flags
;
272 merged
= c
.c_future
->ff_features
| flags
->cf_flags
;
273 c
.c_future
->ff_features
= merged
;
274 flags
->cf_flags
= merged
;
278 c
.c_st
= PySymtable_Build(mod
, filename
, c
.c_future
);
279 if (c
.c_st
== NULL
) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError
, "no symtable");
285 /* XXX initialize to NULL for now, need to handle */
288 co
= compiler_mod(&c
, mod
);
292 assert(co
|| PyErr_Occurred());
297 PyNode_Compile(struct _node
*n
, const char *filename
)
299 PyCodeObject
*co
= NULL
;
300 PyArena
*arena
= PyArena_New();
301 mod_ty mod
= PyAST_FromNode(n
, NULL
, filename
, arena
);
303 co
= PyAST_Compile(mod
, filename
, NULL
, arena
);
309 compiler_free(struct compiler
*c
)
312 PySymtable_Free(c
->c_st
);
314 PyMem_Free(c
->c_future
);
315 Py_DECREF(c
->c_stack
);
319 list2dict(PyObject
*list
)
322 PyObject
*v
, *k
, *dict
= PyDict_New();
324 n
= PyList_Size(list
);
325 for (i
= 0; i
< n
; i
++) {
326 v
= PyInt_FromLong(i
);
331 k
= PyList_GET_ITEM(list
, i
);
332 k
= Py_BuildValue("(OO)", k
, k
->ob_type
);
333 if (k
== NULL
|| PyDict_SetItem(dict
, k
, v
) < 0) {
345 /* Return new dict containing names from src that match scope(s).
347 src is a symbol table dictionary. If the scope of a name matches
348 either scope_type or flag is set, insert it into the new dict. The
349 values are integers, starting at offset and increasing by one for
354 dictbytype(PyObject
*src
, int scope_type
, int flag
, int offset
)
356 Py_ssize_t pos
= 0, i
= offset
, scope
;
357 PyObject
*k
, *v
, *dest
= PyDict_New();
363 while (PyDict_Next(src
, &pos
, &k
, &v
)) {
364 /* XXX this should probably be a macro in symtable.h */
365 assert(PyInt_Check(v
));
366 scope
= (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
368 if (scope
== scope_type
|| PyInt_AS_LONG(v
) & flag
) {
369 PyObject
*tuple
, *item
= PyInt_FromLong(i
);
375 tuple
= Py_BuildValue("(OO)", k
, k
->ob_type
);
376 if (!tuple
|| PyDict_SetItem(dest
, tuple
, item
) < 0) {
389 /* Begin: Peephole optimizations ----------------------------------------- */
391 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
392 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
393 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
394 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
395 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
396 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
397 #define ISBASICBLOCK(blocks, start, bytes) \
398 (blocks[start]==blocks[start+bytes-1])
400 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
401 with LOAD_CONST (c1, c2, ... cn).
402 The consts table must still be in list form so that the
403 new constant (c1, c2, ... cn) can be appended.
404 Called with codestr pointing to the first LOAD_CONST.
405 Bails out with no change if one or more of the LOAD_CONSTs is missing.
406 Also works for BUILD_LIST when followed by an "in" or "not in" test.
409 tuple_of_constants(unsigned char *codestr
, int n
, PyObject
*consts
)
411 PyObject
*newconst
, *constant
;
412 Py_ssize_t i
, arg
, len_consts
;
415 assert(PyList_CheckExact(consts
));
416 assert(codestr
[n
*3] == BUILD_TUPLE
|| codestr
[n
*3] == BUILD_LIST
);
417 assert(GETARG(codestr
, (n
*3)) == n
);
418 for (i
=0 ; i
<n
; i
++)
419 assert(codestr
[i
*3] == LOAD_CONST
);
421 /* Buildup new tuple of constants */
422 newconst
= PyTuple_New(n
);
423 if (newconst
== NULL
)
425 len_consts
= PyList_GET_SIZE(consts
);
426 for (i
=0 ; i
<n
; i
++) {
427 arg
= GETARG(codestr
, (i
*3));
428 assert(arg
< len_consts
);
429 constant
= PyList_GET_ITEM(consts
, arg
);
431 PyTuple_SET_ITEM(newconst
, i
, constant
);
434 /* Append folded constant onto consts */
435 if (PyList_Append(consts
, newconst
)) {
441 /* Write NOPs over old LOAD_CONSTS and
442 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
443 memset(codestr
, NOP
, n
*3);
444 codestr
[n
*3] = LOAD_CONST
;
445 SETARG(codestr
, (n
*3), len_consts
);
449 /* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
450 with LOAD_CONST binop(c1,c2)
451 The consts table must still be in list form so that the
452 new constant can be appended.
453 Called with codestr pointing to the first LOAD_CONST.
454 Abandons the transformation if the folding fails (i.e. 1+'a').
455 If the new constant is a sequence, only folds when the size
456 is below a threshold value. That keeps pyc files from
457 becoming large in the presence of code like: (None,)*1000.
460 fold_binops_on_constants(unsigned char *codestr
, PyObject
*consts
)
462 PyObject
*newconst
, *v
, *w
;
463 Py_ssize_t len_consts
, size
;
467 assert(PyList_CheckExact(consts
));
468 assert(codestr
[0] == LOAD_CONST
);
469 assert(codestr
[3] == LOAD_CONST
);
471 /* Create new constant */
472 v
= PyList_GET_ITEM(consts
, GETARG(codestr
, 0));
473 w
= PyList_GET_ITEM(consts
, GETARG(codestr
, 3));
477 newconst
= PyNumber_Power(v
, w
, Py_None
);
479 case BINARY_MULTIPLY
:
480 newconst
= PyNumber_Multiply(v
, w
);
483 /* Cannot fold this operation statically since
484 the result can depend on the run-time presence
487 case BINARY_TRUE_DIVIDE
:
488 newconst
= PyNumber_TrueDivide(v
, w
);
490 case BINARY_FLOOR_DIVIDE
:
491 newconst
= PyNumber_FloorDivide(v
, w
);
494 newconst
= PyNumber_Remainder(v
, w
);
497 newconst
= PyNumber_Add(v
, w
);
499 case BINARY_SUBTRACT
:
500 newconst
= PyNumber_Subtract(v
, w
);
503 newconst
= PyObject_GetItem(v
, w
);
506 newconst
= PyNumber_Lshift(v
, w
);
509 newconst
= PyNumber_Rshift(v
, w
);
512 newconst
= PyNumber_And(v
, w
);
515 newconst
= PyNumber_Xor(v
, w
);
518 newconst
= PyNumber_Or(v
, w
);
521 /* Called with an unknown opcode */
522 PyErr_Format(PyExc_SystemError
,
523 "unexpected binary operation %d on a constant",
527 if (newconst
== NULL
) {
531 size
= PyObject_Size(newconst
);
534 else if (size
> 20) {
539 /* Append folded constant into consts table */
540 len_consts
= PyList_GET_SIZE(consts
);
541 if (PyList_Append(consts
, newconst
)) {
547 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
548 memset(codestr
, NOP
, 4);
549 codestr
[4] = LOAD_CONST
;
550 SETARG(codestr
, 4, len_consts
);
555 fold_unaryops_on_constants(unsigned char *codestr
, PyObject
*consts
)
557 PyObject
*newconst
=NULL
, *v
;
558 Py_ssize_t len_consts
;
562 assert(PyList_CheckExact(consts
));
563 assert(codestr
[0] == LOAD_CONST
);
565 /* Create new constant */
566 v
= PyList_GET_ITEM(consts
, GETARG(codestr
, 0));
570 /* Preserve the sign of -0.0 */
571 if (PyObject_IsTrue(v
) == 1)
572 newconst
= PyNumber_Negative(v
);
575 newconst
= PyObject_Repr(v
);
578 newconst
= PyNumber_Invert(v
);
581 /* Called with an unknown opcode */
582 PyErr_Format(PyExc_SystemError
,
583 "unexpected unary operation %d on a constant",
587 if (newconst
== NULL
) {
592 /* Append folded constant into consts table */
593 len_consts
= PyList_GET_SIZE(consts
);
594 if (PyList_Append(consts
, newconst
)) {
600 /* Write NOP LOAD_CONST newconst */
602 codestr
[1] = LOAD_CONST
;
603 SETARG(codestr
, 1, len_consts
);
607 static unsigned int *
608 markblocks(unsigned char *code
, int len
)
610 unsigned int *blocks
= PyMem_Malloc(len
*sizeof(int));
611 int i
,j
, opcode
, blockcnt
= 0;
615 memset(blocks
, 0, len
*sizeof(int));
617 /* Mark labels in the first pass */
618 for (i
=0 ; i
<len
; i
+=CODESIZE(opcode
)) {
630 j
= GETJUMPTGT(code
, i
);
635 /* Build block numbers in the second pass */
636 for (i
=0 ; i
<len
; i
++) {
637 blockcnt
+= blocks
[i
]; /* increment blockcnt over labels */
638 blocks
[i
] = blockcnt
;
643 /* Perform basic peephole optimizations to components of a code object.
644 The consts object should still be in list form to allow new constants
647 To keep the optimizer simple, it bails out (does nothing) for code
648 containing extended arguments or that has a length over 32,700. That
649 allows us to avoid overflow and sign issues. Likewise, it bails when
650 the lineno table has complex encoding for gaps >= 255.
652 Optimizations are restricted to simple transformations occuring within a
653 single basic block. All transformations keep the code size the same or
654 smaller. For those that reduce size, the gaps are initially filled with
655 NOPs. Later those NOPs are removed and the jump addresses retargeted in
656 a single pass. Line numbering is adjusted accordingly. */
659 optimize_code(PyObject
*code
, PyObject
* consts
, PyObject
*names
,
660 PyObject
*lineno_obj
)
662 Py_ssize_t i
, j
, codelen
;
664 int tgt
, tgttgt
, opcode
;
665 unsigned char *codestr
= NULL
;
666 unsigned char *lineno
;
668 int new_line
, cum_orig_line
, last_line
, tabsiz
;
669 int cumlc
=0, lastlc
=0; /* Count runs of consecutive LOAD_CONSTs */
670 unsigned int *blocks
= NULL
;
673 /* Bail out if an exception is set */
674 if (PyErr_Occurred())
677 /* Bypass optimization when the lineno table is too complex */
678 assert(PyString_Check(lineno_obj
));
679 lineno
= (unsigned char*)PyString_AS_STRING(lineno_obj
);
680 tabsiz
= PyString_GET_SIZE(lineno_obj
);
681 if (memchr(lineno
, 255, tabsiz
) != NULL
)
684 /* Avoid situations where jump retargeting could overflow */
685 assert(PyString_Check(code
));
686 codelen
= PyString_Size(code
);
690 /* Make a modifiable copy of the code string */
691 codestr
= PyMem_Malloc(codelen
);
694 codestr
= memcpy(codestr
, PyString_AS_STRING(code
), codelen
);
696 /* Verify that RETURN_VALUE terminates the codestring. This allows
697 the various transformation patterns to look ahead several
698 instructions without additional checks to make sure they are not
699 looking beyond the end of the code string.
701 if (codestr
[codelen
-1] != RETURN_VALUE
)
704 /* Mapping to new jump targets after NOPs are removed */
705 addrmap
= PyMem_Malloc(codelen
* sizeof(int));
709 blocks
= markblocks(codestr
, codelen
);
712 assert(PyList_Check(consts
));
714 for (i
=0 ; i
<codelen
; i
+= CODESIZE(codestr
[i
])) {
722 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
723 with JUMP_IF_TRUE POP_TOP */
725 if (codestr
[i
+1] != JUMP_IF_FALSE
||
726 codestr
[i
+4] != POP_TOP
||
727 !ISBASICBLOCK(blocks
,i
,5))
729 tgt
= GETJUMPTGT(codestr
, (i
+1));
730 if (codestr
[tgt
] != POP_TOP
)
732 j
= GETARG(codestr
, i
+1) + 1;
733 codestr
[i
] = JUMP_IF_TRUE
;
734 SETARG(codestr
, i
, j
);
735 codestr
[i
+3] = POP_TOP
;
739 /* not a is b --> a is not b
740 not a in b --> a not in b
741 not a is not b --> a is b
742 not a not in b --> a in b
745 j
= GETARG(codestr
, i
);
746 if (j
< 6 || j
> 9 ||
747 codestr
[i
+3] != UNARY_NOT
||
748 !ISBASICBLOCK(blocks
,i
,4))
750 SETARG(codestr
, i
, (j
^1));
754 /* Replace LOAD_GLOBAL/LOAD_NAME None
755 with LOAD_CONST None */
758 j
= GETARG(codestr
, i
);
759 name
= PyString_AsString(PyTuple_GET_ITEM(names
, j
));
760 if (name
== NULL
|| strcmp(name
, "None") != 0)
762 for (j
=0 ; j
< PyList_GET_SIZE(consts
) ; j
++) {
763 if (PyList_GET_ITEM(consts
, j
) == Py_None
) {
764 codestr
[i
] = LOAD_CONST
;
765 SETARG(codestr
, i
, j
);
772 /* Skip over LOAD_CONST trueconst
773 JUMP_IF_FALSE xx POP_TOP */
776 j
= GETARG(codestr
, i
);
777 if (codestr
[i
+3] != JUMP_IF_FALSE
||
778 codestr
[i
+6] != POP_TOP
||
779 !ISBASICBLOCK(blocks
,i
,7) ||
780 !PyObject_IsTrue(PyList_GET_ITEM(consts
, j
)))
782 memset(codestr
+i
, NOP
, 7);
786 /* Try to fold tuples of constants (includes a case for lists
787 which are only used for "in" and "not in" tests).
788 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
789 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
790 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
793 j
= GETARG(codestr
, i
);
797 ((opcode
== BUILD_TUPLE
&&
798 ISBASICBLOCK(blocks
, h
, 3*(j
+1))) ||
799 (opcode
== BUILD_LIST
&&
800 codestr
[i
+3]==COMPARE_OP
&&
801 ISBASICBLOCK(blocks
, h
, 3*(j
+2)) &&
802 (GETARG(codestr
,i
+3)==6 ||
803 GETARG(codestr
,i
+3)==7))) &&
804 tuple_of_constants(&codestr
[h
], j
, consts
)) {
805 assert(codestr
[i
] == LOAD_CONST
);
809 if (codestr
[i
+3] != UNPACK_SEQUENCE
||
810 !ISBASICBLOCK(blocks
,i
,6) ||
811 j
!= GETARG(codestr
, i
+3))
814 memset(codestr
+i
, NOP
, 6);
816 codestr
[i
] = ROT_TWO
;
817 memset(codestr
+i
+1, NOP
, 5);
819 codestr
[i
] = ROT_THREE
;
820 codestr
[i
+1] = ROT_TWO
;
821 memset(codestr
+i
+2, NOP
, 4);
825 /* Fold binary ops on constants.
826 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
828 case BINARY_MULTIPLY
:
829 case BINARY_TRUE_DIVIDE
:
830 case BINARY_FLOOR_DIVIDE
:
833 case BINARY_SUBTRACT
:
841 ISBASICBLOCK(blocks
, i
-6, 7) &&
842 fold_binops_on_constants(&codestr
[i
-6], consts
)) {
844 assert(codestr
[i
] == LOAD_CONST
);
849 /* Fold unary ops on constants.
850 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
855 ISBASICBLOCK(blocks
, i
-3, 4) &&
856 fold_unaryops_on_constants(&codestr
[i
-3], consts
)) {
858 assert(codestr
[i
] == LOAD_CONST
);
863 /* Simplify conditional jump to conditional jump where the
864 result of the first test implies the success of a similar
865 test or the failure of the opposite test.
871 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
872 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
873 where y+3 is the instruction following the second test.
877 tgt
= GETJUMPTGT(codestr
, i
);
879 if (j
== JUMP_IF_FALSE
|| j
== JUMP_IF_TRUE
) {
881 tgttgt
= GETJUMPTGT(codestr
, tgt
) - i
- 3;
882 SETARG(codestr
, i
, tgttgt
);
885 SETARG(codestr
, i
, tgt
);
889 /* Intentional fallthrough */
891 /* Replace jumps to unconditional jumps */
899 tgt
= GETJUMPTGT(codestr
, i
);
900 if (!UNCONDITIONAL_JUMP(codestr
[tgt
]))
902 tgttgt
= GETJUMPTGT(codestr
, tgt
);
903 if (opcode
== JUMP_FORWARD
) /* JMP_ABS can go backwards */
904 opcode
= JUMP_ABSOLUTE
;
905 if (!ABSOLUTE_JUMP(opcode
))
906 tgttgt
-= i
+ 3; /* Calc relative jump addr */
907 if (tgttgt
< 0) /* No backward relative jumps */
910 SETARG(codestr
, i
, tgttgt
);
916 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
918 if (i
+4 >= codelen
||
919 codestr
[i
+4] != RETURN_VALUE
||
920 !ISBASICBLOCK(blocks
,i
,5))
922 memset(codestr
+i
+1, NOP
, 4);
927 /* Fixup linenotab */
928 for (i
=0, nops
=0 ; i
<codelen
; i
+= CODESIZE(codestr
[i
])) {
929 addrmap
[i
] = i
- nops
;
930 if (codestr
[i
] == NOP
)
935 for (i
=0 ; i
< tabsiz
; i
+=2) {
936 cum_orig_line
+= lineno
[i
];
937 new_line
= addrmap
[cum_orig_line
];
938 assert (new_line
- last_line
< 255);
939 lineno
[i
] =((unsigned char)(new_line
- last_line
));
940 last_line
= new_line
;
943 /* Remove NOPs and fixup jump targets */
944 for (i
=0, h
=0 ; i
<codelen
; ) {
953 j
= addrmap
[GETARG(codestr
, i
)];
954 SETARG(codestr
, i
, j
);
964 j
= addrmap
[GETARG(codestr
, i
) + i
+ 3] - addrmap
[i
] - 3;
965 SETARG(codestr
, i
, j
);
968 adj
= CODESIZE(opcode
);
970 codestr
[h
++] = codestr
[i
++];
972 assert(h
+ nops
== codelen
);
974 code
= PyString_FromStringAndSize((char *)codestr
, h
);
991 /* End: Peephole optimizations ----------------------------------------- */
995 Leave this debugging code for just a little longer.
998 compiler_display_symbols(PyObject *name, PyObject *symbols)
1000 PyObject *key, *value;
1004 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
1005 while (PyDict_Next(symbols, &pos, &key, &value)) {
1006 flags = PyInt_AsLong(value);
1007 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
1008 if (flags & DEF_GLOBAL)
1009 fprintf(stderr, " declared_global");
1010 if (flags & DEF_LOCAL)
1011 fprintf(stderr, " local");
1012 if (flags & DEF_PARAM)
1013 fprintf(stderr, " param");
1014 if (flags & DEF_STAR)
1015 fprintf(stderr, " stararg");
1016 if (flags & DEF_DOUBLESTAR)
1017 fprintf(stderr, " starstar");
1018 if (flags & DEF_INTUPLE)
1019 fprintf(stderr, " tuple");
1020 if (flags & DEF_FREE)
1021 fprintf(stderr, " free");
1022 if (flags & DEF_FREE_GLOBAL)
1023 fprintf(stderr, " global");
1024 if (flags & DEF_FREE_CLASS)
1025 fprintf(stderr, " free/class");
1026 if (flags & DEF_IMPORT)
1027 fprintf(stderr, " import");
1028 fprintf(stderr, "\n");
1030 fprintf(stderr, "\n");
1035 compiler_unit_check(struct compiler_unit
*u
)
1038 for (block
= u
->u_blocks
; block
!= NULL
; block
= block
->b_list
) {
1039 assert(block
!= (void *)0xcbcbcbcb);
1040 assert(block
!= (void *)0xfbfbfbfb);
1041 assert(block
!= (void *)0xdbdbdbdb);
1042 if (block
->b_instr
!= NULL
) {
1043 assert(block
->b_ialloc
> 0);
1044 assert(block
->b_iused
> 0);
1045 assert(block
->b_ialloc
>= block
->b_iused
);
1048 assert (block
->b_iused
== 0);
1049 assert (block
->b_ialloc
== 0);
1055 compiler_unit_free(struct compiler_unit
*u
)
1057 basicblock
*b
, *next
;
1059 compiler_unit_check(u
);
1063 PyObject_Free((void *)b
->b_instr
);
1065 PyObject_Free((void *)b
);
1068 Py_XDECREF(u
->u_ste
);
1069 Py_XDECREF(u
->u_name
);
1070 Py_XDECREF(u
->u_consts
);
1071 Py_XDECREF(u
->u_names
);
1072 Py_XDECREF(u
->u_varnames
);
1073 Py_XDECREF(u
->u_freevars
);
1074 Py_XDECREF(u
->u_cellvars
);
1075 Py_XDECREF(u
->u_private
);
1080 compiler_enter_scope(struct compiler
*c
, identifier name
, void *key
,
1083 struct compiler_unit
*u
;
1085 u
= PyObject_Malloc(sizeof(struct compiler_unit
));
1090 memset(u
, 0, sizeof(struct compiler_unit
));
1092 u
->u_ste
= PySymtable_Lookup(c
->c_st
, key
);
1094 compiler_unit_free(u
);
1099 u
->u_varnames
= list2dict(u
->u_ste
->ste_varnames
);
1100 u
->u_cellvars
= dictbytype(u
->u_ste
->ste_symbols
, CELL
, 0, 0);
1101 u
->u_freevars
= dictbytype(u
->u_ste
->ste_symbols
, FREE
, DEF_FREE_CLASS
,
1102 PyDict_Size(u
->u_cellvars
));
1107 u
->u_firstlineno
= lineno
;
1109 u
->u_lineno_set
= false;
1110 u
->u_consts
= PyDict_New();
1112 compiler_unit_free(u
);
1115 u
->u_names
= PyDict_New();
1117 compiler_unit_free(u
);
1121 u
->u_private
= NULL
;
1123 /* Push the old compiler_unit on the stack. */
1125 PyObject
*wrapper
= PyCObject_FromVoidPtr(c
->u
, NULL
);
1126 if (PyList_Append(c
->c_stack
, wrapper
) < 0) {
1127 compiler_unit_free(u
);
1131 u
->u_private
= c
->u
->u_private
;
1132 Py_XINCREF(u
->u_private
);
1137 if (compiler_use_new_block(c
) == NULL
)
1144 compiler_exit_scope(struct compiler
*c
)
1150 compiler_unit_free(c
->u
);
1151 /* Restore c->u to the parent unit. */
1152 n
= PyList_GET_SIZE(c
->c_stack
) - 1;
1154 wrapper
= PyList_GET_ITEM(c
->c_stack
, n
);
1155 c
->u
= (struct compiler_unit
*)PyCObject_AsVoidPtr(wrapper
);
1156 /* we are deleting from a list so this really shouldn't fail */
1157 if (PySequence_DelItem(c
->c_stack
, n
) < 0)
1158 Py_FatalError("compiler_exit_scope()");
1159 compiler_unit_check(c
->u
);
1166 /* Allocate a new "anonymous" local variable.
1167 Used by list comprehensions and with statements.
1171 compiler_new_tmpname(struct compiler
*c
)
1174 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->u
->u_tmpname
);
1175 return PyString_FromString(tmpname
);
1178 /* Allocate a new block and return a pointer to it.
1179 Returns NULL on error.
1183 compiler_new_block(struct compiler
*c
)
1186 struct compiler_unit
*u
;
1189 b
= (basicblock
*)PyObject_Malloc(sizeof(basicblock
));
1194 memset((void *)b
, 0, sizeof(basicblock
));
1195 assert (b
->b_next
== NULL
);
1196 b
->b_list
= u
->u_blocks
;
1202 compiler_use_new_block(struct compiler
*c
)
1204 basicblock
*block
= compiler_new_block(c
);
1207 c
->u
->u_curblock
= block
;
1212 compiler_next_block(struct compiler
*c
)
1214 basicblock
*block
= compiler_new_block(c
);
1217 c
->u
->u_curblock
->b_next
= block
;
1218 c
->u
->u_curblock
= block
;
1223 compiler_use_next_block(struct compiler
*c
, basicblock
*block
)
1225 assert(block
!= NULL
);
1226 c
->u
->u_curblock
->b_next
= block
;
1227 c
->u
->u_curblock
= block
;
1231 /* Returns the offset of the next instruction in the current block's
1232 b_instr array. Resizes the b_instr as necessary.
1233 Returns -1 on failure.
1237 compiler_next_instr(struct compiler
*c
, basicblock
*b
)
1240 if (b
->b_instr
== NULL
) {
1241 b
->b_instr
= PyObject_Malloc(sizeof(struct instr
) *
1242 DEFAULT_BLOCK_SIZE
);
1243 if (b
->b_instr
== NULL
) {
1247 b
->b_ialloc
= DEFAULT_BLOCK_SIZE
;
1248 memset((char *)b
->b_instr
, 0,
1249 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
1251 else if (b
->b_iused
== b
->b_ialloc
) {
1252 size_t oldsize
, newsize
;
1253 oldsize
= b
->b_ialloc
* sizeof(struct instr
);
1254 newsize
= oldsize
<< 1;
1260 b
->b_instr
= PyObject_Realloc((void *)b
->b_instr
, newsize
);
1261 if (b
->b_instr
== NULL
)
1263 memset((char *)b
->b_instr
+ oldsize
, 0, newsize
- oldsize
);
1265 return b
->b_iused
++;
1269 compiler_set_lineno(struct compiler
*c
, int off
)
1272 if (c
->u
->u_lineno_set
)
1274 c
->u
->u_lineno_set
= true;
1275 b
= c
->u
->u_curblock
;
1276 b
->b_instr
[off
].i_lineno
= c
->u
->u_lineno
;
1280 opcode_stack_effect(int opcode
, int oparg
)
1293 case UNARY_POSITIVE
:
1294 case UNARY_NEGATIVE
:
1304 case BINARY_MULTIPLY
:
1308 case BINARY_SUBTRACT
:
1310 case BINARY_FLOOR_DIVIDE
:
1311 case BINARY_TRUE_DIVIDE
:
1313 case INPLACE_FLOOR_DIVIDE
:
1314 case INPLACE_TRUE_DIVIDE
:
1335 case DELETE_SLICE
+0:
1337 case DELETE_SLICE
+1:
1339 case DELETE_SLICE
+2:
1341 case DELETE_SLICE
+3:
1345 case INPLACE_SUBTRACT
:
1346 case INPLACE_MULTIPLY
:
1347 case INPLACE_DIVIDE
:
1348 case INPLACE_MODULO
:
1374 case PRINT_NEWLINE_TO
:
1376 case INPLACE_LSHIFT
:
1377 case INPLACE_RSHIFT
:
1400 return -1; /* or -2 or -3 if exception occurred */
1408 case UNPACK_SEQUENCE
:
1456 return 3; /* actually pushed by an exception */
1467 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
1469 return -NARGS(oparg
);
1470 case CALL_FUNCTION_VAR
:
1471 case CALL_FUNCTION_KW
:
1472 return -NARGS(oparg
)-1;
1473 case CALL_FUNCTION_VAR_KW
:
1474 return -NARGS(oparg
)-2;
1493 fprintf(stderr
, "opcode = %d\n", opcode
);
1494 Py_FatalError("opcode_stack_effect()");
1497 return 0; /* not reachable */
1500 /* Add an opcode with no argument.
1501 Returns 0 on failure, 1 on success.
1505 compiler_addop(struct compiler
*c
, int opcode
)
1510 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1513 b
= c
->u
->u_curblock
;
1514 i
= &b
->b_instr
[off
];
1515 i
->i_opcode
= opcode
;
1517 if (opcode
== RETURN_VALUE
)
1519 compiler_set_lineno(c
, off
);
1524 compiler_add_o(struct compiler
*c
, PyObject
*dict
, PyObject
*o
)
1529 /* necessary to make sure types aren't coerced (e.g., int and long) */
1530 t
= PyTuple_Pack(2, o
, o
->ob_type
);
1534 v
= PyDict_GetItem(dict
, t
);
1536 arg
= PyDict_Size(dict
);
1537 v
= PyInt_FromLong(arg
);
1542 if (PyDict_SetItem(dict
, t
, v
) < 0) {
1550 arg
= PyInt_AsLong(v
);
1556 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
1559 int arg
= compiler_add_o(c
, dict
, o
);
1562 return compiler_addop_i(c
, opcode
, arg
);
1566 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
1570 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
1573 arg
= compiler_add_o(c
, dict
, mangled
);
1577 return compiler_addop_i(c
, opcode
, arg
);
1580 /* Add an opcode with an integer argument.
1581 Returns 0 on failure, 1 on success.
1585 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
1589 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1592 i
= &c
->u
->u_curblock
->b_instr
[off
];
1593 i
->i_opcode
= opcode
;
1596 compiler_set_lineno(c
, off
);
1601 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
1607 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1610 compiler_set_lineno(c
, off
);
1611 i
= &c
->u
->u_curblock
->b_instr
[off
];
1612 i
->i_opcode
= opcode
;
1622 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1623 like to find better names.) NEW_BLOCK() creates a new block and sets
1624 it as the current block. NEXT_BLOCK() also creates an implicit jump
1625 from the current block to the new block.
1628 /* XXX The returns inside these macros make it impossible to decref
1629 objects created in the local function.
1633 #define NEW_BLOCK(C) { \
1634 if (compiler_use_new_block((C)) == NULL) \
1638 #define NEXT_BLOCK(C) { \
1639 if (compiler_next_block((C)) == NULL) \
1643 #define ADDOP(C, OP) { \
1644 if (!compiler_addop((C), (OP))) \
1648 #define ADDOP_IN_SCOPE(C, OP) { \
1649 if (!compiler_addop((C), (OP))) { \
1650 compiler_exit_scope(c); \
1655 #define ADDOP_O(C, OP, O, TYPE) { \
1656 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1660 #define ADDOP_NAME(C, OP, O, TYPE) { \
1661 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1665 #define ADDOP_I(C, OP, O) { \
1666 if (!compiler_addop_i((C), (OP), (O))) \
1670 #define ADDOP_JABS(C, OP, O) { \
1671 if (!compiler_addop_j((C), (OP), (O), 1)) \
1675 #define ADDOP_JREL(C, OP, O) { \
1676 if (!compiler_addop_j((C), (OP), (O), 0)) \
1680 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1681 the ASDL name to synthesize the name of the C type and the visit function.
1684 #define VISIT(C, TYPE, V) {\
1685 if (!compiler_visit_ ## TYPE((C), (V))) \
1689 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1690 if (!compiler_visit_ ## TYPE((C), (V))) { \
1691 compiler_exit_scope(c); \
1696 #define VISIT_SLICE(C, V, CTX) {\
1697 if (!compiler_visit_slice((C), (V), (CTX))) \
1701 #define VISIT_SEQ(C, TYPE, SEQ) { \
1703 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1704 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1705 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
1706 if (!compiler_visit_ ## TYPE((C), elt)) \
1711 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1713 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1714 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1715 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
1716 if (!compiler_visit_ ## TYPE((C), elt)) { \
1717 compiler_exit_scope(c); \
1724 compiler_isdocstring(stmt_ty s
)
1726 if (s
->kind
!= Expr_kind
)
1728 return s
->v
.Expr
.value
->kind
== Str_kind
;
1731 /* Compile a sequence of statements, checking for a docstring. */
1734 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1739 if (!asdl_seq_LEN(stmts
))
1741 st
= asdl_seq_GET(stmts
, 0);
1742 if (compiler_isdocstring(st
)) {
1744 VISIT(c
, expr
, st
->v
.Expr
.value
);
1745 if (!compiler_nameop(c
, __doc__
, Store
))
1748 for (; i
< asdl_seq_LEN(stmts
); i
++)
1749 VISIT(c
, stmt
, asdl_seq_GET(stmts
, i
));
1753 static PyCodeObject
*
1754 compiler_mod(struct compiler
*c
, mod_ty mod
)
1758 static PyObject
*module
;
1760 module
= PyString_FromString("<module>");
1764 if (!compiler_enter_scope(c
, module
, mod
, 1))
1766 switch (mod
->kind
) {
1768 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1769 compiler_exit_scope(c
);
1773 case Interactive_kind
:
1774 c
->c_interactive
= 1;
1775 VISIT_SEQ_IN_SCOPE(c
, stmt
, mod
->v
.Interactive
.body
);
1777 case Expression_kind
:
1778 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1782 PyErr_SetString(PyExc_SystemError
,
1783 "suite should not be possible");
1786 PyErr_Format(PyExc_SystemError
,
1787 "module kind %d should not be possible",
1791 co
= assemble(c
, addNone
);
1792 compiler_exit_scope(c
);
1796 /* The test for LOCAL must come before the test for FREE in order to
1797 handle classes where name is both local and free. The local var is
1798 a method and the free var is a free var referenced within a method.
1802 get_ref_type(struct compiler
*c
, PyObject
*name
)
1804 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1807 PyOS_snprintf(buf
, sizeof(buf
),
1808 "unknown scope for %.100s in %.100s(%s) in %s\n"
1809 "symbols: %s\nlocals: %s\nglobals: %s\n",
1810 PyString_AS_STRING(name
),
1811 PyString_AS_STRING(c
->u
->u_name
),
1812 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1814 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1815 PyObject_REPR(c
->u
->u_varnames
),
1816 PyObject_REPR(c
->u
->u_names
)
1825 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1828 k
= Py_BuildValue("(OO)", name
, name
->ob_type
);
1831 v
= PyDict_GetItem(dict
, k
);
1835 return PyInt_AS_LONG(v
);
1839 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1841 int i
, free
= PyCode_GetNumFree(co
);
1843 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1844 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1847 for (i
= 0; i
< free
; ++i
) {
1848 /* Bypass com_addop_varname because it will generate
1849 LOAD_DEREF but LOAD_CLOSURE is needed.
1851 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1854 /* Special case: If a class contains a method with a
1855 free variable that has the same name as a method,
1856 the name will be considered free *and* local in the
1857 class. It should be handled by the closure, as
1858 well as by the normal name loookup logic.
1860 reftype
= get_ref_type(c
, name
);
1861 if (reftype
== CELL
)
1862 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1863 else /* (reftype == FREE) */
1864 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1866 printf("lookup %s in %s %d %d\n"
1867 "freevars of %s: %s\n",
1868 PyObject_REPR(name
),
1869 PyString_AS_STRING(c
->u
->u_name
),
1871 PyString_AS_STRING(co
->co_name
),
1872 PyObject_REPR(co
->co_freevars
));
1873 Py_FatalError("compiler_make_closure()");
1875 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1877 ADDOP_I(c
, BUILD_TUPLE
, free
);
1878 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1879 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1884 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1891 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1892 VISIT(c
, expr
, asdl_seq_GET(decos
, i
));
1898 compiler_arguments(struct compiler
*c
, arguments_ty args
)
1901 int n
= asdl_seq_LEN(args
->args
);
1902 /* Correctly handle nested argument lists */
1903 for (i
= 0; i
< n
; i
++) {
1904 expr_ty arg
= asdl_seq_GET(args
->args
, i
);
1905 if (arg
->kind
== Tuple_kind
) {
1906 PyObject
*id
= PyString_FromFormat(".%d", i
);
1910 if (!compiler_nameop(c
, id
, Load
)) {
1915 VISIT(c
, expr
, arg
);
1922 compiler_function(struct compiler
*c
, stmt_ty s
)
1925 PyObject
*first_const
= Py_None
;
1926 arguments_ty args
= s
->v
.FunctionDef
.args
;
1927 asdl_seq
* decos
= s
->v
.FunctionDef
.decorators
;
1929 int i
, n
, docstring
;
1931 assert(s
->kind
== FunctionDef_kind
);
1933 if (!compiler_decorators(c
, decos
))
1936 VISIT_SEQ(c
, expr
, args
->defaults
);
1937 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1941 st
= asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1942 docstring
= compiler_isdocstring(st
);
1944 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1945 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1946 compiler_exit_scope(c
);
1950 /* unpack nested arguments */
1951 compiler_arguments(c
, args
);
1953 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1954 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1955 /* if there was a docstring, we need to skip the first statement */
1956 for (i
= docstring
; i
< n
; i
++) {
1957 stmt_ty s2
= asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1958 if (i
== 0 && s2
->kind
== Expr_kind
&&
1959 s2
->v
.Expr
.value
->kind
== Str_kind
)
1961 VISIT_IN_SCOPE(c
, stmt
, s2
);
1963 co
= assemble(c
, 1);
1964 compiler_exit_scope(c
);
1968 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1971 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1972 ADDOP_I(c
, CALL_FUNCTION
, 1);
1975 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1979 compiler_class(struct compiler
*c
, stmt_ty s
)
1984 /* push class name on stack, needed by BUILD_CLASS */
1985 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1986 /* push the tuple of base classes on the stack */
1987 n
= asdl_seq_LEN(s
->v
.ClassDef
.bases
);
1989 VISIT_SEQ(c
, expr
, s
->v
.ClassDef
.bases
);
1990 ADDOP_I(c
, BUILD_TUPLE
, n
);
1991 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
,
1994 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1995 Py_INCREF(c
->u
->u_private
);
1996 str
= PyString_InternFromString("__name__");
1997 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1999 compiler_exit_scope(c
);
2004 str
= PyString_InternFromString("__module__");
2005 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
2007 compiler_exit_scope(c
);
2012 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
2013 compiler_exit_scope(c
);
2017 ADDOP_IN_SCOPE(c
, LOAD_LOCALS
);
2018 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
2019 co
= assemble(c
, 1);
2020 compiler_exit_scope(c
);
2024 compiler_make_closure(c
, co
, 0);
2027 ADDOP_I(c
, CALL_FUNCTION
, 0);
2028 ADDOP(c
, BUILD_CLASS
);
2029 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
2035 compiler_ifexp(struct compiler
*c
, expr_ty e
)
2037 basicblock
*end
, *next
;
2039 assert(e
->kind
== IfExp_kind
);
2040 end
= compiler_new_block(c
);
2043 next
= compiler_new_block(c
);
2046 VISIT(c
, expr
, e
->v
.IfExp
.test
);
2047 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
2049 VISIT(c
, expr
, e
->v
.IfExp
.body
);
2050 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2051 compiler_use_next_block(c
, next
);
2053 VISIT(c
, expr
, e
->v
.IfExp
.orelse
);
2054 compiler_use_next_block(c
, end
);
2059 compiler_lambda(struct compiler
*c
, expr_ty e
)
2062 static identifier name
;
2063 arguments_ty args
= e
->v
.Lambda
.args
;
2064 assert(e
->kind
== Lambda_kind
);
2067 name
= PyString_InternFromString("<lambda>");
2073 VISIT_SEQ(c
, expr
, args
->defaults
);
2074 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2077 /* unpack nested arguments */
2078 compiler_arguments(c
, args
);
2080 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
2081 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
2082 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
2083 co
= assemble(c
, 1);
2084 compiler_exit_scope(c
);
2088 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
2095 compiler_print(struct compiler
*c
, stmt_ty s
)
2100 assert(s
->kind
== Print_kind
);
2101 n
= asdl_seq_LEN(s
->v
.Print
.values
);
2103 if (s
->v
.Print
.dest
) {
2104 VISIT(c
, expr
, s
->v
.Print
.dest
);
2107 for (i
= 0; i
< n
; i
++) {
2108 expr_ty e
= (expr_ty
)asdl_seq_GET(s
->v
.Print
.values
, i
);
2113 ADDOP(c
, PRINT_ITEM_TO
);
2117 ADDOP(c
, PRINT_ITEM
);
2120 if (s
->v
.Print
.nl
) {
2122 ADDOP(c
, PRINT_NEWLINE_TO
)
2124 ADDOP(c
, PRINT_NEWLINE
)
2132 compiler_if(struct compiler
*c
, stmt_ty s
)
2134 basicblock
*end
, *next
;
2136 assert(s
->kind
== If_kind
);
2137 end
= compiler_new_block(c
);
2140 next
= compiler_new_block(c
);
2143 VISIT(c
, expr
, s
->v
.If
.test
);
2144 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
2146 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
2147 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2148 compiler_use_next_block(c
, next
);
2151 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
2152 compiler_use_next_block(c
, end
);
2157 compiler_for(struct compiler
*c
, stmt_ty s
)
2159 basicblock
*start
, *cleanup
, *end
;
2161 start
= compiler_new_block(c
);
2162 cleanup
= compiler_new_block(c
);
2163 end
= compiler_new_block(c
);
2164 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
2166 ADDOP_JREL(c
, SETUP_LOOP
, end
);
2167 if (!compiler_push_fblock(c
, LOOP
, start
))
2169 VISIT(c
, expr
, s
->v
.For
.iter
);
2171 compiler_use_next_block(c
, start
);
2172 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
2173 VISIT(c
, expr
, s
->v
.For
.target
);
2174 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
2175 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2176 compiler_use_next_block(c
, cleanup
);
2177 ADDOP(c
, POP_BLOCK
);
2178 compiler_pop_fblock(c
, LOOP
, start
);
2179 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
2180 compiler_use_next_block(c
, end
);
2185 compiler_while(struct compiler
*c
, stmt_ty s
)
2187 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
2188 int constant
= expr_constant(s
->v
.While
.test
);
2192 loop
= compiler_new_block(c
);
2193 end
= compiler_new_block(c
);
2194 if (constant
== -1) {
2195 anchor
= compiler_new_block(c
);
2199 if (loop
== NULL
|| end
== NULL
)
2201 if (s
->v
.While
.orelse
) {
2202 orelse
= compiler_new_block(c
);
2209 ADDOP_JREL(c
, SETUP_LOOP
, end
);
2210 compiler_use_next_block(c
, loop
);
2211 if (!compiler_push_fblock(c
, LOOP
, loop
))
2213 if (constant
== -1) {
2214 VISIT(c
, expr
, s
->v
.While
.test
);
2215 ADDOP_JREL(c
, JUMP_IF_FALSE
, anchor
);
2218 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
2219 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
2221 /* XXX should the two POP instructions be in a separate block
2222 if there is no else clause ?
2225 if (constant
== -1) {
2226 compiler_use_next_block(c
, anchor
);
2228 ADDOP(c
, POP_BLOCK
);
2230 compiler_pop_fblock(c
, LOOP
, loop
);
2232 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
2233 compiler_use_next_block(c
, end
);
2239 compiler_continue(struct compiler
*c
)
2241 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
2244 if (!c
->u
->u_nfblocks
)
2245 return compiler_error(c
, LOOP_ERROR_MSG
);
2246 i
= c
->u
->u_nfblocks
- 1;
2247 switch (c
->u
->u_fblock
[i
].fb_type
) {
2249 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
2253 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
)
2256 return compiler_error(c
, LOOP_ERROR_MSG
);
2257 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
2260 return compiler_error(c
,
2261 "'continue' not supported inside 'finally' clause");
2267 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
2273 L: <code for finalbody>
2276 The special instructions use the block stack. Each block
2277 stack entry contains the instruction that created it (here
2278 SETUP_FINALLY), the level of the value stack at the time the
2279 block stack entry was created, and a label (here L).
2282 Pushes the current value stack level and the label
2283 onto the block stack.
2285 Pops en entry from the block stack, and pops the value
2286 stack until its level is the same as indicated on the
2287 block stack. (The label is ignored.)
2289 Pops a variable number of entries from the *value* stack
2290 and re-raises the exception they specify. The number of
2291 entries popped depends on the (pseudo) exception type.
2293 The block stack is unwound when an exception is raised:
2294 when a SETUP_FINALLY entry is found, the exception is pushed
2295 onto the value stack (and the exception condition is cleared),
2296 and the interpreter jumps to the label gotten from the block
2301 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
2303 basicblock
*body
, *end
;
2304 body
= compiler_new_block(c
);
2305 end
= compiler_new_block(c
);
2306 if (body
== NULL
|| end
== NULL
)
2309 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
2310 compiler_use_next_block(c
, body
);
2311 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
2313 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
2314 ADDOP(c
, POP_BLOCK
);
2315 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
2317 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2318 compiler_use_next_block(c
, end
);
2319 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
2321 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
2322 ADDOP(c
, END_FINALLY
);
2323 compiler_pop_fblock(c
, FINALLY_END
, end
);
2329 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2330 (The contents of the value stack is shown in [], with the top
2331 at the right; 'tb' is trace-back info, 'val' the exception's
2332 associated value, and 'exc' the exception.)
2334 Value stack Label Instruction Argument
2340 [tb, val, exc] L1: DUP )
2341 [tb, val, exc, exc] <evaluate E1> )
2342 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2343 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2344 [tb, val, exc, 1] POP )
2346 [tb, val] <assign to V1> (or POP if no V1)
2351 [tb, val, exc, 0] L2: POP
2353 .............................etc.......................
2355 [tb, val, exc, 0] Ln+1: POP
2356 [tb, val, exc] END_FINALLY # re-raise exception
2358 [] L0: <next statement>
2360 Of course, parts are not generated if Vi or Ei is not present.
2363 compiler_try_except(struct compiler
*c
, stmt_ty s
)
2365 basicblock
*body
, *orelse
, *except
, *end
;
2368 body
= compiler_new_block(c
);
2369 except
= compiler_new_block(c
);
2370 orelse
= compiler_new_block(c
);
2371 end
= compiler_new_block(c
);
2372 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
2374 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
2375 compiler_use_next_block(c
, body
);
2376 if (!compiler_push_fblock(c
, EXCEPT
, body
))
2378 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
2379 ADDOP(c
, POP_BLOCK
);
2380 compiler_pop_fblock(c
, EXCEPT
, body
);
2381 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
2382 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
2383 compiler_use_next_block(c
, except
);
2384 for (i
= 0; i
< n
; i
++) {
2385 excepthandler_ty handler
= asdl_seq_GET(
2386 s
->v
.TryExcept
.handlers
, i
);
2387 if (!handler
->type
&& i
< n
-1)
2388 return compiler_error(c
, "default 'except:' must be last");
2389 except
= compiler_new_block(c
);
2392 if (handler
->type
) {
2394 VISIT(c
, expr
, handler
->type
);
2395 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
2396 ADDOP_JREL(c
, JUMP_IF_FALSE
, except
);
2400 if (handler
->name
) {
2401 VISIT(c
, expr
, handler
->name
);
2407 VISIT_SEQ(c
, stmt
, handler
->body
);
2408 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2409 compiler_use_next_block(c
, except
);
2413 ADDOP(c
, END_FINALLY
);
2414 compiler_use_next_block(c
, orelse
);
2415 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
2416 compiler_use_next_block(c
, end
);
2421 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
2423 /* The IMPORT_NAME opcode was already generated. This function
2424 merely needs to bind the result to a name.
2426 If there is a dot in name, we need to split it and emit a
2427 LOAD_ATTR for each name.
2429 const char *src
= PyString_AS_STRING(name
);
2430 const char *dot
= strchr(src
, '.');
2432 /* Consume the base module name to get the first attribute */
2435 /* NB src is only defined when dot != NULL */
2437 dot
= strchr(src
, '.');
2438 attr
= PyString_FromStringAndSize(src
,
2439 dot
? dot
- src
: strlen(src
));
2442 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
2447 return compiler_nameop(c
, asname
, Store
);
2451 compiler_import(struct compiler
*c
, stmt_ty s
)
2453 /* The Import node stores a module name like a.b.c as a single
2454 string. This is convenient for all cases except
2456 where we need to parse that string to extract the individual
2458 XXX Perhaps change the representation to make this case simpler?
2460 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
2462 for (i
= 0; i
< n
; i
++) {
2463 alias_ty alias
= asdl_seq_GET(s
->v
.Import
.names
, i
);
2467 if (c
->c_flags
&& (c
->c_flags
->cf_flags
& CO_FUTURE_ABSIMPORT
))
2468 level
= PyInt_FromLong(0);
2470 level
= PyInt_FromLong(-1);
2475 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2477 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2478 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
2480 if (alias
->asname
) {
2481 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
2486 identifier tmp
= alias
->name
;
2487 const char *base
= PyString_AS_STRING(alias
->name
);
2488 char *dot
= strchr(base
, '.');
2490 tmp
= PyString_FromStringAndSize(base
,
2492 r
= compiler_nameop(c
, tmp
, Store
);
2504 compiler_from_import(struct compiler
*c
, stmt_ty s
)
2506 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
2508 PyObject
*names
= PyTuple_New(n
);
2514 if (s
->v
.ImportFrom
.level
== 0 && c
->c_flags
&&
2515 !(c
->c_flags
->cf_flags
& CO_FUTURE_ABSIMPORT
))
2516 level
= PyInt_FromLong(-1);
2518 level
= PyInt_FromLong(s
->v
.ImportFrom
.level
);
2525 /* build up the names */
2526 for (i
= 0; i
< n
; i
++) {
2527 alias_ty alias
= asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2528 Py_INCREF(alias
->name
);
2529 PyTuple_SET_ITEM(names
, i
, alias
->name
);
2532 if (s
->lineno
> c
->c_future
->ff_lineno
) {
2533 if (!strcmp(PyString_AS_STRING(s
->v
.ImportFrom
.module
),
2537 return compiler_error(c
,
2538 "from __future__ imports must occur "
2539 "at the beginning of the file");
2544 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2546 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
2548 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
2549 for (i
= 0; i
< n
; i
++) {
2550 alias_ty alias
= asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2551 identifier store_name
;
2553 if (i
== 0 && *PyString_AS_STRING(alias
->name
) == '*') {
2555 ADDOP(c
, IMPORT_STAR
);
2559 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
2560 store_name
= alias
->name
;
2562 store_name
= alias
->asname
;
2564 if (!compiler_nameop(c
, store_name
, Store
)) {
2569 /* remove imported module */
2575 compiler_assert(struct compiler
*c
, stmt_ty s
)
2577 static PyObject
*assertion_error
= NULL
;
2580 if (Py_OptimizeFlag
)
2582 if (assertion_error
== NULL
) {
2583 assertion_error
= PyString_FromString("AssertionError");
2584 if (assertion_error
== NULL
)
2587 VISIT(c
, expr
, s
->v
.Assert
.test
);
2588 end
= compiler_new_block(c
);
2591 ADDOP_JREL(c
, JUMP_IF_TRUE
, end
);
2593 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
2594 if (s
->v
.Assert
.msg
) {
2595 VISIT(c
, expr
, s
->v
.Assert
.msg
);
2596 ADDOP_I(c
, RAISE_VARARGS
, 2);
2599 ADDOP_I(c
, RAISE_VARARGS
, 1);
2601 compiler_use_next_block(c
, end
);
2607 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2611 c
->u
->u_lineno
= s
->lineno
;
2612 c
->u
->u_lineno_set
= false;
2614 case FunctionDef_kind
:
2615 return compiler_function(c
, s
);
2617 return compiler_class(c
, s
);
2619 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2620 return compiler_error(c
, "'return' outside function");
2621 if (s
->v
.Return
.value
) {
2622 if (c
->u
->u_ste
->ste_generator
) {
2623 return compiler_error(c
,
2624 "'return' with argument inside generator");
2626 VISIT(c
, expr
, s
->v
.Return
.value
);
2629 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2630 ADDOP(c
, RETURN_VALUE
);
2633 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2636 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2637 VISIT(c
, expr
, s
->v
.Assign
.value
);
2638 for (i
= 0; i
< n
; i
++) {
2642 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2645 case AugAssign_kind
:
2646 return compiler_augassign(c
, s
);
2648 return compiler_print(c
, s
);
2650 return compiler_for(c
, s
);
2652 return compiler_while(c
, s
);
2654 return compiler_if(c
, s
);
2657 if (s
->v
.Raise
.type
) {
2658 VISIT(c
, expr
, s
->v
.Raise
.type
);
2660 if (s
->v
.Raise
.inst
) {
2661 VISIT(c
, expr
, s
->v
.Raise
.inst
);
2663 if (s
->v
.Raise
.tback
) {
2664 VISIT(c
, expr
, s
->v
.Raise
.tback
);
2669 ADDOP_I(c
, RAISE_VARARGS
, n
);
2671 case TryExcept_kind
:
2672 return compiler_try_except(c
, s
);
2673 case TryFinally_kind
:
2674 return compiler_try_finally(c
, s
);
2676 return compiler_assert(c
, s
);
2678 return compiler_import(c
, s
);
2679 case ImportFrom_kind
:
2680 return compiler_from_import(c
, s
);
2682 VISIT(c
, expr
, s
->v
.Exec
.body
);
2683 if (s
->v
.Exec
.globals
) {
2684 VISIT(c
, expr
, s
->v
.Exec
.globals
);
2685 if (s
->v
.Exec
.locals
) {
2686 VISIT(c
, expr
, s
->v
.Exec
.locals
);
2691 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2694 ADDOP(c
, EXEC_STMT
);
2699 VISIT(c
, expr
, s
->v
.Expr
.value
);
2700 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2701 ADDOP(c
, PRINT_EXPR
);
2710 if (!c
->u
->u_nfblocks
)
2711 return compiler_error(c
, "'break' outside loop");
2712 ADDOP(c
, BREAK_LOOP
);
2715 return compiler_continue(c
);
2717 return compiler_with(c
, s
);
2723 unaryop(unaryop_ty op
)
2727 return UNARY_INVERT
;
2731 return UNARY_POSITIVE
;
2733 return UNARY_NEGATIVE
;
2739 binop(struct compiler
*c
, operator_ty op
)
2745 return BINARY_SUBTRACT
;
2747 return BINARY_MULTIPLY
;
2749 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2750 return BINARY_TRUE_DIVIDE
;
2752 return BINARY_DIVIDE
;
2754 return BINARY_MODULO
;
2756 return BINARY_POWER
;
2758 return BINARY_LSHIFT
;
2760 return BINARY_RSHIFT
;
2768 return BINARY_FLOOR_DIVIDE
;
2792 return PyCmp_IS_NOT
;
2796 return PyCmp_NOT_IN
;
2802 inplace_binop(struct compiler
*c
, operator_ty op
)
2808 return INPLACE_SUBTRACT
;
2810 return INPLACE_MULTIPLY
;
2812 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2813 return INPLACE_TRUE_DIVIDE
;
2815 return INPLACE_DIVIDE
;
2817 return INPLACE_MODULO
;
2819 return INPLACE_POWER
;
2821 return INPLACE_LSHIFT
;
2823 return INPLACE_RSHIFT
;
2831 return INPLACE_FLOOR_DIVIDE
;
2833 PyErr_Format(PyExc_SystemError
,
2834 "inplace binary op %d should not be possible", op
);
2839 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2842 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2844 PyObject
*dict
= c
->u
->u_names
;
2846 /* XXX AugStore isn't used anywhere! */
2848 /* First check for assignment to __debug__. Param? */
2849 if ((ctx
== Store
|| ctx
== AugStore
|| ctx
== Del
)
2850 && !strcmp(PyString_AS_STRING(name
), "__debug__")) {
2851 return compiler_error(c
, "can not assign to __debug__");
2854 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2860 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2863 dict
= c
->u
->u_freevars
;
2867 dict
= c
->u
->u_cellvars
;
2871 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2874 case GLOBAL_IMPLICIT
:
2875 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2876 !c
->u
->u_ste
->ste_unoptimized
)
2879 case GLOBAL_EXPLICIT
:
2883 /* scope can be 0 */
2887 /* XXX Leave assert here, but handle __doc__ and the like better */
2888 assert(scope
|| PyString_AS_STRING(name
)[0] == '_');
2893 case Load
: op
= LOAD_DEREF
; break;
2894 case Store
: op
= STORE_DEREF
; break;
2899 PyErr_Format(PyExc_SyntaxError
,
2900 "can not delete variable '%s' referenced "
2902 PyString_AS_STRING(name
));
2907 PyErr_SetString(PyExc_SystemError
,
2908 "param invalid for deref variable");
2914 case Load
: op
= LOAD_FAST
; break;
2915 case Store
: op
= STORE_FAST
; break;
2916 case Del
: op
= DELETE_FAST
; break;
2922 PyErr_SetString(PyExc_SystemError
,
2923 "param invalid for local variable");
2926 ADDOP_O(c
, op
, mangled
, varnames
);
2931 case Load
: op
= LOAD_GLOBAL
; break;
2932 case Store
: op
= STORE_GLOBAL
; break;
2933 case Del
: op
= DELETE_GLOBAL
; break;
2939 PyErr_SetString(PyExc_SystemError
,
2940 "param invalid for global variable");
2946 case Load
: op
= LOAD_NAME
; break;
2947 case Store
: op
= STORE_NAME
; break;
2948 case Del
: op
= DELETE_NAME
; break;
2954 PyErr_SetString(PyExc_SystemError
,
2955 "param invalid for name variable");
2962 arg
= compiler_add_o(c
, dict
, mangled
);
2966 return compiler_addop_i(c
, op
, arg
);
2970 compiler_boolop(struct compiler
*c
, expr_ty e
)
2976 assert(e
->kind
== BoolOp_kind
);
2977 if (e
->v
.BoolOp
.op
== And
)
2978 jumpi
= JUMP_IF_FALSE
;
2980 jumpi
= JUMP_IF_TRUE
;
2981 end
= compiler_new_block(c
);
2984 s
= e
->v
.BoolOp
.values
;
2985 n
= asdl_seq_LEN(s
) - 1;
2986 for (i
= 0; i
< n
; ++i
) {
2987 VISIT(c
, expr
, asdl_seq_GET(s
, i
));
2988 ADDOP_JREL(c
, jumpi
, end
);
2991 VISIT(c
, expr
, asdl_seq_GET(s
, n
));
2992 compiler_use_next_block(c
, end
);
2997 compiler_list(struct compiler
*c
, expr_ty e
)
2999 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
3000 if (e
->v
.List
.ctx
== Store
) {
3001 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
3003 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
3004 if (e
->v
.List
.ctx
== Load
) {
3005 ADDOP_I(c
, BUILD_LIST
, n
);
3011 compiler_tuple(struct compiler
*c
, expr_ty e
)
3013 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
3014 if (e
->v
.Tuple
.ctx
== Store
) {
3015 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
3017 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
3018 if (e
->v
.Tuple
.ctx
== Load
) {
3019 ADDOP_I(c
, BUILD_TUPLE
, n
);
3025 compiler_compare(struct compiler
*c
, expr_ty e
)
3028 basicblock
*cleanup
= NULL
;
3030 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
3031 VISIT(c
, expr
, e
->v
.Compare
.left
);
3032 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
3035 cleanup
= compiler_new_block(c
);
3036 if (cleanup
== NULL
)
3038 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
3040 for (i
= 1; i
< n
; i
++) {
3042 ADDOP(c
, ROT_THREE
);
3043 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
3044 ADDOP_I(c
, COMPARE_OP
,
3045 cmpop((cmpop_ty
)asdl_seq_GET(e
->v
.Compare
.ops
, i
- 1)));
3046 ADDOP_JREL(c
, JUMP_IF_FALSE
, cleanup
);
3050 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
3052 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
3053 ADDOP_I(c
, COMPARE_OP
,
3054 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
3055 cmpop((cmpop_ty
)asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1)));
3057 basicblock
*end
= compiler_new_block(c
);
3060 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
3061 compiler_use_next_block(c
, cleanup
);
3064 compiler_use_next_block(c
, end
);
3070 compiler_call(struct compiler
*c
, expr_ty e
)
3074 VISIT(c
, expr
, e
->v
.Call
.func
);
3075 n
= asdl_seq_LEN(e
->v
.Call
.args
);
3076 VISIT_SEQ(c
, expr
, e
->v
.Call
.args
);
3077 if (e
->v
.Call
.keywords
) {
3078 VISIT_SEQ(c
, keyword
, e
->v
.Call
.keywords
);
3079 n
|= asdl_seq_LEN(e
->v
.Call
.keywords
) << 8;
3081 if (e
->v
.Call
.starargs
) {
3082 VISIT(c
, expr
, e
->v
.Call
.starargs
);
3085 if (e
->v
.Call
.kwargs
) {
3086 VISIT(c
, expr
, e
->v
.Call
.kwargs
);
3091 ADDOP_I(c
, CALL_FUNCTION
, n
);
3094 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
3097 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
3100 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
3107 compiler_listcomp_generator(struct compiler
*c
, PyObject
*tmpname
,
3108 asdl_seq
*generators
, int gen_index
,
3111 /* generate code for the iterator, then each of the ifs,
3112 and then write to the element */
3115 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
3118 start
= compiler_new_block(c
);
3119 skip
= compiler_new_block(c
);
3120 if_cleanup
= compiler_new_block(c
);
3121 anchor
= compiler_new_block(c
);
3123 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
3127 l
= asdl_seq_GET(generators
, gen_index
);
3128 VISIT(c
, expr
, l
->iter
);
3130 compiler_use_next_block(c
, start
);
3131 ADDOP_JREL(c
, FOR_ITER
, anchor
);
3133 VISIT(c
, expr
, l
->target
);
3135 /* XXX this needs to be cleaned up...a lot! */
3136 n
= asdl_seq_LEN(l
->ifs
);
3137 for (i
= 0; i
< n
; i
++) {
3138 expr_ty e
= asdl_seq_GET(l
->ifs
, i
);
3140 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
3145 if (++gen_index
< asdl_seq_LEN(generators
))
3146 if (!compiler_listcomp_generator(c
, tmpname
,
3147 generators
, gen_index
, elt
))
3150 /* only append after the last for generator */
3151 if (gen_index
>= asdl_seq_LEN(generators
)) {
3152 if (!compiler_nameop(c
, tmpname
, Load
))
3154 VISIT(c
, expr
, elt
);
3155 ADDOP(c
, LIST_APPEND
);
3157 compiler_use_next_block(c
, skip
);
3159 for (i
= 0; i
< n
; i
++) {
3160 ADDOP_I(c
, JUMP_FORWARD
, 1);
3162 compiler_use_next_block(c
, if_cleanup
);
3165 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
3166 compiler_use_next_block(c
, anchor
);
3167 /* delete the append method added to locals */
3169 if (!compiler_nameop(c
, tmpname
, Del
))
3176 compiler_listcomp(struct compiler
*c
, expr_ty e
)
3180 static identifier append
;
3181 asdl_seq
*generators
= e
->v
.ListComp
.generators
;
3183 assert(e
->kind
== ListComp_kind
);
3185 append
= PyString_InternFromString("append");
3189 tmp
= compiler_new_tmpname(c
);
3192 ADDOP_I(c
, BUILD_LIST
, 0);
3194 if (compiler_nameop(c
, tmp
, Store
))
3195 rc
= compiler_listcomp_generator(c
, tmp
, generators
, 0,
3202 compiler_genexp_generator(struct compiler
*c
,
3203 asdl_seq
*generators
, int gen_index
,
3206 /* generate code for the iterator, then each of the ifs,
3207 and then write to the element */
3209 comprehension_ty ge
;
3210 basicblock
*start
, *anchor
, *skip
, *if_cleanup
, *end
;
3213 start
= compiler_new_block(c
);
3214 skip
= compiler_new_block(c
);
3215 if_cleanup
= compiler_new_block(c
);
3216 anchor
= compiler_new_block(c
);
3217 end
= compiler_new_block(c
);
3219 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
3220 anchor
== NULL
|| end
== NULL
)
3223 ge
= asdl_seq_GET(generators
, gen_index
);
3224 ADDOP_JREL(c
, SETUP_LOOP
, end
);
3225 if (!compiler_push_fblock(c
, LOOP
, start
))
3228 if (gen_index
== 0) {
3229 /* Receive outermost iter as an implicit argument */
3230 c
->u
->u_argcount
= 1;
3231 ADDOP_I(c
, LOAD_FAST
, 0);
3234 /* Sub-iter - calculate on the fly */
3235 VISIT(c
, expr
, ge
->iter
);
3238 compiler_use_next_block(c
, start
);
3239 ADDOP_JREL(c
, FOR_ITER
, anchor
);
3241 VISIT(c
, expr
, ge
->target
);
3243 /* XXX this needs to be cleaned up...a lot! */
3244 n
= asdl_seq_LEN(ge
->ifs
);
3245 for (i
= 0; i
< n
; i
++) {
3246 expr_ty e
= asdl_seq_GET(ge
->ifs
, i
);
3248 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
3253 if (++gen_index
< asdl_seq_LEN(generators
))
3254 if (!compiler_genexp_generator(c
, generators
, gen_index
, elt
))
3257 /* only append after the last 'for' generator */
3258 if (gen_index
>= asdl_seq_LEN(generators
)) {
3259 VISIT(c
, expr
, elt
);
3260 ADDOP(c
, YIELD_VALUE
);
3263 compiler_use_next_block(c
, skip
);
3265 for (i
= 0; i
< n
; i
++) {
3266 ADDOP_I(c
, JUMP_FORWARD
, 1);
3268 compiler_use_next_block(c
, if_cleanup
);
3272 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
3273 compiler_use_next_block(c
, anchor
);
3274 ADDOP(c
, POP_BLOCK
);
3275 compiler_pop_fblock(c
, LOOP
, start
);
3276 compiler_use_next_block(c
, end
);
3282 compiler_genexp(struct compiler
*c
, expr_ty e
)
3284 static identifier name
;
3286 expr_ty outermost_iter
= ((comprehension_ty
)
3287 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
,
3291 name
= PyString_FromString("<genexpr>");
3296 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
3298 compiler_genexp_generator(c
, e
->v
.GeneratorExp
.generators
, 0,
3299 e
->v
.GeneratorExp
.elt
);
3300 co
= assemble(c
, 1);
3301 compiler_exit_scope(c
);
3305 compiler_make_closure(c
, co
, 0);
3308 VISIT(c
, expr
, outermost_iter
);
3310 ADDOP_I(c
, CALL_FUNCTION
, 1);
3316 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
3318 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
3319 VISIT(c
, expr
, k
->value
);
3323 /* Test whether expression is constant. For constants, report
3324 whether they are true or false.
3326 Return values: 1 for true, 0 for false, -1 for non-constant.
3330 expr_constant(expr_ty e
)
3334 return PyObject_IsTrue(e
->v
.Num
.n
);
3336 return PyObject_IsTrue(e
->v
.Str
.s
);
3343 Implements the with statement from PEP 343.
3345 The semantics outlined in that PEP are as follows:
3350 It is implemented roughly as:
3352 context = (EXPR).__context__()
3353 exit = context.__exit__ # not calling it
3354 value = context.__enter__()
3356 VAR = value # if VAR present in the syntax
3359 if an exception was raised:
3360 exc = copy of (exception, instance, traceback)
3362 exc = (None, None, None)
3366 compiler_with(struct compiler
*c
, stmt_ty s
)
3368 static identifier context_attr
, enter_attr
, exit_attr
;
3369 basicblock
*block
, *finally
;
3370 identifier tmpexit
, tmpvalue
= NULL
;
3372 assert(s
->kind
== With_kind
);
3374 if (!context_attr
) {
3375 context_attr
= PyString_InternFromString("__context__");
3380 enter_attr
= PyString_InternFromString("__enter__");
3385 exit_attr
= PyString_InternFromString("__exit__");
3390 block
= compiler_new_block(c
);
3391 finally
= compiler_new_block(c
);
3392 if (!block
|| !finally
)
3395 /* Create a temporary variable to hold context.__exit__ */
3396 tmpexit
= compiler_new_tmpname(c
);
3397 if (tmpexit
== NULL
)
3399 PyArena_AddPyObject(c
->c_arena
, tmpexit
);
3401 if (s
->v
.With
.optional_vars
) {
3402 /* Create a temporary variable to hold context.__enter__().
3403 We need to do this rather than preserving it on the stack
3404 because SETUP_FINALLY remembers the stack level.
3405 We need to do the assignment *inside* the try/finally
3406 so that context.__exit__() is called when the assignment
3407 fails. But we need to call context.__enter__() *before*
3408 the try/finally so that if it fails we won't call
3411 tmpvalue
= compiler_new_tmpname(c
);
3412 if (tmpvalue
== NULL
)
3414 PyArena_AddPyObject(c
->c_arena
, tmpvalue
);
3417 /* Evaluate (EXPR).__context__() */
3418 VISIT(c
, expr
, s
->v
.With
.context_expr
);
3419 ADDOP_O(c
, LOAD_ATTR
, context_attr
, names
);
3420 ADDOP_I(c
, CALL_FUNCTION
, 0);
3422 /* Squirrel away context.__exit__ */
3424 ADDOP_O(c
, LOAD_ATTR
, exit_attr
, names
);
3425 if (!compiler_nameop(c
, tmpexit
, Store
))
3428 /* Call context.__enter__() */
3429 ADDOP_O(c
, LOAD_ATTR
, enter_attr
, names
);
3430 ADDOP_I(c
, CALL_FUNCTION
, 0);
3432 if (s
->v
.With
.optional_vars
) {
3433 /* Store it in tmpvalue */
3434 if (!compiler_nameop(c
, tmpvalue
, Store
))
3438 /* Discard result from context.__enter__() */
3442 /* Start the try block */
3443 ADDOP_JREL(c
, SETUP_FINALLY
, finally
);
3445 compiler_use_next_block(c
, block
);
3446 if (!compiler_push_fblock(c
, FINALLY_TRY
, block
)) {
3450 if (s
->v
.With
.optional_vars
) {
3451 /* Bind saved result of context.__enter__() to VAR */
3452 if (!compiler_nameop(c
, tmpvalue
, Load
) ||
3453 !compiler_nameop(c
, tmpvalue
, Del
))
3455 VISIT(c
, expr
, s
->v
.With
.optional_vars
);
3459 VISIT_SEQ(c
, stmt
, s
->v
.With
.body
);
3461 /* End of try block; start the finally block */
3462 ADDOP(c
, POP_BLOCK
);
3463 compiler_pop_fblock(c
, FINALLY_TRY
, block
);
3465 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3466 compiler_use_next_block(c
, finally
);
3467 if (!compiler_push_fblock(c
, FINALLY_END
, finally
))
3470 /* Finally block starts; push tmpexit and issue our magic opcode. */
3471 if (!compiler_nameop(c
, tmpexit
, Load
) ||
3472 !compiler_nameop(c
, tmpexit
, Del
))
3474 ADDOP(c
, WITH_CLEANUP
);
3475 ADDOP_I(c
, CALL_FUNCTION
, 3);
3478 /* Finally block ends. */
3479 ADDOP(c
, END_FINALLY
);
3480 compiler_pop_fblock(c
, FINALLY_END
, finally
);
3485 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
3489 if (e
->lineno
> c
->u
->u_lineno
) {
3490 c
->u
->u_lineno
= e
->lineno
;
3491 c
->u
->u_lineno_set
= false;
3495 return compiler_boolop(c
, e
);
3497 VISIT(c
, expr
, e
->v
.BinOp
.left
);
3498 VISIT(c
, expr
, e
->v
.BinOp
.right
);
3499 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
3502 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
3503 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
3506 return compiler_lambda(c
, e
);
3508 return compiler_ifexp(c
, e
);
3510 /* XXX get rid of arg? */
3511 ADDOP_I(c
, BUILD_MAP
, 0);
3512 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
3513 /* We must arrange things just right for STORE_SUBSCR.
3514 It wants the stack to look like (value) (dict) (key) */
3515 for (i
= 0; i
< n
; i
++) {
3517 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Dict
.values
, i
));
3519 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Dict
.keys
, i
));
3520 ADDOP(c
, STORE_SUBSCR
);
3524 return compiler_listcomp(c
, e
);
3525 case GeneratorExp_kind
:
3526 return compiler_genexp(c
, e
);
3528 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
3529 return compiler_error(c
, "'yield' outside function");
3531 for (i = 0; i < c->u->u_nfblocks; i++) {
3532 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3533 return compiler_error(
3534 c, "'yield' not allowed in a 'try' "
3535 "block with a 'finally' clause");
3538 if (e
->v
.Yield
.value
) {
3539 VISIT(c
, expr
, e
->v
.Yield
.value
);
3542 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3544 ADDOP(c
, YIELD_VALUE
);
3547 return compiler_compare(c
, e
);
3549 return compiler_call(c
, e
);
3551 VISIT(c
, expr
, e
->v
.Repr
.value
);
3552 ADDOP(c
, UNARY_CONVERT
);
3555 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
3558 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
3560 /* The following exprs can be assignment targets. */
3561 case Attribute_kind
:
3562 if (e
->v
.Attribute
.ctx
!= AugStore
)
3563 VISIT(c
, expr
, e
->v
.Attribute
.value
);
3564 switch (e
->v
.Attribute
.ctx
) {
3567 /* Fall through to load */
3569 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
3573 /* Fall through to save */
3575 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
3578 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
3582 PyErr_SetString(PyExc_SystemError
,
3583 "param invalid in attribute expression");
3587 case Subscript_kind
:
3588 switch (e
->v
.Subscript
.ctx
) {
3590 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3591 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
3594 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3595 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
3598 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
3601 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3602 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
3605 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3606 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
3610 PyErr_SetString(PyExc_SystemError
,
3611 "param invalid in subscript expression");
3616 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3617 /* child nodes of List and Tuple will have expr_context set */
3619 return compiler_list(c
, e
);
3621 return compiler_tuple(c
, e
);
3627 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3629 expr_ty e
= s
->v
.AugAssign
.target
;
3632 assert(s
->kind
== AugAssign_kind
);
3635 case Attribute_kind
:
3636 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3637 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3640 VISIT(c
, expr
, auge
);
3641 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3642 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3643 auge
->v
.Attribute
.ctx
= AugStore
;
3644 VISIT(c
, expr
, auge
);
3646 case Subscript_kind
:
3647 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3648 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3651 VISIT(c
, expr
, auge
);
3652 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3653 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3654 auge
->v
.Subscript
.ctx
= AugStore
;
3655 VISIT(c
, expr
, auge
);
3658 VISIT(c
, expr
, s
->v
.AugAssign
.target
);
3659 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3660 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3661 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3663 PyErr_Format(PyExc_SystemError
,
3664 "invalid node type (%d) for augmented assignment",
3672 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3674 struct fblockinfo
*f
;
3675 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
)
3677 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3684 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3686 struct compiler_unit
*u
= c
->u
;
3687 assert(u
->u_nfblocks
> 0);
3689 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3690 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3693 /* Raises a SyntaxError and returns 0.
3694 If something goes wrong, a different exception may be raised.
3698 compiler_error(struct compiler
*c
, const char *errstr
)
3701 PyObject
*u
= NULL
, *v
= NULL
;
3703 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3708 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3712 v
= Py_BuildValue("(zO)", errstr
, u
);
3715 PyErr_SetObject(PyExc_SyntaxError
, v
);
3724 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3725 expr_context_ty ctx
)
3729 /* XXX this code is duplicated */
3731 case AugLoad
: /* fall through to Load */
3732 case Load
: op
= BINARY_SUBSCR
; break;
3733 case AugStore
:/* fall through to Store */
3734 case Store
: op
= STORE_SUBSCR
; break;
3735 case Del
: op
= DELETE_SUBSCR
; break;
3737 PyErr_Format(PyExc_SystemError
,
3738 "invalid %s kind %d in subscript\n",
3742 if (ctx
== AugLoad
) {
3743 ADDOP_I(c
, DUP_TOPX
, 2);
3745 else if (ctx
== AugStore
) {
3746 ADDOP(c
, ROT_THREE
);
3753 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3756 assert(s
->kind
== Slice_kind
);
3758 /* only handles the cases where BUILD_SLICE is emitted */
3759 if (s
->v
.Slice
.lower
) {
3760 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3763 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3766 if (s
->v
.Slice
.upper
) {
3767 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3770 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3773 if (s
->v
.Slice
.step
) {
3775 VISIT(c
, expr
, s
->v
.Slice
.step
);
3777 ADDOP_I(c
, BUILD_SLICE
, n
);
3782 compiler_simple_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3784 int op
= 0, slice_offset
= 0, stack_count
= 0;
3786 assert(s
->v
.Slice
.step
== NULL
);
3787 if (s
->v
.Slice
.lower
) {
3790 if (ctx
!= AugStore
)
3791 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3793 if (s
->v
.Slice
.upper
) {
3796 if (ctx
!= AugStore
)
3797 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3800 if (ctx
== AugLoad
) {
3801 switch (stack_count
) {
3802 case 0: ADDOP(c
, DUP_TOP
); break;
3803 case 1: ADDOP_I(c
, DUP_TOPX
, 2); break;
3804 case 2: ADDOP_I(c
, DUP_TOPX
, 3); break;
3807 else if (ctx
== AugStore
) {
3808 switch (stack_count
) {
3809 case 0: ADDOP(c
, ROT_TWO
); break;
3810 case 1: ADDOP(c
, ROT_THREE
); break;
3811 case 2: ADDOP(c
, ROT_FOUR
); break;
3816 case AugLoad
: /* fall through to Load */
3817 case Load
: op
= SLICE
; break;
3818 case AugStore
:/* fall through to Store */
3819 case Store
: op
= STORE_SLICE
; break;
3820 case Del
: op
= DELETE_SLICE
; break;
3823 PyErr_SetString(PyExc_SystemError
,
3824 "param invalid in simple slice");
3828 ADDOP(c
, op
+ slice_offset
);
3833 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3834 expr_context_ty ctx
)
3838 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3841 return compiler_slice(c
, s
, ctx
);
3843 VISIT(c
, expr
, s
->v
.Index
.value
);
3847 PyErr_SetString(PyExc_SystemError
,
3848 "extended slice invalid in nested slice");
3856 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3860 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3863 if (!s
->v
.Slice
.step
)
3864 return compiler_simple_slice(c
, s
, ctx
);
3865 if (!compiler_slice(c
, s
, ctx
))
3867 if (ctx
== AugLoad
) {
3868 ADDOP_I(c
, DUP_TOPX
, 2);
3870 else if (ctx
== AugStore
) {
3871 ADDOP(c
, ROT_THREE
);
3873 return compiler_handle_subscr(c
, "slice", ctx
);
3874 case ExtSlice_kind
: {
3875 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3876 for (i
= 0; i
< n
; i
++) {
3877 slice_ty sub
= asdl_seq_GET(s
->v
.ExtSlice
.dims
, i
);
3878 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3881 ADDOP_I(c
, BUILD_TUPLE
, n
);
3882 return compiler_handle_subscr(c
, "extended slice", ctx
);
3885 if (ctx
!= AugStore
)
3886 VISIT(c
, expr
, s
->v
.Index
.value
);
3887 return compiler_handle_subscr(c
, "index", ctx
);
3889 PyErr_Format(PyExc_SystemError
,
3890 "invalid slice %d", s
->kind
);
3896 /* do depth-first search of basic block graph, starting with block.
3897 post records the block indices in post-order.
3899 XXX must handle implicit jumps from one block to next
3903 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3906 struct instr
*instr
= NULL
;
3911 if (b
->b_next
!= NULL
)
3912 dfs(c
, b
->b_next
, a
);
3913 for (i
= 0; i
< b
->b_iused
; i
++) {
3914 instr
= &b
->b_instr
[i
];
3915 if (instr
->i_jrel
|| instr
->i_jabs
)
3916 dfs(c
, instr
->i_target
, a
);
3918 a
->a_postorder
[a
->a_nblocks
++] = b
;
3922 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3925 struct instr
*instr
;
3926 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3929 b
->b_startdepth
= depth
;
3930 for (i
= 0; i
< b
->b_iused
; i
++) {
3931 instr
= &b
->b_instr
[i
];
3932 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3933 if (depth
> maxdepth
)
3935 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3936 if (instr
->i_jrel
|| instr
->i_jabs
) {
3937 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3939 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3940 instr
->i_opcode
== JUMP_FORWARD
) {
3941 goto out
; /* remaining code is dead */
3946 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3952 /* Find the flow path that needs the largest stack. We assume that
3953 * cycles in the flow graph have no net effect on the stack depth.
3956 stackdepth(struct compiler
*c
)
3958 basicblock
*b
, *entryblock
;
3960 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3962 b
->b_startdepth
= INT_MIN
;
3965 return stackdepth_walk(c
, entryblock
, 0, 0);
3969 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3971 memset(a
, 0, sizeof(struct assembler
));
3972 a
->a_lineno
= firstlineno
;
3973 a
->a_bytecode
= PyString_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3976 a
->a_lnotab
= PyString_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3979 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3980 sizeof(basicblock
*) * nblocks
);
3981 if (!a
->a_postorder
) {
3989 assemble_free(struct assembler
*a
)
3991 Py_XDECREF(a
->a_bytecode
);
3992 Py_XDECREF(a
->a_lnotab
);
3994 PyObject_Free(a
->a_postorder
);
3997 /* Return the size of a basic block in bytes. */
4000 instrsize(struct instr
*instr
)
4002 if (!instr
->i_hasarg
)
4004 if (instr
->i_oparg
> 0xffff)
4010 blocksize(basicblock
*b
)
4015 for (i
= 0; i
< b
->b_iused
; i
++)
4016 size
+= instrsize(&b
->b_instr
[i
]);
4020 /* All about a_lnotab.
4022 c_lnotab is an array of unsigned bytes disguised as a Python string.
4023 It is used to map bytecode offsets to source code line #s (when needed
4026 The array is conceptually a list of
4027 (bytecode offset increment, line number increment)
4028 pairs. The details are important and delicate, best illustrated by example:
4030 byte code offset source code line number
4037 The first trick is that these numbers aren't stored, only the increments
4038 from one row to the next (this doesn't really work, but it's a start):
4040 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
4042 The second trick is that an unsigned byte can't hold negative values, or
4043 values larger than 255, so (a) there's a deep assumption that byte code
4044 offsets and their corresponding line #s both increase monotonically, and (b)
4045 if at least one column jumps by more than 255 from one row to the next, more
4046 than one pair is written to the table. In case #b, there's no way to know
4047 from looking at the table later how many were written. That's the delicate
4048 part. A user of c_lnotab desiring to find the source line number
4049 corresponding to a bytecode address A should do something like this
4052 for addr_incr, line_incr in c_lnotab:
4058 In order for this to work, when the addr field increments by more than 255,
4059 the line # increment in each pair generated must be 0 until the remaining addr
4060 increment is < 256. So, in the example above, com_set_lineno should not (as
4061 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
4062 255, 0, 45, 255, 0, 45.
4066 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
4068 int d_bytecode
, d_lineno
;
4072 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
4073 d_lineno
= i
->i_lineno
- a
->a_lineno
;
4075 assert(d_bytecode
>= 0);
4076 assert(d_lineno
>= 0);
4081 if (d_bytecode
> 255) {
4082 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
4083 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
4084 len
= PyString_GET_SIZE(a
->a_lnotab
);
4085 if (nbytes
>= len
) {
4086 if (len
* 2 < nbytes
)
4090 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
4093 lnotab
= PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
4094 for (j
= 0; j
< ncodes
; j
++) {
4098 d_bytecode
-= ncodes
* 255;
4099 a
->a_lnotab_off
+= ncodes
* 2;
4101 assert(d_bytecode
<= 255);
4102 if (d_lineno
> 255) {
4103 int j
, nbytes
, ncodes
= d_lineno
/ 255;
4104 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
4105 len
= PyString_GET_SIZE(a
->a_lnotab
);
4106 if (nbytes
>= len
) {
4107 if (len
* 2 < nbytes
)
4111 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
4114 lnotab
= PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
4116 *lnotab
++ = d_bytecode
;
4118 for (j
= 1; j
< ncodes
; j
++) {
4122 d_lineno
-= ncodes
* 255;
4123 a
->a_lnotab_off
+= ncodes
* 2;
4126 len
= PyString_GET_SIZE(a
->a_lnotab
);
4127 if (a
->a_lnotab_off
+ 2 >= len
) {
4128 if (_PyString_Resize(&a
->a_lnotab
, len
* 2) < 0)
4131 lnotab
= PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
4133 a
->a_lnotab_off
+= 2;
4135 *lnotab
++ = d_bytecode
;
4136 *lnotab
++ = d_lineno
;
4138 else { /* First line of a block; def stmt, etc. */
4140 *lnotab
++ = d_lineno
;
4142 a
->a_lineno
= i
->i_lineno
;
4143 a
->a_lineno_off
= a
->a_offset
;
4148 Extend the bytecode with a new instruction.
4149 Update lnotab if necessary.
4153 assemble_emit(struct assembler
*a
, struct instr
*i
)
4155 int size
, arg
= 0, ext
= 0;
4156 int len
= PyString_GET_SIZE(a
->a_bytecode
);
4159 size
= instrsize(i
);
4164 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
4166 if (a
->a_offset
+ size
>= len
) {
4167 if (_PyString_Resize(&a
->a_bytecode
, len
* 2) < 0)
4170 code
= PyString_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
4171 a
->a_offset
+= size
;
4173 assert(i
->i_hasarg
);
4174 *code
++ = (char)EXTENDED_ARG
;
4175 *code
++ = ext
& 0xff;
4179 *code
++ = i
->i_opcode
;
4181 assert(size
== 3 || size
== 6);
4182 *code
++ = arg
& 0xff;
4189 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
4192 int bsize
, totsize
, extended_arg_count
, last_extended_arg_count
= 0;
4195 /* Compute the size of each block and fixup jump args.
4196 Replace block pointer with position in bytecode. */
4199 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
4200 b
= a
->a_postorder
[i
];
4201 bsize
= blocksize(b
);
4202 b
->b_offset
= totsize
;
4205 extended_arg_count
= 0;
4206 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
4207 bsize
= b
->b_offset
;
4208 for (i
= 0; i
< b
->b_iused
; i
++) {
4209 struct instr
*instr
= &b
->b_instr
[i
];
4210 /* Relative jumps are computed relative to
4211 the instruction pointer after fetching
4212 the jump instruction.
4214 bsize
+= instrsize(instr
);
4216 instr
->i_oparg
= instr
->i_target
->b_offset
;
4217 else if (instr
->i_jrel
) {
4218 int delta
= instr
->i_target
->b_offset
- bsize
;
4219 instr
->i_oparg
= delta
;
4223 if (instr
->i_oparg
> 0xffff)
4224 extended_arg_count
++;
4228 /* XXX: This is an awful hack that could hurt performance, but
4229 on the bright side it should work until we come up
4230 with a better solution.
4232 In the meantime, should the goto be dropped in favor
4235 The issue is that in the first loop blocksize() is called
4236 which calls instrsize() which requires i_oparg be set
4237 appropriately. There is a bootstrap problem because
4238 i_oparg is calculated in the second loop above.
4240 So we loop until we stop seeing new EXTENDED_ARGs.
4241 The only EXTENDED_ARGs that could be popping up are
4242 ones in jump instructions. So this should converge
4245 if (last_extended_arg_count
!= extended_arg_count
) {
4246 last_extended_arg_count
= extended_arg_count
;
4252 dict_keys_inorder(PyObject
*dict
, int offset
)
4254 PyObject
*tuple
, *k
, *v
;
4255 Py_ssize_t i
, pos
= 0, size
= PyDict_Size(dict
);
4257 tuple
= PyTuple_New(size
);
4260 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
4261 i
= PyInt_AS_LONG(v
);
4262 k
= PyTuple_GET_ITEM(k
, 0);
4264 assert((i
- offset
) < size
);
4265 assert((i
- offset
) >= 0);
4266 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
4272 compute_code_flags(struct compiler
*c
)
4274 PySTEntryObject
*ste
= c
->u
->u_ste
;
4276 if (ste
->ste_type
!= ModuleBlock
)
4277 flags
|= CO_NEWLOCALS
;
4278 if (ste
->ste_type
== FunctionBlock
) {
4279 if (!ste
->ste_unoptimized
)
4280 flags
|= CO_OPTIMIZED
;
4281 if (ste
->ste_nested
)
4283 if (ste
->ste_generator
)
4284 flags
|= CO_GENERATOR
;
4286 if (ste
->ste_varargs
)
4287 flags
|= CO_VARARGS
;
4288 if (ste
->ste_varkeywords
)
4289 flags
|= CO_VARKEYWORDS
;
4290 if (ste
->ste_generator
)
4291 flags
|= CO_GENERATOR
;
4293 /* (Only) inherit compilerflags in PyCF_MASK */
4294 flags
|= (c
->c_flags
->cf_flags
& PyCF_MASK
);
4296 n
= PyDict_Size(c
->u
->u_freevars
);
4300 n
= PyDict_Size(c
->u
->u_cellvars
);
4311 static PyCodeObject
*
4312 makecode(struct compiler
*c
, struct assembler
*a
)
4315 PyCodeObject
*co
= NULL
;
4316 PyObject
*consts
= NULL
;
4317 PyObject
*names
= NULL
;
4318 PyObject
*varnames
= NULL
;
4319 PyObject
*filename
= NULL
;
4320 PyObject
*name
= NULL
;
4321 PyObject
*freevars
= NULL
;
4322 PyObject
*cellvars
= NULL
;
4323 PyObject
*bytecode
= NULL
;
4326 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
4329 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
4332 names
= dict_keys_inorder(c
->u
->u_names
, 0);
4333 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
4334 if (!consts
|| !names
|| !varnames
)
4337 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
4340 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
4343 filename
= PyString_FromString(c
->c_filename
);
4347 nlocals
= PyDict_Size(c
->u
->u_varnames
);
4348 flags
= compute_code_flags(c
);
4352 bytecode
= optimize_code(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
4356 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
4362 co
= PyCode_New(c
->u
->u_argcount
, nlocals
, stackdepth(c
), flags
,
4363 bytecode
, consts
, names
, varnames
,
4365 filename
, c
->u
->u_name
,
4366 c
->u
->u_firstlineno
,
4371 Py_XDECREF(varnames
);
4372 Py_XDECREF(filename
);
4374 Py_XDECREF(freevars
);
4375 Py_XDECREF(cellvars
);
4376 Py_XDECREF(bytecode
);
4380 static PyCodeObject
*
4381 assemble(struct compiler
*c
, int addNone
)
4383 basicblock
*b
, *entryblock
;
4386 PyCodeObject
*co
= NULL
;
4388 /* Make sure every block that falls off the end returns None.
4389 XXX NEXT_BLOCK() isn't quite right, because if the last
4390 block ends with a jump or return b_next shouldn't set.
4392 if (!c
->u
->u_curblock
->b_return
) {
4395 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
4396 ADDOP(c
, RETURN_VALUE
);
4401 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
4406 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
4408 dfs(c
, entryblock
, &a
);
4410 /* Can't modify the bytecode after computing jump offsets. */
4411 assemble_jump_offsets(&a
, c
);
4413 /* Emit code in reverse postorder from dfs. */
4414 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
4415 b
= a
.a_postorder
[i
];
4416 for (j
= 0; j
< b
->b_iused
; j
++)
4417 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
4421 if (_PyString_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
4423 if (_PyString_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
4426 co
= makecode(c
, &a
);