Adding basic imputil documentation.
[python.git] / Python / compile.c
blobeed13792d288fa5e0173f08790ca12c8623af112
1 /*
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
6 * object:
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
11 * this file.
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
21 * objects.
24 #include "Python.h"
26 #include "Python-ast.h"
27 #include "node.h"
28 #include "pyarena.h"
29 #include "ast.h"
30 #include "code.h"
31 #include "compile.h"
32 #include "symtable.h"
33 #include "opcode.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
42 struct instr {
43 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
46 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
52 typedef struct basicblock_ {
53 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
56 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
67 unsigned b_seen : 1;
68 /* b_return is true if a RETURN_VALUE opcode is inserted. */
69 unsigned b_return : 1;
70 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
73 int b_offset;
74 } basicblock;
76 /* fblockinfo tracks the current frame block.
78 A frame block is used to handle loops, try/except, and try/finally.
79 It's called a frame block to distinguish it from a basic block in the
80 compiler IR.
83 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
85 struct fblockinfo {
86 enum fblocktype fb_type;
87 basicblock *fb_block;
90 /* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
93 struct compiler_unit {
94 PySTEntryObject *u_ste;
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
98 the index of them in co_XXX. The index is used as
99 the argument for opcodes that refer to those collections.
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
107 PyObject *u_private; /* for private name mangling */
109 int u_argcount; /* number of arguments for block */
110 /* Pointer to the most recently allocated block. By following b_list
111 members, you can reach all early allocated blocks. */
112 basicblock *u_blocks;
113 basicblock *u_curblock; /* pointer to current block */
114 int u_tmpname; /* temporary variables for list comps */
116 int u_nfblocks;
117 struct fblockinfo u_fblock[CO_MAXBLOCKS];
119 int u_firstlineno; /* the first lineno of the block */
120 int u_lineno; /* the lineno for the current stmt */
121 bool u_lineno_set; /* boolean to indicate whether instr
122 has been generated with current lineno */
125 /* This struct captures the global state of a compilation.
127 The u pointer points to the current compilation unit, while units
128 for enclosing blocks are stored in c_stack. The u and c_stack are
129 managed by compiler_enter_scope() and compiler_exit_scope().
132 struct compiler {
133 const char *c_filename;
134 struct symtable *c_st;
135 PyFutureFeatures *c_future; /* pointer to module's __future__ */
136 PyCompilerFlags *c_flags;
138 int c_interactive; /* true if in interactive mode */
139 int c_nestlevel;
141 struct compiler_unit *u; /* compiler state for current block */
142 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
143 char *c_encoding; /* source encoding (a borrowed reference) */
144 PyArena *c_arena; /* pointer to memory allocation arena */
147 static int compiler_enter_scope(struct compiler *, identifier, void *, int);
148 static void compiler_free(struct compiler *);
149 static basicblock *compiler_new_block(struct compiler *);
150 static int compiler_next_instr(struct compiler *, basicblock *);
151 static int compiler_addop(struct compiler *, int);
152 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
153 static int compiler_addop_i(struct compiler *, int, int);
154 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
155 static basicblock *compiler_use_new_block(struct compiler *);
156 static int compiler_error(struct compiler *, const char *);
157 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
159 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
160 static int compiler_visit_stmt(struct compiler *, stmt_ty);
161 static int compiler_visit_keyword(struct compiler *, keyword_ty);
162 static int compiler_visit_expr(struct compiler *, expr_ty);
163 static int compiler_augassign(struct compiler *, stmt_ty);
164 static int compiler_visit_slice(struct compiler *, slice_ty,
165 expr_context_ty);
167 static int compiler_push_fblock(struct compiler *, enum fblocktype,
168 basicblock *);
169 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
171 /* Returns true if there is a loop on the fblock stack. */
172 static int compiler_in_loop(struct compiler *);
174 static int inplace_binop(struct compiler *, operator_ty);
175 static int expr_constant(expr_ty e);
177 static int compiler_with(struct compiler *, stmt_ty);
179 static PyCodeObject *assemble(struct compiler *, int addNone);
180 static PyObject *__doc__;
182 PyObject *
183 _Py_Mangle(PyObject *privateobj, PyObject *ident)
185 /* Name mangling: __private becomes _classname__private.
186 This is independent from how the name is used. */
187 const char *p, *name = PyString_AsString(ident);
188 char *buffer;
189 size_t nlen, plen;
190 if (privateobj == NULL || !PyString_Check(privateobj) ||
191 name == NULL || name[0] != '_' || name[1] != '_') {
192 Py_INCREF(ident);
193 return ident;
195 p = PyString_AsString(privateobj);
196 nlen = strlen(name);
197 /* Don't mangle __id__ or names with dots.
199 The only time a name with a dot can occur is when
200 we are compiling an import statement that has a
201 package name.
203 TODO(jhylton): Decide whether we want to support
204 mangling of the module name, e.g. __M.X.
206 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
207 || strchr(name, '.')) {
208 Py_INCREF(ident);
209 return ident; /* Don't mangle __whatever__ */
211 /* Strip leading underscores from class name */
212 while (*p == '_')
213 p++;
214 if (*p == '\0') {
215 Py_INCREF(ident);
216 return ident; /* Don't mangle if class is just underscores */
218 plen = strlen(p);
219 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
220 if (!ident)
221 return 0;
222 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
223 buffer = PyString_AS_STRING(ident);
224 buffer[0] = '_';
225 strncpy(buffer+1, p, plen);
226 strcpy(buffer+1+plen, name);
227 return ident;
230 static int
231 compiler_init(struct compiler *c)
233 memset(c, 0, sizeof(struct compiler));
235 c->c_stack = PyList_New(0);
236 if (!c->c_stack)
237 return 0;
239 return 1;
242 PyCodeObject *
243 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
244 PyArena *arena)
246 struct compiler c;
247 PyCodeObject *co = NULL;
248 PyCompilerFlags local_flags;
249 int merged;
251 if (!__doc__) {
252 __doc__ = PyString_InternFromString("__doc__");
253 if (!__doc__)
254 return NULL;
257 if (!compiler_init(&c))
258 return NULL;
259 c.c_filename = filename;
260 c.c_arena = arena;
261 c.c_future = PyFuture_FromAST(mod, filename);
262 if (c.c_future == NULL)
263 goto finally;
264 if (!flags) {
265 local_flags.cf_flags = 0;
266 flags = &local_flags;
268 merged = c.c_future->ff_features | flags->cf_flags;
269 c.c_future->ff_features = merged;
270 flags->cf_flags = merged;
271 c.c_flags = flags;
272 c.c_nestlevel = 0;
274 c.c_st = PySymtable_Build(mod, filename, c.c_future);
275 if (c.c_st == NULL) {
276 if (!PyErr_Occurred())
277 PyErr_SetString(PyExc_SystemError, "no symtable");
278 goto finally;
281 /* XXX initialize to NULL for now, need to handle */
282 c.c_encoding = NULL;
284 co = compiler_mod(&c, mod);
286 finally:
287 compiler_free(&c);
288 assert(co || PyErr_Occurred());
289 return co;
292 PyCodeObject *
293 PyNode_Compile(struct _node *n, const char *filename)
295 PyCodeObject *co = NULL;
296 mod_ty mod;
297 PyArena *arena = PyArena_New();
298 if (!arena)
299 return NULL;
300 mod = PyAST_FromNode(n, NULL, filename, arena);
301 if (mod)
302 co = PyAST_Compile(mod, filename, NULL, arena);
303 PyArena_Free(arena);
304 return co;
307 static void
308 compiler_free(struct compiler *c)
310 if (c->c_st)
311 PySymtable_Free(c->c_st);
312 if (c->c_future)
313 PyObject_Free(c->c_future);
314 Py_DECREF(c->c_stack);
317 static PyObject *
318 list2dict(PyObject *list)
320 Py_ssize_t i, n;
321 PyObject *v, *k;
322 PyObject *dict = PyDict_New();
323 if (!dict) return NULL;
325 n = PyList_Size(list);
326 for (i = 0; i < n; i++) {
327 v = PyInt_FromLong(i);
328 if (!v) {
329 Py_DECREF(dict);
330 return NULL;
332 k = PyList_GET_ITEM(list, i);
333 k = PyTuple_Pack(2, k, k->ob_type);
334 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
335 Py_XDECREF(k);
336 Py_DECREF(v);
337 Py_DECREF(dict);
338 return NULL;
340 Py_DECREF(k);
341 Py_DECREF(v);
343 return dict;
346 /* Return new dict containing names from src that match scope(s).
348 src is a symbol table dictionary. If the scope of a name matches
349 either scope_type or flag is set, insert it into the new dict. The
350 values are integers, starting at offset and increasing by one for
351 each key.
354 static PyObject *
355 dictbytype(PyObject *src, int scope_type, int flag, int offset)
357 Py_ssize_t pos = 0, i = offset, scope;
358 PyObject *k, *v, *dest = PyDict_New();
360 assert(offset >= 0);
361 if (dest == NULL)
362 return NULL;
364 while (PyDict_Next(src, &pos, &k, &v)) {
365 /* XXX this should probably be a macro in symtable.h */
366 assert(PyInt_Check(v));
367 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
369 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
370 PyObject *tuple, *item = PyInt_FromLong(i);
371 if (item == NULL) {
372 Py_DECREF(dest);
373 return NULL;
375 i++;
376 tuple = PyTuple_Pack(2, k, k->ob_type);
377 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
378 Py_DECREF(item);
379 Py_DECREF(dest);
380 Py_XDECREF(tuple);
381 return NULL;
383 Py_DECREF(item);
384 Py_DECREF(tuple);
387 return dest;
390 static void
391 compiler_unit_check(struct compiler_unit *u)
393 basicblock *block;
394 for (block = u->u_blocks; block != NULL; block = block->b_list) {
395 assert(block != (void *)0xcbcbcbcb);
396 assert(block != (void *)0xfbfbfbfb);
397 assert(block != (void *)0xdbdbdbdb);
398 if (block->b_instr != NULL) {
399 assert(block->b_ialloc > 0);
400 assert(block->b_iused > 0);
401 assert(block->b_ialloc >= block->b_iused);
403 else {
404 assert (block->b_iused == 0);
405 assert (block->b_ialloc == 0);
410 static void
411 compiler_unit_free(struct compiler_unit *u)
413 basicblock *b, *next;
415 compiler_unit_check(u);
416 b = u->u_blocks;
417 while (b != NULL) {
418 if (b->b_instr)
419 PyObject_Free((void *)b->b_instr);
420 next = b->b_list;
421 PyObject_Free((void *)b);
422 b = next;
424 Py_CLEAR(u->u_ste);
425 Py_CLEAR(u->u_name);
426 Py_CLEAR(u->u_consts);
427 Py_CLEAR(u->u_names);
428 Py_CLEAR(u->u_varnames);
429 Py_CLEAR(u->u_freevars);
430 Py_CLEAR(u->u_cellvars);
431 Py_CLEAR(u->u_private);
432 PyObject_Free(u);
435 static int
436 compiler_enter_scope(struct compiler *c, identifier name, void *key,
437 int lineno)
439 struct compiler_unit *u;
441 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
442 struct compiler_unit));
443 if (!u) {
444 PyErr_NoMemory();
445 return 0;
447 memset(u, 0, sizeof(struct compiler_unit));
448 u->u_argcount = 0;
449 u->u_ste = PySymtable_Lookup(c->c_st, key);
450 if (!u->u_ste) {
451 compiler_unit_free(u);
452 return 0;
454 Py_INCREF(name);
455 u->u_name = name;
456 u->u_varnames = list2dict(u->u_ste->ste_varnames);
457 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
458 if (!u->u_varnames || !u->u_cellvars) {
459 compiler_unit_free(u);
460 return 0;
463 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
464 PyDict_Size(u->u_cellvars));
465 if (!u->u_freevars) {
466 compiler_unit_free(u);
467 return 0;
470 u->u_blocks = NULL;
471 u->u_tmpname = 0;
472 u->u_nfblocks = 0;
473 u->u_firstlineno = lineno;
474 u->u_lineno = 0;
475 u->u_lineno_set = false;
476 u->u_consts = PyDict_New();
477 if (!u->u_consts) {
478 compiler_unit_free(u);
479 return 0;
481 u->u_names = PyDict_New();
482 if (!u->u_names) {
483 compiler_unit_free(u);
484 return 0;
487 u->u_private = NULL;
489 /* Push the old compiler_unit on the stack. */
490 if (c->u) {
491 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
492 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
493 Py_XDECREF(wrapper);
494 compiler_unit_free(u);
495 return 0;
497 Py_DECREF(wrapper);
498 u->u_private = c->u->u_private;
499 Py_XINCREF(u->u_private);
501 c->u = u;
503 c->c_nestlevel++;
504 if (compiler_use_new_block(c) == NULL)
505 return 0;
507 return 1;
510 static void
511 compiler_exit_scope(struct compiler *c)
513 int n;
514 PyObject *wrapper;
516 c->c_nestlevel--;
517 compiler_unit_free(c->u);
518 /* Restore c->u to the parent unit. */
519 n = PyList_GET_SIZE(c->c_stack) - 1;
520 if (n >= 0) {
521 wrapper = PyList_GET_ITEM(c->c_stack, n);
522 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
523 assert(c->u);
524 /* we are deleting from a list so this really shouldn't fail */
525 if (PySequence_DelItem(c->c_stack, n) < 0)
526 Py_FatalError("compiler_exit_scope()");
527 compiler_unit_check(c->u);
529 else
530 c->u = NULL;
534 /* Allocate a new "anonymous" local variable.
535 Used by list comprehensions and with statements.
538 static PyObject *
539 compiler_new_tmpname(struct compiler *c)
541 char tmpname[256];
542 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
543 return PyString_FromString(tmpname);
546 /* Allocate a new block and return a pointer to it.
547 Returns NULL on error.
550 static basicblock *
551 compiler_new_block(struct compiler *c)
553 basicblock *b;
554 struct compiler_unit *u;
556 u = c->u;
557 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
558 if (b == NULL) {
559 PyErr_NoMemory();
560 return NULL;
562 memset((void *)b, 0, sizeof(basicblock));
563 /* Extend the singly linked list of blocks with new block. */
564 b->b_list = u->u_blocks;
565 u->u_blocks = b;
566 return b;
569 static basicblock *
570 compiler_use_new_block(struct compiler *c)
572 basicblock *block = compiler_new_block(c);
573 if (block == NULL)
574 return NULL;
575 c->u->u_curblock = block;
576 return block;
579 static basicblock *
580 compiler_next_block(struct compiler *c)
582 basicblock *block = compiler_new_block(c);
583 if (block == NULL)
584 return NULL;
585 c->u->u_curblock->b_next = block;
586 c->u->u_curblock = block;
587 return block;
590 static basicblock *
591 compiler_use_next_block(struct compiler *c, basicblock *block)
593 assert(block != NULL);
594 c->u->u_curblock->b_next = block;
595 c->u->u_curblock = block;
596 return block;
599 /* Returns the offset of the next instruction in the current block's
600 b_instr array. Resizes the b_instr as necessary.
601 Returns -1 on failure.
604 static int
605 compiler_next_instr(struct compiler *c, basicblock *b)
607 assert(b != NULL);
608 if (b->b_instr == NULL) {
609 b->b_instr = (struct instr *)PyObject_Malloc(
610 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
611 if (b->b_instr == NULL) {
612 PyErr_NoMemory();
613 return -1;
615 b->b_ialloc = DEFAULT_BLOCK_SIZE;
616 memset((char *)b->b_instr, 0,
617 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
619 else if (b->b_iused == b->b_ialloc) {
620 struct instr *tmp;
621 size_t oldsize, newsize;
622 oldsize = b->b_ialloc * sizeof(struct instr);
623 newsize = oldsize << 1;
624 if (newsize == 0) {
625 PyErr_NoMemory();
626 return -1;
628 b->b_ialloc <<= 1;
629 tmp = (struct instr *)PyObject_Realloc(
630 (void *)b->b_instr, newsize);
631 if (tmp == NULL) {
632 PyErr_NoMemory();
633 return -1;
635 b->b_instr = tmp;
636 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
638 return b->b_iused++;
641 /* Set the i_lineno member of the instruction at offse off if the
642 line number for the current expression/statement (?) has not
643 already been set. If it has been set, the call has no effect.
645 Every time a new node is b
648 static void
649 compiler_set_lineno(struct compiler *c, int off)
651 basicblock *b;
652 if (c->u->u_lineno_set)
653 return;
654 c->u->u_lineno_set = true;
655 b = c->u->u_curblock;
656 b->b_instr[off].i_lineno = c->u->u_lineno;
659 static int
660 opcode_stack_effect(int opcode, int oparg)
662 switch (opcode) {
663 case POP_TOP:
664 return -1;
665 case ROT_TWO:
666 case ROT_THREE:
667 return 0;
668 case DUP_TOP:
669 return 1;
670 case ROT_FOUR:
671 return 0;
673 case UNARY_POSITIVE:
674 case UNARY_NEGATIVE:
675 case UNARY_NOT:
676 case UNARY_CONVERT:
677 case UNARY_INVERT:
678 return 0;
680 case LIST_APPEND:
681 return -2;
683 case BINARY_POWER:
684 case BINARY_MULTIPLY:
685 case BINARY_DIVIDE:
686 case BINARY_MODULO:
687 case BINARY_ADD:
688 case BINARY_SUBTRACT:
689 case BINARY_SUBSCR:
690 case BINARY_FLOOR_DIVIDE:
691 case BINARY_TRUE_DIVIDE:
692 return -1;
693 case INPLACE_FLOOR_DIVIDE:
694 case INPLACE_TRUE_DIVIDE:
695 return -1;
697 case SLICE+0:
698 return 1;
699 case SLICE+1:
700 return 0;
701 case SLICE+2:
702 return 0;
703 case SLICE+3:
704 return -1;
706 case STORE_SLICE+0:
707 return -2;
708 case STORE_SLICE+1:
709 return -3;
710 case STORE_SLICE+2:
711 return -3;
712 case STORE_SLICE+3:
713 return -4;
715 case DELETE_SLICE+0:
716 return -1;
717 case DELETE_SLICE+1:
718 return -2;
719 case DELETE_SLICE+2:
720 return -2;
721 case DELETE_SLICE+3:
722 return -3;
724 case INPLACE_ADD:
725 case INPLACE_SUBTRACT:
726 case INPLACE_MULTIPLY:
727 case INPLACE_DIVIDE:
728 case INPLACE_MODULO:
729 return -1;
730 case STORE_SUBSCR:
731 return -3;
732 case DELETE_SUBSCR:
733 return -2;
735 case BINARY_LSHIFT:
736 case BINARY_RSHIFT:
737 case BINARY_AND:
738 case BINARY_XOR:
739 case BINARY_OR:
740 return -1;
741 case INPLACE_POWER:
742 return -1;
743 case GET_ITER:
744 return 0;
746 case PRINT_EXPR:
747 return -1;
748 case PRINT_ITEM:
749 return -1;
750 case PRINT_NEWLINE:
751 return 0;
752 case PRINT_ITEM_TO:
753 return -2;
754 case PRINT_NEWLINE_TO:
755 return -1;
756 case INPLACE_LSHIFT:
757 case INPLACE_RSHIFT:
758 case INPLACE_AND:
759 case INPLACE_XOR:
760 case INPLACE_OR:
761 return -1;
762 case BREAK_LOOP:
763 return 0;
764 case WITH_CLEANUP:
765 return -1; /* XXX Sometimes more */
766 case LOAD_LOCALS:
767 return 1;
768 case RETURN_VALUE:
769 return -1;
770 case IMPORT_STAR:
771 return -1;
772 case EXEC_STMT:
773 return -3;
774 case YIELD_VALUE:
775 return 0;
777 case POP_BLOCK:
778 return 0;
779 case END_FINALLY:
780 return -1; /* or -2 or -3 if exception occurred */
781 case BUILD_CLASS:
782 return -2;
784 case STORE_NAME:
785 return -1;
786 case DELETE_NAME:
787 return 0;
788 case UNPACK_SEQUENCE:
789 return oparg-1;
790 case FOR_ITER:
791 return 1;
793 case STORE_ATTR:
794 return -2;
795 case DELETE_ATTR:
796 return -1;
797 case STORE_GLOBAL:
798 return -1;
799 case DELETE_GLOBAL:
800 return 0;
801 case DUP_TOPX:
802 return oparg;
803 case LOAD_CONST:
804 return 1;
805 case LOAD_NAME:
806 return 1;
807 case BUILD_TUPLE:
808 case BUILD_LIST:
809 return 1-oparg;
810 case BUILD_MAP:
811 return 1;
812 case LOAD_ATTR:
813 return 0;
814 case COMPARE_OP:
815 return -1;
816 case IMPORT_NAME:
817 return 0;
818 case IMPORT_FROM:
819 return 1;
821 case JUMP_FORWARD:
822 case JUMP_IF_FALSE:
823 case JUMP_IF_TRUE:
824 case JUMP_ABSOLUTE:
825 return 0;
827 case LOAD_GLOBAL:
828 return 1;
830 case CONTINUE_LOOP:
831 return 0;
832 case SETUP_LOOP:
833 return 0;
834 case SETUP_EXCEPT:
835 case SETUP_FINALLY:
836 return 3; /* actually pushed by an exception */
838 case LOAD_FAST:
839 return 1;
840 case STORE_FAST:
841 return -1;
842 case DELETE_FAST:
843 return 0;
845 case RAISE_VARARGS:
846 return -oparg;
847 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
848 case CALL_FUNCTION:
849 return -NARGS(oparg);
850 case CALL_FUNCTION_VAR:
851 case CALL_FUNCTION_KW:
852 return -NARGS(oparg)-1;
853 case CALL_FUNCTION_VAR_KW:
854 return -NARGS(oparg)-2;
855 #undef NARGS
856 case MAKE_FUNCTION:
857 return -oparg;
858 case BUILD_SLICE:
859 if (oparg == 3)
860 return -2;
861 else
862 return -1;
864 case MAKE_CLOSURE:
865 return -oparg;
866 case LOAD_CLOSURE:
867 return 1;
868 case LOAD_DEREF:
869 return 1;
870 case STORE_DEREF:
871 return -1;
872 default:
873 fprintf(stderr, "opcode = %d\n", opcode);
874 Py_FatalError("opcode_stack_effect()");
877 return 0; /* not reachable */
880 /* Add an opcode with no argument.
881 Returns 0 on failure, 1 on success.
884 static int
885 compiler_addop(struct compiler *c, int opcode)
887 basicblock *b;
888 struct instr *i;
889 int off;
890 off = compiler_next_instr(c, c->u->u_curblock);
891 if (off < 0)
892 return 0;
893 b = c->u->u_curblock;
894 i = &b->b_instr[off];
895 i->i_opcode = opcode;
896 i->i_hasarg = 0;
897 if (opcode == RETURN_VALUE)
898 b->b_return = 1;
899 compiler_set_lineno(c, off);
900 return 1;
903 static int
904 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
906 PyObject *t, *v;
907 Py_ssize_t arg;
909 /* necessary to make sure types aren't coerced (e.g., int and long) */
910 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
911 if (PyFloat_Check(o)) {
912 double d = PyFloat_AS_DOUBLE(o);
913 unsigned char* p = (unsigned char*) &d;
914 /* all we need is to make the tuple different in either the 0.0
915 * or -0.0 case from all others, just to avoid the "coercion".
917 if (*p==0 && p[sizeof(double)-1]==0)
918 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
919 else
920 t = PyTuple_Pack(2, o, o->ob_type);
921 } else {
922 t = PyTuple_Pack(2, o, o->ob_type);
924 if (t == NULL)
925 return -1;
927 v = PyDict_GetItem(dict, t);
928 if (!v) {
929 arg = PyDict_Size(dict);
930 v = PyInt_FromLong(arg);
931 if (!v) {
932 Py_DECREF(t);
933 return -1;
935 if (PyDict_SetItem(dict, t, v) < 0) {
936 Py_DECREF(t);
937 Py_DECREF(v);
938 return -1;
940 Py_DECREF(v);
942 else
943 arg = PyInt_AsLong(v);
944 Py_DECREF(t);
945 return arg;
948 static int
949 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
950 PyObject *o)
952 int arg = compiler_add_o(c, dict, o);
953 if (arg < 0)
954 return 0;
955 return compiler_addop_i(c, opcode, arg);
958 static int
959 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
960 PyObject *o)
962 int arg;
963 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
964 if (!mangled)
965 return 0;
966 arg = compiler_add_o(c, dict, mangled);
967 Py_DECREF(mangled);
968 if (arg < 0)
969 return 0;
970 return compiler_addop_i(c, opcode, arg);
973 /* Add an opcode with an integer argument.
974 Returns 0 on failure, 1 on success.
977 static int
978 compiler_addop_i(struct compiler *c, int opcode, int oparg)
980 struct instr *i;
981 int off;
982 off = compiler_next_instr(c, c->u->u_curblock);
983 if (off < 0)
984 return 0;
985 i = &c->u->u_curblock->b_instr[off];
986 i->i_opcode = opcode;
987 i->i_oparg = oparg;
988 i->i_hasarg = 1;
989 compiler_set_lineno(c, off);
990 return 1;
993 static int
994 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
996 struct instr *i;
997 int off;
999 assert(b != NULL);
1000 off = compiler_next_instr(c, c->u->u_curblock);
1001 if (off < 0)
1002 return 0;
1003 i = &c->u->u_curblock->b_instr[off];
1004 i->i_opcode = opcode;
1005 i->i_target = b;
1006 i->i_hasarg = 1;
1007 if (absolute)
1008 i->i_jabs = 1;
1009 else
1010 i->i_jrel = 1;
1011 compiler_set_lineno(c, off);
1012 return 1;
1015 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1016 like to find better names.) NEW_BLOCK() creates a new block and sets
1017 it as the current block. NEXT_BLOCK() also creates an implicit jump
1018 from the current block to the new block.
1021 /* The returns inside these macros make it impossible to decref objects
1022 created in the local function. Local objects should use the arena.
1026 #define NEW_BLOCK(C) { \
1027 if (compiler_use_new_block((C)) == NULL) \
1028 return 0; \
1031 #define NEXT_BLOCK(C) { \
1032 if (compiler_next_block((C)) == NULL) \
1033 return 0; \
1036 #define ADDOP(C, OP) { \
1037 if (!compiler_addop((C), (OP))) \
1038 return 0; \
1041 #define ADDOP_IN_SCOPE(C, OP) { \
1042 if (!compiler_addop((C), (OP))) { \
1043 compiler_exit_scope(c); \
1044 return 0; \
1048 #define ADDOP_O(C, OP, O, TYPE) { \
1049 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1050 return 0; \
1053 #define ADDOP_NAME(C, OP, O, TYPE) { \
1054 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1055 return 0; \
1058 #define ADDOP_I(C, OP, O) { \
1059 if (!compiler_addop_i((C), (OP), (O))) \
1060 return 0; \
1063 #define ADDOP_JABS(C, OP, O) { \
1064 if (!compiler_addop_j((C), (OP), (O), 1)) \
1065 return 0; \
1068 #define ADDOP_JREL(C, OP, O) { \
1069 if (!compiler_addop_j((C), (OP), (O), 0)) \
1070 return 0; \
1073 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1074 the ASDL name to synthesize the name of the C type and the visit function.
1077 #define VISIT(C, TYPE, V) {\
1078 if (!compiler_visit_ ## TYPE((C), (V))) \
1079 return 0; \
1082 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1083 if (!compiler_visit_ ## TYPE((C), (V))) { \
1084 compiler_exit_scope(c); \
1085 return 0; \
1089 #define VISIT_SLICE(C, V, CTX) {\
1090 if (!compiler_visit_slice((C), (V), (CTX))) \
1091 return 0; \
1094 #define VISIT_SEQ(C, TYPE, SEQ) { \
1095 int _i; \
1096 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1097 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1098 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1099 if (!compiler_visit_ ## TYPE((C), elt)) \
1100 return 0; \
1104 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1105 int _i; \
1106 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1107 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1108 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1109 if (!compiler_visit_ ## TYPE((C), elt)) { \
1110 compiler_exit_scope(c); \
1111 return 0; \
1116 static int
1117 compiler_isdocstring(stmt_ty s)
1119 if (s->kind != Expr_kind)
1120 return 0;
1121 return s->v.Expr.value->kind == Str_kind;
1124 /* Compile a sequence of statements, checking for a docstring. */
1126 static int
1127 compiler_body(struct compiler *c, asdl_seq *stmts)
1129 int i = 0;
1130 stmt_ty st;
1132 if (!asdl_seq_LEN(stmts))
1133 return 1;
1134 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1135 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1136 /* don't generate docstrings if -OO */
1137 i = 1;
1138 VISIT(c, expr, st->v.Expr.value);
1139 if (!compiler_nameop(c, __doc__, Store))
1140 return 0;
1142 for (; i < asdl_seq_LEN(stmts); i++)
1143 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1144 return 1;
1147 static PyCodeObject *
1148 compiler_mod(struct compiler *c, mod_ty mod)
1150 PyCodeObject *co;
1151 int addNone = 1;
1152 static PyObject *module;
1153 if (!module) {
1154 module = PyString_FromString("<module>");
1155 if (!module)
1156 return NULL;
1158 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1159 if (!compiler_enter_scope(c, module, mod, 0))
1160 return NULL;
1161 switch (mod->kind) {
1162 case Module_kind:
1163 if (!compiler_body(c, mod->v.Module.body)) {
1164 compiler_exit_scope(c);
1165 return 0;
1167 break;
1168 case Interactive_kind:
1169 c->c_interactive = 1;
1170 VISIT_SEQ_IN_SCOPE(c, stmt,
1171 mod->v.Interactive.body);
1172 break;
1173 case Expression_kind:
1174 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1175 addNone = 0;
1176 break;
1177 case Suite_kind:
1178 PyErr_SetString(PyExc_SystemError,
1179 "suite should not be possible");
1180 return 0;
1181 default:
1182 PyErr_Format(PyExc_SystemError,
1183 "module kind %d should not be possible",
1184 mod->kind);
1185 return 0;
1187 co = assemble(c, addNone);
1188 compiler_exit_scope(c);
1189 return co;
1192 /* The test for LOCAL must come before the test for FREE in order to
1193 handle classes where name is both local and free. The local var is
1194 a method and the free var is a free var referenced within a method.
1197 static int
1198 get_ref_type(struct compiler *c, PyObject *name)
1200 int scope = PyST_GetScope(c->u->u_ste, name);
1201 if (scope == 0) {
1202 char buf[350];
1203 PyOS_snprintf(buf, sizeof(buf),
1204 "unknown scope for %.100s in %.100s(%s) in %s\n"
1205 "symbols: %s\nlocals: %s\nglobals: %s\n",
1206 PyString_AS_STRING(name),
1207 PyString_AS_STRING(c->u->u_name),
1208 PyObject_REPR(c->u->u_ste->ste_id),
1209 c->c_filename,
1210 PyObject_REPR(c->u->u_ste->ste_symbols),
1211 PyObject_REPR(c->u->u_varnames),
1212 PyObject_REPR(c->u->u_names)
1214 Py_FatalError(buf);
1217 return scope;
1220 static int
1221 compiler_lookup_arg(PyObject *dict, PyObject *name)
1223 PyObject *k, *v;
1224 k = PyTuple_Pack(2, name, name->ob_type);
1225 if (k == NULL)
1226 return -1;
1227 v = PyDict_GetItem(dict, k);
1228 Py_DECREF(k);
1229 if (v == NULL)
1230 return -1;
1231 return PyInt_AS_LONG(v);
1234 static int
1235 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1237 int i, free = PyCode_GetNumFree(co);
1238 if (free == 0) {
1239 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1240 ADDOP_I(c, MAKE_FUNCTION, args);
1241 return 1;
1243 for (i = 0; i < free; ++i) {
1244 /* Bypass com_addop_varname because it will generate
1245 LOAD_DEREF but LOAD_CLOSURE is needed.
1247 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1248 int arg, reftype;
1250 /* Special case: If a class contains a method with a
1251 free variable that has the same name as a method,
1252 the name will be considered free *and* local in the
1253 class. It should be handled by the closure, as
1254 well as by the normal name loookup logic.
1256 reftype = get_ref_type(c, name);
1257 if (reftype == CELL)
1258 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1259 else /* (reftype == FREE) */
1260 arg = compiler_lookup_arg(c->u->u_freevars, name);
1261 if (arg == -1) {
1262 printf("lookup %s in %s %d %d\n"
1263 "freevars of %s: %s\n",
1264 PyObject_REPR(name),
1265 PyString_AS_STRING(c->u->u_name),
1266 reftype, arg,
1267 PyString_AS_STRING(co->co_name),
1268 PyObject_REPR(co->co_freevars));
1269 Py_FatalError("compiler_make_closure()");
1271 ADDOP_I(c, LOAD_CLOSURE, arg);
1273 ADDOP_I(c, BUILD_TUPLE, free);
1274 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1275 ADDOP_I(c, MAKE_CLOSURE, args);
1276 return 1;
1279 static int
1280 compiler_decorators(struct compiler *c, asdl_seq* decos)
1282 int i;
1284 if (!decos)
1285 return 1;
1287 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1288 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1290 return 1;
1293 static int
1294 compiler_arguments(struct compiler *c, arguments_ty args)
1296 int i;
1297 int n = asdl_seq_LEN(args->args);
1298 /* Correctly handle nested argument lists */
1299 for (i = 0; i < n; i++) {
1300 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
1301 if (arg->kind == Tuple_kind) {
1302 PyObject *id = PyString_FromFormat(".%d", i);
1303 if (id == NULL) {
1304 return 0;
1306 if (!compiler_nameop(c, id, Load)) {
1307 Py_DECREF(id);
1308 return 0;
1310 Py_DECREF(id);
1311 VISIT(c, expr, arg);
1314 return 1;
1317 static int
1318 compiler_function(struct compiler *c, stmt_ty s)
1320 PyCodeObject *co;
1321 PyObject *first_const = Py_None;
1322 arguments_ty args = s->v.FunctionDef.args;
1323 asdl_seq* decos = s->v.FunctionDef.decorators;
1324 stmt_ty st;
1325 int i, n, docstring;
1327 assert(s->kind == FunctionDef_kind);
1329 if (!compiler_decorators(c, decos))
1330 return 0;
1331 if (args->defaults)
1332 VISIT_SEQ(c, expr, args->defaults);
1333 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1334 s->lineno))
1335 return 0;
1337 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1338 docstring = compiler_isdocstring(st);
1339 if (docstring)
1340 first_const = st->v.Expr.value->v.Str.s;
1341 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1342 compiler_exit_scope(c);
1343 return 0;
1346 /* unpack nested arguments */
1347 compiler_arguments(c, args);
1349 c->u->u_argcount = asdl_seq_LEN(args->args);
1350 n = asdl_seq_LEN(s->v.FunctionDef.body);
1351 /* if there was a docstring, we need to skip the first statement */
1352 for (i = docstring; i < n; i++) {
1353 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1354 VISIT_IN_SCOPE(c, stmt, st);
1356 co = assemble(c, 1);
1357 compiler_exit_scope(c);
1358 if (co == NULL)
1359 return 0;
1361 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1362 Py_DECREF(co);
1364 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1365 ADDOP_I(c, CALL_FUNCTION, 1);
1368 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1371 static int
1372 compiler_class(struct compiler *c, stmt_ty s)
1374 int n;
1375 PyCodeObject *co;
1376 PyObject *str;
1377 /* push class name on stack, needed by BUILD_CLASS */
1378 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1379 /* push the tuple of base classes on the stack */
1380 n = asdl_seq_LEN(s->v.ClassDef.bases);
1381 if (n > 0)
1382 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1383 ADDOP_I(c, BUILD_TUPLE, n);
1384 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1385 s->lineno))
1386 return 0;
1387 c->u->u_private = s->v.ClassDef.name;
1388 Py_INCREF(c->u->u_private);
1389 str = PyString_InternFromString("__name__");
1390 if (!str || !compiler_nameop(c, str, Load)) {
1391 Py_XDECREF(str);
1392 compiler_exit_scope(c);
1393 return 0;
1396 Py_DECREF(str);
1397 str = PyString_InternFromString("__module__");
1398 if (!str || !compiler_nameop(c, str, Store)) {
1399 Py_XDECREF(str);
1400 compiler_exit_scope(c);
1401 return 0;
1403 Py_DECREF(str);
1405 if (!compiler_body(c, s->v.ClassDef.body)) {
1406 compiler_exit_scope(c);
1407 return 0;
1410 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1411 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1412 co = assemble(c, 1);
1413 compiler_exit_scope(c);
1414 if (co == NULL)
1415 return 0;
1417 compiler_make_closure(c, co, 0);
1418 Py_DECREF(co);
1420 ADDOP_I(c, CALL_FUNCTION, 0);
1421 ADDOP(c, BUILD_CLASS);
1422 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1423 return 0;
1424 return 1;
1427 static int
1428 compiler_ifexp(struct compiler *c, expr_ty e)
1430 basicblock *end, *next;
1432 assert(e->kind == IfExp_kind);
1433 end = compiler_new_block(c);
1434 if (end == NULL)
1435 return 0;
1436 next = compiler_new_block(c);
1437 if (next == NULL)
1438 return 0;
1439 VISIT(c, expr, e->v.IfExp.test);
1440 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1441 ADDOP(c, POP_TOP);
1442 VISIT(c, expr, e->v.IfExp.body);
1443 ADDOP_JREL(c, JUMP_FORWARD, end);
1444 compiler_use_next_block(c, next);
1445 ADDOP(c, POP_TOP);
1446 VISIT(c, expr, e->v.IfExp.orelse);
1447 compiler_use_next_block(c, end);
1448 return 1;
1451 static int
1452 compiler_lambda(struct compiler *c, expr_ty e)
1454 PyCodeObject *co;
1455 static identifier name;
1456 arguments_ty args = e->v.Lambda.args;
1457 assert(e->kind == Lambda_kind);
1459 if (!name) {
1460 name = PyString_InternFromString("<lambda>");
1461 if (!name)
1462 return 0;
1465 if (args->defaults)
1466 VISIT_SEQ(c, expr, args->defaults);
1467 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1468 return 0;
1470 /* unpack nested arguments */
1471 compiler_arguments(c, args);
1473 c->u->u_argcount = asdl_seq_LEN(args->args);
1474 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1475 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1476 co = assemble(c, 1);
1477 compiler_exit_scope(c);
1478 if (co == NULL)
1479 return 0;
1481 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1482 Py_DECREF(co);
1484 return 1;
1487 static int
1488 compiler_print(struct compiler *c, stmt_ty s)
1490 int i, n;
1491 bool dest;
1493 assert(s->kind == Print_kind);
1494 n = asdl_seq_LEN(s->v.Print.values);
1495 dest = false;
1496 if (s->v.Print.dest) {
1497 VISIT(c, expr, s->v.Print.dest);
1498 dest = true;
1500 for (i = 0; i < n; i++) {
1501 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1502 if (dest) {
1503 ADDOP(c, DUP_TOP);
1504 VISIT(c, expr, e);
1505 ADDOP(c, ROT_TWO);
1506 ADDOP(c, PRINT_ITEM_TO);
1508 else {
1509 VISIT(c, expr, e);
1510 ADDOP(c, PRINT_ITEM);
1513 if (s->v.Print.nl) {
1514 if (dest)
1515 ADDOP(c, PRINT_NEWLINE_TO)
1516 else
1517 ADDOP(c, PRINT_NEWLINE)
1519 else if (dest)
1520 ADDOP(c, POP_TOP);
1521 return 1;
1524 static int
1525 compiler_if(struct compiler *c, stmt_ty s)
1527 basicblock *end, *next;
1528 int constant;
1529 assert(s->kind == If_kind);
1530 end = compiler_new_block(c);
1531 if (end == NULL)
1532 return 0;
1533 next = compiler_new_block(c);
1534 if (next == NULL)
1535 return 0;
1537 constant = expr_constant(s->v.If.test);
1538 /* constant = 0: "if 0"
1539 * constant = 1: "if 1", "if 2", ...
1540 * constant = -1: rest */
1541 if (constant == 0) {
1542 if (s->v.If.orelse)
1543 VISIT_SEQ(c, stmt, s->v.If.orelse);
1544 } else if (constant == 1) {
1545 VISIT_SEQ(c, stmt, s->v.If.body);
1546 } else {
1547 VISIT(c, expr, s->v.If.test);
1548 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1549 ADDOP(c, POP_TOP);
1550 VISIT_SEQ(c, stmt, s->v.If.body);
1551 ADDOP_JREL(c, JUMP_FORWARD, end);
1552 compiler_use_next_block(c, next);
1553 ADDOP(c, POP_TOP);
1554 if (s->v.If.orelse)
1555 VISIT_SEQ(c, stmt, s->v.If.orelse);
1557 compiler_use_next_block(c, end);
1558 return 1;
1561 static int
1562 compiler_for(struct compiler *c, stmt_ty s)
1564 basicblock *start, *cleanup, *end;
1566 start = compiler_new_block(c);
1567 cleanup = compiler_new_block(c);
1568 end = compiler_new_block(c);
1569 if (start == NULL || end == NULL || cleanup == NULL)
1570 return 0;
1571 ADDOP_JREL(c, SETUP_LOOP, end);
1572 if (!compiler_push_fblock(c, LOOP, start))
1573 return 0;
1574 VISIT(c, expr, s->v.For.iter);
1575 ADDOP(c, GET_ITER);
1576 compiler_use_next_block(c, start);
1577 /* XXX(nnorwitz): is there a better way to handle this?
1578 for loops are special, we want to be able to trace them
1579 each time around, so we need to set an extra line number. */
1580 c->u->u_lineno_set = false;
1581 ADDOP_JREL(c, FOR_ITER, cleanup);
1582 VISIT(c, expr, s->v.For.target);
1583 VISIT_SEQ(c, stmt, s->v.For.body);
1584 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1585 compiler_use_next_block(c, cleanup);
1586 ADDOP(c, POP_BLOCK);
1587 compiler_pop_fblock(c, LOOP, start);
1588 VISIT_SEQ(c, stmt, s->v.For.orelse);
1589 compiler_use_next_block(c, end);
1590 return 1;
1593 static int
1594 compiler_while(struct compiler *c, stmt_ty s)
1596 basicblock *loop, *orelse, *end, *anchor = NULL;
1597 int constant = expr_constant(s->v.While.test);
1599 if (constant == 0)
1600 return 1;
1601 loop = compiler_new_block(c);
1602 end = compiler_new_block(c);
1603 if (constant == -1) {
1604 anchor = compiler_new_block(c);
1605 if (anchor == NULL)
1606 return 0;
1608 if (loop == NULL || end == NULL)
1609 return 0;
1610 if (s->v.While.orelse) {
1611 orelse = compiler_new_block(c);
1612 if (orelse == NULL)
1613 return 0;
1615 else
1616 orelse = NULL;
1618 ADDOP_JREL(c, SETUP_LOOP, end);
1619 compiler_use_next_block(c, loop);
1620 if (!compiler_push_fblock(c, LOOP, loop))
1621 return 0;
1622 if (constant == -1) {
1623 VISIT(c, expr, s->v.While.test);
1624 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1625 ADDOP(c, POP_TOP);
1627 VISIT_SEQ(c, stmt, s->v.While.body);
1628 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1630 /* XXX should the two POP instructions be in a separate block
1631 if there is no else clause ?
1634 if (constant == -1) {
1635 compiler_use_next_block(c, anchor);
1636 ADDOP(c, POP_TOP);
1637 ADDOP(c, POP_BLOCK);
1639 compiler_pop_fblock(c, LOOP, loop);
1640 if (orelse != NULL) /* what if orelse is just pass? */
1641 VISIT_SEQ(c, stmt, s->v.While.orelse);
1642 compiler_use_next_block(c, end);
1644 return 1;
1647 static int
1648 compiler_continue(struct compiler *c)
1650 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1651 static const char IN_FINALLY_ERROR_MSG[] =
1652 "'continue' not supported inside 'finally' clause";
1653 int i;
1655 if (!c->u->u_nfblocks)
1656 return compiler_error(c, LOOP_ERROR_MSG);
1657 i = c->u->u_nfblocks - 1;
1658 switch (c->u->u_fblock[i].fb_type) {
1659 case LOOP:
1660 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1661 break;
1662 case EXCEPT:
1663 case FINALLY_TRY:
1664 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1665 /* Prevent continue anywhere under a finally
1666 even if hidden in a sub-try or except. */
1667 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1668 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1670 if (i == -1)
1671 return compiler_error(c, LOOP_ERROR_MSG);
1672 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1673 break;
1674 case FINALLY_END:
1675 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1678 return 1;
1681 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1683 SETUP_FINALLY L
1684 <code for body>
1685 POP_BLOCK
1686 LOAD_CONST <None>
1687 L: <code for finalbody>
1688 END_FINALLY
1690 The special instructions use the block stack. Each block
1691 stack entry contains the instruction that created it (here
1692 SETUP_FINALLY), the level of the value stack at the time the
1693 block stack entry was created, and a label (here L).
1695 SETUP_FINALLY:
1696 Pushes the current value stack level and the label
1697 onto the block stack.
1698 POP_BLOCK:
1699 Pops en entry from the block stack, and pops the value
1700 stack until its level is the same as indicated on the
1701 block stack. (The label is ignored.)
1702 END_FINALLY:
1703 Pops a variable number of entries from the *value* stack
1704 and re-raises the exception they specify. The number of
1705 entries popped depends on the (pseudo) exception type.
1707 The block stack is unwound when an exception is raised:
1708 when a SETUP_FINALLY entry is found, the exception is pushed
1709 onto the value stack (and the exception condition is cleared),
1710 and the interpreter jumps to the label gotten from the block
1711 stack.
1714 static int
1715 compiler_try_finally(struct compiler *c, stmt_ty s)
1717 basicblock *body, *end;
1718 body = compiler_new_block(c);
1719 end = compiler_new_block(c);
1720 if (body == NULL || end == NULL)
1721 return 0;
1723 ADDOP_JREL(c, SETUP_FINALLY, end);
1724 compiler_use_next_block(c, body);
1725 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1726 return 0;
1727 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1728 ADDOP(c, POP_BLOCK);
1729 compiler_pop_fblock(c, FINALLY_TRY, body);
1731 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1732 compiler_use_next_block(c, end);
1733 if (!compiler_push_fblock(c, FINALLY_END, end))
1734 return 0;
1735 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1736 ADDOP(c, END_FINALLY);
1737 compiler_pop_fblock(c, FINALLY_END, end);
1739 return 1;
1743 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1744 (The contents of the value stack is shown in [], with the top
1745 at the right; 'tb' is trace-back info, 'val' the exception's
1746 associated value, and 'exc' the exception.)
1748 Value stack Label Instruction Argument
1749 [] SETUP_EXCEPT L1
1750 [] <code for S>
1751 [] POP_BLOCK
1752 [] JUMP_FORWARD L0
1754 [tb, val, exc] L1: DUP )
1755 [tb, val, exc, exc] <evaluate E1> )
1756 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1757 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1758 [tb, val, exc, 1] POP )
1759 [tb, val, exc] POP
1760 [tb, val] <assign to V1> (or POP if no V1)
1761 [tb] POP
1762 [] <code for S1>
1763 JUMP_FORWARD L0
1765 [tb, val, exc, 0] L2: POP
1766 [tb, val, exc] DUP
1767 .............................etc.......................
1769 [tb, val, exc, 0] Ln+1: POP
1770 [tb, val, exc] END_FINALLY # re-raise exception
1772 [] L0: <next statement>
1774 Of course, parts are not generated if Vi or Ei is not present.
1776 static int
1777 compiler_try_except(struct compiler *c, stmt_ty s)
1779 basicblock *body, *orelse, *except, *end;
1780 int i, n;
1782 body = compiler_new_block(c);
1783 except = compiler_new_block(c);
1784 orelse = compiler_new_block(c);
1785 end = compiler_new_block(c);
1786 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1787 return 0;
1788 ADDOP_JREL(c, SETUP_EXCEPT, except);
1789 compiler_use_next_block(c, body);
1790 if (!compiler_push_fblock(c, EXCEPT, body))
1791 return 0;
1792 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1793 ADDOP(c, POP_BLOCK);
1794 compiler_pop_fblock(c, EXCEPT, body);
1795 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1796 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1797 compiler_use_next_block(c, except);
1798 for (i = 0; i < n; i++) {
1799 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1800 s->v.TryExcept.handlers, i);
1801 if (!handler->type && i < n-1)
1802 return compiler_error(c, "default 'except:' must be last");
1803 c->u->u_lineno_set = false;
1804 c->u->u_lineno = handler->lineno;
1805 except = compiler_new_block(c);
1806 if (except == NULL)
1807 return 0;
1808 if (handler->type) {
1809 ADDOP(c, DUP_TOP);
1810 VISIT(c, expr, handler->type);
1811 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1812 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1813 ADDOP(c, POP_TOP);
1815 ADDOP(c, POP_TOP);
1816 if (handler->name) {
1817 VISIT(c, expr, handler->name);
1819 else {
1820 ADDOP(c, POP_TOP);
1822 ADDOP(c, POP_TOP);
1823 VISIT_SEQ(c, stmt, handler->body);
1824 ADDOP_JREL(c, JUMP_FORWARD, end);
1825 compiler_use_next_block(c, except);
1826 if (handler->type)
1827 ADDOP(c, POP_TOP);
1829 ADDOP(c, END_FINALLY);
1830 compiler_use_next_block(c, orelse);
1831 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
1832 compiler_use_next_block(c, end);
1833 return 1;
1836 static int
1837 compiler_import_as(struct compiler *c, identifier name, identifier asname)
1839 /* The IMPORT_NAME opcode was already generated. This function
1840 merely needs to bind the result to a name.
1842 If there is a dot in name, we need to split it and emit a
1843 LOAD_ATTR for each name.
1845 const char *src = PyString_AS_STRING(name);
1846 const char *dot = strchr(src, '.');
1847 if (dot) {
1848 /* Consume the base module name to get the first attribute */
1849 src = dot + 1;
1850 while (dot) {
1851 /* NB src is only defined when dot != NULL */
1852 PyObject *attr;
1853 dot = strchr(src, '.');
1854 attr = PyString_FromStringAndSize(src,
1855 dot ? dot - src : strlen(src));
1856 if (!attr)
1857 return -1;
1858 ADDOP_O(c, LOAD_ATTR, attr, names);
1859 Py_DECREF(attr);
1860 src = dot + 1;
1863 return compiler_nameop(c, asname, Store);
1866 static int
1867 compiler_import(struct compiler *c, stmt_ty s)
1869 /* The Import node stores a module name like a.b.c as a single
1870 string. This is convenient for all cases except
1871 import a.b.c as d
1872 where we need to parse that string to extract the individual
1873 module names.
1874 XXX Perhaps change the representation to make this case simpler?
1876 int i, n = asdl_seq_LEN(s->v.Import.names);
1878 for (i = 0; i < n; i++) {
1879 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
1880 int r;
1881 PyObject *level;
1883 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1884 level = PyInt_FromLong(0);
1885 else
1886 level = PyInt_FromLong(-1);
1888 if (level == NULL)
1889 return 0;
1891 ADDOP_O(c, LOAD_CONST, level, consts);
1892 Py_DECREF(level);
1893 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1894 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1896 if (alias->asname) {
1897 r = compiler_import_as(c, alias->name, alias->asname);
1898 if (!r)
1899 return r;
1901 else {
1902 identifier tmp = alias->name;
1903 const char *base = PyString_AS_STRING(alias->name);
1904 char *dot = strchr(base, '.');
1905 if (dot)
1906 tmp = PyString_FromStringAndSize(base,
1907 dot - base);
1908 r = compiler_nameop(c, tmp, Store);
1909 if (dot) {
1910 Py_DECREF(tmp);
1912 if (!r)
1913 return r;
1916 return 1;
1919 static int
1920 compiler_from_import(struct compiler *c, stmt_ty s)
1922 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
1924 PyObject *names = PyTuple_New(n);
1925 PyObject *level;
1927 if (!names)
1928 return 0;
1930 if (s->v.ImportFrom.level == 0 && c->c_flags &&
1931 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1932 level = PyInt_FromLong(-1);
1933 else
1934 level = PyInt_FromLong(s->v.ImportFrom.level);
1936 if (!level) {
1937 Py_DECREF(names);
1938 return 0;
1941 /* build up the names */
1942 for (i = 0; i < n; i++) {
1943 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
1944 Py_INCREF(alias->name);
1945 PyTuple_SET_ITEM(names, i, alias->name);
1948 if (s->lineno > c->c_future->ff_lineno) {
1949 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
1950 "__future__")) {
1951 Py_DECREF(level);
1952 Py_DECREF(names);
1953 return compiler_error(c,
1954 "from __future__ imports must occur "
1955 "at the beginning of the file");
1960 ADDOP_O(c, LOAD_CONST, level, consts);
1961 Py_DECREF(level);
1962 ADDOP_O(c, LOAD_CONST, names, consts);
1963 Py_DECREF(names);
1964 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
1965 for (i = 0; i < n; i++) {
1966 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
1967 identifier store_name;
1969 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
1970 assert(n == 1);
1971 ADDOP(c, IMPORT_STAR);
1972 return 1;
1975 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
1976 store_name = alias->name;
1977 if (alias->asname)
1978 store_name = alias->asname;
1980 if (!compiler_nameop(c, store_name, Store)) {
1981 Py_DECREF(names);
1982 return 0;
1985 /* remove imported module */
1986 ADDOP(c, POP_TOP);
1987 return 1;
1990 static int
1991 compiler_assert(struct compiler *c, stmt_ty s)
1993 static PyObject *assertion_error = NULL;
1994 basicblock *end;
1996 if (Py_OptimizeFlag)
1997 return 1;
1998 if (assertion_error == NULL) {
1999 assertion_error = PyString_FromString("AssertionError");
2000 if (assertion_error == NULL)
2001 return 0;
2003 VISIT(c, expr, s->v.Assert.test);
2004 end = compiler_new_block(c);
2005 if (end == NULL)
2006 return 0;
2007 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2008 ADDOP(c, POP_TOP);
2009 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2010 if (s->v.Assert.msg) {
2011 VISIT(c, expr, s->v.Assert.msg);
2012 ADDOP_I(c, RAISE_VARARGS, 2);
2014 else {
2015 ADDOP_I(c, RAISE_VARARGS, 1);
2017 compiler_use_next_block(c, end);
2018 ADDOP(c, POP_TOP);
2019 return 1;
2022 static int
2023 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2025 int i, n;
2027 /* Always assign a lineno to the next instruction for a stmt. */
2028 c->u->u_lineno = s->lineno;
2029 c->u->u_lineno_set = false;
2031 switch (s->kind) {
2032 case FunctionDef_kind:
2033 return compiler_function(c, s);
2034 case ClassDef_kind:
2035 return compiler_class(c, s);
2036 case Return_kind:
2037 if (c->u->u_ste->ste_type != FunctionBlock)
2038 return compiler_error(c, "'return' outside function");
2039 if (s->v.Return.value) {
2040 VISIT(c, expr, s->v.Return.value);
2042 else
2043 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2044 ADDOP(c, RETURN_VALUE);
2045 break;
2046 case Delete_kind:
2047 VISIT_SEQ(c, expr, s->v.Delete.targets)
2048 break;
2049 case Assign_kind:
2050 n = asdl_seq_LEN(s->v.Assign.targets);
2051 VISIT(c, expr, s->v.Assign.value);
2052 for (i = 0; i < n; i++) {
2053 if (i < n - 1)
2054 ADDOP(c, DUP_TOP);
2055 VISIT(c, expr,
2056 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2058 break;
2059 case AugAssign_kind:
2060 return compiler_augassign(c, s);
2061 case Print_kind:
2062 return compiler_print(c, s);
2063 case For_kind:
2064 return compiler_for(c, s);
2065 case While_kind:
2066 return compiler_while(c, s);
2067 case If_kind:
2068 return compiler_if(c, s);
2069 case Raise_kind:
2070 n = 0;
2071 if (s->v.Raise.type) {
2072 VISIT(c, expr, s->v.Raise.type);
2073 n++;
2074 if (s->v.Raise.inst) {
2075 VISIT(c, expr, s->v.Raise.inst);
2076 n++;
2077 if (s->v.Raise.tback) {
2078 VISIT(c, expr, s->v.Raise.tback);
2079 n++;
2083 ADDOP_I(c, RAISE_VARARGS, n);
2084 break;
2085 case TryExcept_kind:
2086 return compiler_try_except(c, s);
2087 case TryFinally_kind:
2088 return compiler_try_finally(c, s);
2089 case Assert_kind:
2090 return compiler_assert(c, s);
2091 case Import_kind:
2092 return compiler_import(c, s);
2093 case ImportFrom_kind:
2094 return compiler_from_import(c, s);
2095 case Exec_kind:
2096 VISIT(c, expr, s->v.Exec.body);
2097 if (s->v.Exec.globals) {
2098 VISIT(c, expr, s->v.Exec.globals);
2099 if (s->v.Exec.locals) {
2100 VISIT(c, expr, s->v.Exec.locals);
2101 } else {
2102 ADDOP(c, DUP_TOP);
2104 } else {
2105 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2106 ADDOP(c, DUP_TOP);
2108 ADDOP(c, EXEC_STMT);
2109 break;
2110 case Global_kind:
2111 break;
2112 case Expr_kind:
2113 if (c->c_interactive && c->c_nestlevel <= 1) {
2114 VISIT(c, expr, s->v.Expr.value);
2115 ADDOP(c, PRINT_EXPR);
2117 else if (s->v.Expr.value->kind != Str_kind &&
2118 s->v.Expr.value->kind != Num_kind) {
2119 VISIT(c, expr, s->v.Expr.value);
2120 ADDOP(c, POP_TOP);
2122 break;
2123 case Pass_kind:
2124 break;
2125 case Break_kind:
2126 if (!compiler_in_loop(c))
2127 return compiler_error(c, "'break' outside loop");
2128 ADDOP(c, BREAK_LOOP);
2129 break;
2130 case Continue_kind:
2131 return compiler_continue(c);
2132 case With_kind:
2133 return compiler_with(c, s);
2135 return 1;
2138 static int
2139 unaryop(unaryop_ty op)
2141 switch (op) {
2142 case Invert:
2143 return UNARY_INVERT;
2144 case Not:
2145 return UNARY_NOT;
2146 case UAdd:
2147 return UNARY_POSITIVE;
2148 case USub:
2149 return UNARY_NEGATIVE;
2151 return 0;
2154 static int
2155 binop(struct compiler *c, operator_ty op)
2157 switch (op) {
2158 case Add:
2159 return BINARY_ADD;
2160 case Sub:
2161 return BINARY_SUBTRACT;
2162 case Mult:
2163 return BINARY_MULTIPLY;
2164 case Div:
2165 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2166 return BINARY_TRUE_DIVIDE;
2167 else
2168 return BINARY_DIVIDE;
2169 case Mod:
2170 return BINARY_MODULO;
2171 case Pow:
2172 return BINARY_POWER;
2173 case LShift:
2174 return BINARY_LSHIFT;
2175 case RShift:
2176 return BINARY_RSHIFT;
2177 case BitOr:
2178 return BINARY_OR;
2179 case BitXor:
2180 return BINARY_XOR;
2181 case BitAnd:
2182 return BINARY_AND;
2183 case FloorDiv:
2184 return BINARY_FLOOR_DIVIDE;
2186 return 0;
2189 static int
2190 cmpop(cmpop_ty op)
2192 switch (op) {
2193 case Eq:
2194 return PyCmp_EQ;
2195 case NotEq:
2196 return PyCmp_NE;
2197 case Lt:
2198 return PyCmp_LT;
2199 case LtE:
2200 return PyCmp_LE;
2201 case Gt:
2202 return PyCmp_GT;
2203 case GtE:
2204 return PyCmp_GE;
2205 case Is:
2206 return PyCmp_IS;
2207 case IsNot:
2208 return PyCmp_IS_NOT;
2209 case In:
2210 return PyCmp_IN;
2211 case NotIn:
2212 return PyCmp_NOT_IN;
2214 return PyCmp_BAD;
2217 static int
2218 inplace_binop(struct compiler *c, operator_ty op)
2220 switch (op) {
2221 case Add:
2222 return INPLACE_ADD;
2223 case Sub:
2224 return INPLACE_SUBTRACT;
2225 case Mult:
2226 return INPLACE_MULTIPLY;
2227 case Div:
2228 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2229 return INPLACE_TRUE_DIVIDE;
2230 else
2231 return INPLACE_DIVIDE;
2232 case Mod:
2233 return INPLACE_MODULO;
2234 case Pow:
2235 return INPLACE_POWER;
2236 case LShift:
2237 return INPLACE_LSHIFT;
2238 case RShift:
2239 return INPLACE_RSHIFT;
2240 case BitOr:
2241 return INPLACE_OR;
2242 case BitXor:
2243 return INPLACE_XOR;
2244 case BitAnd:
2245 return INPLACE_AND;
2246 case FloorDiv:
2247 return INPLACE_FLOOR_DIVIDE;
2249 PyErr_Format(PyExc_SystemError,
2250 "inplace binary op %d should not be possible", op);
2251 return 0;
2254 static int
2255 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2257 int op, scope, arg;
2258 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2260 PyObject *dict = c->u->u_names;
2261 PyObject *mangled;
2262 /* XXX AugStore isn't used anywhere! */
2264 /* First check for assignment to __debug__. Param? */
2265 if ((ctx == Store || ctx == AugStore || ctx == Del)
2266 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2267 return compiler_error(c, "can not assign to __debug__");
2270 mangled = _Py_Mangle(c->u->u_private, name);
2271 if (!mangled)
2272 return 0;
2274 op = 0;
2275 optype = OP_NAME;
2276 scope = PyST_GetScope(c->u->u_ste, mangled);
2277 switch (scope) {
2278 case FREE:
2279 dict = c->u->u_freevars;
2280 optype = OP_DEREF;
2281 break;
2282 case CELL:
2283 dict = c->u->u_cellvars;
2284 optype = OP_DEREF;
2285 break;
2286 case LOCAL:
2287 if (c->u->u_ste->ste_type == FunctionBlock)
2288 optype = OP_FAST;
2289 break;
2290 case GLOBAL_IMPLICIT:
2291 if (c->u->u_ste->ste_type == FunctionBlock &&
2292 !c->u->u_ste->ste_unoptimized)
2293 optype = OP_GLOBAL;
2294 break;
2295 case GLOBAL_EXPLICIT:
2296 optype = OP_GLOBAL;
2297 break;
2298 default:
2299 /* scope can be 0 */
2300 break;
2303 /* XXX Leave assert here, but handle __doc__ and the like better */
2304 assert(scope || PyString_AS_STRING(name)[0] == '_');
2306 switch (optype) {
2307 case OP_DEREF:
2308 switch (ctx) {
2309 case Load: op = LOAD_DEREF; break;
2310 case Store: op = STORE_DEREF; break;
2311 case AugLoad:
2312 case AugStore:
2313 break;
2314 case Del:
2315 PyErr_Format(PyExc_SyntaxError,
2316 "can not delete variable '%s' referenced "
2317 "in nested scope",
2318 PyString_AS_STRING(name));
2319 Py_DECREF(mangled);
2320 return 0;
2321 case Param:
2322 default:
2323 PyErr_SetString(PyExc_SystemError,
2324 "param invalid for deref variable");
2325 return 0;
2327 break;
2328 case OP_FAST:
2329 switch (ctx) {
2330 case Load: op = LOAD_FAST; break;
2331 case Store: op = STORE_FAST; break;
2332 case Del: op = DELETE_FAST; break;
2333 case AugLoad:
2334 case AugStore:
2335 break;
2336 case Param:
2337 default:
2338 PyErr_SetString(PyExc_SystemError,
2339 "param invalid for local variable");
2340 return 0;
2342 ADDOP_O(c, op, mangled, varnames);
2343 Py_DECREF(mangled);
2344 return 1;
2345 case OP_GLOBAL:
2346 switch (ctx) {
2347 case Load: op = LOAD_GLOBAL; break;
2348 case Store: op = STORE_GLOBAL; break;
2349 case Del: op = DELETE_GLOBAL; break;
2350 case AugLoad:
2351 case AugStore:
2352 break;
2353 case Param:
2354 default:
2355 PyErr_SetString(PyExc_SystemError,
2356 "param invalid for global variable");
2357 return 0;
2359 break;
2360 case OP_NAME:
2361 switch (ctx) {
2362 case Load: op = LOAD_NAME; break;
2363 case Store: op = STORE_NAME; break;
2364 case Del: op = DELETE_NAME; break;
2365 case AugLoad:
2366 case AugStore:
2367 break;
2368 case Param:
2369 default:
2370 PyErr_SetString(PyExc_SystemError,
2371 "param invalid for name variable");
2372 return 0;
2374 break;
2377 assert(op);
2378 arg = compiler_add_o(c, dict, mangled);
2379 Py_DECREF(mangled);
2380 if (arg < 0)
2381 return 0;
2382 return compiler_addop_i(c, op, arg);
2385 static int
2386 compiler_boolop(struct compiler *c, expr_ty e)
2388 basicblock *end;
2389 int jumpi, i, n;
2390 asdl_seq *s;
2392 assert(e->kind == BoolOp_kind);
2393 if (e->v.BoolOp.op == And)
2394 jumpi = JUMP_IF_FALSE;
2395 else
2396 jumpi = JUMP_IF_TRUE;
2397 end = compiler_new_block(c);
2398 if (end == NULL)
2399 return 0;
2400 s = e->v.BoolOp.values;
2401 n = asdl_seq_LEN(s) - 1;
2402 assert(n >= 0);
2403 for (i = 0; i < n; ++i) {
2404 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2405 ADDOP_JREL(c, jumpi, end);
2406 ADDOP(c, POP_TOP)
2408 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2409 compiler_use_next_block(c, end);
2410 return 1;
2413 static int
2414 compiler_list(struct compiler *c, expr_ty e)
2416 int n = asdl_seq_LEN(e->v.List.elts);
2417 if (e->v.List.ctx == Store) {
2418 ADDOP_I(c, UNPACK_SEQUENCE, n);
2420 VISIT_SEQ(c, expr, e->v.List.elts);
2421 if (e->v.List.ctx == Load) {
2422 ADDOP_I(c, BUILD_LIST, n);
2424 return 1;
2427 static int
2428 compiler_tuple(struct compiler *c, expr_ty e)
2430 int n = asdl_seq_LEN(e->v.Tuple.elts);
2431 if (e->v.Tuple.ctx == Store) {
2432 ADDOP_I(c, UNPACK_SEQUENCE, n);
2434 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2435 if (e->v.Tuple.ctx == Load) {
2436 ADDOP_I(c, BUILD_TUPLE, n);
2438 return 1;
2441 static int
2442 compiler_compare(struct compiler *c, expr_ty e)
2444 int i, n;
2445 basicblock *cleanup = NULL;
2447 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2448 VISIT(c, expr, e->v.Compare.left);
2449 n = asdl_seq_LEN(e->v.Compare.ops);
2450 assert(n > 0);
2451 if (n > 1) {
2452 cleanup = compiler_new_block(c);
2453 if (cleanup == NULL)
2454 return 0;
2455 VISIT(c, expr,
2456 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2458 for (i = 1; i < n; i++) {
2459 ADDOP(c, DUP_TOP);
2460 ADDOP(c, ROT_THREE);
2461 ADDOP_I(c, COMPARE_OP,
2462 cmpop((cmpop_ty)(asdl_seq_GET(
2463 e->v.Compare.ops, i - 1))));
2464 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2465 NEXT_BLOCK(c);
2466 ADDOP(c, POP_TOP);
2467 if (i < (n - 1))
2468 VISIT(c, expr,
2469 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2471 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2472 ADDOP_I(c, COMPARE_OP,
2473 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2474 if (n > 1) {
2475 basicblock *end = compiler_new_block(c);
2476 if (end == NULL)
2477 return 0;
2478 ADDOP_JREL(c, JUMP_FORWARD, end);
2479 compiler_use_next_block(c, cleanup);
2480 ADDOP(c, ROT_TWO);
2481 ADDOP(c, POP_TOP);
2482 compiler_use_next_block(c, end);
2484 return 1;
2487 static int
2488 compiler_call(struct compiler *c, expr_ty e)
2490 int n, code = 0;
2492 VISIT(c, expr, e->v.Call.func);
2493 n = asdl_seq_LEN(e->v.Call.args);
2494 VISIT_SEQ(c, expr, e->v.Call.args);
2495 if (e->v.Call.keywords) {
2496 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2497 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2499 if (e->v.Call.starargs) {
2500 VISIT(c, expr, e->v.Call.starargs);
2501 code |= 1;
2503 if (e->v.Call.kwargs) {
2504 VISIT(c, expr, e->v.Call.kwargs);
2505 code |= 2;
2507 switch (code) {
2508 case 0:
2509 ADDOP_I(c, CALL_FUNCTION, n);
2510 break;
2511 case 1:
2512 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2513 break;
2514 case 2:
2515 ADDOP_I(c, CALL_FUNCTION_KW, n);
2516 break;
2517 case 3:
2518 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2519 break;
2521 return 1;
2524 static int
2525 compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
2526 asdl_seq *generators, int gen_index,
2527 expr_ty elt)
2529 /* generate code for the iterator, then each of the ifs,
2530 and then write to the element */
2532 comprehension_ty l;
2533 basicblock *start, *anchor, *skip, *if_cleanup;
2534 int i, n;
2536 start = compiler_new_block(c);
2537 skip = compiler_new_block(c);
2538 if_cleanup = compiler_new_block(c);
2539 anchor = compiler_new_block(c);
2541 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2542 anchor == NULL)
2543 return 0;
2545 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2546 VISIT(c, expr, l->iter);
2547 ADDOP(c, GET_ITER);
2548 compiler_use_next_block(c, start);
2549 ADDOP_JREL(c, FOR_ITER, anchor);
2550 NEXT_BLOCK(c);
2551 VISIT(c, expr, l->target);
2553 /* XXX this needs to be cleaned up...a lot! */
2554 n = asdl_seq_LEN(l->ifs);
2555 for (i = 0; i < n; i++) {
2556 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
2557 VISIT(c, expr, e);
2558 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2559 NEXT_BLOCK(c);
2560 ADDOP(c, POP_TOP);
2563 if (++gen_index < asdl_seq_LEN(generators))
2564 if (!compiler_listcomp_generator(c, tmpname,
2565 generators, gen_index, elt))
2566 return 0;
2568 /* only append after the last for generator */
2569 if (gen_index >= asdl_seq_LEN(generators)) {
2570 if (!compiler_nameop(c, tmpname, Load))
2571 return 0;
2572 VISIT(c, expr, elt);
2573 ADDOP(c, LIST_APPEND);
2575 compiler_use_next_block(c, skip);
2577 for (i = 0; i < n; i++) {
2578 ADDOP_I(c, JUMP_FORWARD, 1);
2579 if (i == 0)
2580 compiler_use_next_block(c, if_cleanup);
2581 ADDOP(c, POP_TOP);
2583 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2584 compiler_use_next_block(c, anchor);
2585 /* delete the temporary list name added to locals */
2586 if (gen_index == 1)
2587 if (!compiler_nameop(c, tmpname, Del))
2588 return 0;
2590 return 1;
2593 static int
2594 compiler_listcomp(struct compiler *c, expr_ty e)
2596 identifier tmp;
2597 int rc = 0;
2598 asdl_seq *generators = e->v.ListComp.generators;
2600 assert(e->kind == ListComp_kind);
2601 tmp = compiler_new_tmpname(c);
2602 if (!tmp)
2603 return 0;
2604 ADDOP_I(c, BUILD_LIST, 0);
2605 ADDOP(c, DUP_TOP);
2606 if (compiler_nameop(c, tmp, Store))
2607 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2608 e->v.ListComp.elt);
2609 Py_DECREF(tmp);
2610 return rc;
2613 static int
2614 compiler_genexp_generator(struct compiler *c,
2615 asdl_seq *generators, int gen_index,
2616 expr_ty elt)
2618 /* generate code for the iterator, then each of the ifs,
2619 and then write to the element */
2621 comprehension_ty ge;
2622 basicblock *start, *anchor, *skip, *if_cleanup, *end;
2623 int i, n;
2625 start = compiler_new_block(c);
2626 skip = compiler_new_block(c);
2627 if_cleanup = compiler_new_block(c);
2628 anchor = compiler_new_block(c);
2629 end = compiler_new_block(c);
2631 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2632 anchor == NULL || end == NULL)
2633 return 0;
2635 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2636 ADDOP_JREL(c, SETUP_LOOP, end);
2637 if (!compiler_push_fblock(c, LOOP, start))
2638 return 0;
2640 if (gen_index == 0) {
2641 /* Receive outermost iter as an implicit argument */
2642 c->u->u_argcount = 1;
2643 ADDOP_I(c, LOAD_FAST, 0);
2645 else {
2646 /* Sub-iter - calculate on the fly */
2647 VISIT(c, expr, ge->iter);
2648 ADDOP(c, GET_ITER);
2650 compiler_use_next_block(c, start);
2651 ADDOP_JREL(c, FOR_ITER, anchor);
2652 NEXT_BLOCK(c);
2653 VISIT(c, expr, ge->target);
2655 /* XXX this needs to be cleaned up...a lot! */
2656 n = asdl_seq_LEN(ge->ifs);
2657 for (i = 0; i < n; i++) {
2658 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
2659 VISIT(c, expr, e);
2660 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2661 NEXT_BLOCK(c);
2662 ADDOP(c, POP_TOP);
2665 if (++gen_index < asdl_seq_LEN(generators))
2666 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2667 return 0;
2669 /* only append after the last 'for' generator */
2670 if (gen_index >= asdl_seq_LEN(generators)) {
2671 VISIT(c, expr, elt);
2672 ADDOP(c, YIELD_VALUE);
2673 ADDOP(c, POP_TOP);
2675 compiler_use_next_block(c, skip);
2677 for (i = 0; i < n; i++) {
2678 ADDOP_I(c, JUMP_FORWARD, 1);
2679 if (i == 0)
2680 compiler_use_next_block(c, if_cleanup);
2682 ADDOP(c, POP_TOP);
2684 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2685 compiler_use_next_block(c, anchor);
2686 ADDOP(c, POP_BLOCK);
2687 compiler_pop_fblock(c, LOOP, start);
2688 compiler_use_next_block(c, end);
2690 return 1;
2693 static int
2694 compiler_genexp(struct compiler *c, expr_ty e)
2696 static identifier name;
2697 PyCodeObject *co;
2698 expr_ty outermost_iter = ((comprehension_ty)
2699 (asdl_seq_GET(e->v.GeneratorExp.generators,
2700 0)))->iter;
2702 if (!name) {
2703 name = PyString_FromString("<genexpr>");
2704 if (!name)
2705 return 0;
2708 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2709 return 0;
2710 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2711 e->v.GeneratorExp.elt);
2712 co = assemble(c, 1);
2713 compiler_exit_scope(c);
2714 if (co == NULL)
2715 return 0;
2717 compiler_make_closure(c, co, 0);
2718 Py_DECREF(co);
2720 VISIT(c, expr, outermost_iter);
2721 ADDOP(c, GET_ITER);
2722 ADDOP_I(c, CALL_FUNCTION, 1);
2724 return 1;
2727 static int
2728 compiler_visit_keyword(struct compiler *c, keyword_ty k)
2730 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2731 VISIT(c, expr, k->value);
2732 return 1;
2735 /* Test whether expression is constant. For constants, report
2736 whether they are true or false.
2738 Return values: 1 for true, 0 for false, -1 for non-constant.
2741 static int
2742 expr_constant(expr_ty e)
2744 switch (e->kind) {
2745 case Num_kind:
2746 return PyObject_IsTrue(e->v.Num.n);
2747 case Str_kind:
2748 return PyObject_IsTrue(e->v.Str.s);
2749 case Name_kind:
2750 /* __debug__ is not assignable, so we can optimize
2751 * it away in if and while statements */
2752 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2753 "__debug__") == 0)
2754 return ! Py_OptimizeFlag;
2755 /* fall through */
2756 default:
2757 return -1;
2762 Implements the with statement from PEP 343.
2764 The semantics outlined in that PEP are as follows:
2766 with EXPR as VAR:
2767 BLOCK
2769 It is implemented roughly as:
2771 context = EXPR
2772 exit = context.__exit__ # not calling it
2773 value = context.__enter__()
2774 try:
2775 VAR = value # if VAR present in the syntax
2776 BLOCK
2777 finally:
2778 if an exception was raised:
2779 exc = copy of (exception, instance, traceback)
2780 else:
2781 exc = (None, None, None)
2782 exit(*exc)
2784 static int
2785 compiler_with(struct compiler *c, stmt_ty s)
2787 static identifier enter_attr, exit_attr;
2788 basicblock *block, *finally;
2789 identifier tmpexit, tmpvalue = NULL;
2791 assert(s->kind == With_kind);
2793 if (!enter_attr) {
2794 enter_attr = PyString_InternFromString("__enter__");
2795 if (!enter_attr)
2796 return 0;
2798 if (!exit_attr) {
2799 exit_attr = PyString_InternFromString("__exit__");
2800 if (!exit_attr)
2801 return 0;
2804 block = compiler_new_block(c);
2805 finally = compiler_new_block(c);
2806 if (!block || !finally)
2807 return 0;
2809 /* Create a temporary variable to hold context.__exit__ */
2810 tmpexit = compiler_new_tmpname(c);
2811 if (tmpexit == NULL)
2812 return 0;
2813 PyArena_AddPyObject(c->c_arena, tmpexit);
2815 if (s->v.With.optional_vars) {
2816 /* Create a temporary variable to hold context.__enter__().
2817 We need to do this rather than preserving it on the stack
2818 because SETUP_FINALLY remembers the stack level.
2819 We need to do the assignment *inside* the try/finally
2820 so that context.__exit__() is called when the assignment
2821 fails. But we need to call context.__enter__() *before*
2822 the try/finally so that if it fails we won't call
2823 context.__exit__().
2825 tmpvalue = compiler_new_tmpname(c);
2826 if (tmpvalue == NULL)
2827 return 0;
2828 PyArena_AddPyObject(c->c_arena, tmpvalue);
2831 /* Evaluate EXPR */
2832 VISIT(c, expr, s->v.With.context_expr);
2834 /* Squirrel away context.__exit__ */
2835 ADDOP(c, DUP_TOP);
2836 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
2837 if (!compiler_nameop(c, tmpexit, Store))
2838 return 0;
2840 /* Call context.__enter__() */
2841 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2842 ADDOP_I(c, CALL_FUNCTION, 0);
2844 if (s->v.With.optional_vars) {
2845 /* Store it in tmpvalue */
2846 if (!compiler_nameop(c, tmpvalue, Store))
2847 return 0;
2849 else {
2850 /* Discard result from context.__enter__() */
2851 ADDOP(c, POP_TOP);
2854 /* Start the try block */
2855 ADDOP_JREL(c, SETUP_FINALLY, finally);
2857 compiler_use_next_block(c, block);
2858 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
2859 return 0;
2862 if (s->v.With.optional_vars) {
2863 /* Bind saved result of context.__enter__() to VAR */
2864 if (!compiler_nameop(c, tmpvalue, Load) ||
2865 !compiler_nameop(c, tmpvalue, Del))
2866 return 0;
2867 VISIT(c, expr, s->v.With.optional_vars);
2870 /* BLOCK code */
2871 VISIT_SEQ(c, stmt, s->v.With.body);
2873 /* End of try block; start the finally block */
2874 ADDOP(c, POP_BLOCK);
2875 compiler_pop_fblock(c, FINALLY_TRY, block);
2877 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2878 compiler_use_next_block(c, finally);
2879 if (!compiler_push_fblock(c, FINALLY_END, finally))
2880 return 0;
2882 /* Finally block starts; push tmpexit and issue our magic opcode. */
2883 if (!compiler_nameop(c, tmpexit, Load) ||
2884 !compiler_nameop(c, tmpexit, Del))
2885 return 0;
2886 ADDOP(c, WITH_CLEANUP);
2888 /* Finally block ends. */
2889 ADDOP(c, END_FINALLY);
2890 compiler_pop_fblock(c, FINALLY_END, finally);
2891 return 1;
2894 static int
2895 compiler_visit_expr(struct compiler *c, expr_ty e)
2897 int i, n;
2899 /* If expr e has a different line number than the last expr/stmt,
2900 set a new line number for the next instruction.
2902 if (e->lineno > c->u->u_lineno) {
2903 c->u->u_lineno = e->lineno;
2904 c->u->u_lineno_set = false;
2906 switch (e->kind) {
2907 case BoolOp_kind:
2908 return compiler_boolop(c, e);
2909 case BinOp_kind:
2910 VISIT(c, expr, e->v.BinOp.left);
2911 VISIT(c, expr, e->v.BinOp.right);
2912 ADDOP(c, binop(c, e->v.BinOp.op));
2913 break;
2914 case UnaryOp_kind:
2915 VISIT(c, expr, e->v.UnaryOp.operand);
2916 ADDOP(c, unaryop(e->v.UnaryOp.op));
2917 break;
2918 case Lambda_kind:
2919 return compiler_lambda(c, e);
2920 case IfExp_kind:
2921 return compiler_ifexp(c, e);
2922 case Dict_kind:
2923 /* XXX get rid of arg? */
2924 ADDOP_I(c, BUILD_MAP, 0);
2925 n = asdl_seq_LEN(e->v.Dict.values);
2926 /* We must arrange things just right for STORE_SUBSCR.
2927 It wants the stack to look like (value) (dict) (key) */
2928 for (i = 0; i < n; i++) {
2929 ADDOP(c, DUP_TOP);
2930 VISIT(c, expr,
2931 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
2932 ADDOP(c, ROT_TWO);
2933 VISIT(c, expr,
2934 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
2935 ADDOP(c, STORE_SUBSCR);
2937 break;
2938 case ListComp_kind:
2939 return compiler_listcomp(c, e);
2940 case GeneratorExp_kind:
2941 return compiler_genexp(c, e);
2942 case Yield_kind:
2943 if (c->u->u_ste->ste_type != FunctionBlock)
2944 return compiler_error(c, "'yield' outside function");
2945 if (e->v.Yield.value) {
2946 VISIT(c, expr, e->v.Yield.value);
2948 else {
2949 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2951 ADDOP(c, YIELD_VALUE);
2952 break;
2953 case Compare_kind:
2954 return compiler_compare(c, e);
2955 case Call_kind:
2956 return compiler_call(c, e);
2957 case Repr_kind:
2958 VISIT(c, expr, e->v.Repr.value);
2959 ADDOP(c, UNARY_CONVERT);
2960 break;
2961 case Num_kind:
2962 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
2963 break;
2964 case Str_kind:
2965 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
2966 break;
2967 /* The following exprs can be assignment targets. */
2968 case Attribute_kind:
2969 if (e->v.Attribute.ctx != AugStore)
2970 VISIT(c, expr, e->v.Attribute.value);
2971 switch (e->v.Attribute.ctx) {
2972 case AugLoad:
2973 ADDOP(c, DUP_TOP);
2974 /* Fall through to load */
2975 case Load:
2976 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
2977 break;
2978 case AugStore:
2979 ADDOP(c, ROT_TWO);
2980 /* Fall through to save */
2981 case Store:
2982 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
2983 break;
2984 case Del:
2985 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
2986 break;
2987 case Param:
2988 default:
2989 PyErr_SetString(PyExc_SystemError,
2990 "param invalid in attribute expression");
2991 return 0;
2993 break;
2994 case Subscript_kind:
2995 switch (e->v.Subscript.ctx) {
2996 case AugLoad:
2997 VISIT(c, expr, e->v.Subscript.value);
2998 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
2999 break;
3000 case Load:
3001 VISIT(c, expr, e->v.Subscript.value);
3002 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3003 break;
3004 case AugStore:
3005 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3006 break;
3007 case Store:
3008 VISIT(c, expr, e->v.Subscript.value);
3009 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3010 break;
3011 case Del:
3012 VISIT(c, expr, e->v.Subscript.value);
3013 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3014 break;
3015 case Param:
3016 default:
3017 PyErr_SetString(PyExc_SystemError,
3018 "param invalid in subscript expression");
3019 return 0;
3021 break;
3022 case Name_kind:
3023 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3024 /* child nodes of List and Tuple will have expr_context set */
3025 case List_kind:
3026 return compiler_list(c, e);
3027 case Tuple_kind:
3028 return compiler_tuple(c, e);
3030 return 1;
3033 static int
3034 compiler_augassign(struct compiler *c, stmt_ty s)
3036 expr_ty e = s->v.AugAssign.target;
3037 expr_ty auge;
3039 assert(s->kind == AugAssign_kind);
3041 switch (e->kind) {
3042 case Attribute_kind:
3043 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3044 AugLoad, e->lineno, e->col_offset, c->c_arena);
3045 if (auge == NULL)
3046 return 0;
3047 VISIT(c, expr, auge);
3048 VISIT(c, expr, s->v.AugAssign.value);
3049 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3050 auge->v.Attribute.ctx = AugStore;
3051 VISIT(c, expr, auge);
3052 break;
3053 case Subscript_kind:
3054 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3055 AugLoad, e->lineno, e->col_offset, c->c_arena);
3056 if (auge == NULL)
3057 return 0;
3058 VISIT(c, expr, auge);
3059 VISIT(c, expr, s->v.AugAssign.value);
3060 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3061 auge->v.Subscript.ctx = AugStore;
3062 VISIT(c, expr, auge);
3063 break;
3064 case Name_kind:
3065 if (!compiler_nameop(c, e->v.Name.id, Load))
3066 return 0;
3067 VISIT(c, expr, s->v.AugAssign.value);
3068 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3069 return compiler_nameop(c, e->v.Name.id, Store);
3070 default:
3071 PyErr_Format(PyExc_SystemError,
3072 "invalid node type (%d) for augmented assignment",
3073 e->kind);
3074 return 0;
3076 return 1;
3079 static int
3080 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3082 struct fblockinfo *f;
3083 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3084 PyErr_SetString(PyExc_SystemError,
3085 "too many statically nested blocks");
3086 return 0;
3088 f = &c->u->u_fblock[c->u->u_nfblocks++];
3089 f->fb_type = t;
3090 f->fb_block = b;
3091 return 1;
3094 static void
3095 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3097 struct compiler_unit *u = c->u;
3098 assert(u->u_nfblocks > 0);
3099 u->u_nfblocks--;
3100 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3101 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3104 static int
3105 compiler_in_loop(struct compiler *c) {
3106 int i;
3107 struct compiler_unit *u = c->u;
3108 for (i = 0; i < u->u_nfblocks; ++i) {
3109 if (u->u_fblock[i].fb_type == LOOP)
3110 return 1;
3112 return 0;
3114 /* Raises a SyntaxError and returns 0.
3115 If something goes wrong, a different exception may be raised.
3118 static int
3119 compiler_error(struct compiler *c, const char *errstr)
3121 PyObject *loc;
3122 PyObject *u = NULL, *v = NULL;
3124 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3125 if (!loc) {
3126 Py_INCREF(Py_None);
3127 loc = Py_None;
3129 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3130 Py_None, loc);
3131 if (!u)
3132 goto exit;
3133 v = Py_BuildValue("(zO)", errstr, u);
3134 if (!v)
3135 goto exit;
3136 PyErr_SetObject(PyExc_SyntaxError, v);
3137 exit:
3138 Py_DECREF(loc);
3139 Py_XDECREF(u);
3140 Py_XDECREF(v);
3141 return 0;
3144 static int
3145 compiler_handle_subscr(struct compiler *c, const char *kind,
3146 expr_context_ty ctx)
3148 int op = 0;
3150 /* XXX this code is duplicated */
3151 switch (ctx) {
3152 case AugLoad: /* fall through to Load */
3153 case Load: op = BINARY_SUBSCR; break;
3154 case AugStore:/* fall through to Store */
3155 case Store: op = STORE_SUBSCR; break;
3156 case Del: op = DELETE_SUBSCR; break;
3157 case Param:
3158 PyErr_Format(PyExc_SystemError,
3159 "invalid %s kind %d in subscript\n",
3160 kind, ctx);
3161 return 0;
3163 if (ctx == AugLoad) {
3164 ADDOP_I(c, DUP_TOPX, 2);
3166 else if (ctx == AugStore) {
3167 ADDOP(c, ROT_THREE);
3169 ADDOP(c, op);
3170 return 1;
3173 static int
3174 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3176 int n = 2;
3177 assert(s->kind == Slice_kind);
3179 /* only handles the cases where BUILD_SLICE is emitted */
3180 if (s->v.Slice.lower) {
3181 VISIT(c, expr, s->v.Slice.lower);
3183 else {
3184 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3187 if (s->v.Slice.upper) {
3188 VISIT(c, expr, s->v.Slice.upper);
3190 else {
3191 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3194 if (s->v.Slice.step) {
3195 n++;
3196 VISIT(c, expr, s->v.Slice.step);
3198 ADDOP_I(c, BUILD_SLICE, n);
3199 return 1;
3202 static int
3203 compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3205 int op = 0, slice_offset = 0, stack_count = 0;
3207 assert(s->v.Slice.step == NULL);
3208 if (s->v.Slice.lower) {
3209 slice_offset++;
3210 stack_count++;
3211 if (ctx != AugStore)
3212 VISIT(c, expr, s->v.Slice.lower);
3214 if (s->v.Slice.upper) {
3215 slice_offset += 2;
3216 stack_count++;
3217 if (ctx != AugStore)
3218 VISIT(c, expr, s->v.Slice.upper);
3221 if (ctx == AugLoad) {
3222 switch (stack_count) {
3223 case 0: ADDOP(c, DUP_TOP); break;
3224 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3225 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3228 else if (ctx == AugStore) {
3229 switch (stack_count) {
3230 case 0: ADDOP(c, ROT_TWO); break;
3231 case 1: ADDOP(c, ROT_THREE); break;
3232 case 2: ADDOP(c, ROT_FOUR); break;
3236 switch (ctx) {
3237 case AugLoad: /* fall through to Load */
3238 case Load: op = SLICE; break;
3239 case AugStore:/* fall through to Store */
3240 case Store: op = STORE_SLICE; break;
3241 case Del: op = DELETE_SLICE; break;
3242 case Param:
3243 default:
3244 PyErr_SetString(PyExc_SystemError,
3245 "param invalid in simple slice");
3246 return 0;
3249 ADDOP(c, op + slice_offset);
3250 return 1;
3253 static int
3254 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3255 expr_context_ty ctx)
3257 switch (s->kind) {
3258 case Ellipsis_kind:
3259 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3260 break;
3261 case Slice_kind:
3262 return compiler_slice(c, s, ctx);
3263 case Index_kind:
3264 VISIT(c, expr, s->v.Index.value);
3265 break;
3266 case ExtSlice_kind:
3267 default:
3268 PyErr_SetString(PyExc_SystemError,
3269 "extended slice invalid in nested slice");
3270 return 0;
3272 return 1;
3275 static int
3276 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3278 char * kindname = NULL;
3279 switch (s->kind) {
3280 case Index_kind:
3281 kindname = "index";
3282 if (ctx != AugStore) {
3283 VISIT(c, expr, s->v.Index.value);
3285 break;
3286 case Ellipsis_kind:
3287 kindname = "ellipsis";
3288 if (ctx != AugStore) {
3289 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3291 break;
3292 case Slice_kind:
3293 kindname = "slice";
3294 if (!s->v.Slice.step)
3295 return compiler_simple_slice(c, s, ctx);
3296 if (ctx != AugStore) {
3297 if (!compiler_slice(c, s, ctx))
3298 return 0;
3300 break;
3301 case ExtSlice_kind:
3302 kindname = "extended slice";
3303 if (ctx != AugStore) {
3304 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3305 for (i = 0; i < n; i++) {
3306 slice_ty sub = (slice_ty)asdl_seq_GET(
3307 s->v.ExtSlice.dims, i);
3308 if (!compiler_visit_nested_slice(c, sub, ctx))
3309 return 0;
3311 ADDOP_I(c, BUILD_TUPLE, n);
3313 break;
3314 default:
3315 PyErr_Format(PyExc_SystemError,
3316 "invalid subscript kind %d", s->kind);
3317 return 0;
3319 return compiler_handle_subscr(c, kindname, ctx);
3323 /* End of the compiler section, beginning of the assembler section */
3325 /* do depth-first search of basic block graph, starting with block.
3326 post records the block indices in post-order.
3328 XXX must handle implicit jumps from one block to next
3331 struct assembler {
3332 PyObject *a_bytecode; /* string containing bytecode */
3333 int a_offset; /* offset into bytecode */
3334 int a_nblocks; /* number of reachable blocks */
3335 basicblock **a_postorder; /* list of blocks in dfs postorder */
3336 PyObject *a_lnotab; /* string containing lnotab */
3337 int a_lnotab_off; /* offset into lnotab */
3338 int a_lineno; /* last lineno of emitted instruction */
3339 int a_lineno_off; /* bytecode offset of last lineno */
3342 static void
3343 dfs(struct compiler *c, basicblock *b, struct assembler *a)
3345 int i;
3346 struct instr *instr = NULL;
3348 if (b->b_seen)
3349 return;
3350 b->b_seen = 1;
3351 if (b->b_next != NULL)
3352 dfs(c, b->b_next, a);
3353 for (i = 0; i < b->b_iused; i++) {
3354 instr = &b->b_instr[i];
3355 if (instr->i_jrel || instr->i_jabs)
3356 dfs(c, instr->i_target, a);
3358 a->a_postorder[a->a_nblocks++] = b;
3361 static int
3362 stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3364 int i;
3365 struct instr *instr;
3366 if (b->b_seen || b->b_startdepth >= depth)
3367 return maxdepth;
3368 b->b_seen = 1;
3369 b->b_startdepth = depth;
3370 for (i = 0; i < b->b_iused; i++) {
3371 instr = &b->b_instr[i];
3372 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3373 if (depth > maxdepth)
3374 maxdepth = depth;
3375 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3376 if (instr->i_jrel || instr->i_jabs) {
3377 maxdepth = stackdepth_walk(c, instr->i_target,
3378 depth, maxdepth);
3379 if (instr->i_opcode == JUMP_ABSOLUTE ||
3380 instr->i_opcode == JUMP_FORWARD) {
3381 goto out; /* remaining code is dead */
3385 if (b->b_next)
3386 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3387 out:
3388 b->b_seen = 0;
3389 return maxdepth;
3392 /* Find the flow path that needs the largest stack. We assume that
3393 * cycles in the flow graph have no net effect on the stack depth.
3395 static int
3396 stackdepth(struct compiler *c)
3398 basicblock *b, *entryblock;
3399 entryblock = NULL;
3400 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3401 b->b_seen = 0;
3402 b->b_startdepth = INT_MIN;
3403 entryblock = b;
3405 if (!entryblock)
3406 return 0;
3407 return stackdepth_walk(c, entryblock, 0, 0);
3410 static int
3411 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3413 memset(a, 0, sizeof(struct assembler));
3414 a->a_lineno = firstlineno;
3415 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3416 if (!a->a_bytecode)
3417 return 0;
3418 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3419 if (!a->a_lnotab)
3420 return 0;
3421 a->a_postorder = (basicblock **)PyObject_Malloc(
3422 sizeof(basicblock *) * nblocks);
3423 if (!a->a_postorder) {
3424 PyErr_NoMemory();
3425 return 0;
3427 return 1;
3430 static void
3431 assemble_free(struct assembler *a)
3433 Py_XDECREF(a->a_bytecode);
3434 Py_XDECREF(a->a_lnotab);
3435 if (a->a_postorder)
3436 PyObject_Free(a->a_postorder);
3439 /* Return the size of a basic block in bytes. */
3441 static int
3442 instrsize(struct instr *instr)
3444 if (!instr->i_hasarg)
3445 return 1;
3446 if (instr->i_oparg > 0xffff)
3447 return 6;
3448 return 3;
3451 static int
3452 blocksize(basicblock *b)
3454 int i;
3455 int size = 0;
3457 for (i = 0; i < b->b_iused; i++)
3458 size += instrsize(&b->b_instr[i]);
3459 return size;
3462 /* All about a_lnotab.
3464 c_lnotab is an array of unsigned bytes disguised as a Python string.
3465 It is used to map bytecode offsets to source code line #s (when needed
3466 for tracebacks).
3468 The array is conceptually a list of
3469 (bytecode offset increment, line number increment)
3470 pairs. The details are important and delicate, best illustrated by example:
3472 byte code offset source code line number
3475 50 7
3476 350 307
3477 361 308
3479 The first trick is that these numbers aren't stored, only the increments
3480 from one row to the next (this doesn't really work, but it's a start):
3482 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3484 The second trick is that an unsigned byte can't hold negative values, or
3485 values larger than 255, so (a) there's a deep assumption that byte code
3486 offsets and their corresponding line #s both increase monotonically, and (b)
3487 if at least one column jumps by more than 255 from one row to the next, more
3488 than one pair is written to the table. In case #b, there's no way to know
3489 from looking at the table later how many were written. That's the delicate
3490 part. A user of c_lnotab desiring to find the source line number
3491 corresponding to a bytecode address A should do something like this
3493 lineno = addr = 0
3494 for addr_incr, line_incr in c_lnotab:
3495 addr += addr_incr
3496 if addr > A:
3497 return lineno
3498 lineno += line_incr
3500 In order for this to work, when the addr field increments by more than 255,
3501 the line # increment in each pair generated must be 0 until the remaining addr
3502 increment is < 256. So, in the example above, assemble_lnotab (it used
3503 to be called com_set_lineno) should not (as was actually done until 2.2)
3504 expand 300, 300 to 255, 255, 45, 45,
3505 but to 255, 0, 45, 255, 0, 45.
3508 static int
3509 assemble_lnotab(struct assembler *a, struct instr *i)
3511 int d_bytecode, d_lineno;
3512 int len;
3513 unsigned char *lnotab;
3515 d_bytecode = a->a_offset - a->a_lineno_off;
3516 d_lineno = i->i_lineno - a->a_lineno;
3518 assert(d_bytecode >= 0);
3519 assert(d_lineno >= 0);
3521 /* XXX(nnorwitz): is there a better way to handle this?
3522 for loops are special, we want to be able to trace them
3523 each time around, so we need to set an extra line number. */
3524 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
3525 return 1;
3527 if (d_bytecode > 255) {
3528 int j, nbytes, ncodes = d_bytecode / 255;
3529 nbytes = a->a_lnotab_off + 2 * ncodes;
3530 len = PyString_GET_SIZE(a->a_lnotab);
3531 if (nbytes >= len) {
3532 if (len * 2 < nbytes)
3533 len = nbytes;
3534 else
3535 len *= 2;
3536 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3537 return 0;
3539 lnotab = (unsigned char *)
3540 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3541 for (j = 0; j < ncodes; j++) {
3542 *lnotab++ = 255;
3543 *lnotab++ = 0;
3545 d_bytecode -= ncodes * 255;
3546 a->a_lnotab_off += ncodes * 2;
3548 assert(d_bytecode <= 255);
3549 if (d_lineno > 255) {
3550 int j, nbytes, ncodes = d_lineno / 255;
3551 nbytes = a->a_lnotab_off + 2 * ncodes;
3552 len = PyString_GET_SIZE(a->a_lnotab);
3553 if (nbytes >= len) {
3554 if (len * 2 < nbytes)
3555 len = nbytes;
3556 else
3557 len *= 2;
3558 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3559 return 0;
3561 lnotab = (unsigned char *)
3562 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3563 *lnotab++ = d_bytecode;
3564 *lnotab++ = 255;
3565 d_bytecode = 0;
3566 for (j = 1; j < ncodes; j++) {
3567 *lnotab++ = 0;
3568 *lnotab++ = 255;
3570 d_lineno -= ncodes * 255;
3571 a->a_lnotab_off += ncodes * 2;
3574 len = PyString_GET_SIZE(a->a_lnotab);
3575 if (a->a_lnotab_off + 2 >= len) {
3576 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
3577 return 0;
3579 lnotab = (unsigned char *)
3580 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3582 a->a_lnotab_off += 2;
3583 if (d_bytecode) {
3584 *lnotab++ = d_bytecode;
3585 *lnotab++ = d_lineno;
3587 else { /* First line of a block; def stmt, etc. */
3588 *lnotab++ = 0;
3589 *lnotab++ = d_lineno;
3591 a->a_lineno = i->i_lineno;
3592 a->a_lineno_off = a->a_offset;
3593 return 1;
3596 /* assemble_emit()
3597 Extend the bytecode with a new instruction.
3598 Update lnotab if necessary.
3601 static int
3602 assemble_emit(struct assembler *a, struct instr *i)
3604 int size, arg = 0, ext = 0;
3605 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
3606 char *code;
3608 size = instrsize(i);
3609 if (i->i_hasarg) {
3610 arg = i->i_oparg;
3611 ext = arg >> 16;
3613 if (i->i_lineno && !assemble_lnotab(a, i))
3614 return 0;
3615 if (a->a_offset + size >= len) {
3616 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
3617 return 0;
3619 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3620 a->a_offset += size;
3621 if (size == 6) {
3622 assert(i->i_hasarg);
3623 *code++ = (char)EXTENDED_ARG;
3624 *code++ = ext & 0xff;
3625 *code++ = ext >> 8;
3626 arg &= 0xffff;
3628 *code++ = i->i_opcode;
3629 if (i->i_hasarg) {
3630 assert(size == 3 || size == 6);
3631 *code++ = arg & 0xff;
3632 *code++ = arg >> 8;
3634 return 1;
3637 static void
3638 assemble_jump_offsets(struct assembler *a, struct compiler *c)
3640 basicblock *b;
3641 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
3642 int i;
3644 /* Compute the size of each block and fixup jump args.
3645 Replace block pointer with position in bytecode. */
3646 start:
3647 totsize = 0;
3648 for (i = a->a_nblocks - 1; i >= 0; i--) {
3649 b = a->a_postorder[i];
3650 bsize = blocksize(b);
3651 b->b_offset = totsize;
3652 totsize += bsize;
3654 extended_arg_count = 0;
3655 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3656 bsize = b->b_offset;
3657 for (i = 0; i < b->b_iused; i++) {
3658 struct instr *instr = &b->b_instr[i];
3659 /* Relative jumps are computed relative to
3660 the instruction pointer after fetching
3661 the jump instruction.
3663 bsize += instrsize(instr);
3664 if (instr->i_jabs)
3665 instr->i_oparg = instr->i_target->b_offset;
3666 else if (instr->i_jrel) {
3667 int delta = instr->i_target->b_offset - bsize;
3668 instr->i_oparg = delta;
3670 else
3671 continue;
3672 if (instr->i_oparg > 0xffff)
3673 extended_arg_count++;
3677 /* XXX: This is an awful hack that could hurt performance, but
3678 on the bright side it should work until we come up
3679 with a better solution.
3681 In the meantime, should the goto be dropped in favor
3682 of a loop?
3684 The issue is that in the first loop blocksize() is called
3685 which calls instrsize() which requires i_oparg be set
3686 appropriately. There is a bootstrap problem because
3687 i_oparg is calculated in the second loop above.
3689 So we loop until we stop seeing new EXTENDED_ARGs.
3690 The only EXTENDED_ARGs that could be popping up are
3691 ones in jump instructions. So this should converge
3692 fairly quickly.
3694 if (last_extended_arg_count != extended_arg_count) {
3695 last_extended_arg_count = extended_arg_count;
3696 goto start;
3700 static PyObject *
3701 dict_keys_inorder(PyObject *dict, int offset)
3703 PyObject *tuple, *k, *v;
3704 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
3706 tuple = PyTuple_New(size);
3707 if (tuple == NULL)
3708 return NULL;
3709 while (PyDict_Next(dict, &pos, &k, &v)) {
3710 i = PyInt_AS_LONG(v);
3711 k = PyTuple_GET_ITEM(k, 0);
3712 Py_INCREF(k);
3713 assert((i - offset) < size);
3714 assert((i - offset) >= 0);
3715 PyTuple_SET_ITEM(tuple, i - offset, k);
3717 return tuple;
3720 static int
3721 compute_code_flags(struct compiler *c)
3723 PySTEntryObject *ste = c->u->u_ste;
3724 int flags = 0, n;
3725 if (ste->ste_type != ModuleBlock)
3726 flags |= CO_NEWLOCALS;
3727 if (ste->ste_type == FunctionBlock) {
3728 if (!ste->ste_unoptimized)
3729 flags |= CO_OPTIMIZED;
3730 if (ste->ste_nested)
3731 flags |= CO_NESTED;
3732 if (ste->ste_generator)
3733 flags |= CO_GENERATOR;
3735 if (ste->ste_varargs)
3736 flags |= CO_VARARGS;
3737 if (ste->ste_varkeywords)
3738 flags |= CO_VARKEYWORDS;
3739 if (ste->ste_generator)
3740 flags |= CO_GENERATOR;
3742 /* (Only) inherit compilerflags in PyCF_MASK */
3743 flags |= (c->c_flags->cf_flags & PyCF_MASK);
3745 n = PyDict_Size(c->u->u_freevars);
3746 if (n < 0)
3747 return -1;
3748 if (n == 0) {
3749 n = PyDict_Size(c->u->u_cellvars);
3750 if (n < 0)
3751 return -1;
3752 if (n == 0) {
3753 flags |= CO_NOFREE;
3757 return flags;
3760 static PyCodeObject *
3761 makecode(struct compiler *c, struct assembler *a)
3763 PyObject *tmp;
3764 PyCodeObject *co = NULL;
3765 PyObject *consts = NULL;
3766 PyObject *names = NULL;
3767 PyObject *varnames = NULL;
3768 PyObject *filename = NULL;
3769 PyObject *name = NULL;
3770 PyObject *freevars = NULL;
3771 PyObject *cellvars = NULL;
3772 PyObject *bytecode = NULL;
3773 int nlocals, flags;
3775 tmp = dict_keys_inorder(c->u->u_consts, 0);
3776 if (!tmp)
3777 goto error;
3778 consts = PySequence_List(tmp); /* optimize_code requires a list */
3779 Py_DECREF(tmp);
3781 names = dict_keys_inorder(c->u->u_names, 0);
3782 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3783 if (!consts || !names || !varnames)
3784 goto error;
3786 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3787 if (!cellvars)
3788 goto error;
3789 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3790 if (!freevars)
3791 goto error;
3792 filename = PyString_FromString(c->c_filename);
3793 if (!filename)
3794 goto error;
3796 nlocals = PyDict_Size(c->u->u_varnames);
3797 flags = compute_code_flags(c);
3798 if (flags < 0)
3799 goto error;
3801 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3802 if (!bytecode)
3803 goto error;
3805 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3806 if (!tmp)
3807 goto error;
3808 Py_DECREF(consts);
3809 consts = tmp;
3811 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3812 bytecode, consts, names, varnames,
3813 freevars, cellvars,
3814 filename, c->u->u_name,
3815 c->u->u_firstlineno,
3816 a->a_lnotab);
3817 error:
3818 Py_XDECREF(consts);
3819 Py_XDECREF(names);
3820 Py_XDECREF(varnames);
3821 Py_XDECREF(filename);
3822 Py_XDECREF(name);
3823 Py_XDECREF(freevars);
3824 Py_XDECREF(cellvars);
3825 Py_XDECREF(bytecode);
3826 return co;
3830 /* For debugging purposes only */
3831 #if 0
3832 static void
3833 dump_instr(const struct instr *i)
3835 const char *jrel = i->i_jrel ? "jrel " : "";
3836 const char *jabs = i->i_jabs ? "jabs " : "";
3837 char arg[128];
3839 *arg = '\0';
3840 if (i->i_hasarg)
3841 sprintf(arg, "arg: %d ", i->i_oparg);
3843 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3844 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3847 static void
3848 dump_basicblock(const basicblock *b)
3850 const char *seen = b->b_seen ? "seen " : "";
3851 const char *b_return = b->b_return ? "return " : "";
3852 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3853 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3854 if (b->b_instr) {
3855 int i;
3856 for (i = 0; i < b->b_iused; i++) {
3857 fprintf(stderr, " [%02d] ", i);
3858 dump_instr(b->b_instr + i);
3862 #endif
3864 static PyCodeObject *
3865 assemble(struct compiler *c, int addNone)
3867 basicblock *b, *entryblock;
3868 struct assembler a;
3869 int i, j, nblocks;
3870 PyCodeObject *co = NULL;
3872 /* Make sure every block that falls off the end returns None.
3873 XXX NEXT_BLOCK() isn't quite right, because if the last
3874 block ends with a jump or return b_next shouldn't set.
3876 if (!c->u->u_curblock->b_return) {
3877 NEXT_BLOCK(c);
3878 if (addNone)
3879 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3880 ADDOP(c, RETURN_VALUE);
3883 nblocks = 0;
3884 entryblock = NULL;
3885 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3886 nblocks++;
3887 entryblock = b;
3890 /* Set firstlineno if it wasn't explicitly set. */
3891 if (!c->u->u_firstlineno) {
3892 if (entryblock && entryblock->b_instr)
3893 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3894 else
3895 c->u->u_firstlineno = 1;
3897 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3898 goto error;
3899 dfs(c, entryblock, &a);
3901 /* Can't modify the bytecode after computing jump offsets. */
3902 assemble_jump_offsets(&a, c);
3904 /* Emit code in reverse postorder from dfs. */
3905 for (i = a.a_nblocks - 1; i >= 0; i--) {
3906 b = a.a_postorder[i];
3907 for (j = 0; j < b->b_iused; j++)
3908 if (!assemble_emit(&a, &b->b_instr[j]))
3909 goto error;
3912 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
3913 goto error;
3914 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
3915 goto error;
3917 co = makecode(c, &a);
3918 error:
3919 assemble_free(&a);
3920 return co;