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 encounter
17 * a problem. So don't invoke them when there is memory which needs to be
18 * released. Code blocks are OK, as the compiler structure takes care of
24 #include "Python-ast.h"
33 int Py_OptimizeFlag
= 0;
38 character encodings aren't handled
40 ref leaks in interpreter when press return on empty line
42 opcode_stack_effect() function should be reviewed since stack depth bugs
43 could be really hard to find later.
45 Dead code is being generated (i.e. after unconditional jumps).
48 #define DEFAULT_BLOCK_SIZE 16
49 #define DEFAULT_BLOCKS 8
50 #define DEFAULT_CODE_SIZE 128
51 #define DEFAULT_LNOTAB_SIZE 16
56 unsigned i_hasarg
: 1;
57 unsigned char i_opcode
;
59 struct basicblock_
*i_target
; /* target block (if jump instruction) */
63 typedef struct basicblock_
{
64 /* next block in the list of blocks for a unit (don't confuse with
66 struct basicblock_
*b_list
;
67 /* number of instructions used */
69 /* length of instruction array (b_instr) */
71 /* pointer to an array of instructions, initially NULL */
72 struct instr
*b_instr
;
73 /* If b_next is non-NULL, it is a pointer to the next
74 block reached by normal control flow. */
75 struct basicblock_
*b_next
;
76 /* b_seen is used to perform a DFS of basicblocks. */
78 /* b_return is true if a RETURN_VALUE opcode is inserted. */
79 unsigned b_return
: 1;
80 /* depth of stack upon entry of block, computed by stackdepth() */
82 /* instruction offset for block, computed by assemble_jump_offsets() */
86 /* fblockinfo tracks the current frame block.
88 A frame block is used to handle loops, try/except, and try/finally.
89 It's called a frame block to distinguish it from a basic block in the
93 enum fblocktype
{ LOOP
, EXCEPT
, FINALLY_TRY
, FINALLY_END
};
96 enum fblocktype fb_type
;
100 /* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block.
103 struct compiler_unit
{
104 PySTEntryObject
*u_ste
;
107 /* The following fields are dicts that map objects to
108 the index of them in co_XXX. The index is used as
109 the argument for opcodes that refer to those collections.
111 PyObject
*u_consts
; /* all constants */
112 PyObject
*u_names
; /* all names */
113 PyObject
*u_varnames
; /* local variables */
114 PyObject
*u_cellvars
; /* cell variables */
115 PyObject
*u_freevars
; /* free variables */
117 PyObject
*u_private
; /* for private name mangling */
119 int u_argcount
; /* number of arguments for block */
120 basicblock
*u_blocks
; /* pointer to list of blocks */
121 basicblock
*u_curblock
; /* pointer to current block */
122 int u_tmpname
; /* temporary variables for list comps */
125 struct fblockinfo u_fblock
[CO_MAXBLOCKS
];
127 int u_firstlineno
; /* the first lineno of the block */
128 int u_lineno
; /* the lineno for the current stmt */
129 bool u_lineno_set
; /* boolean to indicate whether instr
130 has been generated with current lineno */
133 /* This struct captures the global state of a compilation.
135 The u pointer points to the current compilation unit, while units
136 for enclosing blocks are stored in c_stack. The u and c_stack are
137 managed by compiler_enter_scope() and compiler_exit_scope().
141 const char *c_filename
;
142 struct symtable
*c_st
;
143 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
144 PyCompilerFlags
*c_flags
;
149 struct compiler_unit
*u
; /* compiler state for current block */
150 PyObject
*c_stack
; /* Python list holding compiler_unit ptrs */
151 char *c_encoding
; /* source encoding (a borrowed reference) */
152 PyArena
*c_arena
; /* pointer to memory allocation arena */
156 PyObject
*a_bytecode
; /* string containing bytecode */
157 int a_offset
; /* offset into bytecode */
158 int a_nblocks
; /* number of reachable blocks */
159 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
160 PyObject
*a_lnotab
; /* string containing lnotab */
161 int a_lnotab_off
; /* offset into lnotab */
162 int a_lineno
; /* last lineno of emitted instruction */
163 int a_lineno_off
; /* bytecode offset of last lineno */
166 static int compiler_enter_scope(struct compiler
*, identifier
, void *, int);
167 static void compiler_free(struct compiler
*);
168 static basicblock
*compiler_new_block(struct compiler
*);
169 static int compiler_next_instr(struct compiler
*, basicblock
*);
170 static int compiler_addop(struct compiler
*, int);
171 static int compiler_addop_o(struct compiler
*, int, PyObject
*, PyObject
*);
172 static int compiler_addop_i(struct compiler
*, int, int);
173 static int compiler_addop_j(struct compiler
*, int, basicblock
*, int);
174 static basicblock
*compiler_use_new_block(struct compiler
*);
175 static int compiler_error(struct compiler
*, const char *);
176 static int compiler_nameop(struct compiler
*, identifier
, expr_context_ty
);
178 static PyCodeObject
*compiler_mod(struct compiler
*, mod_ty
);
179 static int compiler_visit_stmt(struct compiler
*, stmt_ty
);
180 static int compiler_visit_keyword(struct compiler
*, keyword_ty
);
181 static int compiler_visit_expr(struct compiler
*, expr_ty
);
182 static int compiler_augassign(struct compiler
*, stmt_ty
);
183 static int compiler_visit_slice(struct compiler
*, slice_ty
,
186 static int compiler_push_fblock(struct compiler
*, enum fblocktype
,
188 static void compiler_pop_fblock(struct compiler
*, enum fblocktype
,
191 static int inplace_binop(struct compiler
*, operator_ty
);
192 static int expr_constant(expr_ty e
);
194 static PyCodeObject
*assemble(struct compiler
*, int addNone
);
195 static PyObject
*__doc__
;
198 _Py_Mangle(PyObject
*private, PyObject
*ident
)
200 /* Name mangling: __private becomes _classname__private.
201 This is independent from how the name is used. */
202 const char *p
, *name
= PyString_AsString(ident
);
205 if (private == NULL
|| name
== NULL
|| name
[0] != '_' || name
[1] != '_') {
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
);
296 PyNode_Compile(struct _node
*n
, const char *filename
)
298 PyCodeObject
*co
= NULL
;
299 PyArena
*arena
= PyArena_New();
300 mod_ty mod
= PyAST_FromNode(n
, NULL
, filename
, arena
);
302 co
= PyAST_Compile(mod
, filename
, NULL
, arena
);
308 compiler_free(struct compiler
*c
)
311 PySymtable_Free(c
->c_st
);
313 PyMem_Free(c
->c_future
);
314 Py_DECREF(c
->c_stack
);
318 list2dict(PyObject
*list
)
321 PyObject
*v
, *k
, *dict
= PyDict_New();
323 n
= PyList_Size(list
);
324 for (i
= 0; i
< n
; i
++) {
325 v
= PyInt_FromLong(i
);
330 k
= PyList_GET_ITEM(list
, i
);
331 k
= Py_BuildValue("(OO)", k
, k
->ob_type
);
332 if (k
== NULL
|| PyDict_SetItem(dict
, k
, v
) < 0) {
344 /* Return new dict containing names from src that match scope(s).
346 src is a symbol table dictionary. If the scope of a name matches
347 either scope_type or flag is set, insert it into the new dict. The
348 values are integers, starting at offset and increasing by one for
353 dictbytype(PyObject
*src
, int scope_type
, int flag
, int offset
)
355 int pos
= 0, i
= offset
, scope
;
356 PyObject
*k
, *v
, *dest
= PyDict_New();
362 while (PyDict_Next(src
, &pos
, &k
, &v
)) {
363 /* XXX this should probably be a macro in symtable.h */
364 assert(PyInt_Check(v
));
365 scope
= (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
367 if (scope
== scope_type
|| PyInt_AS_LONG(v
) & flag
) {
368 PyObject
*tuple
, *item
= PyInt_FromLong(i
);
374 tuple
= Py_BuildValue("(OO)", k
, k
->ob_type
);
375 if (!tuple
|| PyDict_SetItem(dest
, tuple
, item
) < 0) {
388 /* Begin: Peephole optimizations ----------------------------------------- */
390 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
391 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
392 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
393 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
394 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
395 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
396 #define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
398 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
399 with LOAD_CONST (c1, c2, ... cn).
400 The consts table must still be in list form so that the
401 new constant (c1, c2, ... cn) can be appended.
402 Called with codestr pointing to the first LOAD_CONST.
403 Bails out with no change if one or more of the LOAD_CONSTs is missing.
404 Also works for BUILD_LIST when followed by an "in" or "not in" test.
407 tuple_of_constants(unsigned char *codestr
, int n
, PyObject
*consts
)
409 PyObject
*newconst
, *constant
;
410 int i
, arg
, len_consts
;
413 assert(PyList_CheckExact(consts
));
414 assert(codestr
[n
*3] == BUILD_TUPLE
|| codestr
[n
*3] == BUILD_LIST
);
415 assert(GETARG(codestr
, (n
*3)) == n
);
416 for (i
=0 ; i
<n
; i
++)
417 assert(codestr
[i
*3] == LOAD_CONST
);
419 /* Buildup new tuple of constants */
420 newconst
= PyTuple_New(n
);
421 if (newconst
== NULL
)
423 len_consts
= PyList_GET_SIZE(consts
);
424 for (i
=0 ; i
<n
; i
++) {
425 arg
= GETARG(codestr
, (i
*3));
426 assert(arg
< len_consts
);
427 constant
= PyList_GET_ITEM(consts
, arg
);
429 PyTuple_SET_ITEM(newconst
, i
, constant
);
432 /* Append folded constant onto consts */
433 if (PyList_Append(consts
, newconst
)) {
439 /* Write NOPs over old LOAD_CONSTS and
440 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
441 memset(codestr
, NOP
, n
*3);
442 codestr
[n
*3] = LOAD_CONST
;
443 SETARG(codestr
, (n
*3), len_consts
);
447 /* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
448 with LOAD_CONST binop(c1,c2)
449 The consts table must still be in list form so that the
450 new constant can be appended.
451 Called with codestr pointing to the first LOAD_CONST.
452 Abandons the transformation if the folding fails (i.e. 1+'a').
453 If the new constant is a sequence, only folds when the size
454 is below a threshold value. That keeps pyc files from
455 becoming large in the presence of code like: (None,)*1000.
458 fold_binops_on_constants(unsigned char *codestr
, PyObject
*consts
)
460 PyObject
*newconst
, *v
, *w
;
461 int len_consts
, opcode
, size
;
464 assert(PyList_CheckExact(consts
));
465 assert(codestr
[0] == LOAD_CONST
);
466 assert(codestr
[3] == LOAD_CONST
);
468 /* Create new constant */
469 v
= PyList_GET_ITEM(consts
, GETARG(codestr
, 0));
470 w
= PyList_GET_ITEM(consts
, GETARG(codestr
, 3));
474 newconst
= PyNumber_Power(v
, w
, Py_None
);
476 case BINARY_MULTIPLY
:
477 newconst
= PyNumber_Multiply(v
, w
);
480 /* Cannot fold this operation statically since
481 the result can depend on the run-time presence of the -Qnew flag */
483 case BINARY_TRUE_DIVIDE
:
484 newconst
= PyNumber_TrueDivide(v
, w
);
486 case BINARY_FLOOR_DIVIDE
:
487 newconst
= PyNumber_FloorDivide(v
, w
);
490 newconst
= PyNumber_Remainder(v
, w
);
493 newconst
= PyNumber_Add(v
, w
);
495 case BINARY_SUBTRACT
:
496 newconst
= PyNumber_Subtract(v
, w
);
499 newconst
= PyObject_GetItem(v
, w
);
502 newconst
= PyNumber_Lshift(v
, w
);
505 newconst
= PyNumber_Rshift(v
, w
);
508 newconst
= PyNumber_And(v
, w
);
511 newconst
= PyNumber_Xor(v
, w
);
514 newconst
= PyNumber_Or(v
, w
);
517 /* Called with an unknown opcode */
518 PyErr_Format(PyExc_SystemError
,
519 "unexpected binary operation %d on a constant",
523 if (newconst
== NULL
) {
527 size
= PyObject_Size(newconst
);
530 else if (size
> 20) {
535 /* Append folded constant into consts table */
536 len_consts
= PyList_GET_SIZE(consts
);
537 if (PyList_Append(consts
, newconst
)) {
543 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
544 memset(codestr
, NOP
, 4);
545 codestr
[4] = LOAD_CONST
;
546 SETARG(codestr
, 4, len_consts
);
551 fold_unaryops_on_constants(unsigned char *codestr
, PyObject
*consts
)
553 PyObject
*newconst
=NULL
, *v
;
554 int len_consts
, opcode
;
557 assert(PyList_CheckExact(consts
));
558 assert(codestr
[0] == LOAD_CONST
);
560 /* Create new constant */
561 v
= PyList_GET_ITEM(consts
, GETARG(codestr
, 0));
565 /* Preserve the sign of -0.0 */
566 if (PyObject_IsTrue(v
) == 1)
567 newconst
= PyNumber_Negative(v
);
570 newconst
= PyObject_Repr(v
);
573 newconst
= PyNumber_Invert(v
);
576 /* Called with an unknown opcode */
577 PyErr_Format(PyExc_SystemError
,
578 "unexpected unary operation %d on a constant",
582 if (newconst
== NULL
) {
587 /* Append folded constant into consts table */
588 len_consts
= PyList_GET_SIZE(consts
);
589 if (PyList_Append(consts
, newconst
)) {
595 /* Write NOP LOAD_CONST newconst */
597 codestr
[1] = LOAD_CONST
;
598 SETARG(codestr
, 1, len_consts
);
602 static unsigned int *
603 markblocks(unsigned char *code
, int len
)
605 unsigned int *blocks
= PyMem_Malloc(len
*sizeof(int));
606 int i
,j
, opcode
, blockcnt
= 0;
610 memset(blocks
, 0, len
*sizeof(int));
612 /* Mark labels in the first pass */
613 for (i
=0 ; i
<len
; i
+=CODESIZE(opcode
)) {
625 j
= GETJUMPTGT(code
, i
);
630 /* Build block numbers in the second pass */
631 for (i
=0 ; i
<len
; i
++) {
632 blockcnt
+= blocks
[i
]; /* increment blockcnt over labels */
633 blocks
[i
] = blockcnt
;
638 /* Perform basic peephole optimizations to components of a code object.
639 The consts object should still be in list form to allow new constants
642 To keep the optimizer simple, it bails out (does nothing) for code
643 containing extended arguments or that has a length over 32,700. That
644 allows us to avoid overflow and sign issues. Likewise, it bails when
645 the lineno table has complex encoding for gaps >= 255.
647 Optimizations are restricted to simple transformations occuring within a
648 single basic block. All transformations keep the code size the same or
649 smaller. For those that reduce size, the gaps are initially filled with
650 NOPs. Later those NOPs are removed and the jump addresses retargeted in
651 a single pass. Line numbering is adjusted accordingly. */
654 optimize_code(PyObject
*code
, PyObject
* consts
, PyObject
*names
, PyObject
*lineno_obj
)
656 int i
, j
, codelen
, nops
, h
, adj
;
657 int tgt
, tgttgt
, opcode
;
658 unsigned char *codestr
= NULL
;
659 unsigned char *lineno
;
661 int new_line
, cum_orig_line
, last_line
, tabsiz
;
662 int cumlc
=0, lastlc
=0; /* Count runs of consecutive LOAD_CONST codes */
663 unsigned int *blocks
= NULL
;
666 /* Bail out if an exception is set */
667 if (PyErr_Occurred())
670 /* Bypass optimization when the lineno table is too complex */
671 assert(PyString_Check(lineno_obj
));
672 lineno
= (unsigned char*)PyString_AS_STRING(lineno_obj
);
673 tabsiz
= PyString_GET_SIZE(lineno_obj
);
674 if (memchr(lineno
, 255, tabsiz
) != NULL
)
677 /* Avoid situations where jump retargeting could overflow */
678 assert(PyString_Check(code
));
679 codelen
= PyString_Size(code
);
683 /* Make a modifiable copy of the code string */
684 codestr
= PyMem_Malloc(codelen
);
687 codestr
= memcpy(codestr
, PyString_AS_STRING(code
), codelen
);
689 /* Verify that RETURN_VALUE terminates the codestring. This allows
690 the various transformation patterns to look ahead several
691 instructions without additional checks to make sure they are not
692 looking beyond the end of the code string.
694 if (codestr
[codelen
-1] != RETURN_VALUE
)
697 /* Mapping to new jump targets after NOPs are removed */
698 addrmap
= PyMem_Malloc(codelen
* sizeof(int));
702 blocks
= markblocks(codestr
, codelen
);
705 assert(PyList_Check(consts
));
707 for (i
=0 ; i
<codelen
; i
+= CODESIZE(codestr
[i
])) {
715 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
716 with JUMP_IF_TRUE POP_TOP */
718 if (codestr
[i
+1] != JUMP_IF_FALSE
||
719 codestr
[i
+4] != POP_TOP
||
720 !ISBASICBLOCK(blocks
,i
,5))
722 tgt
= GETJUMPTGT(codestr
, (i
+1));
723 if (codestr
[tgt
] != POP_TOP
)
725 j
= GETARG(codestr
, i
+1) + 1;
726 codestr
[i
] = JUMP_IF_TRUE
;
727 SETARG(codestr
, i
, j
);
728 codestr
[i
+3] = POP_TOP
;
732 /* not a is b --> a is not b
733 not a in b --> a not in b
734 not a is not b --> a is b
735 not a not in b --> a in b
738 j
= GETARG(codestr
, i
);
739 if (j
< 6 || j
> 9 ||
740 codestr
[i
+3] != UNARY_NOT
||
741 !ISBASICBLOCK(blocks
,i
,4))
743 SETARG(codestr
, i
, (j
^1));
747 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
750 j
= GETARG(codestr
, i
);
751 name
= PyString_AsString(PyTuple_GET_ITEM(names
, j
));
752 if (name
== NULL
|| strcmp(name
, "None") != 0)
754 for (j
=0 ; j
< PyList_GET_SIZE(consts
) ; j
++) {
755 if (PyList_GET_ITEM(consts
, j
) == Py_None
) {
756 codestr
[i
] = LOAD_CONST
;
757 SETARG(codestr
, i
, j
);
764 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
767 j
= GETARG(codestr
, i
);
768 if (codestr
[i
+3] != JUMP_IF_FALSE
||
769 codestr
[i
+6] != POP_TOP
||
770 !ISBASICBLOCK(blocks
,i
,7) ||
771 !PyObject_IsTrue(PyList_GET_ITEM(consts
, j
)))
773 memset(codestr
+i
, NOP
, 7);
777 /* Try to fold tuples of constants (includes a case for lists
778 which are only used for "in" and "not in" tests).
779 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
780 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
781 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
784 j
= GETARG(codestr
, i
);
788 ((opcode
== BUILD_TUPLE
&&
789 ISBASICBLOCK(blocks
, h
, 3*(j
+1))) ||
790 (opcode
== BUILD_LIST
&&
791 codestr
[i
+3]==COMPARE_OP
&&
792 ISBASICBLOCK(blocks
, h
, 3*(j
+2)) &&
793 (GETARG(codestr
,i
+3)==6 ||
794 GETARG(codestr
,i
+3)==7))) &&
795 tuple_of_constants(&codestr
[h
], j
, consts
)) {
796 assert(codestr
[i
] == LOAD_CONST
);
800 if (codestr
[i
+3] != UNPACK_SEQUENCE
||
801 !ISBASICBLOCK(blocks
,i
,6) ||
802 j
!= GETARG(codestr
, i
+3))
805 memset(codestr
+i
, NOP
, 6);
807 codestr
[i
] = ROT_TWO
;
808 memset(codestr
+i
+1, NOP
, 5);
810 codestr
[i
] = ROT_THREE
;
811 codestr
[i
+1] = ROT_TWO
;
812 memset(codestr
+i
+2, NOP
, 4);
816 /* Fold binary ops on constants.
817 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
819 case BINARY_MULTIPLY
:
820 case BINARY_TRUE_DIVIDE
:
821 case BINARY_FLOOR_DIVIDE
:
824 case BINARY_SUBTRACT
:
832 ISBASICBLOCK(blocks
, i
-6, 7) &&
833 fold_binops_on_constants(&codestr
[i
-6], consts
)) {
835 assert(codestr
[i
] == LOAD_CONST
);
840 /* Fold unary ops on constants.
841 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
846 ISBASICBLOCK(blocks
, i
-3, 4) &&
847 fold_unaryops_on_constants(&codestr
[i
-3], consts
)) {
849 assert(codestr
[i
] == LOAD_CONST
);
854 /* Simplify conditional jump to conditional jump where the
855 result of the first test implies the success of a similar
856 test or the failure of the opposite test.
862 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
863 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
864 where y+3 is the instruction following the second test.
868 tgt
= GETJUMPTGT(codestr
, i
);
870 if (j
== JUMP_IF_FALSE
|| j
== JUMP_IF_TRUE
) {
872 tgttgt
= GETJUMPTGT(codestr
, tgt
) - i
- 3;
873 SETARG(codestr
, i
, tgttgt
);
876 SETARG(codestr
, i
, tgt
);
880 /* Intentional fallthrough */
882 /* Replace jumps to unconditional jumps */
890 tgt
= GETJUMPTGT(codestr
, i
);
891 if (!UNCONDITIONAL_JUMP(codestr
[tgt
]))
893 tgttgt
= GETJUMPTGT(codestr
, tgt
);
894 if (opcode
== JUMP_FORWARD
) /* JMP_ABS can go backwards */
895 opcode
= JUMP_ABSOLUTE
;
896 if (!ABSOLUTE_JUMP(opcode
))
897 tgttgt
-= i
+ 3; /* Calc relative jump addr */
898 if (tgttgt
< 0) /* No backward relative jumps */
901 SETARG(codestr
, i
, tgttgt
);
907 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
909 if (i
+4 >= codelen
||
910 codestr
[i
+4] != RETURN_VALUE
||
911 !ISBASICBLOCK(blocks
,i
,5))
913 memset(codestr
+i
+1, NOP
, 4);
918 /* Fixup linenotab */
919 for (i
=0, nops
=0 ; i
<codelen
; i
+= CODESIZE(codestr
[i
])) {
920 addrmap
[i
] = i
- nops
;
921 if (codestr
[i
] == NOP
)
926 for (i
=0 ; i
< tabsiz
; i
+=2) {
927 cum_orig_line
+= lineno
[i
];
928 new_line
= addrmap
[cum_orig_line
];
929 assert (new_line
- last_line
< 255);
930 lineno
[i
] =((unsigned char)(new_line
- last_line
));
931 last_line
= new_line
;
934 /* Remove NOPs and fixup jump targets */
935 for (i
=0, h
=0 ; i
<codelen
; ) {
944 j
= addrmap
[GETARG(codestr
, i
)];
945 SETARG(codestr
, i
, j
);
955 j
= addrmap
[GETARG(codestr
, i
) + i
+ 3] - addrmap
[i
] - 3;
956 SETARG(codestr
, i
, j
);
959 adj
= CODESIZE(opcode
);
961 codestr
[h
++] = codestr
[i
++];
963 assert(h
+ nops
== codelen
);
965 code
= PyString_FromStringAndSize((char *)codestr
, h
);
982 /* End: Peephole optimizations ----------------------------------------- */
986 Leave this debugging code for just a little longer.
989 compiler_display_symbols(PyObject *name, PyObject *symbols)
991 PyObject *key, *value;
994 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
995 while (PyDict_Next(symbols, &pos, &key, &value)) {
996 flags = PyInt_AsLong(value);
997 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
998 if (flags & DEF_GLOBAL)
999 fprintf(stderr, " declared_global");
1000 if (flags & DEF_LOCAL)
1001 fprintf(stderr, " local");
1002 if (flags & DEF_PARAM)
1003 fprintf(stderr, " param");
1004 if (flags & DEF_STAR)
1005 fprintf(stderr, " stararg");
1006 if (flags & DEF_DOUBLESTAR)
1007 fprintf(stderr, " starstar");
1008 if (flags & DEF_INTUPLE)
1009 fprintf(stderr, " tuple");
1010 if (flags & DEF_FREE)
1011 fprintf(stderr, " free");
1012 if (flags & DEF_FREE_GLOBAL)
1013 fprintf(stderr, " global");
1014 if (flags & DEF_FREE_CLASS)
1015 fprintf(stderr, " free/class");
1016 if (flags & DEF_IMPORT)
1017 fprintf(stderr, " import");
1018 fprintf(stderr, "\n");
1020 fprintf(stderr, "\n");
1025 compiler_unit_check(struct compiler_unit
*u
)
1028 for (block
= u
->u_blocks
; block
!= NULL
; block
= block
->b_list
) {
1029 assert(block
!= (void *)0xcbcbcbcb);
1030 assert(block
!= (void *)0xfbfbfbfb);
1031 assert(block
!= (void *)0xdbdbdbdb);
1032 if (block
->b_instr
!= NULL
) {
1033 assert(block
->b_ialloc
> 0);
1034 assert(block
->b_iused
> 0);
1035 assert(block
->b_ialloc
>= block
->b_iused
);
1038 assert (block
->b_iused
== 0);
1039 assert (block
->b_ialloc
== 0);
1045 compiler_unit_free(struct compiler_unit
*u
)
1047 basicblock
*b
, *next
;
1049 compiler_unit_check(u
);
1053 PyObject_Free((void *)b
->b_instr
);
1055 PyObject_Free((void *)b
);
1058 Py_XDECREF(u
->u_ste
);
1059 Py_XDECREF(u
->u_name
);
1060 Py_XDECREF(u
->u_consts
);
1061 Py_XDECREF(u
->u_names
);
1062 Py_XDECREF(u
->u_varnames
);
1063 Py_XDECREF(u
->u_freevars
);
1064 Py_XDECREF(u
->u_cellvars
);
1065 Py_XDECREF(u
->u_private
);
1070 compiler_enter_scope(struct compiler
*c
, identifier name
, void *key
,
1073 struct compiler_unit
*u
;
1075 u
= PyObject_Malloc(sizeof(struct compiler_unit
));
1080 memset(u
, 0, sizeof(struct compiler_unit
));
1082 u
->u_ste
= PySymtable_Lookup(c
->c_st
, key
);
1084 compiler_unit_free(u
);
1089 u
->u_varnames
= list2dict(u
->u_ste
->ste_varnames
);
1090 u
->u_cellvars
= dictbytype(u
->u_ste
->ste_symbols
, CELL
, 0, 0);
1091 u
->u_freevars
= dictbytype(u
->u_ste
->ste_symbols
, FREE
, DEF_FREE_CLASS
,
1092 PyDict_Size(u
->u_cellvars
));
1097 u
->u_firstlineno
= lineno
;
1099 u
->u_lineno_set
= false;
1100 u
->u_consts
= PyDict_New();
1102 compiler_unit_free(u
);
1105 u
->u_names
= PyDict_New();
1107 compiler_unit_free(u
);
1111 u
->u_private
= NULL
;
1113 /* Push the old compiler_unit on the stack. */
1115 PyObject
*wrapper
= PyCObject_FromVoidPtr(c
->u
, NULL
);
1116 if (PyList_Append(c
->c_stack
, wrapper
) < 0) {
1117 compiler_unit_free(u
);
1121 u
->u_private
= c
->u
->u_private
;
1122 Py_XINCREF(u
->u_private
);
1127 if (compiler_use_new_block(c
) == NULL
)
1134 compiler_exit_scope(struct compiler
*c
)
1140 compiler_unit_free(c
->u
);
1141 /* Restore c->u to the parent unit. */
1142 n
= PyList_GET_SIZE(c
->c_stack
) - 1;
1144 wrapper
= PyList_GET_ITEM(c
->c_stack
, n
);
1145 c
->u
= (struct compiler_unit
*)PyCObject_AsVoidPtr(wrapper
);
1146 /* we are deleting from a list so this really shouldn't fail */
1147 if (PySequence_DelItem(c
->c_stack
, n
) < 0)
1148 Py_FatalError("compiler_exit_scope()");
1149 compiler_unit_check(c
->u
);
1156 /* Allocate a new block and return a pointer to it.
1157 Returns NULL on error.
1161 compiler_new_block(struct compiler
*c
)
1164 struct compiler_unit
*u
;
1167 b
= (basicblock
*)PyObject_Malloc(sizeof(basicblock
));
1172 memset((void *)b
, 0, sizeof(basicblock
));
1173 assert (b
->b_next
== NULL
);
1174 b
->b_list
= u
->u_blocks
;
1180 compiler_use_new_block(struct compiler
*c
)
1182 basicblock
*block
= compiler_new_block(c
);
1185 c
->u
->u_curblock
= block
;
1190 compiler_next_block(struct compiler
*c
)
1192 basicblock
*block
= compiler_new_block(c
);
1195 c
->u
->u_curblock
->b_next
= block
;
1196 c
->u
->u_curblock
= block
;
1201 compiler_use_next_block(struct compiler
*c
, basicblock
*block
)
1203 assert(block
!= NULL
);
1204 c
->u
->u_curblock
->b_next
= block
;
1205 c
->u
->u_curblock
= block
;
1209 /* Returns the offset of the next instruction in the current block's
1210 b_instr array. Resizes the b_instr as necessary.
1211 Returns -1 on failure.
1215 compiler_next_instr(struct compiler
*c
, basicblock
*b
)
1218 if (b
->b_instr
== NULL
) {
1219 b
->b_instr
= PyObject_Malloc(sizeof(struct instr
) *
1220 DEFAULT_BLOCK_SIZE
);
1221 if (b
->b_instr
== NULL
) {
1225 b
->b_ialloc
= DEFAULT_BLOCK_SIZE
;
1226 memset((char *)b
->b_instr
, 0,
1227 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
1229 else if (b
->b_iused
== b
->b_ialloc
) {
1230 size_t oldsize
, newsize
;
1231 oldsize
= b
->b_ialloc
* sizeof(struct instr
);
1232 newsize
= oldsize
<< 1;
1238 b
->b_instr
= PyObject_Realloc((void *)b
->b_instr
, newsize
);
1239 if (b
->b_instr
== NULL
)
1241 memset((char *)b
->b_instr
+ oldsize
, 0, newsize
- oldsize
);
1243 return b
->b_iused
++;
1247 compiler_set_lineno(struct compiler
*c
, int off
)
1250 if (c
->u
->u_lineno_set
)
1252 c
->u
->u_lineno_set
= true;
1253 b
= c
->u
->u_curblock
;
1254 b
->b_instr
[off
].i_lineno
= c
->u
->u_lineno
;
1258 opcode_stack_effect(int opcode
, int oparg
)
1271 case UNARY_POSITIVE
:
1272 case UNARY_NEGATIVE
:
1279 case BINARY_MULTIPLY
:
1283 case BINARY_SUBTRACT
:
1285 case BINARY_FLOOR_DIVIDE
:
1286 case BINARY_TRUE_DIVIDE
:
1288 case INPLACE_FLOOR_DIVIDE
:
1289 case INPLACE_TRUE_DIVIDE
:
1310 case DELETE_SLICE
+0:
1312 case DELETE_SLICE
+1:
1314 case DELETE_SLICE
+2:
1316 case DELETE_SLICE
+3:
1320 case INPLACE_SUBTRACT
:
1321 case INPLACE_MULTIPLY
:
1322 case INPLACE_DIVIDE
:
1323 case INPLACE_MODULO
:
1349 case PRINT_NEWLINE_TO
:
1351 case INPLACE_LSHIFT
:
1352 case INPLACE_RSHIFT
:
1374 return -1; /* or -2 or -3 if exception occurred */
1382 case UNPACK_SEQUENCE
:
1430 return 3; /* actually pushed by an exception */
1441 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
1443 return -NARGS(oparg
);
1444 case CALL_FUNCTION_VAR
:
1445 case CALL_FUNCTION_KW
:
1446 return -NARGS(oparg
)-1;
1447 case CALL_FUNCTION_VAR_KW
:
1448 return -NARGS(oparg
)-2;
1467 fprintf(stderr
, "opcode = %d\n", opcode
);
1468 Py_FatalError("opcode_stack_effect()");
1471 return 0; /* not reachable */
1474 /* Add an opcode with no argument.
1475 Returns 0 on failure, 1 on success.
1479 compiler_addop(struct compiler
*c
, int opcode
)
1484 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1487 b
= c
->u
->u_curblock
;
1488 i
= &b
->b_instr
[off
];
1489 i
->i_opcode
= opcode
;
1491 if (opcode
== RETURN_VALUE
)
1493 compiler_set_lineno(c
, off
);
1498 compiler_add_o(struct compiler
*c
, PyObject
*dict
, PyObject
*o
)
1503 /* necessary to make sure types aren't coerced (e.g., int and long) */
1504 t
= PyTuple_Pack(2, o
, o
->ob_type
);
1508 v
= PyDict_GetItem(dict
, t
);
1510 arg
= PyDict_Size(dict
);
1511 v
= PyInt_FromLong(arg
);
1516 if (PyDict_SetItem(dict
, t
, v
) < 0) {
1524 arg
= PyInt_AsLong(v
);
1530 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
1533 int arg
= compiler_add_o(c
, dict
, o
);
1536 return compiler_addop_i(c
, opcode
, arg
);
1540 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
1544 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
1547 arg
= compiler_add_o(c
, dict
, mangled
);
1551 return compiler_addop_i(c
, opcode
, arg
);
1554 /* Add an opcode with an integer argument.
1555 Returns 0 on failure, 1 on success.
1559 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
1563 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1566 i
= &c
->u
->u_curblock
->b_instr
[off
];
1567 i
->i_opcode
= opcode
;
1570 compiler_set_lineno(c
, off
);
1575 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
1581 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1584 compiler_set_lineno(c
, off
);
1585 i
= &c
->u
->u_curblock
->b_instr
[off
];
1586 i
->i_opcode
= opcode
;
1596 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1597 like to find better names.) NEW_BLOCK() creates a new block and sets
1598 it as the current block. NEXT_BLOCK() also creates an implicit jump
1599 from the current block to the new block.
1602 /* XXX The returns inside these macros make it impossible to decref
1603 objects created in the local function.
1607 #define NEW_BLOCK(C) { \
1608 if (compiler_use_new_block((C)) == NULL) \
1612 #define NEXT_BLOCK(C) { \
1613 if (compiler_next_block((C)) == NULL) \
1617 #define ADDOP(C, OP) { \
1618 if (!compiler_addop((C), (OP))) \
1622 #define ADDOP_IN_SCOPE(C, OP) { \
1623 if (!compiler_addop((C), (OP))) { \
1624 compiler_exit_scope(c); \
1629 #define ADDOP_O(C, OP, O, TYPE) { \
1630 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1634 #define ADDOP_NAME(C, OP, O, TYPE) { \
1635 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1639 #define ADDOP_I(C, OP, O) { \
1640 if (!compiler_addop_i((C), (OP), (O))) \
1644 #define ADDOP_JABS(C, OP, O) { \
1645 if (!compiler_addop_j((C), (OP), (O), 1)) \
1649 #define ADDOP_JREL(C, OP, O) { \
1650 if (!compiler_addop_j((C), (OP), (O), 0)) \
1654 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1655 the ASDL name to synthesize the name of the C type and the visit function.
1658 #define VISIT(C, TYPE, V) {\
1659 if (!compiler_visit_ ## TYPE((C), (V))) \
1663 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1664 if (!compiler_visit_ ## TYPE((C), (V))) { \
1665 compiler_exit_scope(c); \
1670 #define VISIT_SLICE(C, V, CTX) {\
1671 if (!compiler_visit_slice((C), (V), (CTX))) \
1675 #define VISIT_SEQ(C, TYPE, SEQ) { \
1677 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1678 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1679 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
1680 if (!compiler_visit_ ## TYPE((C), elt)) \
1685 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1687 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1688 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1689 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
1690 if (!compiler_visit_ ## TYPE((C), elt)) { \
1691 compiler_exit_scope(c); \
1698 compiler_isdocstring(stmt_ty s
)
1700 if (s
->kind
!= Expr_kind
)
1702 return s
->v
.Expr
.value
->kind
== Str_kind
;
1705 /* Compile a sequence of statements, checking for a docstring. */
1708 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1713 if (!asdl_seq_LEN(stmts
))
1715 st
= asdl_seq_GET(stmts
, 0);
1716 if (compiler_isdocstring(st
)) {
1718 VISIT(c
, expr
, st
->v
.Expr
.value
);
1719 if (!compiler_nameop(c
, __doc__
, Store
))
1722 for (; i
< asdl_seq_LEN(stmts
); i
++)
1723 VISIT(c
, stmt
, asdl_seq_GET(stmts
, i
));
1727 static PyCodeObject
*
1728 compiler_mod(struct compiler
*c
, mod_ty mod
)
1732 static PyObject
*module
;
1734 module
= PyString_FromString("<module>");
1738 if (!compiler_enter_scope(c
, module
, mod
, 1))
1740 switch (mod
->kind
) {
1742 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1743 compiler_exit_scope(c
);
1747 case Interactive_kind
:
1748 c
->c_interactive
= 1;
1749 VISIT_SEQ_IN_SCOPE(c
, stmt
, mod
->v
.Interactive
.body
);
1751 case Expression_kind
:
1752 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1756 PyErr_SetString(PyExc_SystemError
,
1757 "suite should not be possible");
1760 PyErr_Format(PyExc_SystemError
,
1761 "module kind %d should not be possible",
1765 co
= assemble(c
, addNone
);
1766 compiler_exit_scope(c
);
1770 /* The test for LOCAL must come before the test for FREE in order to
1771 handle classes where name is both local and free. The local var is
1772 a method and the free var is a free var referenced within a method.
1776 get_ref_type(struct compiler
*c
, PyObject
*name
)
1778 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1781 PyOS_snprintf(buf
, sizeof(buf
),
1782 "unknown scope for %.100s in %.100s(%s) in %s\n"
1783 "symbols: %s\nlocals: %s\nglobals: %s\n",
1784 PyString_AS_STRING(name
),
1785 PyString_AS_STRING(c
->u
->u_name
),
1786 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1788 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1789 PyObject_REPR(c
->u
->u_varnames
),
1790 PyObject_REPR(c
->u
->u_names
)
1799 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1802 k
= Py_BuildValue("(OO)", name
, name
->ob_type
);
1805 v
= PyDict_GetItem(dict
, k
);
1809 return PyInt_AS_LONG(v
);
1813 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1815 int i
, free
= PyCode_GetNumFree(co
);
1817 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1818 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1821 for (i
= 0; i
< free
; ++i
) {
1822 /* Bypass com_addop_varname because it will generate
1823 LOAD_DEREF but LOAD_CLOSURE is needed.
1825 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1828 /* Special case: If a class contains a method with a
1829 free variable that has the same name as a method,
1830 the name will be considered free *and* local in the
1831 class. It should be handled by the closure, as
1832 well as by the normal name loookup logic.
1834 reftype
= get_ref_type(c
, name
);
1835 if (reftype
== CELL
)
1836 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1837 else /* (reftype == FREE) */
1838 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1840 printf("lookup %s in %s %d %d\n"
1841 "freevars of %s: %s\n",
1842 PyObject_REPR(name
),
1843 PyString_AS_STRING(c
->u
->u_name
),
1845 PyString_AS_STRING(co
->co_name
),
1846 PyObject_REPR(co
->co_freevars
));
1847 Py_FatalError("compiler_make_closure()");
1849 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1851 ADDOP_I(c
, BUILD_TUPLE
, free
);
1852 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1853 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1858 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1865 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1866 VISIT(c
, expr
, asdl_seq_GET(decos
, i
));
1872 compiler_arguments(struct compiler
*c
, arguments_ty args
)
1875 int n
= asdl_seq_LEN(args
->args
);
1876 /* Correctly handle nested argument lists */
1877 for (i
= 0; i
< n
; i
++) {
1878 expr_ty arg
= asdl_seq_GET(args
->args
, i
);
1879 if (arg
->kind
== Tuple_kind
) {
1880 PyObject
*id
= PyString_FromFormat(".%d", i
);
1884 if (!compiler_nameop(c
, id
, Load
)) {
1889 VISIT(c
, expr
, arg
);
1896 compiler_function(struct compiler
*c
, stmt_ty s
)
1899 PyObject
*first_const
= Py_None
;
1900 arguments_ty args
= s
->v
.FunctionDef
.args
;
1901 asdl_seq
* decos
= s
->v
.FunctionDef
.decorators
;
1903 int i
, n
, docstring
;
1905 assert(s
->kind
== FunctionDef_kind
);
1907 if (!compiler_decorators(c
, decos
))
1910 VISIT_SEQ(c
, expr
, args
->defaults
);
1911 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1915 st
= asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1916 docstring
= compiler_isdocstring(st
);
1918 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1919 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1920 compiler_exit_scope(c
);
1924 /* unpack nested arguments */
1925 compiler_arguments(c
, args
);
1927 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1928 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1929 /* if there was a docstring, we need to skip the first statement */
1930 for (i
= docstring
; i
< n
; i
++) {
1931 stmt_ty s2
= asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1932 if (i
== 0 && s2
->kind
== Expr_kind
&&
1933 s2
->v
.Expr
.value
->kind
== Str_kind
)
1935 VISIT_IN_SCOPE(c
, stmt
, s2
);
1937 co
= assemble(c
, 1);
1938 compiler_exit_scope(c
);
1942 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1945 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1946 ADDOP_I(c
, CALL_FUNCTION
, 1);
1949 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1953 compiler_class(struct compiler
*c
, stmt_ty s
)
1958 /* push class name on stack, needed by BUILD_CLASS */
1959 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1960 /* push the tuple of base classes on the stack */
1961 n
= asdl_seq_LEN(s
->v
.ClassDef
.bases
);
1963 VISIT_SEQ(c
, expr
, s
->v
.ClassDef
.bases
);
1964 ADDOP_I(c
, BUILD_TUPLE
, n
);
1965 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
,
1968 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1969 Py_INCREF(c
->u
->u_private
);
1970 str
= PyString_InternFromString("__name__");
1971 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1973 compiler_exit_scope(c
);
1978 str
= PyString_InternFromString("__module__");
1979 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
1981 compiler_exit_scope(c
);
1986 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
1987 compiler_exit_scope(c
);
1991 ADDOP_IN_SCOPE(c
, LOAD_LOCALS
);
1992 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1993 co
= assemble(c
, 1);
1994 compiler_exit_scope(c
);
1998 compiler_make_closure(c
, co
, 0);
2001 ADDOP_I(c
, CALL_FUNCTION
, 0);
2002 ADDOP(c
, BUILD_CLASS
);
2003 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
2009 compiler_lambda(struct compiler
*c
, expr_ty e
)
2012 static identifier name
;
2013 arguments_ty args
= e
->v
.Lambda
.args
;
2014 assert(e
->kind
== Lambda_kind
);
2017 name
= PyString_InternFromString("<lambda>");
2023 VISIT_SEQ(c
, expr
, args
->defaults
);
2024 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2027 /* unpack nested arguments */
2028 compiler_arguments(c
, args
);
2030 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
2031 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
2032 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
2033 co
= assemble(c
, 1);
2034 compiler_exit_scope(c
);
2038 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
2045 compiler_print(struct compiler
*c
, stmt_ty s
)
2050 assert(s
->kind
== Print_kind
);
2051 n
= asdl_seq_LEN(s
->v
.Print
.values
);
2053 if (s
->v
.Print
.dest
) {
2054 VISIT(c
, expr
, s
->v
.Print
.dest
);
2057 for (i
= 0; i
< n
; i
++) {
2058 expr_ty e
= (expr_ty
)asdl_seq_GET(s
->v
.Print
.values
, i
);
2063 ADDOP(c
, PRINT_ITEM_TO
);
2067 ADDOP(c
, PRINT_ITEM
);
2070 if (s
->v
.Print
.nl
) {
2072 ADDOP(c
, PRINT_NEWLINE_TO
)
2074 ADDOP(c
, PRINT_NEWLINE
)
2082 compiler_if(struct compiler
*c
, stmt_ty s
)
2084 basicblock
*end
, *next
;
2086 assert(s
->kind
== If_kind
);
2087 end
= compiler_new_block(c
);
2090 next
= compiler_new_block(c
);
2093 VISIT(c
, expr
, s
->v
.If
.test
);
2094 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
2096 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
2097 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2098 compiler_use_next_block(c
, next
);
2101 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
2102 compiler_use_next_block(c
, end
);
2107 compiler_for(struct compiler
*c
, stmt_ty s
)
2109 basicblock
*start
, *cleanup
, *end
;
2111 start
= compiler_new_block(c
);
2112 cleanup
= compiler_new_block(c
);
2113 end
= compiler_new_block(c
);
2114 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
2116 ADDOP_JREL(c
, SETUP_LOOP
, end
);
2117 if (!compiler_push_fblock(c
, LOOP
, start
))
2119 VISIT(c
, expr
, s
->v
.For
.iter
);
2121 compiler_use_next_block(c
, start
);
2122 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
2123 VISIT(c
, expr
, s
->v
.For
.target
);
2124 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
2125 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2126 compiler_use_next_block(c
, cleanup
);
2127 ADDOP(c
, POP_BLOCK
);
2128 compiler_pop_fblock(c
, LOOP
, start
);
2129 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
2130 compiler_use_next_block(c
, end
);
2135 compiler_while(struct compiler
*c
, stmt_ty s
)
2137 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
2138 int constant
= expr_constant(s
->v
.While
.test
);
2142 loop
= compiler_new_block(c
);
2143 end
= compiler_new_block(c
);
2144 if (constant
== -1) {
2145 anchor
= compiler_new_block(c
);
2149 if (loop
== NULL
|| end
== NULL
)
2151 if (s
->v
.While
.orelse
) {
2152 orelse
= compiler_new_block(c
);
2159 ADDOP_JREL(c
, SETUP_LOOP
, end
);
2160 compiler_use_next_block(c
, loop
);
2161 if (!compiler_push_fblock(c
, LOOP
, loop
))
2163 if (constant
== -1) {
2164 VISIT(c
, expr
, s
->v
.While
.test
);
2165 ADDOP_JREL(c
, JUMP_IF_FALSE
, anchor
);
2168 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
2169 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
2171 /* XXX should the two POP instructions be in a separate block
2172 if there is no else clause ?
2175 if (constant
== -1) {
2176 compiler_use_next_block(c
, anchor
);
2178 ADDOP(c
, POP_BLOCK
);
2180 compiler_pop_fblock(c
, LOOP
, loop
);
2182 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
2183 compiler_use_next_block(c
, end
);
2189 compiler_continue(struct compiler
*c
)
2191 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
2194 if (!c
->u
->u_nfblocks
)
2195 return compiler_error(c
, LOOP_ERROR_MSG
);
2196 i
= c
->u
->u_nfblocks
- 1;
2197 switch (c
->u
->u_fblock
[i
].fb_type
) {
2199 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
2203 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
)
2206 return compiler_error(c
, LOOP_ERROR_MSG
);
2207 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
2210 return compiler_error(c
,
2211 "'continue' not supported inside 'finally' clause");
2217 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
2223 L: <code for finalbody>
2226 The special instructions use the block stack. Each block
2227 stack entry contains the instruction that created it (here
2228 SETUP_FINALLY), the level of the value stack at the time the
2229 block stack entry was created, and a label (here L).
2232 Pushes the current value stack level and the label
2233 onto the block stack.
2235 Pops en entry from the block stack, and pops the value
2236 stack until its level is the same as indicated on the
2237 block stack. (The label is ignored.)
2239 Pops a variable number of entries from the *value* stack
2240 and re-raises the exception they specify. The number of
2241 entries popped depends on the (pseudo) exception type.
2243 The block stack is unwound when an exception is raised:
2244 when a SETUP_FINALLY entry is found, the exception is pushed
2245 onto the value stack (and the exception condition is cleared),
2246 and the interpreter jumps to the label gotten from the block
2251 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
2253 basicblock
*body
, *end
;
2254 body
= compiler_new_block(c
);
2255 end
= compiler_new_block(c
);
2256 if (body
== NULL
|| end
== NULL
)
2259 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
2260 compiler_use_next_block(c
, body
);
2261 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
2263 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
2264 ADDOP(c
, POP_BLOCK
);
2265 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
2267 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2268 compiler_use_next_block(c
, end
);
2269 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
2271 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
2272 ADDOP(c
, END_FINALLY
);
2273 compiler_pop_fblock(c
, FINALLY_END
, end
);
2279 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2280 (The contents of the value stack is shown in [], with the top
2281 at the right; 'tb' is trace-back info, 'val' the exception's
2282 associated value, and 'exc' the exception.)
2284 Value stack Label Instruction Argument
2290 [tb, val, exc] L1: DUP )
2291 [tb, val, exc, exc] <evaluate E1> )
2292 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2293 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2294 [tb, val, exc, 1] POP )
2296 [tb, val] <assign to V1> (or POP if no V1)
2301 [tb, val, exc, 0] L2: POP
2303 .............................etc.......................
2305 [tb, val, exc, 0] Ln+1: POP
2306 [tb, val, exc] END_FINALLY # re-raise exception
2308 [] L0: <next statement>
2310 Of course, parts are not generated if Vi or Ei is not present.
2313 compiler_try_except(struct compiler
*c
, stmt_ty s
)
2315 basicblock
*body
, *orelse
, *except
, *end
;
2318 body
= compiler_new_block(c
);
2319 except
= compiler_new_block(c
);
2320 orelse
= compiler_new_block(c
);
2321 end
= compiler_new_block(c
);
2322 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
2324 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
2325 compiler_use_next_block(c
, body
);
2326 if (!compiler_push_fblock(c
, EXCEPT
, body
))
2328 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
2329 ADDOP(c
, POP_BLOCK
);
2330 compiler_pop_fblock(c
, EXCEPT
, body
);
2331 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
2332 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
2333 compiler_use_next_block(c
, except
);
2334 for (i
= 0; i
< n
; i
++) {
2335 excepthandler_ty handler
= asdl_seq_GET(
2336 s
->v
.TryExcept
.handlers
, i
);
2337 if (!handler
->type
&& i
< n
-1)
2338 return compiler_error(c
, "default 'except:' must be last");
2339 except
= compiler_new_block(c
);
2342 if (handler
->type
) {
2344 VISIT(c
, expr
, handler
->type
);
2345 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
2346 ADDOP_JREL(c
, JUMP_IF_FALSE
, except
);
2350 if (handler
->name
) {
2351 VISIT(c
, expr
, handler
->name
);
2357 VISIT_SEQ(c
, stmt
, handler
->body
);
2358 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2359 compiler_use_next_block(c
, except
);
2363 ADDOP(c
, END_FINALLY
);
2364 compiler_use_next_block(c
, orelse
);
2365 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
2366 compiler_use_next_block(c
, end
);
2371 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
2373 /* The IMPORT_NAME opcode was already generated. This function
2374 merely needs to bind the result to a name.
2376 If there is a dot in name, we need to split it and emit a
2377 LOAD_ATTR for each name.
2379 const char *src
= PyString_AS_STRING(name
);
2380 const char *dot
= strchr(src
, '.');
2382 /* Consume the base module name to get the first attribute */
2385 /* NB src is only defined when dot != NULL */
2387 dot
= strchr(src
, '.');
2388 attr
= PyString_FromStringAndSize(src
,
2389 dot
? dot
- src
: strlen(src
));
2392 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
2397 return compiler_nameop(c
, asname
, Store
);
2401 compiler_import(struct compiler
*c
, stmt_ty s
)
2403 /* The Import node stores a module name like a.b.c as a single
2404 string. This is convenient for all cases except
2406 where we need to parse that string to extract the individual
2408 XXX Perhaps change the representation to make this case simpler?
2410 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
2411 for (i
= 0; i
< n
; i
++) {
2412 alias_ty alias
= asdl_seq_GET(s
->v
.Import
.names
, i
);
2415 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2416 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
2418 if (alias
->asname
) {
2419 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
2424 identifier tmp
= alias
->name
;
2425 const char *base
= PyString_AS_STRING(alias
->name
);
2426 char *dot
= strchr(base
, '.');
2428 tmp
= PyString_FromStringAndSize(base
,
2430 r
= compiler_nameop(c
, tmp
, Store
);
2442 compiler_from_import(struct compiler
*c
, stmt_ty s
)
2444 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
2446 PyObject
*names
= PyTuple_New(n
);
2450 /* build up the names */
2451 for (i
= 0; i
< n
; i
++) {
2452 alias_ty alias
= asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2453 Py_INCREF(alias
->name
);
2454 PyTuple_SET_ITEM(names
, i
, alias
->name
);
2457 if (s
->lineno
> c
->c_future
->ff_lineno
) {
2458 if (!strcmp(PyString_AS_STRING(s
->v
.ImportFrom
.module
),
2461 return compiler_error(c
,
2462 "from __future__ imports must occur "
2463 "at the beginning of the file");
2468 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
2470 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
2471 for (i
= 0; i
< n
; i
++) {
2472 alias_ty alias
= asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2473 identifier store_name
;
2475 if (i
== 0 && *PyString_AS_STRING(alias
->name
) == '*') {
2477 ADDOP(c
, IMPORT_STAR
);
2481 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
2482 store_name
= alias
->name
;
2484 store_name
= alias
->asname
;
2486 if (!compiler_nameop(c
, store_name
, Store
)) {
2491 /* remove imported module */
2497 compiler_assert(struct compiler
*c
, stmt_ty s
)
2499 static PyObject
*assertion_error
= NULL
;
2502 if (Py_OptimizeFlag
)
2504 if (assertion_error
== NULL
) {
2505 assertion_error
= PyString_FromString("AssertionError");
2506 if (assertion_error
== NULL
)
2509 VISIT(c
, expr
, s
->v
.Assert
.test
);
2510 end
= compiler_new_block(c
);
2513 ADDOP_JREL(c
, JUMP_IF_TRUE
, end
);
2515 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
2516 if (s
->v
.Assert
.msg
) {
2517 VISIT(c
, expr
, s
->v
.Assert
.msg
);
2518 ADDOP_I(c
, RAISE_VARARGS
, 2);
2521 ADDOP_I(c
, RAISE_VARARGS
, 1);
2523 compiler_use_next_block(c
, end
);
2529 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2533 c
->u
->u_lineno
= s
->lineno
;
2534 c
->u
->u_lineno_set
= false;
2536 case FunctionDef_kind
:
2537 return compiler_function(c
, s
);
2539 return compiler_class(c
, s
);
2541 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2542 return compiler_error(c
, "'return' outside function");
2543 if (s
->v
.Return
.value
) {
2544 if (c
->u
->u_ste
->ste_generator
) {
2545 return compiler_error(c
,
2546 "'return' with argument inside generator");
2548 VISIT(c
, expr
, s
->v
.Return
.value
);
2551 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2552 ADDOP(c
, RETURN_VALUE
);
2555 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2558 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2559 VISIT(c
, expr
, s
->v
.Assign
.value
);
2560 for (i
= 0; i
< n
; i
++) {
2564 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2567 case AugAssign_kind
:
2568 return compiler_augassign(c
, s
);
2570 return compiler_print(c
, s
);
2572 return compiler_for(c
, s
);
2574 return compiler_while(c
, s
);
2576 return compiler_if(c
, s
);
2579 if (s
->v
.Raise
.type
) {
2580 VISIT(c
, expr
, s
->v
.Raise
.type
);
2582 if (s
->v
.Raise
.inst
) {
2583 VISIT(c
, expr
, s
->v
.Raise
.inst
);
2585 if (s
->v
.Raise
.tback
) {
2586 VISIT(c
, expr
, s
->v
.Raise
.tback
);
2591 ADDOP_I(c
, RAISE_VARARGS
, n
);
2593 case TryExcept_kind
:
2594 return compiler_try_except(c
, s
);
2595 case TryFinally_kind
:
2596 return compiler_try_finally(c
, s
);
2598 return compiler_assert(c
, s
);
2600 return compiler_import(c
, s
);
2601 case ImportFrom_kind
:
2602 return compiler_from_import(c
, s
);
2604 VISIT(c
, expr
, s
->v
.Exec
.body
);
2605 if (s
->v
.Exec
.globals
) {
2606 VISIT(c
, expr
, s
->v
.Exec
.globals
);
2607 if (s
->v
.Exec
.locals
) {
2608 VISIT(c
, expr
, s
->v
.Exec
.locals
);
2613 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2616 ADDOP(c
, EXEC_STMT
);
2621 VISIT(c
, expr
, s
->v
.Expr
.value
);
2622 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2623 ADDOP(c
, PRINT_EXPR
);
2632 if (!c
->u
->u_nfblocks
)
2633 return compiler_error(c
, "'break' outside loop");
2634 ADDOP(c
, BREAK_LOOP
);
2637 return compiler_continue(c
);
2643 unaryop(unaryop_ty op
)
2647 return UNARY_INVERT
;
2651 return UNARY_POSITIVE
;
2653 return UNARY_NEGATIVE
;
2659 binop(struct compiler
*c
, operator_ty op
)
2665 return BINARY_SUBTRACT
;
2667 return BINARY_MULTIPLY
;
2669 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2670 return BINARY_TRUE_DIVIDE
;
2672 return BINARY_DIVIDE
;
2674 return BINARY_MODULO
;
2676 return BINARY_POWER
;
2678 return BINARY_LSHIFT
;
2680 return BINARY_RSHIFT
;
2688 return BINARY_FLOOR_DIVIDE
;
2712 return PyCmp_IS_NOT
;
2716 return PyCmp_NOT_IN
;
2722 inplace_binop(struct compiler
*c
, operator_ty op
)
2728 return INPLACE_SUBTRACT
;
2730 return INPLACE_MULTIPLY
;
2732 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2733 return INPLACE_TRUE_DIVIDE
;
2735 return INPLACE_DIVIDE
;
2737 return INPLACE_MODULO
;
2739 return INPLACE_POWER
;
2741 return INPLACE_LSHIFT
;
2743 return INPLACE_RSHIFT
;
2751 return INPLACE_FLOOR_DIVIDE
;
2753 PyErr_Format(PyExc_SystemError
,
2754 "inplace binary op %d should not be possible", op
);
2759 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2762 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2764 PyObject
*dict
= c
->u
->u_names
;
2766 /* XXX AugStore isn't used anywhere! */
2768 /* First check for assignment to __debug__. Param? */
2769 if ((ctx
== Store
|| ctx
== AugStore
|| ctx
== Del
)
2770 && !strcmp(PyString_AS_STRING(name
), "__debug__")) {
2771 return compiler_error(c
, "can not assign to __debug__");
2774 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2780 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2783 dict
= c
->u
->u_freevars
;
2787 dict
= c
->u
->u_cellvars
;
2791 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2794 case GLOBAL_IMPLICIT
:
2795 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2796 !c
->u
->u_ste
->ste_unoptimized
)
2799 case GLOBAL_EXPLICIT
:
2803 /* scope can be 0 */
2807 /* XXX Leave assert here, but handle __doc__ and the like better */
2808 assert(scope
|| PyString_AS_STRING(name
)[0] == '_');
2813 case Load
: op
= LOAD_DEREF
; break;
2814 case Store
: op
= STORE_DEREF
; break;
2819 PyErr_Format(PyExc_SyntaxError
,
2820 "can not delete variable '%s' referenced "
2822 PyString_AS_STRING(name
));
2827 PyErr_SetString(PyExc_SystemError
,
2828 "param invalid for deref variable");
2834 case Load
: op
= LOAD_FAST
; break;
2835 case Store
: op
= STORE_FAST
; break;
2836 case Del
: op
= DELETE_FAST
; break;
2842 PyErr_SetString(PyExc_SystemError
,
2843 "param invalid for local variable");
2846 ADDOP_O(c
, op
, mangled
, varnames
);
2851 case Load
: op
= LOAD_GLOBAL
; break;
2852 case Store
: op
= STORE_GLOBAL
; break;
2853 case Del
: op
= DELETE_GLOBAL
; break;
2859 PyErr_SetString(PyExc_SystemError
,
2860 "param invalid for global variable");
2866 case Load
: op
= LOAD_NAME
; break;
2867 case Store
: op
= STORE_NAME
; break;
2868 case Del
: op
= DELETE_NAME
; break;
2874 PyErr_SetString(PyExc_SystemError
,
2875 "param invalid for name variable");
2882 arg
= compiler_add_o(c
, dict
, mangled
);
2886 return compiler_addop_i(c
, op
, arg
);
2890 compiler_boolop(struct compiler
*c
, expr_ty e
)
2896 assert(e
->kind
== BoolOp_kind
);
2897 if (e
->v
.BoolOp
.op
== And
)
2898 jumpi
= JUMP_IF_FALSE
;
2900 jumpi
= JUMP_IF_TRUE
;
2901 end
= compiler_new_block(c
);
2904 s
= e
->v
.BoolOp
.values
;
2905 n
= asdl_seq_LEN(s
) - 1;
2906 for (i
= 0; i
< n
; ++i
) {
2907 VISIT(c
, expr
, asdl_seq_GET(s
, i
));
2908 ADDOP_JREL(c
, jumpi
, end
);
2911 VISIT(c
, expr
, asdl_seq_GET(s
, n
));
2912 compiler_use_next_block(c
, end
);
2917 compiler_list(struct compiler
*c
, expr_ty e
)
2919 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
2920 if (e
->v
.List
.ctx
== Store
) {
2921 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2923 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
2924 if (e
->v
.List
.ctx
== Load
) {
2925 ADDOP_I(c
, BUILD_LIST
, n
);
2931 compiler_tuple(struct compiler
*c
, expr_ty e
)
2933 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
2934 if (e
->v
.Tuple
.ctx
== Store
) {
2935 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2937 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
2938 if (e
->v
.Tuple
.ctx
== Load
) {
2939 ADDOP_I(c
, BUILD_TUPLE
, n
);
2945 compiler_compare(struct compiler
*c
, expr_ty e
)
2948 basicblock
*cleanup
= NULL
;
2950 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2951 VISIT(c
, expr
, e
->v
.Compare
.left
);
2952 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
2955 cleanup
= compiler_new_block(c
);
2956 if (cleanup
== NULL
)
2958 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
2960 for (i
= 1; i
< n
; i
++) {
2962 ADDOP(c
, ROT_THREE
);
2963 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2964 ADDOP_I(c
, COMPARE_OP
,
2965 cmpop((cmpop_ty
)asdl_seq_GET(e
->v
.Compare
.ops
, i
- 1)));
2966 ADDOP_JREL(c
, JUMP_IF_FALSE
, cleanup
);
2970 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
2972 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
2973 ADDOP_I(c
, COMPARE_OP
,
2974 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2975 cmpop((cmpop_ty
)asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1)));
2977 basicblock
*end
= compiler_new_block(c
);
2980 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2981 compiler_use_next_block(c
, cleanup
);
2984 compiler_use_next_block(c
, end
);
2990 compiler_call(struct compiler
*c
, expr_ty e
)
2994 VISIT(c
, expr
, e
->v
.Call
.func
);
2995 n
= asdl_seq_LEN(e
->v
.Call
.args
);
2996 VISIT_SEQ(c
, expr
, e
->v
.Call
.args
);
2997 if (e
->v
.Call
.keywords
) {
2998 VISIT_SEQ(c
, keyword
, e
->v
.Call
.keywords
);
2999 n
|= asdl_seq_LEN(e
->v
.Call
.keywords
) << 8;
3001 if (e
->v
.Call
.starargs
) {
3002 VISIT(c
, expr
, e
->v
.Call
.starargs
);
3005 if (e
->v
.Call
.kwargs
) {
3006 VISIT(c
, expr
, e
->v
.Call
.kwargs
);
3011 ADDOP_I(c
, CALL_FUNCTION
, n
);
3014 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
3017 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
3020 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
3027 compiler_listcomp_generator(struct compiler
*c
, PyObject
*tmpname
,
3028 asdl_seq
*generators
, int gen_index
,
3031 /* generate code for the iterator, then each of the ifs,
3032 and then write to the element */
3035 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
3038 start
= compiler_new_block(c
);
3039 skip
= compiler_new_block(c
);
3040 if_cleanup
= compiler_new_block(c
);
3041 anchor
= compiler_new_block(c
);
3043 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
3047 l
= asdl_seq_GET(generators
, gen_index
);
3048 VISIT(c
, expr
, l
->iter
);
3050 compiler_use_next_block(c
, start
);
3051 ADDOP_JREL(c
, FOR_ITER
, anchor
);
3053 VISIT(c
, expr
, l
->target
);
3055 /* XXX this needs to be cleaned up...a lot! */
3056 n
= asdl_seq_LEN(l
->ifs
);
3057 for (i
= 0; i
< n
; i
++) {
3058 expr_ty e
= asdl_seq_GET(l
->ifs
, i
);
3060 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
3065 if (++gen_index
< asdl_seq_LEN(generators
))
3066 if (!compiler_listcomp_generator(c
, tmpname
,
3067 generators
, gen_index
, elt
))
3070 /* only append after the last for generator */
3071 if (gen_index
>= asdl_seq_LEN(generators
)) {
3072 if (!compiler_nameop(c
, tmpname
, Load
))
3074 VISIT(c
, expr
, elt
);
3075 ADDOP_I(c
, CALL_FUNCTION
, 1);
3078 compiler_use_next_block(c
, skip
);
3080 for (i
= 0; i
< n
; i
++) {
3081 ADDOP_I(c
, JUMP_FORWARD
, 1);
3083 compiler_use_next_block(c
, if_cleanup
);
3086 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
3087 compiler_use_next_block(c
, anchor
);
3088 /* delete the append method added to locals */
3090 if (!compiler_nameop(c
, tmpname
, Del
))
3097 compiler_listcomp(struct compiler
*c
, expr_ty e
)
3102 static identifier append
;
3103 asdl_seq
*generators
= e
->v
.ListComp
.generators
;
3105 assert(e
->kind
== ListComp_kind
);
3107 append
= PyString_InternFromString("append");
3111 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->u
->u_tmpname
);
3112 tmp
= PyString_FromString(tmpname
);
3115 ADDOP_I(c
, BUILD_LIST
, 0);
3117 ADDOP_O(c
, LOAD_ATTR
, append
, names
);
3118 if (compiler_nameop(c
, tmp
, Store
))
3119 rc
= compiler_listcomp_generator(c
, tmp
, generators
, 0,
3126 compiler_genexp_generator(struct compiler
*c
,
3127 asdl_seq
*generators
, int gen_index
,
3130 /* generate code for the iterator, then each of the ifs,
3131 and then write to the element */
3133 comprehension_ty ge
;
3134 basicblock
*start
, *anchor
, *skip
, *if_cleanup
, *end
;
3137 start
= compiler_new_block(c
);
3138 skip
= compiler_new_block(c
);
3139 if_cleanup
= compiler_new_block(c
);
3140 anchor
= compiler_new_block(c
);
3141 end
= compiler_new_block(c
);
3143 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
3144 anchor
== NULL
|| end
== NULL
)
3147 ge
= asdl_seq_GET(generators
, gen_index
);
3148 ADDOP_JREL(c
, SETUP_LOOP
, end
);
3149 if (!compiler_push_fblock(c
, LOOP
, start
))
3152 if (gen_index
== 0) {
3153 /* Receive outermost iter as an implicit argument */
3154 c
->u
->u_argcount
= 1;
3155 ADDOP_I(c
, LOAD_FAST
, 0);
3158 /* Sub-iter - calculate on the fly */
3159 VISIT(c
, expr
, ge
->iter
);
3162 compiler_use_next_block(c
, start
);
3163 ADDOP_JREL(c
, FOR_ITER
, anchor
);
3165 VISIT(c
, expr
, ge
->target
);
3167 /* XXX this needs to be cleaned up...a lot! */
3168 n
= asdl_seq_LEN(ge
->ifs
);
3169 for (i
= 0; i
< n
; i
++) {
3170 expr_ty e
= asdl_seq_GET(ge
->ifs
, i
);
3172 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
3177 if (++gen_index
< asdl_seq_LEN(generators
))
3178 if (!compiler_genexp_generator(c
, generators
, gen_index
, elt
))
3181 /* only append after the last 'for' generator */
3182 if (gen_index
>= asdl_seq_LEN(generators
)) {
3183 VISIT(c
, expr
, elt
);
3184 ADDOP(c
, YIELD_VALUE
);
3187 compiler_use_next_block(c
, skip
);
3189 for (i
= 0; i
< n
; i
++) {
3190 ADDOP_I(c
, JUMP_FORWARD
, 1);
3192 compiler_use_next_block(c
, if_cleanup
);
3196 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
3197 compiler_use_next_block(c
, anchor
);
3198 ADDOP(c
, POP_BLOCK
);
3199 compiler_pop_fblock(c
, LOOP
, start
);
3200 compiler_use_next_block(c
, end
);
3206 compiler_genexp(struct compiler
*c
, expr_ty e
)
3208 static identifier name
;
3210 expr_ty outermost_iter
= ((comprehension_ty
)
3211 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
,
3215 name
= PyString_FromString("<genexpr>");
3220 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
3222 compiler_genexp_generator(c
, e
->v
.GeneratorExp
.generators
, 0,
3223 e
->v
.GeneratorExp
.elt
);
3224 co
= assemble(c
, 1);
3225 compiler_exit_scope(c
);
3229 compiler_make_closure(c
, co
, 0);
3232 VISIT(c
, expr
, outermost_iter
);
3234 ADDOP_I(c
, CALL_FUNCTION
, 1);
3240 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
3242 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
3243 VISIT(c
, expr
, k
->value
);
3247 /* Test whether expression is constant. For constants, report
3248 whether they are true or false.
3250 Return values: 1 for true, 0 for false, -1 for non-constant.
3254 expr_constant(expr_ty e
)
3258 return PyObject_IsTrue(e
->v
.Num
.n
);
3260 return PyObject_IsTrue(e
->v
.Str
.s
);
3267 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
3271 if (e
->lineno
> c
->u
->u_lineno
) {
3272 c
->u
->u_lineno
= e
->lineno
;
3273 c
->u
->u_lineno_set
= false;
3277 return compiler_boolop(c
, e
);
3279 VISIT(c
, expr
, e
->v
.BinOp
.left
);
3280 VISIT(c
, expr
, e
->v
.BinOp
.right
);
3281 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
3284 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
3285 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
3288 return compiler_lambda(c
, e
);
3290 /* XXX get rid of arg? */
3291 ADDOP_I(c
, BUILD_MAP
, 0);
3292 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
3293 /* We must arrange things just right for STORE_SUBSCR.
3294 It wants the stack to look like (value) (dict) (key) */
3295 for (i
= 0; i
< n
; i
++) {
3297 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Dict
.values
, i
));
3299 VISIT(c
, expr
, asdl_seq_GET(e
->v
.Dict
.keys
, i
));
3300 ADDOP(c
, STORE_SUBSCR
);
3304 return compiler_listcomp(c
, e
);
3305 case GeneratorExp_kind
:
3306 return compiler_genexp(c
, e
);
3308 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
3309 return compiler_error(c
, "'yield' outside function");
3311 for (i = 0; i < c->u->u_nfblocks; i++) {
3312 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3313 return compiler_error(
3314 c, "'yield' not allowed in a 'try' "
3315 "block with a 'finally' clause");
3318 if (e
->v
.Yield
.value
) {
3319 VISIT(c
, expr
, e
->v
.Yield
.value
);
3322 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3324 ADDOP(c
, YIELD_VALUE
);
3327 return compiler_compare(c
, e
);
3329 return compiler_call(c
, e
);
3331 VISIT(c
, expr
, e
->v
.Repr
.value
);
3332 ADDOP(c
, UNARY_CONVERT
);
3335 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
3338 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
3340 /* The following exprs can be assignment targets. */
3341 case Attribute_kind
:
3342 if (e
->v
.Attribute
.ctx
!= AugStore
)
3343 VISIT(c
, expr
, e
->v
.Attribute
.value
);
3344 switch (e
->v
.Attribute
.ctx
) {
3347 /* Fall through to load */
3349 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
3353 /* Fall through to save */
3355 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
3358 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
3362 PyErr_SetString(PyExc_SystemError
,
3363 "param invalid in attribute expression");
3367 case Subscript_kind
:
3368 switch (e
->v
.Subscript
.ctx
) {
3370 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3371 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
3374 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3375 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
3378 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
3381 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3382 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
3385 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3386 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
3390 PyErr_SetString(PyExc_SystemError
,
3391 "param invalid in subscript expression");
3396 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3397 /* child nodes of List and Tuple will have expr_context set */
3399 return compiler_list(c
, e
);
3401 return compiler_tuple(c
, e
);
3407 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3409 expr_ty e
= s
->v
.AugAssign
.target
;
3412 assert(s
->kind
== AugAssign_kind
);
3415 case Attribute_kind
:
3416 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3417 AugLoad
, e
->lineno
, c
->c_arena
);
3420 VISIT(c
, expr
, auge
);
3421 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3422 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3423 auge
->v
.Attribute
.ctx
= AugStore
;
3424 VISIT(c
, expr
, auge
);
3426 case Subscript_kind
:
3427 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3428 AugLoad
, e
->lineno
, c
->c_arena
);
3431 VISIT(c
, expr
, auge
);
3432 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3433 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3434 auge
->v
.Subscript
.ctx
= AugStore
;
3435 VISIT(c
, expr
, auge
);
3438 VISIT(c
, expr
, s
->v
.AugAssign
.target
);
3439 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3440 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3441 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3443 PyErr_Format(PyExc_SystemError
,
3444 "invalid node type (%d) for augmented assignment",
3452 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3454 struct fblockinfo
*f
;
3455 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
)
3457 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3464 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3466 struct compiler_unit
*u
= c
->u
;
3467 assert(u
->u_nfblocks
> 0);
3469 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3470 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3473 /* Raises a SyntaxError and returns 0.
3474 If something goes wrong, a different exception may be raised.
3478 compiler_error(struct compiler
*c
, const char *errstr
)
3481 PyObject
*u
= NULL
, *v
= NULL
;
3483 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3488 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3492 v
= Py_BuildValue("(zO)", errstr
, u
);
3495 PyErr_SetObject(PyExc_SyntaxError
, v
);
3504 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3505 expr_context_ty ctx
)
3509 /* XXX this code is duplicated */
3511 case AugLoad
: /* fall through to Load */
3512 case Load
: op
= BINARY_SUBSCR
; break;
3513 case AugStore
:/* fall through to Store */
3514 case Store
: op
= STORE_SUBSCR
; break;
3515 case Del
: op
= DELETE_SUBSCR
; break;
3517 PyErr_Format(PyExc_SystemError
,
3518 "invalid %s kind %d in subscript\n",
3522 if (ctx
== AugLoad
) {
3523 ADDOP_I(c
, DUP_TOPX
, 2);
3525 else if (ctx
== AugStore
) {
3526 ADDOP(c
, ROT_THREE
);
3533 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3536 assert(s
->kind
== Slice_kind
);
3538 /* only handles the cases where BUILD_SLICE is emitted */
3539 if (s
->v
.Slice
.lower
) {
3540 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3543 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3546 if (s
->v
.Slice
.upper
) {
3547 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3550 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3553 if (s
->v
.Slice
.step
) {
3555 VISIT(c
, expr
, s
->v
.Slice
.step
);
3557 ADDOP_I(c
, BUILD_SLICE
, n
);
3562 compiler_simple_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3564 int op
= 0, slice_offset
= 0, stack_count
= 0;
3566 assert(s
->v
.Slice
.step
== NULL
);
3567 if (s
->v
.Slice
.lower
) {
3570 if (ctx
!= AugStore
)
3571 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3573 if (s
->v
.Slice
.upper
) {
3576 if (ctx
!= AugStore
)
3577 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3580 if (ctx
== AugLoad
) {
3581 switch (stack_count
) {
3582 case 0: ADDOP(c
, DUP_TOP
); break;
3583 case 1: ADDOP_I(c
, DUP_TOPX
, 2); break;
3584 case 2: ADDOP_I(c
, DUP_TOPX
, 3); break;
3587 else if (ctx
== AugStore
) {
3588 switch (stack_count
) {
3589 case 0: ADDOP(c
, ROT_TWO
); break;
3590 case 1: ADDOP(c
, ROT_THREE
); break;
3591 case 2: ADDOP(c
, ROT_FOUR
); break;
3596 case AugLoad
: /* fall through to Load */
3597 case Load
: op
= SLICE
; break;
3598 case AugStore
:/* fall through to Store */
3599 case Store
: op
= STORE_SLICE
; break;
3600 case Del
: op
= DELETE_SLICE
; break;
3603 PyErr_SetString(PyExc_SystemError
,
3604 "param invalid in simple slice");
3608 ADDOP(c
, op
+ slice_offset
);
3613 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3614 expr_context_ty ctx
)
3618 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3621 return compiler_slice(c
, s
, ctx
);
3623 VISIT(c
, expr
, s
->v
.Index
.value
);
3627 PyErr_SetString(PyExc_SystemError
,
3628 "extended slice invalid in nested slice");
3636 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3640 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3643 if (!s
->v
.Slice
.step
)
3644 return compiler_simple_slice(c
, s
, ctx
);
3645 if (!compiler_slice(c
, s
, ctx
))
3647 if (ctx
== AugLoad
) {
3648 ADDOP_I(c
, DUP_TOPX
, 2);
3650 else if (ctx
== AugStore
) {
3651 ADDOP(c
, ROT_THREE
);
3653 return compiler_handle_subscr(c
, "slice", ctx
);
3654 case ExtSlice_kind
: {
3655 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3656 for (i
= 0; i
< n
; i
++) {
3657 slice_ty sub
= asdl_seq_GET(s
->v
.ExtSlice
.dims
, i
);
3658 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3661 ADDOP_I(c
, BUILD_TUPLE
, n
);
3662 return compiler_handle_subscr(c
, "extended slice", ctx
);
3665 if (ctx
!= AugStore
)
3666 VISIT(c
, expr
, s
->v
.Index
.value
);
3667 return compiler_handle_subscr(c
, "index", ctx
);
3669 PyErr_Format(PyExc_SystemError
,
3670 "invalid slice %d", s
->kind
);
3676 /* do depth-first search of basic block graph, starting with block.
3677 post records the block indices in post-order.
3679 XXX must handle implicit jumps from one block to next
3683 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3686 struct instr
*instr
= NULL
;
3691 if (b
->b_next
!= NULL
)
3692 dfs(c
, b
->b_next
, a
);
3693 for (i
= 0; i
< b
->b_iused
; i
++) {
3694 instr
= &b
->b_instr
[i
];
3695 if (instr
->i_jrel
|| instr
->i_jabs
)
3696 dfs(c
, instr
->i_target
, a
);
3698 a
->a_postorder
[a
->a_nblocks
++] = b
;
3702 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3705 struct instr
*instr
;
3706 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3709 b
->b_startdepth
= depth
;
3710 for (i
= 0; i
< b
->b_iused
; i
++) {
3711 instr
= &b
->b_instr
[i
];
3712 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3713 if (depth
> maxdepth
)
3715 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3716 if (instr
->i_jrel
|| instr
->i_jabs
) {
3717 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3719 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3720 instr
->i_opcode
== JUMP_FORWARD
) {
3721 goto out
; /* remaining code is dead */
3726 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3732 /* Find the flow path that needs the largest stack. We assume that
3733 * cycles in the flow graph have no net effect on the stack depth.
3736 stackdepth(struct compiler
*c
)
3738 basicblock
*b
, *entryblock
;
3740 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3742 b
->b_startdepth
= INT_MIN
;
3745 return stackdepth_walk(c
, entryblock
, 0, 0);
3749 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3751 memset(a
, 0, sizeof(struct assembler
));
3752 a
->a_lineno
= firstlineno
;
3753 a
->a_bytecode
= PyString_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3756 a
->a_lnotab
= PyString_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3759 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3760 sizeof(basicblock
*) * nblocks
);
3761 if (!a
->a_postorder
) {
3769 assemble_free(struct assembler
*a
)
3771 Py_XDECREF(a
->a_bytecode
);
3772 Py_XDECREF(a
->a_lnotab
);
3774 PyObject_Free(a
->a_postorder
);
3777 /* Return the size of a basic block in bytes. */
3780 instrsize(struct instr
*instr
)
3782 if (!instr
->i_hasarg
)
3784 if (instr
->i_oparg
> 0xffff)
3790 blocksize(basicblock
*b
)
3795 for (i
= 0; i
< b
->b_iused
; i
++)
3796 size
+= instrsize(&b
->b_instr
[i
]);
3800 /* All about a_lnotab.
3802 c_lnotab is an array of unsigned bytes disguised as a Python string.
3803 It is used to map bytecode offsets to source code line #s (when needed
3806 The array is conceptually a list of
3807 (bytecode offset increment, line number increment)
3808 pairs. The details are important and delicate, best illustrated by example:
3810 byte code offset source code line number
3817 The first trick is that these numbers aren't stored, only the increments
3818 from one row to the next (this doesn't really work, but it's a start):
3820 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3822 The second trick is that an unsigned byte can't hold negative values, or
3823 values larger than 255, so (a) there's a deep assumption that byte code
3824 offsets and their corresponding line #s both increase monotonically, and (b)
3825 if at least one column jumps by more than 255 from one row to the next, more
3826 than one pair is written to the table. In case #b, there's no way to know
3827 from looking at the table later how many were written. That's the delicate
3828 part. A user of c_lnotab desiring to find the source line number
3829 corresponding to a bytecode address A should do something like this
3832 for addr_incr, line_incr in c_lnotab:
3838 In order for this to work, when the addr field increments by more than 255,
3839 the line # increment in each pair generated must be 0 until the remaining addr
3840 increment is < 256. So, in the example above, com_set_lineno should not (as
3841 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3842 255, 0, 45, 255, 0, 45.
3846 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
3848 int d_bytecode
, d_lineno
;
3852 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
3853 d_lineno
= i
->i_lineno
- a
->a_lineno
;
3855 assert(d_bytecode
>= 0);
3856 assert(d_lineno
>= 0);
3861 if (d_bytecode
> 255) {
3862 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
3863 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3864 len
= PyString_GET_SIZE(a
->a_lnotab
);
3865 if (nbytes
>= len
) {
3866 if (len
* 2 < nbytes
)
3870 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3873 lnotab
= PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3874 for (j
= 0; j
< ncodes
; j
++) {
3878 d_bytecode
-= ncodes
* 255;
3879 a
->a_lnotab_off
+= ncodes
* 2;
3881 assert(d_bytecode
<= 255);
3882 if (d_lineno
> 255) {
3883 int j
, nbytes
, ncodes
= d_lineno
/ 255;
3884 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3885 len
= PyString_GET_SIZE(a
->a_lnotab
);
3886 if (nbytes
>= len
) {
3887 if (len
* 2 < nbytes
)
3891 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3894 lnotab
= PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3896 *lnotab
++ = d_bytecode
;
3898 for (j
= 1; j
< ncodes
; j
++) {
3902 d_lineno
-= ncodes
* 255;
3903 a
->a_lnotab_off
+= ncodes
* 2;
3906 len
= PyString_GET_SIZE(a
->a_lnotab
);
3907 if (a
->a_lnotab_off
+ 2 >= len
) {
3908 if (_PyString_Resize(&a
->a_lnotab
, len
* 2) < 0)
3911 lnotab
= PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3913 a
->a_lnotab_off
+= 2;
3915 *lnotab
++ = d_bytecode
;
3916 *lnotab
++ = d_lineno
;
3918 else { /* First line of a block; def stmt, etc. */
3920 *lnotab
++ = d_lineno
;
3922 a
->a_lineno
= i
->i_lineno
;
3923 a
->a_lineno_off
= a
->a_offset
;
3928 Extend the bytecode with a new instruction.
3929 Update lnotab if necessary.
3933 assemble_emit(struct assembler
*a
, struct instr
*i
)
3935 int size
, arg
= 0, ext
= 0;
3936 int len
= PyString_GET_SIZE(a
->a_bytecode
);
3939 size
= instrsize(i
);
3944 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
3946 if (a
->a_offset
+ size
>= len
) {
3947 if (_PyString_Resize(&a
->a_bytecode
, len
* 2) < 0)
3950 code
= PyString_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
3951 a
->a_offset
+= size
;
3953 assert(i
->i_hasarg
);
3954 *code
++ = (char)EXTENDED_ARG
;
3955 *code
++ = ext
& 0xff;
3959 *code
++ = i
->i_opcode
;
3961 assert(size
== 3 || size
== 6);
3962 *code
++ = arg
& 0xff;
3969 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
3972 int bsize
, totsize
, extended_arg_count
, last_extended_arg_count
= 0;
3975 /* Compute the size of each block and fixup jump args.
3976 Replace block pointer with position in bytecode. */
3979 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
3980 b
= a
->a_postorder
[i
];
3981 bsize
= blocksize(b
);
3982 b
->b_offset
= totsize
;
3985 extended_arg_count
= 0;
3986 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3987 bsize
= b
->b_offset
;
3988 for (i
= 0; i
< b
->b_iused
; i
++) {
3989 struct instr
*instr
= &b
->b_instr
[i
];
3990 /* Relative jumps are computed relative to
3991 the instruction pointer after fetching
3992 the jump instruction.
3994 bsize
+= instrsize(instr
);
3996 instr
->i_oparg
= instr
->i_target
->b_offset
;
3997 else if (instr
->i_jrel
) {
3998 int delta
= instr
->i_target
->b_offset
- bsize
;
3999 instr
->i_oparg
= delta
;
4003 if (instr
->i_oparg
> 0xffff)
4004 extended_arg_count
++;
4008 /* XXX: This is an awful hack that could hurt performance, but
4009 on the bright side it should work until we come up
4010 with a better solution.
4012 In the meantime, should the goto be dropped in favor
4015 The issue is that in the first loop blocksize() is called
4016 which calls instrsize() which requires i_oparg be set
4017 appropriately. There is a bootstrap problem because
4018 i_oparg is calculated in the second loop above.
4020 So we loop until we stop seeing new EXTENDED_ARGs.
4021 The only EXTENDED_ARGs that could be popping up are
4022 ones in jump instructions. So this should converge
4025 if (last_extended_arg_count
!= extended_arg_count
) {
4026 last_extended_arg_count
= extended_arg_count
;
4032 dict_keys_inorder(PyObject
*dict
, int offset
)
4034 PyObject
*tuple
, *k
, *v
;
4035 int i
, pos
= 0, size
= PyDict_Size(dict
);
4037 tuple
= PyTuple_New(size
);
4040 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
4041 i
= PyInt_AS_LONG(v
);
4042 k
= PyTuple_GET_ITEM(k
, 0);
4044 assert((i
- offset
) < size
);
4045 assert((i
- offset
) >= 0);
4046 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
4052 compute_code_flags(struct compiler
*c
)
4054 PySTEntryObject
*ste
= c
->u
->u_ste
;
4056 if (ste
->ste_type
!= ModuleBlock
)
4057 flags
|= CO_NEWLOCALS
;
4058 if (ste
->ste_type
== FunctionBlock
) {
4059 if (!ste
->ste_unoptimized
)
4060 flags
|= CO_OPTIMIZED
;
4061 if (ste
->ste_nested
)
4063 if (ste
->ste_generator
)
4064 flags
|= CO_GENERATOR
;
4066 if (ste
->ste_varargs
)
4067 flags
|= CO_VARARGS
;
4068 if (ste
->ste_varkeywords
)
4069 flags
|= CO_VARKEYWORDS
;
4070 if (ste
->ste_generator
)
4071 flags
|= CO_GENERATOR
;
4072 if (c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
4073 flags
|= CO_FUTURE_DIVISION
;
4074 n
= PyDict_Size(c
->u
->u_freevars
);
4078 n
= PyDict_Size(c
->u
->u_cellvars
);
4089 static PyCodeObject
*
4090 makecode(struct compiler
*c
, struct assembler
*a
)
4093 PyCodeObject
*co
= NULL
;
4094 PyObject
*consts
= NULL
;
4095 PyObject
*names
= NULL
;
4096 PyObject
*varnames
= NULL
;
4097 PyObject
*filename
= NULL
;
4098 PyObject
*name
= NULL
;
4099 PyObject
*freevars
= NULL
;
4100 PyObject
*cellvars
= NULL
;
4101 PyObject
*bytecode
= NULL
;
4104 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
4107 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
4110 names
= dict_keys_inorder(c
->u
->u_names
, 0);
4111 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
4112 if (!consts
|| !names
|| !varnames
)
4115 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
4118 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
4121 filename
= PyString_FromString(c
->c_filename
);
4125 nlocals
= PyDict_Size(c
->u
->u_varnames
);
4126 flags
= compute_code_flags(c
);
4130 bytecode
= optimize_code(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
4134 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
4140 co
= PyCode_New(c
->u
->u_argcount
, nlocals
, stackdepth(c
), flags
,
4141 bytecode
, consts
, names
, varnames
,
4143 filename
, c
->u
->u_name
,
4144 c
->u
->u_firstlineno
,
4149 Py_XDECREF(varnames
);
4150 Py_XDECREF(filename
);
4152 Py_XDECREF(freevars
);
4153 Py_XDECREF(cellvars
);
4154 Py_XDECREF(bytecode
);
4158 static PyCodeObject
*
4159 assemble(struct compiler
*c
, int addNone
)
4161 basicblock
*b
, *entryblock
;
4164 PyCodeObject
*co
= NULL
;
4166 /* Make sure every block that falls off the end returns None.
4167 XXX NEXT_BLOCK() isn't quite right, because if the last
4168 block ends with a jump or return b_next shouldn't set.
4170 if (!c
->u
->u_curblock
->b_return
) {
4173 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
4174 ADDOP(c
, RETURN_VALUE
);
4179 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
4184 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
4186 dfs(c
, entryblock
, &a
);
4188 /* Can't modify the bytecode after computing jump offsets. */
4189 assemble_jump_offsets(&a
, c
);
4191 /* Emit code in reverse postorder from dfs. */
4192 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
4193 b
= a
.a_postorder
[i
];
4194 for (j
= 0; j
< b
->b_iused
; j
++)
4195 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
4199 if (_PyString_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
4201 if (_PyString_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
4204 co
= makecode(c
, &a
);