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
43 #define COMP_SETCOMP 1
44 #define COMP_DICTCOMP 2
49 unsigned i_hasarg
: 1;
50 unsigned char i_opcode
;
52 struct basicblock_
*i_target
; /* target block (if jump instruction) */
56 typedef struct basicblock_
{
57 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
60 struct basicblock_
*b_list
;
61 /* number of instructions used */
63 /* length of instruction array (b_instr) */
65 /* pointer to an array of instructions, initially NULL */
66 struct instr
*b_instr
;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_
*b_next
;
70 /* b_seen is used to perform a DFS of basicblocks. */
72 /* b_return is true if a RETURN_VALUE opcode is inserted. */
73 unsigned b_return
: 1;
74 /* depth of stack upon entry of block, computed by stackdepth() */
76 /* instruction offset for block, computed by assemble_jump_offsets() */
80 /* fblockinfo tracks the current frame block.
82 A frame block is used to handle loops, try/except, and try/finally.
83 It's called a frame block to distinguish it from a basic block in the
87 enum fblocktype
{ LOOP
, EXCEPT
, FINALLY_TRY
, FINALLY_END
};
90 enum fblocktype fb_type
;
94 /* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
97 struct compiler_unit
{
98 PySTEntryObject
*u_ste
;
101 /* The following fields are dicts that map objects to
102 the index of them in co_XXX. The index is used as
103 the argument for opcodes that refer to those collections.
105 PyObject
*u_consts
; /* all constants */
106 PyObject
*u_names
; /* all names */
107 PyObject
*u_varnames
; /* local variables */
108 PyObject
*u_cellvars
; /* cell variables */
109 PyObject
*u_freevars
; /* free variables */
111 PyObject
*u_private
; /* for private name mangling */
113 int u_argcount
; /* number of arguments for block */
114 /* Pointer to the most recently allocated block. By following b_list
115 members, you can reach all early allocated blocks. */
116 basicblock
*u_blocks
;
117 basicblock
*u_curblock
; /* pointer to current block */
120 struct fblockinfo u_fblock
[CO_MAXBLOCKS
];
122 int u_firstlineno
; /* the first lineno of the block */
123 int u_lineno
; /* the lineno for the current stmt */
124 bool u_lineno_set
; /* boolean to indicate whether instr
125 has been generated with current lineno */
128 /* This struct captures the global state of a compilation.
130 The u pointer points to the current compilation unit, while units
131 for enclosing blocks are stored in c_stack. The u and c_stack are
132 managed by compiler_enter_scope() and compiler_exit_scope().
136 const char *c_filename
;
137 struct symtable
*c_st
;
138 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
139 PyCompilerFlags
*c_flags
;
141 int c_interactive
; /* true if in interactive mode */
144 struct compiler_unit
*u
; /* compiler state for current block */
145 PyObject
*c_stack
; /* Python list holding compiler_unit ptrs */
146 PyArena
*c_arena
; /* pointer to memory allocation arena */
149 static int compiler_enter_scope(struct compiler
*, identifier
, void *, int);
150 static void compiler_free(struct compiler
*);
151 static basicblock
*compiler_new_block(struct compiler
*);
152 static int compiler_next_instr(struct compiler
*, basicblock
*);
153 static int compiler_addop(struct compiler
*, int);
154 static int compiler_addop_o(struct compiler
*, int, PyObject
*, PyObject
*);
155 static int compiler_addop_i(struct compiler
*, int, int);
156 static int compiler_addop_j(struct compiler
*, int, basicblock
*, int);
157 static basicblock
*compiler_use_new_block(struct compiler
*);
158 static int compiler_error(struct compiler
*, const char *);
159 static int compiler_nameop(struct compiler
*, identifier
, expr_context_ty
);
161 static PyCodeObject
*compiler_mod(struct compiler
*, mod_ty
);
162 static int compiler_visit_stmt(struct compiler
*, stmt_ty
);
163 static int compiler_visit_keyword(struct compiler
*, keyword_ty
);
164 static int compiler_visit_expr(struct compiler
*, expr_ty
);
165 static int compiler_augassign(struct compiler
*, stmt_ty
);
166 static int compiler_visit_slice(struct compiler
*, slice_ty
,
169 static int compiler_push_fblock(struct compiler
*, enum fblocktype
,
171 static void compiler_pop_fblock(struct compiler
*, enum fblocktype
,
173 /* Returns true if there is a loop on the fblock stack. */
174 static int compiler_in_loop(struct compiler
*);
176 static int inplace_binop(struct compiler
*, operator_ty
);
177 static int expr_constant(expr_ty e
);
179 static int compiler_with(struct compiler
*, stmt_ty
);
181 static PyCodeObject
*assemble(struct compiler
*, int addNone
);
182 static PyObject
*__doc__
;
184 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
187 _Py_Mangle(PyObject
*privateobj
, PyObject
*ident
)
189 /* Name mangling: __private becomes _classname__private.
190 This is independent from how the name is used. */
191 const char *p
, *name
= PyString_AsString(ident
);
194 if (privateobj
== NULL
|| !PyString_Check(privateobj
) ||
195 name
== NULL
|| name
[0] != '_' || name
[1] != '_') {
199 p
= PyString_AsString(privateobj
);
201 /* Don't mangle __id__ or names with dots.
203 The only time a name with a dot can occur is when
204 we are compiling an import statement that has a
207 TODO(jhylton): Decide whether we want to support
208 mangling of the module name, e.g. __M.X.
210 if ((name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
211 || strchr(name
, '.')) {
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 */
224 assert(1 <= PY_SSIZE_T_MAX
- nlen
);
225 assert(1 + nlen
<= PY_SSIZE_T_MAX
- plen
);
227 ident
= PyString_FromStringAndSize(NULL
, 1 + nlen
+ plen
);
230 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
231 buffer
= PyString_AS_STRING(ident
);
233 strncpy(buffer
+1, p
, plen
);
234 strcpy(buffer
+1+plen
, name
);
239 compiler_init(struct compiler
*c
)
241 memset(c
, 0, sizeof(struct compiler
));
243 c
->c_stack
= PyList_New(0);
251 PyAST_Compile(mod_ty mod
, const char *filename
, PyCompilerFlags
*flags
,
255 PyCodeObject
*co
= NULL
;
256 PyCompilerFlags local_flags
;
260 __doc__
= PyString_InternFromString("__doc__");
265 if (!compiler_init(&c
))
267 c
.c_filename
= filename
;
269 c
.c_future
= PyFuture_FromAST(mod
, filename
);
270 if (c
.c_future
== NULL
)
273 local_flags
.cf_flags
= 0;
274 flags
= &local_flags
;
276 merged
= c
.c_future
->ff_features
| flags
->cf_flags
;
277 c
.c_future
->ff_features
= merged
;
278 flags
->cf_flags
= merged
;
282 c
.c_st
= PySymtable_Build(mod
, filename
, c
.c_future
);
283 if (c
.c_st
== NULL
) {
284 if (!PyErr_Occurred())
285 PyErr_SetString(PyExc_SystemError
, "no symtable");
289 co
= compiler_mod(&c
, mod
);
293 assert(co
|| PyErr_Occurred());
298 PyNode_Compile(struct _node
*n
, const char *filename
)
300 PyCodeObject
*co
= NULL
;
302 PyArena
*arena
= PyArena_New();
305 mod
= PyAST_FromNode(n
, NULL
, filename
, arena
);
307 co
= PyAST_Compile(mod
, filename
, NULL
, arena
);
313 compiler_free(struct compiler
*c
)
316 PySymtable_Free(c
->c_st
);
318 PyObject_Free(c
->c_future
);
319 Py_DECREF(c
->c_stack
);
323 list2dict(PyObject
*list
)
327 PyObject
*dict
= PyDict_New();
328 if (!dict
) return NULL
;
330 n
= PyList_Size(list
);
331 for (i
= 0; i
< n
; i
++) {
332 v
= PyInt_FromLong(i
);
337 k
= PyList_GET_ITEM(list
, i
);
338 k
= PyTuple_Pack(2, k
, k
->ob_type
);
339 if (k
== NULL
|| PyDict_SetItem(dict
, k
, v
) < 0) {
351 /* Return new dict containing names from src that match scope(s).
353 src is a symbol table dictionary. If the scope of a name matches
354 either scope_type or flag is set, insert it into the new dict. The
355 values are integers, starting at offset and increasing by one for
360 dictbytype(PyObject
*src
, int scope_type
, int flag
, int offset
)
362 Py_ssize_t pos
= 0, i
= offset
, scope
;
363 PyObject
*k
, *v
, *dest
= PyDict_New();
369 while (PyDict_Next(src
, &pos
, &k
, &v
)) {
370 /* XXX this should probably be a macro in symtable.h */
371 assert(PyInt_Check(v
));
372 scope
= (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
374 if (scope
== scope_type
|| PyInt_AS_LONG(v
) & flag
) {
375 PyObject
*tuple
, *item
= PyInt_FromLong(i
);
381 tuple
= PyTuple_Pack(2, k
, k
->ob_type
);
382 if (!tuple
|| PyDict_SetItem(dest
, tuple
, item
) < 0) {
396 compiler_unit_check(struct compiler_unit
*u
)
399 for (block
= u
->u_blocks
; block
!= NULL
; block
= block
->b_list
) {
400 assert((void *)block
!= (void *)0xcbcbcbcb);
401 assert((void *)block
!= (void *)0xfbfbfbfb);
402 assert((void *)block
!= (void *)0xdbdbdbdb);
403 if (block
->b_instr
!= NULL
) {
404 assert(block
->b_ialloc
> 0);
405 assert(block
->b_iused
> 0);
406 assert(block
->b_ialloc
>= block
->b_iused
);
409 assert (block
->b_iused
== 0);
410 assert (block
->b_ialloc
== 0);
416 compiler_unit_free(struct compiler_unit
*u
)
418 basicblock
*b
, *next
;
420 compiler_unit_check(u
);
424 PyObject_Free((void *)b
->b_instr
);
426 PyObject_Free((void *)b
);
431 Py_CLEAR(u
->u_consts
);
432 Py_CLEAR(u
->u_names
);
433 Py_CLEAR(u
->u_varnames
);
434 Py_CLEAR(u
->u_freevars
);
435 Py_CLEAR(u
->u_cellvars
);
436 Py_CLEAR(u
->u_private
);
441 compiler_enter_scope(struct compiler
*c
, identifier name
, void *key
,
444 struct compiler_unit
*u
;
446 u
= (struct compiler_unit
*)PyObject_Malloc(sizeof(
447 struct compiler_unit
));
452 memset(u
, 0, sizeof(struct compiler_unit
));
454 u
->u_ste
= PySymtable_Lookup(c
->c_st
, key
);
456 compiler_unit_free(u
);
461 u
->u_varnames
= list2dict(u
->u_ste
->ste_varnames
);
462 u
->u_cellvars
= dictbytype(u
->u_ste
->ste_symbols
, CELL
, 0, 0);
463 if (!u
->u_varnames
|| !u
->u_cellvars
) {
464 compiler_unit_free(u
);
468 u
->u_freevars
= dictbytype(u
->u_ste
->ste_symbols
, FREE
, DEF_FREE_CLASS
,
469 PyDict_Size(u
->u_cellvars
));
470 if (!u
->u_freevars
) {
471 compiler_unit_free(u
);
477 u
->u_firstlineno
= lineno
;
479 u
->u_lineno_set
= false;
480 u
->u_consts
= PyDict_New();
482 compiler_unit_free(u
);
485 u
->u_names
= PyDict_New();
487 compiler_unit_free(u
);
493 /* Push the old compiler_unit on the stack. */
495 PyObject
*capsule
= PyCapsule_New(c
->u
, COMPILER_CAPSULE_NAME_COMPILER_UNIT
, NULL
);
496 if (!capsule
|| PyList_Append(c
->c_stack
, capsule
) < 0) {
498 compiler_unit_free(u
);
502 u
->u_private
= c
->u
->u_private
;
503 Py_XINCREF(u
->u_private
);
508 if (compiler_use_new_block(c
) == NULL
)
515 compiler_exit_scope(struct compiler
*c
)
521 compiler_unit_free(c
->u
);
522 /* Restore c->u to the parent unit. */
523 n
= PyList_GET_SIZE(c
->c_stack
) - 1;
525 capsule
= PyList_GET_ITEM(c
->c_stack
, n
);
526 c
->u
= (struct compiler_unit
*)PyCapsule_GetPointer(capsule
, COMPILER_CAPSULE_NAME_COMPILER_UNIT
);
528 /* we are deleting from a list so this really shouldn't fail */
529 if (PySequence_DelItem(c
->c_stack
, n
) < 0)
530 Py_FatalError("compiler_exit_scope()");
531 compiler_unit_check(c
->u
);
538 /* Allocate a new block and return a pointer to it.
539 Returns NULL on error.
543 compiler_new_block(struct compiler
*c
)
546 struct compiler_unit
*u
;
549 b
= (basicblock
*)PyObject_Malloc(sizeof(basicblock
));
554 memset((void *)b
, 0, sizeof(basicblock
));
555 /* Extend the singly linked list of blocks with new block. */
556 b
->b_list
= u
->u_blocks
;
562 compiler_use_new_block(struct compiler
*c
)
564 basicblock
*block
= compiler_new_block(c
);
567 c
->u
->u_curblock
= block
;
572 compiler_next_block(struct compiler
*c
)
574 basicblock
*block
= compiler_new_block(c
);
577 c
->u
->u_curblock
->b_next
= block
;
578 c
->u
->u_curblock
= block
;
583 compiler_use_next_block(struct compiler
*c
, basicblock
*block
)
585 assert(block
!= NULL
);
586 c
->u
->u_curblock
->b_next
= block
;
587 c
->u
->u_curblock
= block
;
591 /* Returns the offset of the next instruction in the current block's
592 b_instr array. Resizes the b_instr as necessary.
593 Returns -1 on failure.
597 compiler_next_instr(struct compiler
*c
, basicblock
*b
)
600 if (b
->b_instr
== NULL
) {
601 b
->b_instr
= (struct instr
*)PyObject_Malloc(
602 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
603 if (b
->b_instr
== NULL
) {
607 b
->b_ialloc
= DEFAULT_BLOCK_SIZE
;
608 memset((char *)b
->b_instr
, 0,
609 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
611 else if (b
->b_iused
== b
->b_ialloc
) {
613 size_t oldsize
, newsize
;
614 oldsize
= b
->b_ialloc
* sizeof(struct instr
);
615 newsize
= oldsize
<< 1;
617 if (oldsize
> (PY_SIZE_MAX
>> 1)) {
627 tmp
= (struct instr
*)PyObject_Realloc(
628 (void *)b
->b_instr
, newsize
);
634 memset((char *)b
->b_instr
+ oldsize
, 0, newsize
- oldsize
);
639 /* Set the i_lineno member of the instruction at offset off if the
640 line number for the current expression/statement has not
641 already been set. If it has been set, the call has no effect.
643 The line number is reset in the following cases:
644 - when entering a new scope
646 - on each expression that start a new line
647 - before the "except" clause
648 - before the "for" and "while" expressions
652 compiler_set_lineno(struct compiler
*c
, int off
)
655 if (c
->u
->u_lineno_set
)
657 c
->u
->u_lineno_set
= true;
658 b
= c
->u
->u_curblock
;
659 b
->b_instr
[off
].i_lineno
= c
->u
->u_lineno
;
663 opcode_stack_effect(int opcode
, int oparg
)
691 case BINARY_MULTIPLY
:
695 case BINARY_SUBTRACT
:
697 case BINARY_FLOOR_DIVIDE
:
698 case BINARY_TRUE_DIVIDE
:
700 case INPLACE_FLOOR_DIVIDE
:
701 case INPLACE_TRUE_DIVIDE
:
732 case INPLACE_SUBTRACT
:
733 case INPLACE_MULTIPLY
:
763 case PRINT_NEWLINE_TO
:
776 return -1; /* XXX Sometimes more */
791 return -3; /* or -1 or -2 if no exception occurred or
792 return/break/continue */
800 case UNPACK_SEQUENCE
:
803 return 1; /* or -1, at end of iterator */
835 case JUMP_IF_TRUE_OR_POP
: /* -1 if jump not taken */
836 case JUMP_IF_FALSE_OR_POP
: /* "" */
840 case POP_JUMP_IF_FALSE
:
841 case POP_JUMP_IF_TRUE
:
863 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
865 return -NARGS(oparg
);
866 case CALL_FUNCTION_VAR
:
867 case CALL_FUNCTION_KW
:
868 return -NARGS(oparg
)-1;
869 case CALL_FUNCTION_VAR_KW
:
870 return -NARGS(oparg
)-2;
889 fprintf(stderr
, "opcode = %d\n", opcode
);
890 Py_FatalError("opcode_stack_effect()");
893 return 0; /* not reachable */
896 /* Add an opcode with no argument.
897 Returns 0 on failure, 1 on success.
901 compiler_addop(struct compiler
*c
, int opcode
)
906 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
909 b
= c
->u
->u_curblock
;
910 i
= &b
->b_instr
[off
];
911 i
->i_opcode
= opcode
;
913 if (opcode
== RETURN_VALUE
)
915 compiler_set_lineno(c
, off
);
920 compiler_add_o(struct compiler
*c
, PyObject
*dict
, PyObject
*o
)
926 /* necessary to make sure types aren't coerced (e.g., int and long) */
927 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
928 if (PyFloat_Check(o
)) {
929 d
= PyFloat_AS_DOUBLE(o
);
930 /* all we need is to make the tuple different in either the 0.0
931 * or -0.0 case from all others, just to avoid the "coercion".
933 if (d
== 0.0 && copysign(1.0, d
) < 0.0)
934 t
= PyTuple_Pack(3, o
, o
->ob_type
, Py_None
);
936 t
= PyTuple_Pack(2, o
, o
->ob_type
);
938 #ifndef WITHOUT_COMPLEX
939 else if (PyComplex_Check(o
)) {
941 int real_negzero
, imag_negzero
;
942 /* For the complex case we must make complex(x, 0.)
943 different from complex(x, -0.) and complex(0., y)
944 different from complex(-0., y), for any x and y.
945 All four complex zeros must be distinguished.*/
946 z
= PyComplex_AsCComplex(o
);
947 real_negzero
= z
.real
== 0.0 && copysign(1.0, z
.real
) < 0.0;
948 imag_negzero
= z
.imag
== 0.0 && copysign(1.0, z
.imag
) < 0.0;
949 if (real_negzero
&& imag_negzero
) {
950 t
= PyTuple_Pack(5, o
, o
->ob_type
,
951 Py_None
, Py_None
, Py_None
);
953 else if (imag_negzero
) {
954 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_None
, Py_None
);
956 else if (real_negzero
) {
957 t
= PyTuple_Pack(3, o
, o
->ob_type
, Py_None
);
960 t
= PyTuple_Pack(2, o
, o
->ob_type
);
963 #endif /* WITHOUT_COMPLEX */
965 t
= PyTuple_Pack(2, o
, o
->ob_type
);
970 v
= PyDict_GetItem(dict
, t
);
972 arg
= PyDict_Size(dict
);
973 v
= PyInt_FromLong(arg
);
978 if (PyDict_SetItem(dict
, t
, v
) < 0) {
986 arg
= PyInt_AsLong(v
);
992 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
995 int arg
= compiler_add_o(c
, dict
, o
);
998 return compiler_addop_i(c
, opcode
, arg
);
1002 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
1006 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
1009 arg
= compiler_add_o(c
, dict
, mangled
);
1013 return compiler_addop_i(c
, opcode
, arg
);
1016 /* Add an opcode with an integer argument.
1017 Returns 0 on failure, 1 on success.
1021 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
1025 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1028 i
= &c
->u
->u_curblock
->b_instr
[off
];
1029 i
->i_opcode
= opcode
;
1032 compiler_set_lineno(c
, off
);
1037 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
1043 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1046 i
= &c
->u
->u_curblock
->b_instr
[off
];
1047 i
->i_opcode
= opcode
;
1054 compiler_set_lineno(c
, off
);
1058 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1059 like to find better names.) NEW_BLOCK() creates a new block and sets
1060 it as the current block. NEXT_BLOCK() also creates an implicit jump
1061 from the current block to the new block.
1064 /* The returns inside these macros make it impossible to decref objects
1065 created in the local function. Local objects should use the arena.
1069 #define NEW_BLOCK(C) { \
1070 if (compiler_use_new_block((C)) == NULL) \
1074 #define NEXT_BLOCK(C) { \
1075 if (compiler_next_block((C)) == NULL) \
1079 #define ADDOP(C, OP) { \
1080 if (!compiler_addop((C), (OP))) \
1084 #define ADDOP_IN_SCOPE(C, OP) { \
1085 if (!compiler_addop((C), (OP))) { \
1086 compiler_exit_scope(c); \
1091 #define ADDOP_O(C, OP, O, TYPE) { \
1092 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1096 #define ADDOP_NAME(C, OP, O, TYPE) { \
1097 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1101 #define ADDOP_I(C, OP, O) { \
1102 if (!compiler_addop_i((C), (OP), (O))) \
1106 #define ADDOP_JABS(C, OP, O) { \
1107 if (!compiler_addop_j((C), (OP), (O), 1)) \
1111 #define ADDOP_JREL(C, OP, O) { \
1112 if (!compiler_addop_j((C), (OP), (O), 0)) \
1116 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1117 the ASDL name to synthesize the name of the C type and the visit function.
1120 #define VISIT(C, TYPE, V) {\
1121 if (!compiler_visit_ ## TYPE((C), (V))) \
1125 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1126 if (!compiler_visit_ ## TYPE((C), (V))) { \
1127 compiler_exit_scope(c); \
1132 #define VISIT_SLICE(C, V, CTX) {\
1133 if (!compiler_visit_slice((C), (V), (CTX))) \
1137 #define VISIT_SEQ(C, TYPE, SEQ) { \
1139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1142 if (!compiler_visit_ ## TYPE((C), elt)) \
1147 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1149 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1150 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1151 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1152 if (!compiler_visit_ ## TYPE((C), elt)) { \
1153 compiler_exit_scope(c); \
1160 compiler_isdocstring(stmt_ty s
)
1162 if (s
->kind
!= Expr_kind
)
1164 return s
->v
.Expr
.value
->kind
== Str_kind
;
1167 /* Compile a sequence of statements, checking for a docstring. */
1170 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1175 if (!asdl_seq_LEN(stmts
))
1177 st
= (stmt_ty
)asdl_seq_GET(stmts
, 0);
1178 if (compiler_isdocstring(st
) && Py_OptimizeFlag
< 2) {
1179 /* don't generate docstrings if -OO */
1181 VISIT(c
, expr
, st
->v
.Expr
.value
);
1182 if (!compiler_nameop(c
, __doc__
, Store
))
1185 for (; i
< asdl_seq_LEN(stmts
); i
++)
1186 VISIT(c
, stmt
, (stmt_ty
)asdl_seq_GET(stmts
, i
));
1190 static PyCodeObject
*
1191 compiler_mod(struct compiler
*c
, mod_ty mod
)
1195 static PyObject
*module
;
1197 module
= PyString_InternFromString("<module>");
1201 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1202 if (!compiler_enter_scope(c
, module
, mod
, 0))
1204 switch (mod
->kind
) {
1206 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1207 compiler_exit_scope(c
);
1211 case Interactive_kind
:
1212 c
->c_interactive
= 1;
1213 VISIT_SEQ_IN_SCOPE(c
, stmt
,
1214 mod
->v
.Interactive
.body
);
1216 case Expression_kind
:
1217 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1221 PyErr_SetString(PyExc_SystemError
,
1222 "suite should not be possible");
1225 PyErr_Format(PyExc_SystemError
,
1226 "module kind %d should not be possible",
1230 co
= assemble(c
, addNone
);
1231 compiler_exit_scope(c
);
1235 /* The test for LOCAL must come before the test for FREE in order to
1236 handle classes where name is both local and free. The local var is
1237 a method and the free var is a free var referenced within a method.
1241 get_ref_type(struct compiler
*c
, PyObject
*name
)
1243 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1246 PyOS_snprintf(buf
, sizeof(buf
),
1247 "unknown scope for %.100s in %.100s(%s) in %s\n"
1248 "symbols: %s\nlocals: %s\nglobals: %s",
1249 PyString_AS_STRING(name
),
1250 PyString_AS_STRING(c
->u
->u_name
),
1251 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1253 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1254 PyObject_REPR(c
->u
->u_varnames
),
1255 PyObject_REPR(c
->u
->u_names
)
1264 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1267 k
= PyTuple_Pack(2, name
, name
->ob_type
);
1270 v
= PyDict_GetItem(dict
, k
);
1274 return PyInt_AS_LONG(v
);
1278 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1280 int i
, free
= PyCode_GetNumFree(co
);
1282 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1283 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1286 for (i
= 0; i
< free
; ++i
) {
1287 /* Bypass com_addop_varname because it will generate
1288 LOAD_DEREF but LOAD_CLOSURE is needed.
1290 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1293 /* Special case: If a class contains a method with a
1294 free variable that has the same name as a method,
1295 the name will be considered free *and* local in the
1296 class. It should be handled by the closure, as
1297 well as by the normal name loookup logic.
1299 reftype
= get_ref_type(c
, name
);
1300 if (reftype
== CELL
)
1301 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1302 else /* (reftype == FREE) */
1303 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1305 printf("lookup %s in %s %d %d\n"
1306 "freevars of %s: %s\n",
1307 PyObject_REPR(name
),
1308 PyString_AS_STRING(c
->u
->u_name
),
1310 PyString_AS_STRING(co
->co_name
),
1311 PyObject_REPR(co
->co_freevars
));
1312 Py_FatalError("compiler_make_closure()");
1314 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1316 ADDOP_I(c
, BUILD_TUPLE
, free
);
1317 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1318 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1323 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1330 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1331 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(decos
, i
));
1337 compiler_arguments(struct compiler
*c
, arguments_ty args
)
1340 int n
= asdl_seq_LEN(args
->args
);
1341 /* Correctly handle nested argument lists */
1342 for (i
= 0; i
< n
; i
++) {
1343 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
->args
, i
);
1344 if (arg
->kind
== Tuple_kind
) {
1345 PyObject
*id
= PyString_FromFormat(".%d", i
);
1349 if (!compiler_nameop(c
, id
, Load
)) {
1354 VISIT(c
, expr
, arg
);
1361 compiler_function(struct compiler
*c
, stmt_ty s
)
1364 PyObject
*first_const
= Py_None
;
1365 arguments_ty args
= s
->v
.FunctionDef
.args
;
1366 asdl_seq
* decos
= s
->v
.FunctionDef
.decorator_list
;
1368 int i
, n
, docstring
;
1370 assert(s
->kind
== FunctionDef_kind
);
1372 if (!compiler_decorators(c
, decos
))
1375 VISIT_SEQ(c
, expr
, args
->defaults
);
1376 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1380 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1381 docstring
= compiler_isdocstring(st
);
1382 if (docstring
&& Py_OptimizeFlag
< 2)
1383 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1384 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1385 compiler_exit_scope(c
);
1389 /* unpack nested arguments */
1390 compiler_arguments(c
, args
);
1392 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1393 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1394 /* if there was a docstring, we need to skip the first statement */
1395 for (i
= docstring
; i
< n
; i
++) {
1396 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1397 VISIT_IN_SCOPE(c
, stmt
, st
);
1399 co
= assemble(c
, 1);
1400 compiler_exit_scope(c
);
1404 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1407 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1408 ADDOP_I(c
, CALL_FUNCTION
, 1);
1411 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1415 compiler_class(struct compiler
*c
, stmt_ty s
)
1420 asdl_seq
* decos
= s
->v
.ClassDef
.decorator_list
;
1422 if (!compiler_decorators(c
, decos
))
1425 /* push class name on stack, needed by BUILD_CLASS */
1426 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1427 /* push the tuple of base classes on the stack */
1428 n
= asdl_seq_LEN(s
->v
.ClassDef
.bases
);
1430 VISIT_SEQ(c
, expr
, s
->v
.ClassDef
.bases
);
1431 ADDOP_I(c
, BUILD_TUPLE
, n
);
1432 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
,
1435 Py_XDECREF(c
->u
->u_private
);
1436 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1437 Py_INCREF(c
->u
->u_private
);
1438 str
= PyString_InternFromString("__name__");
1439 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1441 compiler_exit_scope(c
);
1446 str
= PyString_InternFromString("__module__");
1447 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
1449 compiler_exit_scope(c
);
1454 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
1455 compiler_exit_scope(c
);
1459 ADDOP_IN_SCOPE(c
, LOAD_LOCALS
);
1460 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1461 co
= assemble(c
, 1);
1462 compiler_exit_scope(c
);
1466 compiler_make_closure(c
, co
, 0);
1469 ADDOP_I(c
, CALL_FUNCTION
, 0);
1470 ADDOP(c
, BUILD_CLASS
);
1471 /* apply decorators */
1472 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1473 ADDOP_I(c
, CALL_FUNCTION
, 1);
1475 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
1481 compiler_ifexp(struct compiler
*c
, expr_ty e
)
1483 basicblock
*end
, *next
;
1485 assert(e
->kind
== IfExp_kind
);
1486 end
= compiler_new_block(c
);
1489 next
= compiler_new_block(c
);
1492 VISIT(c
, expr
, e
->v
.IfExp
.test
);
1493 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, next
);
1494 VISIT(c
, expr
, e
->v
.IfExp
.body
);
1495 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1496 compiler_use_next_block(c
, next
);
1497 VISIT(c
, expr
, e
->v
.IfExp
.orelse
);
1498 compiler_use_next_block(c
, end
);
1503 compiler_lambda(struct compiler
*c
, expr_ty e
)
1506 static identifier name
;
1507 arguments_ty args
= e
->v
.Lambda
.args
;
1508 assert(e
->kind
== Lambda_kind
);
1511 name
= PyString_InternFromString("<lambda>");
1517 VISIT_SEQ(c
, expr
, args
->defaults
);
1518 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
1521 /* unpack nested arguments */
1522 compiler_arguments(c
, args
);
1524 /* Make None the first constant, so the lambda can't have a
1526 if (compiler_add_o(c
, c
->u
->u_consts
, Py_None
) < 0)
1529 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1530 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
1531 if (c
->u
->u_ste
->ste_generator
) {
1532 ADDOP_IN_SCOPE(c
, POP_TOP
);
1535 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1537 co
= assemble(c
, 1);
1538 compiler_exit_scope(c
);
1542 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1549 compiler_print(struct compiler
*c
, stmt_ty s
)
1554 assert(s
->kind
== Print_kind
);
1555 n
= asdl_seq_LEN(s
->v
.Print
.values
);
1557 if (s
->v
.Print
.dest
) {
1558 VISIT(c
, expr
, s
->v
.Print
.dest
);
1561 for (i
= 0; i
< n
; i
++) {
1562 expr_ty e
= (expr_ty
)asdl_seq_GET(s
->v
.Print
.values
, i
);
1567 ADDOP(c
, PRINT_ITEM_TO
);
1571 ADDOP(c
, PRINT_ITEM
);
1574 if (s
->v
.Print
.nl
) {
1576 ADDOP(c
, PRINT_NEWLINE_TO
)
1578 ADDOP(c
, PRINT_NEWLINE
)
1586 compiler_if(struct compiler
*c
, stmt_ty s
)
1588 basicblock
*end
, *next
;
1590 assert(s
->kind
== If_kind
);
1591 end
= compiler_new_block(c
);
1595 constant
= expr_constant(s
->v
.If
.test
);
1596 /* constant = 0: "if 0"
1597 * constant = 1: "if 1", "if 2", ...
1598 * constant = -1: rest */
1599 if (constant
== 0) {
1601 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1602 } else if (constant
== 1) {
1603 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1605 if (s
->v
.If
.orelse
) {
1606 next
= compiler_new_block(c
);
1612 VISIT(c
, expr
, s
->v
.If
.test
);
1613 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, next
);
1614 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1615 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1616 if (s
->v
.If
.orelse
) {
1617 compiler_use_next_block(c
, next
);
1618 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1621 compiler_use_next_block(c
, end
);
1626 compiler_for(struct compiler
*c
, stmt_ty s
)
1628 basicblock
*start
, *cleanup
, *end
;
1630 start
= compiler_new_block(c
);
1631 cleanup
= compiler_new_block(c
);
1632 end
= compiler_new_block(c
);
1633 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
1635 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1636 if (!compiler_push_fblock(c
, LOOP
, start
))
1638 VISIT(c
, expr
, s
->v
.For
.iter
);
1640 compiler_use_next_block(c
, start
);
1641 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
1642 VISIT(c
, expr
, s
->v
.For
.target
);
1643 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
1644 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
1645 compiler_use_next_block(c
, cleanup
);
1646 ADDOP(c
, POP_BLOCK
);
1647 compiler_pop_fblock(c
, LOOP
, start
);
1648 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
1649 compiler_use_next_block(c
, end
);
1654 compiler_while(struct compiler
*c
, stmt_ty s
)
1656 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
1657 int constant
= expr_constant(s
->v
.While
.test
);
1659 if (constant
== 0) {
1660 if (s
->v
.While
.orelse
)
1661 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1664 loop
= compiler_new_block(c
);
1665 end
= compiler_new_block(c
);
1666 if (constant
== -1) {
1667 anchor
= compiler_new_block(c
);
1671 if (loop
== NULL
|| end
== NULL
)
1673 if (s
->v
.While
.orelse
) {
1674 orelse
= compiler_new_block(c
);
1681 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1682 compiler_use_next_block(c
, loop
);
1683 if (!compiler_push_fblock(c
, LOOP
, loop
))
1685 if (constant
== -1) {
1686 VISIT(c
, expr
, s
->v
.While
.test
);
1687 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, anchor
);
1689 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
1690 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
1692 /* XXX should the two POP instructions be in a separate block
1693 if there is no else clause ?
1696 if (constant
== -1) {
1697 compiler_use_next_block(c
, anchor
);
1698 ADDOP(c
, POP_BLOCK
);
1700 compiler_pop_fblock(c
, LOOP
, loop
);
1701 if (orelse
!= NULL
) /* what if orelse is just pass? */
1702 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1703 compiler_use_next_block(c
, end
);
1709 compiler_continue(struct compiler
*c
)
1711 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
1712 static const char IN_FINALLY_ERROR_MSG
[] =
1713 "'continue' not supported inside 'finally' clause";
1716 if (!c
->u
->u_nfblocks
)
1717 return compiler_error(c
, LOOP_ERROR_MSG
);
1718 i
= c
->u
->u_nfblocks
- 1;
1719 switch (c
->u
->u_fblock
[i
].fb_type
) {
1721 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
1725 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
) {
1726 /* Prevent continue anywhere under a finally
1727 even if hidden in a sub-try or except. */
1728 if (c
->u
->u_fblock
[i
].fb_type
== FINALLY_END
)
1729 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1732 return compiler_error(c
, LOOP_ERROR_MSG
);
1733 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
1736 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1742 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1748 L: <code for finalbody>
1751 The special instructions use the block stack. Each block
1752 stack entry contains the instruction that created it (here
1753 SETUP_FINALLY), the level of the value stack at the time the
1754 block stack entry was created, and a label (here L).
1757 Pushes the current value stack level and the label
1758 onto the block stack.
1760 Pops en entry from the block stack, and pops the value
1761 stack until its level is the same as indicated on the
1762 block stack. (The label is ignored.)
1764 Pops a variable number of entries from the *value* stack
1765 and re-raises the exception they specify. The number of
1766 entries popped depends on the (pseudo) exception type.
1768 The block stack is unwound when an exception is raised:
1769 when a SETUP_FINALLY entry is found, the exception is pushed
1770 onto the value stack (and the exception condition is cleared),
1771 and the interpreter jumps to the label gotten from the block
1776 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
1778 basicblock
*body
, *end
;
1779 body
= compiler_new_block(c
);
1780 end
= compiler_new_block(c
);
1781 if (body
== NULL
|| end
== NULL
)
1784 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
1785 compiler_use_next_block(c
, body
);
1786 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
1788 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
1789 ADDOP(c
, POP_BLOCK
);
1790 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
1792 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1793 compiler_use_next_block(c
, end
);
1794 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
1796 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
1797 ADDOP(c
, END_FINALLY
);
1798 compiler_pop_fblock(c
, FINALLY_END
, end
);
1804 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1805 (The contents of the value stack is shown in [], with the top
1806 at the right; 'tb' is trace-back info, 'val' the exception's
1807 associated value, and 'exc' the exception.)
1809 Value stack Label Instruction Argument
1815 [tb, val, exc] L1: DUP )
1816 [tb, val, exc, exc] <evaluate E1> )
1817 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1818 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1820 [tb, val] <assign to V1> (or POP if no V1)
1825 [tb, val, exc] L2: DUP
1826 .............................etc.......................
1828 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1830 [] L0: <next statement>
1832 Of course, parts are not generated if Vi or Ei is not present.
1835 compiler_try_except(struct compiler
*c
, stmt_ty s
)
1837 basicblock
*body
, *orelse
, *except
, *end
;
1840 body
= compiler_new_block(c
);
1841 except
= compiler_new_block(c
);
1842 orelse
= compiler_new_block(c
);
1843 end
= compiler_new_block(c
);
1844 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
1846 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
1847 compiler_use_next_block(c
, body
);
1848 if (!compiler_push_fblock(c
, EXCEPT
, body
))
1850 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
1851 ADDOP(c
, POP_BLOCK
);
1852 compiler_pop_fblock(c
, EXCEPT
, body
);
1853 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
1854 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
1855 compiler_use_next_block(c
, except
);
1856 for (i
= 0; i
< n
; i
++) {
1857 excepthandler_ty handler
= (excepthandler_ty
)asdl_seq_GET(
1858 s
->v
.TryExcept
.handlers
, i
);
1859 if (!handler
->v
.ExceptHandler
.type
&& i
< n
-1)
1860 return compiler_error(c
, "default 'except:' must be last");
1861 c
->u
->u_lineno_set
= false;
1862 c
->u
->u_lineno
= handler
->lineno
;
1863 except
= compiler_new_block(c
);
1866 if (handler
->v
.ExceptHandler
.type
) {
1868 VISIT(c
, expr
, handler
->v
.ExceptHandler
.type
);
1869 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
1870 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, except
);
1873 if (handler
->v
.ExceptHandler
.name
) {
1874 VISIT(c
, expr
, handler
->v
.ExceptHandler
.name
);
1880 VISIT_SEQ(c
, stmt
, handler
->v
.ExceptHandler
.body
);
1881 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1882 compiler_use_next_block(c
, except
);
1884 ADDOP(c
, END_FINALLY
);
1885 compiler_use_next_block(c
, orelse
);
1886 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
1887 compiler_use_next_block(c
, end
);
1892 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
1894 /* The IMPORT_NAME opcode was already generated. This function
1895 merely needs to bind the result to a name.
1897 If there is a dot in name, we need to split it and emit a
1898 LOAD_ATTR for each name.
1900 const char *src
= PyString_AS_STRING(name
);
1901 const char *dot
= strchr(src
, '.');
1903 /* Consume the base module name to get the first attribute */
1906 /* NB src is only defined when dot != NULL */
1908 dot
= strchr(src
, '.');
1909 attr
= PyString_FromStringAndSize(src
,
1910 dot
? dot
- src
: strlen(src
));
1913 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
1918 return compiler_nameop(c
, asname
, Store
);
1922 compiler_import(struct compiler
*c
, stmt_ty s
)
1924 /* The Import node stores a module name like a.b.c as a single
1925 string. This is convenient for all cases except
1927 where we need to parse that string to extract the individual
1929 XXX Perhaps change the representation to make this case simpler?
1931 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
1933 for (i
= 0; i
< n
; i
++) {
1934 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.Import
.names
, i
);
1938 if (c
->c_flags
&& (c
->c_flags
->cf_flags
& CO_FUTURE_ABSOLUTE_IMPORT
))
1939 level
= PyInt_FromLong(0);
1941 level
= PyInt_FromLong(-1);
1946 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
1948 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1949 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
1951 if (alias
->asname
) {
1952 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
1957 identifier tmp
= alias
->name
;
1958 const char *base
= PyString_AS_STRING(alias
->name
);
1959 char *dot
= strchr(base
, '.');
1961 tmp
= PyString_FromStringAndSize(base
,
1963 r
= compiler_nameop(c
, tmp
, Store
);
1975 compiler_from_import(struct compiler
*c
, stmt_ty s
)
1977 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
1979 PyObject
*names
= PyTuple_New(n
);
1981 static PyObject
*empty_string
;
1983 if (!empty_string
) {
1984 empty_string
= PyString_FromString("");
1992 if (s
->v
.ImportFrom
.level
== 0 && c
->c_flags
&&
1993 !(c
->c_flags
->cf_flags
& CO_FUTURE_ABSOLUTE_IMPORT
))
1994 level
= PyInt_FromLong(-1);
1996 level
= PyInt_FromLong(s
->v
.ImportFrom
.level
);
2003 /* build up the names */
2004 for (i
= 0; i
< n
; i
++) {
2005 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2006 Py_INCREF(alias
->name
);
2007 PyTuple_SET_ITEM(names
, i
, alias
->name
);
2010 if (s
->lineno
> c
->c_future
->ff_lineno
&& s
->v
.ImportFrom
.module
&&
2011 !strcmp(PyString_AS_STRING(s
->v
.ImportFrom
.module
), "__future__")) {
2014 return compiler_error(c
, "from __future__ imports must occur "
2015 "at the beginning of the file");
2018 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2020 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
2022 if (s
->v
.ImportFrom
.module
) {
2023 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
2026 ADDOP_NAME(c
, IMPORT_NAME
, empty_string
, names
);
2028 for (i
= 0; i
< n
; i
++) {
2029 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2030 identifier store_name
;
2032 if (i
== 0 && *PyString_AS_STRING(alias
->name
) == '*') {
2034 ADDOP(c
, IMPORT_STAR
);
2038 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
2039 store_name
= alias
->name
;
2041 store_name
= alias
->asname
;
2043 if (!compiler_nameop(c
, store_name
, Store
)) {
2048 /* remove imported module */
2054 compiler_assert(struct compiler
*c
, stmt_ty s
)
2056 static PyObject
*assertion_error
= NULL
;
2059 if (Py_OptimizeFlag
)
2061 if (assertion_error
== NULL
) {
2062 assertion_error
= PyString_InternFromString("AssertionError");
2063 if (assertion_error
== NULL
)
2066 if (s
->v
.Assert
.test
->kind
== Tuple_kind
&&
2067 asdl_seq_LEN(s
->v
.Assert
.test
->v
.Tuple
.elts
) > 0) {
2069 "assertion is always true, perhaps remove parentheses?";
2070 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
,
2071 c
->u
->u_lineno
, NULL
, NULL
) == -1)
2074 VISIT(c
, expr
, s
->v
.Assert
.test
);
2075 end
= compiler_new_block(c
);
2078 ADDOP_JABS(c
, POP_JUMP_IF_TRUE
, end
);
2079 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
2080 if (s
->v
.Assert
.msg
) {
2081 VISIT(c
, expr
, s
->v
.Assert
.msg
);
2082 ADDOP_I(c
, RAISE_VARARGS
, 2);
2085 ADDOP_I(c
, RAISE_VARARGS
, 1);
2087 compiler_use_next_block(c
, end
);
2092 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2096 /* Always assign a lineno to the next instruction for a stmt. */
2097 c
->u
->u_lineno
= s
->lineno
;
2098 c
->u
->u_lineno_set
= false;
2101 case FunctionDef_kind
:
2102 return compiler_function(c
, s
);
2104 return compiler_class(c
, s
);
2106 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2107 return compiler_error(c
, "'return' outside function");
2108 if (s
->v
.Return
.value
) {
2109 VISIT(c
, expr
, s
->v
.Return
.value
);
2112 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2113 ADDOP(c
, RETURN_VALUE
);
2116 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2119 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2120 VISIT(c
, expr
, s
->v
.Assign
.value
);
2121 for (i
= 0; i
< n
; i
++) {
2125 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2128 case AugAssign_kind
:
2129 return compiler_augassign(c
, s
);
2131 return compiler_print(c
, s
);
2133 return compiler_for(c
, s
);
2135 return compiler_while(c
, s
);
2137 return compiler_if(c
, s
);
2140 if (s
->v
.Raise
.type
) {
2141 VISIT(c
, expr
, s
->v
.Raise
.type
);
2143 if (s
->v
.Raise
.inst
) {
2144 VISIT(c
, expr
, s
->v
.Raise
.inst
);
2146 if (s
->v
.Raise
.tback
) {
2147 VISIT(c
, expr
, s
->v
.Raise
.tback
);
2152 ADDOP_I(c
, RAISE_VARARGS
, n
);
2154 case TryExcept_kind
:
2155 return compiler_try_except(c
, s
);
2156 case TryFinally_kind
:
2157 return compiler_try_finally(c
, s
);
2159 return compiler_assert(c
, s
);
2161 return compiler_import(c
, s
);
2162 case ImportFrom_kind
:
2163 return compiler_from_import(c
, s
);
2165 VISIT(c
, expr
, s
->v
.Exec
.body
);
2166 if (s
->v
.Exec
.globals
) {
2167 VISIT(c
, expr
, s
->v
.Exec
.globals
);
2168 if (s
->v
.Exec
.locals
) {
2169 VISIT(c
, expr
, s
->v
.Exec
.locals
);
2174 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2177 ADDOP(c
, EXEC_STMT
);
2182 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2183 VISIT(c
, expr
, s
->v
.Expr
.value
);
2184 ADDOP(c
, PRINT_EXPR
);
2186 else if (s
->v
.Expr
.value
->kind
!= Str_kind
&&
2187 s
->v
.Expr
.value
->kind
!= Num_kind
) {
2188 VISIT(c
, expr
, s
->v
.Expr
.value
);
2195 if (!compiler_in_loop(c
))
2196 return compiler_error(c
, "'break' outside loop");
2197 ADDOP(c
, BREAK_LOOP
);
2200 return compiler_continue(c
);
2202 return compiler_with(c
, s
);
2208 unaryop(unaryop_ty op
)
2212 return UNARY_INVERT
;
2216 return UNARY_POSITIVE
;
2218 return UNARY_NEGATIVE
;
2220 PyErr_Format(PyExc_SystemError
,
2221 "unary op %d should not be possible", op
);
2227 binop(struct compiler
*c
, operator_ty op
)
2233 return BINARY_SUBTRACT
;
2235 return BINARY_MULTIPLY
;
2237 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2238 return BINARY_TRUE_DIVIDE
;
2240 return BINARY_DIVIDE
;
2242 return BINARY_MODULO
;
2244 return BINARY_POWER
;
2246 return BINARY_LSHIFT
;
2248 return BINARY_RSHIFT
;
2256 return BINARY_FLOOR_DIVIDE
;
2258 PyErr_Format(PyExc_SystemError
,
2259 "binary op %d should not be possible", op
);
2283 return PyCmp_IS_NOT
;
2287 return PyCmp_NOT_IN
;
2294 inplace_binop(struct compiler
*c
, operator_ty op
)
2300 return INPLACE_SUBTRACT
;
2302 return INPLACE_MULTIPLY
;
2304 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2305 return INPLACE_TRUE_DIVIDE
;
2307 return INPLACE_DIVIDE
;
2309 return INPLACE_MODULO
;
2311 return INPLACE_POWER
;
2313 return INPLACE_LSHIFT
;
2315 return INPLACE_RSHIFT
;
2323 return INPLACE_FLOOR_DIVIDE
;
2325 PyErr_Format(PyExc_SystemError
,
2326 "inplace binary op %d should not be possible", op
);
2332 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2335 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2337 PyObject
*dict
= c
->u
->u_names
;
2339 /* XXX AugStore isn't used anywhere! */
2341 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2347 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2350 dict
= c
->u
->u_freevars
;
2354 dict
= c
->u
->u_cellvars
;
2358 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2361 case GLOBAL_IMPLICIT
:
2362 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2363 !c
->u
->u_ste
->ste_unoptimized
)
2366 case GLOBAL_EXPLICIT
:
2370 /* scope can be 0 */
2374 /* XXX Leave assert here, but handle __doc__ and the like better */
2375 assert(scope
|| PyString_AS_STRING(name
)[0] == '_');
2380 case Load
: op
= LOAD_DEREF
; break;
2381 case Store
: op
= STORE_DEREF
; break;
2386 PyErr_Format(PyExc_SyntaxError
,
2387 "can not delete variable '%s' referenced "
2389 PyString_AS_STRING(name
));
2394 PyErr_SetString(PyExc_SystemError
,
2395 "param invalid for deref variable");
2401 case Load
: op
= LOAD_FAST
; break;
2402 case Store
: op
= STORE_FAST
; break;
2403 case Del
: op
= DELETE_FAST
; break;
2409 PyErr_SetString(PyExc_SystemError
,
2410 "param invalid for local variable");
2413 ADDOP_O(c
, op
, mangled
, varnames
);
2418 case Load
: op
= LOAD_GLOBAL
; break;
2419 case Store
: op
= STORE_GLOBAL
; break;
2420 case Del
: op
= DELETE_GLOBAL
; break;
2426 PyErr_SetString(PyExc_SystemError
,
2427 "param invalid for global variable");
2433 case Load
: op
= LOAD_NAME
; break;
2434 case Store
: op
= STORE_NAME
; break;
2435 case Del
: op
= DELETE_NAME
; break;
2441 PyErr_SetString(PyExc_SystemError
,
2442 "param invalid for name variable");
2449 arg
= compiler_add_o(c
, dict
, mangled
);
2453 return compiler_addop_i(c
, op
, arg
);
2457 compiler_boolop(struct compiler
*c
, expr_ty e
)
2463 assert(e
->kind
== BoolOp_kind
);
2464 if (e
->v
.BoolOp
.op
== And
)
2465 jumpi
= JUMP_IF_FALSE_OR_POP
;
2467 jumpi
= JUMP_IF_TRUE_OR_POP
;
2468 end
= compiler_new_block(c
);
2471 s
= e
->v
.BoolOp
.values
;
2472 n
= asdl_seq_LEN(s
) - 1;
2474 for (i
= 0; i
< n
; ++i
) {
2475 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, i
));
2476 ADDOP_JABS(c
, jumpi
, end
);
2478 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, n
));
2479 compiler_use_next_block(c
, end
);
2484 compiler_list(struct compiler
*c
, expr_ty e
)
2486 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
2487 if (e
->v
.List
.ctx
== Store
) {
2488 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2490 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
2491 if (e
->v
.List
.ctx
== Load
) {
2492 ADDOP_I(c
, BUILD_LIST
, n
);
2498 compiler_tuple(struct compiler
*c
, expr_ty e
)
2500 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
2501 if (e
->v
.Tuple
.ctx
== Store
) {
2502 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2504 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
2505 if (e
->v
.Tuple
.ctx
== Load
) {
2506 ADDOP_I(c
, BUILD_TUPLE
, n
);
2512 compiler_compare(struct compiler
*c
, expr_ty e
)
2515 basicblock
*cleanup
= NULL
;
2517 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2518 VISIT(c
, expr
, e
->v
.Compare
.left
);
2519 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
2522 cleanup
= compiler_new_block(c
);
2523 if (cleanup
== NULL
)
2526 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
2528 for (i
= 1; i
< n
; i
++) {
2530 ADDOP(c
, ROT_THREE
);
2531 ADDOP_I(c
, COMPARE_OP
,
2532 cmpop((cmpop_ty
)(asdl_seq_GET(
2533 e
->v
.Compare
.ops
, i
- 1))));
2534 ADDOP_JABS(c
, JUMP_IF_FALSE_OR_POP
, cleanup
);
2538 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
2540 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
2541 ADDOP_I(c
, COMPARE_OP
,
2542 cmpop((cmpop_ty
)(asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1))));
2544 basicblock
*end
= compiler_new_block(c
);
2547 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2548 compiler_use_next_block(c
, cleanup
);
2551 compiler_use_next_block(c
, end
);
2557 compiler_call(struct compiler
*c
, expr_ty e
)
2561 VISIT(c
, expr
, e
->v
.Call
.func
);
2562 n
= asdl_seq_LEN(e
->v
.Call
.args
);
2563 VISIT_SEQ(c
, expr
, e
->v
.Call
.args
);
2564 if (e
->v
.Call
.keywords
) {
2565 VISIT_SEQ(c
, keyword
, e
->v
.Call
.keywords
);
2566 n
|= asdl_seq_LEN(e
->v
.Call
.keywords
) << 8;
2568 if (e
->v
.Call
.starargs
) {
2569 VISIT(c
, expr
, e
->v
.Call
.starargs
);
2572 if (e
->v
.Call
.kwargs
) {
2573 VISIT(c
, expr
, e
->v
.Call
.kwargs
);
2578 ADDOP_I(c
, CALL_FUNCTION
, n
);
2581 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
2584 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
2587 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
2594 compiler_listcomp_generator(struct compiler
*c
, asdl_seq
*generators
,
2595 int gen_index
, expr_ty elt
)
2597 /* generate code for the iterator, then each of the ifs,
2598 and then write to the element */
2601 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
2604 start
= compiler_new_block(c
);
2605 skip
= compiler_new_block(c
);
2606 if_cleanup
= compiler_new_block(c
);
2607 anchor
= compiler_new_block(c
);
2609 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2613 l
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2614 VISIT(c
, expr
, l
->iter
);
2616 compiler_use_next_block(c
, start
);
2617 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2619 VISIT(c
, expr
, l
->target
);
2621 /* XXX this needs to be cleaned up...a lot! */
2622 n
= asdl_seq_LEN(l
->ifs
);
2623 for (i
= 0; i
< n
; i
++) {
2624 expr_ty e
= (expr_ty
)asdl_seq_GET(l
->ifs
, i
);
2626 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, if_cleanup
);
2630 if (++gen_index
< asdl_seq_LEN(generators
))
2631 if (!compiler_listcomp_generator(c
, generators
, gen_index
, elt
))
2634 /* only append after the last for generator */
2635 if (gen_index
>= asdl_seq_LEN(generators
)) {
2636 VISIT(c
, expr
, elt
);
2637 ADDOP_I(c
, LIST_APPEND
, gen_index
+1);
2639 compiler_use_next_block(c
, skip
);
2641 compiler_use_next_block(c
, if_cleanup
);
2642 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2643 compiler_use_next_block(c
, anchor
);
2649 compiler_listcomp(struct compiler
*c
, expr_ty e
)
2651 assert(e
->kind
== ListComp_kind
);
2652 ADDOP_I(c
, BUILD_LIST
, 0);
2653 return compiler_listcomp_generator(c
, e
->v
.ListComp
.generators
, 0,
2657 /* Dict and set comprehensions and generator expressions work by creating a
2658 nested function to perform the actual iteration. This means that the
2659 iteration variables don't leak into the current scope.
2660 The defined function is called immediately following its definition, with the
2661 result of that call being the result of the expression.
2662 The LC/SC version returns the populated container, while the GE version is
2663 flagged in symtable.c as a generator, so it returns the generator object
2664 when the function is called.
2665 This code *knows* that the loop cannot contain break, continue, or return,
2666 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2669 - iterate over the generator sequence instead of using recursion
2673 compiler_comprehension_generator(struct compiler
*c
,
2674 asdl_seq
*generators
, int gen_index
,
2675 expr_ty elt
, expr_ty val
, int type
)
2677 /* generate code for the iterator, then each of the ifs,
2678 and then write to the element */
2680 comprehension_ty gen
;
2681 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
2684 start
= compiler_new_block(c
);
2685 skip
= compiler_new_block(c
);
2686 if_cleanup
= compiler_new_block(c
);
2687 anchor
= compiler_new_block(c
);
2689 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2693 gen
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2695 if (gen_index
== 0) {
2696 /* Receive outermost iter as an implicit argument */
2697 c
->u
->u_argcount
= 1;
2698 ADDOP_I(c
, LOAD_FAST
, 0);
2701 /* Sub-iter - calculate on the fly */
2702 VISIT(c
, expr
, gen
->iter
);
2705 compiler_use_next_block(c
, start
);
2706 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2708 VISIT(c
, expr
, gen
->target
);
2710 /* XXX this needs to be cleaned up...a lot! */
2711 n
= asdl_seq_LEN(gen
->ifs
);
2712 for (i
= 0; i
< n
; i
++) {
2713 expr_ty e
= (expr_ty
)asdl_seq_GET(gen
->ifs
, i
);
2715 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, if_cleanup
);
2719 if (++gen_index
< asdl_seq_LEN(generators
))
2720 if (!compiler_comprehension_generator(c
,
2721 generators
, gen_index
,
2725 /* only append after the last for generator */
2726 if (gen_index
>= asdl_seq_LEN(generators
)) {
2727 /* comprehension specific code */
2730 VISIT(c
, expr
, elt
);
2731 ADDOP(c
, YIELD_VALUE
);
2735 VISIT(c
, expr
, elt
);
2736 ADDOP_I(c
, SET_ADD
, gen_index
+ 1);
2739 /* With 'd[k] = v', v is evaluated before k, so we do
2741 VISIT(c
, expr
, val
);
2742 VISIT(c
, expr
, elt
);
2743 ADDOP_I(c
, MAP_ADD
, gen_index
+ 1);
2749 compiler_use_next_block(c
, skip
);
2751 compiler_use_next_block(c
, if_cleanup
);
2752 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2753 compiler_use_next_block(c
, anchor
);
2759 compiler_comprehension(struct compiler
*c
, expr_ty e
, int type
, identifier name
,
2760 asdl_seq
*generators
, expr_ty elt
, expr_ty val
)
2762 PyCodeObject
*co
= NULL
;
2763 expr_ty outermost_iter
;
2765 outermost_iter
= ((comprehension_ty
)
2766 asdl_seq_GET(generators
, 0))->iter
;
2768 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2771 if (type
!= COMP_GENEXP
) {
2781 PyErr_Format(PyExc_SystemError
,
2782 "unknown comprehension type %d", type
);
2783 goto error_in_scope
;
2789 if (!compiler_comprehension_generator(c
, generators
, 0, elt
,
2791 goto error_in_scope
;
2793 if (type
!= COMP_GENEXP
) {
2794 ADDOP(c
, RETURN_VALUE
);
2797 co
= assemble(c
, 1);
2798 compiler_exit_scope(c
);
2802 if (!compiler_make_closure(c
, co
, 0))
2806 VISIT(c
, expr
, outermost_iter
);
2808 ADDOP_I(c
, CALL_FUNCTION
, 1);
2811 compiler_exit_scope(c
);
2818 compiler_genexp(struct compiler
*c
, expr_ty e
)
2820 static identifier name
;
2822 name
= PyString_FromString("<genexpr>");
2826 assert(e
->kind
== GeneratorExp_kind
);
2827 return compiler_comprehension(c
, e
, COMP_GENEXP
, name
,
2828 e
->v
.GeneratorExp
.generators
,
2829 e
->v
.GeneratorExp
.elt
, NULL
);
2833 compiler_setcomp(struct compiler
*c
, expr_ty e
)
2835 static identifier name
;
2837 name
= PyString_FromString("<setcomp>");
2841 assert(e
->kind
== SetComp_kind
);
2842 return compiler_comprehension(c
, e
, COMP_SETCOMP
, name
,
2843 e
->v
.SetComp
.generators
,
2844 e
->v
.SetComp
.elt
, NULL
);
2848 compiler_dictcomp(struct compiler
*c
, expr_ty e
)
2850 static identifier name
;
2852 name
= PyString_FromString("<dictcomp>");
2856 assert(e
->kind
== DictComp_kind
);
2857 return compiler_comprehension(c
, e
, COMP_DICTCOMP
, name
,
2858 e
->v
.DictComp
.generators
,
2859 e
->v
.DictComp
.key
, e
->v
.DictComp
.value
);
2863 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
2865 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
2866 VISIT(c
, expr
, k
->value
);
2870 /* Test whether expression is constant. For constants, report
2871 whether they are true or false.
2873 Return values: 1 for true, 0 for false, -1 for non-constant.
2877 expr_constant(expr_ty e
)
2881 return PyObject_IsTrue(e
->v
.Num
.n
);
2883 return PyObject_IsTrue(e
->v
.Str
.s
);
2885 /* __debug__ is not assignable, so we can optimize
2886 * it away in if and while statements */
2887 if (strcmp(PyString_AS_STRING(e
->v
.Name
.id
),
2889 return ! Py_OptimizeFlag
;
2897 Implements the with statement from PEP 343.
2899 The semantics outlined in that PEP are as follows:
2904 It is implemented roughly as:
2907 exit = context.__exit__ # not calling it
2908 value = context.__enter__()
2910 VAR = value # if VAR present in the syntax
2913 if an exception was raised:
2914 exc = copy of (exception, instance, traceback)
2916 exc = (None, None, None)
2920 compiler_with(struct compiler
*c
, stmt_ty s
)
2922 basicblock
*block
, *finally
;
2924 assert(s
->kind
== With_kind
);
2926 block
= compiler_new_block(c
);
2927 finally
= compiler_new_block(c
);
2928 if (!block
|| !finally
)
2932 VISIT(c
, expr
, s
->v
.With
.context_expr
);
2933 ADDOP_JREL(c
, SETUP_WITH
, finally
);
2935 /* SETUP_WITH pushes a finally block. */
2936 compiler_use_next_block(c
, block
);
2937 /* Note that the block is actually called SETUP_WITH in ceval.c, but
2938 functions the same as SETUP_FINALLY except that exceptions are
2940 if (!compiler_push_fblock(c
, FINALLY_TRY
, block
)) {
2944 if (s
->v
.With
.optional_vars
) {
2945 VISIT(c
, expr
, s
->v
.With
.optional_vars
);
2948 /* Discard result from context.__enter__() */
2953 VISIT_SEQ(c
, stmt
, s
->v
.With
.body
);
2955 /* End of try block; start the finally block */
2956 ADDOP(c
, POP_BLOCK
);
2957 compiler_pop_fblock(c
, FINALLY_TRY
, block
);
2959 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2960 compiler_use_next_block(c
, finally
);
2961 if (!compiler_push_fblock(c
, FINALLY_END
, finally
))
2964 /* Finally block starts; context.__exit__ is on the stack under
2965 the exception or return information. Just issue our magic
2967 ADDOP(c
, WITH_CLEANUP
);
2969 /* Finally block ends. */
2970 ADDOP(c
, END_FINALLY
);
2971 compiler_pop_fblock(c
, FINALLY_END
, finally
);
2976 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
2980 /* If expr e has a different line number than the last expr/stmt,
2981 set a new line number for the next instruction.
2983 if (e
->lineno
> c
->u
->u_lineno
) {
2984 c
->u
->u_lineno
= e
->lineno
;
2985 c
->u
->u_lineno_set
= false;
2989 return compiler_boolop(c
, e
);
2991 VISIT(c
, expr
, e
->v
.BinOp
.left
);
2992 VISIT(c
, expr
, e
->v
.BinOp
.right
);
2993 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
2996 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
2997 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
3000 return compiler_lambda(c
, e
);
3002 return compiler_ifexp(c
, e
);
3004 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
3005 ADDOP_I(c
, BUILD_MAP
, (n
>0xFFFF ? 0xFFFF : n
));
3006 for (i
= 0; i
< n
; i
++) {
3008 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.values
, i
));
3010 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.keys
, i
));
3011 ADDOP(c
, STORE_MAP
);
3015 n
= asdl_seq_LEN(e
->v
.Set
.elts
);
3016 VISIT_SEQ(c
, expr
, e
->v
.Set
.elts
);
3017 ADDOP_I(c
, BUILD_SET
, n
);
3020 return compiler_listcomp(c
, e
);
3022 return compiler_setcomp(c
, e
);
3024 return compiler_dictcomp(c
, e
);
3025 case GeneratorExp_kind
:
3026 return compiler_genexp(c
, e
);
3028 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
3029 return compiler_error(c
, "'yield' outside function");
3030 if (e
->v
.Yield
.value
) {
3031 VISIT(c
, expr
, e
->v
.Yield
.value
);
3034 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3036 ADDOP(c
, YIELD_VALUE
);
3039 return compiler_compare(c
, e
);
3041 return compiler_call(c
, e
);
3043 VISIT(c
, expr
, e
->v
.Repr
.value
);
3044 ADDOP(c
, UNARY_CONVERT
);
3047 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
3050 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
3052 /* The following exprs can be assignment targets. */
3053 case Attribute_kind
:
3054 if (e
->v
.Attribute
.ctx
!= AugStore
)
3055 VISIT(c
, expr
, e
->v
.Attribute
.value
);
3056 switch (e
->v
.Attribute
.ctx
) {
3059 /* Fall through to load */
3061 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
3065 /* Fall through to save */
3067 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
3070 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
3074 PyErr_SetString(PyExc_SystemError
,
3075 "param invalid in attribute expression");
3079 case Subscript_kind
:
3080 switch (e
->v
.Subscript
.ctx
) {
3082 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3083 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
3086 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3087 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
3090 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
3093 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3094 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
3097 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3098 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
3102 PyErr_SetString(PyExc_SystemError
,
3103 "param invalid in subscript expression");
3108 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3109 /* child nodes of List and Tuple will have expr_context set */
3111 return compiler_list(c
, e
);
3113 return compiler_tuple(c
, e
);
3119 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3121 expr_ty e
= s
->v
.AugAssign
.target
;
3124 assert(s
->kind
== AugAssign_kind
);
3127 case Attribute_kind
:
3128 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3129 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3132 VISIT(c
, expr
, auge
);
3133 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3134 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3135 auge
->v
.Attribute
.ctx
= AugStore
;
3136 VISIT(c
, expr
, auge
);
3138 case Subscript_kind
:
3139 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3140 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3143 VISIT(c
, expr
, auge
);
3144 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3145 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3146 auge
->v
.Subscript
.ctx
= AugStore
;
3147 VISIT(c
, expr
, auge
);
3150 if (!compiler_nameop(c
, e
->v
.Name
.id
, Load
))
3152 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3153 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3154 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3156 PyErr_Format(PyExc_SystemError
,
3157 "invalid node type (%d) for augmented assignment",
3165 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3167 struct fblockinfo
*f
;
3168 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
) {
3169 PyErr_SetString(PyExc_SystemError
,
3170 "too many statically nested blocks");
3173 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3180 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3182 struct compiler_unit
*u
= c
->u
;
3183 assert(u
->u_nfblocks
> 0);
3185 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3186 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3190 compiler_in_loop(struct compiler
*c
) {
3192 struct compiler_unit
*u
= c
->u
;
3193 for (i
= 0; i
< u
->u_nfblocks
; ++i
) {
3194 if (u
->u_fblock
[i
].fb_type
== LOOP
)
3199 /* Raises a SyntaxError and returns 0.
3200 If something goes wrong, a different exception may be raised.
3204 compiler_error(struct compiler
*c
, const char *errstr
)
3207 PyObject
*u
= NULL
, *v
= NULL
;
3209 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3214 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3218 v
= Py_BuildValue("(zO)", errstr
, u
);
3221 PyErr_SetObject(PyExc_SyntaxError
, v
);
3230 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3231 expr_context_ty ctx
)
3235 /* XXX this code is duplicated */
3237 case AugLoad
: /* fall through to Load */
3238 case Load
: op
= BINARY_SUBSCR
; break;
3239 case AugStore
:/* fall through to Store */
3240 case Store
: op
= STORE_SUBSCR
; break;
3241 case Del
: op
= DELETE_SUBSCR
; break;
3243 PyErr_Format(PyExc_SystemError
,
3244 "invalid %s kind %d in subscript\n",
3248 if (ctx
== AugLoad
) {
3249 ADDOP_I(c
, DUP_TOPX
, 2);
3251 else if (ctx
== AugStore
) {
3252 ADDOP(c
, ROT_THREE
);
3259 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3262 assert(s
->kind
== Slice_kind
);
3264 /* only handles the cases where BUILD_SLICE is emitted */
3265 if (s
->v
.Slice
.lower
) {
3266 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3269 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3272 if (s
->v
.Slice
.upper
) {
3273 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3276 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3279 if (s
->v
.Slice
.step
) {
3281 VISIT(c
, expr
, s
->v
.Slice
.step
);
3283 ADDOP_I(c
, BUILD_SLICE
, n
);
3288 compiler_simple_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3290 int op
= 0, slice_offset
= 0, stack_count
= 0;
3292 assert(s
->v
.Slice
.step
== NULL
);
3293 if (s
->v
.Slice
.lower
) {
3296 if (ctx
!= AugStore
)
3297 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3299 if (s
->v
.Slice
.upper
) {
3302 if (ctx
!= AugStore
)
3303 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3306 if (ctx
== AugLoad
) {
3307 switch (stack_count
) {
3308 case 0: ADDOP(c
, DUP_TOP
); break;
3309 case 1: ADDOP_I(c
, DUP_TOPX
, 2); break;
3310 case 2: ADDOP_I(c
, DUP_TOPX
, 3); break;
3313 else if (ctx
== AugStore
) {
3314 switch (stack_count
) {
3315 case 0: ADDOP(c
, ROT_TWO
); break;
3316 case 1: ADDOP(c
, ROT_THREE
); break;
3317 case 2: ADDOP(c
, ROT_FOUR
); break;
3322 case AugLoad
: /* fall through to Load */
3323 case Load
: op
= SLICE
; break;
3324 case AugStore
:/* fall through to Store */
3325 case Store
: op
= STORE_SLICE
; break;
3326 case Del
: op
= DELETE_SLICE
; break;
3329 PyErr_SetString(PyExc_SystemError
,
3330 "param invalid in simple slice");
3334 ADDOP(c
, op
+ slice_offset
);
3339 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3340 expr_context_ty ctx
)
3344 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3347 return compiler_slice(c
, s
, ctx
);
3349 VISIT(c
, expr
, s
->v
.Index
.value
);
3353 PyErr_SetString(PyExc_SystemError
,
3354 "extended slice invalid in nested slice");
3361 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3363 char * kindname
= NULL
;
3367 if (ctx
!= AugStore
) {
3368 VISIT(c
, expr
, s
->v
.Index
.value
);
3372 kindname
= "ellipsis";
3373 if (ctx
!= AugStore
) {
3374 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3379 if (!s
->v
.Slice
.step
)
3380 return compiler_simple_slice(c
, s
, ctx
);
3381 if (ctx
!= AugStore
) {
3382 if (!compiler_slice(c
, s
, ctx
))
3387 kindname
= "extended slice";
3388 if (ctx
!= AugStore
) {
3389 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3390 for (i
= 0; i
< n
; i
++) {
3391 slice_ty sub
= (slice_ty
)asdl_seq_GET(
3392 s
->v
.ExtSlice
.dims
, i
);
3393 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3396 ADDOP_I(c
, BUILD_TUPLE
, n
);
3400 PyErr_Format(PyExc_SystemError
,
3401 "invalid subscript kind %d", s
->kind
);
3404 return compiler_handle_subscr(c
, kindname
, ctx
);
3408 /* End of the compiler section, beginning of the assembler section */
3410 /* do depth-first search of basic block graph, starting with block.
3411 post records the block indices in post-order.
3413 XXX must handle implicit jumps from one block to next
3417 PyObject
*a_bytecode
; /* string containing bytecode */
3418 int a_offset
; /* offset into bytecode */
3419 int a_nblocks
; /* number of reachable blocks */
3420 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
3421 PyObject
*a_lnotab
; /* string containing lnotab */
3422 int a_lnotab_off
; /* offset into lnotab */
3423 int a_lineno
; /* last lineno of emitted instruction */
3424 int a_lineno_off
; /* bytecode offset of last lineno */
3428 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3431 struct instr
*instr
= NULL
;
3436 if (b
->b_next
!= NULL
)
3437 dfs(c
, b
->b_next
, a
);
3438 for (i
= 0; i
< b
->b_iused
; i
++) {
3439 instr
= &b
->b_instr
[i
];
3440 if (instr
->i_jrel
|| instr
->i_jabs
)
3441 dfs(c
, instr
->i_target
, a
);
3443 a
->a_postorder
[a
->a_nblocks
++] = b
;
3447 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3449 int i
, target_depth
;
3450 struct instr
*instr
;
3451 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3454 b
->b_startdepth
= depth
;
3455 for (i
= 0; i
< b
->b_iused
; i
++) {
3456 instr
= &b
->b_instr
[i
];
3457 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3458 if (depth
> maxdepth
)
3460 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3461 if (instr
->i_jrel
|| instr
->i_jabs
) {
3462 target_depth
= depth
;
3463 if (instr
->i_opcode
== FOR_ITER
) {
3464 target_depth
= depth
-2;
3465 } else if (instr
->i_opcode
== SETUP_FINALLY
||
3466 instr
->i_opcode
== SETUP_EXCEPT
) {
3467 target_depth
= depth
+3;
3468 if (target_depth
> maxdepth
)
3469 maxdepth
= target_depth
;
3471 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3472 target_depth
, maxdepth
);
3473 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3474 instr
->i_opcode
== JUMP_FORWARD
) {
3475 goto out
; /* remaining code is dead */
3480 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3486 /* Find the flow path that needs the largest stack. We assume that
3487 * cycles in the flow graph have no net effect on the stack depth.
3490 stackdepth(struct compiler
*c
)
3492 basicblock
*b
, *entryblock
;
3494 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3496 b
->b_startdepth
= INT_MIN
;
3501 return stackdepth_walk(c
, entryblock
, 0, 0);
3505 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3507 memset(a
, 0, sizeof(struct assembler
));
3508 a
->a_lineno
= firstlineno
;
3509 a
->a_bytecode
= PyString_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3512 a
->a_lnotab
= PyString_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3515 if (nblocks
> PY_SIZE_MAX
/ sizeof(basicblock
*)) {
3519 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3520 sizeof(basicblock
*) * nblocks
);
3521 if (!a
->a_postorder
) {
3529 assemble_free(struct assembler
*a
)
3531 Py_XDECREF(a
->a_bytecode
);
3532 Py_XDECREF(a
->a_lnotab
);
3534 PyObject_Free(a
->a_postorder
);
3537 /* Return the size of a basic block in bytes. */
3540 instrsize(struct instr
*instr
)
3542 if (!instr
->i_hasarg
)
3543 return 1; /* 1 byte for the opcode*/
3544 if (instr
->i_oparg
> 0xffff)
3545 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3546 return 3; /* 1 (opcode) + 2 (oparg) */
3550 blocksize(basicblock
*b
)
3555 for (i
= 0; i
< b
->b_iused
; i
++)
3556 size
+= instrsize(&b
->b_instr
[i
]);
3560 /* Appends a pair to the end of the line number table, a_lnotab, representing
3561 the instruction's bytecode offset and line number. See
3562 Objects/lnotab_notes.txt for the description of the line number table. */
3565 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
3567 int d_bytecode
, d_lineno
;
3569 unsigned char *lnotab
;
3571 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
3572 d_lineno
= i
->i_lineno
- a
->a_lineno
;
3574 assert(d_bytecode
>= 0);
3575 assert(d_lineno
>= 0);
3577 if(d_bytecode
== 0 && d_lineno
== 0)
3580 if (d_bytecode
> 255) {
3581 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
3582 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3583 len
= PyString_GET_SIZE(a
->a_lnotab
);
3584 if (nbytes
>= len
) {
3585 if ((len
<= INT_MAX
/ 2) && (len
* 2 < nbytes
))
3587 else if (len
<= INT_MAX
/ 2)
3593 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3596 lnotab
= (unsigned char *)
3597 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3598 for (j
= 0; j
< ncodes
; j
++) {
3602 d_bytecode
-= ncodes
* 255;
3603 a
->a_lnotab_off
+= ncodes
* 2;
3605 assert(d_bytecode
<= 255);
3606 if (d_lineno
> 255) {
3607 int j
, nbytes
, ncodes
= d_lineno
/ 255;
3608 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3609 len
= PyString_GET_SIZE(a
->a_lnotab
);
3610 if (nbytes
>= len
) {
3611 if ((len
<= INT_MAX
/ 2) && len
* 2 < nbytes
)
3613 else if (len
<= INT_MAX
/ 2)
3619 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3622 lnotab
= (unsigned char *)
3623 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3624 *lnotab
++ = d_bytecode
;
3627 for (j
= 1; j
< ncodes
; j
++) {
3631 d_lineno
-= ncodes
* 255;
3632 a
->a_lnotab_off
+= ncodes
* 2;
3635 len
= PyString_GET_SIZE(a
->a_lnotab
);
3636 if (a
->a_lnotab_off
+ 2 >= len
) {
3637 if (_PyString_Resize(&a
->a_lnotab
, len
* 2) < 0)
3640 lnotab
= (unsigned char *)
3641 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3643 a
->a_lnotab_off
+= 2;
3645 *lnotab
++ = d_bytecode
;
3646 *lnotab
++ = d_lineno
;
3648 else { /* First line of a block; def stmt, etc. */
3650 *lnotab
++ = d_lineno
;
3652 a
->a_lineno
= i
->i_lineno
;
3653 a
->a_lineno_off
= a
->a_offset
;
3658 Extend the bytecode with a new instruction.
3659 Update lnotab if necessary.
3663 assemble_emit(struct assembler
*a
, struct instr
*i
)
3665 int size
, arg
= 0, ext
= 0;
3666 Py_ssize_t len
= PyString_GET_SIZE(a
->a_bytecode
);
3669 size
= instrsize(i
);
3674 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
3676 if (a
->a_offset
+ size
>= len
) {
3677 if (len
> PY_SSIZE_T_MAX
/ 2)
3679 if (_PyString_Resize(&a
->a_bytecode
, len
* 2) < 0)
3682 code
= PyString_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
3683 a
->a_offset
+= size
;
3685 assert(i
->i_hasarg
);
3686 *code
++ = (char)EXTENDED_ARG
;
3687 *code
++ = ext
& 0xff;
3691 *code
++ = i
->i_opcode
;
3693 assert(size
== 3 || size
== 6);
3694 *code
++ = arg
& 0xff;
3701 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
3704 int bsize
, totsize
, extended_arg_count
= 0, last_extended_arg_count
;
3707 /* Compute the size of each block and fixup jump args.
3708 Replace block pointer with position in bytecode. */
3711 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
3712 b
= a
->a_postorder
[i
];
3713 bsize
= blocksize(b
);
3714 b
->b_offset
= totsize
;
3717 last_extended_arg_count
= extended_arg_count
;
3718 extended_arg_count
= 0;
3719 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3720 bsize
= b
->b_offset
;
3721 for (i
= 0; i
< b
->b_iused
; i
++) {
3722 struct instr
*instr
= &b
->b_instr
[i
];
3723 /* Relative jumps are computed relative to
3724 the instruction pointer after fetching
3725 the jump instruction.
3727 bsize
+= instrsize(instr
);
3729 instr
->i_oparg
= instr
->i_target
->b_offset
;
3730 else if (instr
->i_jrel
) {
3731 int delta
= instr
->i_target
->b_offset
- bsize
;
3732 instr
->i_oparg
= delta
;
3736 if (instr
->i_oparg
> 0xffff)
3737 extended_arg_count
++;
3741 /* XXX: This is an awful hack that could hurt performance, but
3742 on the bright side it should work until we come up
3743 with a better solution.
3745 The issue is that in the first loop blocksize() is called
3746 which calls instrsize() which requires i_oparg be set
3747 appropriately. There is a bootstrap problem because
3748 i_oparg is calculated in the second loop above.
3750 So we loop until we stop seeing new EXTENDED_ARGs.
3751 The only EXTENDED_ARGs that could be popping up are
3752 ones in jump instructions. So this should converge
3755 } while (last_extended_arg_count
!= extended_arg_count
);
3759 dict_keys_inorder(PyObject
*dict
, int offset
)
3761 PyObject
*tuple
, *k
, *v
;
3762 Py_ssize_t i
, pos
= 0, size
= PyDict_Size(dict
);
3764 tuple
= PyTuple_New(size
);
3767 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3768 i
= PyInt_AS_LONG(v
);
3769 /* The keys of the dictionary are tuples. (see compiler_add_o)
3770 The object we want is always first, though. */
3771 k
= PyTuple_GET_ITEM(k
, 0);
3773 assert((i
- offset
) < size
);
3774 assert((i
- offset
) >= 0);
3775 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3781 compute_code_flags(struct compiler
*c
)
3783 PySTEntryObject
*ste
= c
->u
->u_ste
;
3785 if (ste
->ste_type
!= ModuleBlock
)
3786 flags
|= CO_NEWLOCALS
;
3787 if (ste
->ste_type
== FunctionBlock
) {
3788 if (!ste
->ste_unoptimized
)
3789 flags
|= CO_OPTIMIZED
;
3790 if (ste
->ste_nested
)
3792 if (ste
->ste_generator
)
3793 flags
|= CO_GENERATOR
;
3794 if (ste
->ste_varargs
)
3795 flags
|= CO_VARARGS
;
3796 if (ste
->ste_varkeywords
)
3797 flags
|= CO_VARKEYWORDS
;
3800 /* (Only) inherit compilerflags in PyCF_MASK */
3801 flags
|= (c
->c_flags
->cf_flags
& PyCF_MASK
);
3803 n
= PyDict_Size(c
->u
->u_freevars
);
3807 n
= PyDict_Size(c
->u
->u_cellvars
);
3818 static PyCodeObject
*
3819 makecode(struct compiler
*c
, struct assembler
*a
)
3822 PyCodeObject
*co
= NULL
;
3823 PyObject
*consts
= NULL
;
3824 PyObject
*names
= NULL
;
3825 PyObject
*varnames
= NULL
;
3826 PyObject
*filename
= NULL
;
3827 PyObject
*name
= NULL
;
3828 PyObject
*freevars
= NULL
;
3829 PyObject
*cellvars
= NULL
;
3830 PyObject
*bytecode
= NULL
;
3833 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
3836 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
3839 names
= dict_keys_inorder(c
->u
->u_names
, 0);
3840 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
3841 if (!consts
|| !names
|| !varnames
)
3844 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
3847 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
3850 filename
= PyString_FromString(c
->c_filename
);
3854 nlocals
= PyDict_Size(c
->u
->u_varnames
);
3855 flags
= compute_code_flags(c
);
3859 bytecode
= PyCode_Optimize(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
3863 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
3869 co
= PyCode_New(c
->u
->u_argcount
, nlocals
, stackdepth(c
), flags
,
3870 bytecode
, consts
, names
, varnames
,
3872 filename
, c
->u
->u_name
,
3873 c
->u
->u_firstlineno
,
3878 Py_XDECREF(varnames
);
3879 Py_XDECREF(filename
);
3881 Py_XDECREF(freevars
);
3882 Py_XDECREF(cellvars
);
3883 Py_XDECREF(bytecode
);
3888 /* For debugging purposes only */
3891 dump_instr(const struct instr
*i
)
3893 const char *jrel
= i
->i_jrel
? "jrel " : "";
3894 const char *jabs
= i
->i_jabs
? "jabs " : "";
3899 sprintf(arg
, "arg: %d ", i
->i_oparg
);
3901 fprintf(stderr
, "line: %d, opcode: %d %s%s%s\n",
3902 i
->i_lineno
, i
->i_opcode
, arg
, jabs
, jrel
);
3906 dump_basicblock(const basicblock
*b
)
3908 const char *seen
= b
->b_seen
? "seen " : "";
3909 const char *b_return
= b
->b_return
? "return " : "";
3910 fprintf(stderr
, "used: %d, depth: %d, offset: %d %s%s\n",
3911 b
->b_iused
, b
->b_startdepth
, b
->b_offset
, seen
, b_return
);
3914 for (i
= 0; i
< b
->b_iused
; i
++) {
3915 fprintf(stderr
, " [%02d] ", i
);
3916 dump_instr(b
->b_instr
+ i
);
3922 static PyCodeObject
*
3923 assemble(struct compiler
*c
, int addNone
)
3925 basicblock
*b
, *entryblock
;
3928 PyCodeObject
*co
= NULL
;
3930 /* Make sure every block that falls off the end returns None.
3931 XXX NEXT_BLOCK() isn't quite right, because if the last
3932 block ends with a jump or return b_next shouldn't set.
3934 if (!c
->u
->u_curblock
->b_return
) {
3937 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3938 ADDOP(c
, RETURN_VALUE
);
3943 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3948 /* Set firstlineno if it wasn't explicitly set. */
3949 if (!c
->u
->u_firstlineno
) {
3950 if (entryblock
&& entryblock
->b_instr
)
3951 c
->u
->u_firstlineno
= entryblock
->b_instr
->i_lineno
;
3953 c
->u
->u_firstlineno
= 1;
3955 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
3957 dfs(c
, entryblock
, &a
);
3959 /* Can't modify the bytecode after computing jump offsets. */
3960 assemble_jump_offsets(&a
, c
);
3962 /* Emit code in reverse postorder from dfs. */
3963 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
3964 b
= a
.a_postorder
[i
];
3965 for (j
= 0; j
< b
->b_iused
; j
++)
3966 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
3970 if (_PyString_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
3972 if (_PyString_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
3975 co
= makecode(c
, &a
);