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
12 * 5. Optimize the byte code (peephole optimizations). See peephole.c
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
17 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
20 * structure takes care of releasing those. Use the arena to manage
26 #include "Python-ast.h"
35 int Py_OptimizeFlag
= 0;
37 #define DEFAULT_BLOCK_SIZE 16
38 #define DEFAULT_BLOCKS 8
39 #define DEFAULT_CODE_SIZE 128
40 #define DEFAULT_LNOTAB_SIZE 16
45 unsigned i_hasarg
: 1;
46 unsigned char i_opcode
;
48 struct basicblock_
*i_target
; /* target block (if jump instruction) */
52 typedef struct basicblock_
{
53 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
56 struct basicblock_
*b_list
;
57 /* number of instructions used */
59 /* length of instruction array (b_instr) */
61 /* pointer to an array of instructions, initially NULL */
62 struct instr
*b_instr
;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_
*b_next
;
66 /* b_seen is used to perform a DFS of basicblocks. */
68 /* b_return is true if a RETURN_VALUE opcode is inserted. */
69 unsigned b_return
: 1;
70 /* depth of stack upon entry of block, computed by stackdepth() */
72 /* instruction offset for block, computed by assemble_jump_offsets() */
76 /* fblockinfo tracks the current frame block.
78 A frame block is used to handle loops, try/except, and try/finally.
79 It's called a frame block to distinguish it from a basic block in the
83 enum fblocktype
{ LOOP
, EXCEPT
, FINALLY_TRY
, FINALLY_END
};
86 enum fblocktype fb_type
;
90 /* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
93 struct compiler_unit
{
94 PySTEntryObject
*u_ste
;
97 /* The following fields are dicts that map objects to
98 the index of them in co_XXX. The index is used as
99 the argument for opcodes that refer to those collections.
101 PyObject
*u_consts
; /* all constants */
102 PyObject
*u_names
; /* all names */
103 PyObject
*u_varnames
; /* local variables */
104 PyObject
*u_cellvars
; /* cell variables */
105 PyObject
*u_freevars
; /* free variables */
107 PyObject
*u_private
; /* for private name mangling */
109 int u_argcount
; /* number of arguments for block */
110 /* Pointer to the most recently allocated block. By following b_list
111 members, you can reach all early allocated blocks. */
112 basicblock
*u_blocks
;
113 basicblock
*u_curblock
; /* pointer to current block */
114 int u_tmpname
; /* temporary variables for list comps */
117 struct fblockinfo u_fblock
[CO_MAXBLOCKS
];
119 int u_firstlineno
; /* the first lineno of the block */
120 int u_lineno
; /* the lineno for the current stmt */
121 bool u_lineno_set
; /* boolean to indicate whether instr
122 has been generated with current lineno */
125 /* This struct captures the global state of a compilation.
127 The u pointer points to the current compilation unit, while units
128 for enclosing blocks are stored in c_stack. The u and c_stack are
129 managed by compiler_enter_scope() and compiler_exit_scope().
133 const char *c_filename
;
134 struct symtable
*c_st
;
135 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
136 PyCompilerFlags
*c_flags
;
138 int c_interactive
; /* true if in interactive mode */
141 struct compiler_unit
*u
; /* compiler state for current block */
142 PyObject
*c_stack
; /* Python list holding compiler_unit ptrs */
143 char *c_encoding
; /* source encoding (a borrowed reference) */
144 PyArena
*c_arena
; /* pointer to memory allocation arena */
147 static int compiler_enter_scope(struct compiler
*, identifier
, void *, int);
148 static void compiler_free(struct compiler
*);
149 static basicblock
*compiler_new_block(struct compiler
*);
150 static int compiler_next_instr(struct compiler
*, basicblock
*);
151 static int compiler_addop(struct compiler
*, int);
152 static int compiler_addop_o(struct compiler
*, int, PyObject
*, PyObject
*);
153 static int compiler_addop_i(struct compiler
*, int, int);
154 static int compiler_addop_j(struct compiler
*, int, basicblock
*, int);
155 static basicblock
*compiler_use_new_block(struct compiler
*);
156 static int compiler_error(struct compiler
*, const char *);
157 static int compiler_nameop(struct compiler
*, identifier
, expr_context_ty
);
159 static PyCodeObject
*compiler_mod(struct compiler
*, mod_ty
);
160 static int compiler_visit_stmt(struct compiler
*, stmt_ty
);
161 static int compiler_visit_keyword(struct compiler
*, keyword_ty
);
162 static int compiler_visit_expr(struct compiler
*, expr_ty
);
163 static int compiler_augassign(struct compiler
*, stmt_ty
);
164 static int compiler_visit_slice(struct compiler
*, slice_ty
,
167 static int compiler_push_fblock(struct compiler
*, enum fblocktype
,
169 static void compiler_pop_fblock(struct compiler
*, enum fblocktype
,
171 /* Returns true if there is a loop on the fblock stack. */
172 static int compiler_in_loop(struct compiler
*);
174 static int inplace_binop(struct compiler
*, operator_ty
);
175 static int expr_constant(expr_ty e
);
177 static int compiler_with(struct compiler
*, stmt_ty
);
179 static PyCodeObject
*assemble(struct compiler
*, int addNone
);
180 static PyObject
*__doc__
;
183 _Py_Mangle(PyObject
*privateobj
, PyObject
*ident
)
185 /* Name mangling: __private becomes _classname__private.
186 This is independent from how the name is used. */
187 const char *p
, *name
= PyString_AsString(ident
);
190 if (privateobj
== NULL
|| !PyString_Check(privateobj
) ||
191 name
== NULL
|| name
[0] != '_' || name
[1] != '_') {
195 p
= PyString_AsString(privateobj
);
197 if (name
[nlen
-1] == '_' && name
[nlen
-2] == '_') {
199 return ident
; /* Don't mangle __whatever__ */
201 /* Strip leading underscores from class name */
206 return ident
; /* Don't mangle if class is just underscores */
209 ident
= PyString_FromStringAndSize(NULL
, 1 + nlen
+ plen
);
212 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
213 buffer
= PyString_AS_STRING(ident
);
215 strncpy(buffer
+1, p
, plen
);
216 strcpy(buffer
+1+plen
, name
);
221 compiler_init(struct compiler
*c
)
223 memset(c
, 0, sizeof(struct compiler
));
225 c
->c_stack
= PyList_New(0);
233 PyAST_Compile(mod_ty mod
, const char *filename
, PyCompilerFlags
*flags
,
237 PyCodeObject
*co
= NULL
;
238 PyCompilerFlags local_flags
;
242 __doc__
= PyString_InternFromString("__doc__");
247 if (!compiler_init(&c
))
249 c
.c_filename
= filename
;
251 c
.c_future
= PyFuture_FromAST(mod
, filename
);
252 if (c
.c_future
== NULL
)
255 local_flags
.cf_flags
= 0;
256 flags
= &local_flags
;
258 merged
= c
.c_future
->ff_features
| flags
->cf_flags
;
259 c
.c_future
->ff_features
= merged
;
260 flags
->cf_flags
= merged
;
264 c
.c_st
= PySymtable_Build(mod
, filename
, c
.c_future
);
265 if (c
.c_st
== NULL
) {
266 if (!PyErr_Occurred())
267 PyErr_SetString(PyExc_SystemError
, "no symtable");
271 /* XXX initialize to NULL for now, need to handle */
274 co
= compiler_mod(&c
, mod
);
278 assert(co
|| PyErr_Occurred());
283 PyNode_Compile(struct _node
*n
, const char *filename
)
285 PyCodeObject
*co
= NULL
;
287 PyArena
*arena
= PyArena_New();
290 mod
= PyAST_FromNode(n
, NULL
, filename
, arena
);
292 co
= PyAST_Compile(mod
, filename
, NULL
, arena
);
298 compiler_free(struct compiler
*c
)
301 PySymtable_Free(c
->c_st
);
303 PyObject_Free(c
->c_future
);
304 Py_DECREF(c
->c_stack
);
308 list2dict(PyObject
*list
)
312 PyObject
*dict
= PyDict_New();
313 if (!dict
) return NULL
;
315 n
= PyList_Size(list
);
316 for (i
= 0; i
< n
; i
++) {
317 v
= PyInt_FromLong(i
);
322 k
= PyList_GET_ITEM(list
, i
);
323 k
= PyTuple_Pack(2, k
, k
->ob_type
);
324 if (k
== NULL
|| PyDict_SetItem(dict
, k
, v
) < 0) {
336 /* Return new dict containing names from src that match scope(s).
338 src is a symbol table dictionary. If the scope of a name matches
339 either scope_type or flag is set, insert it into the new dict. The
340 values are integers, starting at offset and increasing by one for
345 dictbytype(PyObject
*src
, int scope_type
, int flag
, int offset
)
347 Py_ssize_t pos
= 0, i
= offset
, scope
;
348 PyObject
*k
, *v
, *dest
= PyDict_New();
354 while (PyDict_Next(src
, &pos
, &k
, &v
)) {
355 /* XXX this should probably be a macro in symtable.h */
356 assert(PyInt_Check(v
));
357 scope
= (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
359 if (scope
== scope_type
|| PyInt_AS_LONG(v
) & flag
) {
360 PyObject
*tuple
, *item
= PyInt_FromLong(i
);
366 tuple
= PyTuple_Pack(2, k
, k
->ob_type
);
367 if (!tuple
|| PyDict_SetItem(dest
, tuple
, item
) < 0) {
381 compiler_unit_check(struct compiler_unit
*u
)
384 for (block
= u
->u_blocks
; block
!= NULL
; block
= block
->b_list
) {
385 assert(block
!= (void *)0xcbcbcbcb);
386 assert(block
!= (void *)0xfbfbfbfb);
387 assert(block
!= (void *)0xdbdbdbdb);
388 if (block
->b_instr
!= NULL
) {
389 assert(block
->b_ialloc
> 0);
390 assert(block
->b_iused
> 0);
391 assert(block
->b_ialloc
>= block
->b_iused
);
394 assert (block
->b_iused
== 0);
395 assert (block
->b_ialloc
== 0);
401 compiler_unit_free(struct compiler_unit
*u
)
403 basicblock
*b
, *next
;
405 compiler_unit_check(u
);
409 PyObject_Free((void *)b
->b_instr
);
411 PyObject_Free((void *)b
);
416 Py_CLEAR(u
->u_consts
);
417 Py_CLEAR(u
->u_names
);
418 Py_CLEAR(u
->u_varnames
);
419 Py_CLEAR(u
->u_freevars
);
420 Py_CLEAR(u
->u_cellvars
);
421 Py_CLEAR(u
->u_private
);
426 compiler_enter_scope(struct compiler
*c
, identifier name
, void *key
,
429 struct compiler_unit
*u
;
431 u
= (struct compiler_unit
*)PyObject_Malloc(sizeof(
432 struct compiler_unit
));
437 memset(u
, 0, sizeof(struct compiler_unit
));
439 u
->u_ste
= PySymtable_Lookup(c
->c_st
, key
);
441 compiler_unit_free(u
);
446 u
->u_varnames
= list2dict(u
->u_ste
->ste_varnames
);
447 u
->u_cellvars
= dictbytype(u
->u_ste
->ste_symbols
, CELL
, 0, 0);
448 if (!u
->u_varnames
|| !u
->u_cellvars
) {
449 compiler_unit_free(u
);
453 u
->u_freevars
= dictbytype(u
->u_ste
->ste_symbols
, FREE
, DEF_FREE_CLASS
,
454 PyDict_Size(u
->u_cellvars
));
455 if (!u
->u_freevars
) {
456 compiler_unit_free(u
);
463 u
->u_firstlineno
= lineno
;
465 u
->u_lineno_set
= false;
466 u
->u_consts
= PyDict_New();
468 compiler_unit_free(u
);
471 u
->u_names
= PyDict_New();
473 compiler_unit_free(u
);
479 /* Push the old compiler_unit on the stack. */
481 PyObject
*wrapper
= PyCObject_FromVoidPtr(c
->u
, NULL
);
482 if (!wrapper
|| PyList_Append(c
->c_stack
, wrapper
) < 0) {
484 compiler_unit_free(u
);
488 u
->u_private
= c
->u
->u_private
;
489 Py_XINCREF(u
->u_private
);
494 if (compiler_use_new_block(c
) == NULL
)
501 compiler_exit_scope(struct compiler
*c
)
507 compiler_unit_free(c
->u
);
508 /* Restore c->u to the parent unit. */
509 n
= PyList_GET_SIZE(c
->c_stack
) - 1;
511 wrapper
= PyList_GET_ITEM(c
->c_stack
, n
);
512 c
->u
= (struct compiler_unit
*)PyCObject_AsVoidPtr(wrapper
);
514 /* we are deleting from a list so this really shouldn't fail */
515 if (PySequence_DelItem(c
->c_stack
, n
) < 0)
516 Py_FatalError("compiler_exit_scope()");
517 compiler_unit_check(c
->u
);
524 /* Allocate a new "anonymous" local variable.
525 Used by list comprehensions and with statements.
529 compiler_new_tmpname(struct compiler
*c
)
532 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->u
->u_tmpname
);
533 return PyString_FromString(tmpname
);
536 /* Allocate a new block and return a pointer to it.
537 Returns NULL on error.
541 compiler_new_block(struct compiler
*c
)
544 struct compiler_unit
*u
;
547 b
= (basicblock
*)PyObject_Malloc(sizeof(basicblock
));
552 memset((void *)b
, 0, sizeof(basicblock
));
553 /* Extend the singly linked list of blocks with new block. */
554 b
->b_list
= u
->u_blocks
;
560 compiler_use_new_block(struct compiler
*c
)
562 basicblock
*block
= compiler_new_block(c
);
565 c
->u
->u_curblock
= block
;
570 compiler_next_block(struct compiler
*c
)
572 basicblock
*block
= compiler_new_block(c
);
575 c
->u
->u_curblock
->b_next
= block
;
576 c
->u
->u_curblock
= block
;
581 compiler_use_next_block(struct compiler
*c
, basicblock
*block
)
583 assert(block
!= NULL
);
584 c
->u
->u_curblock
->b_next
= block
;
585 c
->u
->u_curblock
= block
;
589 /* Returns the offset of the next instruction in the current block's
590 b_instr array. Resizes the b_instr as necessary.
591 Returns -1 on failure.
595 compiler_next_instr(struct compiler
*c
, basicblock
*b
)
598 if (b
->b_instr
== NULL
) {
599 b
->b_instr
= (struct instr
*)PyObject_Malloc(
600 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
601 if (b
->b_instr
== NULL
) {
605 b
->b_ialloc
= DEFAULT_BLOCK_SIZE
;
606 memset((char *)b
->b_instr
, 0,
607 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
609 else if (b
->b_iused
== b
->b_ialloc
) {
611 size_t oldsize
, newsize
;
612 oldsize
= b
->b_ialloc
* sizeof(struct instr
);
613 newsize
= oldsize
<< 1;
619 tmp
= (struct instr
*)PyObject_Realloc(
620 (void *)b
->b_instr
, newsize
);
626 memset((char *)b
->b_instr
+ oldsize
, 0, newsize
- oldsize
);
631 /* Set the i_lineno member of the instruction at offse off if the
632 line number for the current expression/statement (?) has not
633 already been set. If it has been set, the call has no effect.
635 Every time a new node is b
639 compiler_set_lineno(struct compiler
*c
, int off
)
642 if (c
->u
->u_lineno_set
)
644 c
->u
->u_lineno_set
= true;
645 b
= c
->u
->u_curblock
;
646 b
->b_instr
[off
].i_lineno
= c
->u
->u_lineno
;
650 opcode_stack_effect(int opcode
, int oparg
)
674 case BINARY_MULTIPLY
:
678 case BINARY_SUBTRACT
:
680 case BINARY_FLOOR_DIVIDE
:
681 case BINARY_TRUE_DIVIDE
:
683 case INPLACE_FLOOR_DIVIDE
:
684 case INPLACE_TRUE_DIVIDE
:
715 case INPLACE_SUBTRACT
:
716 case INPLACE_MULTIPLY
:
744 case PRINT_NEWLINE_TO
:
755 return -1; /* XXX Sometimes more */
770 return -1; /* or -2 or -3 if exception occurred */
778 case UNPACK_SEQUENCE
:
826 return 3; /* actually pushed by an exception */
837 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
839 return -NARGS(oparg
);
840 case CALL_FUNCTION_VAR
:
841 case CALL_FUNCTION_KW
:
842 return -NARGS(oparg
)-1;
843 case CALL_FUNCTION_VAR_KW
:
844 return -NARGS(oparg
)-2;
863 fprintf(stderr
, "opcode = %d\n", opcode
);
864 Py_FatalError("opcode_stack_effect()");
867 return 0; /* not reachable */
870 /* Add an opcode with no argument.
871 Returns 0 on failure, 1 on success.
875 compiler_addop(struct compiler
*c
, int opcode
)
880 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
883 b
= c
->u
->u_curblock
;
884 i
= &b
->b_instr
[off
];
885 i
->i_opcode
= opcode
;
887 if (opcode
== RETURN_VALUE
)
889 compiler_set_lineno(c
, off
);
894 compiler_add_o(struct compiler
*c
, PyObject
*dict
, PyObject
*o
)
899 /* necessary to make sure types aren't coerced (e.g., int and long) */
900 t
= PyTuple_Pack(2, o
, o
->ob_type
);
904 v
= PyDict_GetItem(dict
, t
);
906 arg
= PyDict_Size(dict
);
907 v
= PyInt_FromLong(arg
);
912 if (PyDict_SetItem(dict
, t
, v
) < 0) {
920 arg
= PyInt_AsLong(v
);
926 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
929 int arg
= compiler_add_o(c
, dict
, o
);
932 return compiler_addop_i(c
, opcode
, arg
);
936 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
940 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
943 arg
= compiler_add_o(c
, dict
, mangled
);
947 return compiler_addop_i(c
, opcode
, arg
);
950 /* Add an opcode with an integer argument.
951 Returns 0 on failure, 1 on success.
955 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
959 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
962 i
= &c
->u
->u_curblock
->b_instr
[off
];
963 i
->i_opcode
= opcode
;
966 compiler_set_lineno(c
, off
);
971 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
977 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
980 i
= &c
->u
->u_curblock
->b_instr
[off
];
981 i
->i_opcode
= opcode
;
988 compiler_set_lineno(c
, off
);
992 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
993 like to find better names.) NEW_BLOCK() creates a new block and sets
994 it as the current block. NEXT_BLOCK() also creates an implicit jump
995 from the current block to the new block.
998 /* The returns inside these macros make it impossible to decref objects
999 created in the local function. Local objects should use the arena.
1003 #define NEW_BLOCK(C) { \
1004 if (compiler_use_new_block((C)) == NULL) \
1008 #define NEXT_BLOCK(C) { \
1009 if (compiler_next_block((C)) == NULL) \
1013 #define ADDOP(C, OP) { \
1014 if (!compiler_addop((C), (OP))) \
1018 #define ADDOP_IN_SCOPE(C, OP) { \
1019 if (!compiler_addop((C), (OP))) { \
1020 compiler_exit_scope(c); \
1025 #define ADDOP_O(C, OP, O, TYPE) { \
1026 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1030 #define ADDOP_NAME(C, OP, O, TYPE) { \
1031 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1035 #define ADDOP_I(C, OP, O) { \
1036 if (!compiler_addop_i((C), (OP), (O))) \
1040 #define ADDOP_JABS(C, OP, O) { \
1041 if (!compiler_addop_j((C), (OP), (O), 1)) \
1045 #define ADDOP_JREL(C, OP, O) { \
1046 if (!compiler_addop_j((C), (OP), (O), 0)) \
1050 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1051 the ASDL name to synthesize the name of the C type and the visit function.
1054 #define VISIT(C, TYPE, V) {\
1055 if (!compiler_visit_ ## TYPE((C), (V))) \
1059 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1060 if (!compiler_visit_ ## TYPE((C), (V))) { \
1061 compiler_exit_scope(c); \
1066 #define VISIT_SLICE(C, V, CTX) {\
1067 if (!compiler_visit_slice((C), (V), (CTX))) \
1071 #define VISIT_SEQ(C, TYPE, SEQ) { \
1073 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1074 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1075 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1076 if (!compiler_visit_ ## TYPE((C), elt)) \
1081 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1083 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1084 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1085 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1086 if (!compiler_visit_ ## TYPE((C), elt)) { \
1087 compiler_exit_scope(c); \
1094 compiler_isdocstring(stmt_ty s
)
1096 if (s
->kind
!= Expr_kind
)
1098 return s
->v
.Expr
.value
->kind
== Str_kind
;
1101 /* Compile a sequence of statements, checking for a docstring. */
1104 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1109 if (!asdl_seq_LEN(stmts
))
1111 st
= (stmt_ty
)asdl_seq_GET(stmts
, 0);
1112 if (compiler_isdocstring(st
)) {
1114 VISIT(c
, expr
, st
->v
.Expr
.value
);
1115 if (!compiler_nameop(c
, __doc__
, Store
))
1118 for (; i
< asdl_seq_LEN(stmts
); i
++)
1119 VISIT(c
, stmt
, (stmt_ty
)asdl_seq_GET(stmts
, i
));
1123 static PyCodeObject
*
1124 compiler_mod(struct compiler
*c
, mod_ty mod
)
1128 static PyObject
*module
;
1130 module
= PyString_FromString("<module>");
1134 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1135 if (!compiler_enter_scope(c
, module
, mod
, 0))
1137 switch (mod
->kind
) {
1139 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1140 compiler_exit_scope(c
);
1144 case Interactive_kind
:
1145 c
->c_interactive
= 1;
1146 VISIT_SEQ_IN_SCOPE(c
, stmt
,
1147 mod
->v
.Interactive
.body
);
1149 case Expression_kind
:
1150 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1154 PyErr_SetString(PyExc_SystemError
,
1155 "suite should not be possible");
1158 PyErr_Format(PyExc_SystemError
,
1159 "module kind %d should not be possible",
1163 co
= assemble(c
, addNone
);
1164 compiler_exit_scope(c
);
1168 /* The test for LOCAL must come before the test for FREE in order to
1169 handle classes where name is both local and free. The local var is
1170 a method and the free var is a free var referenced within a method.
1174 get_ref_type(struct compiler
*c
, PyObject
*name
)
1176 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1179 PyOS_snprintf(buf
, sizeof(buf
),
1180 "unknown scope for %.100s in %.100s(%s) in %s\n"
1181 "symbols: %s\nlocals: %s\nglobals: %s\n",
1182 PyString_AS_STRING(name
),
1183 PyString_AS_STRING(c
->u
->u_name
),
1184 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1186 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1187 PyObject_REPR(c
->u
->u_varnames
),
1188 PyObject_REPR(c
->u
->u_names
)
1197 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1200 k
= PyTuple_Pack(2, name
, name
->ob_type
);
1203 v
= PyDict_GetItem(dict
, k
);
1207 return PyInt_AS_LONG(v
);
1211 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1213 int i
, free
= PyCode_GetNumFree(co
);
1215 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1216 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1219 for (i
= 0; i
< free
; ++i
) {
1220 /* Bypass com_addop_varname because it will generate
1221 LOAD_DEREF but LOAD_CLOSURE is needed.
1223 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1226 /* Special case: If a class contains a method with a
1227 free variable that has the same name as a method,
1228 the name will be considered free *and* local in the
1229 class. It should be handled by the closure, as
1230 well as by the normal name loookup logic.
1232 reftype
= get_ref_type(c
, name
);
1233 if (reftype
== CELL
)
1234 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1235 else /* (reftype == FREE) */
1236 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1238 printf("lookup %s in %s %d %d\n"
1239 "freevars of %s: %s\n",
1240 PyObject_REPR(name
),
1241 PyString_AS_STRING(c
->u
->u_name
),
1243 PyString_AS_STRING(co
->co_name
),
1244 PyObject_REPR(co
->co_freevars
));
1245 Py_FatalError("compiler_make_closure()");
1247 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1249 ADDOP_I(c
, BUILD_TUPLE
, free
);
1250 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1251 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1256 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1263 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1264 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(decos
, i
));
1270 compiler_arguments(struct compiler
*c
, arguments_ty args
)
1273 int n
= asdl_seq_LEN(args
->args
);
1274 /* Correctly handle nested argument lists */
1275 for (i
= 0; i
< n
; i
++) {
1276 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
->args
, i
);
1277 if (arg
->kind
== Tuple_kind
) {
1278 PyObject
*id
= PyString_FromFormat(".%d", i
);
1282 if (!compiler_nameop(c
, id
, Load
)) {
1287 VISIT(c
, expr
, arg
);
1294 compiler_function(struct compiler
*c
, stmt_ty s
)
1297 PyObject
*first_const
= Py_None
;
1298 arguments_ty args
= s
->v
.FunctionDef
.args
;
1299 asdl_seq
* decos
= s
->v
.FunctionDef
.decorators
;
1301 int i
, n
, docstring
;
1303 assert(s
->kind
== FunctionDef_kind
);
1305 if (!compiler_decorators(c
, decos
))
1308 VISIT_SEQ(c
, expr
, args
->defaults
);
1309 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1313 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1314 docstring
= compiler_isdocstring(st
);
1316 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1317 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1318 compiler_exit_scope(c
);
1322 /* unpack nested arguments */
1323 compiler_arguments(c
, args
);
1325 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1326 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1327 /* if there was a docstring, we need to skip the first statement */
1328 for (i
= docstring
; i
< n
; i
++) {
1329 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1330 VISIT_IN_SCOPE(c
, stmt
, st
);
1332 co
= assemble(c
, 1);
1333 compiler_exit_scope(c
);
1337 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1340 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1341 ADDOP_I(c
, CALL_FUNCTION
, 1);
1344 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1348 compiler_class(struct compiler
*c
, stmt_ty s
)
1353 /* push class name on stack, needed by BUILD_CLASS */
1354 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1355 /* push the tuple of base classes on the stack */
1356 n
= asdl_seq_LEN(s
->v
.ClassDef
.bases
);
1358 VISIT_SEQ(c
, expr
, s
->v
.ClassDef
.bases
);
1359 ADDOP_I(c
, BUILD_TUPLE
, n
);
1360 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
,
1363 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1364 Py_INCREF(c
->u
->u_private
);
1365 str
= PyString_InternFromString("__name__");
1366 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1368 compiler_exit_scope(c
);
1373 str
= PyString_InternFromString("__module__");
1374 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
1376 compiler_exit_scope(c
);
1381 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
1382 compiler_exit_scope(c
);
1386 ADDOP_IN_SCOPE(c
, LOAD_LOCALS
);
1387 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1388 co
= assemble(c
, 1);
1389 compiler_exit_scope(c
);
1393 compiler_make_closure(c
, co
, 0);
1396 ADDOP_I(c
, CALL_FUNCTION
, 0);
1397 ADDOP(c
, BUILD_CLASS
);
1398 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
1404 compiler_ifexp(struct compiler
*c
, expr_ty e
)
1406 basicblock
*end
, *next
;
1408 assert(e
->kind
== IfExp_kind
);
1409 end
= compiler_new_block(c
);
1412 next
= compiler_new_block(c
);
1415 VISIT(c
, expr
, e
->v
.IfExp
.test
);
1416 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
1418 VISIT(c
, expr
, e
->v
.IfExp
.body
);
1419 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1420 compiler_use_next_block(c
, next
);
1422 VISIT(c
, expr
, e
->v
.IfExp
.orelse
);
1423 compiler_use_next_block(c
, end
);
1428 compiler_lambda(struct compiler
*c
, expr_ty e
)
1431 static identifier name
;
1432 arguments_ty args
= e
->v
.Lambda
.args
;
1433 assert(e
->kind
== Lambda_kind
);
1436 name
= PyString_InternFromString("<lambda>");
1442 VISIT_SEQ(c
, expr
, args
->defaults
);
1443 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
1446 /* unpack nested arguments */
1447 compiler_arguments(c
, args
);
1449 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1450 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
1451 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1452 co
= assemble(c
, 1);
1453 compiler_exit_scope(c
);
1457 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1464 compiler_print(struct compiler
*c
, stmt_ty s
)
1469 assert(s
->kind
== Print_kind
);
1470 n
= asdl_seq_LEN(s
->v
.Print
.values
);
1472 if (s
->v
.Print
.dest
) {
1473 VISIT(c
, expr
, s
->v
.Print
.dest
);
1476 for (i
= 0; i
< n
; i
++) {
1477 expr_ty e
= (expr_ty
)asdl_seq_GET(s
->v
.Print
.values
, i
);
1482 ADDOP(c
, PRINT_ITEM_TO
);
1486 ADDOP(c
, PRINT_ITEM
);
1489 if (s
->v
.Print
.nl
) {
1491 ADDOP(c
, PRINT_NEWLINE_TO
)
1493 ADDOP(c
, PRINT_NEWLINE
)
1501 compiler_if(struct compiler
*c
, stmt_ty s
)
1503 basicblock
*end
, *next
;
1505 assert(s
->kind
== If_kind
);
1506 end
= compiler_new_block(c
);
1509 next
= compiler_new_block(c
);
1513 constant
= expr_constant(s
->v
.If
.test
);
1514 /* constant = 0: "if 0"
1515 * constant = 1: "if 1", "if 2", ...
1516 * constant = -1: rest */
1517 if (constant
== 0) {
1519 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1520 } else if (constant
== 1) {
1521 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1523 VISIT(c
, expr
, s
->v
.If
.test
);
1524 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
1526 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1527 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1528 compiler_use_next_block(c
, next
);
1531 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1533 compiler_use_next_block(c
, end
);
1538 compiler_for(struct compiler
*c
, stmt_ty s
)
1540 basicblock
*start
, *cleanup
, *end
;
1542 start
= compiler_new_block(c
);
1543 cleanup
= compiler_new_block(c
);
1544 end
= compiler_new_block(c
);
1545 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
1547 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1548 if (!compiler_push_fblock(c
, LOOP
, start
))
1550 VISIT(c
, expr
, s
->v
.For
.iter
);
1552 compiler_use_next_block(c
, start
);
1553 /* XXX(nnorwitz): is there a better way to handle this?
1554 for loops are special, we want to be able to trace them
1555 each time around, so we need to set an extra line number. */
1556 c
->u
->u_lineno_set
= false;
1557 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
1558 VISIT(c
, expr
, s
->v
.For
.target
);
1559 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
1560 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
1561 compiler_use_next_block(c
, cleanup
);
1562 ADDOP(c
, POP_BLOCK
);
1563 compiler_pop_fblock(c
, LOOP
, start
);
1564 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
1565 compiler_use_next_block(c
, end
);
1570 compiler_while(struct compiler
*c
, stmt_ty s
)
1572 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
1573 int constant
= expr_constant(s
->v
.While
.test
);
1577 loop
= compiler_new_block(c
);
1578 end
= compiler_new_block(c
);
1579 if (constant
== -1) {
1580 anchor
= compiler_new_block(c
);
1584 if (loop
== NULL
|| end
== NULL
)
1586 if (s
->v
.While
.orelse
) {
1587 orelse
= compiler_new_block(c
);
1594 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1595 compiler_use_next_block(c
, loop
);
1596 if (!compiler_push_fblock(c
, LOOP
, loop
))
1598 if (constant
== -1) {
1599 VISIT(c
, expr
, s
->v
.While
.test
);
1600 ADDOP_JREL(c
, JUMP_IF_FALSE
, anchor
);
1603 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
1604 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
1606 /* XXX should the two POP instructions be in a separate block
1607 if there is no else clause ?
1610 if (constant
== -1) {
1611 compiler_use_next_block(c
, anchor
);
1613 ADDOP(c
, POP_BLOCK
);
1615 compiler_pop_fblock(c
, LOOP
, loop
);
1616 if (orelse
!= NULL
) /* what if orelse is just pass? */
1617 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1618 compiler_use_next_block(c
, end
);
1624 compiler_continue(struct compiler
*c
)
1626 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
1627 static const char IN_FINALLY_ERROR_MSG
[] =
1628 "'continue' not supported inside 'finally' clause";
1631 if (!c
->u
->u_nfblocks
)
1632 return compiler_error(c
, LOOP_ERROR_MSG
);
1633 i
= c
->u
->u_nfblocks
- 1;
1634 switch (c
->u
->u_fblock
[i
].fb_type
) {
1636 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
1640 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
) {
1641 /* Prevent continue anywhere under a finally
1642 even if hidden in a sub-try or except. */
1643 if (c
->u
->u_fblock
[i
].fb_type
== FINALLY_END
)
1644 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1647 return compiler_error(c
, LOOP_ERROR_MSG
);
1648 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
1651 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1657 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1663 L: <code for finalbody>
1666 The special instructions use the block stack. Each block
1667 stack entry contains the instruction that created it (here
1668 SETUP_FINALLY), the level of the value stack at the time the
1669 block stack entry was created, and a label (here L).
1672 Pushes the current value stack level and the label
1673 onto the block stack.
1675 Pops en entry from the block stack, and pops the value
1676 stack until its level is the same as indicated on the
1677 block stack. (The label is ignored.)
1679 Pops a variable number of entries from the *value* stack
1680 and re-raises the exception they specify. The number of
1681 entries popped depends on the (pseudo) exception type.
1683 The block stack is unwound when an exception is raised:
1684 when a SETUP_FINALLY entry is found, the exception is pushed
1685 onto the value stack (and the exception condition is cleared),
1686 and the interpreter jumps to the label gotten from the block
1691 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
1693 basicblock
*body
, *end
;
1694 body
= compiler_new_block(c
);
1695 end
= compiler_new_block(c
);
1696 if (body
== NULL
|| end
== NULL
)
1699 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
1700 compiler_use_next_block(c
, body
);
1701 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
1703 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
1704 ADDOP(c
, POP_BLOCK
);
1705 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
1707 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1708 compiler_use_next_block(c
, end
);
1709 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
1711 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
1712 ADDOP(c
, END_FINALLY
);
1713 compiler_pop_fblock(c
, FINALLY_END
, end
);
1719 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1720 (The contents of the value stack is shown in [], with the top
1721 at the right; 'tb' is trace-back info, 'val' the exception's
1722 associated value, and 'exc' the exception.)
1724 Value stack Label Instruction Argument
1730 [tb, val, exc] L1: DUP )
1731 [tb, val, exc, exc] <evaluate E1> )
1732 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1733 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1734 [tb, val, exc, 1] POP )
1736 [tb, val] <assign to V1> (or POP if no V1)
1741 [tb, val, exc, 0] L2: POP
1743 .............................etc.......................
1745 [tb, val, exc, 0] Ln+1: POP
1746 [tb, val, exc] END_FINALLY # re-raise exception
1748 [] L0: <next statement>
1750 Of course, parts are not generated if Vi or Ei is not present.
1753 compiler_try_except(struct compiler
*c
, stmt_ty s
)
1755 basicblock
*body
, *orelse
, *except
, *end
;
1758 body
= compiler_new_block(c
);
1759 except
= compiler_new_block(c
);
1760 orelse
= compiler_new_block(c
);
1761 end
= compiler_new_block(c
);
1762 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
1764 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
1765 compiler_use_next_block(c
, body
);
1766 if (!compiler_push_fblock(c
, EXCEPT
, body
))
1768 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
1769 ADDOP(c
, POP_BLOCK
);
1770 compiler_pop_fblock(c
, EXCEPT
, body
);
1771 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
1772 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
1773 compiler_use_next_block(c
, except
);
1774 for (i
= 0; i
< n
; i
++) {
1775 excepthandler_ty handler
= (excepthandler_ty
)asdl_seq_GET(
1776 s
->v
.TryExcept
.handlers
, i
);
1777 if (!handler
->type
&& i
< n
-1)
1778 return compiler_error(c
, "default 'except:' must be last");
1779 c
->u
->u_lineno_set
= false;
1780 c
->u
->u_lineno
= handler
->lineno
;
1781 except
= compiler_new_block(c
);
1784 if (handler
->type
) {
1786 VISIT(c
, expr
, handler
->type
);
1787 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
1788 ADDOP_JREL(c
, JUMP_IF_FALSE
, except
);
1792 if (handler
->name
) {
1793 VISIT(c
, expr
, handler
->name
);
1799 VISIT_SEQ(c
, stmt
, handler
->body
);
1800 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1801 compiler_use_next_block(c
, except
);
1805 ADDOP(c
, END_FINALLY
);
1806 compiler_use_next_block(c
, orelse
);
1807 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
1808 compiler_use_next_block(c
, end
);
1813 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
1815 /* The IMPORT_NAME opcode was already generated. This function
1816 merely needs to bind the result to a name.
1818 If there is a dot in name, we need to split it and emit a
1819 LOAD_ATTR for each name.
1821 const char *src
= PyString_AS_STRING(name
);
1822 const char *dot
= strchr(src
, '.');
1824 /* Consume the base module name to get the first attribute */
1827 /* NB src is only defined when dot != NULL */
1829 dot
= strchr(src
, '.');
1830 attr
= PyString_FromStringAndSize(src
,
1831 dot
? dot
- src
: strlen(src
));
1834 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
1839 return compiler_nameop(c
, asname
, Store
);
1843 compiler_import(struct compiler
*c
, stmt_ty s
)
1845 /* The Import node stores a module name like a.b.c as a single
1846 string. This is convenient for all cases except
1848 where we need to parse that string to extract the individual
1850 XXX Perhaps change the representation to make this case simpler?
1852 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
1854 for (i
= 0; i
< n
; i
++) {
1855 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.Import
.names
, i
);
1859 if (c
->c_flags
&& (c
->c_flags
->cf_flags
& CO_FUTURE_ABSOLUTE_IMPORT
))
1860 level
= PyInt_FromLong(0);
1862 level
= PyInt_FromLong(-1);
1867 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
1869 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1870 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
1872 if (alias
->asname
) {
1873 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
1878 identifier tmp
= alias
->name
;
1879 const char *base
= PyString_AS_STRING(alias
->name
);
1880 char *dot
= strchr(base
, '.');
1882 tmp
= PyString_FromStringAndSize(base
,
1884 r
= compiler_nameop(c
, tmp
, Store
);
1896 compiler_from_import(struct compiler
*c
, stmt_ty s
)
1898 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
1900 PyObject
*names
= PyTuple_New(n
);
1906 if (s
->v
.ImportFrom
.level
== 0 && c
->c_flags
&&
1907 !(c
->c_flags
->cf_flags
& CO_FUTURE_ABSOLUTE_IMPORT
))
1908 level
= PyInt_FromLong(-1);
1910 level
= PyInt_FromLong(s
->v
.ImportFrom
.level
);
1917 /* build up the names */
1918 for (i
= 0; i
< n
; i
++) {
1919 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
1920 Py_INCREF(alias
->name
);
1921 PyTuple_SET_ITEM(names
, i
, alias
->name
);
1924 if (s
->lineno
> c
->c_future
->ff_lineno
) {
1925 if (!strcmp(PyString_AS_STRING(s
->v
.ImportFrom
.module
),
1929 return compiler_error(c
,
1930 "from __future__ imports must occur "
1931 "at the beginning of the file");
1936 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
1938 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
1940 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
1941 for (i
= 0; i
< n
; i
++) {
1942 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
1943 identifier store_name
;
1945 if (i
== 0 && *PyString_AS_STRING(alias
->name
) == '*') {
1947 ADDOP(c
, IMPORT_STAR
);
1951 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
1952 store_name
= alias
->name
;
1954 store_name
= alias
->asname
;
1956 if (!compiler_nameop(c
, store_name
, Store
)) {
1961 /* remove imported module */
1967 compiler_assert(struct compiler
*c
, stmt_ty s
)
1969 static PyObject
*assertion_error
= NULL
;
1972 if (Py_OptimizeFlag
)
1974 if (assertion_error
== NULL
) {
1975 assertion_error
= PyString_FromString("AssertionError");
1976 if (assertion_error
== NULL
)
1979 VISIT(c
, expr
, s
->v
.Assert
.test
);
1980 end
= compiler_new_block(c
);
1983 ADDOP_JREL(c
, JUMP_IF_TRUE
, end
);
1985 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
1986 if (s
->v
.Assert
.msg
) {
1987 VISIT(c
, expr
, s
->v
.Assert
.msg
);
1988 ADDOP_I(c
, RAISE_VARARGS
, 2);
1991 ADDOP_I(c
, RAISE_VARARGS
, 1);
1993 compiler_use_next_block(c
, end
);
1999 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2003 /* Always assign a lineno to the next instruction for a stmt. */
2004 c
->u
->u_lineno
= s
->lineno
;
2005 c
->u
->u_lineno_set
= false;
2008 case FunctionDef_kind
:
2009 return compiler_function(c
, s
);
2011 return compiler_class(c
, s
);
2013 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2014 return compiler_error(c
, "'return' outside function");
2015 if (s
->v
.Return
.value
) {
2016 VISIT(c
, expr
, s
->v
.Return
.value
);
2019 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2020 ADDOP(c
, RETURN_VALUE
);
2023 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2026 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2027 VISIT(c
, expr
, s
->v
.Assign
.value
);
2028 for (i
= 0; i
< n
; i
++) {
2032 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2035 case AugAssign_kind
:
2036 return compiler_augassign(c
, s
);
2038 return compiler_print(c
, s
);
2040 return compiler_for(c
, s
);
2042 return compiler_while(c
, s
);
2044 return compiler_if(c
, s
);
2047 if (s
->v
.Raise
.type
) {
2048 VISIT(c
, expr
, s
->v
.Raise
.type
);
2050 if (s
->v
.Raise
.inst
) {
2051 VISIT(c
, expr
, s
->v
.Raise
.inst
);
2053 if (s
->v
.Raise
.tback
) {
2054 VISIT(c
, expr
, s
->v
.Raise
.tback
);
2059 ADDOP_I(c
, RAISE_VARARGS
, n
);
2061 case TryExcept_kind
:
2062 return compiler_try_except(c
, s
);
2063 case TryFinally_kind
:
2064 return compiler_try_finally(c
, s
);
2066 return compiler_assert(c
, s
);
2068 return compiler_import(c
, s
);
2069 case ImportFrom_kind
:
2070 return compiler_from_import(c
, s
);
2072 VISIT(c
, expr
, s
->v
.Exec
.body
);
2073 if (s
->v
.Exec
.globals
) {
2074 VISIT(c
, expr
, s
->v
.Exec
.globals
);
2075 if (s
->v
.Exec
.locals
) {
2076 VISIT(c
, expr
, s
->v
.Exec
.locals
);
2081 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2084 ADDOP(c
, EXEC_STMT
);
2089 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2090 VISIT(c
, expr
, s
->v
.Expr
.value
);
2091 ADDOP(c
, PRINT_EXPR
);
2093 else if (s
->v
.Expr
.value
->kind
!= Str_kind
&&
2094 s
->v
.Expr
.value
->kind
!= Num_kind
) {
2095 VISIT(c
, expr
, s
->v
.Expr
.value
);
2102 if (!compiler_in_loop(c
))
2103 return compiler_error(c
, "'break' outside loop");
2104 ADDOP(c
, BREAK_LOOP
);
2107 return compiler_continue(c
);
2109 return compiler_with(c
, s
);
2115 unaryop(unaryop_ty op
)
2119 return UNARY_INVERT
;
2123 return UNARY_POSITIVE
;
2125 return UNARY_NEGATIVE
;
2131 binop(struct compiler
*c
, operator_ty op
)
2137 return BINARY_SUBTRACT
;
2139 return BINARY_MULTIPLY
;
2141 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2142 return BINARY_TRUE_DIVIDE
;
2144 return BINARY_DIVIDE
;
2146 return BINARY_MODULO
;
2148 return BINARY_POWER
;
2150 return BINARY_LSHIFT
;
2152 return BINARY_RSHIFT
;
2160 return BINARY_FLOOR_DIVIDE
;
2184 return PyCmp_IS_NOT
;
2188 return PyCmp_NOT_IN
;
2194 inplace_binop(struct compiler
*c
, operator_ty op
)
2200 return INPLACE_SUBTRACT
;
2202 return INPLACE_MULTIPLY
;
2204 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2205 return INPLACE_TRUE_DIVIDE
;
2207 return INPLACE_DIVIDE
;
2209 return INPLACE_MODULO
;
2211 return INPLACE_POWER
;
2213 return INPLACE_LSHIFT
;
2215 return INPLACE_RSHIFT
;
2223 return INPLACE_FLOOR_DIVIDE
;
2225 PyErr_Format(PyExc_SystemError
,
2226 "inplace binary op %d should not be possible", op
);
2231 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2234 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2236 PyObject
*dict
= c
->u
->u_names
;
2238 /* XXX AugStore isn't used anywhere! */
2240 /* First check for assignment to __debug__. Param? */
2241 if ((ctx
== Store
|| ctx
== AugStore
|| ctx
== Del
)
2242 && !strcmp(PyString_AS_STRING(name
), "__debug__")) {
2243 return compiler_error(c
, "can not assign to __debug__");
2246 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2252 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2255 dict
= c
->u
->u_freevars
;
2259 dict
= c
->u
->u_cellvars
;
2263 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2266 case GLOBAL_IMPLICIT
:
2267 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2268 !c
->u
->u_ste
->ste_unoptimized
)
2271 case GLOBAL_EXPLICIT
:
2275 /* scope can be 0 */
2279 /* XXX Leave assert here, but handle __doc__ and the like better */
2280 assert(scope
|| PyString_AS_STRING(name
)[0] == '_');
2285 case Load
: op
= LOAD_DEREF
; break;
2286 case Store
: op
= STORE_DEREF
; break;
2291 PyErr_Format(PyExc_SyntaxError
,
2292 "can not delete variable '%s' referenced "
2294 PyString_AS_STRING(name
));
2299 PyErr_SetString(PyExc_SystemError
,
2300 "param invalid for deref variable");
2306 case Load
: op
= LOAD_FAST
; break;
2307 case Store
: op
= STORE_FAST
; break;
2308 case Del
: op
= DELETE_FAST
; break;
2314 PyErr_SetString(PyExc_SystemError
,
2315 "param invalid for local variable");
2318 ADDOP_O(c
, op
, mangled
, varnames
);
2323 case Load
: op
= LOAD_GLOBAL
; break;
2324 case Store
: op
= STORE_GLOBAL
; break;
2325 case Del
: op
= DELETE_GLOBAL
; break;
2331 PyErr_SetString(PyExc_SystemError
,
2332 "param invalid for global variable");
2338 case Load
: op
= LOAD_NAME
; break;
2339 case Store
: op
= STORE_NAME
; break;
2340 case Del
: op
= DELETE_NAME
; break;
2346 PyErr_SetString(PyExc_SystemError
,
2347 "param invalid for name variable");
2354 arg
= compiler_add_o(c
, dict
, mangled
);
2358 return compiler_addop_i(c
, op
, arg
);
2362 compiler_boolop(struct compiler
*c
, expr_ty e
)
2368 assert(e
->kind
== BoolOp_kind
);
2369 if (e
->v
.BoolOp
.op
== And
)
2370 jumpi
= JUMP_IF_FALSE
;
2372 jumpi
= JUMP_IF_TRUE
;
2373 end
= compiler_new_block(c
);
2376 s
= e
->v
.BoolOp
.values
;
2377 n
= asdl_seq_LEN(s
) - 1;
2379 for (i
= 0; i
< n
; ++i
) {
2380 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, i
));
2381 ADDOP_JREL(c
, jumpi
, end
);
2384 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, n
));
2385 compiler_use_next_block(c
, end
);
2390 compiler_list(struct compiler
*c
, expr_ty e
)
2392 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
2393 if (e
->v
.List
.ctx
== Store
) {
2394 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2396 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
2397 if (e
->v
.List
.ctx
== Load
) {
2398 ADDOP_I(c
, BUILD_LIST
, n
);
2404 compiler_tuple(struct compiler
*c
, expr_ty e
)
2406 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
2407 if (e
->v
.Tuple
.ctx
== Store
) {
2408 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2410 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
2411 if (e
->v
.Tuple
.ctx
== Load
) {
2412 ADDOP_I(c
, BUILD_TUPLE
, n
);
2418 compiler_compare(struct compiler
*c
, expr_ty e
)
2421 basicblock
*cleanup
= NULL
;
2423 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2424 VISIT(c
, expr
, e
->v
.Compare
.left
);
2425 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
2428 cleanup
= compiler_new_block(c
);
2429 if (cleanup
== NULL
)
2432 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
2434 for (i
= 1; i
< n
; i
++) {
2436 ADDOP(c
, ROT_THREE
);
2437 ADDOP_I(c
, COMPARE_OP
,
2438 cmpop((cmpop_ty
)(asdl_seq_GET(
2439 e
->v
.Compare
.ops
, i
- 1))));
2440 ADDOP_JREL(c
, JUMP_IF_FALSE
, cleanup
);
2445 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
2447 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
2448 ADDOP_I(c
, COMPARE_OP
,
2449 cmpop((cmpop_ty
)(asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1))));
2451 basicblock
*end
= compiler_new_block(c
);
2454 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2455 compiler_use_next_block(c
, cleanup
);
2458 compiler_use_next_block(c
, end
);
2464 compiler_call(struct compiler
*c
, expr_ty e
)
2468 VISIT(c
, expr
, e
->v
.Call
.func
);
2469 n
= asdl_seq_LEN(e
->v
.Call
.args
);
2470 VISIT_SEQ(c
, expr
, e
->v
.Call
.args
);
2471 if (e
->v
.Call
.keywords
) {
2472 VISIT_SEQ(c
, keyword
, e
->v
.Call
.keywords
);
2473 n
|= asdl_seq_LEN(e
->v
.Call
.keywords
) << 8;
2475 if (e
->v
.Call
.starargs
) {
2476 VISIT(c
, expr
, e
->v
.Call
.starargs
);
2479 if (e
->v
.Call
.kwargs
) {
2480 VISIT(c
, expr
, e
->v
.Call
.kwargs
);
2485 ADDOP_I(c
, CALL_FUNCTION
, n
);
2488 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
2491 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
2494 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
2501 compiler_listcomp_generator(struct compiler
*c
, PyObject
*tmpname
,
2502 asdl_seq
*generators
, int gen_index
,
2505 /* generate code for the iterator, then each of the ifs,
2506 and then write to the element */
2509 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
2512 start
= compiler_new_block(c
);
2513 skip
= compiler_new_block(c
);
2514 if_cleanup
= compiler_new_block(c
);
2515 anchor
= compiler_new_block(c
);
2517 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2521 l
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2522 VISIT(c
, expr
, l
->iter
);
2524 compiler_use_next_block(c
, start
);
2525 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2527 VISIT(c
, expr
, l
->target
);
2529 /* XXX this needs to be cleaned up...a lot! */
2530 n
= asdl_seq_LEN(l
->ifs
);
2531 for (i
= 0; i
< n
; i
++) {
2532 expr_ty e
= (expr_ty
)asdl_seq_GET(l
->ifs
, i
);
2534 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
2539 if (++gen_index
< asdl_seq_LEN(generators
))
2540 if (!compiler_listcomp_generator(c
, tmpname
,
2541 generators
, gen_index
, elt
))
2544 /* only append after the last for generator */
2545 if (gen_index
>= asdl_seq_LEN(generators
)) {
2546 if (!compiler_nameop(c
, tmpname
, Load
))
2548 VISIT(c
, expr
, elt
);
2549 ADDOP(c
, LIST_APPEND
);
2551 compiler_use_next_block(c
, skip
);
2553 for (i
= 0; i
< n
; i
++) {
2554 ADDOP_I(c
, JUMP_FORWARD
, 1);
2556 compiler_use_next_block(c
, if_cleanup
);
2559 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2560 compiler_use_next_block(c
, anchor
);
2561 /* delete the temporary list name added to locals */
2563 if (!compiler_nameop(c
, tmpname
, Del
))
2570 compiler_listcomp(struct compiler
*c
, expr_ty e
)
2574 asdl_seq
*generators
= e
->v
.ListComp
.generators
;
2576 assert(e
->kind
== ListComp_kind
);
2577 tmp
= compiler_new_tmpname(c
);
2580 ADDOP_I(c
, BUILD_LIST
, 0);
2582 if (compiler_nameop(c
, tmp
, Store
))
2583 rc
= compiler_listcomp_generator(c
, tmp
, generators
, 0,
2590 compiler_genexp_generator(struct compiler
*c
,
2591 asdl_seq
*generators
, int gen_index
,
2594 /* generate code for the iterator, then each of the ifs,
2595 and then write to the element */
2597 comprehension_ty ge
;
2598 basicblock
*start
, *anchor
, *skip
, *if_cleanup
, *end
;
2601 start
= compiler_new_block(c
);
2602 skip
= compiler_new_block(c
);
2603 if_cleanup
= compiler_new_block(c
);
2604 anchor
= compiler_new_block(c
);
2605 end
= compiler_new_block(c
);
2607 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2608 anchor
== NULL
|| end
== NULL
)
2611 ge
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2612 ADDOP_JREL(c
, SETUP_LOOP
, end
);
2613 if (!compiler_push_fblock(c
, LOOP
, start
))
2616 if (gen_index
== 0) {
2617 /* Receive outermost iter as an implicit argument */
2618 c
->u
->u_argcount
= 1;
2619 ADDOP_I(c
, LOAD_FAST
, 0);
2622 /* Sub-iter - calculate on the fly */
2623 VISIT(c
, expr
, ge
->iter
);
2626 compiler_use_next_block(c
, start
);
2627 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2629 VISIT(c
, expr
, ge
->target
);
2631 /* XXX this needs to be cleaned up...a lot! */
2632 n
= asdl_seq_LEN(ge
->ifs
);
2633 for (i
= 0; i
< n
; i
++) {
2634 expr_ty e
= (expr_ty
)asdl_seq_GET(ge
->ifs
, i
);
2636 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
2641 if (++gen_index
< asdl_seq_LEN(generators
))
2642 if (!compiler_genexp_generator(c
, generators
, gen_index
, elt
))
2645 /* only append after the last 'for' generator */
2646 if (gen_index
>= asdl_seq_LEN(generators
)) {
2647 VISIT(c
, expr
, elt
);
2648 ADDOP(c
, YIELD_VALUE
);
2651 compiler_use_next_block(c
, skip
);
2653 for (i
= 0; i
< n
; i
++) {
2654 ADDOP_I(c
, JUMP_FORWARD
, 1);
2656 compiler_use_next_block(c
, if_cleanup
);
2660 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2661 compiler_use_next_block(c
, anchor
);
2662 ADDOP(c
, POP_BLOCK
);
2663 compiler_pop_fblock(c
, LOOP
, start
);
2664 compiler_use_next_block(c
, end
);
2670 compiler_genexp(struct compiler
*c
, expr_ty e
)
2672 static identifier name
;
2674 expr_ty outermost_iter
= ((comprehension_ty
)
2675 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
,
2679 name
= PyString_FromString("<genexpr>");
2684 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2686 compiler_genexp_generator(c
, e
->v
.GeneratorExp
.generators
, 0,
2687 e
->v
.GeneratorExp
.elt
);
2688 co
= assemble(c
, 1);
2689 compiler_exit_scope(c
);
2693 compiler_make_closure(c
, co
, 0);
2696 VISIT(c
, expr
, outermost_iter
);
2698 ADDOP_I(c
, CALL_FUNCTION
, 1);
2704 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
2706 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
2707 VISIT(c
, expr
, k
->value
);
2711 /* Test whether expression is constant. For constants, report
2712 whether they are true or false.
2714 Return values: 1 for true, 0 for false, -1 for non-constant.
2718 expr_constant(expr_ty e
)
2722 return PyObject_IsTrue(e
->v
.Num
.n
);
2724 return PyObject_IsTrue(e
->v
.Str
.s
);
2726 /* __debug__ is not assignable, so we can optimize
2727 * it away in if and while statements */
2728 if (strcmp(PyString_AS_STRING(e
->v
.Name
.id
),
2730 return ! Py_OptimizeFlag
;
2738 Implements the with statement from PEP 343.
2740 The semantics outlined in that PEP are as follows:
2745 It is implemented roughly as:
2748 exit = context.__exit__ # not calling it
2749 value = context.__enter__()
2751 VAR = value # if VAR present in the syntax
2754 if an exception was raised:
2755 exc = copy of (exception, instance, traceback)
2757 exc = (None, None, None)
2761 compiler_with(struct compiler
*c
, stmt_ty s
)
2763 static identifier enter_attr
, exit_attr
;
2764 basicblock
*block
, *finally
;
2765 identifier tmpexit
, tmpvalue
= NULL
;
2767 assert(s
->kind
== With_kind
);
2770 enter_attr
= PyString_InternFromString("__enter__");
2775 exit_attr
= PyString_InternFromString("__exit__");
2780 block
= compiler_new_block(c
);
2781 finally
= compiler_new_block(c
);
2782 if (!block
|| !finally
)
2785 /* Create a temporary variable to hold context.__exit__ */
2786 tmpexit
= compiler_new_tmpname(c
);
2787 if (tmpexit
== NULL
)
2789 PyArena_AddPyObject(c
->c_arena
, tmpexit
);
2791 if (s
->v
.With
.optional_vars
) {
2792 /* Create a temporary variable to hold context.__enter__().
2793 We need to do this rather than preserving it on the stack
2794 because SETUP_FINALLY remembers the stack level.
2795 We need to do the assignment *inside* the try/finally
2796 so that context.__exit__() is called when the assignment
2797 fails. But we need to call context.__enter__() *before*
2798 the try/finally so that if it fails we won't call
2801 tmpvalue
= compiler_new_tmpname(c
);
2802 if (tmpvalue
== NULL
)
2804 PyArena_AddPyObject(c
->c_arena
, tmpvalue
);
2808 VISIT(c
, expr
, s
->v
.With
.context_expr
);
2810 /* Squirrel away context.__exit__ */
2812 ADDOP_O(c
, LOAD_ATTR
, exit_attr
, names
);
2813 if (!compiler_nameop(c
, tmpexit
, Store
))
2816 /* Call context.__enter__() */
2817 ADDOP_O(c
, LOAD_ATTR
, enter_attr
, names
);
2818 ADDOP_I(c
, CALL_FUNCTION
, 0);
2820 if (s
->v
.With
.optional_vars
) {
2821 /* Store it in tmpvalue */
2822 if (!compiler_nameop(c
, tmpvalue
, Store
))
2826 /* Discard result from context.__enter__() */
2830 /* Start the try block */
2831 ADDOP_JREL(c
, SETUP_FINALLY
, finally
);
2833 compiler_use_next_block(c
, block
);
2834 if (!compiler_push_fblock(c
, FINALLY_TRY
, block
)) {
2838 if (s
->v
.With
.optional_vars
) {
2839 /* Bind saved result of context.__enter__() to VAR */
2840 if (!compiler_nameop(c
, tmpvalue
, Load
) ||
2841 !compiler_nameop(c
, tmpvalue
, Del
))
2843 VISIT(c
, expr
, s
->v
.With
.optional_vars
);
2847 VISIT_SEQ(c
, stmt
, s
->v
.With
.body
);
2849 /* End of try block; start the finally block */
2850 ADDOP(c
, POP_BLOCK
);
2851 compiler_pop_fblock(c
, FINALLY_TRY
, block
);
2853 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2854 compiler_use_next_block(c
, finally
);
2855 if (!compiler_push_fblock(c
, FINALLY_END
, finally
))
2858 /* Finally block starts; push tmpexit and issue our magic opcode. */
2859 if (!compiler_nameop(c
, tmpexit
, Load
) ||
2860 !compiler_nameop(c
, tmpexit
, Del
))
2862 ADDOP(c
, WITH_CLEANUP
);
2864 /* Finally block ends. */
2865 ADDOP(c
, END_FINALLY
);
2866 compiler_pop_fblock(c
, FINALLY_END
, finally
);
2871 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
2875 /* If expr e has a different line number than the last expr/stmt,
2876 set a new line number for the next instruction.
2878 if (e
->lineno
> c
->u
->u_lineno
) {
2879 c
->u
->u_lineno
= e
->lineno
;
2880 c
->u
->u_lineno_set
= false;
2884 return compiler_boolop(c
, e
);
2886 VISIT(c
, expr
, e
->v
.BinOp
.left
);
2887 VISIT(c
, expr
, e
->v
.BinOp
.right
);
2888 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
2891 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
2892 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
2895 return compiler_lambda(c
, e
);
2897 return compiler_ifexp(c
, e
);
2899 /* XXX get rid of arg? */
2900 ADDOP_I(c
, BUILD_MAP
, 0);
2901 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
2902 /* We must arrange things just right for STORE_SUBSCR.
2903 It wants the stack to look like (value) (dict) (key) */
2904 for (i
= 0; i
< n
; i
++) {
2907 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.values
, i
));
2910 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.keys
, i
));
2911 ADDOP(c
, STORE_SUBSCR
);
2915 return compiler_listcomp(c
, e
);
2916 case GeneratorExp_kind
:
2917 return compiler_genexp(c
, e
);
2919 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2920 return compiler_error(c
, "'yield' outside function");
2921 if (e
->v
.Yield
.value
) {
2922 VISIT(c
, expr
, e
->v
.Yield
.value
);
2925 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2927 ADDOP(c
, YIELD_VALUE
);
2930 return compiler_compare(c
, e
);
2932 return compiler_call(c
, e
);
2934 VISIT(c
, expr
, e
->v
.Repr
.value
);
2935 ADDOP(c
, UNARY_CONVERT
);
2938 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
2941 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
2943 /* The following exprs can be assignment targets. */
2944 case Attribute_kind
:
2945 if (e
->v
.Attribute
.ctx
!= AugStore
)
2946 VISIT(c
, expr
, e
->v
.Attribute
.value
);
2947 switch (e
->v
.Attribute
.ctx
) {
2950 /* Fall through to load */
2952 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
2956 /* Fall through to save */
2958 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
2961 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
2965 PyErr_SetString(PyExc_SystemError
,
2966 "param invalid in attribute expression");
2970 case Subscript_kind
:
2971 switch (e
->v
.Subscript
.ctx
) {
2973 VISIT(c
, expr
, e
->v
.Subscript
.value
);
2974 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
2977 VISIT(c
, expr
, e
->v
.Subscript
.value
);
2978 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
2981 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
2984 VISIT(c
, expr
, e
->v
.Subscript
.value
);
2985 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
2988 VISIT(c
, expr
, e
->v
.Subscript
.value
);
2989 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
2993 PyErr_SetString(PyExc_SystemError
,
2994 "param invalid in subscript expression");
2999 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3000 /* child nodes of List and Tuple will have expr_context set */
3002 return compiler_list(c
, e
);
3004 return compiler_tuple(c
, e
);
3010 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3012 expr_ty e
= s
->v
.AugAssign
.target
;
3015 assert(s
->kind
== AugAssign_kind
);
3018 case Attribute_kind
:
3019 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3020 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3023 VISIT(c
, expr
, auge
);
3024 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3025 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3026 auge
->v
.Attribute
.ctx
= AugStore
;
3027 VISIT(c
, expr
, auge
);
3029 case Subscript_kind
:
3030 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3031 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3034 VISIT(c
, expr
, auge
);
3035 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3036 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3037 auge
->v
.Subscript
.ctx
= AugStore
;
3038 VISIT(c
, expr
, auge
);
3041 if (!compiler_nameop(c
, e
->v
.Name
.id
, Load
))
3043 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3044 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3045 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3047 PyErr_Format(PyExc_SystemError
,
3048 "invalid node type (%d) for augmented assignment",
3056 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3058 struct fblockinfo
*f
;
3059 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
) {
3060 PyErr_SetString(PyExc_SystemError
,
3061 "too many statically nested blocks");
3064 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3071 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3073 struct compiler_unit
*u
= c
->u
;
3074 assert(u
->u_nfblocks
> 0);
3076 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3077 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3081 compiler_in_loop(struct compiler
*c
) {
3083 struct compiler_unit
*u
= c
->u
;
3084 for (i
= 0; i
< u
->u_nfblocks
; ++i
) {
3085 if (u
->u_fblock
[i
].fb_type
== LOOP
)
3090 /* Raises a SyntaxError and returns 0.
3091 If something goes wrong, a different exception may be raised.
3095 compiler_error(struct compiler
*c
, const char *errstr
)
3098 PyObject
*u
= NULL
, *v
= NULL
;
3100 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3105 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3109 v
= Py_BuildValue("(zO)", errstr
, u
);
3112 PyErr_SetObject(PyExc_SyntaxError
, v
);
3121 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3122 expr_context_ty ctx
)
3126 /* XXX this code is duplicated */
3128 case AugLoad
: /* fall through to Load */
3129 case Load
: op
= BINARY_SUBSCR
; break;
3130 case AugStore
:/* fall through to Store */
3131 case Store
: op
= STORE_SUBSCR
; break;
3132 case Del
: op
= DELETE_SUBSCR
; break;
3134 PyErr_Format(PyExc_SystemError
,
3135 "invalid %s kind %d in subscript\n",
3139 if (ctx
== AugLoad
) {
3140 ADDOP_I(c
, DUP_TOPX
, 2);
3142 else if (ctx
== AugStore
) {
3143 ADDOP(c
, ROT_THREE
);
3150 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3153 assert(s
->kind
== Slice_kind
);
3155 /* only handles the cases where BUILD_SLICE is emitted */
3156 if (s
->v
.Slice
.lower
) {
3157 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3160 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3163 if (s
->v
.Slice
.upper
) {
3164 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3167 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3170 if (s
->v
.Slice
.step
) {
3172 VISIT(c
, expr
, s
->v
.Slice
.step
);
3174 ADDOP_I(c
, BUILD_SLICE
, n
);
3179 compiler_simple_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3181 int op
= 0, slice_offset
= 0, stack_count
= 0;
3183 assert(s
->v
.Slice
.step
== NULL
);
3184 if (s
->v
.Slice
.lower
) {
3187 if (ctx
!= AugStore
)
3188 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3190 if (s
->v
.Slice
.upper
) {
3193 if (ctx
!= AugStore
)
3194 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3197 if (ctx
== AugLoad
) {
3198 switch (stack_count
) {
3199 case 0: ADDOP(c
, DUP_TOP
); break;
3200 case 1: ADDOP_I(c
, DUP_TOPX
, 2); break;
3201 case 2: ADDOP_I(c
, DUP_TOPX
, 3); break;
3204 else if (ctx
== AugStore
) {
3205 switch (stack_count
) {
3206 case 0: ADDOP(c
, ROT_TWO
); break;
3207 case 1: ADDOP(c
, ROT_THREE
); break;
3208 case 2: ADDOP(c
, ROT_FOUR
); break;
3213 case AugLoad
: /* fall through to Load */
3214 case Load
: op
= SLICE
; break;
3215 case AugStore
:/* fall through to Store */
3216 case Store
: op
= STORE_SLICE
; break;
3217 case Del
: op
= DELETE_SLICE
; break;
3220 PyErr_SetString(PyExc_SystemError
,
3221 "param invalid in simple slice");
3225 ADDOP(c
, op
+ slice_offset
);
3230 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3231 expr_context_ty ctx
)
3235 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3238 return compiler_slice(c
, s
, ctx
);
3240 VISIT(c
, expr
, s
->v
.Index
.value
);
3244 PyErr_SetString(PyExc_SystemError
,
3245 "extended slice invalid in nested slice");
3252 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3254 char * kindname
= NULL
;
3258 if (ctx
!= AugStore
) {
3259 VISIT(c
, expr
, s
->v
.Index
.value
);
3263 kindname
= "ellipsis";
3264 if (ctx
!= AugStore
) {
3265 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3270 if (!s
->v
.Slice
.step
)
3271 return compiler_simple_slice(c
, s
, ctx
);
3272 if (ctx
!= AugStore
) {
3273 if (!compiler_slice(c
, s
, ctx
))
3278 kindname
= "extended slice";
3279 if (ctx
!= AugStore
) {
3280 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3281 for (i
= 0; i
< n
; i
++) {
3282 slice_ty sub
= (slice_ty
)asdl_seq_GET(
3283 s
->v
.ExtSlice
.dims
, i
);
3284 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3287 ADDOP_I(c
, BUILD_TUPLE
, n
);
3291 PyErr_Format(PyExc_SystemError
,
3292 "invalid subscript kind %d", s
->kind
);
3295 return compiler_handle_subscr(c
, kindname
, ctx
);
3299 /* End of the compiler section, beginning of the assembler section */
3301 /* do depth-first search of basic block graph, starting with block.
3302 post records the block indices in post-order.
3304 XXX must handle implicit jumps from one block to next
3308 PyObject
*a_bytecode
; /* string containing bytecode */
3309 int a_offset
; /* offset into bytecode */
3310 int a_nblocks
; /* number of reachable blocks */
3311 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
3312 PyObject
*a_lnotab
; /* string containing lnotab */
3313 int a_lnotab_off
; /* offset into lnotab */
3314 int a_lineno
; /* last lineno of emitted instruction */
3315 int a_lineno_off
; /* bytecode offset of last lineno */
3319 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3322 struct instr
*instr
= NULL
;
3327 if (b
->b_next
!= NULL
)
3328 dfs(c
, b
->b_next
, a
);
3329 for (i
= 0; i
< b
->b_iused
; i
++) {
3330 instr
= &b
->b_instr
[i
];
3331 if (instr
->i_jrel
|| instr
->i_jabs
)
3332 dfs(c
, instr
->i_target
, a
);
3334 a
->a_postorder
[a
->a_nblocks
++] = b
;
3338 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3341 struct instr
*instr
;
3342 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3345 b
->b_startdepth
= depth
;
3346 for (i
= 0; i
< b
->b_iused
; i
++) {
3347 instr
= &b
->b_instr
[i
];
3348 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3349 if (depth
> maxdepth
)
3351 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3352 if (instr
->i_jrel
|| instr
->i_jabs
) {
3353 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3355 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3356 instr
->i_opcode
== JUMP_FORWARD
) {
3357 goto out
; /* remaining code is dead */
3362 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3368 /* Find the flow path that needs the largest stack. We assume that
3369 * cycles in the flow graph have no net effect on the stack depth.
3372 stackdepth(struct compiler
*c
)
3374 basicblock
*b
, *entryblock
;
3376 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3378 b
->b_startdepth
= INT_MIN
;
3383 return stackdepth_walk(c
, entryblock
, 0, 0);
3387 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3389 memset(a
, 0, sizeof(struct assembler
));
3390 a
->a_lineno
= firstlineno
;
3391 a
->a_bytecode
= PyString_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3394 a
->a_lnotab
= PyString_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3397 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3398 sizeof(basicblock
*) * nblocks
);
3399 if (!a
->a_postorder
) {
3407 assemble_free(struct assembler
*a
)
3409 Py_XDECREF(a
->a_bytecode
);
3410 Py_XDECREF(a
->a_lnotab
);
3412 PyObject_Free(a
->a_postorder
);
3415 /* Return the size of a basic block in bytes. */
3418 instrsize(struct instr
*instr
)
3420 if (!instr
->i_hasarg
)
3422 if (instr
->i_oparg
> 0xffff)
3428 blocksize(basicblock
*b
)
3433 for (i
= 0; i
< b
->b_iused
; i
++)
3434 size
+= instrsize(&b
->b_instr
[i
]);
3438 /* All about a_lnotab.
3440 c_lnotab is an array of unsigned bytes disguised as a Python string.
3441 It is used to map bytecode offsets to source code line #s (when needed
3444 The array is conceptually a list of
3445 (bytecode offset increment, line number increment)
3446 pairs. The details are important and delicate, best illustrated by example:
3448 byte code offset source code line number
3455 The first trick is that these numbers aren't stored, only the increments
3456 from one row to the next (this doesn't really work, but it's a start):
3458 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3460 The second trick is that an unsigned byte can't hold negative values, or
3461 values larger than 255, so (a) there's a deep assumption that byte code
3462 offsets and their corresponding line #s both increase monotonically, and (b)
3463 if at least one column jumps by more than 255 from one row to the next, more
3464 than one pair is written to the table. In case #b, there's no way to know
3465 from looking at the table later how many were written. That's the delicate
3466 part. A user of c_lnotab desiring to find the source line number
3467 corresponding to a bytecode address A should do something like this
3470 for addr_incr, line_incr in c_lnotab:
3476 In order for this to work, when the addr field increments by more than 255,
3477 the line # increment in each pair generated must be 0 until the remaining addr
3478 increment is < 256. So, in the example above, assemble_lnotab (it used
3479 to be called com_set_lineno) should not (as was actually done until 2.2)
3480 expand 300, 300 to 255, 255, 45, 45,
3481 but to 255, 0, 45, 255, 0, 45.
3485 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
3487 int d_bytecode
, d_lineno
;
3489 unsigned char *lnotab
;
3491 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
3492 d_lineno
= i
->i_lineno
- a
->a_lineno
;
3494 assert(d_bytecode
>= 0);
3495 assert(d_lineno
>= 0);
3497 /* XXX(nnorwitz): is there a better way to handle this?
3498 for loops are special, we want to be able to trace them
3499 each time around, so we need to set an extra line number. */
3500 if (d_lineno
== 0 && i
->i_opcode
!= FOR_ITER
)
3503 if (d_bytecode
> 255) {
3504 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
3505 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3506 len
= PyString_GET_SIZE(a
->a_lnotab
);
3507 if (nbytes
>= len
) {
3508 if (len
* 2 < nbytes
)
3512 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3515 lnotab
= (unsigned char *)
3516 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3517 for (j
= 0; j
< ncodes
; j
++) {
3521 d_bytecode
-= ncodes
* 255;
3522 a
->a_lnotab_off
+= ncodes
* 2;
3524 assert(d_bytecode
<= 255);
3525 if (d_lineno
> 255) {
3526 int j
, nbytes
, ncodes
= d_lineno
/ 255;
3527 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3528 len
= PyString_GET_SIZE(a
->a_lnotab
);
3529 if (nbytes
>= len
) {
3530 if (len
* 2 < nbytes
)
3534 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3537 lnotab
= (unsigned char *)
3538 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3539 *lnotab
++ = d_bytecode
;
3542 for (j
= 1; j
< ncodes
; j
++) {
3546 d_lineno
-= ncodes
* 255;
3547 a
->a_lnotab_off
+= ncodes
* 2;
3550 len
= PyString_GET_SIZE(a
->a_lnotab
);
3551 if (a
->a_lnotab_off
+ 2 >= len
) {
3552 if (_PyString_Resize(&a
->a_lnotab
, len
* 2) < 0)
3555 lnotab
= (unsigned char *)
3556 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3558 a
->a_lnotab_off
+= 2;
3560 *lnotab
++ = d_bytecode
;
3561 *lnotab
++ = d_lineno
;
3563 else { /* First line of a block; def stmt, etc. */
3565 *lnotab
++ = d_lineno
;
3567 a
->a_lineno
= i
->i_lineno
;
3568 a
->a_lineno_off
= a
->a_offset
;
3573 Extend the bytecode with a new instruction.
3574 Update lnotab if necessary.
3578 assemble_emit(struct assembler
*a
, struct instr
*i
)
3580 int size
, arg
= 0, ext
= 0;
3581 Py_ssize_t len
= PyString_GET_SIZE(a
->a_bytecode
);
3584 size
= instrsize(i
);
3589 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
3591 if (a
->a_offset
+ size
>= len
) {
3592 if (_PyString_Resize(&a
->a_bytecode
, len
* 2) < 0)
3595 code
= PyString_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
3596 a
->a_offset
+= size
;
3598 assert(i
->i_hasarg
);
3599 *code
++ = (char)EXTENDED_ARG
;
3600 *code
++ = ext
& 0xff;
3604 *code
++ = i
->i_opcode
;
3606 assert(size
== 3 || size
== 6);
3607 *code
++ = arg
& 0xff;
3614 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
3617 int bsize
, totsize
, extended_arg_count
, last_extended_arg_count
= 0;
3620 /* Compute the size of each block and fixup jump args.
3621 Replace block pointer with position in bytecode. */
3624 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
3625 b
= a
->a_postorder
[i
];
3626 bsize
= blocksize(b
);
3627 b
->b_offset
= totsize
;
3630 extended_arg_count
= 0;
3631 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3632 bsize
= b
->b_offset
;
3633 for (i
= 0; i
< b
->b_iused
; i
++) {
3634 struct instr
*instr
= &b
->b_instr
[i
];
3635 /* Relative jumps are computed relative to
3636 the instruction pointer after fetching
3637 the jump instruction.
3639 bsize
+= instrsize(instr
);
3641 instr
->i_oparg
= instr
->i_target
->b_offset
;
3642 else if (instr
->i_jrel
) {
3643 int delta
= instr
->i_target
->b_offset
- bsize
;
3644 instr
->i_oparg
= delta
;
3648 if (instr
->i_oparg
> 0xffff)
3649 extended_arg_count
++;
3653 /* XXX: This is an awful hack that could hurt performance, but
3654 on the bright side it should work until we come up
3655 with a better solution.
3657 In the meantime, should the goto be dropped in favor
3660 The issue is that in the first loop blocksize() is called
3661 which calls instrsize() which requires i_oparg be set
3662 appropriately. There is a bootstrap problem because
3663 i_oparg is calculated in the second loop above.
3665 So we loop until we stop seeing new EXTENDED_ARGs.
3666 The only EXTENDED_ARGs that could be popping up are
3667 ones in jump instructions. So this should converge
3670 if (last_extended_arg_count
!= extended_arg_count
) {
3671 last_extended_arg_count
= extended_arg_count
;
3677 dict_keys_inorder(PyObject
*dict
, int offset
)
3679 PyObject
*tuple
, *k
, *v
;
3680 Py_ssize_t i
, pos
= 0, size
= PyDict_Size(dict
);
3682 tuple
= PyTuple_New(size
);
3685 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3686 i
= PyInt_AS_LONG(v
);
3687 k
= PyTuple_GET_ITEM(k
, 0);
3689 assert((i
- offset
) < size
);
3690 assert((i
- offset
) >= 0);
3691 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3697 compute_code_flags(struct compiler
*c
)
3699 PySTEntryObject
*ste
= c
->u
->u_ste
;
3701 if (ste
->ste_type
!= ModuleBlock
)
3702 flags
|= CO_NEWLOCALS
;
3703 if (ste
->ste_type
== FunctionBlock
) {
3704 if (!ste
->ste_unoptimized
)
3705 flags
|= CO_OPTIMIZED
;
3706 if (ste
->ste_nested
)
3708 if (ste
->ste_generator
)
3709 flags
|= CO_GENERATOR
;
3711 if (ste
->ste_varargs
)
3712 flags
|= CO_VARARGS
;
3713 if (ste
->ste_varkeywords
)
3714 flags
|= CO_VARKEYWORDS
;
3715 if (ste
->ste_generator
)
3716 flags
|= CO_GENERATOR
;
3718 /* (Only) inherit compilerflags in PyCF_MASK */
3719 flags
|= (c
->c_flags
->cf_flags
& PyCF_MASK
);
3721 n
= PyDict_Size(c
->u
->u_freevars
);
3725 n
= PyDict_Size(c
->u
->u_cellvars
);
3736 static PyCodeObject
*
3737 makecode(struct compiler
*c
, struct assembler
*a
)
3740 PyCodeObject
*co
= NULL
;
3741 PyObject
*consts
= NULL
;
3742 PyObject
*names
= NULL
;
3743 PyObject
*varnames
= NULL
;
3744 PyObject
*filename
= NULL
;
3745 PyObject
*name
= NULL
;
3746 PyObject
*freevars
= NULL
;
3747 PyObject
*cellvars
= NULL
;
3748 PyObject
*bytecode
= NULL
;
3751 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
3754 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
3757 names
= dict_keys_inorder(c
->u
->u_names
, 0);
3758 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
3759 if (!consts
|| !names
|| !varnames
)
3762 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
3765 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
3768 filename
= PyString_FromString(c
->c_filename
);
3772 nlocals
= PyDict_Size(c
->u
->u_varnames
);
3773 flags
= compute_code_flags(c
);
3777 bytecode
= PyCode_Optimize(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
3781 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
3787 co
= PyCode_New(c
->u
->u_argcount
, nlocals
, stackdepth(c
), flags
,
3788 bytecode
, consts
, names
, varnames
,
3790 filename
, c
->u
->u_name
,
3791 c
->u
->u_firstlineno
,
3796 Py_XDECREF(varnames
);
3797 Py_XDECREF(filename
);
3799 Py_XDECREF(freevars
);
3800 Py_XDECREF(cellvars
);
3801 Py_XDECREF(bytecode
);
3806 /* For debugging purposes only */
3809 dump_instr(const struct instr
*i
)
3811 const char *jrel
= i
->i_jrel
? "jrel " : "";
3812 const char *jabs
= i
->i_jabs
? "jabs " : "";
3817 sprintf(arg
, "arg: %d ", i
->i_oparg
);
3819 fprintf(stderr
, "line: %d, opcode: %d %s%s%s\n",
3820 i
->i_lineno
, i
->i_opcode
, arg
, jabs
, jrel
);
3824 dump_basicblock(const basicblock
*b
)
3826 const char *seen
= b
->b_seen
? "seen " : "";
3827 const char *b_return
= b
->b_return
? "return " : "";
3828 fprintf(stderr
, "used: %d, depth: %d, offset: %d %s%s\n",
3829 b
->b_iused
, b
->b_startdepth
, b
->b_offset
, seen
, b_return
);
3832 for (i
= 0; i
< b
->b_iused
; i
++) {
3833 fprintf(stderr
, " [%02d] ", i
);
3834 dump_instr(b
->b_instr
+ i
);
3840 static PyCodeObject
*
3841 assemble(struct compiler
*c
, int addNone
)
3843 basicblock
*b
, *entryblock
;
3846 PyCodeObject
*co
= NULL
;
3848 /* Make sure every block that falls off the end returns None.
3849 XXX NEXT_BLOCK() isn't quite right, because if the last
3850 block ends with a jump or return b_next shouldn't set.
3852 if (!c
->u
->u_curblock
->b_return
) {
3855 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3856 ADDOP(c
, RETURN_VALUE
);
3861 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3866 /* Set firstlineno if it wasn't explicitly set. */
3867 if (!c
->u
->u_firstlineno
) {
3868 if (entryblock
&& entryblock
->b_instr
)
3869 c
->u
->u_firstlineno
= entryblock
->b_instr
->i_lineno
;
3871 c
->u
->u_firstlineno
= 1;
3873 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
3875 dfs(c
, entryblock
, &a
);
3877 /* Can't modify the bytecode after computing jump offsets. */
3878 assemble_jump_offsets(&a
, c
);
3880 /* Emit code in reverse postorder from dfs. */
3881 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
3882 b
= a
.a_postorder
[i
];
3883 for (j
= 0; j
< b
->b_iused
; j
++)
3884 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
3888 if (_PyString_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
3890 if (_PyString_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
3893 co
= makecode(c
, &a
);