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
)
914 /* necessary to make sure types aren't coerced (e.g., int and long) */
915 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
916 if (PyFloat_Check(o
)) {
917 d
= PyFloat_AS_DOUBLE(o
);
918 /* all we need is to make the tuple different in either the 0.0
919 * or -0.0 case from all others, just to avoid the "coercion".
921 if (d
== 0.0 && copysign(1.0, d
) < 0.0)
922 t
= PyTuple_Pack(3, o
, o
->ob_type
, Py_None
);
924 t
= PyTuple_Pack(2, o
, o
->ob_type
);
926 #ifndef WITHOUT_COMPLEX
927 else if (PyComplex_Check(o
)) {
929 int real_negzero
, imag_negzero
;
930 /* For the complex case we must make complex(x, 0.)
931 different from complex(x, -0.) and complex(0., y)
932 different from complex(-0., y), for any x and y.
933 All four complex zeros must be distinguished.*/
934 z
= PyComplex_AsCComplex(o
);
935 real_negzero
= z
.real
== 0.0 && copysign(1.0, z
.real
) < 0.0;
936 imag_negzero
= z
.imag
== 0.0 && copysign(1.0, z
.imag
) < 0.0;
937 if (real_negzero
&& imag_negzero
) {
938 t
= PyTuple_Pack(5, o
, o
->ob_type
,
939 Py_None
, Py_None
, Py_None
);
941 else if (imag_negzero
) {
942 t
= PyTuple_Pack(4, o
, o
->ob_type
, Py_None
, Py_None
);
944 else if (real_negzero
) {
945 t
= PyTuple_Pack(3, o
, o
->ob_type
, Py_None
);
948 t
= PyTuple_Pack(2, o
, o
->ob_type
);
951 #endif /* WITHOUT_COMPLEX */
953 t
= PyTuple_Pack(2, o
, o
->ob_type
);
958 v
= PyDict_GetItem(dict
, t
);
960 if (PyErr_Occurred())
962 arg
= PyDict_Size(dict
);
963 v
= PyLong_FromLong(arg
);
968 if (PyDict_SetItem(dict
, t
, v
) < 0) {
976 arg
= PyLong_AsLong(v
);
982 compiler_addop_o(struct compiler
*c
, int opcode
, PyObject
*dict
,
985 int arg
= compiler_add_o(c
, dict
, o
);
988 return compiler_addop_i(c
, opcode
, arg
);
992 compiler_addop_name(struct compiler
*c
, int opcode
, PyObject
*dict
,
996 PyObject
*mangled
= _Py_Mangle(c
->u
->u_private
, o
);
999 arg
= compiler_add_o(c
, dict
, mangled
);
1003 return compiler_addop_i(c
, opcode
, arg
);
1006 /* Add an opcode with an integer argument.
1007 Returns 0 on failure, 1 on success.
1011 compiler_addop_i(struct compiler
*c
, int opcode
, int oparg
)
1015 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1018 i
= &c
->u
->u_curblock
->b_instr
[off
];
1019 i
->i_opcode
= opcode
;
1022 compiler_set_lineno(c
, off
);
1027 compiler_addop_j(struct compiler
*c
, int opcode
, basicblock
*b
, int absolute
)
1033 off
= compiler_next_instr(c
, c
->u
->u_curblock
);
1036 i
= &c
->u
->u_curblock
->b_instr
[off
];
1037 i
->i_opcode
= opcode
;
1044 compiler_set_lineno(c
, off
);
1048 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1049 like to find better names.) NEW_BLOCK() creates a new block and sets
1050 it as the current block. NEXT_BLOCK() also creates an implicit jump
1051 from the current block to the new block.
1054 /* The returns inside these macros make it impossible to decref objects
1055 created in the local function. Local objects should use the arena.
1059 #define NEW_BLOCK(C) { \
1060 if (compiler_use_new_block((C)) == NULL) \
1064 #define NEXT_BLOCK(C) { \
1065 if (compiler_next_block((C)) == NULL) \
1069 #define ADDOP(C, OP) { \
1070 if (!compiler_addop((C), (OP))) \
1074 #define ADDOP_IN_SCOPE(C, OP) { \
1075 if (!compiler_addop((C), (OP))) { \
1076 compiler_exit_scope(c); \
1081 #define ADDOP_O(C, OP, O, TYPE) { \
1082 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1086 #define ADDOP_NAME(C, OP, O, TYPE) { \
1087 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1091 #define ADDOP_I(C, OP, O) { \
1092 if (!compiler_addop_i((C), (OP), (O))) \
1096 #define ADDOP_JABS(C, OP, O) { \
1097 if (!compiler_addop_j((C), (OP), (O), 1)) \
1101 #define ADDOP_JREL(C, OP, O) { \
1102 if (!compiler_addop_j((C), (OP), (O), 0)) \
1106 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1107 the ASDL name to synthesize the name of the C type and the visit function.
1110 #define VISIT(C, TYPE, V) {\
1111 if (!compiler_visit_ ## TYPE((C), (V))) \
1115 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1116 if (!compiler_visit_ ## TYPE((C), (V))) { \
1117 compiler_exit_scope(c); \
1122 #define VISIT_SLICE(C, V, CTX) {\
1123 if (!compiler_visit_slice((C), (V), (CTX))) \
1127 #define VISIT_SEQ(C, TYPE, SEQ) { \
1129 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1130 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1131 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1132 if (!compiler_visit_ ## TYPE((C), elt)) \
1137 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1142 if (!compiler_visit_ ## TYPE((C), elt)) { \
1143 compiler_exit_scope(c); \
1150 compiler_isdocstring(stmt_ty s
)
1152 if (s
->kind
!= Expr_kind
)
1154 return s
->v
.Expr
.value
->kind
== Str_kind
;
1157 /* Compile a sequence of statements, checking for a docstring. */
1160 compiler_body(struct compiler
*c
, asdl_seq
*stmts
)
1165 if (!asdl_seq_LEN(stmts
))
1167 st
= (stmt_ty
)asdl_seq_GET(stmts
, 0);
1168 if (compiler_isdocstring(st
) && Py_OptimizeFlag
< 2) {
1169 /* don't generate docstrings if -OO */
1171 VISIT(c
, expr
, st
->v
.Expr
.value
);
1172 if (!compiler_nameop(c
, __doc__
, Store
))
1175 for (; i
< asdl_seq_LEN(stmts
); i
++)
1176 VISIT(c
, stmt
, (stmt_ty
)asdl_seq_GET(stmts
, i
));
1180 static PyCodeObject
*
1181 compiler_mod(struct compiler
*c
, mod_ty mod
)
1185 static PyObject
*module
;
1187 module
= PyUnicode_InternFromString("<module>");
1191 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1192 if (!compiler_enter_scope(c
, module
, mod
, 0))
1194 switch (mod
->kind
) {
1196 if (!compiler_body(c
, mod
->v
.Module
.body
)) {
1197 compiler_exit_scope(c
);
1201 case Interactive_kind
:
1202 c
->c_interactive
= 1;
1203 VISIT_SEQ_IN_SCOPE(c
, stmt
,
1204 mod
->v
.Interactive
.body
);
1206 case Expression_kind
:
1207 VISIT_IN_SCOPE(c
, expr
, mod
->v
.Expression
.body
);
1211 PyErr_SetString(PyExc_SystemError
,
1212 "suite should not be possible");
1215 PyErr_Format(PyExc_SystemError
,
1216 "module kind %d should not be possible",
1220 co
= assemble(c
, addNone
);
1221 compiler_exit_scope(c
);
1225 /* The test for LOCAL must come before the test for FREE in order to
1226 handle classes where name is both local and free. The local var is
1227 a method and the free var is a free var referenced within a method.
1231 get_ref_type(struct compiler
*c
, PyObject
*name
)
1233 int scope
= PyST_GetScope(c
->u
->u_ste
, name
);
1236 PyOS_snprintf(buf
, sizeof(buf
),
1237 "unknown scope for %.100s in %.100s(%s) in %s\n"
1238 "symbols: %s\nlocals: %s\nglobals: %s",
1239 PyBytes_AS_STRING(name
),
1240 PyBytes_AS_STRING(c
->u
->u_name
),
1241 PyObject_REPR(c
->u
->u_ste
->ste_id
),
1243 PyObject_REPR(c
->u
->u_ste
->ste_symbols
),
1244 PyObject_REPR(c
->u
->u_varnames
),
1245 PyObject_REPR(c
->u
->u_names
)
1254 compiler_lookup_arg(PyObject
*dict
, PyObject
*name
)
1257 k
= PyTuple_Pack(2, name
, name
->ob_type
);
1260 v
= PyDict_GetItem(dict
, k
);
1264 return PyLong_AS_LONG(v
);
1268 compiler_make_closure(struct compiler
*c
, PyCodeObject
*co
, int args
)
1270 int i
, free
= PyCode_GetNumFree(co
);
1272 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1273 ADDOP_I(c
, MAKE_FUNCTION
, args
);
1276 for (i
= 0; i
< free
; ++i
) {
1277 /* Bypass com_addop_varname because it will generate
1278 LOAD_DEREF but LOAD_CLOSURE is needed.
1280 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
1283 /* Special case: If a class contains a method with a
1284 free variable that has the same name as a method,
1285 the name will be considered free *and* local in the
1286 class. It should be handled by the closure, as
1287 well as by the normal name loookup logic.
1289 reftype
= get_ref_type(c
, name
);
1290 if (reftype
== CELL
)
1291 arg
= compiler_lookup_arg(c
->u
->u_cellvars
, name
);
1292 else /* (reftype == FREE) */
1293 arg
= compiler_lookup_arg(c
->u
->u_freevars
, name
);
1296 "lookup %s in %s %d %d\n"
1297 "freevars of %s: %s\n",
1298 PyObject_REPR(name
),
1299 PyBytes_AS_STRING(c
->u
->u_name
),
1301 _PyUnicode_AsString(co
->co_name
),
1302 PyObject_REPR(co
->co_freevars
));
1303 Py_FatalError("compiler_make_closure()");
1305 ADDOP_I(c
, LOAD_CLOSURE
, arg
);
1307 ADDOP_I(c
, BUILD_TUPLE
, free
);
1308 ADDOP_O(c
, LOAD_CONST
, (PyObject
*)co
, consts
);
1309 ADDOP_I(c
, MAKE_CLOSURE
, args
);
1314 compiler_decorators(struct compiler
*c
, asdl_seq
* decos
)
1321 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1322 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(decos
, i
));
1328 compiler_visit_kwonlydefaults(struct compiler
*c
, asdl_seq
*kwonlyargs
,
1329 asdl_seq
*kw_defaults
)
1331 int i
, default_count
= 0;
1332 for (i
= 0; i
< asdl_seq_LEN(kwonlyargs
); i
++) {
1333 arg_ty arg
= asdl_seq_GET(kwonlyargs
, i
);
1334 expr_ty default_
= asdl_seq_GET(kw_defaults
, i
);
1336 ADDOP_O(c
, LOAD_CONST
, arg
->arg
, consts
);
1337 if (!compiler_visit_expr(c
, default_
)) {
1343 return default_count
;
1347 compiler_visit_argannotation(struct compiler
*c
, identifier id
,
1348 expr_ty annotation
, PyObject
*names
)
1351 VISIT(c
, expr
, annotation
);
1352 if (PyList_Append(names
, id
))
1359 compiler_visit_argannotations(struct compiler
*c
, asdl_seq
* args
,
1363 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1364 arg_ty arg
= (arg_ty
)asdl_seq_GET(args
, i
);
1365 error
= compiler_visit_argannotation(
1377 compiler_visit_annotations(struct compiler
*c
, arguments_ty args
,
1380 /* Push arg annotations and a list of the argument names. Return the #
1381 of items pushed. The expressions are evaluated out-of-order wrt the
1384 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1386 static identifier return_str
;
1389 names
= PyList_New(0);
1393 if (compiler_visit_argannotations(c
, args
->args
, names
))
1395 if (args
->varargannotation
&&
1396 compiler_visit_argannotation(c
, args
->vararg
,
1397 args
->varargannotation
, names
))
1399 if (compiler_visit_argannotations(c
, args
->kwonlyargs
, names
))
1401 if (args
->kwargannotation
&&
1402 compiler_visit_argannotation(c
, args
->kwarg
,
1403 args
->kwargannotation
, names
))
1407 return_str
= PyUnicode_InternFromString("return");
1411 if (compiler_visit_argannotation(c
, return_str
, returns
, names
)) {
1415 len
= PyList_GET_SIZE(names
);
1417 /* len must fit in 16 bits, and len is incremented below */
1418 PyErr_SetString(PyExc_SyntaxError
,
1419 "too many annotations");
1423 /* convert names to a tuple and place on stack */
1426 PyObject
*s
= PyTuple_New(len
);
1429 for (i
= 0; i
< len
; i
++) {
1430 elt
= PyList_GET_ITEM(names
, i
);
1432 PyTuple_SET_ITEM(s
, i
, elt
);
1434 ADDOP_O(c
, LOAD_CONST
, s
, consts
);
1436 len
++; /* include the just-pushed tuple */
1447 compiler_function(struct compiler
*c
, stmt_ty s
)
1450 PyObject
*first_const
= Py_None
;
1451 arguments_ty args
= s
->v
.FunctionDef
.args
;
1452 expr_ty returns
= s
->v
.FunctionDef
.returns
;
1453 asdl_seq
* decos
= s
->v
.FunctionDef
.decorator_list
;
1455 int i
, n
, docstring
, kw_default_count
= 0, arglength
;
1456 int num_annotations
;
1458 assert(s
->kind
== FunctionDef_kind
);
1460 if (!compiler_decorators(c
, decos
))
1462 if (args
->kwonlyargs
) {
1463 int res
= compiler_visit_kwonlydefaults(c
, args
->kwonlyargs
,
1467 kw_default_count
= res
;
1470 VISIT_SEQ(c
, expr
, args
->defaults
);
1471 num_annotations
= compiler_visit_annotations(c
, args
, returns
);
1472 if (num_annotations
< 0)
1474 assert((num_annotations
& 0xFFFF) == num_annotations
);
1476 if (!compiler_enter_scope(c
, s
->v
.FunctionDef
.name
, (void *)s
,
1480 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, 0);
1481 docstring
= compiler_isdocstring(st
);
1482 if (docstring
&& Py_OptimizeFlag
< 2)
1483 first_const
= st
->v
.Expr
.value
->v
.Str
.s
;
1484 if (compiler_add_o(c
, c
->u
->u_consts
, first_const
) < 0) {
1485 compiler_exit_scope(c
);
1489 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1490 c
->u
->u_kwonlyargcount
= asdl_seq_LEN(args
->kwonlyargs
);
1491 n
= asdl_seq_LEN(s
->v
.FunctionDef
.body
);
1492 /* if there was a docstring, we need to skip the first statement */
1493 for (i
= docstring
; i
< n
; i
++) {
1494 st
= (stmt_ty
)asdl_seq_GET(s
->v
.FunctionDef
.body
, i
);
1495 VISIT_IN_SCOPE(c
, stmt
, st
);
1497 co
= assemble(c
, 1);
1498 compiler_exit_scope(c
);
1502 arglength
= asdl_seq_LEN(args
->defaults
);
1503 arglength
|= kw_default_count
<< 8;
1504 arglength
|= num_annotations
<< 16;
1505 compiler_make_closure(c
, co
, arglength
);
1509 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1510 ADDOP_I(c
, CALL_FUNCTION
, 1);
1513 return compiler_nameop(c
, s
->v
.FunctionDef
.name
, Store
);
1517 compiler_class(struct compiler
*c
, stmt_ty s
)
1522 asdl_seq
* decos
= s
->v
.ClassDef
.decorator_list
;
1524 if (!compiler_decorators(c
, decos
))
1527 /* ultimately generate code for:
1528 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1530 <func> is a function/closure created from the class body;
1531 it has a single argument (__locals__) where the dict
1532 (or MutableSequence) representing the locals is passed
1533 <name> is the class name
1534 <bases> is the positional arguments and *varargs argument
1535 <keywords> is the keyword arguments and **kwds argument
1536 This borrows from compiler_call.
1539 /* 1. compile the class body into a code object */
1540 if (!compiler_enter_scope(c
, s
->v
.ClassDef
.name
, (void *)s
, s
->lineno
))
1542 /* this block represents what we do in the new scope */
1544 /* use the class name for name mangling */
1545 Py_INCREF(s
->v
.ClassDef
.name
);
1546 Py_XDECREF(c
->u
->u_private
);
1547 c
->u
->u_private
= s
->v
.ClassDef
.name
;
1548 /* force it to have one mandatory argument */
1549 c
->u
->u_argcount
= 1;
1550 /* load the first argument (__locals__) ... */
1551 ADDOP_I(c
, LOAD_FAST
, 0);
1552 /* ... and store it into f_locals */
1553 ADDOP_IN_SCOPE(c
, STORE_LOCALS
);
1554 /* load (global) __name__ ... */
1555 str
= PyUnicode_InternFromString("__name__");
1556 if (!str
|| !compiler_nameop(c
, str
, Load
)) {
1558 compiler_exit_scope(c
);
1562 /* ... and store it as __module__ */
1563 str
= PyUnicode_InternFromString("__module__");
1564 if (!str
|| !compiler_nameop(c
, str
, Store
)) {
1566 compiler_exit_scope(c
);
1570 /* compile the body proper */
1571 if (!compiler_body(c
, s
->v
.ClassDef
.body
)) {
1572 compiler_exit_scope(c
);
1575 /* return the (empty) __class__ cell */
1576 str
= PyUnicode_InternFromString("__class__");
1578 compiler_exit_scope(c
);
1581 i
= compiler_lookup_arg(c
->u
->u_cellvars
, str
);
1584 /* This happens when nobody references the cell */
1587 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1590 /* Return the cell where to store __class__ */
1591 ADDOP_I(c
, LOAD_CLOSURE
, i
);
1593 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1594 /* create the code object */
1595 co
= assemble(c
, 1);
1597 /* leave the new scope */
1598 compiler_exit_scope(c
);
1602 /* 2. load the 'build_class' function */
1603 ADDOP(c
, LOAD_BUILD_CLASS
);
1605 /* 3. load a function (or closure) made from the code object */
1606 compiler_make_closure(c
, co
, 0);
1609 /* 4. load class name */
1610 ADDOP_O(c
, LOAD_CONST
, s
->v
.ClassDef
.name
, consts
);
1612 /* 5. generate the rest of the code for the call */
1613 if (!compiler_call_helper(c
, 2,
1614 s
->v
.ClassDef
.bases
,
1615 s
->v
.ClassDef
.keywords
,
1616 s
->v
.ClassDef
.starargs
,
1617 s
->v
.ClassDef
.kwargs
))
1620 /* 6. apply decorators */
1621 for (i
= 0; i
< asdl_seq_LEN(decos
); i
++) {
1622 ADDOP_I(c
, CALL_FUNCTION
, 1);
1625 /* 7. store into <name> */
1626 if (!compiler_nameop(c
, s
->v
.ClassDef
.name
, Store
))
1632 compiler_ifexp(struct compiler
*c
, expr_ty e
)
1634 basicblock
*end
, *next
;
1636 assert(e
->kind
== IfExp_kind
);
1637 end
= compiler_new_block(c
);
1640 next
= compiler_new_block(c
);
1643 VISIT(c
, expr
, e
->v
.IfExp
.test
);
1644 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, next
);
1645 VISIT(c
, expr
, e
->v
.IfExp
.body
);
1646 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1647 compiler_use_next_block(c
, next
);
1648 VISIT(c
, expr
, e
->v
.IfExp
.orelse
);
1649 compiler_use_next_block(c
, end
);
1654 compiler_lambda(struct compiler
*c
, expr_ty e
)
1657 static identifier name
;
1658 int kw_default_count
= 0, arglength
;
1659 arguments_ty args
= e
->v
.Lambda
.args
;
1660 assert(e
->kind
== Lambda_kind
);
1663 name
= PyUnicode_InternFromString("<lambda>");
1668 if (args
->kwonlyargs
) {
1669 int res
= compiler_visit_kwonlydefaults(c
, args
->kwonlyargs
,
1671 if (res
< 0) return 0;
1672 kw_default_count
= res
;
1675 VISIT_SEQ(c
, expr
, args
->defaults
);
1676 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
1679 c
->u
->u_argcount
= asdl_seq_LEN(args
->args
);
1680 c
->u
->u_kwonlyargcount
= asdl_seq_LEN(args
->kwonlyargs
);
1681 VISIT_IN_SCOPE(c
, expr
, e
->v
.Lambda
.body
);
1682 if (c
->u
->u_ste
->ste_generator
) {
1683 ADDOP_IN_SCOPE(c
, POP_TOP
);
1686 ADDOP_IN_SCOPE(c
, RETURN_VALUE
);
1688 co
= assemble(c
, 1);
1689 compiler_exit_scope(c
);
1693 arglength
= asdl_seq_LEN(args
->defaults
);
1694 arglength
|= kw_default_count
<< 8;
1695 compiler_make_closure(c
, co
, arglength
);
1702 compiler_if(struct compiler
*c
, stmt_ty s
)
1704 basicblock
*end
, *next
;
1706 assert(s
->kind
== If_kind
);
1707 end
= compiler_new_block(c
);
1711 constant
= expr_constant(s
->v
.If
.test
);
1712 /* constant = 0: "if 0"
1713 * constant = 1: "if 1", "if 2", ...
1714 * constant = -1: rest */
1715 if (constant
== 0) {
1717 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1718 } else if (constant
== 1) {
1719 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1721 if (s
->v
.If
.orelse
) {
1722 next
= compiler_new_block(c
);
1728 VISIT(c
, expr
, s
->v
.If
.test
);
1729 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, next
);
1730 VISIT_SEQ(c
, stmt
, s
->v
.If
.body
);
1731 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
1732 if (s
->v
.If
.orelse
) {
1733 compiler_use_next_block(c
, next
);
1734 VISIT_SEQ(c
, stmt
, s
->v
.If
.orelse
);
1737 compiler_use_next_block(c
, end
);
1742 compiler_for(struct compiler
*c
, stmt_ty s
)
1744 basicblock
*start
, *cleanup
, *end
;
1746 start
= compiler_new_block(c
);
1747 cleanup
= compiler_new_block(c
);
1748 end
= compiler_new_block(c
);
1749 if (start
== NULL
|| end
== NULL
|| cleanup
== NULL
)
1751 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1752 if (!compiler_push_fblock(c
, LOOP
, start
))
1754 VISIT(c
, expr
, s
->v
.For
.iter
);
1756 compiler_use_next_block(c
, start
);
1757 /* for expressions must be traced on each iteration,
1758 so we need to set an extra line number. */
1759 c
->u
->u_lineno_set
= 0;
1760 ADDOP_JREL(c
, FOR_ITER
, cleanup
);
1761 VISIT(c
, expr
, s
->v
.For
.target
);
1762 VISIT_SEQ(c
, stmt
, s
->v
.For
.body
);
1763 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
1764 compiler_use_next_block(c
, cleanup
);
1765 ADDOP(c
, POP_BLOCK
);
1766 compiler_pop_fblock(c
, LOOP
, start
);
1767 VISIT_SEQ(c
, stmt
, s
->v
.For
.orelse
);
1768 compiler_use_next_block(c
, end
);
1773 compiler_while(struct compiler
*c
, stmt_ty s
)
1775 basicblock
*loop
, *orelse
, *end
, *anchor
= NULL
;
1776 int constant
= expr_constant(s
->v
.While
.test
);
1778 if (constant
== 0) {
1779 if (s
->v
.While
.orelse
)
1780 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1783 loop
= compiler_new_block(c
);
1784 end
= compiler_new_block(c
);
1785 if (constant
== -1) {
1786 anchor
= compiler_new_block(c
);
1790 if (loop
== NULL
|| end
== NULL
)
1792 if (s
->v
.While
.orelse
) {
1793 orelse
= compiler_new_block(c
);
1800 ADDOP_JREL(c
, SETUP_LOOP
, end
);
1801 compiler_use_next_block(c
, loop
);
1802 if (!compiler_push_fblock(c
, LOOP
, loop
))
1804 if (constant
== -1) {
1805 /* while expressions must be traced on each iteration,
1806 so we need to set an extra line number. */
1807 c
->u
->u_lineno_set
= 0;
1808 VISIT(c
, expr
, s
->v
.While
.test
);
1809 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, anchor
);
1811 VISIT_SEQ(c
, stmt
, s
->v
.While
.body
);
1812 ADDOP_JABS(c
, JUMP_ABSOLUTE
, loop
);
1814 /* XXX should the two POP instructions be in a separate block
1815 if there is no else clause ?
1818 if (constant
== -1) {
1819 compiler_use_next_block(c
, anchor
);
1820 ADDOP(c
, POP_BLOCK
);
1822 compiler_pop_fblock(c
, LOOP
, loop
);
1823 if (orelse
!= NULL
) /* what if orelse is just pass? */
1824 VISIT_SEQ(c
, stmt
, s
->v
.While
.orelse
);
1825 compiler_use_next_block(c
, end
);
1831 compiler_continue(struct compiler
*c
)
1833 static const char LOOP_ERROR_MSG
[] = "'continue' not properly in loop";
1834 static const char IN_FINALLY_ERROR_MSG
[] =
1835 "'continue' not supported inside 'finally' clause";
1838 if (!c
->u
->u_nfblocks
)
1839 return compiler_error(c
, LOOP_ERROR_MSG
);
1840 i
= c
->u
->u_nfblocks
- 1;
1841 switch (c
->u
->u_fblock
[i
].fb_type
) {
1843 ADDOP_JABS(c
, JUMP_ABSOLUTE
, c
->u
->u_fblock
[i
].fb_block
);
1847 while (--i
>= 0 && c
->u
->u_fblock
[i
].fb_type
!= LOOP
) {
1848 /* Prevent continue anywhere under a finally
1849 even if hidden in a sub-try or except. */
1850 if (c
->u
->u_fblock
[i
].fb_type
== FINALLY_END
)
1851 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1854 return compiler_error(c
, LOOP_ERROR_MSG
);
1855 ADDOP_JABS(c
, CONTINUE_LOOP
, c
->u
->u_fblock
[i
].fb_block
);
1858 return compiler_error(c
, IN_FINALLY_ERROR_MSG
);
1864 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1870 L: <code for finalbody>
1873 The special instructions use the block stack. Each block
1874 stack entry contains the instruction that created it (here
1875 SETUP_FINALLY), the level of the value stack at the time the
1876 block stack entry was created, and a label (here L).
1879 Pushes the current value stack level and the label
1880 onto the block stack.
1882 Pops en entry from the block stack, and pops the value
1883 stack until its level is the same as indicated on the
1884 block stack. (The label is ignored.)
1886 Pops a variable number of entries from the *value* stack
1887 and re-raises the exception they specify. The number of
1888 entries popped depends on the (pseudo) exception type.
1890 The block stack is unwound when an exception is raised:
1891 when a SETUP_FINALLY entry is found, the exception is pushed
1892 onto the value stack (and the exception condition is cleared),
1893 and the interpreter jumps to the label gotten from the block
1898 compiler_try_finally(struct compiler
*c
, stmt_ty s
)
1900 basicblock
*body
, *end
;
1901 body
= compiler_new_block(c
);
1902 end
= compiler_new_block(c
);
1903 if (body
== NULL
|| end
== NULL
)
1906 ADDOP_JREL(c
, SETUP_FINALLY
, end
);
1907 compiler_use_next_block(c
, body
);
1908 if (!compiler_push_fblock(c
, FINALLY_TRY
, body
))
1910 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.body
);
1911 ADDOP(c
, POP_BLOCK
);
1912 compiler_pop_fblock(c
, FINALLY_TRY
, body
);
1914 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
1915 compiler_use_next_block(c
, end
);
1916 if (!compiler_push_fblock(c
, FINALLY_END
, end
))
1918 VISIT_SEQ(c
, stmt
, s
->v
.TryFinally
.finalbody
);
1919 ADDOP(c
, END_FINALLY
);
1920 compiler_pop_fblock(c
, FINALLY_END
, end
);
1926 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
1927 (The contents of the value stack is shown in [], with the top
1928 at the right; 'tb' is trace-back info, 'val' the exception's
1929 associated value, and 'exc' the exception.)
1931 Value stack Label Instruction Argument
1937 [tb, val, exc] L1: DUP )
1938 [tb, val, exc, exc] <evaluate E1> )
1939 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1940 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1942 [tb, val] <assign to V1> (or POP if no V1)
1947 [tb, val, exc] L2: DUP
1948 .............................etc.......................
1950 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1952 [] L0: <next statement>
1954 Of course, parts are not generated if Vi or Ei is not present.
1957 compiler_try_except(struct compiler
*c
, stmt_ty s
)
1959 basicblock
*body
, *orelse
, *except
, *end
;
1962 body
= compiler_new_block(c
);
1963 except
= compiler_new_block(c
);
1964 orelse
= compiler_new_block(c
);
1965 end
= compiler_new_block(c
);
1966 if (body
== NULL
|| except
== NULL
|| orelse
== NULL
|| end
== NULL
)
1968 ADDOP_JREL(c
, SETUP_EXCEPT
, except
);
1969 compiler_use_next_block(c
, body
);
1970 if (!compiler_push_fblock(c
, EXCEPT
, body
))
1972 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.body
);
1973 ADDOP(c
, POP_BLOCK
);
1974 compiler_pop_fblock(c
, EXCEPT
, body
);
1975 ADDOP_JREL(c
, JUMP_FORWARD
, orelse
);
1976 n
= asdl_seq_LEN(s
->v
.TryExcept
.handlers
);
1977 compiler_use_next_block(c
, except
);
1978 for (i
= 0; i
< n
; i
++) {
1979 excepthandler_ty handler
= (excepthandler_ty
)asdl_seq_GET(
1980 s
->v
.TryExcept
.handlers
, i
);
1981 if (!handler
->v
.ExceptHandler
.type
&& i
< n
-1)
1982 return compiler_error(c
, "default 'except:' must be last");
1983 c
->u
->u_lineno_set
= 0;
1984 c
->u
->u_lineno
= handler
->lineno
;
1985 except
= compiler_new_block(c
);
1988 if (handler
->v
.ExceptHandler
.type
) {
1990 VISIT(c
, expr
, handler
->v
.ExceptHandler
.type
);
1991 ADDOP_I(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
1992 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, except
);
1995 if (handler
->v
.ExceptHandler
.name
) {
1996 basicblock
*cleanup_end
, *cleanup_body
;
1998 cleanup_end
= compiler_new_block(c
);
1999 cleanup_body
= compiler_new_block(c
);
2000 if(!(cleanup_end
|| cleanup_body
))
2003 compiler_nameop(c
, handler
->v
.ExceptHandler
.name
, Store
);
2009 except type as name:
2018 ADDOP_JREL(c
, SETUP_FINALLY
, cleanup_end
);
2019 compiler_use_next_block(c
, cleanup_body
);
2020 if (!compiler_push_fblock(c
, FINALLY_TRY
, cleanup_body
))
2024 VISIT_SEQ(c
, stmt
, handler
->v
.ExceptHandler
.body
);
2025 ADDOP(c
, POP_BLOCK
);
2026 ADDOP(c
, POP_EXCEPT
);
2027 compiler_pop_fblock(c
, FINALLY_TRY
, cleanup_body
);
2030 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2031 compiler_use_next_block(c
, cleanup_end
);
2032 if (!compiler_push_fblock(c
, FINALLY_END
, cleanup_end
))
2036 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2037 compiler_nameop(c
, handler
->v
.ExceptHandler
.name
, Store
);
2040 compiler_nameop(c
, handler
->v
.ExceptHandler
.name
, Del
);
2042 ADDOP(c
, END_FINALLY
);
2043 compiler_pop_fblock(c
, FINALLY_END
, cleanup_end
);
2046 basicblock
*cleanup_body
;
2048 cleanup_body
= compiler_new_block(c
);
2054 compiler_use_next_block(c
, cleanup_body
);
2055 if (!compiler_push_fblock(c
, FINALLY_TRY
, cleanup_body
))
2057 VISIT_SEQ(c
, stmt
, handler
->v
.ExceptHandler
.body
);
2058 ADDOP(c
, POP_EXCEPT
);
2059 compiler_pop_fblock(c
, FINALLY_TRY
, cleanup_body
);
2061 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2062 compiler_use_next_block(c
, except
);
2064 ADDOP(c
, END_FINALLY
);
2065 compiler_use_next_block(c
, orelse
);
2066 VISIT_SEQ(c
, stmt
, s
->v
.TryExcept
.orelse
);
2067 compiler_use_next_block(c
, end
);
2072 compiler_import_as(struct compiler
*c
, identifier name
, identifier asname
)
2074 /* The IMPORT_NAME opcode was already generated. This function
2075 merely needs to bind the result to a name.
2077 If there is a dot in name, we need to split it and emit a
2078 LOAD_ATTR for each name.
2080 const Py_UNICODE
*src
= PyUnicode_AS_UNICODE(name
);
2081 const Py_UNICODE
*dot
= Py_UNICODE_strchr(src
, '.');
2083 /* Consume the base module name to get the first attribute */
2086 /* NB src is only defined when dot != NULL */
2088 dot
= Py_UNICODE_strchr(src
, '.');
2089 attr
= PyUnicode_FromUnicode(src
,
2090 dot
? dot
- src
: Py_UNICODE_strlen(src
));
2093 ADDOP_O(c
, LOAD_ATTR
, attr
, names
);
2098 return compiler_nameop(c
, asname
, Store
);
2102 compiler_import(struct compiler
*c
, stmt_ty s
)
2104 /* The Import node stores a module name like a.b.c as a single
2105 string. This is convenient for all cases except
2107 where we need to parse that string to extract the individual
2109 XXX Perhaps change the representation to make this case simpler?
2111 int i
, n
= asdl_seq_LEN(s
->v
.Import
.names
);
2113 for (i
= 0; i
< n
; i
++) {
2114 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.Import
.names
, i
);
2118 level
= PyLong_FromLong(0);
2122 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2124 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2125 ADDOP_NAME(c
, IMPORT_NAME
, alias
->name
, names
);
2127 if (alias
->asname
) {
2128 r
= compiler_import_as(c
, alias
->name
, alias
->asname
);
2133 identifier tmp
= alias
->name
;
2134 const Py_UNICODE
*base
= PyUnicode_AS_UNICODE(alias
->name
);
2135 Py_UNICODE
*dot
= Py_UNICODE_strchr(base
, '.');
2137 tmp
= PyUnicode_FromUnicode(base
,
2139 r
= compiler_nameop(c
, tmp
, Store
);
2151 compiler_from_import(struct compiler
*c
, stmt_ty s
)
2153 int i
, n
= asdl_seq_LEN(s
->v
.ImportFrom
.names
);
2155 PyObject
*names
= PyTuple_New(n
);
2161 level
= PyLong_FromLong(s
->v
.ImportFrom
.level
);
2167 /* build up the names */
2168 for (i
= 0; i
< n
; i
++) {
2169 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2170 Py_INCREF(alias
->name
);
2171 PyTuple_SET_ITEM(names
, i
, alias
->name
);
2174 if (s
->lineno
> c
->c_future
->ff_lineno
) {
2175 if (!PyUnicode_CompareWithASCIIString(s
->v
.ImportFrom
.module
,
2179 return compiler_error(c
,
2180 "from __future__ imports must occur "
2181 "at the beginning of the file");
2186 ADDOP_O(c
, LOAD_CONST
, level
, consts
);
2188 ADDOP_O(c
, LOAD_CONST
, names
, consts
);
2190 ADDOP_NAME(c
, IMPORT_NAME
, s
->v
.ImportFrom
.module
, names
);
2191 for (i
= 0; i
< n
; i
++) {
2192 alias_ty alias
= (alias_ty
)asdl_seq_GET(s
->v
.ImportFrom
.names
, i
);
2193 identifier store_name
;
2195 if (i
== 0 && *PyUnicode_AS_UNICODE(alias
->name
) == '*') {
2197 ADDOP(c
, IMPORT_STAR
);
2201 ADDOP_NAME(c
, IMPORT_FROM
, alias
->name
, names
);
2202 store_name
= alias
->name
;
2204 store_name
= alias
->asname
;
2206 if (!compiler_nameop(c
, store_name
, Store
)) {
2211 /* remove imported module */
2217 compiler_assert(struct compiler
*c
, stmt_ty s
)
2219 static PyObject
*assertion_error
= NULL
;
2222 if (Py_OptimizeFlag
)
2224 if (assertion_error
== NULL
) {
2225 assertion_error
= PyUnicode_InternFromString("AssertionError");
2226 if (assertion_error
== NULL
)
2229 if (s
->v
.Assert
.test
->kind
== Tuple_kind
&&
2230 asdl_seq_LEN(s
->v
.Assert
.test
->v
.Tuple
.elts
) > 0) {
2232 "assertion is always true, perhaps remove parentheses?";
2233 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, c
->c_filename
,
2234 c
->u
->u_lineno
, NULL
, NULL
) == -1)
2237 VISIT(c
, expr
, s
->v
.Assert
.test
);
2238 end
= compiler_new_block(c
);
2241 ADDOP_JABS(c
, POP_JUMP_IF_TRUE
, end
);
2242 ADDOP_O(c
, LOAD_GLOBAL
, assertion_error
, names
);
2243 if (s
->v
.Assert
.msg
) {
2244 VISIT(c
, expr
, s
->v
.Assert
.msg
);
2245 ADDOP_I(c
, CALL_FUNCTION
, 1);
2247 ADDOP_I(c
, RAISE_VARARGS
, 1);
2248 compiler_use_next_block(c
, end
);
2253 compiler_visit_stmt(struct compiler
*c
, stmt_ty s
)
2257 /* Always assign a lineno to the next instruction for a stmt. */
2258 c
->u
->u_lineno
= s
->lineno
;
2259 c
->u
->u_lineno_set
= 0;
2262 case FunctionDef_kind
:
2263 return compiler_function(c
, s
);
2265 return compiler_class(c
, s
);
2267 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
2268 return compiler_error(c
, "'return' outside function");
2269 if (s
->v
.Return
.value
) {
2270 VISIT(c
, expr
, s
->v
.Return
.value
);
2273 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
2274 ADDOP(c
, RETURN_VALUE
);
2277 VISIT_SEQ(c
, expr
, s
->v
.Delete
.targets
)
2280 n
= asdl_seq_LEN(s
->v
.Assign
.targets
);
2281 VISIT(c
, expr
, s
->v
.Assign
.value
);
2282 for (i
= 0; i
< n
; i
++) {
2286 (expr_ty
)asdl_seq_GET(s
->v
.Assign
.targets
, i
));
2289 case AugAssign_kind
:
2290 return compiler_augassign(c
, s
);
2292 return compiler_for(c
, s
);
2294 return compiler_while(c
, s
);
2296 return compiler_if(c
, s
);
2299 if (s
->v
.Raise
.exc
) {
2300 VISIT(c
, expr
, s
->v
.Raise
.exc
);
2302 if (s
->v
.Raise
.cause
) {
2303 VISIT(c
, expr
, s
->v
.Raise
.cause
);
2307 ADDOP_I(c
, RAISE_VARARGS
, n
);
2309 case TryExcept_kind
:
2310 return compiler_try_except(c
, s
);
2311 case TryFinally_kind
:
2312 return compiler_try_finally(c
, s
);
2314 return compiler_assert(c
, s
);
2316 return compiler_import(c
, s
);
2317 case ImportFrom_kind
:
2318 return compiler_from_import(c
, s
);
2323 if (c
->c_interactive
&& c
->c_nestlevel
<= 1) {
2324 VISIT(c
, expr
, s
->v
.Expr
.value
);
2325 ADDOP(c
, PRINT_EXPR
);
2327 else if (s
->v
.Expr
.value
->kind
!= Str_kind
&&
2328 s
->v
.Expr
.value
->kind
!= Num_kind
) {
2329 VISIT(c
, expr
, s
->v
.Expr
.value
);
2336 if (!compiler_in_loop(c
))
2337 return compiler_error(c
, "'break' outside loop");
2338 ADDOP(c
, BREAK_LOOP
);
2341 return compiler_continue(c
);
2343 return compiler_with(c
, s
);
2349 unaryop(unaryop_ty op
)
2353 return UNARY_INVERT
;
2357 return UNARY_POSITIVE
;
2359 return UNARY_NEGATIVE
;
2361 PyErr_Format(PyExc_SystemError
,
2362 "unary op %d should not be possible", op
);
2368 binop(struct compiler
*c
, operator_ty op
)
2374 return BINARY_SUBTRACT
;
2376 return BINARY_MULTIPLY
;
2378 return BINARY_TRUE_DIVIDE
;
2380 return BINARY_MODULO
;
2382 return BINARY_POWER
;
2384 return BINARY_LSHIFT
;
2386 return BINARY_RSHIFT
;
2394 return BINARY_FLOOR_DIVIDE
;
2396 PyErr_Format(PyExc_SystemError
,
2397 "binary op %d should not be possible", op
);
2421 return PyCmp_IS_NOT
;
2425 return PyCmp_NOT_IN
;
2432 inplace_binop(struct compiler
*c
, operator_ty op
)
2438 return INPLACE_SUBTRACT
;
2440 return INPLACE_MULTIPLY
;
2442 return INPLACE_TRUE_DIVIDE
;
2444 return INPLACE_MODULO
;
2446 return INPLACE_POWER
;
2448 return INPLACE_LSHIFT
;
2450 return INPLACE_RSHIFT
;
2458 return INPLACE_FLOOR_DIVIDE
;
2460 PyErr_Format(PyExc_SystemError
,
2461 "inplace binary op %d should not be possible", op
);
2467 compiler_nameop(struct compiler
*c
, identifier name
, expr_context_ty ctx
)
2470 enum { OP_FAST
, OP_GLOBAL
, OP_DEREF
, OP_NAME
} optype
;
2472 PyObject
*dict
= c
->u
->u_names
;
2474 /* XXX AugStore isn't used anywhere! */
2476 mangled
= _Py_Mangle(c
->u
->u_private
, name
);
2482 scope
= PyST_GetScope(c
->u
->u_ste
, mangled
);
2485 dict
= c
->u
->u_freevars
;
2489 dict
= c
->u
->u_cellvars
;
2493 if (c
->u
->u_ste
->ste_type
== FunctionBlock
)
2496 case GLOBAL_IMPLICIT
:
2497 if (c
->u
->u_ste
->ste_type
== FunctionBlock
&&
2498 !c
->u
->u_ste
->ste_unoptimized
)
2501 case GLOBAL_EXPLICIT
:
2505 /* scope can be 0 */
2509 /* XXX Leave assert here, but handle __doc__ and the like better */
2510 assert(scope
|| PyUnicode_AS_UNICODE(name
)[0] == '_');
2515 case Load
: op
= LOAD_DEREF
; break;
2516 case Store
: op
= STORE_DEREF
; break;
2521 PyErr_Format(PyExc_SyntaxError
,
2522 "can not delete variable '%S' referenced "
2529 PyErr_SetString(PyExc_SystemError
,
2530 "param invalid for deref variable");
2536 case Load
: op
= LOAD_FAST
; break;
2537 case Store
: op
= STORE_FAST
; break;
2538 case Del
: op
= DELETE_FAST
; break;
2544 PyErr_SetString(PyExc_SystemError
,
2545 "param invalid for local variable");
2548 ADDOP_O(c
, op
, mangled
, varnames
);
2553 case Load
: op
= LOAD_GLOBAL
; break;
2554 case Store
: op
= STORE_GLOBAL
; break;
2555 case Del
: op
= DELETE_GLOBAL
; break;
2561 PyErr_SetString(PyExc_SystemError
,
2562 "param invalid for global variable");
2568 case Load
: op
= LOAD_NAME
; break;
2569 case Store
: op
= STORE_NAME
; break;
2570 case Del
: op
= DELETE_NAME
; break;
2576 PyErr_SetString(PyExc_SystemError
,
2577 "param invalid for name variable");
2584 arg
= compiler_add_o(c
, dict
, mangled
);
2588 return compiler_addop_i(c
, op
, arg
);
2592 compiler_boolop(struct compiler
*c
, expr_ty e
)
2598 assert(e
->kind
== BoolOp_kind
);
2599 if (e
->v
.BoolOp
.op
== And
)
2600 jumpi
= JUMP_IF_FALSE_OR_POP
;
2602 jumpi
= JUMP_IF_TRUE_OR_POP
;
2603 end
= compiler_new_block(c
);
2606 s
= e
->v
.BoolOp
.values
;
2607 n
= asdl_seq_LEN(s
) - 1;
2609 for (i
= 0; i
< n
; ++i
) {
2610 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, i
));
2611 ADDOP_JABS(c
, jumpi
, end
);
2613 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(s
, n
));
2614 compiler_use_next_block(c
, end
);
2619 compiler_list(struct compiler
*c
, expr_ty e
)
2621 int n
= asdl_seq_LEN(e
->v
.List
.elts
);
2622 if (e
->v
.List
.ctx
== Store
) {
2623 int i
, seen_star
= 0;
2624 for (i
= 0; i
< n
; i
++) {
2625 expr_ty elt
= asdl_seq_GET(e
->v
.List
.elts
, i
);
2626 if (elt
->kind
== Starred_kind
&& !seen_star
) {
2627 if ((i
>= (1 << 8)) ||
2628 (n
-i
-1 >= (INT_MAX
>> 8)))
2629 return compiler_error(c
,
2630 "too many expressions in "
2631 "star-unpacking assignment");
2632 ADDOP_I(c
, UNPACK_EX
, (i
+ ((n
-i
-1) << 8)));
2634 asdl_seq_SET(e
->v
.List
.elts
, i
, elt
->v
.Starred
.value
);
2635 } else if (elt
->kind
== Starred_kind
) {
2636 return compiler_error(c
,
2637 "two starred expressions in assignment");
2641 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2644 VISIT_SEQ(c
, expr
, e
->v
.List
.elts
);
2645 if (e
->v
.List
.ctx
== Load
) {
2646 ADDOP_I(c
, BUILD_LIST
, n
);
2652 compiler_tuple(struct compiler
*c
, expr_ty e
)
2654 int n
= asdl_seq_LEN(e
->v
.Tuple
.elts
);
2655 if (e
->v
.Tuple
.ctx
== Store
) {
2656 int i
, seen_star
= 0;
2657 for (i
= 0; i
< n
; i
++) {
2658 expr_ty elt
= asdl_seq_GET(e
->v
.Tuple
.elts
, i
);
2659 if (elt
->kind
== Starred_kind
&& !seen_star
) {
2660 if ((i
>= (1 << 8)) ||
2661 (n
-i
-1 >= (INT_MAX
>> 8)))
2662 return compiler_error(c
,
2663 "too many expressions in "
2664 "star-unpacking assignment");
2665 ADDOP_I(c
, UNPACK_EX
, (i
+ ((n
-i
-1) << 8)));
2667 asdl_seq_SET(e
->v
.Tuple
.elts
, i
, elt
->v
.Starred
.value
);
2668 } else if (elt
->kind
== Starred_kind
) {
2669 return compiler_error(c
,
2670 "two starred expressions in assignment");
2674 ADDOP_I(c
, UNPACK_SEQUENCE
, n
);
2677 VISIT_SEQ(c
, expr
, e
->v
.Tuple
.elts
);
2678 if (e
->v
.Tuple
.ctx
== Load
) {
2679 ADDOP_I(c
, BUILD_TUPLE
, n
);
2685 compiler_compare(struct compiler
*c
, expr_ty e
)
2688 basicblock
*cleanup
= NULL
;
2690 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2691 VISIT(c
, expr
, e
->v
.Compare
.left
);
2692 n
= asdl_seq_LEN(e
->v
.Compare
.ops
);
2695 cleanup
= compiler_new_block(c
);
2696 if (cleanup
== NULL
)
2699 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, 0));
2701 for (i
= 1; i
< n
; i
++) {
2703 ADDOP(c
, ROT_THREE
);
2704 ADDOP_I(c
, COMPARE_OP
,
2705 cmpop((cmpop_ty
)(asdl_seq_GET(
2706 e
->v
.Compare
.ops
, i
- 1))));
2707 ADDOP_JABS(c
, JUMP_IF_FALSE_OR_POP
, cleanup
);
2711 (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, i
));
2713 VISIT(c
, expr
, (expr_ty
)asdl_seq_GET(e
->v
.Compare
.comparators
, n
- 1));
2714 ADDOP_I(c
, COMPARE_OP
,
2715 cmpop((cmpop_ty
)(asdl_seq_GET(e
->v
.Compare
.ops
, n
- 1))));
2717 basicblock
*end
= compiler_new_block(c
);
2720 ADDOP_JREL(c
, JUMP_FORWARD
, end
);
2721 compiler_use_next_block(c
, cleanup
);
2724 compiler_use_next_block(c
, end
);
2730 compiler_call(struct compiler
*c
, expr_ty e
)
2732 VISIT(c
, expr
, e
->v
.Call
.func
);
2733 return compiler_call_helper(c
, 0,
2740 /* shared code between compiler_call and compiler_class */
2742 compiler_call_helper(struct compiler
*c
,
2743 int n
, /* Args already pushed */
2751 n
+= asdl_seq_LEN(args
);
2752 VISIT_SEQ(c
, expr
, args
);
2754 VISIT_SEQ(c
, keyword
, keywords
);
2755 n
|= asdl_seq_LEN(keywords
) << 8;
2758 VISIT(c
, expr
, starargs
);
2762 VISIT(c
, expr
, kwargs
);
2767 ADDOP_I(c
, CALL_FUNCTION
, n
);
2770 ADDOP_I(c
, CALL_FUNCTION_VAR
, n
);
2773 ADDOP_I(c
, CALL_FUNCTION_KW
, n
);
2776 ADDOP_I(c
, CALL_FUNCTION_VAR_KW
, n
);
2783 /* List and set comprehensions and generator expressions work by creating a
2784 nested function to perform the actual iteration. This means that the
2785 iteration variables don't leak into the current scope.
2786 The defined function is called immediately following its definition, with the
2787 result of that call being the result of the expression.
2788 The LC/SC version returns the populated container, while the GE version is
2789 flagged in symtable.c as a generator, so it returns the generator object
2790 when the function is called.
2791 This code *knows* that the loop cannot contain break, continue, or return,
2792 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2795 - iterate over the generator sequence instead of using recursion
2799 compiler_comprehension_generator(struct compiler
*c
,
2800 asdl_seq
*generators
, int gen_index
,
2801 expr_ty elt
, expr_ty val
, int type
)
2803 /* generate code for the iterator, then each of the ifs,
2804 and then write to the element */
2806 comprehension_ty gen
;
2807 basicblock
*start
, *anchor
, *skip
, *if_cleanup
;
2810 start
= compiler_new_block(c
);
2811 skip
= compiler_new_block(c
);
2812 if_cleanup
= compiler_new_block(c
);
2813 anchor
= compiler_new_block(c
);
2815 if (start
== NULL
|| skip
== NULL
|| if_cleanup
== NULL
||
2819 gen
= (comprehension_ty
)asdl_seq_GET(generators
, gen_index
);
2821 if (gen_index
== 0) {
2822 /* Receive outermost iter as an implicit argument */
2823 c
->u
->u_argcount
= 1;
2824 ADDOP_I(c
, LOAD_FAST
, 0);
2827 /* Sub-iter - calculate on the fly */
2828 VISIT(c
, expr
, gen
->iter
);
2831 compiler_use_next_block(c
, start
);
2832 ADDOP_JREL(c
, FOR_ITER
, anchor
);
2834 VISIT(c
, expr
, gen
->target
);
2836 /* XXX this needs to be cleaned up...a lot! */
2837 n
= asdl_seq_LEN(gen
->ifs
);
2838 for (i
= 0; i
< n
; i
++) {
2839 expr_ty e
= (expr_ty
)asdl_seq_GET(gen
->ifs
, i
);
2841 ADDOP_JABS(c
, POP_JUMP_IF_FALSE
, if_cleanup
);
2845 if (++gen_index
< asdl_seq_LEN(generators
))
2846 if (!compiler_comprehension_generator(c
,
2847 generators
, gen_index
,
2851 /* only append after the last for generator */
2852 if (gen_index
>= asdl_seq_LEN(generators
)) {
2853 /* comprehension specific code */
2856 VISIT(c
, expr
, elt
);
2857 ADDOP(c
, YIELD_VALUE
);
2861 VISIT(c
, expr
, elt
);
2862 ADDOP_I(c
, LIST_APPEND
, gen_index
+ 1);
2865 VISIT(c
, expr
, elt
);
2866 ADDOP_I(c
, SET_ADD
, gen_index
+ 1);
2869 /* With 'd[k] = v', v is evaluated before k, so we do
2871 VISIT(c
, expr
, val
);
2872 VISIT(c
, expr
, elt
);
2873 ADDOP_I(c
, MAP_ADD
, gen_index
+ 1);
2879 compiler_use_next_block(c
, skip
);
2881 compiler_use_next_block(c
, if_cleanup
);
2882 ADDOP_JABS(c
, JUMP_ABSOLUTE
, start
);
2883 compiler_use_next_block(c
, anchor
);
2889 compiler_comprehension(struct compiler
*c
, expr_ty e
, int type
, identifier name
,
2890 asdl_seq
*generators
, expr_ty elt
, expr_ty val
)
2892 PyCodeObject
*co
= NULL
;
2893 expr_ty outermost_iter
;
2895 outermost_iter
= ((comprehension_ty
)
2896 asdl_seq_GET(generators
, 0))->iter
;
2898 if (!compiler_enter_scope(c
, name
, (void *)e
, e
->lineno
))
2901 if (type
!= COMP_GENEXP
) {
2914 PyErr_Format(PyExc_SystemError
,
2915 "unknown comprehension type %d", type
);
2916 goto error_in_scope
;
2922 if (!compiler_comprehension_generator(c
, generators
, 0, elt
,
2924 goto error_in_scope
;
2926 if (type
!= COMP_GENEXP
) {
2927 ADDOP(c
, RETURN_VALUE
);
2930 co
= assemble(c
, 1);
2931 compiler_exit_scope(c
);
2935 if (!compiler_make_closure(c
, co
, 0))
2939 VISIT(c
, expr
, outermost_iter
);
2941 ADDOP_I(c
, CALL_FUNCTION
, 1);
2944 compiler_exit_scope(c
);
2951 compiler_genexp(struct compiler
*c
, expr_ty e
)
2953 static identifier name
;
2955 name
= PyUnicode_FromString("<genexpr>");
2959 assert(e
->kind
== GeneratorExp_kind
);
2960 return compiler_comprehension(c
, e
, COMP_GENEXP
, name
,
2961 e
->v
.GeneratorExp
.generators
,
2962 e
->v
.GeneratorExp
.elt
, NULL
);
2966 compiler_listcomp(struct compiler
*c
, expr_ty e
)
2968 static identifier name
;
2970 name
= PyUnicode_FromString("<listcomp>");
2974 assert(e
->kind
== ListComp_kind
);
2975 return compiler_comprehension(c
, e
, COMP_LISTCOMP
, name
,
2976 e
->v
.ListComp
.generators
,
2977 e
->v
.ListComp
.elt
, NULL
);
2981 compiler_setcomp(struct compiler
*c
, expr_ty e
)
2983 static identifier name
;
2985 name
= PyUnicode_FromString("<setcomp>");
2989 assert(e
->kind
== SetComp_kind
);
2990 return compiler_comprehension(c
, e
, COMP_SETCOMP
, name
,
2991 e
->v
.SetComp
.generators
,
2992 e
->v
.SetComp
.elt
, NULL
);
2997 compiler_dictcomp(struct compiler
*c
, expr_ty e
)
2999 static identifier name
;
3001 name
= PyUnicode_FromString("<dictcomp>");
3005 assert(e
->kind
== DictComp_kind
);
3006 return compiler_comprehension(c
, e
, COMP_DICTCOMP
, name
,
3007 e
->v
.DictComp
.generators
,
3008 e
->v
.DictComp
.key
, e
->v
.DictComp
.value
);
3013 compiler_visit_keyword(struct compiler
*c
, keyword_ty k
)
3015 ADDOP_O(c
, LOAD_CONST
, k
->arg
, consts
);
3016 VISIT(c
, expr
, k
->value
);
3020 /* Test whether expression is constant. For constants, report
3021 whether they are true or false.
3023 Return values: 1 for true, 0 for false, -1 for non-constant.
3027 expr_constant(expr_ty e
)
3034 return PyObject_IsTrue(e
->v
.Num
.n
);
3036 return PyObject_IsTrue(e
->v
.Str
.s
);
3038 /* optimize away names that can't be reassigned */
3039 id
= PyBytes_AS_STRING(
3040 _PyUnicode_AsDefaultEncodedString(e
->v
.Name
.id
, NULL
));
3041 if (strcmp(id
, "True") == 0) return 1;
3042 if (strcmp(id
, "False") == 0) return 0;
3043 if (strcmp(id
, "None") == 0) return 0;
3044 if (strcmp(id
, "__debug__") == 0)
3045 return ! Py_OptimizeFlag
;
3053 Implements the with statement from PEP 343.
3055 The semantics outlined in that PEP are as follows:
3060 It is implemented roughly as:
3063 exit = context.__exit__ # not calling it
3064 value = context.__enter__()
3066 VAR = value # if VAR present in the syntax
3069 if an exception was raised:
3070 exc = copy of (exception, instance, traceback)
3072 exc = (None, None, None)
3076 compiler_with(struct compiler
*c
, stmt_ty s
)
3078 static identifier enter_attr
, exit_attr
;
3079 basicblock
*block
, *finally
;
3080 identifier tmpvalue
= NULL
, tmpexit
= NULL
;
3082 assert(s
->kind
== With_kind
);
3085 enter_attr
= PyUnicode_InternFromString("__enter__");
3090 exit_attr
= PyUnicode_InternFromString("__exit__");
3095 block
= compiler_new_block(c
);
3096 finally
= compiler_new_block(c
);
3097 if (!block
|| !finally
)
3100 if (s
->v
.With
.optional_vars
) {
3101 /* Create a temporary variable to hold context.__enter__().
3102 We need to do this rather than preserving it on the stack
3103 because SETUP_FINALLY remembers the stack level.
3104 We need to do the assignment *inside* the try/finally
3105 so that context.__exit__() is called when the assignment
3106 fails. But we need to call context.__enter__() *before*
3107 the try/finally so that if it fails we won't call
3110 tmpvalue
= compiler_new_tmpname(c
);
3111 if (tmpvalue
== NULL
)
3113 PyArena_AddPyObject(c
->c_arena
, tmpvalue
);
3115 tmpexit
= compiler_new_tmpname(c
);
3116 if (tmpexit
== NULL
)
3118 PyArena_AddPyObject(c
->c_arena
, tmpexit
);
3121 VISIT(c
, expr
, s
->v
.With
.context_expr
);
3123 /* Squirrel away context.__exit__ by stuffing it under context */
3125 ADDOP_O(c
, LOAD_ATTR
, exit_attr
, names
);
3126 if (!compiler_nameop(c
, tmpexit
, Store
))
3129 /* Call context.__enter__() */
3130 ADDOP_O(c
, LOAD_ATTR
, enter_attr
, names
);
3131 ADDOP_I(c
, CALL_FUNCTION
, 0);
3133 if (s
->v
.With
.optional_vars
) {
3134 /* Store it in tmpvalue */
3135 if (!compiler_nameop(c
, tmpvalue
, Store
))
3139 /* Discard result from context.__enter__() */
3143 /* Start the try block */
3144 ADDOP_JREL(c
, SETUP_FINALLY
, finally
);
3146 compiler_use_next_block(c
, block
);
3147 if (!compiler_push_fblock(c
, FINALLY_TRY
, block
)) {
3151 if (s
->v
.With
.optional_vars
) {
3152 /* Bind saved result of context.__enter__() to VAR */
3153 if (!compiler_nameop(c
, tmpvalue
, Load
) ||
3154 !compiler_nameop(c
, tmpvalue
, Del
))
3156 VISIT(c
, expr
, s
->v
.With
.optional_vars
);
3160 VISIT_SEQ(c
, stmt
, s
->v
.With
.body
);
3162 /* End of try block; start the finally block */
3163 ADDOP(c
, POP_BLOCK
);
3164 compiler_pop_fblock(c
, FINALLY_TRY
, block
);
3166 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3167 compiler_use_next_block(c
, finally
);
3168 if (!compiler_push_fblock(c
, FINALLY_END
, finally
))
3171 /* Finally block starts; context.__exit__ is on the stack under
3172 the exception or return information. Just issue our magic
3174 if (!compiler_nameop(c
, tmpexit
, Load
) ||
3175 !compiler_nameop(c
, tmpexit
, Del
))
3177 ADDOP(c
, WITH_CLEANUP
);
3179 /* Finally block ends. */
3180 ADDOP(c
, END_FINALLY
);
3181 compiler_pop_fblock(c
, FINALLY_END
, finally
);
3186 compiler_visit_expr(struct compiler
*c
, expr_ty e
)
3190 /* If expr e has a different line number than the last expr/stmt,
3191 set a new line number for the next instruction.
3193 if (e
->lineno
> c
->u
->u_lineno
) {
3194 c
->u
->u_lineno
= e
->lineno
;
3195 c
->u
->u_lineno_set
= 0;
3199 return compiler_boolop(c
, e
);
3201 VISIT(c
, expr
, e
->v
.BinOp
.left
);
3202 VISIT(c
, expr
, e
->v
.BinOp
.right
);
3203 ADDOP(c
, binop(c
, e
->v
.BinOp
.op
));
3206 VISIT(c
, expr
, e
->v
.UnaryOp
.operand
);
3207 ADDOP(c
, unaryop(e
->v
.UnaryOp
.op
));
3210 return compiler_lambda(c
, e
);
3212 return compiler_ifexp(c
, e
);
3214 n
= asdl_seq_LEN(e
->v
.Dict
.values
);
3215 ADDOP_I(c
, BUILD_MAP
, (n
>0xFFFF ? 0xFFFF : n
));
3216 for (i
= 0; i
< n
; i
++) {
3218 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.values
, i
));
3220 (expr_ty
)asdl_seq_GET(e
->v
.Dict
.keys
, i
));
3221 ADDOP(c
, STORE_MAP
);
3225 n
= asdl_seq_LEN(e
->v
.Set
.elts
);
3226 VISIT_SEQ(c
, expr
, e
->v
.Set
.elts
);
3227 ADDOP_I(c
, BUILD_SET
, n
);
3229 case GeneratorExp_kind
:
3230 return compiler_genexp(c
, e
);
3232 return compiler_listcomp(c
, e
);
3234 return compiler_setcomp(c
, e
);
3236 return compiler_dictcomp(c
, e
);
3238 if (c
->u
->u_ste
->ste_type
!= FunctionBlock
)
3239 return compiler_error(c
, "'yield' outside function");
3240 if (e
->v
.Yield
.value
) {
3241 VISIT(c
, expr
, e
->v
.Yield
.value
);
3244 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3246 ADDOP(c
, YIELD_VALUE
);
3249 return compiler_compare(c
, e
);
3251 return compiler_call(c
, e
);
3253 ADDOP_O(c
, LOAD_CONST
, e
->v
.Num
.n
, consts
);
3256 ADDOP_O(c
, LOAD_CONST
, e
->v
.Str
.s
, consts
);
3259 ADDOP_O(c
, LOAD_CONST
, e
->v
.Bytes
.s
, consts
);
3262 ADDOP_O(c
, LOAD_CONST
, Py_Ellipsis
, consts
);
3264 /* The following exprs can be assignment targets. */
3265 case Attribute_kind
:
3266 if (e
->v
.Attribute
.ctx
!= AugStore
)
3267 VISIT(c
, expr
, e
->v
.Attribute
.value
);
3268 switch (e
->v
.Attribute
.ctx
) {
3271 /* Fall through to load */
3273 ADDOP_NAME(c
, LOAD_ATTR
, e
->v
.Attribute
.attr
, names
);
3277 /* Fall through to save */
3279 ADDOP_NAME(c
, STORE_ATTR
, e
->v
.Attribute
.attr
, names
);
3282 ADDOP_NAME(c
, DELETE_ATTR
, e
->v
.Attribute
.attr
, names
);
3286 PyErr_SetString(PyExc_SystemError
,
3287 "param invalid in attribute expression");
3291 case Subscript_kind
:
3292 switch (e
->v
.Subscript
.ctx
) {
3294 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3295 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugLoad
);
3298 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3299 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Load
);
3302 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, AugStore
);
3305 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3306 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Store
);
3309 VISIT(c
, expr
, e
->v
.Subscript
.value
);
3310 VISIT_SLICE(c
, e
->v
.Subscript
.slice
, Del
);
3314 PyErr_SetString(PyExc_SystemError
,
3315 "param invalid in subscript expression");
3320 switch (e
->v
.Starred
.ctx
) {
3322 /* In all legitimate cases, the Starred node was already replaced
3323 * by compiler_list/compiler_tuple. XXX: is that okay? */
3324 return compiler_error(c
,
3325 "starred assignment target must be in a list or tuple");
3327 return compiler_error(c
,
3328 "can use starred expression only as assignment target");
3332 return compiler_nameop(c
, e
->v
.Name
.id
, e
->v
.Name
.ctx
);
3333 /* child nodes of List and Tuple will have expr_context set */
3335 return compiler_list(c
, e
);
3337 return compiler_tuple(c
, e
);
3343 compiler_augassign(struct compiler
*c
, stmt_ty s
)
3345 expr_ty e
= s
->v
.AugAssign
.target
;
3348 assert(s
->kind
== AugAssign_kind
);
3351 case Attribute_kind
:
3352 auge
= Attribute(e
->v
.Attribute
.value
, e
->v
.Attribute
.attr
,
3353 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3356 VISIT(c
, expr
, auge
);
3357 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3358 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3359 auge
->v
.Attribute
.ctx
= AugStore
;
3360 VISIT(c
, expr
, auge
);
3362 case Subscript_kind
:
3363 auge
= Subscript(e
->v
.Subscript
.value
, e
->v
.Subscript
.slice
,
3364 AugLoad
, e
->lineno
, e
->col_offset
, c
->c_arena
);
3367 VISIT(c
, expr
, auge
);
3368 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3369 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3370 auge
->v
.Subscript
.ctx
= AugStore
;
3371 VISIT(c
, expr
, auge
);
3374 if (!compiler_nameop(c
, e
->v
.Name
.id
, Load
))
3376 VISIT(c
, expr
, s
->v
.AugAssign
.value
);
3377 ADDOP(c
, inplace_binop(c
, s
->v
.AugAssign
.op
));
3378 return compiler_nameop(c
, e
->v
.Name
.id
, Store
);
3380 PyErr_Format(PyExc_SystemError
,
3381 "invalid node type (%d) for augmented assignment",
3389 compiler_push_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3391 struct fblockinfo
*f
;
3392 if (c
->u
->u_nfblocks
>= CO_MAXBLOCKS
) {
3393 PyErr_SetString(PyExc_SystemError
,
3394 "too many statically nested blocks");
3397 f
= &c
->u
->u_fblock
[c
->u
->u_nfblocks
++];
3404 compiler_pop_fblock(struct compiler
*c
, enum fblocktype t
, basicblock
*b
)
3406 struct compiler_unit
*u
= c
->u
;
3407 assert(u
->u_nfblocks
> 0);
3409 assert(u
->u_fblock
[u
->u_nfblocks
].fb_type
== t
);
3410 assert(u
->u_fblock
[u
->u_nfblocks
].fb_block
== b
);
3414 compiler_in_loop(struct compiler
*c
) {
3416 struct compiler_unit
*u
= c
->u
;
3417 for (i
= 0; i
< u
->u_nfblocks
; ++i
) {
3418 if (u
->u_fblock
[i
].fb_type
== LOOP
)
3423 /* Raises a SyntaxError and returns 0.
3424 If something goes wrong, a different exception may be raised.
3428 compiler_error(struct compiler
*c
, const char *errstr
)
3431 PyObject
*u
= NULL
, *v
= NULL
;
3433 loc
= PyErr_ProgramText(c
->c_filename
, c
->u
->u_lineno
);
3438 u
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->u
->u_lineno
,
3442 v
= Py_BuildValue("(zO)", errstr
, u
);
3445 PyErr_SetObject(PyExc_SyntaxError
, v
);
3454 compiler_handle_subscr(struct compiler
*c
, const char *kind
,
3455 expr_context_ty ctx
)
3459 /* XXX this code is duplicated */
3461 case AugLoad
: /* fall through to Load */
3462 case Load
: op
= BINARY_SUBSCR
; break;
3463 case AugStore
:/* fall through to Store */
3464 case Store
: op
= STORE_SUBSCR
; break;
3465 case Del
: op
= DELETE_SUBSCR
; break;
3467 PyErr_Format(PyExc_SystemError
,
3468 "invalid %s kind %d in subscript\n",
3472 if (ctx
== AugLoad
) {
3473 ADDOP_I(c
, DUP_TOPX
, 2);
3475 else if (ctx
== AugStore
) {
3476 ADDOP(c
, ROT_THREE
);
3483 compiler_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3486 assert(s
->kind
== Slice_kind
);
3488 /* only handles the cases where BUILD_SLICE is emitted */
3489 if (s
->v
.Slice
.lower
) {
3490 VISIT(c
, expr
, s
->v
.Slice
.lower
);
3493 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3496 if (s
->v
.Slice
.upper
) {
3497 VISIT(c
, expr
, s
->v
.Slice
.upper
);
3500 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
3503 if (s
->v
.Slice
.step
) {
3505 VISIT(c
, expr
, s
->v
.Slice
.step
);
3507 ADDOP_I(c
, BUILD_SLICE
, n
);
3512 compiler_visit_nested_slice(struct compiler
*c
, slice_ty s
,
3513 expr_context_ty ctx
)
3517 return compiler_slice(c
, s
, ctx
);
3519 VISIT(c
, expr
, s
->v
.Index
.value
);
3523 PyErr_SetString(PyExc_SystemError
,
3524 "extended slice invalid in nested slice");
3531 compiler_visit_slice(struct compiler
*c
, slice_ty s
, expr_context_ty ctx
)
3533 char * kindname
= NULL
;
3537 if (ctx
!= AugStore
) {
3538 VISIT(c
, expr
, s
->v
.Index
.value
);
3543 if (ctx
!= AugStore
) {
3544 if (!compiler_slice(c
, s
, ctx
))
3549 kindname
= "extended slice";
3550 if (ctx
!= AugStore
) {
3551 int i
, n
= asdl_seq_LEN(s
->v
.ExtSlice
.dims
);
3552 for (i
= 0; i
< n
; i
++) {
3553 slice_ty sub
= (slice_ty
)asdl_seq_GET(
3554 s
->v
.ExtSlice
.dims
, i
);
3555 if (!compiler_visit_nested_slice(c
, sub
, ctx
))
3558 ADDOP_I(c
, BUILD_TUPLE
, n
);
3562 PyErr_Format(PyExc_SystemError
,
3563 "invalid subscript kind %d", s
->kind
);
3566 return compiler_handle_subscr(c
, kindname
, ctx
);
3569 /* End of the compiler section, beginning of the assembler section */
3571 /* do depth-first search of basic block graph, starting with block.
3572 post records the block indices in post-order.
3574 XXX must handle implicit jumps from one block to next
3578 PyObject
*a_bytecode
; /* string containing bytecode */
3579 int a_offset
; /* offset into bytecode */
3580 int a_nblocks
; /* number of reachable blocks */
3581 basicblock
**a_postorder
; /* list of blocks in dfs postorder */
3582 PyObject
*a_lnotab
; /* string containing lnotab */
3583 int a_lnotab_off
; /* offset into lnotab */
3584 int a_lineno
; /* last lineno of emitted instruction */
3585 int a_lineno_off
; /* bytecode offset of last lineno */
3589 dfs(struct compiler
*c
, basicblock
*b
, struct assembler
*a
)
3592 struct instr
*instr
= NULL
;
3597 if (b
->b_next
!= NULL
)
3598 dfs(c
, b
->b_next
, a
);
3599 for (i
= 0; i
< b
->b_iused
; i
++) {
3600 instr
= &b
->b_instr
[i
];
3601 if (instr
->i_jrel
|| instr
->i_jabs
)
3602 dfs(c
, instr
->i_target
, a
);
3604 a
->a_postorder
[a
->a_nblocks
++] = b
;
3608 stackdepth_walk(struct compiler
*c
, basicblock
*b
, int depth
, int maxdepth
)
3611 struct instr
*instr
;
3612 if (b
->b_seen
|| b
->b_startdepth
>= depth
)
3615 b
->b_startdepth
= depth
;
3616 for (i
= 0; i
< b
->b_iused
; i
++) {
3617 instr
= &b
->b_instr
[i
];
3618 depth
+= opcode_stack_effect(instr
->i_opcode
, instr
->i_oparg
);
3619 if (depth
> maxdepth
)
3621 assert(depth
>= 0); /* invalid code or bug in stackdepth() */
3622 if (instr
->i_jrel
|| instr
->i_jabs
) {
3623 maxdepth
= stackdepth_walk(c
, instr
->i_target
,
3625 if (instr
->i_opcode
== JUMP_ABSOLUTE
||
3626 instr
->i_opcode
== JUMP_FORWARD
) {
3627 goto out
; /* remaining code is dead */
3632 maxdepth
= stackdepth_walk(c
, b
->b_next
, depth
, maxdepth
);
3638 /* Find the flow path that needs the largest stack. We assume that
3639 * cycles in the flow graph have no net effect on the stack depth.
3642 stackdepth(struct compiler
*c
)
3644 basicblock
*b
, *entryblock
;
3646 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3648 b
->b_startdepth
= INT_MIN
;
3653 return stackdepth_walk(c
, entryblock
, 0, 0);
3657 assemble_init(struct assembler
*a
, int nblocks
, int firstlineno
)
3659 memset(a
, 0, sizeof(struct assembler
));
3660 a
->a_lineno
= firstlineno
;
3661 a
->a_bytecode
= PyBytes_FromStringAndSize(NULL
, DEFAULT_CODE_SIZE
);
3664 a
->a_lnotab
= PyBytes_FromStringAndSize(NULL
, DEFAULT_LNOTAB_SIZE
);
3667 if (nblocks
> PY_SIZE_MAX
/ sizeof(basicblock
*)) {
3671 a
->a_postorder
= (basicblock
**)PyObject_Malloc(
3672 sizeof(basicblock
*) * nblocks
);
3673 if (!a
->a_postorder
) {
3681 assemble_free(struct assembler
*a
)
3683 Py_XDECREF(a
->a_bytecode
);
3684 Py_XDECREF(a
->a_lnotab
);
3686 PyObject_Free(a
->a_postorder
);
3689 /* Return the size of a basic block in bytes. */
3692 instrsize(struct instr
*instr
)
3694 if (!instr
->i_hasarg
)
3695 return 1; /* 1 byte for the opcode*/
3696 if (instr
->i_oparg
> 0xffff)
3697 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3698 return 3; /* 1 (opcode) + 2 (oparg) */
3702 blocksize(basicblock
*b
)
3707 for (i
= 0; i
< b
->b_iused
; i
++)
3708 size
+= instrsize(&b
->b_instr
[i
]);
3712 /* All about a_lnotab.
3714 c_lnotab is an array of unsigned bytes disguised as a Python string.
3715 It is used to map bytecode offsets to source code line #s (when needed
3718 The array is conceptually a list of
3719 (bytecode offset increment, line number increment)
3720 pairs. The details are important and delicate, best illustrated by example:
3722 byte code offset source code line number
3729 The first trick is that these numbers aren't stored, only the increments
3730 from one row to the next (this doesn't really work, but it's a start):
3732 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3734 The second trick is that an unsigned byte can't hold negative values, or
3735 values larger than 255, so (a) there's a deep assumption that byte code
3736 offsets and their corresponding line #s both increase monotonically, and (b)
3737 if at least one column jumps by more than 255 from one row to the next, more
3738 than one pair is written to the table. In case #b, there's no way to know
3739 from looking at the table later how many were written. That's the delicate
3740 part. A user of c_lnotab desiring to find the source line number
3741 corresponding to a bytecode address A should do something like this
3744 for addr_incr, line_incr in c_lnotab:
3750 In order for this to work, when the addr field increments by more than 255,
3751 the line # increment in each pair generated must be 0 until the remaining addr
3752 increment is < 256. So, in the example above, assemble_lnotab (it used
3753 to be called com_set_lineno) should not (as was actually done until 2.2)
3754 expand 300, 300 to 255, 255, 45, 45,
3755 but to 255, 0, 45, 255, 0, 45.
3759 assemble_lnotab(struct assembler
*a
, struct instr
*i
)
3761 int d_bytecode
, d_lineno
;
3763 unsigned char *lnotab
;
3765 d_bytecode
= a
->a_offset
- a
->a_lineno_off
;
3766 d_lineno
= i
->i_lineno
- a
->a_lineno
;
3768 assert(d_bytecode
>= 0);
3769 assert(d_lineno
>= 0);
3771 if(d_bytecode
== 0 && d_lineno
== 0)
3774 if (d_bytecode
> 255) {
3775 int j
, nbytes
, ncodes
= d_bytecode
/ 255;
3776 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3777 len
= PyBytes_GET_SIZE(a
->a_lnotab
);
3778 if (nbytes
>= len
) {
3779 if ((len
<= INT_MAX
/ 2) && (len
* 2 < nbytes
))
3781 else if (len
<= INT_MAX
/ 2)
3787 if (_PyBytes_Resize(&a
->a_lnotab
, len
) < 0)
3790 lnotab
= (unsigned char *)
3791 PyBytes_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3792 for (j
= 0; j
< ncodes
; j
++) {
3796 d_bytecode
-= ncodes
* 255;
3797 a
->a_lnotab_off
+= ncodes
* 2;
3799 assert(d_bytecode
<= 255);
3800 if (d_lineno
> 255) {
3801 int j
, nbytes
, ncodes
= d_lineno
/ 255;
3802 nbytes
= a
->a_lnotab_off
+ 2 * ncodes
;
3803 len
= PyBytes_GET_SIZE(a
->a_lnotab
);
3804 if (nbytes
>= len
) {
3805 if ((len
<= INT_MAX
/ 2) && len
* 2 < nbytes
)
3807 else if (len
<= INT_MAX
/ 2)
3813 if (_PyBytes_Resize(&a
->a_lnotab
, len
) < 0)
3816 lnotab
= (unsigned char *)
3817 PyBytes_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3818 *lnotab
++ = d_bytecode
;
3821 for (j
= 1; j
< ncodes
; j
++) {
3825 d_lineno
-= ncodes
* 255;
3826 a
->a_lnotab_off
+= ncodes
* 2;
3829 len
= PyBytes_GET_SIZE(a
->a_lnotab
);
3830 if (a
->a_lnotab_off
+ 2 >= len
) {
3831 if (_PyBytes_Resize(&a
->a_lnotab
, len
* 2) < 0)
3834 lnotab
= (unsigned char *)
3835 PyBytes_AS_STRING(a
->a_lnotab
) + a
->a_lnotab_off
;
3837 a
->a_lnotab_off
+= 2;
3839 *lnotab
++ = d_bytecode
;
3840 *lnotab
++ = d_lineno
;
3842 else { /* First line of a block; def stmt, etc. */
3844 *lnotab
++ = d_lineno
;
3846 a
->a_lineno
= i
->i_lineno
;
3847 a
->a_lineno_off
= a
->a_offset
;
3852 Extend the bytecode with a new instruction.
3853 Update lnotab if necessary.
3857 assemble_emit(struct assembler
*a
, struct instr
*i
)
3859 int size
, arg
= 0, ext
= 0;
3860 Py_ssize_t len
= PyBytes_GET_SIZE(a
->a_bytecode
);
3863 size
= instrsize(i
);
3868 if (i
->i_lineno
&& !assemble_lnotab(a
, i
))
3870 if (a
->a_offset
+ size
>= len
) {
3871 if (len
> PY_SSIZE_T_MAX
/ 2)
3873 if (_PyBytes_Resize(&a
->a_bytecode
, len
* 2) < 0)
3876 code
= PyBytes_AS_STRING(a
->a_bytecode
) + a
->a_offset
;
3877 a
->a_offset
+= size
;
3879 assert(i
->i_hasarg
);
3880 *code
++ = (char)EXTENDED_ARG
;
3881 *code
++ = ext
& 0xff;
3885 *code
++ = i
->i_opcode
;
3887 assert(size
== 3 || size
== 6);
3888 *code
++ = arg
& 0xff;
3895 assemble_jump_offsets(struct assembler
*a
, struct compiler
*c
)
3898 int bsize
, totsize
, extended_arg_count
, last_extended_arg_count
= 0;
3901 /* Compute the size of each block and fixup jump args.
3902 Replace block pointer with position in bytecode. */
3905 for (i
= a
->a_nblocks
- 1; i
>= 0; i
--) {
3906 b
= a
->a_postorder
[i
];
3907 bsize
= blocksize(b
);
3908 b
->b_offset
= totsize
;
3911 extended_arg_count
= 0;
3912 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
3913 bsize
= b
->b_offset
;
3914 for (i
= 0; i
< b
->b_iused
; i
++) {
3915 struct instr
*instr
= &b
->b_instr
[i
];
3916 /* Relative jumps are computed relative to
3917 the instruction pointer after fetching
3918 the jump instruction.
3920 bsize
+= instrsize(instr
);
3922 instr
->i_oparg
= instr
->i_target
->b_offset
;
3923 else if (instr
->i_jrel
) {
3924 int delta
= instr
->i_target
->b_offset
- bsize
;
3925 instr
->i_oparg
= delta
;
3929 if (instr
->i_oparg
> 0xffff)
3930 extended_arg_count
++;
3934 /* XXX: This is an awful hack that could hurt performance, but
3935 on the bright side it should work until we come up
3936 with a better solution.
3938 In the meantime, should the goto be dropped in favor
3941 The issue is that in the first loop blocksize() is called
3942 which calls instrsize() which requires i_oparg be set
3943 appropriately. There is a bootstrap problem because
3944 i_oparg is calculated in the second loop above.
3946 So we loop until we stop seeing new EXTENDED_ARGs.
3947 The only EXTENDED_ARGs that could be popping up are
3948 ones in jump instructions. So this should converge
3951 if (last_extended_arg_count
!= extended_arg_count
) {
3952 last_extended_arg_count
= extended_arg_count
;
3958 dict_keys_inorder(PyObject
*dict
, int offset
)
3960 PyObject
*tuple
, *k
, *v
;
3961 Py_ssize_t i
, pos
= 0, size
= PyDict_Size(dict
);
3963 tuple
= PyTuple_New(size
);
3966 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3967 i
= PyLong_AS_LONG(v
);
3968 /* The keys of the dictionary are tuples. (see compiler_add_o)
3969 The object we want is always first, though. */
3970 k
= PyTuple_GET_ITEM(k
, 0);
3972 assert((i
- offset
) < size
);
3973 assert((i
- offset
) >= 0);
3974 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3980 compute_code_flags(struct compiler
*c
)
3982 PySTEntryObject
*ste
= c
->u
->u_ste
;
3984 if (ste
->ste_type
!= ModuleBlock
)
3985 flags
|= CO_NEWLOCALS
;
3986 if (ste
->ste_type
== FunctionBlock
) {
3987 if (!ste
->ste_unoptimized
)
3988 flags
|= CO_OPTIMIZED
;
3989 if (ste
->ste_nested
)
3991 if (ste
->ste_generator
)
3992 flags
|= CO_GENERATOR
;
3993 if (ste
->ste_varargs
)
3994 flags
|= CO_VARARGS
;
3995 if (ste
->ste_varkeywords
)
3996 flags
|= CO_VARKEYWORDS
;
3999 /* (Only) inherit compilerflags in PyCF_MASK */
4000 flags
|= (c
->c_flags
->cf_flags
& PyCF_MASK
);
4002 n
= PyDict_Size(c
->u
->u_freevars
);
4006 n
= PyDict_Size(c
->u
->u_cellvars
);
4017 static PyCodeObject
*
4018 makecode(struct compiler
*c
, struct assembler
*a
)
4021 PyCodeObject
*co
= NULL
;
4022 PyObject
*consts
= NULL
;
4023 PyObject
*names
= NULL
;
4024 PyObject
*varnames
= NULL
;
4025 PyObject
*filename
= NULL
;
4026 PyObject
*name
= NULL
;
4027 PyObject
*freevars
= NULL
;
4028 PyObject
*cellvars
= NULL
;
4029 PyObject
*bytecode
= NULL
;
4032 tmp
= dict_keys_inorder(c
->u
->u_consts
, 0);
4035 consts
= PySequence_List(tmp
); /* optimize_code requires a list */
4038 names
= dict_keys_inorder(c
->u
->u_names
, 0);
4039 varnames
= dict_keys_inorder(c
->u
->u_varnames
, 0);
4040 if (!consts
|| !names
|| !varnames
)
4043 cellvars
= dict_keys_inorder(c
->u
->u_cellvars
, 0);
4046 freevars
= dict_keys_inorder(c
->u
->u_freevars
, PyTuple_Size(cellvars
));
4049 filename
= PyUnicode_DecodeFSDefault(c
->c_filename
);
4053 nlocals
= PyDict_Size(c
->u
->u_varnames
);
4054 flags
= compute_code_flags(c
);
4058 bytecode
= PyCode_Optimize(a
->a_bytecode
, consts
, names
, a
->a_lnotab
);
4062 tmp
= PyList_AsTuple(consts
); /* PyCode_New requires a tuple */
4068 co
= PyCode_New(c
->u
->u_argcount
, c
->u
->u_kwonlyargcount
,
4069 nlocals
, stackdepth(c
), flags
,
4070 bytecode
, consts
, names
, varnames
,
4072 filename
, c
->u
->u_name
,
4073 c
->u
->u_firstlineno
,
4078 Py_XDECREF(varnames
);
4079 Py_XDECREF(filename
);
4081 Py_XDECREF(freevars
);
4082 Py_XDECREF(cellvars
);
4083 Py_XDECREF(bytecode
);
4088 /* For debugging purposes only */
4091 dump_instr(const struct instr
*i
)
4093 const char *jrel
= i
->i_jrel
? "jrel " : "";
4094 const char *jabs
= i
->i_jabs
? "jabs " : "";
4099 sprintf(arg
, "arg: %d ", i
->i_oparg
);
4101 fprintf(stderr
, "line: %d, opcode: %d %s%s%s\n",
4102 i
->i_lineno
, i
->i_opcode
, arg
, jabs
, jrel
);
4106 dump_basicblock(const basicblock
*b
)
4108 const char *seen
= b
->b_seen
? "seen " : "";
4109 const char *b_return
= b
->b_return
? "return " : "";
4110 fprintf(stderr
, "used: %d, depth: %d, offset: %d %s%s\n",
4111 b
->b_iused
, b
->b_startdepth
, b
->b_offset
, seen
, b_return
);
4114 for (i
= 0; i
< b
->b_iused
; i
++) {
4115 fprintf(stderr
, " [%02d] ", i
);
4116 dump_instr(b
->b_instr
+ i
);
4122 static PyCodeObject
*
4123 assemble(struct compiler
*c
, int addNone
)
4125 basicblock
*b
, *entryblock
;
4128 PyCodeObject
*co
= NULL
;
4130 /* Make sure every block that falls off the end returns None.
4131 XXX NEXT_BLOCK() isn't quite right, because if the last
4132 block ends with a jump or return b_next shouldn't set.
4134 if (!c
->u
->u_curblock
->b_return
) {
4137 ADDOP_O(c
, LOAD_CONST
, Py_None
, consts
);
4138 ADDOP(c
, RETURN_VALUE
);
4143 for (b
= c
->u
->u_blocks
; b
!= NULL
; b
= b
->b_list
) {
4148 /* Set firstlineno if it wasn't explicitly set. */
4149 if (!c
->u
->u_firstlineno
) {
4150 if (entryblock
&& entryblock
->b_instr
)
4151 c
->u
->u_firstlineno
= entryblock
->b_instr
->i_lineno
;
4153 c
->u
->u_firstlineno
= 1;
4155 if (!assemble_init(&a
, nblocks
, c
->u
->u_firstlineno
))
4157 dfs(c
, entryblock
, &a
);
4159 /* Can't modify the bytecode after computing jump offsets. */
4160 assemble_jump_offsets(&a
, c
);
4162 /* Emit code in reverse postorder from dfs. */
4163 for (i
= a
.a_nblocks
- 1; i
>= 0; i
--) {
4164 b
= a
.a_postorder
[i
];
4165 for (j
= 0; j
< b
->b_iused
; j
++)
4166 if (!assemble_emit(&a
, &b
->b_instr
[j
]))
4170 if (_PyBytes_Resize(&a
.a_lnotab
, a
.a_lnotab_off
) < 0)
4172 if (_PyBytes_Resize(&a
.a_bytecode
, a
.a_offset
) < 0)
4175 co
= makecode(c
, &a
);