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_LISTCOMP 1
44 #define COMP_SETCOMP 2
45 #define COMP_DICTCOMP 3
50 unsigned i_hasarg
: 1;
51 unsigned char i_opcode
;
53 struct basicblock_
*i_target
; /* target block (if jump instruction) */
57 typedef struct basicblock_
{
58 /* Each basicblock in a compilation unit is linked via b_list in the
59 reverse order that the block are allocated. b_list points to the next
60 block, not to be confused with b_next, which is next by control flow. */
61 struct basicblock_
*b_list
;
62 /* number of instructions used */
64 /* length of instruction array (b_instr) */
66 /* pointer to an array of instructions, initially NULL */
67 struct instr
*b_instr
;
68 /* If b_next is non-NULL, it is a pointer to the next
69 block reached by normal control flow. */
70 struct basicblock_
*b_next
;
71 /* b_seen is used to perform a DFS of basicblocks. */
73 /* b_return is true if a RETURN_VALUE opcode is inserted. */
74 unsigned b_return
: 1;
75 /* depth of stack upon entry of block, computed by stackdepth() */
77 /* instruction offset for block, computed by assemble_jump_offsets() */
81 /* fblockinfo tracks the current frame block.
83 A frame block is used to handle loops, try/except, and try/finally.
84 It's called a frame block to distinguish it from a basic block in the
88 enum fblocktype
{ LOOP
, EXCEPT
, FINALLY_TRY
, FINALLY_END
};
91 enum fblocktype fb_type
;
95 /* The following items change on entry and exit of code blocks.
96 They must be saved and restored when returning to a block.
98 struct compiler_unit
{
99 PySTEntryObject
*u_ste
;
102 /* The following fields are dicts that map objects to
103 the index of them in co_XXX. The index is used as
104 the argument for opcodes that refer to those collections.
106 PyObject
*u_consts
; /* all constants */
107 PyObject
*u_names
; /* all names */
108 PyObject
*u_varnames
; /* local variables */
109 PyObject
*u_cellvars
; /* cell variables */
110 PyObject
*u_freevars
; /* free variables */
112 PyObject
*u_private
; /* for private name mangling */
114 int u_argcount
; /* number of arguments for block */
115 int u_kwonlyargcount
; /* number of keyword only arguments for block */
116 /* Pointer to the most recently allocated block. By following b_list
117 members, you can reach all early allocated blocks. */
118 basicblock
*u_blocks
;
119 basicblock
*u_curblock
; /* pointer to current block */
120 int u_tmpname
; /* temporary variables for list comps */
123 struct fblockinfo u_fblock
[CO_MAXBLOCKS
];
125 int u_firstlineno
; /* the first lineno of the block */
126 int u_lineno
; /* the lineno for the current stmt */
127 int u_lineno_set
; /* boolean to indicate whether instr
128 has been generated with current lineno */
131 /* This struct captures the global state of a compilation.
133 The u pointer points to the current compilation unit, while units
134 for enclosing blocks are stored in c_stack. The u and c_stack are
135 managed by compiler_enter_scope() and compiler_exit_scope().
139 const char *c_filename
;
140 struct symtable
*c_st
;
141 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
142 PyCompilerFlags
*c_flags
;
144 int c_interactive
; /* true if in interactive mode */
147 struct compiler_unit
*u
; /* compiler state for current block */
148 PyObject
*c_stack
; /* Python list holding compiler_unit ptrs */
149 char *c_encoding
; /* source encoding (a borrowed reference) */
150 PyArena
*c_arena
; /* pointer to memory allocation arena */
153 static int compiler_enter_scope(struct compiler
*, identifier
, void *, int);
154 static void compiler_free(struct compiler
*);
155 static basicblock
*compiler_new_block(struct compiler
*);
156 static int compiler_next_instr(struct compiler
*, basicblock
*);
157 static int compiler_addop(struct compiler
*, int);
158 static int compiler_addop_o(struct compiler
*, int, PyObject
*, PyObject
*);
159 static int compiler_addop_i(struct compiler
*, int, int);
160 static int compiler_addop_j(struct compiler
*, int, basicblock
*, int);
161 static basicblock
*compiler_use_new_block(struct compiler
*);
162 static int compiler_error(struct compiler
*, const char *);
163 static int compiler_nameop(struct compiler
*, identifier
, expr_context_ty
);
165 static PyCodeObject
*compiler_mod(struct compiler
*, mod_ty
);
166 static int compiler_visit_stmt(struct compiler
*, stmt_ty
);
167 static int compiler_visit_keyword(struct compiler
*, keyword_ty
);
168 static int compiler_visit_expr(struct compiler
*, expr_ty
);
169 static int compiler_augassign(struct compiler
*, stmt_ty
);
170 static int compiler_visit_slice(struct compiler
*, slice_ty
,
173 static int compiler_push_fblock(struct compiler
*, enum fblocktype
,
175 static void compiler_pop_fblock(struct compiler
*, enum fblocktype
,
177 /* Returns true if there is a loop on the fblock stack. */
178 static int compiler_in_loop(struct compiler
*);
180 static int inplace_binop(struct compiler
*, operator_ty
);
181 static int expr_constant(expr_ty e
);
183 static int compiler_with(struct compiler
*, stmt_ty
);
184 static int compiler_call_helper(struct compiler
*c
, int n
,
190 static PyCodeObject
*assemble(struct compiler
*, int addNone
);
191 static PyObject
*__doc__
;
193 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
196 _Py_Mangle(PyObject
*privateobj
, PyObject
*ident
)
198 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */
200 const Py_UNICODE
*p
, *name
= PyUnicode_AS_UNICODE(ident
);
203 if (privateobj
== NULL
|| !PyUnicode_Check(privateobj
) ||
204 name
== NULL
|| name
[0] != '_' || name
[1] != '_') {
208 p
= PyUnicode_AS_UNICODE(privateobj
);
209 nlen
= Py_UNICODE_strlen(name
);
210 /* Don't mangle __id__ or names with dots.
212 The only time a name with a dot can occur is when
213 we are compiling an import statement that has a
216 TODO(jhylton): Decide whether we want to support
217 mangling of the module name, e.g. __M.X.
219 if ((name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
220 || Py_UNICODE_strchr(name
, '.')) {
222 return ident
; /* Don't mangle __whatever__ */
224 /* Strip leading underscores from class name */
229 return ident
; /* Don't mangle if class is just underscores */
231 plen
= Py_UNICODE_strlen(p
);
233 assert(1 <= PY_SSIZE_T_MAX
- nlen
);
234 assert(1 + nlen
<= PY_SSIZE_T_MAX
- plen
);
236 ident
= PyUnicode_FromStringAndSize(NULL
, 1 + nlen
+ plen
);
239 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
240 buffer
= PyUnicode_AS_UNICODE(ident
);
242 Py_UNICODE_strncpy(buffer
+1, p
, plen
);
243 Py_UNICODE_strcpy(buffer
+1+plen
, name
);
248 compiler_init(struct compiler
*c
)
250 memset(c
, 0, sizeof(struct compiler
));
252 c
->c_stack
= PyList_New(0);
260 PyAST_Compile(mod_ty mod
, const char *filename
, PyCompilerFlags
*flags
,
264 PyCodeObject
*co
= NULL
;
265 PyCompilerFlags local_flags
;
269 __doc__
= PyUnicode_InternFromString("__doc__");
274 if (!compiler_init(&c
))
276 c
.c_filename
= filename
;
278 c
.c_future
= PyFuture_FromAST(mod
, filename
);
279 if (c
.c_future
== NULL
)
282 local_flags
.cf_flags
= 0;
283 flags
= &local_flags
;
285 merged
= c
.c_future
->ff_features
| flags
->cf_flags
;
286 c
.c_future
->ff_features
= merged
;
287 flags
->cf_flags
= merged
;
291 c
.c_st
= PySymtable_Build(mod
, filename
, c
.c_future
);
292 if (c
.c_st
== NULL
) {
293 if (!PyErr_Occurred())
294 PyErr_SetString(PyExc_SystemError
, "no symtable");
298 /* XXX initialize to NULL for now, need to handle */
301 co
= compiler_mod(&c
, mod
);
305 assert(co
|| PyErr_Occurred());
310 PyNode_Compile(struct _node
*n
, const char *filename
)
312 PyCodeObject
*co
= NULL
;
314 PyArena
*arena
= PyArena_New();
317 mod
= PyAST_FromNode(n
, NULL
, filename
, arena
);
319 co
= PyAST_Compile(mod
, filename
, NULL
, arena
);
325 compiler_free(struct compiler
*c
)
328 PySymtable_Free(c
->c_st
);
330 PyObject_Free(c
->c_future
);
331 Py_DECREF(c
->c_stack
);
335 list2dict(PyObject
*list
)
339 PyObject
*dict
= PyDict_New();
340 if (!dict
) return NULL
;
342 n
= PyList_Size(list
);
343 for (i
= 0; i
< n
; i
++) {
344 v
= PyLong_FromLong(i
);
349 k
= PyList_GET_ITEM(list
, i
);
350 k
= PyTuple_Pack(2, k
, k
->ob_type
);
351 if (k
== NULL
|| PyDict_SetItem(dict
, k
, v
) < 0) {
363 /* Return new dict containing names from src that match scope(s).
365 src is a symbol table dictionary. If the scope of a name matches
366 either scope_type or flag is set, insert it into the new dict. The
367 values are integers, starting at offset and increasing by one for
372 dictbytype(PyObject
*src
, int scope_type
, int flag
, int offset
)
374 Py_ssize_t pos
= 0, i
= offset
, scope
;
375 PyObject
*k
, *v
, *dest
= PyDict_New();
381 while (PyDict_Next(src
, &pos
, &k
, &v
)) {
382 /* XXX this should probably be a macro in symtable.h */
384 assert(PyLong_Check(v
));
385 vi
= PyLong_AS_LONG(v
);
386 scope
= (vi
>> SCOPE_OFFSET
) & SCOPE_MASK
;
388 if (scope
== scope_type
|| vi
& flag
) {
389 PyObject
*tuple
, *item
= PyLong_FromLong(i
);
395 tuple
= PyTuple_Pack(2, k
, k
->ob_type
);
396 if (!tuple
|| PyDict_SetItem(dest
, tuple
, item
) < 0) {
410 compiler_unit_check(struct compiler_unit
*u
)
413 for (block
= u
->u_blocks
; block
!= NULL
; block
= block
->b_list
) {
414 assert((void *)block
!= (void *)0xcbcbcbcb);
415 assert((void *)block
!= (void *)0xfbfbfbfb);
416 assert((void *)block
!= (void *)0xdbdbdbdb);
417 if (block
->b_instr
!= NULL
) {
418 assert(block
->b_ialloc
> 0);
419 assert(block
->b_iused
> 0);
420 assert(block
->b_ialloc
>= block
->b_iused
);
423 assert (block
->b_iused
== 0);
424 assert (block
->b_ialloc
== 0);
430 compiler_unit_free(struct compiler_unit
*u
)
432 basicblock
*b
, *next
;
434 compiler_unit_check(u
);
438 PyObject_Free((void *)b
->b_instr
);
440 PyObject_Free((void *)b
);
445 Py_CLEAR(u
->u_consts
);
446 Py_CLEAR(u
->u_names
);
447 Py_CLEAR(u
->u_varnames
);
448 Py_CLEAR(u
->u_freevars
);
449 Py_CLEAR(u
->u_cellvars
);
450 Py_CLEAR(u
->u_private
);
455 compiler_enter_scope(struct compiler
*c
, identifier name
, void *key
,
458 struct compiler_unit
*u
;
460 u
= (struct compiler_unit
*)PyObject_Malloc(sizeof(
461 struct compiler_unit
));
466 memset(u
, 0, sizeof(struct compiler_unit
));
468 u
->u_kwonlyargcount
= 0;
469 u
->u_ste
= PySymtable_Lookup(c
->c_st
, key
);
471 compiler_unit_free(u
);
476 u
->u_varnames
= list2dict(u
->u_ste
->ste_varnames
);
477 u
->u_cellvars
= dictbytype(u
->u_ste
->ste_symbols
, CELL
, 0, 0);
478 if (!u
->u_varnames
|| !u
->u_cellvars
) {
479 compiler_unit_free(u
);
483 u
->u_freevars
= dictbytype(u
->u_ste
->ste_symbols
, FREE
, DEF_FREE_CLASS
,
484 PyDict_Size(u
->u_cellvars
));
485 if (!u
->u_freevars
) {
486 compiler_unit_free(u
);
493 u
->u_firstlineno
= lineno
;
496 u
->u_consts
= PyDict_New();
498 compiler_unit_free(u
);
501 u
->u_names
= PyDict_New();
503 compiler_unit_free(u
);
509 /* Push the old compiler_unit on the stack. */
511 PyObject
*capsule
= PyCapsule_New(c
->u
, COMPILER_CAPSULE_NAME_COMPILER_UNIT
, NULL
);
512 if (!capsule
|| PyList_Append(c
->c_stack
, capsule
) < 0) {
514 compiler_unit_free(u
);
518 u
->u_private
= c
->u
->u_private
;
519 Py_XINCREF(u
->u_private
);
524 if (compiler_use_new_block(c
) == NULL
)
531 compiler_exit_scope(struct compiler
*c
)
537 compiler_unit_free(c
->u
);
538 /* Restore c->u to the parent unit. */
539 n
= PyList_GET_SIZE(c
->c_stack
) - 1;
541 capsule
= PyList_GET_ITEM(c
->c_stack
, n
);
542 c
->u
= (struct compiler_unit
*)PyCapsule_GetPointer(capsule
, COMPILER_CAPSULE_NAME_COMPILER_UNIT
);
544 /* we are deleting from a list so this really shouldn't fail */
545 if (PySequence_DelItem(c
->c_stack
, n
) < 0)
546 Py_FatalError("compiler_exit_scope()");
547 compiler_unit_check(c
->u
);
554 /* Allocate a new "anonymous" local variable. Used by with statements. */
557 compiler_new_tmpname(struct compiler
*c
)
560 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->u
->u_tmpname
);
561 return PyUnicode_FromString(tmpname
);
564 /* Allocate a new block and return a pointer to it.
565 Returns NULL on error.
569 compiler_new_block(struct compiler
*c
)
572 struct compiler_unit
*u
;
575 b
= (basicblock
*)PyObject_Malloc(sizeof(basicblock
));
580 memset((void *)b
, 0, sizeof(basicblock
));
581 /* Extend the singly linked list of blocks with new block. */
582 b
->b_list
= u
->u_blocks
;
588 compiler_use_new_block(struct compiler
*c
)
590 basicblock
*block
= compiler_new_block(c
);
593 c
->u
->u_curblock
= block
;
598 compiler_next_block(struct compiler
*c
)
600 basicblock
*block
= compiler_new_block(c
);
603 c
->u
->u_curblock
->b_next
= block
;
604 c
->u
->u_curblock
= block
;
609 compiler_use_next_block(struct compiler
*c
, basicblock
*block
)
611 assert(block
!= NULL
);
612 c
->u
->u_curblock
->b_next
= block
;
613 c
->u
->u_curblock
= block
;
617 /* Returns the offset of the next instruction in the current block's
618 b_instr array. Resizes the b_instr as necessary.
619 Returns -1 on failure.
623 compiler_next_instr(struct compiler
*c
, basicblock
*b
)
626 if (b
->b_instr
== NULL
) {
627 b
->b_instr
= (struct instr
*)PyObject_Malloc(
628 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
629 if (b
->b_instr
== NULL
) {
633 b
->b_ialloc
= DEFAULT_BLOCK_SIZE
;
634 memset((char *)b
->b_instr
, 0,
635 sizeof(struct instr
) * DEFAULT_BLOCK_SIZE
);
637 else if (b
->b_iused
== b
->b_ialloc
) {
639 size_t oldsize
, newsize
;
640 oldsize
= b
->b_ialloc
* sizeof(struct instr
);
641 newsize
= oldsize
<< 1;
643 if (oldsize
> (PY_SIZE_MAX
>> 1)) {
653 tmp
= (struct instr
*)PyObject_Realloc(
654 (void *)b
->b_instr
, newsize
);
660 memset((char *)b
->b_instr
+ oldsize
, 0, newsize
- oldsize
);
665 /* Set the i_lineno member of the instruction at offset off if the
666 line number for the current expression/statement has not
667 already been set. If it has been set, the call has no effect.
669 The line number is reset in the following cases:
670 - when entering a new scope
672 - on each expression that start a new line
673 - before the "except" clause
674 - before the "for" and "while" expressions
678 compiler_set_lineno(struct compiler
*c
, int off
)
681 if (c
->u
->u_lineno_set
)
683 c
->u
->u_lineno_set
= 1;
684 b
= c
->u
->u_curblock
;
685 b
->b_instr
[off
].i_lineno
= c
->u
->u_lineno
;
689 opcode_stack_effect(int opcode
, int oparg
)
715 case BINARY_MULTIPLY
:
718 case BINARY_SUBTRACT
:
720 case BINARY_FLOOR_DIVIDE
:
721 case BINARY_TRUE_DIVIDE
:
723 case INPLACE_FLOOR_DIVIDE
:
724 case INPLACE_TRUE_DIVIDE
:
728 case INPLACE_SUBTRACT
:
729 case INPLACE_MULTIPLY
:
752 case LOAD_BUILD_CLASS
:
763 return -1; /* XXX Sometimes more */
776 return 0; /* -3 except if bad bytecode */
778 return -1; /* or -2 or -3 if exception occurred */
784 case UNPACK_SEQUENCE
:
787 return (oparg
&0xFF) + (oparg
>>8);
821 case JUMP_IF_TRUE_OR_POP
: /* -1 if jump not taken */
822 case JUMP_IF_FALSE_OR_POP
: /* "" */
826 case POP_JUMP_IF_FALSE
:
827 case POP_JUMP_IF_TRUE
:
839 return 6; /* can push 3 values for the new exception
840 + 3 others for the previous exception state */
851 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
853 return -NARGS(oparg
);
854 case CALL_FUNCTION_VAR
:
855 case CALL_FUNCTION_KW
:
856 return -NARGS(oparg
)-1;
857 case CALL_FUNCTION_VAR_KW
:
858 return -NARGS(oparg
)-2;
860 return -NARGS(oparg
) - ((oparg
>> 16) & 0xffff);
862 return -1 - NARGS(oparg
) - ((oparg
>> 16) & 0xffff);
877 fprintf(stderr
, "opcode = %d\n", opcode
);
878 Py_FatalError("opcode_stack_effect()");
881 return 0; /* not reachable */
884 /* Add an opcode with no argument.
885 Returns 0 on failure, 1 on success.
889 compiler_addop(struct compiler
*c
, int opcode
)
894 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
897 b
= c
->u
->u_curblock
;
898 i
= &b
->b_instr
[off
];
899 i
->i_opcode
= opcode
;
901 if (opcode
== RETURN_VALUE
)
903 compiler_set_lineno(c
, off
);
908 compiler_add_o(struct compiler
*c
, PyObject
*dict
, PyObject
*o
)
912 unsigned char *p
, *q
;
915 int real_part_zero
, imag_part_zero
;
917 /* necessary to make sure types aren't coerced (e.g., int and long) */
918 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
919 if (PyFloat_Check(o
)) {
920 d
= PyFloat_AS_DOUBLE(o
);
921 p
= (unsigned char*) &d
;
922 /* all we need is to make the tuple different in either the 0.0
923 * or -0.0 case from all others, just to avoid the "coercion".
925 if (*p
==0 && p
[sizeof(double)-1]==0)
926 t
= PyTuple_Pack(3, o
, o
->ob_type
, Py_None
);
928 t
= PyTuple_Pack(2, o
, o
->ob_type
);
930 else if (PyComplex_Check(o
)) {
931 /* complex case is even messier: we need to make complex(x,
932 0.) different from complex(x, -0.) and complex(0., y)
933 different from complex(-0., y), for any x and y. In
934 particular, all four complex zeros should be
936 z
= PyComplex_AsCComplex(o
);
937 p
= (unsigned char*) &(z
.real
);
938 q
= (unsigned char*) &(z
.imag
);
939 /* all that matters here is that on IEEE platforms
940 real_part_zero will be true if z.real == 0., and false if
941 z.real == -0. In fact, real_part_zero will also be true
942 for some other rarely occurring nonzero floats, but this
943 doesn't matter. Similar comments apply to
945 real_part_zero
= *p
==0 && p
[sizeof(double)-1]==0;
946 imag_part_zero
= *q
==0 && q
[sizeof(double)-1]==0;
947 if (real_part_zero
&& imag_part_zero
) {
948 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_True
, Py_True
);
950 else if (real_part_zero
&& !imag_part_zero
) {
951 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_True
, Py_False
);
953 else if (!real_part_zero
&& imag_part_zero
) {
954 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_False
, Py_True
);
957 t
= PyTuple_Pack(2, o
, o
->ob_type
);
961 t
= PyTuple_Pack(2, o
, o
->ob_type
);
966 v
= PyDict_GetItem(dict
, t
);
968 if (PyErr_Occurred())
970 arg
= PyDict_Size(dict
);
971 v
= PyLong_FromLong(arg
);
976 if (PyDict_SetItem(dict
, t
, v
) < 0) {
984 arg
= PyLong_AsLong(v
);
990 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
993 int arg
= compiler_add_o(c
, dict
, o
);
996 return compiler_addop_i(c
, opcode
, arg
);
1000 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
1004 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
1007 arg
= compiler_add_o(c
, dict
, mangled
);
1011 return compiler_addop_i(c
, opcode
, arg
);
1014 /* Add an opcode with an integer argument.
1015 Returns 0 on failure, 1 on success.
1019 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
1023 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1026 i
= &c
->u
->u_curblock
->b_instr
[off
];
1027 i
->i_opcode
= opcode
;
1030 compiler_set_lineno(c
, off
);
1035 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
1041 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1044 i
= &c
->u
->u_curblock
->b_instr
[off
];
1045 i
->i_opcode
= opcode
;
1052 compiler_set_lineno(c
, off
);
1056 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1057 like to find better names.) NEW_BLOCK() creates a new block and sets
1058 it as the current block. NEXT_BLOCK() also creates an implicit jump
1059 from the current block to the new block.
1062 /* The returns inside these macros make it impossible to decref objects
1063 created in the local function. Local objects should use the arena.
1067 #define NEW_BLOCK(C) { \
1068 if (compiler_use_new_block((C)) == NULL) \
1072 #define NEXT_BLOCK(C) { \
1073 if (compiler_next_block((C)) == NULL) \
1077 #define ADDOP(C, OP) { \
1078 if (!compiler_addop((C), (OP))) \
1082 #define ADDOP_IN_SCOPE(C, OP) { \
1083 if (!compiler_addop((C), (OP))) { \
1084 compiler_exit_scope(c); \
1089 #define ADDOP_O(C, OP, O, TYPE) { \
1090 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1094 #define ADDOP_NAME(C, OP, O, TYPE) { \
1095 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1099 #define ADDOP_I(C, OP, O) { \
1100 if (!compiler_addop_i((C), (OP), (O))) \
1104 #define ADDOP_JABS(C, OP, O) { \
1105 if (!compiler_addop_j((C), (OP), (O), 1)) \
1109 #define ADDOP_JREL(C, OP, O) { \
1110 if (!compiler_addop_j((C), (OP), (O), 0)) \
1114 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1115 the ASDL name to synthesize the name of the C type and the visit function.
1118 #define VISIT(C, TYPE, V) {\
1119 if (!compiler_visit_ ## TYPE((C), (V))) \
1123 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1124 if (!compiler_visit_ ## TYPE((C), (V))) { \
1125 compiler_exit_scope(c); \
1130 #define VISIT_SLICE(C, V, CTX) {\
1131 if (!compiler_visit_slice((C), (V), (CTX))) \
1135 #define VISIT_SEQ(C, TYPE, SEQ) { \
1137 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1138 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1139 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1140 if (!compiler_visit_ ## TYPE((C), elt)) \
1145 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1147 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1148 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1149 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1150 if (!compiler_visit_ ## TYPE((C), elt)) { \
1151 compiler_exit_scope(c); \
1158 compiler_isdocstring(stmt_ty s
)
1160 if (s
->kind
!= Expr_kind
)
1162 return s
->v
.Expr
.value
->kind
== Str_kind
;
1165 /* Compile a sequence of statements, checking for a docstring. */
1168 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1173 if (!asdl_seq_LEN(stmts
))
1175 st
= (stmt_ty
)asdl_seq_GET(stmts
, 0);
1176 if (compiler_isdocstring(st
) && Py_OptimizeFlag
< 2) {
1177 /* don't generate docstrings if -OO */
1179 VISIT(c
, expr
, st
->v
.Expr
.value
);
1180 if (!compiler_nameop(c
, __doc__
, Store
))
1183 for (; i
< asdl_seq_LEN(stmts
); i
++)
1184 VISIT(c
, stmt
, (stmt_ty
)asdl_seq_GET(stmts
, i
));
1188 static PyCodeObject
*
1189 compiler_mod(struct compiler
*c
, mod_ty mod
)
1193 static PyObject
*module
;
1195 module
= PyUnicode_InternFromString("<module>");
1199 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1200 if (!compiler_enter_scope(c
, module
, mod
, 0))
1202 switch (mod
->kind
) {
1204 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1205 compiler_exit_scope(c
);
1209 case Interactive_kind
:
1210 c
->c_interactive
= 1;
1211 VISIT_SEQ_IN_SCOPE(c
, stmt
,
1212 mod
->v
.Interactive
.body
);
1214 case Expression_kind
:
1215 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1219 PyErr_SetString(PyExc_SystemError
,
1220 "suite should not be possible");
1223 PyErr_Format(PyExc_SystemError
,
1224 "module kind %d should not be possible",
1228 co
= assemble(c
, addNone
);
1229 compiler_exit_scope(c
);
1233 /* The test for LOCAL must come before the test for FREE in order to
1234 handle classes where name is both local and free. The local var is
1235 a method and the free var is a free var referenced within a method.
1239 get_ref_type(struct compiler
*c
, PyObject
*name
)
1241 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1244 PyOS_snprintf(buf
, sizeof(buf
),
1245 "unknown scope for %.100s in %.100s(%s) in %s\n"
1246 "symbols: %s\nlocals: %s\nglobals: %s",
1247 PyBytes_AS_STRING(name
),
1248 PyBytes_AS_STRING(c
->u
->u_name
),
1249 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1251 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1252 PyObject_REPR(c
->u
->u_varnames
),
1253 PyObject_REPR(c
->u
->u_names
)
1262 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1265 k
= PyTuple_Pack(2, name
, name
->ob_type
);
1268 v
= PyDict_GetItem(dict
, k
);
1272 return PyLong_AS_LONG(v
);
1276 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1278 int i
, free
= PyCode_GetNumFree(co
);
1280 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1281 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1284 for (i
= 0; i
< free
; ++i
) {
1285 /* Bypass com_addop_varname because it will generate
1286 LOAD_DEREF but LOAD_CLOSURE is needed.
1288 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1291 /* Special case: If a class contains a method with a
1292 free variable that has the same name as a method,
1293 the name will be considered free *and* local in the
1294 class. It should be handled by the closure, as
1295 well as by the normal name loookup logic.
1297 reftype
= get_ref_type(c
, name
);
1298 if (reftype
== CELL
)
1299 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1300 else /* (reftype == FREE) */
1301 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1304 "lookup %s in %s %d %d\n"
1305 "freevars of %s: %s\n",
1306 PyObject_REPR(name
),
1307 PyBytes_AS_STRING(c
->u
->u_name
),
1309 _PyUnicode_AsString(co
->co_name
),
1310 PyObject_REPR(co
->co_freevars
));
1311 Py_FatalError("compiler_make_closure()");
1313 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1315 ADDOP_I(c
, BUILD_TUPLE
, free
);
1316 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1317 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1322 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1329 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1330 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(decos
, i
));
1336 compiler_visit_kwonlydefaults(struct compiler
*c
, asdl_seq
*kwonlyargs
,
1337 asdl_seq
*kw_defaults
)
1339 int i
, default_count
= 0;
1340 for (i
= 0; i
< asdl_seq_LEN(kwonlyargs
); i
++) {
1341 arg_ty arg
= asdl_seq_GET(kwonlyargs
, i
);
1342 expr_ty default_
= asdl_seq_GET(kw_defaults
, i
);
1344 ADDOP_O(c
, LOAD_CONST
, arg
->arg
, consts
);
1345 if (!compiler_visit_expr(c
, default_
)) {
1351 return default_count
;
1355 compiler_visit_argannotation(struct compiler
*c
, identifier id
,
1356 expr_ty annotation
, PyObject
*names
)
1359 VISIT(c
, expr
, annotation
);
1360 if (PyList_Append(names
, id
))
1367 compiler_visit_argannotations(struct compiler
*c
, asdl_seq
* args
,
1371 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1372 arg_ty arg
= (arg_ty
)asdl_seq_GET(args
, i
);
1373 error
= compiler_visit_argannotation(
1385 compiler_visit_annotations(struct compiler
*c
, arguments_ty args
,
1388 /* Push arg annotations and a list of the argument names. Return the #
1389 of items pushed. The expressions are evaluated out-of-order wrt the
1392 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1394 static identifier return_str
;
1397 names
= PyList_New(0);
1401 if (compiler_visit_argannotations(c
, args
->args
, names
))
1403 if (args
->varargannotation
&&
1404 compiler_visit_argannotation(c
, args
->vararg
,
1405 args
->varargannotation
, names
))
1407 if (compiler_visit_argannotations(c
, args
->kwonlyargs
, names
))
1409 if (args
->kwargannotation
&&
1410 compiler_visit_argannotation(c
, args
->kwarg
,
1411 args
->kwargannotation
, names
))
1415 return_str
= PyUnicode_InternFromString("return");
1419 if (compiler_visit_argannotation(c
, return_str
, returns
, names
)) {
1423 len
= PyList_GET_SIZE(names
);
1425 /* len must fit in 16 bits, and len is incremented below */
1426 PyErr_SetString(PyExc_SyntaxError
,
1427 "too many annotations");
1431 /* convert names to a tuple and place on stack */
1434 PyObject
*s
= PyTuple_New(len
);
1437 for (i
= 0; i
< len
; i
++) {
1438 elt
= PyList_GET_ITEM(names
, i
);
1440 PyTuple_SET_ITEM(s
, i
, elt
);
1442 ADDOP_O(c
, LOAD_CONST
, s
, consts
);
1444 len
++; /* include the just-pushed tuple */
1455 compiler_function(struct compiler
*c
, stmt_ty s
)
1458 PyObject
*first_const
= Py_None
;
1459 arguments_ty args
= s
->v
.FunctionDef
.args
;
1460 expr_ty returns
= s
->v
.FunctionDef
.returns
;
1461 asdl_seq
* decos
= s
->v
.FunctionDef
.decorator_list
;
1463 int i
, n
, docstring
, kw_default_count
= 0, arglength
;
1464 int num_annotations
;
1466 assert(s
->kind
== FunctionDef_kind
);
1468 if (!compiler_decorators(c
, decos
))
1470 if (args
->kwonlyargs
) {
1471 int res
= compiler_visit_kwonlydefaults(c
, args
->kwonlyargs
,
1475 kw_default_count
= res
;
1478 VISIT_SEQ(c
, expr
, args
->defaults
);
1479 num_annotations
= compiler_visit_annotations(c
, args
, returns
);
1480 if (num_annotations
< 0)
1482 assert((num_annotations
& 0xFFFF) == num_annotations
);
1484 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1488 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1489 docstring
= compiler_isdocstring(st
);
1490 if (docstring
&& Py_OptimizeFlag
< 2)
1491 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1492 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1493 compiler_exit_scope(c
);
1497 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1498 c
->u
->u_kwonlyargcount
= asdl_seq_LEN(args
->kwonlyargs
);
1499 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1500 /* if there was a docstring, we need to skip the first statement */
1501 for (i
= docstring
; i
< n
; i
++) {
1502 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1503 VISIT_IN_SCOPE(c
, stmt
, st
);
1505 co
= assemble(c
, 1);
1506 compiler_exit_scope(c
);
1510 arglength
= asdl_seq_LEN(args
->defaults
);
1511 arglength
|= kw_default_count
<< 8;
1512 arglength
|= num_annotations
<< 16;
1513 compiler_make_closure(c
, co
, arglength
);
1517 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1518 ADDOP_I(c
, CALL_FUNCTION
, 1);
1521 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1525 compiler_class(struct compiler
*c
, stmt_ty s
)
1530 asdl_seq
* decos
= s
->v
.ClassDef
.decorator_list
;
1532 if (!compiler_decorators(c
, decos
))
1535 /* ultimately generate code for:
1536 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1538 <func> is a function/closure created from the class body;
1539 it has a single argument (__locals__) where the dict
1540 (or MutableSequence) representing the locals is passed
1541 <name> is the class name
1542 <bases> is the positional arguments and *varargs argument
1543 <keywords> is the keyword arguments and **kwds argument
1544 This borrows from compiler_call.
1547 /* 1. compile the class body into a code object */
1548 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
, s
->lineno
))
1550 /* this block represents what we do in the new scope */
1552 /* use the class name for name mangling */
1553 Py_INCREF(s
->v
.ClassDef
.name
);
1554 Py_XDECREF(c
->u
->u_private
);
1555 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1556 /* force it to have one mandatory argument */
1557 c
->u
->u_argcount
= 1;
1558 /* load the first argument (__locals__) ... */
1559 ADDOP_I(c
, LOAD_FAST
, 0);
1560 /* ... and store it into f_locals */
1561 ADDOP_IN_SCOPE(c
, STORE_LOCALS
);
1562 /* load (global) __name__ ... */
1563 str
= PyUnicode_InternFromString("__name__");
1564 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1566 compiler_exit_scope(c
);
1570 /* ... and store it as __module__ */
1571 str
= PyUnicode_InternFromString("__module__");
1572 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
1574 compiler_exit_scope(c
);
1578 /* compile the body proper */
1579 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
1580 compiler_exit_scope(c
);
1583 /* return the (empty) __class__ cell */
1584 str
= PyUnicode_InternFromString("__class__");
1586 compiler_exit_scope(c
);
1589 i
= compiler_lookup_arg(c
->u
->u_cellvars
, str
);
1592 /* This happens when nobody references the cell */
1595 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1598 /* Return the cell where to store __class__ */
1599 ADDOP_I(c
, LOAD_CLOSURE
, i
);
1601 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1602 /* create the code object */
1603 co
= assemble(c
, 1);
1605 /* leave the new scope */
1606 compiler_exit_scope(c
);
1610 /* 2. load the 'build_class' function */
1611 ADDOP(c
, LOAD_BUILD_CLASS
);
1613 /* 3. load a function (or closure) made from the code object */
1614 compiler_make_closure(c
, co
, 0);
1617 /* 4. load class name */
1618 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1620 /* 5. generate the rest of the code for the call */
1621 if (!compiler_call_helper(c
, 2,
1622 s
->v
.ClassDef
.bases
,
1623 s
->v
.ClassDef
.keywords
,
1624 s
->v
.ClassDef
.starargs
,
1625 s
->v
.ClassDef
.kwargs
))
1628 /* 6. apply decorators */
1629 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1630 ADDOP_I(c
, CALL_FUNCTION
, 1);
1633 /* 7. store into <name> */
1634 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
1640 compiler_ifexp(struct compiler
*c
, expr_ty e
)
1642 basicblock
*end
, *next
;
1644 assert(e
->kind
== IfExp_kind
);
1645 end
= compiler_new_block(c
);
1648 next
= compiler_new_block(c
);
1651 VISIT(c
, expr
, e
->v
.IfExp
.test
);
1652 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, next
);
1653 VISIT(c
, expr
, e
->v
.IfExp
.body
);
1654 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1655 compiler_use_next_block(c
, next
);
1656 VISIT(c
, expr
, e
->v
.IfExp
.orelse
);
1657 compiler_use_next_block(c
, end
);
1662 compiler_lambda(struct compiler
*c
, expr_ty e
)
1665 static identifier name
;
1666 int kw_default_count
= 0, arglength
;
1667 arguments_ty args
= e
->v
.Lambda
.args
;
1668 assert(e
->kind
== Lambda_kind
);
1671 name
= PyUnicode_InternFromString("<lambda>");
1676 if (args
->kwonlyargs
) {
1677 int res
= compiler_visit_kwonlydefaults(c
, args
->kwonlyargs
,
1679 if (res
< 0) return 0;
1680 kw_default_count
= res
;
1683 VISIT_SEQ(c
, expr
, args
->defaults
);
1684 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
1687 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1688 c
->u
->u_kwonlyargcount
= asdl_seq_LEN(args
->kwonlyargs
);
1689 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
1690 if (c
->u
->u_ste
->ste_generator
) {
1691 ADDOP_IN_SCOPE(c
, POP_TOP
);
1694 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1696 co
= assemble(c
, 1);
1697 compiler_exit_scope(c
);
1701 arglength
= asdl_seq_LEN(args
->defaults
);
1702 arglength
|= kw_default_count
<< 8;
1703 compiler_make_closure(c
, co
, arglength
);
1710 compiler_if(struct compiler
*c
, stmt_ty s
)
1712 basicblock
*end
, *next
;
1714 assert(s
->kind
== If_kind
);
1715 end
= compiler_new_block(c
);
1719 constant
= expr_constant(s
->v
.If
.test
);
1720 /* constant = 0: "if 0"
1721 * constant = 1: "if 1", "if 2", ...
1722 * constant = -1: rest */
1723 if (constant
== 0) {
1725 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1726 } else if (constant
== 1) {
1727 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1729 if (s
->v
.If
.orelse
) {
1730 next
= compiler_new_block(c
);
1736 VISIT(c
, expr
, s
->v
.If
.test
);
1737 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, next
);
1738 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1739 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1740 if (s
->v
.If
.orelse
) {
1741 compiler_use_next_block(c
, next
);
1742 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1745 compiler_use_next_block(c
, end
);
1750 compiler_for(struct compiler
*c
, stmt_ty s
)
1752 basicblock
*start
, *cleanup
, *end
;
1754 start
= compiler_new_block(c
);
1755 cleanup
= compiler_new_block(c
);
1756 end
= compiler_new_block(c
);
1757 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
1759 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1760 if (!compiler_push_fblock(c
, LOOP
, start
))
1762 VISIT(c
, expr
, s
->v
.For
.iter
);
1764 compiler_use_next_block(c
, start
);
1765 /* for expressions must be traced on each iteration,
1766 so we need to set an extra line number. */
1767 c
->u
->u_lineno_set
= 0;
1768 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
1769 VISIT(c
, expr
, s
->v
.For
.target
);
1770 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
1771 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
1772 compiler_use_next_block(c
, cleanup
);
1773 ADDOP(c
, POP_BLOCK
);
1774 compiler_pop_fblock(c
, LOOP
, start
);
1775 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
1776 compiler_use_next_block(c
, end
);
1781 compiler_while(struct compiler
*c
, stmt_ty s
)
1783 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
1784 int constant
= expr_constant(s
->v
.While
.test
);
1786 if (constant
== 0) {
1787 if (s
->v
.While
.orelse
)
1788 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1791 loop
= compiler_new_block(c
);
1792 end
= compiler_new_block(c
);
1793 if (constant
== -1) {
1794 anchor
= compiler_new_block(c
);
1798 if (loop
== NULL
|| end
== NULL
)
1800 if (s
->v
.While
.orelse
) {
1801 orelse
= compiler_new_block(c
);
1808 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1809 compiler_use_next_block(c
, loop
);
1810 if (!compiler_push_fblock(c
, LOOP
, loop
))
1812 if (constant
== -1) {
1813 /* while expressions must be traced on each iteration,
1814 so we need to set an extra line number. */
1815 c
->u
->u_lineno_set
= 0;
1816 VISIT(c
, expr
, s
->v
.While
.test
);
1817 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, anchor
);
1819 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
1820 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
1822 /* XXX should the two POP instructions be in a separate block
1823 if there is no else clause ?
1826 if (constant
== -1) {
1827 compiler_use_next_block(c
, anchor
);
1828 ADDOP(c
, POP_BLOCK
);
1830 compiler_pop_fblock(c
, LOOP
, loop
);
1831 if (orelse
!= NULL
) /* what if orelse is just pass? */
1832 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1833 compiler_use_next_block(c
, end
);
1839 compiler_continue(struct compiler
*c
)
1841 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
1842 static const char IN_FINALLY_ERROR_MSG
[] =
1843 "'continue' not supported inside 'finally' clause";
1846 if (!c
->u
->u_nfblocks
)
1847 return compiler_error(c
, LOOP_ERROR_MSG
);
1848 i
= c
->u
->u_nfblocks
- 1;
1849 switch (c
->u
->u_fblock
[i
].fb_type
) {
1851 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
1855 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
) {
1856 /* Prevent continue anywhere under a finally
1857 even if hidden in a sub-try or except. */
1858 if (c
->u
->u_fblock
[i
].fb_type
== FINALLY_END
)
1859 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1862 return compiler_error(c
, LOOP_ERROR_MSG
);
1863 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
1866 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1872 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1878 L: <code for finalbody>
1881 The special instructions use the block stack. Each block
1882 stack entry contains the instruction that created it (here
1883 SETUP_FINALLY), the level of the value stack at the time the
1884 block stack entry was created, and a label (here L).
1887 Pushes the current value stack level and the label
1888 onto the block stack.
1890 Pops en entry from the block stack, and pops the value
1891 stack until its level is the same as indicated on the
1892 block stack. (The label is ignored.)
1894 Pops a variable number of entries from the *value* stack
1895 and re-raises the exception they specify. The number of
1896 entries popped depends on the (pseudo) exception type.
1898 The block stack is unwound when an exception is raised:
1899 when a SETUP_FINALLY entry is found, the exception is pushed
1900 onto the value stack (and the exception condition is cleared),
1901 and the interpreter jumps to the label gotten from the block
1906 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
1908 basicblock
*body
, *end
;
1909 body
= compiler_new_block(c
);
1910 end
= compiler_new_block(c
);
1911 if (body
== NULL
|| end
== NULL
)
1914 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
1915 compiler_use_next_block(c
, body
);
1916 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
1918 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
1919 ADDOP(c
, POP_BLOCK
);
1920 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
1922 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1923 compiler_use_next_block(c
, end
);
1924 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
1926 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
1927 ADDOP(c
, END_FINALLY
);
1928 compiler_pop_fblock(c
, FINALLY_END
, end
);
1934 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
1935 (The contents of the value stack is shown in [], with the top
1936 at the right; 'tb' is trace-back info, 'val' the exception's
1937 associated value, and 'exc' the exception.)
1939 Value stack Label Instruction Argument
1945 [tb, val, exc] L1: DUP )
1946 [tb, val, exc, exc] <evaluate E1> )
1947 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1948 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1950 [tb, val] <assign to V1> (or POP if no V1)
1955 [tb, val, exc] L2: DUP
1956 .............................etc.......................
1958 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1960 [] L0: <next statement>
1962 Of course, parts are not generated if Vi or Ei is not present.
1965 compiler_try_except(struct compiler
*c
, stmt_ty s
)
1967 basicblock
*body
, *orelse
, *except
, *end
;
1970 body
= compiler_new_block(c
);
1971 except
= compiler_new_block(c
);
1972 orelse
= compiler_new_block(c
);
1973 end
= compiler_new_block(c
);
1974 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
1976 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
1977 compiler_use_next_block(c
, body
);
1978 if (!compiler_push_fblock(c
, EXCEPT
, body
))
1980 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
1981 ADDOP(c
, POP_BLOCK
);
1982 compiler_pop_fblock(c
, EXCEPT
, body
);
1983 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
1984 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
1985 compiler_use_next_block(c
, except
);
1986 for (i
= 0; i
< n
; i
++) {
1987 excepthandler_ty handler
= (excepthandler_ty
)asdl_seq_GET(
1988 s
->v
.TryExcept
.handlers
, i
);
1989 if (!handler
->v
.ExceptHandler
.type
&& i
< n
-1)
1990 return compiler_error(c
, "default 'except:' must be last");
1991 c
->u
->u_lineno_set
= 0;
1992 c
->u
->u_lineno
= handler
->lineno
;
1993 except
= compiler_new_block(c
);
1996 if (handler
->v
.ExceptHandler
.type
) {
1998 VISIT(c
, expr
, handler
->v
.ExceptHandler
.type
);
1999 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
2000 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, except
);
2003 if (handler
->v
.ExceptHandler
.name
) {
2004 basicblock
*cleanup_end
, *cleanup_body
;
2006 cleanup_end
= compiler_new_block(c
);
2007 cleanup_body
= compiler_new_block(c
);
2008 if(!(cleanup_end
|| cleanup_body
))
2011 compiler_nameop(c
, handler
->v
.ExceptHandler
.name
, Store
);
2017 except type as name:
2026 ADDOP_JREL(c
, SETUP_FINALLY
, cleanup_end
);
2027 compiler_use_next_block(c
, cleanup_body
);
2028 if (!compiler_push_fblock(c
, FINALLY_TRY
, cleanup_body
))
2032 VISIT_SEQ(c
, stmt
, handler
->v
.ExceptHandler
.body
);
2033 ADDOP(c
, POP_BLOCK
);
2034 ADDOP(c
, POP_EXCEPT
);
2035 compiler_pop_fblock(c
, FINALLY_TRY
, cleanup_body
);
2038 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2039 compiler_use_next_block(c
, cleanup_end
);
2040 if (!compiler_push_fblock(c
, FINALLY_END
, cleanup_end
))
2044 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2045 compiler_nameop(c
, handler
->v
.ExceptHandler
.name
, Store
);
2048 compiler_nameop(c
, handler
->v
.ExceptHandler
.name
, Del
);
2050 ADDOP(c
, END_FINALLY
);
2051 compiler_pop_fblock(c
, FINALLY_END
, cleanup_end
);
2054 basicblock
*cleanup_body
;
2056 cleanup_body
= compiler_new_block(c
);
2062 compiler_use_next_block(c
, cleanup_body
);
2063 if (!compiler_push_fblock(c
, FINALLY_TRY
, cleanup_body
))
2065 VISIT_SEQ(c
, stmt
, handler
->v
.ExceptHandler
.body
);
2066 ADDOP(c
, POP_EXCEPT
);
2067 compiler_pop_fblock(c
, FINALLY_TRY
, cleanup_body
);
2069 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2070 compiler_use_next_block(c
, except
);
2072 ADDOP(c
, END_FINALLY
);
2073 compiler_use_next_block(c
, orelse
);
2074 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
2075 compiler_use_next_block(c
, end
);
2080 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
2082 /* The IMPORT_NAME opcode was already generated. This function
2083 merely needs to bind the result to a name.
2085 If there is a dot in name, we need to split it and emit a
2086 LOAD_ATTR for each name.
2088 const Py_UNICODE
*src
= PyUnicode_AS_UNICODE(name
);
2089 const Py_UNICODE
*dot
= Py_UNICODE_strchr(src
, '.');
2091 /* Consume the base module name to get the first attribute */
2094 /* NB src is only defined when dot != NULL */
2096 dot
= Py_UNICODE_strchr(src
, '.');
2097 attr
= PyUnicode_FromUnicode(src
,
2098 dot
? dot
- src
: Py_UNICODE_strlen(src
));
2101 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
2106 return compiler_nameop(c
, asname
, Store
);
2110 compiler_import(struct compiler
*c
, stmt_ty s
)
2112 /* The Import node stores a module name like a.b.c as a single
2113 string. This is convenient for all cases except
2115 where we need to parse that string to extract the individual
2117 XXX Perhaps change the representation to make this case simpler?
2119 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
2121 for (i
= 0; i
< n
; i
++) {
2122 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.Import
.names
, i
);
2126 level
= PyLong_FromLong(0);
2130 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2132 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2133 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
2135 if (alias
->asname
) {
2136 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
2141 identifier tmp
= alias
->name
;
2142 const Py_UNICODE
*base
= PyUnicode_AS_UNICODE(alias
->name
);
2143 Py_UNICODE
*dot
= Py_UNICODE_strchr(base
, '.');
2145 tmp
= PyUnicode_FromUnicode(base
,
2147 r
= compiler_nameop(c
, tmp
, Store
);
2159 compiler_from_import(struct compiler
*c
, stmt_ty s
)
2161 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
2163 PyObject
*names
= PyTuple_New(n
);
2169 level
= PyLong_FromLong(s
->v
.ImportFrom
.level
);
2175 /* build up the names */
2176 for (i
= 0; i
< n
; i
++) {
2177 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2178 Py_INCREF(alias
->name
);
2179 PyTuple_SET_ITEM(names
, i
, alias
->name
);
2182 if (s
->lineno
> c
->c_future
->ff_lineno
) {
2183 if (!PyUnicode_CompareWithASCIIString(s
->v
.ImportFrom
.module
,
2187 return compiler_error(c
,
2188 "from __future__ imports must occur "
2189 "at the beginning of the file");
2194 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2196 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
2198 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
2199 for (i
= 0; i
< n
; i
++) {
2200 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2201 identifier store_name
;
2203 if (i
== 0 && *PyUnicode_AS_UNICODE(alias
->name
) == '*') {
2205 ADDOP(c
, IMPORT_STAR
);
2209 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
2210 store_name
= alias
->name
;
2212 store_name
= alias
->asname
;
2214 if (!compiler_nameop(c
, store_name
, Store
)) {
2219 /* remove imported module */
2225 compiler_assert(struct compiler
*c
, stmt_ty s
)
2227 static PyObject
*assertion_error
= NULL
;
2230 if (Py_OptimizeFlag
)
2232 if (assertion_error
== NULL
) {
2233 assertion_error
= PyUnicode_InternFromString("AssertionError");
2234 if (assertion_error
== NULL
)
2237 if (s
->v
.Assert
.test
->kind
== Tuple_kind
&&
2238 asdl_seq_LEN(s
->v
.Assert
.test
->v
.Tuple
.elts
) > 0) {
2240 "assertion is always true, perhaps remove parentheses?";
2241 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
,
2242 c
->u
->u_lineno
, NULL
, NULL
) == -1)
2245 VISIT(c
, expr
, s
->v
.Assert
.test
);
2246 end
= compiler_new_block(c
);
2249 ADDOP_JABS(c
, POP_JUMP_IF_TRUE
, end
);
2250 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
2251 if (s
->v
.Assert
.msg
) {
2252 VISIT(c
, expr
, s
->v
.Assert
.msg
);
2253 ADDOP_I(c
, CALL_FUNCTION
, 1);
2255 ADDOP_I(c
, RAISE_VARARGS
, 1);
2256 compiler_use_next_block(c
, end
);
2261 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2265 /* Always assign a lineno to the next instruction for a stmt. */
2266 c
->u
->u_lineno
= s
->lineno
;
2267 c
->u
->u_lineno_set
= 0;
2270 case FunctionDef_kind
:
2271 return compiler_function(c
, s
);
2273 return compiler_class(c
, s
);
2275 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2276 return compiler_error(c
, "'return' outside function");
2277 if (s
->v
.Return
.value
) {
2278 VISIT(c
, expr
, s
->v
.Return
.value
);
2281 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2282 ADDOP(c
, RETURN_VALUE
);
2285 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2288 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2289 VISIT(c
, expr
, s
->v
.Assign
.value
);
2290 for (i
= 0; i
< n
; i
++) {
2294 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2297 case AugAssign_kind
:
2298 return compiler_augassign(c
, s
);
2300 return compiler_for(c
, s
);
2302 return compiler_while(c
, s
);
2304 return compiler_if(c
, s
);
2307 if (s
->v
.Raise
.exc
) {
2308 VISIT(c
, expr
, s
->v
.Raise
.exc
);
2310 if (s
->v
.Raise
.cause
) {
2311 VISIT(c
, expr
, s
->v
.Raise
.cause
);
2315 ADDOP_I(c
, RAISE_VARARGS
, n
);
2317 case TryExcept_kind
:
2318 return compiler_try_except(c
, s
);
2319 case TryFinally_kind
:
2320 return compiler_try_finally(c
, s
);
2322 return compiler_assert(c
, s
);
2324 return compiler_import(c
, s
);
2325 case ImportFrom_kind
:
2326 return compiler_from_import(c
, s
);
2331 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2332 VISIT(c
, expr
, s
->v
.Expr
.value
);
2333 ADDOP(c
, PRINT_EXPR
);
2335 else if (s
->v
.Expr
.value
->kind
!= Str_kind
&&
2336 s
->v
.Expr
.value
->kind
!= Num_kind
) {
2337 VISIT(c
, expr
, s
->v
.Expr
.value
);
2344 if (!compiler_in_loop(c
))
2345 return compiler_error(c
, "'break' outside loop");
2346 ADDOP(c
, BREAK_LOOP
);
2349 return compiler_continue(c
);
2351 return compiler_with(c
, s
);
2357 unaryop(unaryop_ty op
)
2361 return UNARY_INVERT
;
2365 return UNARY_POSITIVE
;
2367 return UNARY_NEGATIVE
;
2369 PyErr_Format(PyExc_SystemError
,
2370 "unary op %d should not be possible", op
);
2376 binop(struct compiler
*c
, operator_ty op
)
2382 return BINARY_SUBTRACT
;
2384 return BINARY_MULTIPLY
;
2386 return BINARY_TRUE_DIVIDE
;
2388 return BINARY_MODULO
;
2390 return BINARY_POWER
;
2392 return BINARY_LSHIFT
;
2394 return BINARY_RSHIFT
;
2402 return BINARY_FLOOR_DIVIDE
;
2404 PyErr_Format(PyExc_SystemError
,
2405 "binary op %d should not be possible", op
);
2429 return PyCmp_IS_NOT
;
2433 return PyCmp_NOT_IN
;
2440 inplace_binop(struct compiler
*c
, operator_ty op
)
2446 return INPLACE_SUBTRACT
;
2448 return INPLACE_MULTIPLY
;
2450 return INPLACE_TRUE_DIVIDE
;
2452 return INPLACE_MODULO
;
2454 return INPLACE_POWER
;
2456 return INPLACE_LSHIFT
;
2458 return INPLACE_RSHIFT
;
2466 return INPLACE_FLOOR_DIVIDE
;
2468 PyErr_Format(PyExc_SystemError
,
2469 "inplace binary op %d should not be possible", op
);
2475 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2478 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2480 PyObject
*dict
= c
->u
->u_names
;
2482 /* XXX AugStore isn't used anywhere! */
2484 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2490 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2493 dict
= c
->u
->u_freevars
;
2497 dict
= c
->u
->u_cellvars
;
2501 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2504 case GLOBAL_IMPLICIT
:
2505 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2506 !c
->u
->u_ste
->ste_unoptimized
)
2509 case GLOBAL_EXPLICIT
:
2513 /* scope can be 0 */
2517 /* XXX Leave assert here, but handle __doc__ and the like better */
2518 assert(scope
|| PyUnicode_AS_UNICODE(name
)[0] == '_');
2523 case Load
: op
= LOAD_DEREF
; break;
2524 case Store
: op
= STORE_DEREF
; break;
2529 PyErr_Format(PyExc_SyntaxError
,
2530 "can not delete variable '%S' referenced "
2537 PyErr_SetString(PyExc_SystemError
,
2538 "param invalid for deref variable");
2544 case Load
: op
= LOAD_FAST
; break;
2545 case Store
: op
= STORE_FAST
; break;
2546 case Del
: op
= DELETE_FAST
; break;
2552 PyErr_SetString(PyExc_SystemError
,
2553 "param invalid for local variable");
2556 ADDOP_O(c
, op
, mangled
, varnames
);
2561 case Load
: op
= LOAD_GLOBAL
; break;
2562 case Store
: op
= STORE_GLOBAL
; break;
2563 case Del
: op
= DELETE_GLOBAL
; break;
2569 PyErr_SetString(PyExc_SystemError
,
2570 "param invalid for global variable");
2576 case Load
: op
= LOAD_NAME
; break;
2577 case Store
: op
= STORE_NAME
; break;
2578 case Del
: op
= DELETE_NAME
; break;
2584 PyErr_SetString(PyExc_SystemError
,
2585 "param invalid for name variable");
2592 arg
= compiler_add_o(c
, dict
, mangled
);
2596 return compiler_addop_i(c
, op
, arg
);
2600 compiler_boolop(struct compiler
*c
, expr_ty e
)
2606 assert(e
->kind
== BoolOp_kind
);
2607 if (e
->v
.BoolOp
.op
== And
)
2608 jumpi
= JUMP_IF_FALSE_OR_POP
;
2610 jumpi
= JUMP_IF_TRUE_OR_POP
;
2611 end
= compiler_new_block(c
);
2614 s
= e
->v
.BoolOp
.values
;
2615 n
= asdl_seq_LEN(s
) - 1;
2617 for (i
= 0; i
< n
; ++i
) {
2618 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, i
));
2619 ADDOP_JABS(c
, jumpi
, end
);
2621 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, n
));
2622 compiler_use_next_block(c
, end
);
2627 compiler_list(struct compiler
*c
, expr_ty e
)
2629 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
2630 if (e
->v
.List
.ctx
== Store
) {
2631 int i
, seen_star
= 0;
2632 for (i
= 0; i
< n
; i
++) {
2633 expr_ty elt
= asdl_seq_GET(e
->v
.List
.elts
, i
);
2634 if (elt
->kind
== Starred_kind
&& !seen_star
) {
2635 if ((i
>= (1 << 8)) ||
2636 (n
-i
-1 >= (INT_MAX
>> 8)))
2637 return compiler_error(c
,
2638 "too many expressions in "
2639 "star-unpacking assignment");
2640 ADDOP_I(c
, UNPACK_EX
, (i
+ ((n
-i
-1) << 8)));
2642 asdl_seq_SET(e
->v
.List
.elts
, i
, elt
->v
.Starred
.value
);
2643 } else if (elt
->kind
== Starred_kind
) {
2644 return compiler_error(c
,
2645 "two starred expressions in assignment");
2649 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2652 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
2653 if (e
->v
.List
.ctx
== Load
) {
2654 ADDOP_I(c
, BUILD_LIST
, n
);
2660 compiler_tuple(struct compiler
*c
, expr_ty e
)
2662 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
2663 if (e
->v
.Tuple
.ctx
== Store
) {
2664 int i
, seen_star
= 0;
2665 for (i
= 0; i
< n
; i
++) {
2666 expr_ty elt
= asdl_seq_GET(e
->v
.Tuple
.elts
, i
);
2667 if (elt
->kind
== Starred_kind
&& !seen_star
) {
2668 if ((i
>= (1 << 8)) ||
2669 (n
-i
-1 >= (INT_MAX
>> 8)))
2670 return compiler_error(c
,
2671 "too many expressions in "
2672 "star-unpacking assignment");
2673 ADDOP_I(c
, UNPACK_EX
, (i
+ ((n
-i
-1) << 8)));
2675 asdl_seq_SET(e
->v
.Tuple
.elts
, i
, elt
->v
.Starred
.value
);
2676 } else if (elt
->kind
== Starred_kind
) {
2677 return compiler_error(c
,
2678 "two starred expressions in assignment");
2682 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2685 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
2686 if (e
->v
.Tuple
.ctx
== Load
) {
2687 ADDOP_I(c
, BUILD_TUPLE
, n
);
2693 compiler_compare(struct compiler
*c
, expr_ty e
)
2696 basicblock
*cleanup
= NULL
;
2698 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2699 VISIT(c
, expr
, e
->v
.Compare
.left
);
2700 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
2703 cleanup
= compiler_new_block(c
);
2704 if (cleanup
== NULL
)
2707 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
2709 for (i
= 1; i
< n
; i
++) {
2711 ADDOP(c
, ROT_THREE
);
2712 ADDOP_I(c
, COMPARE_OP
,
2713 cmpop((cmpop_ty
)(asdl_seq_GET(
2714 e
->v
.Compare
.ops
, i
- 1))));
2715 ADDOP_JABS(c
, JUMP_IF_FALSE_OR_POP
, cleanup
);
2719 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
2721 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
2722 ADDOP_I(c
, COMPARE_OP
,
2723 cmpop((cmpop_ty
)(asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1))));
2725 basicblock
*end
= compiler_new_block(c
);
2728 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2729 compiler_use_next_block(c
, cleanup
);
2732 compiler_use_next_block(c
, end
);
2738 compiler_call(struct compiler
*c
, expr_ty e
)
2740 VISIT(c
, expr
, e
->v
.Call
.func
);
2741 return compiler_call_helper(c
, 0,
2748 /* shared code between compiler_call and compiler_class */
2750 compiler_call_helper(struct compiler
*c
,
2751 int n
, /* Args already pushed */
2759 n
+= asdl_seq_LEN(args
);
2760 VISIT_SEQ(c
, expr
, args
);
2762 VISIT_SEQ(c
, keyword
, keywords
);
2763 n
|= asdl_seq_LEN(keywords
) << 8;
2766 VISIT(c
, expr
, starargs
);
2770 VISIT(c
, expr
, kwargs
);
2775 ADDOP_I(c
, CALL_FUNCTION
, n
);
2778 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
2781 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
2784 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
2791 /* List and set comprehensions and generator expressions work by creating a
2792 nested function to perform the actual iteration. This means that the
2793 iteration variables don't leak into the current scope.
2794 The defined function is called immediately following its definition, with the
2795 result of that call being the result of the expression.
2796 The LC/SC version returns the populated container, while the GE version is
2797 flagged in symtable.c as a generator, so it returns the generator object
2798 when the function is called.
2799 This code *knows* that the loop cannot contain break, continue, or return,
2800 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2803 - iterate over the generator sequence instead of using recursion
2807 compiler_comprehension_generator(struct compiler
*c
,
2808 asdl_seq
*generators
, int gen_index
,
2809 expr_ty elt
, expr_ty val
, int type
)
2811 /* generate code for the iterator, then each of the ifs,
2812 and then write to the element */
2814 comprehension_ty gen
;
2815 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
2818 start
= compiler_new_block(c
);
2819 skip
= compiler_new_block(c
);
2820 if_cleanup
= compiler_new_block(c
);
2821 anchor
= compiler_new_block(c
);
2823 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2827 gen
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2829 if (gen_index
== 0) {
2830 /* Receive outermost iter as an implicit argument */
2831 c
->u
->u_argcount
= 1;
2832 ADDOP_I(c
, LOAD_FAST
, 0);
2835 /* Sub-iter - calculate on the fly */
2836 VISIT(c
, expr
, gen
->iter
);
2839 compiler_use_next_block(c
, start
);
2840 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2842 VISIT(c
, expr
, gen
->target
);
2844 /* XXX this needs to be cleaned up...a lot! */
2845 n
= asdl_seq_LEN(gen
->ifs
);
2846 for (i
= 0; i
< n
; i
++) {
2847 expr_ty e
= (expr_ty
)asdl_seq_GET(gen
->ifs
, i
);
2849 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, if_cleanup
);
2853 if (++gen_index
< asdl_seq_LEN(generators
))
2854 if (!compiler_comprehension_generator(c
,
2855 generators
, gen_index
,
2859 /* only append after the last for generator */
2860 if (gen_index
>= asdl_seq_LEN(generators
)) {
2861 /* comprehension specific code */
2864 VISIT(c
, expr
, elt
);
2865 ADDOP(c
, YIELD_VALUE
);
2869 VISIT(c
, expr
, elt
);
2870 ADDOP_I(c
, LIST_APPEND
, gen_index
+ 1);
2873 VISIT(c
, expr
, elt
);
2874 ADDOP_I(c
, SET_ADD
, gen_index
+ 1);
2877 /* With 'd[k] = v', v is evaluated before k, so we do
2879 VISIT(c
, expr
, val
);
2880 VISIT(c
, expr
, elt
);
2881 ADDOP_I(c
, MAP_ADD
, gen_index
+ 1);
2887 compiler_use_next_block(c
, skip
);
2889 compiler_use_next_block(c
, if_cleanup
);
2890 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2891 compiler_use_next_block(c
, anchor
);
2897 compiler_comprehension(struct compiler
*c
, expr_ty e
, int type
, identifier name
,
2898 asdl_seq
*generators
, expr_ty elt
, expr_ty val
)
2900 PyCodeObject
*co
= NULL
;
2901 expr_ty outermost_iter
;
2903 outermost_iter
= ((comprehension_ty
)
2904 asdl_seq_GET(generators
, 0))->iter
;
2906 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2909 if (type
!= COMP_GENEXP
) {
2922 PyErr_Format(PyExc_SystemError
,
2923 "unknown comprehension type %d", type
);
2924 goto error_in_scope
;
2930 if (!compiler_comprehension_generator(c
, generators
, 0, elt
,
2932 goto error_in_scope
;
2934 if (type
!= COMP_GENEXP
) {
2935 ADDOP(c
, RETURN_VALUE
);
2938 co
= assemble(c
, 1);
2939 compiler_exit_scope(c
);
2943 if (!compiler_make_closure(c
, co
, 0))
2947 VISIT(c
, expr
, outermost_iter
);
2949 ADDOP_I(c
, CALL_FUNCTION
, 1);
2952 compiler_exit_scope(c
);
2959 compiler_genexp(struct compiler
*c
, expr_ty e
)
2961 static identifier name
;
2963 name
= PyUnicode_FromString("<genexpr>");
2967 assert(e
->kind
== GeneratorExp_kind
);
2968 return compiler_comprehension(c
, e
, COMP_GENEXP
, name
,
2969 e
->v
.GeneratorExp
.generators
,
2970 e
->v
.GeneratorExp
.elt
, NULL
);
2974 compiler_listcomp(struct compiler
*c
, expr_ty e
)
2976 static identifier name
;
2978 name
= PyUnicode_FromString("<listcomp>");
2982 assert(e
->kind
== ListComp_kind
);
2983 return compiler_comprehension(c
, e
, COMP_LISTCOMP
, name
,
2984 e
->v
.ListComp
.generators
,
2985 e
->v
.ListComp
.elt
, NULL
);
2989 compiler_setcomp(struct compiler
*c
, expr_ty e
)
2991 static identifier name
;
2993 name
= PyUnicode_FromString("<setcomp>");
2997 assert(e
->kind
== SetComp_kind
);
2998 return compiler_comprehension(c
, e
, COMP_SETCOMP
, name
,
2999 e
->v
.SetComp
.generators
,
3000 e
->v
.SetComp
.elt
, NULL
);
3005 compiler_dictcomp(struct compiler
*c
, expr_ty e
)
3007 static identifier name
;
3009 name
= PyUnicode_FromString("<dictcomp>");
3013 assert(e
->kind
== DictComp_kind
);
3014 return compiler_comprehension(c
, e
, COMP_DICTCOMP
, name
,
3015 e
->v
.DictComp
.generators
,
3016 e
->v
.DictComp
.key
, e
->v
.DictComp
.value
);
3021 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
3023 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
3024 VISIT(c
, expr
, k
->value
);
3028 /* Test whether expression is constant. For constants, report
3029 whether they are true or false.
3031 Return values: 1 for true, 0 for false, -1 for non-constant.
3035 expr_constant(expr_ty e
)
3042 return PyObject_IsTrue(e
->v
.Num
.n
);
3044 return PyObject_IsTrue(e
->v
.Str
.s
);
3046 /* optimize away names that can't be reassigned */
3047 id
= PyBytes_AS_STRING(
3048 _PyUnicode_AsDefaultEncodedString(e
->v
.Name
.id
, NULL
));
3049 if (strcmp(id
, "True") == 0) return 1;
3050 if (strcmp(id
, "False") == 0) return 0;
3051 if (strcmp(id
, "None") == 0) return 0;
3052 if (strcmp(id
, "__debug__") == 0)
3053 return ! Py_OptimizeFlag
;
3061 Implements the with statement from PEP 343.
3063 The semantics outlined in that PEP are as follows:
3068 It is implemented roughly as:
3071 exit = context.__exit__ # not calling it
3072 value = context.__enter__()
3074 VAR = value # if VAR present in the syntax
3077 if an exception was raised:
3078 exc = copy of (exception, instance, traceback)
3080 exc = (None, None, None)
3084 compiler_with(struct compiler
*c
, stmt_ty s
)
3086 static identifier enter_attr
, exit_attr
;
3087 basicblock
*block
, *finally
;
3088 identifier tmpvalue
= NULL
, tmpexit
= NULL
;
3090 assert(s
->kind
== With_kind
);
3093 enter_attr
= PyUnicode_InternFromString("__enter__");
3098 exit_attr
= PyUnicode_InternFromString("__exit__");
3103 block
= compiler_new_block(c
);
3104 finally
= compiler_new_block(c
);
3105 if (!block
|| !finally
)
3108 if (s
->v
.With
.optional_vars
) {
3109 /* Create a temporary variable to hold context.__enter__().
3110 We need to do this rather than preserving it on the stack
3111 because SETUP_FINALLY remembers the stack level.
3112 We need to do the assignment *inside* the try/finally
3113 so that context.__exit__() is called when the assignment
3114 fails. But we need to call context.__enter__() *before*
3115 the try/finally so that if it fails we won't call
3118 tmpvalue
= compiler_new_tmpname(c
);
3119 if (tmpvalue
== NULL
)
3121 PyArena_AddPyObject(c
->c_arena
, tmpvalue
);
3123 tmpexit
= compiler_new_tmpname(c
);
3124 if (tmpexit
== NULL
)
3126 PyArena_AddPyObject(c
->c_arena
, tmpexit
);
3129 VISIT(c
, expr
, s
->v
.With
.context_expr
);
3131 /* Squirrel away context.__exit__ by stuffing it under context */
3133 ADDOP_O(c
, LOAD_ATTR
, exit_attr
, names
);
3134 if (!compiler_nameop(c
, tmpexit
, Store
))
3137 /* Call context.__enter__() */
3138 ADDOP_O(c
, LOAD_ATTR
, enter_attr
, names
);
3139 ADDOP_I(c
, CALL_FUNCTION
, 0);
3141 if (s
->v
.With
.optional_vars
) {
3142 /* Store it in tmpvalue */
3143 if (!compiler_nameop(c
, tmpvalue
, Store
))
3147 /* Discard result from context.__enter__() */
3151 /* Start the try block */
3152 ADDOP_JREL(c
, SETUP_FINALLY
, finally
);
3154 compiler_use_next_block(c
, block
);
3155 if (!compiler_push_fblock(c
, FINALLY_TRY
, block
)) {
3159 if (s
->v
.With
.optional_vars
) {
3160 /* Bind saved result of context.__enter__() to VAR */
3161 if (!compiler_nameop(c
, tmpvalue
, Load
) ||
3162 !compiler_nameop(c
, tmpvalue
, Del
))
3164 VISIT(c
, expr
, s
->v
.With
.optional_vars
);
3168 VISIT_SEQ(c
, stmt
, s
->v
.With
.body
);
3170 /* End of try block; start the finally block */
3171 ADDOP(c
, POP_BLOCK
);
3172 compiler_pop_fblock(c
, FINALLY_TRY
, block
);
3174 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3175 compiler_use_next_block(c
, finally
);
3176 if (!compiler_push_fblock(c
, FINALLY_END
, finally
))
3179 /* Finally block starts; context.__exit__ is on the stack under
3180 the exception or return information. Just issue our magic
3182 if (!compiler_nameop(c
, tmpexit
, Load
) ||
3183 !compiler_nameop(c
, tmpexit
, Del
))
3185 ADDOP(c
, WITH_CLEANUP
);
3187 /* Finally block ends. */
3188 ADDOP(c
, END_FINALLY
);
3189 compiler_pop_fblock(c
, FINALLY_END
, finally
);
3194 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
3198 /* If expr e has a different line number than the last expr/stmt,
3199 set a new line number for the next instruction.
3201 if (e
->lineno
> c
->u
->u_lineno
) {
3202 c
->u
->u_lineno
= e
->lineno
;
3203 c
->u
->u_lineno_set
= 0;
3207 return compiler_boolop(c
, e
);
3209 VISIT(c
, expr
, e
->v
.BinOp
.left
);
3210 VISIT(c
, expr
, e
->v
.BinOp
.right
);
3211 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
3214 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
3215 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
3218 return compiler_lambda(c
, e
);
3220 return compiler_ifexp(c
, e
);
3222 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
3223 ADDOP_I(c
, BUILD_MAP
, (n
>0xFFFF ? 0xFFFF : n
));
3224 for (i
= 0; i
< n
; i
++) {
3226 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.values
, i
));
3228 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.keys
, i
));
3229 ADDOP(c
, STORE_MAP
);
3233 n
= asdl_seq_LEN(e
->v
.Set
.elts
);
3234 VISIT_SEQ(c
, expr
, e
->v
.Set
.elts
);
3235 ADDOP_I(c
, BUILD_SET
, n
);
3237 case GeneratorExp_kind
:
3238 return compiler_genexp(c
, e
);
3240 return compiler_listcomp(c
, e
);
3242 return compiler_setcomp(c
, e
);
3244 return compiler_dictcomp(c
, e
);
3246 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
3247 return compiler_error(c
, "'yield' outside function");
3248 if (e
->v
.Yield
.value
) {
3249 VISIT(c
, expr
, e
->v
.Yield
.value
);
3252 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3254 ADDOP(c
, YIELD_VALUE
);
3257 return compiler_compare(c
, e
);
3259 return compiler_call(c
, e
);
3261 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
3264 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
3267 ADDOP_O(c
, LOAD_CONST
, e
->v
.Bytes
.s
, consts
);
3270 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3272 /* The following exprs can be assignment targets. */
3273 case Attribute_kind
:
3274 if (e
->v
.Attribute
.ctx
!= AugStore
)
3275 VISIT(c
, expr
, e
->v
.Attribute
.value
);
3276 switch (e
->v
.Attribute
.ctx
) {
3279 /* Fall through to load */
3281 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
3285 /* Fall through to save */
3287 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
3290 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
3294 PyErr_SetString(PyExc_SystemError
,
3295 "param invalid in attribute expression");
3299 case Subscript_kind
:
3300 switch (e
->v
.Subscript
.ctx
) {
3302 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3303 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
3306 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3307 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
3310 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
3313 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3314 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
3317 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3318 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
3322 PyErr_SetString(PyExc_SystemError
,
3323 "param invalid in subscript expression");
3328 switch (e
->v
.Starred
.ctx
) {
3330 /* In all legitimate cases, the Starred node was already replaced
3331 * by compiler_list/compiler_tuple. XXX: is that okay? */
3332 return compiler_error(c
,
3333 "starred assignment target must be in a list or tuple");
3335 return compiler_error(c
,
3336 "can use starred expression only as assignment target");
3340 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3341 /* child nodes of List and Tuple will have expr_context set */
3343 return compiler_list(c
, e
);
3345 return compiler_tuple(c
, e
);
3351 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3353 expr_ty e
= s
->v
.AugAssign
.target
;
3356 assert(s
->kind
== AugAssign_kind
);
3359 case Attribute_kind
:
3360 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3361 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3364 VISIT(c
, expr
, auge
);
3365 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3366 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3367 auge
->v
.Attribute
.ctx
= AugStore
;
3368 VISIT(c
, expr
, auge
);
3370 case Subscript_kind
:
3371 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3372 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3375 VISIT(c
, expr
, auge
);
3376 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3377 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3378 auge
->v
.Subscript
.ctx
= AugStore
;
3379 VISIT(c
, expr
, auge
);
3382 if (!compiler_nameop(c
, e
->v
.Name
.id
, Load
))
3384 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3385 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3386 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3388 PyErr_Format(PyExc_SystemError
,
3389 "invalid node type (%d) for augmented assignment",
3397 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3399 struct fblockinfo
*f
;
3400 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
) {
3401 PyErr_SetString(PyExc_SystemError
,
3402 "too many statically nested blocks");
3405 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3412 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3414 struct compiler_unit
*u
= c
->u
;
3415 assert(u
->u_nfblocks
> 0);
3417 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3418 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3422 compiler_in_loop(struct compiler
*c
) {
3424 struct compiler_unit
*u
= c
->u
;
3425 for (i
= 0; i
< u
->u_nfblocks
; ++i
) {
3426 if (u
->u_fblock
[i
].fb_type
== LOOP
)
3431 /* Raises a SyntaxError and returns 0.
3432 If something goes wrong, a different exception may be raised.
3436 compiler_error(struct compiler
*c
, const char *errstr
)
3439 PyObject
*u
= NULL
, *v
= NULL
;
3441 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3446 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3450 v
= Py_BuildValue("(zO)", errstr
, u
);
3453 PyErr_SetObject(PyExc_SyntaxError
, v
);
3462 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3463 expr_context_ty ctx
)
3467 /* XXX this code is duplicated */
3469 case AugLoad
: /* fall through to Load */
3470 case Load
: op
= BINARY_SUBSCR
; break;
3471 case AugStore
:/* fall through to Store */
3472 case Store
: op
= STORE_SUBSCR
; break;
3473 case Del
: op
= DELETE_SUBSCR
; break;
3475 PyErr_Format(PyExc_SystemError
,
3476 "invalid %s kind %d in subscript\n",
3480 if (ctx
== AugLoad
) {
3481 ADDOP_I(c
, DUP_TOPX
, 2);
3483 else if (ctx
== AugStore
) {
3484 ADDOP(c
, ROT_THREE
);
3491 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3494 assert(s
->kind
== Slice_kind
);
3496 /* only handles the cases where BUILD_SLICE is emitted */
3497 if (s
->v
.Slice
.lower
) {
3498 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3501 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3504 if (s
->v
.Slice
.upper
) {
3505 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3508 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3511 if (s
->v
.Slice
.step
) {
3513 VISIT(c
, expr
, s
->v
.Slice
.step
);
3515 ADDOP_I(c
, BUILD_SLICE
, n
);
3520 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3521 expr_context_ty ctx
)
3525 return compiler_slice(c
, s
, ctx
);
3527 VISIT(c
, expr
, s
->v
.Index
.value
);
3531 PyErr_SetString(PyExc_SystemError
,
3532 "extended slice invalid in nested slice");
3539 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3541 char * kindname
= NULL
;
3545 if (ctx
!= AugStore
) {
3546 VISIT(c
, expr
, s
->v
.Index
.value
);
3551 if (ctx
!= AugStore
) {
3552 if (!compiler_slice(c
, s
, ctx
))
3557 kindname
= "extended slice";
3558 if (ctx
!= AugStore
) {
3559 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3560 for (i
= 0; i
< n
; i
++) {
3561 slice_ty sub
= (slice_ty
)asdl_seq_GET(
3562 s
->v
.ExtSlice
.dims
, i
);
3563 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3566 ADDOP_I(c
, BUILD_TUPLE
, n
);
3570 PyErr_Format(PyExc_SystemError
,
3571 "invalid subscript kind %d", s
->kind
);
3574 return compiler_handle_subscr(c
, kindname
, ctx
);
3577 /* End of the compiler section, beginning of the assembler section */
3579 /* do depth-first search of basic block graph, starting with block.
3580 post records the block indices in post-order.
3582 XXX must handle implicit jumps from one block to next
3586 PyObject
*a_bytecode
; /* string containing bytecode */
3587 int a_offset
; /* offset into bytecode */
3588 int a_nblocks
; /* number of reachable blocks */
3589 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
3590 PyObject
*a_lnotab
; /* string containing lnotab */
3591 int a_lnotab_off
; /* offset into lnotab */
3592 int a_lineno
; /* last lineno of emitted instruction */
3593 int a_lineno_off
; /* bytecode offset of last lineno */
3597 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3600 struct instr
*instr
= NULL
;
3605 if (b
->b_next
!= NULL
)
3606 dfs(c
, b
->b_next
, a
);
3607 for (i
= 0; i
< b
->b_iused
; i
++) {
3608 instr
= &b
->b_instr
[i
];
3609 if (instr
->i_jrel
|| instr
->i_jabs
)
3610 dfs(c
, instr
->i_target
, a
);
3612 a
->a_postorder
[a
->a_nblocks
++] = b
;
3616 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3619 struct instr
*instr
;
3620 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3623 b
->b_startdepth
= depth
;
3624 for (i
= 0; i
< b
->b_iused
; i
++) {
3625 instr
= &b
->b_instr
[i
];
3626 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3627 if (depth
> maxdepth
)
3629 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3630 if (instr
->i_jrel
|| instr
->i_jabs
) {
3631 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3633 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3634 instr
->i_opcode
== JUMP_FORWARD
) {
3635 goto out
; /* remaining code is dead */
3640 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3646 /* Find the flow path that needs the largest stack. We assume that
3647 * cycles in the flow graph have no net effect on the stack depth.
3650 stackdepth(struct compiler
*c
)
3652 basicblock
*b
, *entryblock
;
3654 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3656 b
->b_startdepth
= INT_MIN
;
3661 return stackdepth_walk(c
, entryblock
, 0, 0);
3665 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3667 memset(a
, 0, sizeof(struct assembler
));
3668 a
->a_lineno
= firstlineno
;
3669 a
->a_bytecode
= PyBytes_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3672 a
->a_lnotab
= PyBytes_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3675 if (nblocks
> PY_SIZE_MAX
/ sizeof(basicblock
*)) {
3679 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3680 sizeof(basicblock
*) * nblocks
);
3681 if (!a
->a_postorder
) {
3689 assemble_free(struct assembler
*a
)
3691 Py_XDECREF(a
->a_bytecode
);
3692 Py_XDECREF(a
->a_lnotab
);
3694 PyObject_Free(a
->a_postorder
);
3697 /* Return the size of a basic block in bytes. */
3700 instrsize(struct instr
*instr
)
3702 if (!instr
->i_hasarg
)
3703 return 1; /* 1 byte for the opcode*/
3704 if (instr
->i_oparg
> 0xffff)
3705 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3706 return 3; /* 1 (opcode) + 2 (oparg) */
3710 blocksize(basicblock
*b
)
3715 for (i
= 0; i
< b
->b_iused
; i
++)
3716 size
+= instrsize(&b
->b_instr
[i
]);
3720 /* All about a_lnotab.
3722 c_lnotab is an array of unsigned bytes disguised as a Python string.
3723 It is used to map bytecode offsets to source code line #s (when needed
3726 The array is conceptually a list of
3727 (bytecode offset increment, line number increment)
3728 pairs. The details are important and delicate, best illustrated by example:
3730 byte code offset source code line number
3737 The first trick is that these numbers aren't stored, only the increments
3738 from one row to the next (this doesn't really work, but it's a start):
3740 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3742 The second trick is that an unsigned byte can't hold negative values, or
3743 values larger than 255, so (a) there's a deep assumption that byte code
3744 offsets and their corresponding line #s both increase monotonically, and (b)
3745 if at least one column jumps by more than 255 from one row to the next, more
3746 than one pair is written to the table. In case #b, there's no way to know
3747 from looking at the table later how many were written. That's the delicate
3748 part. A user of c_lnotab desiring to find the source line number
3749 corresponding to a bytecode address A should do something like this
3752 for addr_incr, line_incr in c_lnotab:
3758 In order for this to work, when the addr field increments by more than 255,
3759 the line # increment in each pair generated must be 0 until the remaining addr
3760 increment is < 256. So, in the example above, assemble_lnotab (it used
3761 to be called com_set_lineno) should not (as was actually done until 2.2)
3762 expand 300, 300 to 255, 255, 45, 45,
3763 but to 255, 0, 45, 255, 0, 45.
3767 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
3769 int d_bytecode
, d_lineno
;
3771 unsigned char *lnotab
;
3773 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
3774 d_lineno
= i
->i_lineno
- a
->a_lineno
;
3776 assert(d_bytecode
>= 0);
3777 assert(d_lineno
>= 0);
3779 if(d_bytecode
== 0 && d_lineno
== 0)
3782 if (d_bytecode
> 255) {
3783 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
3784 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3785 len
= PyBytes_GET_SIZE(a
->a_lnotab
);
3786 if (nbytes
>= len
) {
3787 if ((len
<= INT_MAX
/ 2) && (len
* 2 < nbytes
))
3789 else if (len
<= INT_MAX
/ 2)
3795 if (_PyBytes_Resize(&a
->a_lnotab
, len
) < 0)
3798 lnotab
= (unsigned char *)
3799 PyBytes_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3800 for (j
= 0; j
< ncodes
; j
++) {
3804 d_bytecode
-= ncodes
* 255;
3805 a
->a_lnotab_off
+= ncodes
* 2;
3807 assert(d_bytecode
<= 255);
3808 if (d_lineno
> 255) {
3809 int j
, nbytes
, ncodes
= d_lineno
/ 255;
3810 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3811 len
= PyBytes_GET_SIZE(a
->a_lnotab
);
3812 if (nbytes
>= len
) {
3813 if ((len
<= INT_MAX
/ 2) && len
* 2 < nbytes
)
3815 else if (len
<= INT_MAX
/ 2)
3821 if (_PyBytes_Resize(&a
->a_lnotab
, len
) < 0)
3824 lnotab
= (unsigned char *)
3825 PyBytes_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3826 *lnotab
++ = d_bytecode
;
3829 for (j
= 1; j
< ncodes
; j
++) {
3833 d_lineno
-= ncodes
* 255;
3834 a
->a_lnotab_off
+= ncodes
* 2;
3837 len
= PyBytes_GET_SIZE(a
->a_lnotab
);
3838 if (a
->a_lnotab_off
+ 2 >= len
) {
3839 if (_PyBytes_Resize(&a
->a_lnotab
, len
* 2) < 0)
3842 lnotab
= (unsigned char *)
3843 PyBytes_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3845 a
->a_lnotab_off
+= 2;
3847 *lnotab
++ = d_bytecode
;
3848 *lnotab
++ = d_lineno
;
3850 else { /* First line of a block; def stmt, etc. */
3852 *lnotab
++ = d_lineno
;
3854 a
->a_lineno
= i
->i_lineno
;
3855 a
->a_lineno_off
= a
->a_offset
;
3860 Extend the bytecode with a new instruction.
3861 Update lnotab if necessary.
3865 assemble_emit(struct assembler
*a
, struct instr
*i
)
3867 int size
, arg
= 0, ext
= 0;
3868 Py_ssize_t len
= PyBytes_GET_SIZE(a
->a_bytecode
);
3871 size
= instrsize(i
);
3876 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
3878 if (a
->a_offset
+ size
>= len
) {
3879 if (len
> PY_SSIZE_T_MAX
/ 2)
3881 if (_PyBytes_Resize(&a
->a_bytecode
, len
* 2) < 0)
3884 code
= PyBytes_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
3885 a
->a_offset
+= size
;
3887 assert(i
->i_hasarg
);
3888 *code
++ = (char)EXTENDED_ARG
;
3889 *code
++ = ext
& 0xff;
3893 *code
++ = i
->i_opcode
;
3895 assert(size
== 3 || size
== 6);
3896 *code
++ = arg
& 0xff;
3903 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
3906 int bsize
, totsize
, extended_arg_count
, last_extended_arg_count
= 0;
3909 /* Compute the size of each block and fixup jump args.
3910 Replace block pointer with position in bytecode. */
3913 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
3914 b
= a
->a_postorder
[i
];
3915 bsize
= blocksize(b
);
3916 b
->b_offset
= totsize
;
3919 extended_arg_count
= 0;
3920 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3921 bsize
= b
->b_offset
;
3922 for (i
= 0; i
< b
->b_iused
; i
++) {
3923 struct instr
*instr
= &b
->b_instr
[i
];
3924 /* Relative jumps are computed relative to
3925 the instruction pointer after fetching
3926 the jump instruction.
3928 bsize
+= instrsize(instr
);
3930 instr
->i_oparg
= instr
->i_target
->b_offset
;
3931 else if (instr
->i_jrel
) {
3932 int delta
= instr
->i_target
->b_offset
- bsize
;
3933 instr
->i_oparg
= delta
;
3937 if (instr
->i_oparg
> 0xffff)
3938 extended_arg_count
++;
3942 /* XXX: This is an awful hack that could hurt performance, but
3943 on the bright side it should work until we come up
3944 with a better solution.
3946 In the meantime, should the goto be dropped in favor
3949 The issue is that in the first loop blocksize() is called
3950 which calls instrsize() which requires i_oparg be set
3951 appropriately. There is a bootstrap problem because
3952 i_oparg is calculated in the second loop above.
3954 So we loop until we stop seeing new EXTENDED_ARGs.
3955 The only EXTENDED_ARGs that could be popping up are
3956 ones in jump instructions. So this should converge
3959 if (last_extended_arg_count
!= extended_arg_count
) {
3960 last_extended_arg_count
= extended_arg_count
;
3966 dict_keys_inorder(PyObject
*dict
, int offset
)
3968 PyObject
*tuple
, *k
, *v
;
3969 Py_ssize_t i
, pos
= 0, size
= PyDict_Size(dict
);
3971 tuple
= PyTuple_New(size
);
3974 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3975 i
= PyLong_AS_LONG(v
);
3976 /* The keys of the dictionary are tuples. (see compiler_add_o)
3977 The object we want is always first, though. */
3978 k
= PyTuple_GET_ITEM(k
, 0);
3980 assert((i
- offset
) < size
);
3981 assert((i
- offset
) >= 0);
3982 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3988 compute_code_flags(struct compiler
*c
)
3990 PySTEntryObject
*ste
= c
->u
->u_ste
;
3992 if (ste
->ste_type
!= ModuleBlock
)
3993 flags
|= CO_NEWLOCALS
;
3994 if (ste
->ste_type
== FunctionBlock
) {
3995 if (!ste
->ste_unoptimized
)
3996 flags
|= CO_OPTIMIZED
;
3997 if (ste
->ste_nested
)
3999 if (ste
->ste_generator
)
4000 flags
|= CO_GENERATOR
;
4001 if (ste
->ste_varargs
)
4002 flags
|= CO_VARARGS
;
4003 if (ste
->ste_varkeywords
)
4004 flags
|= CO_VARKEYWORDS
;
4007 /* (Only) inherit compilerflags in PyCF_MASK */
4008 flags
|= (c
->c_flags
->cf_flags
& PyCF_MASK
);
4010 n
= PyDict_Size(c
->u
->u_freevars
);
4014 n
= PyDict_Size(c
->u
->u_cellvars
);
4025 static PyCodeObject
*
4026 makecode(struct compiler
*c
, struct assembler
*a
)
4029 PyCodeObject
*co
= NULL
;
4030 PyObject
*consts
= NULL
;
4031 PyObject
*names
= NULL
;
4032 PyObject
*varnames
= NULL
;
4033 PyObject
*filename
= NULL
;
4034 PyObject
*name
= NULL
;
4035 PyObject
*freevars
= NULL
;
4036 PyObject
*cellvars
= NULL
;
4037 PyObject
*bytecode
= NULL
;
4040 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
4043 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
4046 names
= dict_keys_inorder(c
->u
->u_names
, 0);
4047 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
4048 if (!consts
|| !names
|| !varnames
)
4051 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
4054 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
4057 filename
= PyUnicode_DecodeFSDefault(c
->c_filename
);
4061 nlocals
= PyDict_Size(c
->u
->u_varnames
);
4062 flags
= compute_code_flags(c
);
4066 bytecode
= PyCode_Optimize(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
4070 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
4076 co
= PyCode_New(c
->u
->u_argcount
, c
->u
->u_kwonlyargcount
,
4077 nlocals
, stackdepth(c
), flags
,
4078 bytecode
, consts
, names
, varnames
,
4080 filename
, c
->u
->u_name
,
4081 c
->u
->u_firstlineno
,
4086 Py_XDECREF(varnames
);
4087 Py_XDECREF(filename
);
4089 Py_XDECREF(freevars
);
4090 Py_XDECREF(cellvars
);
4091 Py_XDECREF(bytecode
);
4096 /* For debugging purposes only */
4099 dump_instr(const struct instr
*i
)
4101 const char *jrel
= i
->i_jrel
? "jrel " : "";
4102 const char *jabs
= i
->i_jabs
? "jabs " : "";
4107 sprintf(arg
, "arg: %d ", i
->i_oparg
);
4109 fprintf(stderr
, "line: %d, opcode: %d %s%s%s\n",
4110 i
->i_lineno
, i
->i_opcode
, arg
, jabs
, jrel
);
4114 dump_basicblock(const basicblock
*b
)
4116 const char *seen
= b
->b_seen
? "seen " : "";
4117 const char *b_return
= b
->b_return
? "return " : "";
4118 fprintf(stderr
, "used: %d, depth: %d, offset: %d %s%s\n",
4119 b
->b_iused
, b
->b_startdepth
, b
->b_offset
, seen
, b_return
);
4122 for (i
= 0; i
< b
->b_iused
; i
++) {
4123 fprintf(stderr
, " [%02d] ", i
);
4124 dump_instr(b
->b_instr
+ i
);
4130 static PyCodeObject
*
4131 assemble(struct compiler
*c
, int addNone
)
4133 basicblock
*b
, *entryblock
;
4136 PyCodeObject
*co
= NULL
;
4138 /* Make sure every block that falls off the end returns None.
4139 XXX NEXT_BLOCK() isn't quite right, because if the last
4140 block ends with a jump or return b_next shouldn't set.
4142 if (!c
->u
->u_curblock
->b_return
) {
4145 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
4146 ADDOP(c
, RETURN_VALUE
);
4151 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
4156 /* Set firstlineno if it wasn't explicitly set. */
4157 if (!c
->u
->u_firstlineno
) {
4158 if (entryblock
&& entryblock
->b_instr
)
4159 c
->u
->u_firstlineno
= entryblock
->b_instr
->i_lineno
;
4161 c
->u
->u_firstlineno
= 1;
4163 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
4165 dfs(c
, entryblock
, &a
);
4167 /* Can't modify the bytecode after computing jump offsets. */
4168 assemble_jump_offsets(&a
, c
);
4170 /* Emit code in reverse postorder from dfs. */
4171 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
4172 b
= a
.a_postorder
[i
];
4173 for (j
= 0; j
< b
->b_iused
; j
++)
4174 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
4178 if (_PyBytes_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
4180 if (_PyBytes_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
4183 co
= makecode(c
, &a
);