This reverts r63675 based on the discussion in this thread:
[python.git] / Python / compile.c
blobc81218d0326cb647897ae35f657ba942c506cc8e
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((void *)block != (void *)0xcbcbcbcb);
396 assert((void *)block != (void *)0xfbfbfbfb);
397 assert((void *)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 offset 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 The line number is reset in the following cases:
646 - when entering a new scope
647 - on each statement
648 - on each expression that start a new line
649 - before the "except" clause
650 - before the "for" and "while" expressions
653 static void
654 compiler_set_lineno(struct compiler *c, int off)
656 basicblock *b;
657 if (c->u->u_lineno_set)
658 return;
659 c->u->u_lineno_set = true;
660 b = c->u->u_curblock;
661 b->b_instr[off].i_lineno = c->u->u_lineno;
664 static int
665 opcode_stack_effect(int opcode, int oparg)
667 switch (opcode) {
668 case POP_TOP:
669 return -1;
670 case ROT_TWO:
671 case ROT_THREE:
672 return 0;
673 case DUP_TOP:
674 return 1;
675 case ROT_FOUR:
676 return 0;
678 case UNARY_POSITIVE:
679 case UNARY_NEGATIVE:
680 case UNARY_NOT:
681 case UNARY_CONVERT:
682 case UNARY_INVERT:
683 return 0;
685 case LIST_APPEND:
686 return -2;
688 case BINARY_POWER:
689 case BINARY_MULTIPLY:
690 case BINARY_DIVIDE:
691 case BINARY_MODULO:
692 case BINARY_ADD:
693 case BINARY_SUBTRACT:
694 case BINARY_SUBSCR:
695 case BINARY_FLOOR_DIVIDE:
696 case BINARY_TRUE_DIVIDE:
697 return -1;
698 case INPLACE_FLOOR_DIVIDE:
699 case INPLACE_TRUE_DIVIDE:
700 return -1;
702 case SLICE+0:
703 return 1;
704 case SLICE+1:
705 return 0;
706 case SLICE+2:
707 return 0;
708 case SLICE+3:
709 return -1;
711 case STORE_SLICE+0:
712 return -2;
713 case STORE_SLICE+1:
714 return -3;
715 case STORE_SLICE+2:
716 return -3;
717 case STORE_SLICE+3:
718 return -4;
720 case DELETE_SLICE+0:
721 return -1;
722 case DELETE_SLICE+1:
723 return -2;
724 case DELETE_SLICE+2:
725 return -2;
726 case DELETE_SLICE+3:
727 return -3;
729 case INPLACE_ADD:
730 case INPLACE_SUBTRACT:
731 case INPLACE_MULTIPLY:
732 case INPLACE_DIVIDE:
733 case INPLACE_MODULO:
734 return -1;
735 case STORE_SUBSCR:
736 return -3;
737 case STORE_MAP:
738 return -2;
739 case DELETE_SUBSCR:
740 return -2;
742 case BINARY_LSHIFT:
743 case BINARY_RSHIFT:
744 case BINARY_AND:
745 case BINARY_XOR:
746 case BINARY_OR:
747 return -1;
748 case INPLACE_POWER:
749 return -1;
750 case GET_ITER:
751 return 0;
753 case PRINT_EXPR:
754 return -1;
755 case PRINT_ITEM:
756 return -1;
757 case PRINT_NEWLINE:
758 return 0;
759 case PRINT_ITEM_TO:
760 return -2;
761 case PRINT_NEWLINE_TO:
762 return -1;
763 case INPLACE_LSHIFT:
764 case INPLACE_RSHIFT:
765 case INPLACE_AND:
766 case INPLACE_XOR:
767 case INPLACE_OR:
768 return -1;
769 case BREAK_LOOP:
770 return 0;
771 case WITH_CLEANUP:
772 return -1; /* XXX Sometimes more */
773 case LOAD_LOCALS:
774 return 1;
775 case RETURN_VALUE:
776 return -1;
777 case IMPORT_STAR:
778 return -1;
779 case EXEC_STMT:
780 return -3;
781 case YIELD_VALUE:
782 return 0;
784 case POP_BLOCK:
785 return 0;
786 case END_FINALLY:
787 return -1; /* or -2 or -3 if exception occurred */
788 case BUILD_CLASS:
789 return -2;
791 case STORE_NAME:
792 return -1;
793 case DELETE_NAME:
794 return 0;
795 case UNPACK_SEQUENCE:
796 return oparg-1;
797 case FOR_ITER:
798 return 1;
800 case STORE_ATTR:
801 return -2;
802 case DELETE_ATTR:
803 return -1;
804 case STORE_GLOBAL:
805 return -1;
806 case DELETE_GLOBAL:
807 return 0;
808 case DUP_TOPX:
809 return oparg;
810 case LOAD_CONST:
811 return 1;
812 case LOAD_NAME:
813 return 1;
814 case BUILD_TUPLE:
815 case BUILD_LIST:
816 return 1-oparg;
817 case BUILD_MAP:
818 return 1;
819 case LOAD_ATTR:
820 return 0;
821 case COMPARE_OP:
822 return -1;
823 case IMPORT_NAME:
824 return 0;
825 case IMPORT_FROM:
826 return 1;
828 case JUMP_FORWARD:
829 case JUMP_IF_FALSE:
830 case JUMP_IF_TRUE:
831 case JUMP_ABSOLUTE:
832 return 0;
834 case LOAD_GLOBAL:
835 return 1;
837 case CONTINUE_LOOP:
838 return 0;
839 case SETUP_LOOP:
840 return 0;
841 case SETUP_EXCEPT:
842 case SETUP_FINALLY:
843 return 3; /* actually pushed by an exception */
845 case LOAD_FAST:
846 return 1;
847 case STORE_FAST:
848 return -1;
849 case DELETE_FAST:
850 return 0;
852 case RAISE_VARARGS:
853 return -oparg;
854 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
855 case CALL_FUNCTION:
856 return -NARGS(oparg);
857 case CALL_FUNCTION_VAR:
858 case CALL_FUNCTION_KW:
859 return -NARGS(oparg)-1;
860 case CALL_FUNCTION_VAR_KW:
861 return -NARGS(oparg)-2;
862 #undef NARGS
863 case MAKE_FUNCTION:
864 return -oparg;
865 case BUILD_SLICE:
866 if (oparg == 3)
867 return -2;
868 else
869 return -1;
871 case MAKE_CLOSURE:
872 return -oparg;
873 case LOAD_CLOSURE:
874 return 1;
875 case LOAD_DEREF:
876 return 1;
877 case STORE_DEREF:
878 return -1;
879 default:
880 fprintf(stderr, "opcode = %d\n", opcode);
881 Py_FatalError("opcode_stack_effect()");
884 return 0; /* not reachable */
887 /* Add an opcode with no argument.
888 Returns 0 on failure, 1 on success.
891 static int
892 compiler_addop(struct compiler *c, int opcode)
894 basicblock *b;
895 struct instr *i;
896 int off;
897 off = compiler_next_instr(c, c->u->u_curblock);
898 if (off < 0)
899 return 0;
900 b = c->u->u_curblock;
901 i = &b->b_instr[off];
902 i->i_opcode = opcode;
903 i->i_hasarg = 0;
904 if (opcode == RETURN_VALUE)
905 b->b_return = 1;
906 compiler_set_lineno(c, off);
907 return 1;
910 static int
911 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
913 PyObject *t, *v;
914 Py_ssize_t arg;
915 unsigned char *p, *q;
916 Py_complex z;
917 double d;
918 int real_part_zero, imag_part_zero;
920 /* necessary to make sure types aren't coerced (e.g., int and long) */
921 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
922 if (PyFloat_Check(o)) {
923 d = PyFloat_AS_DOUBLE(o);
924 p = (unsigned char*) &d;
925 /* all we need is to make the tuple different in either the 0.0
926 * or -0.0 case from all others, just to avoid the "coercion".
928 if (*p==0 && p[sizeof(double)-1]==0)
929 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
930 else
931 t = PyTuple_Pack(2, o, o->ob_type);
933 else if (PyComplex_Check(o)) {
934 /* complex case is even messier: we need to make complex(x,
935 0.) different from complex(x, -0.) and complex(0., y)
936 different from complex(-0., y), for any x and y. In
937 particular, all four complex zeros should be
938 distinguished.*/
939 z = PyComplex_AsCComplex(o);
940 p = (unsigned char*) &(z.real);
941 q = (unsigned char*) &(z.imag);
942 /* all that matters here is that on IEEE platforms
943 real_part_zero will be true if z.real == 0., and false if
944 z.real == -0. In fact, real_part_zero will also be true
945 for some other rarely occurring nonzero floats, but this
946 doesn't matter. Similar comments apply to
947 imag_part_zero. */
948 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
949 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
950 if (real_part_zero && imag_part_zero) {
951 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
953 else if (real_part_zero && !imag_part_zero) {
954 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
956 else if (!real_part_zero && imag_part_zero) {
957 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
959 else {
960 t = PyTuple_Pack(2, o, o->ob_type);
963 else {
964 t = PyTuple_Pack(2, o, o->ob_type);
966 if (t == NULL)
967 return -1;
969 v = PyDict_GetItem(dict, t);
970 if (!v) {
971 arg = PyDict_Size(dict);
972 v = PyInt_FromLong(arg);
973 if (!v) {
974 Py_DECREF(t);
975 return -1;
977 if (PyDict_SetItem(dict, t, v) < 0) {
978 Py_DECREF(t);
979 Py_DECREF(v);
980 return -1;
982 Py_DECREF(v);
984 else
985 arg = PyInt_AsLong(v);
986 Py_DECREF(t);
987 return arg;
990 static int
991 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
992 PyObject *o)
994 int arg = compiler_add_o(c, dict, o);
995 if (arg < 0)
996 return 0;
997 return compiler_addop_i(c, opcode, arg);
1000 static int
1001 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1002 PyObject *o)
1004 int arg;
1005 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1006 if (!mangled)
1007 return 0;
1008 arg = compiler_add_o(c, dict, mangled);
1009 Py_DECREF(mangled);
1010 if (arg < 0)
1011 return 0;
1012 return compiler_addop_i(c, opcode, arg);
1015 /* Add an opcode with an integer argument.
1016 Returns 0 on failure, 1 on success.
1019 static int
1020 compiler_addop_i(struct compiler *c, int opcode, int oparg)
1022 struct instr *i;
1023 int off;
1024 off = compiler_next_instr(c, c->u->u_curblock);
1025 if (off < 0)
1026 return 0;
1027 i = &c->u->u_curblock->b_instr[off];
1028 i->i_opcode = opcode;
1029 i->i_oparg = oparg;
1030 i->i_hasarg = 1;
1031 compiler_set_lineno(c, off);
1032 return 1;
1035 static int
1036 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1038 struct instr *i;
1039 int off;
1041 assert(b != NULL);
1042 off = compiler_next_instr(c, c->u->u_curblock);
1043 if (off < 0)
1044 return 0;
1045 i = &c->u->u_curblock->b_instr[off];
1046 i->i_opcode = opcode;
1047 i->i_target = b;
1048 i->i_hasarg = 1;
1049 if (absolute)
1050 i->i_jabs = 1;
1051 else
1052 i->i_jrel = 1;
1053 compiler_set_lineno(c, off);
1054 return 1;
1057 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1058 like to find better names.) NEW_BLOCK() creates a new block and sets
1059 it as the current block. NEXT_BLOCK() also creates an implicit jump
1060 from the current block to the new block.
1063 /* The returns inside these macros make it impossible to decref objects
1064 created in the local function. Local objects should use the arena.
1068 #define NEW_BLOCK(C) { \
1069 if (compiler_use_new_block((C)) == NULL) \
1070 return 0; \
1073 #define NEXT_BLOCK(C) { \
1074 if (compiler_next_block((C)) == NULL) \
1075 return 0; \
1078 #define ADDOP(C, OP) { \
1079 if (!compiler_addop((C), (OP))) \
1080 return 0; \
1083 #define ADDOP_IN_SCOPE(C, OP) { \
1084 if (!compiler_addop((C), (OP))) { \
1085 compiler_exit_scope(c); \
1086 return 0; \
1090 #define ADDOP_O(C, OP, O, TYPE) { \
1091 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1092 return 0; \
1095 #define ADDOP_NAME(C, OP, O, TYPE) { \
1096 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1097 return 0; \
1100 #define ADDOP_I(C, OP, O) { \
1101 if (!compiler_addop_i((C), (OP), (O))) \
1102 return 0; \
1105 #define ADDOP_JABS(C, OP, O) { \
1106 if (!compiler_addop_j((C), (OP), (O), 1)) \
1107 return 0; \
1110 #define ADDOP_JREL(C, OP, O) { \
1111 if (!compiler_addop_j((C), (OP), (O), 0)) \
1112 return 0; \
1115 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1116 the ASDL name to synthesize the name of the C type and the visit function.
1119 #define VISIT(C, TYPE, V) {\
1120 if (!compiler_visit_ ## TYPE((C), (V))) \
1121 return 0; \
1124 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1125 if (!compiler_visit_ ## TYPE((C), (V))) { \
1126 compiler_exit_scope(c); \
1127 return 0; \
1131 #define VISIT_SLICE(C, V, CTX) {\
1132 if (!compiler_visit_slice((C), (V), (CTX))) \
1133 return 0; \
1136 #define VISIT_SEQ(C, TYPE, SEQ) { \
1137 int _i; \
1138 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1139 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1140 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1141 if (!compiler_visit_ ## TYPE((C), elt)) \
1142 return 0; \
1146 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1147 int _i; \
1148 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1149 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1150 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1151 if (!compiler_visit_ ## TYPE((C), elt)) { \
1152 compiler_exit_scope(c); \
1153 return 0; \
1158 static int
1159 compiler_isdocstring(stmt_ty s)
1161 if (s->kind != Expr_kind)
1162 return 0;
1163 return s->v.Expr.value->kind == Str_kind;
1166 /* Compile a sequence of statements, checking for a docstring. */
1168 static int
1169 compiler_body(struct compiler *c, asdl_seq *stmts)
1171 int i = 0;
1172 stmt_ty st;
1174 if (!asdl_seq_LEN(stmts))
1175 return 1;
1176 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1177 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1178 /* don't generate docstrings if -OO */
1179 i = 1;
1180 VISIT(c, expr, st->v.Expr.value);
1181 if (!compiler_nameop(c, __doc__, Store))
1182 return 0;
1184 for (; i < asdl_seq_LEN(stmts); i++)
1185 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1186 return 1;
1189 static PyCodeObject *
1190 compiler_mod(struct compiler *c, mod_ty mod)
1192 PyCodeObject *co;
1193 int addNone = 1;
1194 static PyObject *module;
1195 if (!module) {
1196 module = PyString_InternFromString("<module>");
1197 if (!module)
1198 return NULL;
1200 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1201 if (!compiler_enter_scope(c, module, mod, 0))
1202 return NULL;
1203 switch (mod->kind) {
1204 case Module_kind:
1205 if (!compiler_body(c, mod->v.Module.body)) {
1206 compiler_exit_scope(c);
1207 return 0;
1209 break;
1210 case Interactive_kind:
1211 c->c_interactive = 1;
1212 VISIT_SEQ_IN_SCOPE(c, stmt,
1213 mod->v.Interactive.body);
1214 break;
1215 case Expression_kind:
1216 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1217 addNone = 0;
1218 break;
1219 case Suite_kind:
1220 PyErr_SetString(PyExc_SystemError,
1221 "suite should not be possible");
1222 return 0;
1223 default:
1224 PyErr_Format(PyExc_SystemError,
1225 "module kind %d should not be possible",
1226 mod->kind);
1227 return 0;
1229 co = assemble(c, addNone);
1230 compiler_exit_scope(c);
1231 return co;
1234 /* The test for LOCAL must come before the test for FREE in order to
1235 handle classes where name is both local and free. The local var is
1236 a method and the free var is a free var referenced within a method.
1239 static int
1240 get_ref_type(struct compiler *c, PyObject *name)
1242 int scope = PyST_GetScope(c->u->u_ste, name);
1243 if (scope == 0) {
1244 char buf[350];
1245 PyOS_snprintf(buf, sizeof(buf),
1246 "unknown scope for %.100s in %.100s(%s) in %s\n"
1247 "symbols: %s\nlocals: %s\nglobals: %s\n",
1248 PyString_AS_STRING(name),
1249 PyString_AS_STRING(c->u->u_name),
1250 PyObject_REPR(c->u->u_ste->ste_id),
1251 c->c_filename,
1252 PyObject_REPR(c->u->u_ste->ste_symbols),
1253 PyObject_REPR(c->u->u_varnames),
1254 PyObject_REPR(c->u->u_names)
1256 Py_FatalError(buf);
1259 return scope;
1262 static int
1263 compiler_lookup_arg(PyObject *dict, PyObject *name)
1265 PyObject *k, *v;
1266 k = PyTuple_Pack(2, name, name->ob_type);
1267 if (k == NULL)
1268 return -1;
1269 v = PyDict_GetItem(dict, k);
1270 Py_DECREF(k);
1271 if (v == NULL)
1272 return -1;
1273 return PyInt_AS_LONG(v);
1276 static int
1277 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1279 int i, free = PyCode_GetNumFree(co);
1280 if (free == 0) {
1281 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1282 ADDOP_I(c, MAKE_FUNCTION, args);
1283 return 1;
1285 for (i = 0; i < free; ++i) {
1286 /* Bypass com_addop_varname because it will generate
1287 LOAD_DEREF but LOAD_CLOSURE is needed.
1289 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1290 int arg, reftype;
1292 /* Special case: If a class contains a method with a
1293 free variable that has the same name as a method,
1294 the name will be considered free *and* local in the
1295 class. It should be handled by the closure, as
1296 well as by the normal name loookup logic.
1298 reftype = get_ref_type(c, name);
1299 if (reftype == CELL)
1300 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1301 else /* (reftype == FREE) */
1302 arg = compiler_lookup_arg(c->u->u_freevars, name);
1303 if (arg == -1) {
1304 printf("lookup %s in %s %d %d\n"
1305 "freevars of %s: %s\n",
1306 PyObject_REPR(name),
1307 PyString_AS_STRING(c->u->u_name),
1308 reftype, arg,
1309 PyString_AS_STRING(co->co_name),
1310 PyObject_REPR(co->co_freevars));
1311 Py_FatalError("compiler_make_closure()");
1313 ADDOP_I(c, LOAD_CLOSURE, arg);
1315 ADDOP_I(c, BUILD_TUPLE, free);
1316 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1317 ADDOP_I(c, MAKE_CLOSURE, args);
1318 return 1;
1321 static int
1322 compiler_decorators(struct compiler *c, asdl_seq* decos)
1324 int i;
1326 if (!decos)
1327 return 1;
1329 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1330 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1332 return 1;
1335 static int
1336 compiler_arguments(struct compiler *c, arguments_ty args)
1338 int i;
1339 int n = asdl_seq_LEN(args->args);
1340 /* Correctly handle nested argument lists */
1341 for (i = 0; i < n; i++) {
1342 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
1343 if (arg->kind == Tuple_kind) {
1344 PyObject *id = PyString_FromFormat(".%d", i);
1345 if (id == NULL) {
1346 return 0;
1348 if (!compiler_nameop(c, id, Load)) {
1349 Py_DECREF(id);
1350 return 0;
1352 Py_DECREF(id);
1353 VISIT(c, expr, arg);
1356 return 1;
1359 static int
1360 compiler_function(struct compiler *c, stmt_ty s)
1362 PyCodeObject *co;
1363 PyObject *first_const = Py_None;
1364 arguments_ty args = s->v.FunctionDef.args;
1365 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1366 stmt_ty st;
1367 int i, n, docstring;
1369 assert(s->kind == FunctionDef_kind);
1371 if (!compiler_decorators(c, decos))
1372 return 0;
1373 if (args->defaults)
1374 VISIT_SEQ(c, expr, args->defaults);
1375 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1376 s->lineno))
1377 return 0;
1379 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1380 docstring = compiler_isdocstring(st);
1381 if (docstring && Py_OptimizeFlag < 2)
1382 first_const = st->v.Expr.value->v.Str.s;
1383 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1384 compiler_exit_scope(c);
1385 return 0;
1388 /* unpack nested arguments */
1389 compiler_arguments(c, args);
1391 c->u->u_argcount = asdl_seq_LEN(args->args);
1392 n = asdl_seq_LEN(s->v.FunctionDef.body);
1393 /* if there was a docstring, we need to skip the first statement */
1394 for (i = docstring; i < n; i++) {
1395 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1396 VISIT_IN_SCOPE(c, stmt, st);
1398 co = assemble(c, 1);
1399 compiler_exit_scope(c);
1400 if (co == NULL)
1401 return 0;
1403 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1404 Py_DECREF(co);
1406 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1407 ADDOP_I(c, CALL_FUNCTION, 1);
1410 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1413 static int
1414 compiler_class(struct compiler *c, stmt_ty s)
1416 int n, i;
1417 PyCodeObject *co;
1418 PyObject *str;
1419 asdl_seq* decos = s->v.ClassDef.decorator_list;
1421 if (!compiler_decorators(c, decos))
1422 return 0;
1424 /* push class name on stack, needed by BUILD_CLASS */
1425 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1426 /* push the tuple of base classes on the stack */
1427 n = asdl_seq_LEN(s->v.ClassDef.bases);
1428 if (n > 0)
1429 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1430 ADDOP_I(c, BUILD_TUPLE, n);
1431 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1432 s->lineno))
1433 return 0;
1434 Py_XDECREF(c->u->u_private);
1435 c->u->u_private = s->v.ClassDef.name;
1436 Py_INCREF(c->u->u_private);
1437 str = PyString_InternFromString("__name__");
1438 if (!str || !compiler_nameop(c, str, Load)) {
1439 Py_XDECREF(str);
1440 compiler_exit_scope(c);
1441 return 0;
1444 Py_DECREF(str);
1445 str = PyString_InternFromString("__module__");
1446 if (!str || !compiler_nameop(c, str, Store)) {
1447 Py_XDECREF(str);
1448 compiler_exit_scope(c);
1449 return 0;
1451 Py_DECREF(str);
1453 if (!compiler_body(c, s->v.ClassDef.body)) {
1454 compiler_exit_scope(c);
1455 return 0;
1458 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1459 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1460 co = assemble(c, 1);
1461 compiler_exit_scope(c);
1462 if (co == NULL)
1463 return 0;
1465 compiler_make_closure(c, co, 0);
1466 Py_DECREF(co);
1468 ADDOP_I(c, CALL_FUNCTION, 0);
1469 ADDOP(c, BUILD_CLASS);
1470 /* apply decorators */
1471 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1472 ADDOP_I(c, CALL_FUNCTION, 1);
1474 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1475 return 0;
1476 return 1;
1479 static int
1480 compiler_ifexp(struct compiler *c, expr_ty e)
1482 basicblock *end, *next;
1484 assert(e->kind == IfExp_kind);
1485 end = compiler_new_block(c);
1486 if (end == NULL)
1487 return 0;
1488 next = compiler_new_block(c);
1489 if (next == NULL)
1490 return 0;
1491 VISIT(c, expr, e->v.IfExp.test);
1492 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1493 ADDOP(c, POP_TOP);
1494 VISIT(c, expr, e->v.IfExp.body);
1495 ADDOP_JREL(c, JUMP_FORWARD, end);
1496 compiler_use_next_block(c, next);
1497 ADDOP(c, POP_TOP);
1498 VISIT(c, expr, e->v.IfExp.orelse);
1499 compiler_use_next_block(c, end);
1500 return 1;
1503 static int
1504 compiler_lambda(struct compiler *c, expr_ty e)
1506 PyCodeObject *co;
1507 static identifier name;
1508 arguments_ty args = e->v.Lambda.args;
1509 assert(e->kind == Lambda_kind);
1511 if (!name) {
1512 name = PyString_InternFromString("<lambda>");
1513 if (!name)
1514 return 0;
1517 if (args->defaults)
1518 VISIT_SEQ(c, expr, args->defaults);
1519 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1520 return 0;
1522 /* unpack nested arguments */
1523 compiler_arguments(c, args);
1525 c->u->u_argcount = asdl_seq_LEN(args->args);
1526 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1527 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1528 co = assemble(c, 1);
1529 compiler_exit_scope(c);
1530 if (co == NULL)
1531 return 0;
1533 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1534 Py_DECREF(co);
1536 return 1;
1539 static int
1540 compiler_print(struct compiler *c, stmt_ty s)
1542 int i, n;
1543 bool dest;
1545 assert(s->kind == Print_kind);
1546 n = asdl_seq_LEN(s->v.Print.values);
1547 dest = false;
1548 if (s->v.Print.dest) {
1549 VISIT(c, expr, s->v.Print.dest);
1550 dest = true;
1552 for (i = 0; i < n; i++) {
1553 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1554 if (dest) {
1555 ADDOP(c, DUP_TOP);
1556 VISIT(c, expr, e);
1557 ADDOP(c, ROT_TWO);
1558 ADDOP(c, PRINT_ITEM_TO);
1560 else {
1561 VISIT(c, expr, e);
1562 ADDOP(c, PRINT_ITEM);
1565 if (s->v.Print.nl) {
1566 if (dest)
1567 ADDOP(c, PRINT_NEWLINE_TO)
1568 else
1569 ADDOP(c, PRINT_NEWLINE)
1571 else if (dest)
1572 ADDOP(c, POP_TOP);
1573 return 1;
1576 static int
1577 compiler_if(struct compiler *c, stmt_ty s)
1579 basicblock *end, *next;
1580 int constant;
1581 assert(s->kind == If_kind);
1582 end = compiler_new_block(c);
1583 if (end == NULL)
1584 return 0;
1585 next = compiler_new_block(c);
1586 if (next == NULL)
1587 return 0;
1589 constant = expr_constant(s->v.If.test);
1590 /* constant = 0: "if 0"
1591 * constant = 1: "if 1", "if 2", ...
1592 * constant = -1: rest */
1593 if (constant == 0) {
1594 if (s->v.If.orelse)
1595 VISIT_SEQ(c, stmt, s->v.If.orelse);
1596 } else if (constant == 1) {
1597 VISIT_SEQ(c, stmt, s->v.If.body);
1598 } else {
1599 VISIT(c, expr, s->v.If.test);
1600 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1601 ADDOP(c, POP_TOP);
1602 VISIT_SEQ(c, stmt, s->v.If.body);
1603 ADDOP_JREL(c, JUMP_FORWARD, end);
1604 compiler_use_next_block(c, next);
1605 ADDOP(c, POP_TOP);
1606 if (s->v.If.orelse)
1607 VISIT_SEQ(c, stmt, s->v.If.orelse);
1609 compiler_use_next_block(c, end);
1610 return 1;
1613 static int
1614 compiler_for(struct compiler *c, stmt_ty s)
1616 basicblock *start, *cleanup, *end;
1618 start = compiler_new_block(c);
1619 cleanup = compiler_new_block(c);
1620 end = compiler_new_block(c);
1621 if (start == NULL || end == NULL || cleanup == NULL)
1622 return 0;
1623 ADDOP_JREL(c, SETUP_LOOP, end);
1624 if (!compiler_push_fblock(c, LOOP, start))
1625 return 0;
1626 VISIT(c, expr, s->v.For.iter);
1627 ADDOP(c, GET_ITER);
1628 compiler_use_next_block(c, start);
1629 /* for expressions must be traced on each iteration,
1630 so we need to set an extra line number. */
1631 c->u->u_lineno_set = false;
1632 ADDOP_JREL(c, FOR_ITER, cleanup);
1633 VISIT(c, expr, s->v.For.target);
1634 VISIT_SEQ(c, stmt, s->v.For.body);
1635 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1636 compiler_use_next_block(c, cleanup);
1637 ADDOP(c, POP_BLOCK);
1638 compiler_pop_fblock(c, LOOP, start);
1639 VISIT_SEQ(c, stmt, s->v.For.orelse);
1640 compiler_use_next_block(c, end);
1641 return 1;
1644 static int
1645 compiler_while(struct compiler *c, stmt_ty s)
1647 basicblock *loop, *orelse, *end, *anchor = NULL;
1648 int constant = expr_constant(s->v.While.test);
1650 if (constant == 0) {
1651 if (s->v.While.orelse)
1652 VISIT_SEQ(c, stmt, s->v.While.orelse);
1653 return 1;
1655 loop = compiler_new_block(c);
1656 end = compiler_new_block(c);
1657 if (constant == -1) {
1658 anchor = compiler_new_block(c);
1659 if (anchor == NULL)
1660 return 0;
1662 if (loop == NULL || end == NULL)
1663 return 0;
1664 if (s->v.While.orelse) {
1665 orelse = compiler_new_block(c);
1666 if (orelse == NULL)
1667 return 0;
1669 else
1670 orelse = NULL;
1672 ADDOP_JREL(c, SETUP_LOOP, end);
1673 compiler_use_next_block(c, loop);
1674 if (!compiler_push_fblock(c, LOOP, loop))
1675 return 0;
1676 if (constant == -1) {
1677 /* while expressions must be traced on each iteration,
1678 so we need to set an extra line number. */
1679 c->u->u_lineno_set = false;
1680 VISIT(c, expr, s->v.While.test);
1681 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1682 ADDOP(c, POP_TOP);
1684 VISIT_SEQ(c, stmt, s->v.While.body);
1685 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1687 /* XXX should the two POP instructions be in a separate block
1688 if there is no else clause ?
1691 if (constant == -1) {
1692 compiler_use_next_block(c, anchor);
1693 ADDOP(c, POP_TOP);
1694 ADDOP(c, POP_BLOCK);
1696 compiler_pop_fblock(c, LOOP, loop);
1697 if (orelse != NULL) /* what if orelse is just pass? */
1698 VISIT_SEQ(c, stmt, s->v.While.orelse);
1699 compiler_use_next_block(c, end);
1701 return 1;
1704 static int
1705 compiler_continue(struct compiler *c)
1707 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1708 static const char IN_FINALLY_ERROR_MSG[] =
1709 "'continue' not supported inside 'finally' clause";
1710 int i;
1712 if (!c->u->u_nfblocks)
1713 return compiler_error(c, LOOP_ERROR_MSG);
1714 i = c->u->u_nfblocks - 1;
1715 switch (c->u->u_fblock[i].fb_type) {
1716 case LOOP:
1717 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1718 break;
1719 case EXCEPT:
1720 case FINALLY_TRY:
1721 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1722 /* Prevent continue anywhere under a finally
1723 even if hidden in a sub-try or except. */
1724 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1725 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1727 if (i == -1)
1728 return compiler_error(c, LOOP_ERROR_MSG);
1729 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1730 break;
1731 case FINALLY_END:
1732 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1735 return 1;
1738 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1740 SETUP_FINALLY L
1741 <code for body>
1742 POP_BLOCK
1743 LOAD_CONST <None>
1744 L: <code for finalbody>
1745 END_FINALLY
1747 The special instructions use the block stack. Each block
1748 stack entry contains the instruction that created it (here
1749 SETUP_FINALLY), the level of the value stack at the time the
1750 block stack entry was created, and a label (here L).
1752 SETUP_FINALLY:
1753 Pushes the current value stack level and the label
1754 onto the block stack.
1755 POP_BLOCK:
1756 Pops en entry from the block stack, and pops the value
1757 stack until its level is the same as indicated on the
1758 block stack. (The label is ignored.)
1759 END_FINALLY:
1760 Pops a variable number of entries from the *value* stack
1761 and re-raises the exception they specify. The number of
1762 entries popped depends on the (pseudo) exception type.
1764 The block stack is unwound when an exception is raised:
1765 when a SETUP_FINALLY entry is found, the exception is pushed
1766 onto the value stack (and the exception condition is cleared),
1767 and the interpreter jumps to the label gotten from the block
1768 stack.
1771 static int
1772 compiler_try_finally(struct compiler *c, stmt_ty s)
1774 basicblock *body, *end;
1775 body = compiler_new_block(c);
1776 end = compiler_new_block(c);
1777 if (body == NULL || end == NULL)
1778 return 0;
1780 ADDOP_JREL(c, SETUP_FINALLY, end);
1781 compiler_use_next_block(c, body);
1782 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1783 return 0;
1784 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1785 ADDOP(c, POP_BLOCK);
1786 compiler_pop_fblock(c, FINALLY_TRY, body);
1788 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1789 compiler_use_next_block(c, end);
1790 if (!compiler_push_fblock(c, FINALLY_END, end))
1791 return 0;
1792 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1793 ADDOP(c, END_FINALLY);
1794 compiler_pop_fblock(c, FINALLY_END, end);
1796 return 1;
1800 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1801 (The contents of the value stack is shown in [], with the top
1802 at the right; 'tb' is trace-back info, 'val' the exception's
1803 associated value, and 'exc' the exception.)
1805 Value stack Label Instruction Argument
1806 [] SETUP_EXCEPT L1
1807 [] <code for S>
1808 [] POP_BLOCK
1809 [] JUMP_FORWARD L0
1811 [tb, val, exc] L1: DUP )
1812 [tb, val, exc, exc] <evaluate E1> )
1813 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1814 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1815 [tb, val, exc, 1] POP )
1816 [tb, val, exc] POP
1817 [tb, val] <assign to V1> (or POP if no V1)
1818 [tb] POP
1819 [] <code for S1>
1820 JUMP_FORWARD L0
1822 [tb, val, exc, 0] L2: POP
1823 [tb, val, exc] DUP
1824 .............................etc.......................
1826 [tb, val, exc, 0] Ln+1: POP
1827 [tb, val, exc] END_FINALLY # re-raise exception
1829 [] L0: <next statement>
1831 Of course, parts are not generated if Vi or Ei is not present.
1833 static int
1834 compiler_try_except(struct compiler *c, stmt_ty s)
1836 basicblock *body, *orelse, *except, *end;
1837 int i, n;
1839 body = compiler_new_block(c);
1840 except = compiler_new_block(c);
1841 orelse = compiler_new_block(c);
1842 end = compiler_new_block(c);
1843 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1844 return 0;
1845 ADDOP_JREL(c, SETUP_EXCEPT, except);
1846 compiler_use_next_block(c, body);
1847 if (!compiler_push_fblock(c, EXCEPT, body))
1848 return 0;
1849 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1850 ADDOP(c, POP_BLOCK);
1851 compiler_pop_fblock(c, EXCEPT, body);
1852 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1853 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1854 compiler_use_next_block(c, except);
1855 for (i = 0; i < n; i++) {
1856 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1857 s->v.TryExcept.handlers, i);
1858 if (!handler->v.ExceptHandler.type && i < n-1)
1859 return compiler_error(c, "default 'except:' must be last");
1860 c->u->u_lineno_set = false;
1861 c->u->u_lineno = handler->lineno;
1862 except = compiler_new_block(c);
1863 if (except == NULL)
1864 return 0;
1865 if (handler->v.ExceptHandler.type) {
1866 ADDOP(c, DUP_TOP);
1867 VISIT(c, expr, handler->v.ExceptHandler.type);
1868 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1869 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1870 ADDOP(c, POP_TOP);
1872 ADDOP(c, POP_TOP);
1873 if (handler->v.ExceptHandler.name) {
1874 VISIT(c, expr, handler->v.ExceptHandler.name);
1876 else {
1877 ADDOP(c, POP_TOP);
1879 ADDOP(c, POP_TOP);
1880 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
1881 ADDOP_JREL(c, JUMP_FORWARD, end);
1882 compiler_use_next_block(c, except);
1883 if (handler->v.ExceptHandler.type)
1884 ADDOP(c, POP_TOP);
1886 ADDOP(c, END_FINALLY);
1887 compiler_use_next_block(c, orelse);
1888 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
1889 compiler_use_next_block(c, end);
1890 return 1;
1893 static int
1894 compiler_import_as(struct compiler *c, identifier name, identifier asname)
1896 /* The IMPORT_NAME opcode was already generated. This function
1897 merely needs to bind the result to a name.
1899 If there is a dot in name, we need to split it and emit a
1900 LOAD_ATTR for each name.
1902 const char *src = PyString_AS_STRING(name);
1903 const char *dot = strchr(src, '.');
1904 if (dot) {
1905 /* Consume the base module name to get the first attribute */
1906 src = dot + 1;
1907 while (dot) {
1908 /* NB src is only defined when dot != NULL */
1909 PyObject *attr;
1910 dot = strchr(src, '.');
1911 attr = PyString_FromStringAndSize(src,
1912 dot ? dot - src : strlen(src));
1913 if (!attr)
1914 return -1;
1915 ADDOP_O(c, LOAD_ATTR, attr, names);
1916 Py_DECREF(attr);
1917 src = dot + 1;
1920 return compiler_nameop(c, asname, Store);
1923 static int
1924 compiler_import(struct compiler *c, stmt_ty s)
1926 /* The Import node stores a module name like a.b.c as a single
1927 string. This is convenient for all cases except
1928 import a.b.c as d
1929 where we need to parse that string to extract the individual
1930 module names.
1931 XXX Perhaps change the representation to make this case simpler?
1933 int i, n = asdl_seq_LEN(s->v.Import.names);
1935 for (i = 0; i < n; i++) {
1936 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
1937 int r;
1938 PyObject *level;
1940 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1941 level = PyInt_FromLong(0);
1942 else
1943 level = PyInt_FromLong(-1);
1945 if (level == NULL)
1946 return 0;
1948 ADDOP_O(c, LOAD_CONST, level, consts);
1949 Py_DECREF(level);
1950 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1951 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1953 if (alias->asname) {
1954 r = compiler_import_as(c, alias->name, alias->asname);
1955 if (!r)
1956 return r;
1958 else {
1959 identifier tmp = alias->name;
1960 const char *base = PyString_AS_STRING(alias->name);
1961 char *dot = strchr(base, '.');
1962 if (dot)
1963 tmp = PyString_FromStringAndSize(base,
1964 dot - base);
1965 r = compiler_nameop(c, tmp, Store);
1966 if (dot) {
1967 Py_DECREF(tmp);
1969 if (!r)
1970 return r;
1973 return 1;
1976 static int
1977 compiler_from_import(struct compiler *c, stmt_ty s)
1979 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
1981 PyObject *names = PyTuple_New(n);
1982 PyObject *level;
1984 if (!names)
1985 return 0;
1987 if (s->v.ImportFrom.level == 0 && c->c_flags &&
1988 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1989 level = PyInt_FromLong(-1);
1990 else
1991 level = PyInt_FromLong(s->v.ImportFrom.level);
1993 if (!level) {
1994 Py_DECREF(names);
1995 return 0;
1998 /* build up the names */
1999 for (i = 0; i < n; i++) {
2000 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2001 Py_INCREF(alias->name);
2002 PyTuple_SET_ITEM(names, i, alias->name);
2005 if (s->lineno > c->c_future->ff_lineno) {
2006 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2007 "__future__")) {
2008 Py_DECREF(level);
2009 Py_DECREF(names);
2010 return compiler_error(c,
2011 "from __future__ imports must occur "
2012 "at the beginning of the file");
2017 ADDOP_O(c, LOAD_CONST, level, consts);
2018 Py_DECREF(level);
2019 ADDOP_O(c, LOAD_CONST, names, consts);
2020 Py_DECREF(names);
2021 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2022 for (i = 0; i < n; i++) {
2023 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2024 identifier store_name;
2026 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2027 assert(n == 1);
2028 ADDOP(c, IMPORT_STAR);
2029 return 1;
2032 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2033 store_name = alias->name;
2034 if (alias->asname)
2035 store_name = alias->asname;
2037 if (!compiler_nameop(c, store_name, Store)) {
2038 Py_DECREF(names);
2039 return 0;
2042 /* remove imported module */
2043 ADDOP(c, POP_TOP);
2044 return 1;
2047 static int
2048 compiler_assert(struct compiler *c, stmt_ty s)
2050 static PyObject *assertion_error = NULL;
2051 basicblock *end;
2053 if (Py_OptimizeFlag)
2054 return 1;
2055 if (assertion_error == NULL) {
2056 assertion_error = PyString_InternFromString("AssertionError");
2057 if (assertion_error == NULL)
2058 return 0;
2060 if (s->v.Assert.test->kind == Tuple_kind &&
2061 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2062 const char* msg =
2063 "assertion is always true, perhaps remove parentheses?";
2064 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2065 c->u->u_lineno, NULL, NULL) == -1)
2066 return 0;
2068 VISIT(c, expr, s->v.Assert.test);
2069 end = compiler_new_block(c);
2070 if (end == NULL)
2071 return 0;
2072 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2073 ADDOP(c, POP_TOP);
2074 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2075 if (s->v.Assert.msg) {
2076 VISIT(c, expr, s->v.Assert.msg);
2077 ADDOP_I(c, RAISE_VARARGS, 2);
2079 else {
2080 ADDOP_I(c, RAISE_VARARGS, 1);
2082 compiler_use_next_block(c, end);
2083 ADDOP(c, POP_TOP);
2084 return 1;
2087 static int
2088 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2090 int i, n;
2092 /* Always assign a lineno to the next instruction for a stmt. */
2093 c->u->u_lineno = s->lineno;
2094 c->u->u_lineno_set = false;
2096 switch (s->kind) {
2097 case FunctionDef_kind:
2098 return compiler_function(c, s);
2099 case ClassDef_kind:
2100 return compiler_class(c, s);
2101 case Return_kind:
2102 if (c->u->u_ste->ste_type != FunctionBlock)
2103 return compiler_error(c, "'return' outside function");
2104 if (s->v.Return.value) {
2105 VISIT(c, expr, s->v.Return.value);
2107 else
2108 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2109 ADDOP(c, RETURN_VALUE);
2110 break;
2111 case Delete_kind:
2112 VISIT_SEQ(c, expr, s->v.Delete.targets)
2113 break;
2114 case Assign_kind:
2115 n = asdl_seq_LEN(s->v.Assign.targets);
2116 VISIT(c, expr, s->v.Assign.value);
2117 for (i = 0; i < n; i++) {
2118 if (i < n - 1)
2119 ADDOP(c, DUP_TOP);
2120 VISIT(c, expr,
2121 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2123 break;
2124 case AugAssign_kind:
2125 return compiler_augassign(c, s);
2126 case Print_kind:
2127 return compiler_print(c, s);
2128 case For_kind:
2129 return compiler_for(c, s);
2130 case While_kind:
2131 return compiler_while(c, s);
2132 case If_kind:
2133 return compiler_if(c, s);
2134 case Raise_kind:
2135 n = 0;
2136 if (s->v.Raise.type) {
2137 VISIT(c, expr, s->v.Raise.type);
2138 n++;
2139 if (s->v.Raise.inst) {
2140 VISIT(c, expr, s->v.Raise.inst);
2141 n++;
2142 if (s->v.Raise.tback) {
2143 VISIT(c, expr, s->v.Raise.tback);
2144 n++;
2148 ADDOP_I(c, RAISE_VARARGS, n);
2149 break;
2150 case TryExcept_kind:
2151 return compiler_try_except(c, s);
2152 case TryFinally_kind:
2153 return compiler_try_finally(c, s);
2154 case Assert_kind:
2155 return compiler_assert(c, s);
2156 case Import_kind:
2157 return compiler_import(c, s);
2158 case ImportFrom_kind:
2159 return compiler_from_import(c, s);
2160 case Exec_kind:
2161 VISIT(c, expr, s->v.Exec.body);
2162 if (s->v.Exec.globals) {
2163 VISIT(c, expr, s->v.Exec.globals);
2164 if (s->v.Exec.locals) {
2165 VISIT(c, expr, s->v.Exec.locals);
2166 } else {
2167 ADDOP(c, DUP_TOP);
2169 } else {
2170 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2171 ADDOP(c, DUP_TOP);
2173 ADDOP(c, EXEC_STMT);
2174 break;
2175 case Global_kind:
2176 break;
2177 case Expr_kind:
2178 if (c->c_interactive && c->c_nestlevel <= 1) {
2179 VISIT(c, expr, s->v.Expr.value);
2180 ADDOP(c, PRINT_EXPR);
2182 else if (s->v.Expr.value->kind != Str_kind &&
2183 s->v.Expr.value->kind != Num_kind) {
2184 VISIT(c, expr, s->v.Expr.value);
2185 ADDOP(c, POP_TOP);
2187 break;
2188 case Pass_kind:
2189 break;
2190 case Break_kind:
2191 if (!compiler_in_loop(c))
2192 return compiler_error(c, "'break' outside loop");
2193 ADDOP(c, BREAK_LOOP);
2194 break;
2195 case Continue_kind:
2196 return compiler_continue(c);
2197 case With_kind:
2198 return compiler_with(c, s);
2200 return 1;
2203 static int
2204 unaryop(unaryop_ty op)
2206 switch (op) {
2207 case Invert:
2208 return UNARY_INVERT;
2209 case Not:
2210 return UNARY_NOT;
2211 case UAdd:
2212 return UNARY_POSITIVE;
2213 case USub:
2214 return UNARY_NEGATIVE;
2215 default:
2216 PyErr_Format(PyExc_SystemError,
2217 "unary op %d should not be possible", op);
2218 return 0;
2222 static int
2223 binop(struct compiler *c, operator_ty op)
2225 switch (op) {
2226 case Add:
2227 return BINARY_ADD;
2228 case Sub:
2229 return BINARY_SUBTRACT;
2230 case Mult:
2231 return BINARY_MULTIPLY;
2232 case Div:
2233 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2234 return BINARY_TRUE_DIVIDE;
2235 else
2236 return BINARY_DIVIDE;
2237 case Mod:
2238 return BINARY_MODULO;
2239 case Pow:
2240 return BINARY_POWER;
2241 case LShift:
2242 return BINARY_LSHIFT;
2243 case RShift:
2244 return BINARY_RSHIFT;
2245 case BitOr:
2246 return BINARY_OR;
2247 case BitXor:
2248 return BINARY_XOR;
2249 case BitAnd:
2250 return BINARY_AND;
2251 case FloorDiv:
2252 return BINARY_FLOOR_DIVIDE;
2253 default:
2254 PyErr_Format(PyExc_SystemError,
2255 "binary op %d should not be possible", op);
2256 return 0;
2260 static int
2261 cmpop(cmpop_ty op)
2263 switch (op) {
2264 case Eq:
2265 return PyCmp_EQ;
2266 case NotEq:
2267 return PyCmp_NE;
2268 case Lt:
2269 return PyCmp_LT;
2270 case LtE:
2271 return PyCmp_LE;
2272 case Gt:
2273 return PyCmp_GT;
2274 case GtE:
2275 return PyCmp_GE;
2276 case Is:
2277 return PyCmp_IS;
2278 case IsNot:
2279 return PyCmp_IS_NOT;
2280 case In:
2281 return PyCmp_IN;
2282 case NotIn:
2283 return PyCmp_NOT_IN;
2284 default:
2285 return PyCmp_BAD;
2289 static int
2290 inplace_binop(struct compiler *c, operator_ty op)
2292 switch (op) {
2293 case Add:
2294 return INPLACE_ADD;
2295 case Sub:
2296 return INPLACE_SUBTRACT;
2297 case Mult:
2298 return INPLACE_MULTIPLY;
2299 case Div:
2300 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2301 return INPLACE_TRUE_DIVIDE;
2302 else
2303 return INPLACE_DIVIDE;
2304 case Mod:
2305 return INPLACE_MODULO;
2306 case Pow:
2307 return INPLACE_POWER;
2308 case LShift:
2309 return INPLACE_LSHIFT;
2310 case RShift:
2311 return INPLACE_RSHIFT;
2312 case BitOr:
2313 return INPLACE_OR;
2314 case BitXor:
2315 return INPLACE_XOR;
2316 case BitAnd:
2317 return INPLACE_AND;
2318 case FloorDiv:
2319 return INPLACE_FLOOR_DIVIDE;
2320 default:
2321 PyErr_Format(PyExc_SystemError,
2322 "inplace binary op %d should not be possible", op);
2323 return 0;
2327 static int
2328 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2330 int op, scope, arg;
2331 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2333 PyObject *dict = c->u->u_names;
2334 PyObject *mangled;
2335 /* XXX AugStore isn't used anywhere! */
2337 /* First check for assignment to __debug__. Param? */
2338 if ((ctx == Store || ctx == AugStore || ctx == Del)
2339 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2340 return compiler_error(c, "can not assign to __debug__");
2343 mangled = _Py_Mangle(c->u->u_private, name);
2344 if (!mangled)
2345 return 0;
2347 op = 0;
2348 optype = OP_NAME;
2349 scope = PyST_GetScope(c->u->u_ste, mangled);
2350 switch (scope) {
2351 case FREE:
2352 dict = c->u->u_freevars;
2353 optype = OP_DEREF;
2354 break;
2355 case CELL:
2356 dict = c->u->u_cellvars;
2357 optype = OP_DEREF;
2358 break;
2359 case LOCAL:
2360 if (c->u->u_ste->ste_type == FunctionBlock)
2361 optype = OP_FAST;
2362 break;
2363 case GLOBAL_IMPLICIT:
2364 if (c->u->u_ste->ste_type == FunctionBlock &&
2365 !c->u->u_ste->ste_unoptimized)
2366 optype = OP_GLOBAL;
2367 break;
2368 case GLOBAL_EXPLICIT:
2369 optype = OP_GLOBAL;
2370 break;
2371 default:
2372 /* scope can be 0 */
2373 break;
2376 /* XXX Leave assert here, but handle __doc__ and the like better */
2377 assert(scope || PyString_AS_STRING(name)[0] == '_');
2379 switch (optype) {
2380 case OP_DEREF:
2381 switch (ctx) {
2382 case Load: op = LOAD_DEREF; break;
2383 case Store: op = STORE_DEREF; break;
2384 case AugLoad:
2385 case AugStore:
2386 break;
2387 case Del:
2388 PyErr_Format(PyExc_SyntaxError,
2389 "can not delete variable '%s' referenced "
2390 "in nested scope",
2391 PyString_AS_STRING(name));
2392 Py_DECREF(mangled);
2393 return 0;
2394 case Param:
2395 default:
2396 PyErr_SetString(PyExc_SystemError,
2397 "param invalid for deref variable");
2398 return 0;
2400 break;
2401 case OP_FAST:
2402 switch (ctx) {
2403 case Load: op = LOAD_FAST; break;
2404 case Store: op = STORE_FAST; break;
2405 case Del: op = DELETE_FAST; break;
2406 case AugLoad:
2407 case AugStore:
2408 break;
2409 case Param:
2410 default:
2411 PyErr_SetString(PyExc_SystemError,
2412 "param invalid for local variable");
2413 return 0;
2415 ADDOP_O(c, op, mangled, varnames);
2416 Py_DECREF(mangled);
2417 return 1;
2418 case OP_GLOBAL:
2419 switch (ctx) {
2420 case Load: op = LOAD_GLOBAL; break;
2421 case Store: op = STORE_GLOBAL; break;
2422 case Del: op = DELETE_GLOBAL; break;
2423 case AugLoad:
2424 case AugStore:
2425 break;
2426 case Param:
2427 default:
2428 PyErr_SetString(PyExc_SystemError,
2429 "param invalid for global variable");
2430 return 0;
2432 break;
2433 case OP_NAME:
2434 switch (ctx) {
2435 case Load: op = LOAD_NAME; break;
2436 case Store: op = STORE_NAME; break;
2437 case Del: op = DELETE_NAME; break;
2438 case AugLoad:
2439 case AugStore:
2440 break;
2441 case Param:
2442 default:
2443 PyErr_SetString(PyExc_SystemError,
2444 "param invalid for name variable");
2445 return 0;
2447 break;
2450 assert(op);
2451 arg = compiler_add_o(c, dict, mangled);
2452 Py_DECREF(mangled);
2453 if (arg < 0)
2454 return 0;
2455 return compiler_addop_i(c, op, arg);
2458 static int
2459 compiler_boolop(struct compiler *c, expr_ty e)
2461 basicblock *end;
2462 int jumpi, i, n;
2463 asdl_seq *s;
2465 assert(e->kind == BoolOp_kind);
2466 if (e->v.BoolOp.op == And)
2467 jumpi = JUMP_IF_FALSE;
2468 else
2469 jumpi = JUMP_IF_TRUE;
2470 end = compiler_new_block(c);
2471 if (end == NULL)
2472 return 0;
2473 s = e->v.BoolOp.values;
2474 n = asdl_seq_LEN(s) - 1;
2475 assert(n >= 0);
2476 for (i = 0; i < n; ++i) {
2477 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2478 ADDOP_JREL(c, jumpi, end);
2479 ADDOP(c, POP_TOP)
2481 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2482 compiler_use_next_block(c, end);
2483 return 1;
2486 static int
2487 compiler_list(struct compiler *c, expr_ty e)
2489 int n = asdl_seq_LEN(e->v.List.elts);
2490 if (e->v.List.ctx == Store) {
2491 ADDOP_I(c, UNPACK_SEQUENCE, n);
2493 VISIT_SEQ(c, expr, e->v.List.elts);
2494 if (e->v.List.ctx == Load) {
2495 ADDOP_I(c, BUILD_LIST, n);
2497 return 1;
2500 static int
2501 compiler_tuple(struct compiler *c, expr_ty e)
2503 int n = asdl_seq_LEN(e->v.Tuple.elts);
2504 if (e->v.Tuple.ctx == Store) {
2505 ADDOP_I(c, UNPACK_SEQUENCE, n);
2507 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2508 if (e->v.Tuple.ctx == Load) {
2509 ADDOP_I(c, BUILD_TUPLE, n);
2511 return 1;
2514 static int
2515 compiler_compare(struct compiler *c, expr_ty e)
2517 int i, n;
2518 basicblock *cleanup = NULL;
2520 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2521 VISIT(c, expr, e->v.Compare.left);
2522 n = asdl_seq_LEN(e->v.Compare.ops);
2523 assert(n > 0);
2524 if (n > 1) {
2525 cleanup = compiler_new_block(c);
2526 if (cleanup == NULL)
2527 return 0;
2528 VISIT(c, expr,
2529 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2531 for (i = 1; i < n; i++) {
2532 ADDOP(c, DUP_TOP);
2533 ADDOP(c, ROT_THREE);
2534 ADDOP_I(c, COMPARE_OP,
2535 cmpop((cmpop_ty)(asdl_seq_GET(
2536 e->v.Compare.ops, i - 1))));
2537 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2538 NEXT_BLOCK(c);
2539 ADDOP(c, POP_TOP);
2540 if (i < (n - 1))
2541 VISIT(c, expr,
2542 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2544 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2545 ADDOP_I(c, COMPARE_OP,
2546 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2547 if (n > 1) {
2548 basicblock *end = compiler_new_block(c);
2549 if (end == NULL)
2550 return 0;
2551 ADDOP_JREL(c, JUMP_FORWARD, end);
2552 compiler_use_next_block(c, cleanup);
2553 ADDOP(c, ROT_TWO);
2554 ADDOP(c, POP_TOP);
2555 compiler_use_next_block(c, end);
2557 return 1;
2560 static int
2561 compiler_call(struct compiler *c, expr_ty e)
2563 int n, code = 0;
2565 VISIT(c, expr, e->v.Call.func);
2566 n = asdl_seq_LEN(e->v.Call.args);
2567 VISIT_SEQ(c, expr, e->v.Call.args);
2568 if (e->v.Call.keywords) {
2569 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2570 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2572 if (e->v.Call.starargs) {
2573 VISIT(c, expr, e->v.Call.starargs);
2574 code |= 1;
2576 if (e->v.Call.kwargs) {
2577 VISIT(c, expr, e->v.Call.kwargs);
2578 code |= 2;
2580 switch (code) {
2581 case 0:
2582 ADDOP_I(c, CALL_FUNCTION, n);
2583 break;
2584 case 1:
2585 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2586 break;
2587 case 2:
2588 ADDOP_I(c, CALL_FUNCTION_KW, n);
2589 break;
2590 case 3:
2591 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2592 break;
2594 return 1;
2597 static int
2598 compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
2599 asdl_seq *generators, int gen_index,
2600 expr_ty elt)
2602 /* generate code for the iterator, then each of the ifs,
2603 and then write to the element */
2605 comprehension_ty l;
2606 basicblock *start, *anchor, *skip, *if_cleanup;
2607 int i, n;
2609 start = compiler_new_block(c);
2610 skip = compiler_new_block(c);
2611 if_cleanup = compiler_new_block(c);
2612 anchor = compiler_new_block(c);
2614 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2615 anchor == NULL)
2616 return 0;
2618 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2619 VISIT(c, expr, l->iter);
2620 ADDOP(c, GET_ITER);
2621 compiler_use_next_block(c, start);
2622 ADDOP_JREL(c, FOR_ITER, anchor);
2623 NEXT_BLOCK(c);
2624 VISIT(c, expr, l->target);
2626 /* XXX this needs to be cleaned up...a lot! */
2627 n = asdl_seq_LEN(l->ifs);
2628 for (i = 0; i < n; i++) {
2629 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
2630 VISIT(c, expr, e);
2631 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2632 NEXT_BLOCK(c);
2633 ADDOP(c, POP_TOP);
2636 if (++gen_index < asdl_seq_LEN(generators))
2637 if (!compiler_listcomp_generator(c, tmpname,
2638 generators, gen_index, elt))
2639 return 0;
2641 /* only append after the last for generator */
2642 if (gen_index >= asdl_seq_LEN(generators)) {
2643 if (!compiler_nameop(c, tmpname, Load))
2644 return 0;
2645 VISIT(c, expr, elt);
2646 ADDOP(c, LIST_APPEND);
2648 compiler_use_next_block(c, skip);
2650 for (i = 0; i < n; i++) {
2651 ADDOP_I(c, JUMP_FORWARD, 1);
2652 if (i == 0)
2653 compiler_use_next_block(c, if_cleanup);
2654 ADDOP(c, POP_TOP);
2656 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2657 compiler_use_next_block(c, anchor);
2658 /* delete the temporary list name added to locals */
2659 if (gen_index == 1)
2660 if (!compiler_nameop(c, tmpname, Del))
2661 return 0;
2663 return 1;
2666 static int
2667 compiler_listcomp(struct compiler *c, expr_ty e)
2669 identifier tmp;
2670 int rc = 0;
2671 asdl_seq *generators = e->v.ListComp.generators;
2673 assert(e->kind == ListComp_kind);
2674 tmp = compiler_new_tmpname(c);
2675 if (!tmp)
2676 return 0;
2677 ADDOP_I(c, BUILD_LIST, 0);
2678 ADDOP(c, DUP_TOP);
2679 if (compiler_nameop(c, tmp, Store))
2680 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2681 e->v.ListComp.elt);
2682 Py_DECREF(tmp);
2683 return rc;
2686 static int
2687 compiler_genexp_generator(struct compiler *c,
2688 asdl_seq *generators, int gen_index,
2689 expr_ty elt)
2691 /* generate code for the iterator, then each of the ifs,
2692 and then write to the element */
2694 comprehension_ty ge;
2695 basicblock *start, *anchor, *skip, *if_cleanup, *end;
2696 int i, n;
2698 start = compiler_new_block(c);
2699 skip = compiler_new_block(c);
2700 if_cleanup = compiler_new_block(c);
2701 anchor = compiler_new_block(c);
2702 end = compiler_new_block(c);
2704 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2705 anchor == NULL || end == NULL)
2706 return 0;
2708 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2709 ADDOP_JREL(c, SETUP_LOOP, end);
2710 if (!compiler_push_fblock(c, LOOP, start))
2711 return 0;
2713 if (gen_index == 0) {
2714 /* Receive outermost iter as an implicit argument */
2715 c->u->u_argcount = 1;
2716 ADDOP_I(c, LOAD_FAST, 0);
2718 else {
2719 /* Sub-iter - calculate on the fly */
2720 VISIT(c, expr, ge->iter);
2721 ADDOP(c, GET_ITER);
2723 compiler_use_next_block(c, start);
2724 ADDOP_JREL(c, FOR_ITER, anchor);
2725 NEXT_BLOCK(c);
2726 VISIT(c, expr, ge->target);
2728 /* XXX this needs to be cleaned up...a lot! */
2729 n = asdl_seq_LEN(ge->ifs);
2730 for (i = 0; i < n; i++) {
2731 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
2732 VISIT(c, expr, e);
2733 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2734 NEXT_BLOCK(c);
2735 ADDOP(c, POP_TOP);
2738 if (++gen_index < asdl_seq_LEN(generators))
2739 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2740 return 0;
2742 /* only append after the last 'for' generator */
2743 if (gen_index >= asdl_seq_LEN(generators)) {
2744 VISIT(c, expr, elt);
2745 ADDOP(c, YIELD_VALUE);
2746 ADDOP(c, POP_TOP);
2748 compiler_use_next_block(c, skip);
2750 for (i = 0; i < n; i++) {
2751 ADDOP_I(c, JUMP_FORWARD, 1);
2752 if (i == 0)
2753 compiler_use_next_block(c, if_cleanup);
2755 ADDOP(c, POP_TOP);
2757 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2758 compiler_use_next_block(c, anchor);
2759 ADDOP(c, POP_BLOCK);
2760 compiler_pop_fblock(c, LOOP, start);
2761 compiler_use_next_block(c, end);
2763 return 1;
2766 static int
2767 compiler_genexp(struct compiler *c, expr_ty e)
2769 static identifier name;
2770 PyCodeObject *co;
2771 expr_ty outermost_iter = ((comprehension_ty)
2772 (asdl_seq_GET(e->v.GeneratorExp.generators,
2773 0)))->iter;
2775 if (!name) {
2776 name = PyString_FromString("<genexpr>");
2777 if (!name)
2778 return 0;
2781 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2782 return 0;
2783 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2784 e->v.GeneratorExp.elt);
2785 co = assemble(c, 1);
2786 compiler_exit_scope(c);
2787 if (co == NULL)
2788 return 0;
2790 compiler_make_closure(c, co, 0);
2791 Py_DECREF(co);
2793 VISIT(c, expr, outermost_iter);
2794 ADDOP(c, GET_ITER);
2795 ADDOP_I(c, CALL_FUNCTION, 1);
2797 return 1;
2800 static int
2801 compiler_visit_keyword(struct compiler *c, keyword_ty k)
2803 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2804 VISIT(c, expr, k->value);
2805 return 1;
2808 /* Test whether expression is constant. For constants, report
2809 whether they are true or false.
2811 Return values: 1 for true, 0 for false, -1 for non-constant.
2814 static int
2815 expr_constant(expr_ty e)
2817 switch (e->kind) {
2818 case Num_kind:
2819 return PyObject_IsTrue(e->v.Num.n);
2820 case Str_kind:
2821 return PyObject_IsTrue(e->v.Str.s);
2822 case Name_kind:
2823 /* __debug__ is not assignable, so we can optimize
2824 * it away in if and while statements */
2825 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2826 "__debug__") == 0)
2827 return ! Py_OptimizeFlag;
2828 /* fall through */
2829 default:
2830 return -1;
2835 Implements the with statement from PEP 343.
2837 The semantics outlined in that PEP are as follows:
2839 with EXPR as VAR:
2840 BLOCK
2842 It is implemented roughly as:
2844 context = EXPR
2845 exit = context.__exit__ # not calling it
2846 value = context.__enter__()
2847 try:
2848 VAR = value # if VAR present in the syntax
2849 BLOCK
2850 finally:
2851 if an exception was raised:
2852 exc = copy of (exception, instance, traceback)
2853 else:
2854 exc = (None, None, None)
2855 exit(*exc)
2857 static int
2858 compiler_with(struct compiler *c, stmt_ty s)
2860 static identifier enter_attr, exit_attr;
2861 basicblock *block, *finally;
2862 identifier tmpvalue = NULL;
2864 assert(s->kind == With_kind);
2866 if (!enter_attr) {
2867 enter_attr = PyString_InternFromString("__enter__");
2868 if (!enter_attr)
2869 return 0;
2871 if (!exit_attr) {
2872 exit_attr = PyString_InternFromString("__exit__");
2873 if (!exit_attr)
2874 return 0;
2877 block = compiler_new_block(c);
2878 finally = compiler_new_block(c);
2879 if (!block || !finally)
2880 return 0;
2882 if (s->v.With.optional_vars) {
2883 /* Create a temporary variable to hold context.__enter__().
2884 We need to do this rather than preserving it on the stack
2885 because SETUP_FINALLY remembers the stack level.
2886 We need to do the assignment *inside* the try/finally
2887 so that context.__exit__() is called when the assignment
2888 fails. But we need to call context.__enter__() *before*
2889 the try/finally so that if it fails we won't call
2890 context.__exit__().
2892 tmpvalue = compiler_new_tmpname(c);
2893 if (tmpvalue == NULL)
2894 return 0;
2895 PyArena_AddPyObject(c->c_arena, tmpvalue);
2898 /* Evaluate EXPR */
2899 VISIT(c, expr, s->v.With.context_expr);
2901 /* Squirrel away context.__exit__ by stuffing it under context */
2902 ADDOP(c, DUP_TOP);
2903 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
2904 ADDOP(c, ROT_TWO);
2906 /* Call context.__enter__() */
2907 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2908 ADDOP_I(c, CALL_FUNCTION, 0);
2910 if (s->v.With.optional_vars) {
2911 /* Store it in tmpvalue */
2912 if (!compiler_nameop(c, tmpvalue, Store))
2913 return 0;
2915 else {
2916 /* Discard result from context.__enter__() */
2917 ADDOP(c, POP_TOP);
2920 /* Start the try block */
2921 ADDOP_JREL(c, SETUP_FINALLY, finally);
2923 compiler_use_next_block(c, block);
2924 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
2925 return 0;
2928 if (s->v.With.optional_vars) {
2929 /* Bind saved result of context.__enter__() to VAR */
2930 if (!compiler_nameop(c, tmpvalue, Load) ||
2931 !compiler_nameop(c, tmpvalue, Del))
2932 return 0;
2933 VISIT(c, expr, s->v.With.optional_vars);
2936 /* BLOCK code */
2937 VISIT_SEQ(c, stmt, s->v.With.body);
2939 /* End of try block; start the finally block */
2940 ADDOP(c, POP_BLOCK);
2941 compiler_pop_fblock(c, FINALLY_TRY, block);
2943 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2944 compiler_use_next_block(c, finally);
2945 if (!compiler_push_fblock(c, FINALLY_END, finally))
2946 return 0;
2948 /* Finally block starts; context.__exit__ is on the stack under
2949 the exception or return information. Just issue our magic
2950 opcode. */
2951 ADDOP(c, WITH_CLEANUP);
2953 /* Finally block ends. */
2954 ADDOP(c, END_FINALLY);
2955 compiler_pop_fblock(c, FINALLY_END, finally);
2956 return 1;
2959 static int
2960 compiler_visit_expr(struct compiler *c, expr_ty e)
2962 int i, n;
2964 /* If expr e has a different line number than the last expr/stmt,
2965 set a new line number for the next instruction.
2967 if (e->lineno > c->u->u_lineno) {
2968 c->u->u_lineno = e->lineno;
2969 c->u->u_lineno_set = false;
2971 switch (e->kind) {
2972 case BoolOp_kind:
2973 return compiler_boolop(c, e);
2974 case BinOp_kind:
2975 VISIT(c, expr, e->v.BinOp.left);
2976 VISIT(c, expr, e->v.BinOp.right);
2977 ADDOP(c, binop(c, e->v.BinOp.op));
2978 break;
2979 case UnaryOp_kind:
2980 VISIT(c, expr, e->v.UnaryOp.operand);
2981 ADDOP(c, unaryop(e->v.UnaryOp.op));
2982 break;
2983 case Lambda_kind:
2984 return compiler_lambda(c, e);
2985 case IfExp_kind:
2986 return compiler_ifexp(c, e);
2987 case Dict_kind:
2988 n = asdl_seq_LEN(e->v.Dict.values);
2989 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
2990 for (i = 0; i < n; i++) {
2991 VISIT(c, expr,
2992 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
2993 VISIT(c, expr,
2994 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
2995 ADDOP(c, STORE_MAP);
2997 break;
2998 case ListComp_kind:
2999 return compiler_listcomp(c, e);
3000 case GeneratorExp_kind:
3001 return compiler_genexp(c, e);
3002 case Yield_kind:
3003 if (c->u->u_ste->ste_type != FunctionBlock)
3004 return compiler_error(c, "'yield' outside function");
3005 if (e->v.Yield.value) {
3006 VISIT(c, expr, e->v.Yield.value);
3008 else {
3009 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3011 ADDOP(c, YIELD_VALUE);
3012 break;
3013 case Compare_kind:
3014 return compiler_compare(c, e);
3015 case Call_kind:
3016 return compiler_call(c, e);
3017 case Repr_kind:
3018 VISIT(c, expr, e->v.Repr.value);
3019 ADDOP(c, UNARY_CONVERT);
3020 break;
3021 case Num_kind:
3022 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3023 break;
3024 case Str_kind:
3025 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3026 break;
3027 /* The following exprs can be assignment targets. */
3028 case Attribute_kind:
3029 if (e->v.Attribute.ctx != AugStore)
3030 VISIT(c, expr, e->v.Attribute.value);
3031 switch (e->v.Attribute.ctx) {
3032 case AugLoad:
3033 ADDOP(c, DUP_TOP);
3034 /* Fall through to load */
3035 case Load:
3036 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3037 break;
3038 case AugStore:
3039 ADDOP(c, ROT_TWO);
3040 /* Fall through to save */
3041 case Store:
3042 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3043 break;
3044 case Del:
3045 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3046 break;
3047 case Param:
3048 default:
3049 PyErr_SetString(PyExc_SystemError,
3050 "param invalid in attribute expression");
3051 return 0;
3053 break;
3054 case Subscript_kind:
3055 switch (e->v.Subscript.ctx) {
3056 case AugLoad:
3057 VISIT(c, expr, e->v.Subscript.value);
3058 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3059 break;
3060 case Load:
3061 VISIT(c, expr, e->v.Subscript.value);
3062 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3063 break;
3064 case AugStore:
3065 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3066 break;
3067 case Store:
3068 VISIT(c, expr, e->v.Subscript.value);
3069 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3070 break;
3071 case Del:
3072 VISIT(c, expr, e->v.Subscript.value);
3073 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3074 break;
3075 case Param:
3076 default:
3077 PyErr_SetString(PyExc_SystemError,
3078 "param invalid in subscript expression");
3079 return 0;
3081 break;
3082 case Name_kind:
3083 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3084 /* child nodes of List and Tuple will have expr_context set */
3085 case List_kind:
3086 return compiler_list(c, e);
3087 case Tuple_kind:
3088 return compiler_tuple(c, e);
3090 return 1;
3093 static int
3094 compiler_augassign(struct compiler *c, stmt_ty s)
3096 expr_ty e = s->v.AugAssign.target;
3097 expr_ty auge;
3099 assert(s->kind == AugAssign_kind);
3101 switch (e->kind) {
3102 case Attribute_kind:
3103 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3104 AugLoad, e->lineno, e->col_offset, c->c_arena);
3105 if (auge == NULL)
3106 return 0;
3107 VISIT(c, expr, auge);
3108 VISIT(c, expr, s->v.AugAssign.value);
3109 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3110 auge->v.Attribute.ctx = AugStore;
3111 VISIT(c, expr, auge);
3112 break;
3113 case Subscript_kind:
3114 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3115 AugLoad, e->lineno, e->col_offset, c->c_arena);
3116 if (auge == NULL)
3117 return 0;
3118 VISIT(c, expr, auge);
3119 VISIT(c, expr, s->v.AugAssign.value);
3120 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3121 auge->v.Subscript.ctx = AugStore;
3122 VISIT(c, expr, auge);
3123 break;
3124 case Name_kind:
3125 if (!compiler_nameop(c, e->v.Name.id, Load))
3126 return 0;
3127 VISIT(c, expr, s->v.AugAssign.value);
3128 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3129 return compiler_nameop(c, e->v.Name.id, Store);
3130 default:
3131 PyErr_Format(PyExc_SystemError,
3132 "invalid node type (%d) for augmented assignment",
3133 e->kind);
3134 return 0;
3136 return 1;
3139 static int
3140 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3142 struct fblockinfo *f;
3143 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3144 PyErr_SetString(PyExc_SystemError,
3145 "too many statically nested blocks");
3146 return 0;
3148 f = &c->u->u_fblock[c->u->u_nfblocks++];
3149 f->fb_type = t;
3150 f->fb_block = b;
3151 return 1;
3154 static void
3155 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3157 struct compiler_unit *u = c->u;
3158 assert(u->u_nfblocks > 0);
3159 u->u_nfblocks--;
3160 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3161 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3164 static int
3165 compiler_in_loop(struct compiler *c) {
3166 int i;
3167 struct compiler_unit *u = c->u;
3168 for (i = 0; i < u->u_nfblocks; ++i) {
3169 if (u->u_fblock[i].fb_type == LOOP)
3170 return 1;
3172 return 0;
3174 /* Raises a SyntaxError and returns 0.
3175 If something goes wrong, a different exception may be raised.
3178 static int
3179 compiler_error(struct compiler *c, const char *errstr)
3181 PyObject *loc;
3182 PyObject *u = NULL, *v = NULL;
3184 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3185 if (!loc) {
3186 Py_INCREF(Py_None);
3187 loc = Py_None;
3189 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3190 Py_None, loc);
3191 if (!u)
3192 goto exit;
3193 v = Py_BuildValue("(zO)", errstr, u);
3194 if (!v)
3195 goto exit;
3196 PyErr_SetObject(PyExc_SyntaxError, v);
3197 exit:
3198 Py_DECREF(loc);
3199 Py_XDECREF(u);
3200 Py_XDECREF(v);
3201 return 0;
3204 static int
3205 compiler_handle_subscr(struct compiler *c, const char *kind,
3206 expr_context_ty ctx)
3208 int op = 0;
3210 /* XXX this code is duplicated */
3211 switch (ctx) {
3212 case AugLoad: /* fall through to Load */
3213 case Load: op = BINARY_SUBSCR; break;
3214 case AugStore:/* fall through to Store */
3215 case Store: op = STORE_SUBSCR; break;
3216 case Del: op = DELETE_SUBSCR; break;
3217 case Param:
3218 PyErr_Format(PyExc_SystemError,
3219 "invalid %s kind %d in subscript\n",
3220 kind, ctx);
3221 return 0;
3223 if (ctx == AugLoad) {
3224 ADDOP_I(c, DUP_TOPX, 2);
3226 else if (ctx == AugStore) {
3227 ADDOP(c, ROT_THREE);
3229 ADDOP(c, op);
3230 return 1;
3233 static int
3234 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3236 int n = 2;
3237 assert(s->kind == Slice_kind);
3239 /* only handles the cases where BUILD_SLICE is emitted */
3240 if (s->v.Slice.lower) {
3241 VISIT(c, expr, s->v.Slice.lower);
3243 else {
3244 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3247 if (s->v.Slice.upper) {
3248 VISIT(c, expr, s->v.Slice.upper);
3250 else {
3251 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3254 if (s->v.Slice.step) {
3255 n++;
3256 VISIT(c, expr, s->v.Slice.step);
3258 ADDOP_I(c, BUILD_SLICE, n);
3259 return 1;
3262 static int
3263 compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3265 int op = 0, slice_offset = 0, stack_count = 0;
3267 assert(s->v.Slice.step == NULL);
3268 if (s->v.Slice.lower) {
3269 slice_offset++;
3270 stack_count++;
3271 if (ctx != AugStore)
3272 VISIT(c, expr, s->v.Slice.lower);
3274 if (s->v.Slice.upper) {
3275 slice_offset += 2;
3276 stack_count++;
3277 if (ctx != AugStore)
3278 VISIT(c, expr, s->v.Slice.upper);
3281 if (ctx == AugLoad) {
3282 switch (stack_count) {
3283 case 0: ADDOP(c, DUP_TOP); break;
3284 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3285 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3288 else if (ctx == AugStore) {
3289 switch (stack_count) {
3290 case 0: ADDOP(c, ROT_TWO); break;
3291 case 1: ADDOP(c, ROT_THREE); break;
3292 case 2: ADDOP(c, ROT_FOUR); break;
3296 switch (ctx) {
3297 case AugLoad: /* fall through to Load */
3298 case Load: op = SLICE; break;
3299 case AugStore:/* fall through to Store */
3300 case Store: op = STORE_SLICE; break;
3301 case Del: op = DELETE_SLICE; break;
3302 case Param:
3303 default:
3304 PyErr_SetString(PyExc_SystemError,
3305 "param invalid in simple slice");
3306 return 0;
3309 ADDOP(c, op + slice_offset);
3310 return 1;
3313 static int
3314 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3315 expr_context_ty ctx)
3317 switch (s->kind) {
3318 case Ellipsis_kind:
3319 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3320 break;
3321 case Slice_kind:
3322 return compiler_slice(c, s, ctx);
3323 case Index_kind:
3324 VISIT(c, expr, s->v.Index.value);
3325 break;
3326 case ExtSlice_kind:
3327 default:
3328 PyErr_SetString(PyExc_SystemError,
3329 "extended slice invalid in nested slice");
3330 return 0;
3332 return 1;
3335 static int
3336 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3338 char * kindname = NULL;
3339 switch (s->kind) {
3340 case Index_kind:
3341 kindname = "index";
3342 if (ctx != AugStore) {
3343 VISIT(c, expr, s->v.Index.value);
3345 break;
3346 case Ellipsis_kind:
3347 kindname = "ellipsis";
3348 if (ctx != AugStore) {
3349 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3351 break;
3352 case Slice_kind:
3353 kindname = "slice";
3354 if (!s->v.Slice.step)
3355 return compiler_simple_slice(c, s, ctx);
3356 if (ctx != AugStore) {
3357 if (!compiler_slice(c, s, ctx))
3358 return 0;
3360 break;
3361 case ExtSlice_kind:
3362 kindname = "extended slice";
3363 if (ctx != AugStore) {
3364 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3365 for (i = 0; i < n; i++) {
3366 slice_ty sub = (slice_ty)asdl_seq_GET(
3367 s->v.ExtSlice.dims, i);
3368 if (!compiler_visit_nested_slice(c, sub, ctx))
3369 return 0;
3371 ADDOP_I(c, BUILD_TUPLE, n);
3373 break;
3374 default:
3375 PyErr_Format(PyExc_SystemError,
3376 "invalid subscript kind %d", s->kind);
3377 return 0;
3379 return compiler_handle_subscr(c, kindname, ctx);
3383 /* End of the compiler section, beginning of the assembler section */
3385 /* do depth-first search of basic block graph, starting with block.
3386 post records the block indices in post-order.
3388 XXX must handle implicit jumps from one block to next
3391 struct assembler {
3392 PyObject *a_bytecode; /* string containing bytecode */
3393 int a_offset; /* offset into bytecode */
3394 int a_nblocks; /* number of reachable blocks */
3395 basicblock **a_postorder; /* list of blocks in dfs postorder */
3396 PyObject *a_lnotab; /* string containing lnotab */
3397 int a_lnotab_off; /* offset into lnotab */
3398 int a_lineno; /* last lineno of emitted instruction */
3399 int a_lineno_off; /* bytecode offset of last lineno */
3402 static void
3403 dfs(struct compiler *c, basicblock *b, struct assembler *a)
3405 int i;
3406 struct instr *instr = NULL;
3408 if (b->b_seen)
3409 return;
3410 b->b_seen = 1;
3411 if (b->b_next != NULL)
3412 dfs(c, b->b_next, a);
3413 for (i = 0; i < b->b_iused; i++) {
3414 instr = &b->b_instr[i];
3415 if (instr->i_jrel || instr->i_jabs)
3416 dfs(c, instr->i_target, a);
3418 a->a_postorder[a->a_nblocks++] = b;
3421 static int
3422 stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3424 int i;
3425 struct instr *instr;
3426 if (b->b_seen || b->b_startdepth >= depth)
3427 return maxdepth;
3428 b->b_seen = 1;
3429 b->b_startdepth = depth;
3430 for (i = 0; i < b->b_iused; i++) {
3431 instr = &b->b_instr[i];
3432 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3433 if (depth > maxdepth)
3434 maxdepth = depth;
3435 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3436 if (instr->i_jrel || instr->i_jabs) {
3437 maxdepth = stackdepth_walk(c, instr->i_target,
3438 depth, maxdepth);
3439 if (instr->i_opcode == JUMP_ABSOLUTE ||
3440 instr->i_opcode == JUMP_FORWARD) {
3441 goto out; /* remaining code is dead */
3445 if (b->b_next)
3446 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3447 out:
3448 b->b_seen = 0;
3449 return maxdepth;
3452 /* Find the flow path that needs the largest stack. We assume that
3453 * cycles in the flow graph have no net effect on the stack depth.
3455 static int
3456 stackdepth(struct compiler *c)
3458 basicblock *b, *entryblock;
3459 entryblock = NULL;
3460 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3461 b->b_seen = 0;
3462 b->b_startdepth = INT_MIN;
3463 entryblock = b;
3465 if (!entryblock)
3466 return 0;
3467 return stackdepth_walk(c, entryblock, 0, 0);
3470 static int
3471 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3473 memset(a, 0, sizeof(struct assembler));
3474 a->a_lineno = firstlineno;
3475 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3476 if (!a->a_bytecode)
3477 return 0;
3478 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3479 if (!a->a_lnotab)
3480 return 0;
3481 a->a_postorder = (basicblock **)PyObject_Malloc(
3482 sizeof(basicblock *) * nblocks);
3483 if (!a->a_postorder) {
3484 PyErr_NoMemory();
3485 return 0;
3487 return 1;
3490 static void
3491 assemble_free(struct assembler *a)
3493 Py_XDECREF(a->a_bytecode);
3494 Py_XDECREF(a->a_lnotab);
3495 if (a->a_postorder)
3496 PyObject_Free(a->a_postorder);
3499 /* Return the size of a basic block in bytes. */
3501 static int
3502 instrsize(struct instr *instr)
3504 if (!instr->i_hasarg)
3505 return 1; /* 1 byte for the opcode*/
3506 if (instr->i_oparg > 0xffff)
3507 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3508 return 3; /* 1 (opcode) + 2 (oparg) */
3511 static int
3512 blocksize(basicblock *b)
3514 int i;
3515 int size = 0;
3517 for (i = 0; i < b->b_iused; i++)
3518 size += instrsize(&b->b_instr[i]);
3519 return size;
3522 /* All about a_lnotab.
3524 c_lnotab is an array of unsigned bytes disguised as a Python string.
3525 It is used to map bytecode offsets to source code line #s (when needed
3526 for tracebacks).
3528 The array is conceptually a list of
3529 (bytecode offset increment, line number increment)
3530 pairs. The details are important and delicate, best illustrated by example:
3532 byte code offset source code line number
3535 50 7
3536 350 307
3537 361 308
3539 The first trick is that these numbers aren't stored, only the increments
3540 from one row to the next (this doesn't really work, but it's a start):
3542 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3544 The second trick is that an unsigned byte can't hold negative values, or
3545 values larger than 255, so (a) there's a deep assumption that byte code
3546 offsets and their corresponding line #s both increase monotonically, and (b)
3547 if at least one column jumps by more than 255 from one row to the next, more
3548 than one pair is written to the table. In case #b, there's no way to know
3549 from looking at the table later how many were written. That's the delicate
3550 part. A user of c_lnotab desiring to find the source line number
3551 corresponding to a bytecode address A should do something like this
3553 lineno = addr = 0
3554 for addr_incr, line_incr in c_lnotab:
3555 addr += addr_incr
3556 if addr > A:
3557 return lineno
3558 lineno += line_incr
3560 In order for this to work, when the addr field increments by more than 255,
3561 the line # increment in each pair generated must be 0 until the remaining addr
3562 increment is < 256. So, in the example above, assemble_lnotab (it used
3563 to be called com_set_lineno) should not (as was actually done until 2.2)
3564 expand 300, 300 to 255, 255, 45, 45,
3565 but to 255, 0, 45, 255, 0, 45.
3568 static int
3569 assemble_lnotab(struct assembler *a, struct instr *i)
3571 int d_bytecode, d_lineno;
3572 int len;
3573 unsigned char *lnotab;
3575 d_bytecode = a->a_offset - a->a_lineno_off;
3576 d_lineno = i->i_lineno - a->a_lineno;
3578 assert(d_bytecode >= 0);
3579 assert(d_lineno >= 0);
3581 if(d_bytecode == 0 && d_lineno == 0)
3582 return 1;
3584 if (d_bytecode > 255) {
3585 int j, nbytes, ncodes = d_bytecode / 255;
3586 nbytes = a->a_lnotab_off + 2 * ncodes;
3587 len = PyString_GET_SIZE(a->a_lnotab);
3588 if (nbytes >= len) {
3589 if (len * 2 < nbytes)
3590 len = nbytes;
3591 else
3592 len *= 2;
3593 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3594 return 0;
3596 lnotab = (unsigned char *)
3597 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3598 for (j = 0; j < ncodes; j++) {
3599 *lnotab++ = 255;
3600 *lnotab++ = 0;
3602 d_bytecode -= ncodes * 255;
3603 a->a_lnotab_off += ncodes * 2;
3605 assert(d_bytecode <= 255);
3606 if (d_lineno > 255) {
3607 int j, nbytes, ncodes = d_lineno / 255;
3608 nbytes = a->a_lnotab_off + 2 * ncodes;
3609 len = PyString_GET_SIZE(a->a_lnotab);
3610 if (nbytes >= len) {
3611 if (len * 2 < nbytes)
3612 len = nbytes;
3613 else
3614 len *= 2;
3615 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3616 return 0;
3618 lnotab = (unsigned char *)
3619 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3620 *lnotab++ = d_bytecode;
3621 *lnotab++ = 255;
3622 d_bytecode = 0;
3623 for (j = 1; j < ncodes; j++) {
3624 *lnotab++ = 0;
3625 *lnotab++ = 255;
3627 d_lineno -= ncodes * 255;
3628 a->a_lnotab_off += ncodes * 2;
3631 len = PyString_GET_SIZE(a->a_lnotab);
3632 if (a->a_lnotab_off + 2 >= len) {
3633 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
3634 return 0;
3636 lnotab = (unsigned char *)
3637 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3639 a->a_lnotab_off += 2;
3640 if (d_bytecode) {
3641 *lnotab++ = d_bytecode;
3642 *lnotab++ = d_lineno;
3644 else { /* First line of a block; def stmt, etc. */
3645 *lnotab++ = 0;
3646 *lnotab++ = d_lineno;
3648 a->a_lineno = i->i_lineno;
3649 a->a_lineno_off = a->a_offset;
3650 return 1;
3653 /* assemble_emit()
3654 Extend the bytecode with a new instruction.
3655 Update lnotab if necessary.
3658 static int
3659 assemble_emit(struct assembler *a, struct instr *i)
3661 int size, arg = 0, ext = 0;
3662 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
3663 char *code;
3665 size = instrsize(i);
3666 if (i->i_hasarg) {
3667 arg = i->i_oparg;
3668 ext = arg >> 16;
3670 if (i->i_lineno && !assemble_lnotab(a, i))
3671 return 0;
3672 if (a->a_offset + size >= len) {
3673 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
3674 return 0;
3676 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3677 a->a_offset += size;
3678 if (size == 6) {
3679 assert(i->i_hasarg);
3680 *code++ = (char)EXTENDED_ARG;
3681 *code++ = ext & 0xff;
3682 *code++ = ext >> 8;
3683 arg &= 0xffff;
3685 *code++ = i->i_opcode;
3686 if (i->i_hasarg) {
3687 assert(size == 3 || size == 6);
3688 *code++ = arg & 0xff;
3689 *code++ = arg >> 8;
3691 return 1;
3694 static void
3695 assemble_jump_offsets(struct assembler *a, struct compiler *c)
3697 basicblock *b;
3698 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
3699 int i;
3701 /* Compute the size of each block and fixup jump args.
3702 Replace block pointer with position in bytecode. */
3703 start:
3704 totsize = 0;
3705 for (i = a->a_nblocks - 1; i >= 0; i--) {
3706 b = a->a_postorder[i];
3707 bsize = blocksize(b);
3708 b->b_offset = totsize;
3709 totsize += bsize;
3711 extended_arg_count = 0;
3712 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3713 bsize = b->b_offset;
3714 for (i = 0; i < b->b_iused; i++) {
3715 struct instr *instr = &b->b_instr[i];
3716 /* Relative jumps are computed relative to
3717 the instruction pointer after fetching
3718 the jump instruction.
3720 bsize += instrsize(instr);
3721 if (instr->i_jabs)
3722 instr->i_oparg = instr->i_target->b_offset;
3723 else if (instr->i_jrel) {
3724 int delta = instr->i_target->b_offset - bsize;
3725 instr->i_oparg = delta;
3727 else
3728 continue;
3729 if (instr->i_oparg > 0xffff)
3730 extended_arg_count++;
3734 /* XXX: This is an awful hack that could hurt performance, but
3735 on the bright side it should work until we come up
3736 with a better solution.
3738 In the meantime, should the goto be dropped in favor
3739 of a loop?
3741 The issue is that in the first loop blocksize() is called
3742 which calls instrsize() which requires i_oparg be set
3743 appropriately. There is a bootstrap problem because
3744 i_oparg is calculated in the second loop above.
3746 So we loop until we stop seeing new EXTENDED_ARGs.
3747 The only EXTENDED_ARGs that could be popping up are
3748 ones in jump instructions. So this should converge
3749 fairly quickly.
3751 if (last_extended_arg_count != extended_arg_count) {
3752 last_extended_arg_count = extended_arg_count;
3753 goto start;
3757 static PyObject *
3758 dict_keys_inorder(PyObject *dict, int offset)
3760 PyObject *tuple, *k, *v;
3761 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
3763 tuple = PyTuple_New(size);
3764 if (tuple == NULL)
3765 return NULL;
3766 while (PyDict_Next(dict, &pos, &k, &v)) {
3767 i = PyInt_AS_LONG(v);
3768 k = PyTuple_GET_ITEM(k, 0);
3769 Py_INCREF(k);
3770 assert((i - offset) < size);
3771 assert((i - offset) >= 0);
3772 PyTuple_SET_ITEM(tuple, i - offset, k);
3774 return tuple;
3777 static int
3778 compute_code_flags(struct compiler *c)
3780 PySTEntryObject *ste = c->u->u_ste;
3781 int flags = 0, n;
3782 if (ste->ste_type != ModuleBlock)
3783 flags |= CO_NEWLOCALS;
3784 if (ste->ste_type == FunctionBlock) {
3785 if (!ste->ste_unoptimized)
3786 flags |= CO_OPTIMIZED;
3787 if (ste->ste_nested)
3788 flags |= CO_NESTED;
3789 if (ste->ste_generator)
3790 flags |= CO_GENERATOR;
3792 if (ste->ste_varargs)
3793 flags |= CO_VARARGS;
3794 if (ste->ste_varkeywords)
3795 flags |= CO_VARKEYWORDS;
3796 if (ste->ste_generator)
3797 flags |= CO_GENERATOR;
3799 /* (Only) inherit compilerflags in PyCF_MASK */
3800 flags |= (c->c_flags->cf_flags & PyCF_MASK);
3802 n = PyDict_Size(c->u->u_freevars);
3803 if (n < 0)
3804 return -1;
3805 if (n == 0) {
3806 n = PyDict_Size(c->u->u_cellvars);
3807 if (n < 0)
3808 return -1;
3809 if (n == 0) {
3810 flags |= CO_NOFREE;
3814 return flags;
3817 static PyCodeObject *
3818 makecode(struct compiler *c, struct assembler *a)
3820 PyObject *tmp;
3821 PyCodeObject *co = NULL;
3822 PyObject *consts = NULL;
3823 PyObject *names = NULL;
3824 PyObject *varnames = NULL;
3825 PyObject *filename = NULL;
3826 PyObject *name = NULL;
3827 PyObject *freevars = NULL;
3828 PyObject *cellvars = NULL;
3829 PyObject *bytecode = NULL;
3830 int nlocals, flags;
3832 tmp = dict_keys_inorder(c->u->u_consts, 0);
3833 if (!tmp)
3834 goto error;
3835 consts = PySequence_List(tmp); /* optimize_code requires a list */
3836 Py_DECREF(tmp);
3838 names = dict_keys_inorder(c->u->u_names, 0);
3839 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3840 if (!consts || !names || !varnames)
3841 goto error;
3843 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3844 if (!cellvars)
3845 goto error;
3846 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3847 if (!freevars)
3848 goto error;
3849 filename = PyString_FromString(c->c_filename);
3850 if (!filename)
3851 goto error;
3853 nlocals = PyDict_Size(c->u->u_varnames);
3854 flags = compute_code_flags(c);
3855 if (flags < 0)
3856 goto error;
3858 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3859 if (!bytecode)
3860 goto error;
3862 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3863 if (!tmp)
3864 goto error;
3865 Py_DECREF(consts);
3866 consts = tmp;
3868 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3869 bytecode, consts, names, varnames,
3870 freevars, cellvars,
3871 filename, c->u->u_name,
3872 c->u->u_firstlineno,
3873 a->a_lnotab);
3874 error:
3875 Py_XDECREF(consts);
3876 Py_XDECREF(names);
3877 Py_XDECREF(varnames);
3878 Py_XDECREF(filename);
3879 Py_XDECREF(name);
3880 Py_XDECREF(freevars);
3881 Py_XDECREF(cellvars);
3882 Py_XDECREF(bytecode);
3883 return co;
3887 /* For debugging purposes only */
3888 #if 0
3889 static void
3890 dump_instr(const struct instr *i)
3892 const char *jrel = i->i_jrel ? "jrel " : "";
3893 const char *jabs = i->i_jabs ? "jabs " : "";
3894 char arg[128];
3896 *arg = '\0';
3897 if (i->i_hasarg)
3898 sprintf(arg, "arg: %d ", i->i_oparg);
3900 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3901 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3904 static void
3905 dump_basicblock(const basicblock *b)
3907 const char *seen = b->b_seen ? "seen " : "";
3908 const char *b_return = b->b_return ? "return " : "";
3909 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3910 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3911 if (b->b_instr) {
3912 int i;
3913 for (i = 0; i < b->b_iused; i++) {
3914 fprintf(stderr, " [%02d] ", i);
3915 dump_instr(b->b_instr + i);
3919 #endif
3921 static PyCodeObject *
3922 assemble(struct compiler *c, int addNone)
3924 basicblock *b, *entryblock;
3925 struct assembler a;
3926 int i, j, nblocks;
3927 PyCodeObject *co = NULL;
3929 /* Make sure every block that falls off the end returns None.
3930 XXX NEXT_BLOCK() isn't quite right, because if the last
3931 block ends with a jump or return b_next shouldn't set.
3933 if (!c->u->u_curblock->b_return) {
3934 NEXT_BLOCK(c);
3935 if (addNone)
3936 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3937 ADDOP(c, RETURN_VALUE);
3940 nblocks = 0;
3941 entryblock = NULL;
3942 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3943 nblocks++;
3944 entryblock = b;
3947 /* Set firstlineno if it wasn't explicitly set. */
3948 if (!c->u->u_firstlineno) {
3949 if (entryblock && entryblock->b_instr)
3950 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3951 else
3952 c->u->u_firstlineno = 1;
3954 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3955 goto error;
3956 dfs(c, entryblock, &a);
3958 /* Can't modify the bytecode after computing jump offsets. */
3959 assemble_jump_offsets(&a, c);
3961 /* Emit code in reverse postorder from dfs. */
3962 for (i = a.a_nblocks - 1; i >= 0; i--) {
3963 b = a.a_postorder[i];
3964 for (j = 0; j < b->b_iused; j++)
3965 if (!assemble_emit(&a, &b->b_instr[j]))
3966 goto error;
3969 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
3970 goto error;
3971 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
3972 goto error;
3974 co = makecode(c, &a);
3975 error:
3976 assemble_free(&a);
3977 return co;