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 /* Don't mangle __id__ or names with dots.
199 The only time a name with a dot can occur is when
200 we are compiling an import statement that has a
203 TODO(jhylton): Decide whether we want to support
204 mangling of the module name, e.g. __M.X.
206 if ((name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
207 || strchr(name
, '.')) {
209 return ident
; /* Don't mangle __whatever__ */
211 /* Strip leading underscores from class name */
216 return ident
; /* Don't mangle if class is just underscores */
220 assert(1 <= PY_SSIZE_T_MAX
- nlen
);
221 assert(1 + nlen
<= PY_SSIZE_T_MAX
- plen
);
223 ident
= PyString_FromStringAndSize(NULL
, 1 + nlen
+ plen
);
226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
227 buffer
= PyString_AS_STRING(ident
);
229 strncpy(buffer
+1, p
, plen
);
230 strcpy(buffer
+1+plen
, name
);
235 compiler_init(struct compiler
*c
)
237 memset(c
, 0, sizeof(struct compiler
));
239 c
->c_stack
= PyList_New(0);
247 PyAST_Compile(mod_ty mod
, const char *filename
, PyCompilerFlags
*flags
,
251 PyCodeObject
*co
= NULL
;
252 PyCompilerFlags local_flags
;
256 __doc__
= PyString_InternFromString("__doc__");
261 if (!compiler_init(&c
))
263 c
.c_filename
= filename
;
265 c
.c_future
= PyFuture_FromAST(mod
, filename
);
266 if (c
.c_future
== NULL
)
269 local_flags
.cf_flags
= 0;
270 flags
= &local_flags
;
272 merged
= c
.c_future
->ff_features
| flags
->cf_flags
;
273 c
.c_future
->ff_features
= merged
;
274 flags
->cf_flags
= merged
;
278 c
.c_st
= PySymtable_Build(mod
, filename
, c
.c_future
);
279 if (c
.c_st
== NULL
) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError
, "no symtable");
285 /* XXX initialize to NULL for now, need to handle */
288 co
= compiler_mod(&c
, mod
);
292 assert(co
|| PyErr_Occurred());
297 PyNode_Compile(struct _node
*n
, const char *filename
)
299 PyCodeObject
*co
= NULL
;
301 PyArena
*arena
= PyArena_New();
304 mod
= PyAST_FromNode(n
, NULL
, filename
, arena
);
306 co
= PyAST_Compile(mod
, filename
, NULL
, arena
);
312 compiler_free(struct compiler
*c
)
315 PySymtable_Free(c
->c_st
);
317 PyObject_Free(c
->c_future
);
318 Py_DECREF(c
->c_stack
);
322 list2dict(PyObject
*list
)
326 PyObject
*dict
= PyDict_New();
327 if (!dict
) return NULL
;
329 n
= PyList_Size(list
);
330 for (i
= 0; i
< n
; i
++) {
331 v
= PyInt_FromLong(i
);
336 k
= PyList_GET_ITEM(list
, i
);
337 k
= PyTuple_Pack(2, k
, k
->ob_type
);
338 if (k
== NULL
|| PyDict_SetItem(dict
, k
, v
) < 0) {
350 /* Return new dict containing names from src that match scope(s).
352 src is a symbol table dictionary. If the scope of a name matches
353 either scope_type or flag is set, insert it into the new dict. The
354 values are integers, starting at offset and increasing by one for
359 dictbytype(PyObject
*src
, int scope_type
, int flag
, int offset
)
361 Py_ssize_t pos
= 0, i
= offset
, scope
;
362 PyObject
*k
, *v
, *dest
= PyDict_New();
368 while (PyDict_Next(src
, &pos
, &k
, &v
)) {
369 /* XXX this should probably be a macro in symtable.h */
370 assert(PyInt_Check(v
));
371 scope
= (PyInt_AS_LONG(v
) >> SCOPE_OFF
) & SCOPE_MASK
;
373 if (scope
== scope_type
|| PyInt_AS_LONG(v
) & flag
) {
374 PyObject
*tuple
, *item
= PyInt_FromLong(i
);
380 tuple
= PyTuple_Pack(2, k
, k
->ob_type
);
381 if (!tuple
|| PyDict_SetItem(dest
, tuple
, item
) < 0) {
395 compiler_unit_check(struct compiler_unit
*u
)
398 for (block
= u
->u_blocks
; block
!= NULL
; block
= block
->b_list
) {
399 assert((void *)block
!= (void *)0xcbcbcbcb);
400 assert((void *)block
!= (void *)0xfbfbfbfb);
401 assert((void *)block
!= (void *)0xdbdbdbdb);
402 if (block
->b_instr
!= NULL
) {
403 assert(block
->b_ialloc
> 0);
404 assert(block
->b_iused
> 0);
405 assert(block
->b_ialloc
>= block
->b_iused
);
408 assert (block
->b_iused
== 0);
409 assert (block
->b_ialloc
== 0);
415 compiler_unit_free(struct compiler_unit
*u
)
417 basicblock
*b
, *next
;
419 compiler_unit_check(u
);
423 PyObject_Free((void *)b
->b_instr
);
425 PyObject_Free((void *)b
);
430 Py_CLEAR(u
->u_consts
);
431 Py_CLEAR(u
->u_names
);
432 Py_CLEAR(u
->u_varnames
);
433 Py_CLEAR(u
->u_freevars
);
434 Py_CLEAR(u
->u_cellvars
);
435 Py_CLEAR(u
->u_private
);
440 compiler_enter_scope(struct compiler
*c
, identifier name
, void *key
,
443 struct compiler_unit
*u
;
445 u
= (struct compiler_unit
*)PyObject_Malloc(sizeof(
446 struct compiler_unit
));
451 memset(u
, 0, sizeof(struct compiler_unit
));
453 u
->u_ste
= PySymtable_Lookup(c
->c_st
, key
);
455 compiler_unit_free(u
);
460 u
->u_varnames
= list2dict(u
->u_ste
->ste_varnames
);
461 u
->u_cellvars
= dictbytype(u
->u_ste
->ste_symbols
, CELL
, 0, 0);
462 if (!u
->u_varnames
|| !u
->u_cellvars
) {
463 compiler_unit_free(u
);
467 u
->u_freevars
= dictbytype(u
->u_ste
->ste_symbols
, FREE
, DEF_FREE_CLASS
,
468 PyDict_Size(u
->u_cellvars
));
469 if (!u
->u_freevars
) {
470 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
*wrapper
= PyCObject_FromVoidPtr(c
->u
, NULL
);
496 if (!wrapper
|| PyList_Append(c
->c_stack
, wrapper
) < 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 wrapper
= PyList_GET_ITEM(c
->c_stack
, n
);
526 c
->u
= (struct compiler_unit
*)PyCObject_AsVoidPtr(wrapper
);
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 "anonymous" local variable.
539 Used by list comprehensions and with statements.
543 compiler_new_tmpname(struct compiler
*c
)
546 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->u
->u_tmpname
);
547 return PyString_FromString(tmpname
);
550 /* Allocate a new block and return a pointer to it.
551 Returns NULL on error.
555 compiler_new_block(struct compiler
*c
)
558 struct compiler_unit
*u
;
561 b
= (basicblock
*)PyObject_Malloc(sizeof(basicblock
));
566 memset((void *)b
, 0, sizeof(basicblock
));
567 /* Extend the singly linked list of blocks with new block. */
568 b
->b_list
= u
->u_blocks
;
574 compiler_use_new_block(struct compiler
*c
)
576 basicblock
*block
= compiler_new_block(c
);
579 c
->u
->u_curblock
= block
;
584 compiler_next_block(struct compiler
*c
)
586 basicblock
*block
= compiler_new_block(c
);
589 c
->u
->u_curblock
->b_next
= block
;
590 c
->u
->u_curblock
= block
;
595 compiler_use_next_block(struct compiler
*c
, basicblock
*block
)
597 assert(block
!= NULL
);
598 c
->u
->u_curblock
->b_next
= block
;
599 c
->u
->u_curblock
= block
;
603 /* Returns the offset of the next instruction in the current block's
604 b_instr array. Resizes the b_instr as necessary.
605 Returns -1 on failure.
609 compiler_next_instr(struct compiler
*c
, basicblock
*b
)
612 if (b
->b_instr
== NULL
) {
613 b
->b_instr
= (struct instr
*)PyObject_Malloc(
614 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
615 if (b
->b_instr
== NULL
) {
619 b
->b_ialloc
= DEFAULT_BLOCK_SIZE
;
620 memset((char *)b
->b_instr
, 0,
621 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
623 else if (b
->b_iused
== b
->b_ialloc
) {
625 size_t oldsize
, newsize
;
626 oldsize
= b
->b_ialloc
* sizeof(struct instr
);
627 newsize
= oldsize
<< 1;
629 if (oldsize
> (PY_SIZE_MAX
>> 1)) {
639 tmp
= (struct instr
*)PyObject_Realloc(
640 (void *)b
->b_instr
, newsize
);
646 memset((char *)b
->b_instr
+ oldsize
, 0, newsize
- oldsize
);
651 /* Set the i_lineno member of the instruction at offset off if the
652 line number for the current expression/statement has not
653 already been set. If it has been set, the call has no effect.
655 The line number is reset in the following cases:
656 - when entering a new scope
658 - on each expression that start a new line
659 - before the "except" clause
660 - before the "for" and "while" expressions
664 compiler_set_lineno(struct compiler
*c
, int off
)
667 if (c
->u
->u_lineno_set
)
669 c
->u
->u_lineno_set
= true;
670 b
= c
->u
->u_curblock
;
671 b
->b_instr
[off
].i_lineno
= c
->u
->u_lineno
;
675 opcode_stack_effect(int opcode
, int oparg
)
699 case BINARY_MULTIPLY
:
703 case BINARY_SUBTRACT
:
705 case BINARY_FLOOR_DIVIDE
:
706 case BINARY_TRUE_DIVIDE
:
708 case INPLACE_FLOOR_DIVIDE
:
709 case INPLACE_TRUE_DIVIDE
:
740 case INPLACE_SUBTRACT
:
741 case INPLACE_MULTIPLY
:
771 case PRINT_NEWLINE_TO
:
782 return -1; /* XXX Sometimes more */
797 return -1; /* or -2 or -3 if exception occurred */
805 case UNPACK_SEQUENCE
:
853 return 3; /* actually pushed by an exception */
864 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
866 return -NARGS(oparg
);
867 case CALL_FUNCTION_VAR
:
868 case CALL_FUNCTION_KW
:
869 return -NARGS(oparg
)-1;
870 case CALL_FUNCTION_VAR_KW
:
871 return -NARGS(oparg
)-2;
890 fprintf(stderr
, "opcode = %d\n", opcode
);
891 Py_FatalError("opcode_stack_effect()");
894 return 0; /* not reachable */
897 /* Add an opcode with no argument.
898 Returns 0 on failure, 1 on success.
902 compiler_addop(struct compiler
*c
, int opcode
)
907 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
910 b
= c
->u
->u_curblock
;
911 i
= &b
->b_instr
[off
];
912 i
->i_opcode
= opcode
;
914 if (opcode
== RETURN_VALUE
)
916 compiler_set_lineno(c
, off
);
921 compiler_add_o(struct compiler
*c
, PyObject
*dict
, PyObject
*o
)
925 unsigned char *p
, *q
;
928 int real_part_zero
, imag_part_zero
;
930 /* necessary to make sure types aren't coerced (e.g., int and long) */
931 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
932 if (PyFloat_Check(o
)) {
933 d
= PyFloat_AS_DOUBLE(o
);
934 p
= (unsigned char*) &d
;
935 /* all we need is to make the tuple different in either the 0.0
936 * or -0.0 case from all others, just to avoid the "coercion".
938 if (*p
==0 && p
[sizeof(double)-1]==0)
939 t
= PyTuple_Pack(3, o
, o
->ob_type
, Py_None
);
941 t
= PyTuple_Pack(2, o
, o
->ob_type
);
943 else if (PyComplex_Check(o
)) {
944 /* complex case is even messier: we need to make complex(x,
945 0.) different from complex(x, -0.) and complex(0., y)
946 different from complex(-0., y), for any x and y. In
947 particular, all four complex zeros should be
949 z
= PyComplex_AsCComplex(o
);
950 p
= (unsigned char*) &(z
.real
);
951 q
= (unsigned char*) &(z
.imag
);
952 /* all that matters here is that on IEEE platforms
953 real_part_zero will be true if z.real == 0., and false if
954 z.real == -0. In fact, real_part_zero will also be true
955 for some other rarely occurring nonzero floats, but this
956 doesn't matter. Similar comments apply to
958 real_part_zero
= *p
==0 && p
[sizeof(double)-1]==0;
959 imag_part_zero
= *q
==0 && q
[sizeof(double)-1]==0;
960 if (real_part_zero
&& imag_part_zero
) {
961 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_True
, Py_True
);
963 else if (real_part_zero
&& !imag_part_zero
) {
964 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_True
, Py_False
);
966 else if (!real_part_zero
&& imag_part_zero
) {
967 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_False
, Py_True
);
970 t
= PyTuple_Pack(2, o
, o
->ob_type
);
974 t
= PyTuple_Pack(2, o
, o
->ob_type
);
979 v
= PyDict_GetItem(dict
, t
);
981 arg
= PyDict_Size(dict
);
982 v
= PyInt_FromLong(arg
);
987 if (PyDict_SetItem(dict
, t
, v
) < 0) {
995 arg
= PyInt_AsLong(v
);
1001 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
1004 int arg
= compiler_add_o(c
, dict
, o
);
1007 return compiler_addop_i(c
, opcode
, arg
);
1011 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
1015 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
1018 arg
= compiler_add_o(c
, dict
, mangled
);
1022 return compiler_addop_i(c
, opcode
, arg
);
1025 /* Add an opcode with an integer argument.
1026 Returns 0 on failure, 1 on success.
1030 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
1034 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1037 i
= &c
->u
->u_curblock
->b_instr
[off
];
1038 i
->i_opcode
= opcode
;
1041 compiler_set_lineno(c
, off
);
1046 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
1052 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1055 i
= &c
->u
->u_curblock
->b_instr
[off
];
1056 i
->i_opcode
= opcode
;
1063 compiler_set_lineno(c
, off
);
1067 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1068 like to find better names.) NEW_BLOCK() creates a new block and sets
1069 it as the current block. NEXT_BLOCK() also creates an implicit jump
1070 from the current block to the new block.
1073 /* The returns inside these macros make it impossible to decref objects
1074 created in the local function. Local objects should use the arena.
1078 #define NEW_BLOCK(C) { \
1079 if (compiler_use_new_block((C)) == NULL) \
1083 #define NEXT_BLOCK(C) { \
1084 if (compiler_next_block((C)) == NULL) \
1088 #define ADDOP(C, OP) { \
1089 if (!compiler_addop((C), (OP))) \
1093 #define ADDOP_IN_SCOPE(C, OP) { \
1094 if (!compiler_addop((C), (OP))) { \
1095 compiler_exit_scope(c); \
1100 #define ADDOP_O(C, OP, O, TYPE) { \
1101 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1105 #define ADDOP_NAME(C, OP, O, TYPE) { \
1106 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1110 #define ADDOP_I(C, OP, O) { \
1111 if (!compiler_addop_i((C), (OP), (O))) \
1115 #define ADDOP_JABS(C, OP, O) { \
1116 if (!compiler_addop_j((C), (OP), (O), 1)) \
1120 #define ADDOP_JREL(C, OP, O) { \
1121 if (!compiler_addop_j((C), (OP), (O), 0)) \
1125 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1126 the ASDL name to synthesize the name of the C type and the visit function.
1129 #define VISIT(C, TYPE, V) {\
1130 if (!compiler_visit_ ## TYPE((C), (V))) \
1134 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1135 if (!compiler_visit_ ## TYPE((C), (V))) { \
1136 compiler_exit_scope(c); \
1141 #define VISIT_SLICE(C, V, CTX) {\
1142 if (!compiler_visit_slice((C), (V), (CTX))) \
1146 #define VISIT_SEQ(C, TYPE, SEQ) { \
1148 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1149 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1150 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1151 if (!compiler_visit_ ## TYPE((C), elt)) \
1156 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1158 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1159 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1160 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1161 if (!compiler_visit_ ## TYPE((C), elt)) { \
1162 compiler_exit_scope(c); \
1169 compiler_isdocstring(stmt_ty s
)
1171 if (s
->kind
!= Expr_kind
)
1173 return s
->v
.Expr
.value
->kind
== Str_kind
;
1176 /* Compile a sequence of statements, checking for a docstring. */
1179 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1184 if (!asdl_seq_LEN(stmts
))
1186 st
= (stmt_ty
)asdl_seq_GET(stmts
, 0);
1187 if (compiler_isdocstring(st
) && Py_OptimizeFlag
< 2) {
1188 /* don't generate docstrings if -OO */
1190 VISIT(c
, expr
, st
->v
.Expr
.value
);
1191 if (!compiler_nameop(c
, __doc__
, Store
))
1194 for (; i
< asdl_seq_LEN(stmts
); i
++)
1195 VISIT(c
, stmt
, (stmt_ty
)asdl_seq_GET(stmts
, i
));
1199 static PyCodeObject
*
1200 compiler_mod(struct compiler
*c
, mod_ty mod
)
1204 static PyObject
*module
;
1206 module
= PyString_InternFromString("<module>");
1210 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1211 if (!compiler_enter_scope(c
, module
, mod
, 0))
1213 switch (mod
->kind
) {
1215 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1216 compiler_exit_scope(c
);
1220 case Interactive_kind
:
1221 c
->c_interactive
= 1;
1222 VISIT_SEQ_IN_SCOPE(c
, stmt
,
1223 mod
->v
.Interactive
.body
);
1225 case Expression_kind
:
1226 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1230 PyErr_SetString(PyExc_SystemError
,
1231 "suite should not be possible");
1234 PyErr_Format(PyExc_SystemError
,
1235 "module kind %d should not be possible",
1239 co
= assemble(c
, addNone
);
1240 compiler_exit_scope(c
);
1244 /* The test for LOCAL must come before the test for FREE in order to
1245 handle classes where name is both local and free. The local var is
1246 a method and the free var is a free var referenced within a method.
1250 get_ref_type(struct compiler
*c
, PyObject
*name
)
1252 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1255 PyOS_snprintf(buf
, sizeof(buf
),
1256 "unknown scope for %.100s in %.100s(%s) in %s\n"
1257 "symbols: %s\nlocals: %s\nglobals: %s\n",
1258 PyString_AS_STRING(name
),
1259 PyString_AS_STRING(c
->u
->u_name
),
1260 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1262 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1263 PyObject_REPR(c
->u
->u_varnames
),
1264 PyObject_REPR(c
->u
->u_names
)
1273 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1276 k
= PyTuple_Pack(2, name
, name
->ob_type
);
1279 v
= PyDict_GetItem(dict
, k
);
1283 return PyInt_AS_LONG(v
);
1287 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1289 int i
, free
= PyCode_GetNumFree(co
);
1291 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1292 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1295 for (i
= 0; i
< free
; ++i
) {
1296 /* Bypass com_addop_varname because it will generate
1297 LOAD_DEREF but LOAD_CLOSURE is needed.
1299 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1302 /* Special case: If a class contains a method with a
1303 free variable that has the same name as a method,
1304 the name will be considered free *and* local in the
1305 class. It should be handled by the closure, as
1306 well as by the normal name loookup logic.
1308 reftype
= get_ref_type(c
, name
);
1309 if (reftype
== CELL
)
1310 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1311 else /* (reftype == FREE) */
1312 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1314 printf("lookup %s in %s %d %d\n"
1315 "freevars of %s: %s\n",
1316 PyObject_REPR(name
),
1317 PyString_AS_STRING(c
->u
->u_name
),
1319 PyString_AS_STRING(co
->co_name
),
1320 PyObject_REPR(co
->co_freevars
));
1321 Py_FatalError("compiler_make_closure()");
1323 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1325 ADDOP_I(c
, BUILD_TUPLE
, free
);
1326 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1327 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1332 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1339 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1340 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(decos
, i
));
1346 compiler_arguments(struct compiler
*c
, arguments_ty args
)
1349 int n
= asdl_seq_LEN(args
->args
);
1350 /* Correctly handle nested argument lists */
1351 for (i
= 0; i
< n
; i
++) {
1352 expr_ty arg
= (expr_ty
)asdl_seq_GET(args
->args
, i
);
1353 if (arg
->kind
== Tuple_kind
) {
1354 PyObject
*id
= PyString_FromFormat(".%d", i
);
1358 if (!compiler_nameop(c
, id
, Load
)) {
1363 VISIT(c
, expr
, arg
);
1370 compiler_function(struct compiler
*c
, stmt_ty s
)
1373 PyObject
*first_const
= Py_None
;
1374 arguments_ty args
= s
->v
.FunctionDef
.args
;
1375 asdl_seq
* decos
= s
->v
.FunctionDef
.decorator_list
;
1377 int i
, n
, docstring
;
1379 assert(s
->kind
== FunctionDef_kind
);
1381 if (!compiler_decorators(c
, decos
))
1384 VISIT_SEQ(c
, expr
, args
->defaults
);
1385 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1389 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1390 docstring
= compiler_isdocstring(st
);
1391 if (docstring
&& Py_OptimizeFlag
< 2)
1392 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1393 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1394 compiler_exit_scope(c
);
1398 /* unpack nested arguments */
1399 compiler_arguments(c
, args
);
1401 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1402 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1403 /* if there was a docstring, we need to skip the first statement */
1404 for (i
= docstring
; i
< n
; i
++) {
1405 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1406 VISIT_IN_SCOPE(c
, stmt
, st
);
1408 co
= assemble(c
, 1);
1409 compiler_exit_scope(c
);
1413 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1416 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1417 ADDOP_I(c
, CALL_FUNCTION
, 1);
1420 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1424 compiler_class(struct compiler
*c
, stmt_ty s
)
1429 asdl_seq
* decos
= s
->v
.ClassDef
.decorator_list
;
1431 if (!compiler_decorators(c
, decos
))
1434 /* push class name on stack, needed by BUILD_CLASS */
1435 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1436 /* push the tuple of base classes on the stack */
1437 n
= asdl_seq_LEN(s
->v
.ClassDef
.bases
);
1439 VISIT_SEQ(c
, expr
, s
->v
.ClassDef
.bases
);
1440 ADDOP_I(c
, BUILD_TUPLE
, n
);
1441 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
,
1444 Py_XDECREF(c
->u
->u_private
);
1445 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1446 Py_INCREF(c
->u
->u_private
);
1447 str
= PyString_InternFromString("__name__");
1448 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1450 compiler_exit_scope(c
);
1455 str
= PyString_InternFromString("__module__");
1456 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
1458 compiler_exit_scope(c
);
1463 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
1464 compiler_exit_scope(c
);
1468 ADDOP_IN_SCOPE(c
, LOAD_LOCALS
);
1469 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1470 co
= assemble(c
, 1);
1471 compiler_exit_scope(c
);
1475 compiler_make_closure(c
, co
, 0);
1478 ADDOP_I(c
, CALL_FUNCTION
, 0);
1479 ADDOP(c
, BUILD_CLASS
);
1480 /* apply decorators */
1481 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1482 ADDOP_I(c
, CALL_FUNCTION
, 1);
1484 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
1490 compiler_ifexp(struct compiler
*c
, expr_ty e
)
1492 basicblock
*end
, *next
;
1494 assert(e
->kind
== IfExp_kind
);
1495 end
= compiler_new_block(c
);
1498 next
= compiler_new_block(c
);
1501 VISIT(c
, expr
, e
->v
.IfExp
.test
);
1502 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
1504 VISIT(c
, expr
, e
->v
.IfExp
.body
);
1505 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1506 compiler_use_next_block(c
, next
);
1508 VISIT(c
, expr
, e
->v
.IfExp
.orelse
);
1509 compiler_use_next_block(c
, end
);
1514 compiler_lambda(struct compiler
*c
, expr_ty e
)
1517 static identifier name
;
1518 arguments_ty args
= e
->v
.Lambda
.args
;
1519 assert(e
->kind
== Lambda_kind
);
1522 name
= PyString_InternFromString("<lambda>");
1528 VISIT_SEQ(c
, expr
, args
->defaults
);
1529 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
1532 /* unpack nested arguments */
1533 compiler_arguments(c
, args
);
1535 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1536 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
1537 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1538 co
= assemble(c
, 1);
1539 compiler_exit_scope(c
);
1543 compiler_make_closure(c
, co
, asdl_seq_LEN(args
->defaults
));
1550 compiler_print(struct compiler
*c
, stmt_ty s
)
1555 assert(s
->kind
== Print_kind
);
1556 n
= asdl_seq_LEN(s
->v
.Print
.values
);
1558 if (s
->v
.Print
.dest
) {
1559 VISIT(c
, expr
, s
->v
.Print
.dest
);
1562 for (i
= 0; i
< n
; i
++) {
1563 expr_ty e
= (expr_ty
)asdl_seq_GET(s
->v
.Print
.values
, i
);
1568 ADDOP(c
, PRINT_ITEM_TO
);
1572 ADDOP(c
, PRINT_ITEM
);
1575 if (s
->v
.Print
.nl
) {
1577 ADDOP(c
, PRINT_NEWLINE_TO
)
1579 ADDOP(c
, PRINT_NEWLINE
)
1587 compiler_if(struct compiler
*c
, stmt_ty s
)
1589 basicblock
*end
, *next
;
1591 assert(s
->kind
== If_kind
);
1592 end
= compiler_new_block(c
);
1595 next
= compiler_new_block(c
);
1599 constant
= expr_constant(s
->v
.If
.test
);
1600 /* constant = 0: "if 0"
1601 * constant = 1: "if 1", "if 2", ...
1602 * constant = -1: rest */
1603 if (constant
== 0) {
1605 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1606 } else if (constant
== 1) {
1607 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1609 VISIT(c
, expr
, s
->v
.If
.test
);
1610 ADDOP_JREL(c
, JUMP_IF_FALSE
, next
);
1612 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1613 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1614 compiler_use_next_block(c
, next
);
1617 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1619 compiler_use_next_block(c
, end
);
1624 compiler_for(struct compiler
*c
, stmt_ty s
)
1626 basicblock
*start
, *cleanup
, *end
;
1628 start
= compiler_new_block(c
);
1629 cleanup
= compiler_new_block(c
);
1630 end
= compiler_new_block(c
);
1631 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
1633 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1634 if (!compiler_push_fblock(c
, LOOP
, start
))
1636 VISIT(c
, expr
, s
->v
.For
.iter
);
1638 compiler_use_next_block(c
, start
);
1639 /* for expressions must be traced on each iteration,
1640 so we need to set an extra line number. */
1641 c
->u
->u_lineno_set
= false;
1642 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
1643 VISIT(c
, expr
, s
->v
.For
.target
);
1644 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
1645 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
1646 compiler_use_next_block(c
, cleanup
);
1647 ADDOP(c
, POP_BLOCK
);
1648 compiler_pop_fblock(c
, LOOP
, start
);
1649 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
1650 compiler_use_next_block(c
, end
);
1655 compiler_while(struct compiler
*c
, stmt_ty s
)
1657 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
1658 int constant
= expr_constant(s
->v
.While
.test
);
1660 if (constant
== 0) {
1661 if (s
->v
.While
.orelse
)
1662 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1665 loop
= compiler_new_block(c
);
1666 end
= compiler_new_block(c
);
1667 if (constant
== -1) {
1668 anchor
= compiler_new_block(c
);
1672 if (loop
== NULL
|| end
== NULL
)
1674 if (s
->v
.While
.orelse
) {
1675 orelse
= compiler_new_block(c
);
1682 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1683 compiler_use_next_block(c
, loop
);
1684 if (!compiler_push_fblock(c
, LOOP
, loop
))
1686 if (constant
== -1) {
1687 /* while expressions must be traced on each iteration,
1688 so we need to set an extra line number. */
1689 c
->u
->u_lineno_set
= false;
1690 VISIT(c
, expr
, s
->v
.While
.test
);
1691 ADDOP_JREL(c
, JUMP_IF_FALSE
, anchor
);
1694 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
1695 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
1697 /* XXX should the two POP instructions be in a separate block
1698 if there is no else clause ?
1701 if (constant
== -1) {
1702 compiler_use_next_block(c
, anchor
);
1704 ADDOP(c
, POP_BLOCK
);
1706 compiler_pop_fblock(c
, LOOP
, loop
);
1707 if (orelse
!= NULL
) /* what if orelse is just pass? */
1708 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1709 compiler_use_next_block(c
, end
);
1715 compiler_continue(struct compiler
*c
)
1717 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
1718 static const char IN_FINALLY_ERROR_MSG
[] =
1719 "'continue' not supported inside 'finally' clause";
1722 if (!c
->u
->u_nfblocks
)
1723 return compiler_error(c
, LOOP_ERROR_MSG
);
1724 i
= c
->u
->u_nfblocks
- 1;
1725 switch (c
->u
->u_fblock
[i
].fb_type
) {
1727 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
1731 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
) {
1732 /* Prevent continue anywhere under a finally
1733 even if hidden in a sub-try or except. */
1734 if (c
->u
->u_fblock
[i
].fb_type
== FINALLY_END
)
1735 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1738 return compiler_error(c
, LOOP_ERROR_MSG
);
1739 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
1742 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1748 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1754 L: <code for finalbody>
1757 The special instructions use the block stack. Each block
1758 stack entry contains the instruction that created it (here
1759 SETUP_FINALLY), the level of the value stack at the time the
1760 block stack entry was created, and a label (here L).
1763 Pushes the current value stack level and the label
1764 onto the block stack.
1766 Pops en entry from the block stack, and pops the value
1767 stack until its level is the same as indicated on the
1768 block stack. (The label is ignored.)
1770 Pops a variable number of entries from the *value* stack
1771 and re-raises the exception they specify. The number of
1772 entries popped depends on the (pseudo) exception type.
1774 The block stack is unwound when an exception is raised:
1775 when a SETUP_FINALLY entry is found, the exception is pushed
1776 onto the value stack (and the exception condition is cleared),
1777 and the interpreter jumps to the label gotten from the block
1782 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
1784 basicblock
*body
, *end
;
1785 body
= compiler_new_block(c
);
1786 end
= compiler_new_block(c
);
1787 if (body
== NULL
|| end
== NULL
)
1790 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
1791 compiler_use_next_block(c
, body
);
1792 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
1794 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
1795 ADDOP(c
, POP_BLOCK
);
1796 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
1798 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1799 compiler_use_next_block(c
, end
);
1800 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
1802 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
1803 ADDOP(c
, END_FINALLY
);
1804 compiler_pop_fblock(c
, FINALLY_END
, end
);
1810 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1811 (The contents of the value stack is shown in [], with the top
1812 at the right; 'tb' is trace-back info, 'val' the exception's
1813 associated value, and 'exc' the exception.)
1815 Value stack Label Instruction Argument
1821 [tb, val, exc] L1: DUP )
1822 [tb, val, exc, exc] <evaluate E1> )
1823 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1824 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1825 [tb, val, exc, 1] POP )
1827 [tb, val] <assign to V1> (or POP if no V1)
1832 [tb, val, exc, 0] L2: POP
1834 .............................etc.......................
1836 [tb, val, exc, 0] Ln+1: POP
1837 [tb, val, exc] END_FINALLY # re-raise exception
1839 [] L0: <next statement>
1841 Of course, parts are not generated if Vi or Ei is not present.
1844 compiler_try_except(struct compiler
*c
, stmt_ty s
)
1846 basicblock
*body
, *orelse
, *except
, *end
;
1849 body
= compiler_new_block(c
);
1850 except
= compiler_new_block(c
);
1851 orelse
= compiler_new_block(c
);
1852 end
= compiler_new_block(c
);
1853 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
1855 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
1856 compiler_use_next_block(c
, body
);
1857 if (!compiler_push_fblock(c
, EXCEPT
, body
))
1859 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
1860 ADDOP(c
, POP_BLOCK
);
1861 compiler_pop_fblock(c
, EXCEPT
, body
);
1862 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
1863 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
1864 compiler_use_next_block(c
, except
);
1865 for (i
= 0; i
< n
; i
++) {
1866 excepthandler_ty handler
= (excepthandler_ty
)asdl_seq_GET(
1867 s
->v
.TryExcept
.handlers
, i
);
1868 if (!handler
->v
.ExceptHandler
.type
&& i
< n
-1)
1869 return compiler_error(c
, "default 'except:' must be last");
1870 c
->u
->u_lineno_set
= false;
1871 c
->u
->u_lineno
= handler
->lineno
;
1872 except
= compiler_new_block(c
);
1875 if (handler
->v
.ExceptHandler
.type
) {
1877 VISIT(c
, expr
, handler
->v
.ExceptHandler
.type
);
1878 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
1879 ADDOP_JREL(c
, JUMP_IF_FALSE
, except
);
1883 if (handler
->v
.ExceptHandler
.name
) {
1884 VISIT(c
, expr
, handler
->v
.ExceptHandler
.name
);
1890 VISIT_SEQ(c
, stmt
, handler
->v
.ExceptHandler
.body
);
1891 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1892 compiler_use_next_block(c
, except
);
1893 if (handler
->v
.ExceptHandler
.type
)
1896 ADDOP(c
, END_FINALLY
);
1897 compiler_use_next_block(c
, orelse
);
1898 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
1899 compiler_use_next_block(c
, end
);
1904 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
1906 /* The IMPORT_NAME opcode was already generated. This function
1907 merely needs to bind the result to a name.
1909 If there is a dot in name, we need to split it and emit a
1910 LOAD_ATTR for each name.
1912 const char *src
= PyString_AS_STRING(name
);
1913 const char *dot
= strchr(src
, '.');
1915 /* Consume the base module name to get the first attribute */
1918 /* NB src is only defined when dot != NULL */
1920 dot
= strchr(src
, '.');
1921 attr
= PyString_FromStringAndSize(src
,
1922 dot
? dot
- src
: strlen(src
));
1925 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
1930 return compiler_nameop(c
, asname
, Store
);
1934 compiler_import(struct compiler
*c
, stmt_ty s
)
1936 /* The Import node stores a module name like a.b.c as a single
1937 string. This is convenient for all cases except
1939 where we need to parse that string to extract the individual
1941 XXX Perhaps change the representation to make this case simpler?
1943 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
1945 for (i
= 0; i
< n
; i
++) {
1946 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.Import
.names
, i
);
1950 if (c
->c_flags
&& (c
->c_flags
->cf_flags
& CO_FUTURE_ABSOLUTE_IMPORT
))
1951 level
= PyInt_FromLong(0);
1953 level
= PyInt_FromLong(-1);
1958 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
1960 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1961 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
1963 if (alias
->asname
) {
1964 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
1969 identifier tmp
= alias
->name
;
1970 const char *base
= PyString_AS_STRING(alias
->name
);
1971 char *dot
= strchr(base
, '.');
1973 tmp
= PyString_FromStringAndSize(base
,
1975 r
= compiler_nameop(c
, tmp
, Store
);
1987 compiler_from_import(struct compiler
*c
, stmt_ty s
)
1989 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
1991 PyObject
*names
= PyTuple_New(n
);
1997 if (s
->v
.ImportFrom
.level
== 0 && c
->c_flags
&&
1998 !(c
->c_flags
->cf_flags
& CO_FUTURE_ABSOLUTE_IMPORT
))
1999 level
= PyInt_FromLong(-1);
2001 level
= PyInt_FromLong(s
->v
.ImportFrom
.level
);
2008 /* build up the names */
2009 for (i
= 0; i
< n
; i
++) {
2010 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2011 Py_INCREF(alias
->name
);
2012 PyTuple_SET_ITEM(names
, i
, alias
->name
);
2015 if (s
->lineno
> c
->c_future
->ff_lineno
) {
2016 if (!strcmp(PyString_AS_STRING(s
->v
.ImportFrom
.module
),
2020 return compiler_error(c
,
2021 "from __future__ imports must occur "
2022 "at the beginning of the file");
2027 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2029 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
2031 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
2032 for (i
= 0; i
< n
; i
++) {
2033 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2034 identifier store_name
;
2036 if (i
== 0 && *PyString_AS_STRING(alias
->name
) == '*') {
2038 ADDOP(c
, IMPORT_STAR
);
2042 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
2043 store_name
= alias
->name
;
2045 store_name
= alias
->asname
;
2047 if (!compiler_nameop(c
, store_name
, Store
)) {
2052 /* remove imported module */
2058 compiler_assert(struct compiler
*c
, stmt_ty s
)
2060 static PyObject
*assertion_error
= NULL
;
2063 if (Py_OptimizeFlag
)
2065 if (assertion_error
== NULL
) {
2066 assertion_error
= PyString_InternFromString("AssertionError");
2067 if (assertion_error
== NULL
)
2070 if (s
->v
.Assert
.test
->kind
== Tuple_kind
&&
2071 asdl_seq_LEN(s
->v
.Assert
.test
->v
.Tuple
.elts
) > 0) {
2073 "assertion is always true, perhaps remove parentheses?";
2074 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
,
2075 c
->u
->u_lineno
, NULL
, NULL
) == -1)
2078 VISIT(c
, expr
, s
->v
.Assert
.test
);
2079 end
= compiler_new_block(c
);
2082 ADDOP_JREL(c
, JUMP_IF_TRUE
, end
);
2084 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
2085 if (s
->v
.Assert
.msg
) {
2086 VISIT(c
, expr
, s
->v
.Assert
.msg
);
2087 ADDOP_I(c
, RAISE_VARARGS
, 2);
2090 ADDOP_I(c
, RAISE_VARARGS
, 1);
2092 compiler_use_next_block(c
, end
);
2098 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2102 /* Always assign a lineno to the next instruction for a stmt. */
2103 c
->u
->u_lineno
= s
->lineno
;
2104 c
->u
->u_lineno_set
= false;
2107 case FunctionDef_kind
:
2108 return compiler_function(c
, s
);
2110 return compiler_class(c
, s
);
2112 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2113 return compiler_error(c
, "'return' outside function");
2114 if (s
->v
.Return
.value
) {
2115 VISIT(c
, expr
, s
->v
.Return
.value
);
2118 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2119 ADDOP(c
, RETURN_VALUE
);
2122 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2125 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2126 VISIT(c
, expr
, s
->v
.Assign
.value
);
2127 for (i
= 0; i
< n
; i
++) {
2131 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2134 case AugAssign_kind
:
2135 return compiler_augassign(c
, s
);
2137 return compiler_print(c
, s
);
2139 return compiler_for(c
, s
);
2141 return compiler_while(c
, s
);
2143 return compiler_if(c
, s
);
2146 if (s
->v
.Raise
.type
) {
2147 VISIT(c
, expr
, s
->v
.Raise
.type
);
2149 if (s
->v
.Raise
.inst
) {
2150 VISIT(c
, expr
, s
->v
.Raise
.inst
);
2152 if (s
->v
.Raise
.tback
) {
2153 VISIT(c
, expr
, s
->v
.Raise
.tback
);
2158 ADDOP_I(c
, RAISE_VARARGS
, n
);
2160 case TryExcept_kind
:
2161 return compiler_try_except(c
, s
);
2162 case TryFinally_kind
:
2163 return compiler_try_finally(c
, s
);
2165 return compiler_assert(c
, s
);
2167 return compiler_import(c
, s
);
2168 case ImportFrom_kind
:
2169 return compiler_from_import(c
, s
);
2171 VISIT(c
, expr
, s
->v
.Exec
.body
);
2172 if (s
->v
.Exec
.globals
) {
2173 VISIT(c
, expr
, s
->v
.Exec
.globals
);
2174 if (s
->v
.Exec
.locals
) {
2175 VISIT(c
, expr
, s
->v
.Exec
.locals
);
2180 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2183 ADDOP(c
, EXEC_STMT
);
2188 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2189 VISIT(c
, expr
, s
->v
.Expr
.value
);
2190 ADDOP(c
, PRINT_EXPR
);
2192 else if (s
->v
.Expr
.value
->kind
!= Str_kind
&&
2193 s
->v
.Expr
.value
->kind
!= Num_kind
) {
2194 VISIT(c
, expr
, s
->v
.Expr
.value
);
2201 if (!compiler_in_loop(c
))
2202 return compiler_error(c
, "'break' outside loop");
2203 ADDOP(c
, BREAK_LOOP
);
2206 return compiler_continue(c
);
2208 return compiler_with(c
, s
);
2214 unaryop(unaryop_ty op
)
2218 return UNARY_INVERT
;
2222 return UNARY_POSITIVE
;
2224 return UNARY_NEGATIVE
;
2226 PyErr_Format(PyExc_SystemError
,
2227 "unary op %d should not be possible", op
);
2233 binop(struct compiler
*c
, operator_ty op
)
2239 return BINARY_SUBTRACT
;
2241 return BINARY_MULTIPLY
;
2243 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2244 return BINARY_TRUE_DIVIDE
;
2246 return BINARY_DIVIDE
;
2248 return BINARY_MODULO
;
2250 return BINARY_POWER
;
2252 return BINARY_LSHIFT
;
2254 return BINARY_RSHIFT
;
2262 return BINARY_FLOOR_DIVIDE
;
2264 PyErr_Format(PyExc_SystemError
,
2265 "binary op %d should not be possible", op
);
2289 return PyCmp_IS_NOT
;
2293 return PyCmp_NOT_IN
;
2300 inplace_binop(struct compiler
*c
, operator_ty op
)
2306 return INPLACE_SUBTRACT
;
2308 return INPLACE_MULTIPLY
;
2310 if (c
->c_flags
&& c
->c_flags
->cf_flags
& CO_FUTURE_DIVISION
)
2311 return INPLACE_TRUE_DIVIDE
;
2313 return INPLACE_DIVIDE
;
2315 return INPLACE_MODULO
;
2317 return INPLACE_POWER
;
2319 return INPLACE_LSHIFT
;
2321 return INPLACE_RSHIFT
;
2329 return INPLACE_FLOOR_DIVIDE
;
2331 PyErr_Format(PyExc_SystemError
,
2332 "inplace binary op %d should not be possible", op
);
2338 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2341 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2343 PyObject
*dict
= c
->u
->u_names
;
2345 /* XXX AugStore isn't used anywhere! */
2347 /* First check for assignment to __debug__. Param? */
2348 if ((ctx
== Store
|| ctx
== AugStore
|| ctx
== Del
)
2349 && !strcmp(PyString_AS_STRING(name
), "__debug__")) {
2350 return compiler_error(c
, "can not assign to __debug__");
2353 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2359 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2362 dict
= c
->u
->u_freevars
;
2366 dict
= c
->u
->u_cellvars
;
2370 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2373 case GLOBAL_IMPLICIT
:
2374 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2375 !c
->u
->u_ste
->ste_unoptimized
)
2378 case GLOBAL_EXPLICIT
:
2382 /* scope can be 0 */
2386 /* XXX Leave assert here, but handle __doc__ and the like better */
2387 assert(scope
|| PyString_AS_STRING(name
)[0] == '_');
2392 case Load
: op
= LOAD_DEREF
; break;
2393 case Store
: op
= STORE_DEREF
; break;
2398 PyErr_Format(PyExc_SyntaxError
,
2399 "can not delete variable '%s' referenced "
2401 PyString_AS_STRING(name
));
2406 PyErr_SetString(PyExc_SystemError
,
2407 "param invalid for deref variable");
2413 case Load
: op
= LOAD_FAST
; break;
2414 case Store
: op
= STORE_FAST
; break;
2415 case Del
: op
= DELETE_FAST
; break;
2421 PyErr_SetString(PyExc_SystemError
,
2422 "param invalid for local variable");
2425 ADDOP_O(c
, op
, mangled
, varnames
);
2430 case Load
: op
= LOAD_GLOBAL
; break;
2431 case Store
: op
= STORE_GLOBAL
; break;
2432 case Del
: op
= DELETE_GLOBAL
; break;
2438 PyErr_SetString(PyExc_SystemError
,
2439 "param invalid for global variable");
2445 case Load
: op
= LOAD_NAME
; break;
2446 case Store
: op
= STORE_NAME
; break;
2447 case Del
: op
= DELETE_NAME
; break;
2453 PyErr_SetString(PyExc_SystemError
,
2454 "param invalid for name variable");
2461 arg
= compiler_add_o(c
, dict
, mangled
);
2465 return compiler_addop_i(c
, op
, arg
);
2469 compiler_boolop(struct compiler
*c
, expr_ty e
)
2475 assert(e
->kind
== BoolOp_kind
);
2476 if (e
->v
.BoolOp
.op
== And
)
2477 jumpi
= JUMP_IF_FALSE
;
2479 jumpi
= JUMP_IF_TRUE
;
2480 end
= compiler_new_block(c
);
2483 s
= e
->v
.BoolOp
.values
;
2484 n
= asdl_seq_LEN(s
) - 1;
2486 for (i
= 0; i
< n
; ++i
) {
2487 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, i
));
2488 ADDOP_JREL(c
, jumpi
, end
);
2491 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, n
));
2492 compiler_use_next_block(c
, end
);
2497 compiler_list(struct compiler
*c
, expr_ty e
)
2499 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
2500 if (e
->v
.List
.ctx
== Store
) {
2501 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2503 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
2504 if (e
->v
.List
.ctx
== Load
) {
2505 ADDOP_I(c
, BUILD_LIST
, n
);
2511 compiler_tuple(struct compiler
*c
, expr_ty e
)
2513 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
2514 if (e
->v
.Tuple
.ctx
== Store
) {
2515 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2517 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
2518 if (e
->v
.Tuple
.ctx
== Load
) {
2519 ADDOP_I(c
, BUILD_TUPLE
, n
);
2525 compiler_compare(struct compiler
*c
, expr_ty e
)
2528 basicblock
*cleanup
= NULL
;
2530 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2531 VISIT(c
, expr
, e
->v
.Compare
.left
);
2532 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
2535 cleanup
= compiler_new_block(c
);
2536 if (cleanup
== NULL
)
2539 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
2541 for (i
= 1; i
< n
; i
++) {
2543 ADDOP(c
, ROT_THREE
);
2544 ADDOP_I(c
, COMPARE_OP
,
2545 cmpop((cmpop_ty
)(asdl_seq_GET(
2546 e
->v
.Compare
.ops
, i
- 1))));
2547 ADDOP_JREL(c
, JUMP_IF_FALSE
, cleanup
);
2552 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
2554 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
2555 ADDOP_I(c
, COMPARE_OP
,
2556 cmpop((cmpop_ty
)(asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1))));
2558 basicblock
*end
= compiler_new_block(c
);
2561 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2562 compiler_use_next_block(c
, cleanup
);
2565 compiler_use_next_block(c
, end
);
2571 compiler_call(struct compiler
*c
, expr_ty e
)
2575 VISIT(c
, expr
, e
->v
.Call
.func
);
2576 n
= asdl_seq_LEN(e
->v
.Call
.args
);
2577 VISIT_SEQ(c
, expr
, e
->v
.Call
.args
);
2578 if (e
->v
.Call
.keywords
) {
2579 VISIT_SEQ(c
, keyword
, e
->v
.Call
.keywords
);
2580 n
|= asdl_seq_LEN(e
->v
.Call
.keywords
) << 8;
2582 if (e
->v
.Call
.starargs
) {
2583 VISIT(c
, expr
, e
->v
.Call
.starargs
);
2586 if (e
->v
.Call
.kwargs
) {
2587 VISIT(c
, expr
, e
->v
.Call
.kwargs
);
2592 ADDOP_I(c
, CALL_FUNCTION
, n
);
2595 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
2598 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
2601 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
2608 compiler_listcomp_generator(struct compiler
*c
, PyObject
*tmpname
,
2609 asdl_seq
*generators
, int gen_index
,
2612 /* generate code for the iterator, then each of the ifs,
2613 and then write to the element */
2616 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
2619 start
= compiler_new_block(c
);
2620 skip
= compiler_new_block(c
);
2621 if_cleanup
= compiler_new_block(c
);
2622 anchor
= compiler_new_block(c
);
2624 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2628 l
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2629 VISIT(c
, expr
, l
->iter
);
2631 compiler_use_next_block(c
, start
);
2632 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2634 VISIT(c
, expr
, l
->target
);
2636 /* XXX this needs to be cleaned up...a lot! */
2637 n
= asdl_seq_LEN(l
->ifs
);
2638 for (i
= 0; i
< n
; i
++) {
2639 expr_ty e
= (expr_ty
)asdl_seq_GET(l
->ifs
, i
);
2641 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
2646 if (++gen_index
< asdl_seq_LEN(generators
))
2647 if (!compiler_listcomp_generator(c
, tmpname
,
2648 generators
, gen_index
, elt
))
2651 /* only append after the last for generator */
2652 if (gen_index
>= asdl_seq_LEN(generators
)) {
2653 if (!compiler_nameop(c
, tmpname
, Load
))
2655 VISIT(c
, expr
, elt
);
2656 ADDOP(c
, LIST_APPEND
);
2658 compiler_use_next_block(c
, skip
);
2660 for (i
= 0; i
< n
; i
++) {
2661 ADDOP_I(c
, JUMP_FORWARD
, 1);
2663 compiler_use_next_block(c
, if_cleanup
);
2666 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2667 compiler_use_next_block(c
, anchor
);
2668 /* delete the temporary list name added to locals */
2670 if (!compiler_nameop(c
, tmpname
, Del
))
2677 compiler_listcomp(struct compiler
*c
, expr_ty e
)
2681 asdl_seq
*generators
= e
->v
.ListComp
.generators
;
2683 assert(e
->kind
== ListComp_kind
);
2684 tmp
= compiler_new_tmpname(c
);
2687 ADDOP_I(c
, BUILD_LIST
, 0);
2689 if (compiler_nameop(c
, tmp
, Store
))
2690 rc
= compiler_listcomp_generator(c
, tmp
, generators
, 0,
2697 compiler_genexp_generator(struct compiler
*c
,
2698 asdl_seq
*generators
, int gen_index
,
2701 /* generate code for the iterator, then each of the ifs,
2702 and then write to the element */
2704 comprehension_ty ge
;
2705 basicblock
*start
, *anchor
, *skip
, *if_cleanup
, *end
;
2708 start
= compiler_new_block(c
);
2709 skip
= compiler_new_block(c
);
2710 if_cleanup
= compiler_new_block(c
);
2711 anchor
= compiler_new_block(c
);
2712 end
= compiler_new_block(c
);
2714 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2715 anchor
== NULL
|| end
== NULL
)
2718 ge
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2719 ADDOP_JREL(c
, SETUP_LOOP
, end
);
2720 if (!compiler_push_fblock(c
, LOOP
, start
))
2723 if (gen_index
== 0) {
2724 /* Receive outermost iter as an implicit argument */
2725 c
->u
->u_argcount
= 1;
2726 ADDOP_I(c
, LOAD_FAST
, 0);
2729 /* Sub-iter - calculate on the fly */
2730 VISIT(c
, expr
, ge
->iter
);
2733 compiler_use_next_block(c
, start
);
2734 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2736 VISIT(c
, expr
, ge
->target
);
2738 /* XXX this needs to be cleaned up...a lot! */
2739 n
= asdl_seq_LEN(ge
->ifs
);
2740 for (i
= 0; i
< n
; i
++) {
2741 expr_ty e
= (expr_ty
)asdl_seq_GET(ge
->ifs
, i
);
2743 ADDOP_JREL(c
, JUMP_IF_FALSE
, if_cleanup
);
2748 if (++gen_index
< asdl_seq_LEN(generators
))
2749 if (!compiler_genexp_generator(c
, generators
, gen_index
, elt
))
2752 /* only append after the last 'for' generator */
2753 if (gen_index
>= asdl_seq_LEN(generators
)) {
2754 VISIT(c
, expr
, elt
);
2755 ADDOP(c
, YIELD_VALUE
);
2758 compiler_use_next_block(c
, skip
);
2760 for (i
= 0; i
< n
; i
++) {
2761 ADDOP_I(c
, JUMP_FORWARD
, 1);
2763 compiler_use_next_block(c
, if_cleanup
);
2767 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2768 compiler_use_next_block(c
, anchor
);
2769 ADDOP(c
, POP_BLOCK
);
2770 compiler_pop_fblock(c
, LOOP
, start
);
2771 compiler_use_next_block(c
, end
);
2777 compiler_genexp(struct compiler
*c
, expr_ty e
)
2779 static identifier name
;
2781 expr_ty outermost_iter
= ((comprehension_ty
)
2782 (asdl_seq_GET(e
->v
.GeneratorExp
.generators
,
2786 name
= PyString_FromString("<genexpr>");
2791 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2793 compiler_genexp_generator(c
, e
->v
.GeneratorExp
.generators
, 0,
2794 e
->v
.GeneratorExp
.elt
);
2795 co
= assemble(c
, 1);
2796 compiler_exit_scope(c
);
2800 compiler_make_closure(c
, co
, 0);
2803 VISIT(c
, expr
, outermost_iter
);
2805 ADDOP_I(c
, CALL_FUNCTION
, 1);
2811 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
2813 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
2814 VISIT(c
, expr
, k
->value
);
2818 /* Test whether expression is constant. For constants, report
2819 whether they are true or false.
2821 Return values: 1 for true, 0 for false, -1 for non-constant.
2825 expr_constant(expr_ty e
)
2829 return PyObject_IsTrue(e
->v
.Num
.n
);
2831 return PyObject_IsTrue(e
->v
.Str
.s
);
2833 /* __debug__ is not assignable, so we can optimize
2834 * it away in if and while statements */
2835 if (strcmp(PyString_AS_STRING(e
->v
.Name
.id
),
2837 return ! Py_OptimizeFlag
;
2845 Implements the with statement from PEP 343.
2847 The semantics outlined in that PEP are as follows:
2852 It is implemented roughly as:
2855 exit = context.__exit__ # not calling it
2856 value = context.__enter__()
2858 VAR = value # if VAR present in the syntax
2861 if an exception was raised:
2862 exc = copy of (exception, instance, traceback)
2864 exc = (None, None, None)
2868 compiler_with(struct compiler
*c
, stmt_ty s
)
2870 static identifier enter_attr
, exit_attr
;
2871 basicblock
*block
, *finally
;
2872 identifier tmpvalue
= NULL
;
2874 assert(s
->kind
== With_kind
);
2877 enter_attr
= PyString_InternFromString("__enter__");
2882 exit_attr
= PyString_InternFromString("__exit__");
2887 block
= compiler_new_block(c
);
2888 finally
= compiler_new_block(c
);
2889 if (!block
|| !finally
)
2892 if (s
->v
.With
.optional_vars
) {
2893 /* Create a temporary variable to hold context.__enter__().
2894 We need to do this rather than preserving it on the stack
2895 because SETUP_FINALLY remembers the stack level.
2896 We need to do the assignment *inside* the try/finally
2897 so that context.__exit__() is called when the assignment
2898 fails. But we need to call context.__enter__() *before*
2899 the try/finally so that if it fails we won't call
2902 tmpvalue
= compiler_new_tmpname(c
);
2903 if (tmpvalue
== NULL
)
2905 PyArena_AddPyObject(c
->c_arena
, tmpvalue
);
2909 VISIT(c
, expr
, s
->v
.With
.context_expr
);
2911 /* Squirrel away context.__exit__ by stuffing it under context */
2913 ADDOP_O(c
, LOAD_ATTR
, exit_attr
, names
);
2916 /* Call context.__enter__() */
2917 ADDOP_O(c
, LOAD_ATTR
, enter_attr
, names
);
2918 ADDOP_I(c
, CALL_FUNCTION
, 0);
2920 if (s
->v
.With
.optional_vars
) {
2921 /* Store it in tmpvalue */
2922 if (!compiler_nameop(c
, tmpvalue
, Store
))
2926 /* Discard result from context.__enter__() */
2930 /* Start the try block */
2931 ADDOP_JREL(c
, SETUP_FINALLY
, finally
);
2933 compiler_use_next_block(c
, block
);
2934 if (!compiler_push_fblock(c
, FINALLY_TRY
, block
)) {
2938 if (s
->v
.With
.optional_vars
) {
2939 /* Bind saved result of context.__enter__() to VAR */
2940 if (!compiler_nameop(c
, tmpvalue
, Load
) ||
2941 !compiler_nameop(c
, tmpvalue
, Del
))
2943 VISIT(c
, expr
, s
->v
.With
.optional_vars
);
2947 VISIT_SEQ(c
, stmt
, s
->v
.With
.body
);
2949 /* End of try block; start the finally block */
2950 ADDOP(c
, POP_BLOCK
);
2951 compiler_pop_fblock(c
, FINALLY_TRY
, block
);
2953 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2954 compiler_use_next_block(c
, finally
);
2955 if (!compiler_push_fblock(c
, FINALLY_END
, finally
))
2958 /* Finally block starts; context.__exit__ is on the stack under
2959 the exception or return information. Just issue our magic
2961 ADDOP(c
, WITH_CLEANUP
);
2963 /* Finally block ends. */
2964 ADDOP(c
, END_FINALLY
);
2965 compiler_pop_fblock(c
, FINALLY_END
, finally
);
2970 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
2974 /* If expr e has a different line number than the last expr/stmt,
2975 set a new line number for the next instruction.
2977 if (e
->lineno
> c
->u
->u_lineno
) {
2978 c
->u
->u_lineno
= e
->lineno
;
2979 c
->u
->u_lineno_set
= false;
2983 return compiler_boolop(c
, e
);
2985 VISIT(c
, expr
, e
->v
.BinOp
.left
);
2986 VISIT(c
, expr
, e
->v
.BinOp
.right
);
2987 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
2990 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
2991 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
2994 return compiler_lambda(c
, e
);
2996 return compiler_ifexp(c
, e
);
2998 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
2999 ADDOP_I(c
, BUILD_MAP
, (n
>0xFFFF ? 0xFFFF : n
));
3000 for (i
= 0; i
< n
; i
++) {
3002 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.values
, i
));
3004 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.keys
, i
));
3005 ADDOP(c
, STORE_MAP
);
3009 return compiler_listcomp(c
, e
);
3010 case GeneratorExp_kind
:
3011 return compiler_genexp(c
, e
);
3013 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
3014 return compiler_error(c
, "'yield' outside function");
3015 if (e
->v
.Yield
.value
) {
3016 VISIT(c
, expr
, e
->v
.Yield
.value
);
3019 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3021 ADDOP(c
, YIELD_VALUE
);
3024 return compiler_compare(c
, e
);
3026 return compiler_call(c
, e
);
3028 VISIT(c
, expr
, e
->v
.Repr
.value
);
3029 ADDOP(c
, UNARY_CONVERT
);
3032 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
3035 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
3037 /* The following exprs can be assignment targets. */
3038 case Attribute_kind
:
3039 if (e
->v
.Attribute
.ctx
!= AugStore
)
3040 VISIT(c
, expr
, e
->v
.Attribute
.value
);
3041 switch (e
->v
.Attribute
.ctx
) {
3044 /* Fall through to load */
3046 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
3050 /* Fall through to save */
3052 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
3055 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
3059 PyErr_SetString(PyExc_SystemError
,
3060 "param invalid in attribute expression");
3064 case Subscript_kind
:
3065 switch (e
->v
.Subscript
.ctx
) {
3067 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3068 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
3071 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3072 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
3075 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
3078 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3079 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
3082 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3083 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
3087 PyErr_SetString(PyExc_SystemError
,
3088 "param invalid in subscript expression");
3093 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3094 /* child nodes of List and Tuple will have expr_context set */
3096 return compiler_list(c
, e
);
3098 return compiler_tuple(c
, e
);
3104 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3106 expr_ty e
= s
->v
.AugAssign
.target
;
3109 assert(s
->kind
== AugAssign_kind
);
3112 case Attribute_kind
:
3113 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3114 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3117 VISIT(c
, expr
, auge
);
3118 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3119 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3120 auge
->v
.Attribute
.ctx
= AugStore
;
3121 VISIT(c
, expr
, auge
);
3123 case Subscript_kind
:
3124 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3125 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3128 VISIT(c
, expr
, auge
);
3129 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3130 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3131 auge
->v
.Subscript
.ctx
= AugStore
;
3132 VISIT(c
, expr
, auge
);
3135 if (!compiler_nameop(c
, e
->v
.Name
.id
, Load
))
3137 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3138 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3139 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3141 PyErr_Format(PyExc_SystemError
,
3142 "invalid node type (%d) for augmented assignment",
3150 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3152 struct fblockinfo
*f
;
3153 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
) {
3154 PyErr_SetString(PyExc_SystemError
,
3155 "too many statically nested blocks");
3158 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3165 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3167 struct compiler_unit
*u
= c
->u
;
3168 assert(u
->u_nfblocks
> 0);
3170 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3171 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3175 compiler_in_loop(struct compiler
*c
) {
3177 struct compiler_unit
*u
= c
->u
;
3178 for (i
= 0; i
< u
->u_nfblocks
; ++i
) {
3179 if (u
->u_fblock
[i
].fb_type
== LOOP
)
3184 /* Raises a SyntaxError and returns 0.
3185 If something goes wrong, a different exception may be raised.
3189 compiler_error(struct compiler
*c
, const char *errstr
)
3192 PyObject
*u
= NULL
, *v
= NULL
;
3194 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3199 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3203 v
= Py_BuildValue("(zO)", errstr
, u
);
3206 PyErr_SetObject(PyExc_SyntaxError
, v
);
3215 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3216 expr_context_ty ctx
)
3220 /* XXX this code is duplicated */
3222 case AugLoad
: /* fall through to Load */
3223 case Load
: op
= BINARY_SUBSCR
; break;
3224 case AugStore
:/* fall through to Store */
3225 case Store
: op
= STORE_SUBSCR
; break;
3226 case Del
: op
= DELETE_SUBSCR
; break;
3228 PyErr_Format(PyExc_SystemError
,
3229 "invalid %s kind %d in subscript\n",
3233 if (ctx
== AugLoad
) {
3234 ADDOP_I(c
, DUP_TOPX
, 2);
3236 else if (ctx
== AugStore
) {
3237 ADDOP(c
, ROT_THREE
);
3244 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3247 assert(s
->kind
== Slice_kind
);
3249 /* only handles the cases where BUILD_SLICE is emitted */
3250 if (s
->v
.Slice
.lower
) {
3251 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3254 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3257 if (s
->v
.Slice
.upper
) {
3258 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3261 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3264 if (s
->v
.Slice
.step
) {
3266 VISIT(c
, expr
, s
->v
.Slice
.step
);
3268 ADDOP_I(c
, BUILD_SLICE
, n
);
3273 compiler_simple_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3275 int op
= 0, slice_offset
= 0, stack_count
= 0;
3277 assert(s
->v
.Slice
.step
== NULL
);
3278 if (s
->v
.Slice
.lower
) {
3281 if (ctx
!= AugStore
)
3282 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3284 if (s
->v
.Slice
.upper
) {
3287 if (ctx
!= AugStore
)
3288 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3291 if (ctx
== AugLoad
) {
3292 switch (stack_count
) {
3293 case 0: ADDOP(c
, DUP_TOP
); break;
3294 case 1: ADDOP_I(c
, DUP_TOPX
, 2); break;
3295 case 2: ADDOP_I(c
, DUP_TOPX
, 3); break;
3298 else if (ctx
== AugStore
) {
3299 switch (stack_count
) {
3300 case 0: ADDOP(c
, ROT_TWO
); break;
3301 case 1: ADDOP(c
, ROT_THREE
); break;
3302 case 2: ADDOP(c
, ROT_FOUR
); break;
3307 case AugLoad
: /* fall through to Load */
3308 case Load
: op
= SLICE
; break;
3309 case AugStore
:/* fall through to Store */
3310 case Store
: op
= STORE_SLICE
; break;
3311 case Del
: op
= DELETE_SLICE
; break;
3314 PyErr_SetString(PyExc_SystemError
,
3315 "param invalid in simple slice");
3319 ADDOP(c
, op
+ slice_offset
);
3324 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3325 expr_context_ty ctx
)
3329 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3332 return compiler_slice(c
, s
, ctx
);
3334 VISIT(c
, expr
, s
->v
.Index
.value
);
3338 PyErr_SetString(PyExc_SystemError
,
3339 "extended slice invalid in nested slice");
3346 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3348 char * kindname
= NULL
;
3352 if (ctx
!= AugStore
) {
3353 VISIT(c
, expr
, s
->v
.Index
.value
);
3357 kindname
= "ellipsis";
3358 if (ctx
!= AugStore
) {
3359 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3364 if (!s
->v
.Slice
.step
)
3365 return compiler_simple_slice(c
, s
, ctx
);
3366 if (ctx
!= AugStore
) {
3367 if (!compiler_slice(c
, s
, ctx
))
3372 kindname
= "extended slice";
3373 if (ctx
!= AugStore
) {
3374 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3375 for (i
= 0; i
< n
; i
++) {
3376 slice_ty sub
= (slice_ty
)asdl_seq_GET(
3377 s
->v
.ExtSlice
.dims
, i
);
3378 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3381 ADDOP_I(c
, BUILD_TUPLE
, n
);
3385 PyErr_Format(PyExc_SystemError
,
3386 "invalid subscript kind %d", s
->kind
);
3389 return compiler_handle_subscr(c
, kindname
, ctx
);
3393 /* End of the compiler section, beginning of the assembler section */
3395 /* do depth-first search of basic block graph, starting with block.
3396 post records the block indices in post-order.
3398 XXX must handle implicit jumps from one block to next
3402 PyObject
*a_bytecode
; /* string containing bytecode */
3403 int a_offset
; /* offset into bytecode */
3404 int a_nblocks
; /* number of reachable blocks */
3405 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
3406 PyObject
*a_lnotab
; /* string containing lnotab */
3407 int a_lnotab_off
; /* offset into lnotab */
3408 int a_lineno
; /* last lineno of emitted instruction */
3409 int a_lineno_off
; /* bytecode offset of last lineno */
3413 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3416 struct instr
*instr
= NULL
;
3421 if (b
->b_next
!= NULL
)
3422 dfs(c
, b
->b_next
, a
);
3423 for (i
= 0; i
< b
->b_iused
; i
++) {
3424 instr
= &b
->b_instr
[i
];
3425 if (instr
->i_jrel
|| instr
->i_jabs
)
3426 dfs(c
, instr
->i_target
, a
);
3428 a
->a_postorder
[a
->a_nblocks
++] = b
;
3432 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3435 struct instr
*instr
;
3436 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3439 b
->b_startdepth
= depth
;
3440 for (i
= 0; i
< b
->b_iused
; i
++) {
3441 instr
= &b
->b_instr
[i
];
3442 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3443 if (depth
> maxdepth
)
3445 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3446 if (instr
->i_jrel
|| instr
->i_jabs
) {
3447 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3449 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3450 instr
->i_opcode
== JUMP_FORWARD
) {
3451 goto out
; /* remaining code is dead */
3456 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3462 /* Find the flow path that needs the largest stack. We assume that
3463 * cycles in the flow graph have no net effect on the stack depth.
3466 stackdepth(struct compiler
*c
)
3468 basicblock
*b
, *entryblock
;
3470 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3472 b
->b_startdepth
= INT_MIN
;
3477 return stackdepth_walk(c
, entryblock
, 0, 0);
3481 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3483 memset(a
, 0, sizeof(struct assembler
));
3484 a
->a_lineno
= firstlineno
;
3485 a
->a_bytecode
= PyString_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3488 a
->a_lnotab
= PyString_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3491 if (nblocks
> PY_SIZE_MAX
/ sizeof(basicblock
*)) {
3495 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3496 sizeof(basicblock
*) * nblocks
);
3497 if (!a
->a_postorder
) {
3505 assemble_free(struct assembler
*a
)
3507 Py_XDECREF(a
->a_bytecode
);
3508 Py_XDECREF(a
->a_lnotab
);
3510 PyObject_Free(a
->a_postorder
);
3513 /* Return the size of a basic block in bytes. */
3516 instrsize(struct instr
*instr
)
3518 if (!instr
->i_hasarg
)
3519 return 1; /* 1 byte for the opcode*/
3520 if (instr
->i_oparg
> 0xffff)
3521 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3522 return 3; /* 1 (opcode) + 2 (oparg) */
3526 blocksize(basicblock
*b
)
3531 for (i
= 0; i
< b
->b_iused
; i
++)
3532 size
+= instrsize(&b
->b_instr
[i
]);
3536 /* All about a_lnotab.
3538 c_lnotab is an array of unsigned bytes disguised as a Python string.
3539 It is used to map bytecode offsets to source code line #s (when needed
3542 The array is conceptually a list of
3543 (bytecode offset increment, line number increment)
3544 pairs. The details are important and delicate, best illustrated by example:
3546 byte code offset source code line number
3553 The first trick is that these numbers aren't stored, only the increments
3554 from one row to the next (this doesn't really work, but it's a start):
3556 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3558 The second trick is that an unsigned byte can't hold negative values, or
3559 values larger than 255, so (a) there's a deep assumption that byte code
3560 offsets and their corresponding line #s both increase monotonically, and (b)
3561 if at least one column jumps by more than 255 from one row to the next, more
3562 than one pair is written to the table. In case #b, there's no way to know
3563 from looking at the table later how many were written. That's the delicate
3564 part. A user of c_lnotab desiring to find the source line number
3565 corresponding to a bytecode address A should do something like this
3568 for addr_incr, line_incr in c_lnotab:
3574 In order for this to work, when the addr field increments by more than 255,
3575 the line # increment in each pair generated must be 0 until the remaining addr
3576 increment is < 256. So, in the example above, assemble_lnotab (it used
3577 to be called com_set_lineno) should not (as was actually done until 2.2)
3578 expand 300, 300 to 255, 255, 45, 45,
3579 but to 255, 0, 45, 255, 0, 45.
3583 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
3585 int d_bytecode
, d_lineno
;
3587 unsigned char *lnotab
;
3589 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
3590 d_lineno
= i
->i_lineno
- a
->a_lineno
;
3592 assert(d_bytecode
>= 0);
3593 assert(d_lineno
>= 0);
3595 if(d_bytecode
== 0 && d_lineno
== 0)
3598 if (d_bytecode
> 255) {
3599 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
3600 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3601 len
= PyString_GET_SIZE(a
->a_lnotab
);
3602 if (nbytes
>= len
) {
3603 if ((len
<= INT_MAX
/ 2) && (len
* 2 < nbytes
))
3605 else if (len
<= INT_MAX
/ 2)
3611 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3614 lnotab
= (unsigned char *)
3615 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3616 for (j
= 0; j
< ncodes
; j
++) {
3620 d_bytecode
-= ncodes
* 255;
3621 a
->a_lnotab_off
+= ncodes
* 2;
3623 assert(d_bytecode
<= 255);
3624 if (d_lineno
> 255) {
3625 int j
, nbytes
, ncodes
= d_lineno
/ 255;
3626 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3627 len
= PyString_GET_SIZE(a
->a_lnotab
);
3628 if (nbytes
>= len
) {
3629 if ((len
<= INT_MAX
/ 2) && len
* 2 < nbytes
)
3631 else if (len
<= INT_MAX
/ 2)
3637 if (_PyString_Resize(&a
->a_lnotab
, len
) < 0)
3640 lnotab
= (unsigned char *)
3641 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3642 *lnotab
++ = d_bytecode
;
3645 for (j
= 1; j
< ncodes
; j
++) {
3649 d_lineno
-= ncodes
* 255;
3650 a
->a_lnotab_off
+= ncodes
* 2;
3653 len
= PyString_GET_SIZE(a
->a_lnotab
);
3654 if (a
->a_lnotab_off
+ 2 >= len
) {
3655 if (_PyString_Resize(&a
->a_lnotab
, len
* 2) < 0)
3658 lnotab
= (unsigned char *)
3659 PyString_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3661 a
->a_lnotab_off
+= 2;
3663 *lnotab
++ = d_bytecode
;
3664 *lnotab
++ = d_lineno
;
3666 else { /* First line of a block; def stmt, etc. */
3668 *lnotab
++ = d_lineno
;
3670 a
->a_lineno
= i
->i_lineno
;
3671 a
->a_lineno_off
= a
->a_offset
;
3676 Extend the bytecode with a new instruction.
3677 Update lnotab if necessary.
3681 assemble_emit(struct assembler
*a
, struct instr
*i
)
3683 int size
, arg
= 0, ext
= 0;
3684 Py_ssize_t len
= PyString_GET_SIZE(a
->a_bytecode
);
3687 size
= instrsize(i
);
3692 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
3694 if (a
->a_offset
+ size
>= len
) {
3695 if (len
> PY_SSIZE_T_MAX
/ 2)
3697 if (_PyString_Resize(&a
->a_bytecode
, len
* 2) < 0)
3700 code
= PyString_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
3701 a
->a_offset
+= size
;
3703 assert(i
->i_hasarg
);
3704 *code
++ = (char)EXTENDED_ARG
;
3705 *code
++ = ext
& 0xff;
3709 *code
++ = i
->i_opcode
;
3711 assert(size
== 3 || size
== 6);
3712 *code
++ = arg
& 0xff;
3719 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
3722 int bsize
, totsize
, extended_arg_count
, last_extended_arg_count
= 0;
3725 /* Compute the size of each block and fixup jump args.
3726 Replace block pointer with position in bytecode. */
3729 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
3730 b
= a
->a_postorder
[i
];
3731 bsize
= blocksize(b
);
3732 b
->b_offset
= totsize
;
3735 extended_arg_count
= 0;
3736 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3737 bsize
= b
->b_offset
;
3738 for (i
= 0; i
< b
->b_iused
; i
++) {
3739 struct instr
*instr
= &b
->b_instr
[i
];
3740 /* Relative jumps are computed relative to
3741 the instruction pointer after fetching
3742 the jump instruction.
3744 bsize
+= instrsize(instr
);
3746 instr
->i_oparg
= instr
->i_target
->b_offset
;
3747 else if (instr
->i_jrel
) {
3748 int delta
= instr
->i_target
->b_offset
- bsize
;
3749 instr
->i_oparg
= delta
;
3753 if (instr
->i_oparg
> 0xffff)
3754 extended_arg_count
++;
3758 /* XXX: This is an awful hack that could hurt performance, but
3759 on the bright side it should work until we come up
3760 with a better solution.
3762 In the meantime, should the goto be dropped in favor
3765 The issue is that in the first loop blocksize() is called
3766 which calls instrsize() which requires i_oparg be set
3767 appropriately. There is a bootstrap problem because
3768 i_oparg is calculated in the second loop above.
3770 So we loop until we stop seeing new EXTENDED_ARGs.
3771 The only EXTENDED_ARGs that could be popping up are
3772 ones in jump instructions. So this should converge
3775 if (last_extended_arg_count
!= extended_arg_count
) {
3776 last_extended_arg_count
= extended_arg_count
;
3782 dict_keys_inorder(PyObject
*dict
, int offset
)
3784 PyObject
*tuple
, *k
, *v
;
3785 Py_ssize_t i
, pos
= 0, size
= PyDict_Size(dict
);
3787 tuple
= PyTuple_New(size
);
3790 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3791 i
= PyInt_AS_LONG(v
);
3792 k
= PyTuple_GET_ITEM(k
, 0);
3794 assert((i
- offset
) < size
);
3795 assert((i
- offset
) >= 0);
3796 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3802 compute_code_flags(struct compiler
*c
)
3804 PySTEntryObject
*ste
= c
->u
->u_ste
;
3806 if (ste
->ste_type
!= ModuleBlock
)
3807 flags
|= CO_NEWLOCALS
;
3808 if (ste
->ste_type
== FunctionBlock
) {
3809 if (!ste
->ste_unoptimized
)
3810 flags
|= CO_OPTIMIZED
;
3811 if (ste
->ste_nested
)
3813 if (ste
->ste_generator
)
3814 flags
|= CO_GENERATOR
;
3816 if (ste
->ste_varargs
)
3817 flags
|= CO_VARARGS
;
3818 if (ste
->ste_varkeywords
)
3819 flags
|= CO_VARKEYWORDS
;
3820 if (ste
->ste_generator
)
3821 flags
|= CO_GENERATOR
;
3823 /* (Only) inherit compilerflags in PyCF_MASK */
3824 flags
|= (c
->c_flags
->cf_flags
& PyCF_MASK
);
3826 n
= PyDict_Size(c
->u
->u_freevars
);
3830 n
= PyDict_Size(c
->u
->u_cellvars
);
3841 static PyCodeObject
*
3842 makecode(struct compiler
*c
, struct assembler
*a
)
3845 PyCodeObject
*co
= NULL
;
3846 PyObject
*consts
= NULL
;
3847 PyObject
*names
= NULL
;
3848 PyObject
*varnames
= NULL
;
3849 PyObject
*filename
= NULL
;
3850 PyObject
*name
= NULL
;
3851 PyObject
*freevars
= NULL
;
3852 PyObject
*cellvars
= NULL
;
3853 PyObject
*bytecode
= NULL
;
3856 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
3859 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
3862 names
= dict_keys_inorder(c
->u
->u_names
, 0);
3863 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
3864 if (!consts
|| !names
|| !varnames
)
3867 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
3870 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
3873 filename
= PyString_FromString(c
->c_filename
);
3877 nlocals
= PyDict_Size(c
->u
->u_varnames
);
3878 flags
= compute_code_flags(c
);
3882 bytecode
= PyCode_Optimize(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
3886 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
3892 co
= PyCode_New(c
->u
->u_argcount
, nlocals
, stackdepth(c
), flags
,
3893 bytecode
, consts
, names
, varnames
,
3895 filename
, c
->u
->u_name
,
3896 c
->u
->u_firstlineno
,
3901 Py_XDECREF(varnames
);
3902 Py_XDECREF(filename
);
3904 Py_XDECREF(freevars
);
3905 Py_XDECREF(cellvars
);
3906 Py_XDECREF(bytecode
);
3911 /* For debugging purposes only */
3914 dump_instr(const struct instr
*i
)
3916 const char *jrel
= i
->i_jrel
? "jrel " : "";
3917 const char *jabs
= i
->i_jabs
? "jabs " : "";
3922 sprintf(arg
, "arg: %d ", i
->i_oparg
);
3924 fprintf(stderr
, "line: %d, opcode: %d %s%s%s\n",
3925 i
->i_lineno
, i
->i_opcode
, arg
, jabs
, jrel
);
3929 dump_basicblock(const basicblock
*b
)
3931 const char *seen
= b
->b_seen
? "seen " : "";
3932 const char *b_return
= b
->b_return
? "return " : "";
3933 fprintf(stderr
, "used: %d, depth: %d, offset: %d %s%s\n",
3934 b
->b_iused
, b
->b_startdepth
, b
->b_offset
, seen
, b_return
);
3937 for (i
= 0; i
< b
->b_iused
; i
++) {
3938 fprintf(stderr
, " [%02d] ", i
);
3939 dump_instr(b
->b_instr
+ i
);
3945 static PyCodeObject
*
3946 assemble(struct compiler
*c
, int addNone
)
3948 basicblock
*b
, *entryblock
;
3951 PyCodeObject
*co
= NULL
;
3953 /* Make sure every block that falls off the end returns None.
3954 XXX NEXT_BLOCK() isn't quite right, because if the last
3955 block ends with a jump or return b_next shouldn't set.
3957 if (!c
->u
->u_curblock
->b_return
) {
3960 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3961 ADDOP(c
, RETURN_VALUE
);
3966 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3971 /* Set firstlineno if it wasn't explicitly set. */
3972 if (!c
->u
->u_firstlineno
) {
3973 if (entryblock
&& entryblock
->b_instr
)
3974 c
->u
->u_firstlineno
= entryblock
->b_instr
->i_lineno
;
3976 c
->u
->u_firstlineno
= 1;
3978 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
3980 dfs(c
, entryblock
, &a
);
3982 /* Can't modify the bytecode after computing jump offsets. */
3983 assemble_jump_offsets(&a
, c
);
3985 /* Emit code in reverse postorder from dfs. */
3986 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
3987 b
= a
.a_postorder
[i
];
3988 for (j
= 0; j
< b
->b_iused
; j
++)
3989 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
3993 if (_PyString_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
3995 if (_PyString_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
3998 co
= makecode(c
, &a
);