Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Python / compile.c
blob1e275390c988ab009da5b2fb832156c08847089f
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 #define COMP_GENEXP 0
43 #define COMP_SETCOMP 1
44 #define COMP_DICTCOMP 2
46 struct instr {
47 unsigned i_jabs : 1;
48 unsigned i_jrel : 1;
49 unsigned i_hasarg : 1;
50 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
56 typedef struct basicblock_ {
57 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
60 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
71 unsigned b_seen : 1;
72 /* b_return is true if a RETURN_VALUE opcode is inserted. */
73 unsigned b_return : 1;
74 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
77 int b_offset;
78 } basicblock;
80 /* fblockinfo tracks the current frame block.
82 A frame block is used to handle loops, try/except, and try/finally.
83 It's called a frame block to distinguish it from a basic block in the
84 compiler IR.
87 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
89 struct fblockinfo {
90 enum fblocktype fb_type;
91 basicblock *fb_block;
94 /* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
97 struct compiler_unit {
98 PySTEntryObject *u_ste;
100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
102 the index of them in co_XXX. The index is used as
103 the argument for opcodes that refer to those collections.
105 PyObject *u_consts; /* all constants */
106 PyObject *u_names; /* all names */
107 PyObject *u_varnames; /* local variables */
108 PyObject *u_cellvars; /* cell variables */
109 PyObject *u_freevars; /* free variables */
111 PyObject *u_private; /* for private name mangling */
113 int u_argcount; /* number of arguments for block */
114 /* Pointer to the most recently allocated block. By following b_list
115 members, you can reach all early allocated blocks. */
116 basicblock *u_blocks;
117 basicblock *u_curblock; /* pointer to current block */
119 int u_nfblocks;
120 struct fblockinfo u_fblock[CO_MAXBLOCKS];
122 int u_firstlineno; /* the first lineno of the block */
123 int u_lineno; /* the lineno for the current stmt */
124 bool u_lineno_set; /* boolean to indicate whether instr
125 has been generated with current lineno */
128 /* This struct captures the global state of a compilation.
130 The u pointer points to the current compilation unit, while units
131 for enclosing blocks are stored in c_stack. The u and c_stack are
132 managed by compiler_enter_scope() and compiler_exit_scope().
135 struct compiler {
136 const char *c_filename;
137 struct symtable *c_st;
138 PyFutureFeatures *c_future; /* pointer to module's __future__ */
139 PyCompilerFlags *c_flags;
141 int c_interactive; /* true if in interactive mode */
142 int c_nestlevel;
144 struct compiler_unit *u; /* compiler state for current block */
145 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
146 PyArena *c_arena; /* pointer to memory allocation arena */
149 static int compiler_enter_scope(struct compiler *, identifier, void *, int);
150 static void compiler_free(struct compiler *);
151 static basicblock *compiler_new_block(struct compiler *);
152 static int compiler_next_instr(struct compiler *, basicblock *);
153 static int compiler_addop(struct compiler *, int);
154 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
155 static int compiler_addop_i(struct compiler *, int, int);
156 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
157 static basicblock *compiler_use_new_block(struct compiler *);
158 static int compiler_error(struct compiler *, const char *);
159 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
161 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
162 static int compiler_visit_stmt(struct compiler *, stmt_ty);
163 static int compiler_visit_keyword(struct compiler *, keyword_ty);
164 static int compiler_visit_expr(struct compiler *, expr_ty);
165 static int compiler_augassign(struct compiler *, stmt_ty);
166 static int compiler_visit_slice(struct compiler *, slice_ty,
167 expr_context_ty);
169 static int compiler_push_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
171 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
172 basicblock *);
173 /* Returns true if there is a loop on the fblock stack. */
174 static int compiler_in_loop(struct compiler *);
176 static int inplace_binop(struct compiler *, operator_ty);
177 static int expr_constant(expr_ty e);
179 static int compiler_with(struct compiler *, stmt_ty);
181 static PyCodeObject *assemble(struct compiler *, int addNone);
182 static PyObject *__doc__;
184 PyObject *
185 _Py_Mangle(PyObject *privateobj, PyObject *ident)
187 /* Name mangling: __private becomes _classname__private.
188 This is independent from how the name is used. */
189 const char *p, *name = PyString_AsString(ident);
190 char *buffer;
191 size_t nlen, plen;
192 if (privateobj == NULL || !PyString_Check(privateobj) ||
193 name == NULL || name[0] != '_' || name[1] != '_') {
194 Py_INCREF(ident);
195 return ident;
197 p = PyString_AsString(privateobj);
198 nlen = strlen(name);
199 /* Don't mangle __id__ or names with dots.
201 The only time a name with a dot can occur is when
202 we are compiling an import statement that has a
203 package name.
205 TODO(jhylton): Decide whether we want to support
206 mangling of the module name, e.g. __M.X.
208 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
209 || strchr(name, '.')) {
210 Py_INCREF(ident);
211 return ident; /* Don't mangle __whatever__ */
213 /* Strip leading underscores from class name */
214 while (*p == '_')
215 p++;
216 if (*p == '\0') {
217 Py_INCREF(ident);
218 return ident; /* Don't mangle if class is just underscores */
220 plen = strlen(p);
222 assert(1 <= PY_SSIZE_T_MAX - nlen);
223 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
225 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
226 if (!ident)
227 return 0;
228 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
229 buffer = PyString_AS_STRING(ident);
230 buffer[0] = '_';
231 strncpy(buffer+1, p, plen);
232 strcpy(buffer+1+plen, name);
233 return ident;
236 static int
237 compiler_init(struct compiler *c)
239 memset(c, 0, sizeof(struct compiler));
241 c->c_stack = PyList_New(0);
242 if (!c->c_stack)
243 return 0;
245 return 1;
248 PyCodeObject *
249 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
250 PyArena *arena)
252 struct compiler c;
253 PyCodeObject *co = NULL;
254 PyCompilerFlags local_flags;
255 int merged;
257 if (!__doc__) {
258 __doc__ = PyString_InternFromString("__doc__");
259 if (!__doc__)
260 return NULL;
263 if (!compiler_init(&c))
264 return NULL;
265 c.c_filename = filename;
266 c.c_arena = arena;
267 c.c_future = PyFuture_FromAST(mod, filename);
268 if (c.c_future == NULL)
269 goto finally;
270 if (!flags) {
271 local_flags.cf_flags = 0;
272 flags = &local_flags;
274 merged = c.c_future->ff_features | flags->cf_flags;
275 c.c_future->ff_features = merged;
276 flags->cf_flags = merged;
277 c.c_flags = flags;
278 c.c_nestlevel = 0;
280 c.c_st = PySymtable_Build(mod, filename, c.c_future);
281 if (c.c_st == NULL) {
282 if (!PyErr_Occurred())
283 PyErr_SetString(PyExc_SystemError, "no symtable");
284 goto finally;
287 co = compiler_mod(&c, mod);
289 finally:
290 compiler_free(&c);
291 assert(co || PyErr_Occurred());
292 return co;
295 PyCodeObject *
296 PyNode_Compile(struct _node *n, const char *filename)
298 PyCodeObject *co = NULL;
299 mod_ty mod;
300 PyArena *arena = PyArena_New();
301 if (!arena)
302 return NULL;
303 mod = PyAST_FromNode(n, NULL, filename, arena);
304 if (mod)
305 co = PyAST_Compile(mod, filename, NULL, arena);
306 PyArena_Free(arena);
307 return co;
310 static void
311 compiler_free(struct compiler *c)
313 if (c->c_st)
314 PySymtable_Free(c->c_st);
315 if (c->c_future)
316 PyObject_Free(c->c_future);
317 Py_DECREF(c->c_stack);
320 static PyObject *
321 list2dict(PyObject *list)
323 Py_ssize_t i, n;
324 PyObject *v, *k;
325 PyObject *dict = PyDict_New();
326 if (!dict) return NULL;
328 n = PyList_Size(list);
329 for (i = 0; i < n; i++) {
330 v = PyInt_FromLong(i);
331 if (!v) {
332 Py_DECREF(dict);
333 return NULL;
335 k = PyList_GET_ITEM(list, i);
336 k = PyTuple_Pack(2, k, k->ob_type);
337 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
338 Py_XDECREF(k);
339 Py_DECREF(v);
340 Py_DECREF(dict);
341 return NULL;
343 Py_DECREF(k);
344 Py_DECREF(v);
346 return dict;
349 /* Return new dict containing names from src that match scope(s).
351 src is a symbol table dictionary. If the scope of a name matches
352 either scope_type or flag is set, insert it into the new dict. The
353 values are integers, starting at offset and increasing by one for
354 each key.
357 static PyObject *
358 dictbytype(PyObject *src, int scope_type, int flag, int offset)
360 Py_ssize_t pos = 0, i = offset, scope;
361 PyObject *k, *v, *dest = PyDict_New();
363 assert(offset >= 0);
364 if (dest == NULL)
365 return NULL;
367 while (PyDict_Next(src, &pos, &k, &v)) {
368 /* XXX this should probably be a macro in symtable.h */
369 assert(PyInt_Check(v));
370 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
372 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
373 PyObject *tuple, *item = PyInt_FromLong(i);
374 if (item == NULL) {
375 Py_DECREF(dest);
376 return NULL;
378 i++;
379 tuple = PyTuple_Pack(2, k, k->ob_type);
380 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
381 Py_DECREF(item);
382 Py_DECREF(dest);
383 Py_XDECREF(tuple);
384 return NULL;
386 Py_DECREF(item);
387 Py_DECREF(tuple);
390 return dest;
393 static void
394 compiler_unit_check(struct compiler_unit *u)
396 basicblock *block;
397 for (block = u->u_blocks; block != NULL; block = block->b_list) {
398 assert((void *)block != (void *)0xcbcbcbcb);
399 assert((void *)block != (void *)0xfbfbfbfb);
400 assert((void *)block != (void *)0xdbdbdbdb);
401 if (block->b_instr != NULL) {
402 assert(block->b_ialloc > 0);
403 assert(block->b_iused > 0);
404 assert(block->b_ialloc >= block->b_iused);
406 else {
407 assert (block->b_iused == 0);
408 assert (block->b_ialloc == 0);
413 static void
414 compiler_unit_free(struct compiler_unit *u)
416 basicblock *b, *next;
418 compiler_unit_check(u);
419 b = u->u_blocks;
420 while (b != NULL) {
421 if (b->b_instr)
422 PyObject_Free((void *)b->b_instr);
423 next = b->b_list;
424 PyObject_Free((void *)b);
425 b = next;
427 Py_CLEAR(u->u_ste);
428 Py_CLEAR(u->u_name);
429 Py_CLEAR(u->u_consts);
430 Py_CLEAR(u->u_names);
431 Py_CLEAR(u->u_varnames);
432 Py_CLEAR(u->u_freevars);
433 Py_CLEAR(u->u_cellvars);
434 Py_CLEAR(u->u_private);
435 PyObject_Free(u);
438 static int
439 compiler_enter_scope(struct compiler *c, identifier name, void *key,
440 int lineno)
442 struct compiler_unit *u;
444 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
445 struct compiler_unit));
446 if (!u) {
447 PyErr_NoMemory();
448 return 0;
450 memset(u, 0, sizeof(struct compiler_unit));
451 u->u_argcount = 0;
452 u->u_ste = PySymtable_Lookup(c->c_st, key);
453 if (!u->u_ste) {
454 compiler_unit_free(u);
455 return 0;
457 Py_INCREF(name);
458 u->u_name = name;
459 u->u_varnames = list2dict(u->u_ste->ste_varnames);
460 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
461 if (!u->u_varnames || !u->u_cellvars) {
462 compiler_unit_free(u);
463 return 0;
466 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
467 PyDict_Size(u->u_cellvars));
468 if (!u->u_freevars) {
469 compiler_unit_free(u);
470 return 0;
473 u->u_blocks = NULL;
474 u->u_nfblocks = 0;
475 u->u_firstlineno = lineno;
476 u->u_lineno = 0;
477 u->u_lineno_set = false;
478 u->u_consts = PyDict_New();
479 if (!u->u_consts) {
480 compiler_unit_free(u);
481 return 0;
483 u->u_names = PyDict_New();
484 if (!u->u_names) {
485 compiler_unit_free(u);
486 return 0;
489 u->u_private = NULL;
491 /* Push the old compiler_unit on the stack. */
492 if (c->u) {
493 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
494 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
495 Py_XDECREF(wrapper);
496 compiler_unit_free(u);
497 return 0;
499 Py_DECREF(wrapper);
500 u->u_private = c->u->u_private;
501 Py_XINCREF(u->u_private);
503 c->u = u;
505 c->c_nestlevel++;
506 if (compiler_use_new_block(c) == NULL)
507 return 0;
509 return 1;
512 static void
513 compiler_exit_scope(struct compiler *c)
515 int n;
516 PyObject *wrapper;
518 c->c_nestlevel--;
519 compiler_unit_free(c->u);
520 /* Restore c->u to the parent unit. */
521 n = PyList_GET_SIZE(c->c_stack) - 1;
522 if (n >= 0) {
523 wrapper = PyList_GET_ITEM(c->c_stack, n);
524 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
525 assert(c->u);
526 /* we are deleting from a list so this really shouldn't fail */
527 if (PySequence_DelItem(c->c_stack, n) < 0)
528 Py_FatalError("compiler_exit_scope()");
529 compiler_unit_check(c->u);
531 else
532 c->u = NULL;
536 /* Allocate a new block and return a pointer to it.
537 Returns NULL on error.
540 static basicblock *
541 compiler_new_block(struct compiler *c)
543 basicblock *b;
544 struct compiler_unit *u;
546 u = c->u;
547 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
548 if (b == NULL) {
549 PyErr_NoMemory();
550 return NULL;
552 memset((void *)b, 0, sizeof(basicblock));
553 /* Extend the singly linked list of blocks with new block. */
554 b->b_list = u->u_blocks;
555 u->u_blocks = b;
556 return b;
559 static basicblock *
560 compiler_use_new_block(struct compiler *c)
562 basicblock *block = compiler_new_block(c);
563 if (block == NULL)
564 return NULL;
565 c->u->u_curblock = block;
566 return block;
569 static basicblock *
570 compiler_next_block(struct compiler *c)
572 basicblock *block = compiler_new_block(c);
573 if (block == NULL)
574 return NULL;
575 c->u->u_curblock->b_next = block;
576 c->u->u_curblock = block;
577 return block;
580 static basicblock *
581 compiler_use_next_block(struct compiler *c, basicblock *block)
583 assert(block != NULL);
584 c->u->u_curblock->b_next = block;
585 c->u->u_curblock = block;
586 return block;
589 /* Returns the offset of the next instruction in the current block's
590 b_instr array. Resizes the b_instr as necessary.
591 Returns -1 on failure.
594 static int
595 compiler_next_instr(struct compiler *c, basicblock *b)
597 assert(b != NULL);
598 if (b->b_instr == NULL) {
599 b->b_instr = (struct instr *)PyObject_Malloc(
600 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
601 if (b->b_instr == NULL) {
602 PyErr_NoMemory();
603 return -1;
605 b->b_ialloc = DEFAULT_BLOCK_SIZE;
606 memset((char *)b->b_instr, 0,
607 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
609 else if (b->b_iused == b->b_ialloc) {
610 struct instr *tmp;
611 size_t oldsize, newsize;
612 oldsize = b->b_ialloc * sizeof(struct instr);
613 newsize = oldsize << 1;
615 if (oldsize > (PY_SIZE_MAX >> 1)) {
616 PyErr_NoMemory();
617 return -1;
620 if (newsize == 0) {
621 PyErr_NoMemory();
622 return -1;
624 b->b_ialloc <<= 1;
625 tmp = (struct instr *)PyObject_Realloc(
626 (void *)b->b_instr, newsize);
627 if (tmp == NULL) {
628 PyErr_NoMemory();
629 return -1;
631 b->b_instr = tmp;
632 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
634 return b->b_iused++;
637 /* Set the i_lineno member of the instruction at offset off if the
638 line number for the current expression/statement has not
639 already been set. If it has been set, the call has no effect.
641 The line number is reset in the following cases:
642 - when entering a new scope
643 - on each statement
644 - on each expression that start a new line
645 - before the "except" clause
646 - before the "for" and "while" expressions
649 static void
650 compiler_set_lineno(struct compiler *c, int off)
652 basicblock *b;
653 if (c->u->u_lineno_set)
654 return;
655 c->u->u_lineno_set = true;
656 b = c->u->u_curblock;
657 b->b_instr[off].i_lineno = c->u->u_lineno;
660 static int
661 opcode_stack_effect(int opcode, int oparg)
663 switch (opcode) {
664 case POP_TOP:
665 return -1;
666 case ROT_TWO:
667 case ROT_THREE:
668 return 0;
669 case DUP_TOP:
670 return 1;
671 case ROT_FOUR:
672 return 0;
674 case UNARY_POSITIVE:
675 case UNARY_NEGATIVE:
676 case UNARY_NOT:
677 case UNARY_CONVERT:
678 case UNARY_INVERT:
679 return 0;
681 case SET_ADD:
682 case LIST_APPEND:
683 return -1;
685 case MAP_ADD:
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 0;
704 case SLICE+1:
705 return -1;
706 case SLICE+2:
707 return -1;
708 case SLICE+3:
709 return -2;
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 SETUP_WITH:
772 return 4;
773 case WITH_CLEANUP:
774 return -1; /* XXX Sometimes more */
775 case LOAD_LOCALS:
776 return 1;
777 case RETURN_VALUE:
778 return -1;
779 case IMPORT_STAR:
780 return -1;
781 case EXEC_STMT:
782 return -3;
783 case YIELD_VALUE:
784 return 0;
786 case POP_BLOCK:
787 return 0;
788 case END_FINALLY:
789 return -3; /* or -1 or -2 if no exception occurred or
790 return/break/continue */
791 case BUILD_CLASS:
792 return -2;
794 case STORE_NAME:
795 return -1;
796 case DELETE_NAME:
797 return 0;
798 case UNPACK_SEQUENCE:
799 return oparg-1;
800 case FOR_ITER:
801 return 1; /* or -1, at end of iterator */
803 case STORE_ATTR:
804 return -2;
805 case DELETE_ATTR:
806 return -1;
807 case STORE_GLOBAL:
808 return -1;
809 case DELETE_GLOBAL:
810 return 0;
811 case DUP_TOPX:
812 return oparg;
813 case LOAD_CONST:
814 return 1;
815 case LOAD_NAME:
816 return 1;
817 case BUILD_TUPLE:
818 case BUILD_LIST:
819 case BUILD_SET:
820 return 1-oparg;
821 case BUILD_MAP:
822 return 1;
823 case LOAD_ATTR:
824 return 0;
825 case COMPARE_OP:
826 return -1;
827 case IMPORT_NAME:
828 return -1;
829 case IMPORT_FROM:
830 return 1;
832 case JUMP_FORWARD:
833 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
834 case JUMP_IF_FALSE_OR_POP: /* "" */
835 case JUMP_ABSOLUTE:
836 return 0;
838 case POP_JUMP_IF_FALSE:
839 case POP_JUMP_IF_TRUE:
840 return -1;
842 case LOAD_GLOBAL:
843 return 1;
845 case CONTINUE_LOOP:
846 return 0;
847 case SETUP_LOOP:
848 case SETUP_EXCEPT:
849 case SETUP_FINALLY:
850 return 0;
852 case LOAD_FAST:
853 return 1;
854 case STORE_FAST:
855 return -1;
856 case DELETE_FAST:
857 return 0;
859 case RAISE_VARARGS:
860 return -oparg;
861 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
862 case CALL_FUNCTION:
863 return -NARGS(oparg);
864 case CALL_FUNCTION_VAR:
865 case CALL_FUNCTION_KW:
866 return -NARGS(oparg)-1;
867 case CALL_FUNCTION_VAR_KW:
868 return -NARGS(oparg)-2;
869 #undef NARGS
870 case MAKE_FUNCTION:
871 return -oparg;
872 case BUILD_SLICE:
873 if (oparg == 3)
874 return -2;
875 else
876 return -1;
878 case MAKE_CLOSURE:
879 return -oparg-1;
880 case LOAD_CLOSURE:
881 return 1;
882 case LOAD_DEREF:
883 return 1;
884 case STORE_DEREF:
885 return -1;
886 default:
887 fprintf(stderr, "opcode = %d\n", opcode);
888 Py_FatalError("opcode_stack_effect()");
891 return 0; /* not reachable */
894 /* Add an opcode with no argument.
895 Returns 0 on failure, 1 on success.
898 static int
899 compiler_addop(struct compiler *c, int opcode)
901 basicblock *b;
902 struct instr *i;
903 int off;
904 off = compiler_next_instr(c, c->u->u_curblock);
905 if (off < 0)
906 return 0;
907 b = c->u->u_curblock;
908 i = &b->b_instr[off];
909 i->i_opcode = opcode;
910 i->i_hasarg = 0;
911 if (opcode == RETURN_VALUE)
912 b->b_return = 1;
913 compiler_set_lineno(c, off);
914 return 1;
917 static int
918 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
920 PyObject *t, *v;
921 Py_ssize_t arg;
922 double d;
924 /* necessary to make sure types aren't coerced (e.g., int and long) */
925 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
926 if (PyFloat_Check(o)) {
927 d = PyFloat_AS_DOUBLE(o);
928 /* all we need is to make the tuple different in either the 0.0
929 * or -0.0 case from all others, just to avoid the "coercion".
931 if (d == 0.0 && copysign(1.0, d) < 0.0)
932 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
933 else
934 t = PyTuple_Pack(2, o, o->ob_type);
936 #ifndef WITHOUT_COMPLEX
937 else if (PyComplex_Check(o)) {
938 Py_complex z;
939 int real_negzero, imag_negzero;
940 /* For the complex case we must make complex(x, 0.)
941 different from complex(x, -0.) and complex(0., y)
942 different from complex(-0., y), for any x and y.
943 All four complex zeros must be distinguished.*/
944 z = PyComplex_AsCComplex(o);
945 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
946 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
947 if (real_negzero && imag_negzero) {
948 t = PyTuple_Pack(5, o, o->ob_type,
949 Py_None, Py_None, Py_None);
951 else if (imag_negzero) {
952 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
954 else if (real_negzero) {
955 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
957 else {
958 t = PyTuple_Pack(2, o, o->ob_type);
961 #endif /* WITHOUT_COMPLEX */
962 else {
963 t = PyTuple_Pack(2, o, o->ob_type);
965 if (t == NULL)
966 return -1;
968 v = PyDict_GetItem(dict, t);
969 if (!v) {
970 arg = PyDict_Size(dict);
971 v = PyInt_FromLong(arg);
972 if (!v) {
973 Py_DECREF(t);
974 return -1;
976 if (PyDict_SetItem(dict, t, v) < 0) {
977 Py_DECREF(t);
978 Py_DECREF(v);
979 return -1;
981 Py_DECREF(v);
983 else
984 arg = PyInt_AsLong(v);
985 Py_DECREF(t);
986 return arg;
989 static int
990 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
991 PyObject *o)
993 int arg = compiler_add_o(c, dict, o);
994 if (arg < 0)
995 return 0;
996 return compiler_addop_i(c, opcode, arg);
999 static int
1000 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1001 PyObject *o)
1003 int arg;
1004 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1005 if (!mangled)
1006 return 0;
1007 arg = compiler_add_o(c, dict, mangled);
1008 Py_DECREF(mangled);
1009 if (arg < 0)
1010 return 0;
1011 return compiler_addop_i(c, opcode, arg);
1014 /* Add an opcode with an integer argument.
1015 Returns 0 on failure, 1 on success.
1018 static int
1019 compiler_addop_i(struct compiler *c, int opcode, int oparg)
1021 struct instr *i;
1022 int off;
1023 off = compiler_next_instr(c, c->u->u_curblock);
1024 if (off < 0)
1025 return 0;
1026 i = &c->u->u_curblock->b_instr[off];
1027 i->i_opcode = opcode;
1028 i->i_oparg = oparg;
1029 i->i_hasarg = 1;
1030 compiler_set_lineno(c, off);
1031 return 1;
1034 static int
1035 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1037 struct instr *i;
1038 int off;
1040 assert(b != NULL);
1041 off = compiler_next_instr(c, c->u->u_curblock);
1042 if (off < 0)
1043 return 0;
1044 i = &c->u->u_curblock->b_instr[off];
1045 i->i_opcode = opcode;
1046 i->i_target = b;
1047 i->i_hasarg = 1;
1048 if (absolute)
1049 i->i_jabs = 1;
1050 else
1051 i->i_jrel = 1;
1052 compiler_set_lineno(c, off);
1053 return 1;
1056 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1057 like to find better names.) NEW_BLOCK() creates a new block and sets
1058 it as the current block. NEXT_BLOCK() also creates an implicit jump
1059 from the current block to the new block.
1062 /* The returns inside these macros make it impossible to decref objects
1063 created in the local function. Local objects should use the arena.
1067 #define NEW_BLOCK(C) { \
1068 if (compiler_use_new_block((C)) == NULL) \
1069 return 0; \
1072 #define NEXT_BLOCK(C) { \
1073 if (compiler_next_block((C)) == NULL) \
1074 return 0; \
1077 #define ADDOP(C, OP) { \
1078 if (!compiler_addop((C), (OP))) \
1079 return 0; \
1082 #define ADDOP_IN_SCOPE(C, OP) { \
1083 if (!compiler_addop((C), (OP))) { \
1084 compiler_exit_scope(c); \
1085 return 0; \
1089 #define ADDOP_O(C, OP, O, TYPE) { \
1090 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1091 return 0; \
1094 #define ADDOP_NAME(C, OP, O, TYPE) { \
1095 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1096 return 0; \
1099 #define ADDOP_I(C, OP, O) { \
1100 if (!compiler_addop_i((C), (OP), (O))) \
1101 return 0; \
1104 #define ADDOP_JABS(C, OP, O) { \
1105 if (!compiler_addop_j((C), (OP), (O), 1)) \
1106 return 0; \
1109 #define ADDOP_JREL(C, OP, O) { \
1110 if (!compiler_addop_j((C), (OP), (O), 0)) \
1111 return 0; \
1114 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1115 the ASDL name to synthesize the name of the C type and the visit function.
1118 #define VISIT(C, TYPE, V) {\
1119 if (!compiler_visit_ ## TYPE((C), (V))) \
1120 return 0; \
1123 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1124 if (!compiler_visit_ ## TYPE((C), (V))) { \
1125 compiler_exit_scope(c); \
1126 return 0; \
1130 #define VISIT_SLICE(C, V, CTX) {\
1131 if (!compiler_visit_slice((C), (V), (CTX))) \
1132 return 0; \
1135 #define VISIT_SEQ(C, TYPE, SEQ) { \
1136 int _i; \
1137 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1138 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1139 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1140 if (!compiler_visit_ ## TYPE((C), elt)) \
1141 return 0; \
1145 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1146 int _i; \
1147 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1148 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1149 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1150 if (!compiler_visit_ ## TYPE((C), elt)) { \
1151 compiler_exit_scope(c); \
1152 return 0; \
1157 static int
1158 compiler_isdocstring(stmt_ty s)
1160 if (s->kind != Expr_kind)
1161 return 0;
1162 return s->v.Expr.value->kind == Str_kind;
1165 /* Compile a sequence of statements, checking for a docstring. */
1167 static int
1168 compiler_body(struct compiler *c, asdl_seq *stmts)
1170 int i = 0;
1171 stmt_ty st;
1173 if (!asdl_seq_LEN(stmts))
1174 return 1;
1175 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1176 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1177 /* don't generate docstrings if -OO */
1178 i = 1;
1179 VISIT(c, expr, st->v.Expr.value);
1180 if (!compiler_nameop(c, __doc__, Store))
1181 return 0;
1183 for (; i < asdl_seq_LEN(stmts); i++)
1184 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1185 return 1;
1188 static PyCodeObject *
1189 compiler_mod(struct compiler *c, mod_ty mod)
1191 PyCodeObject *co;
1192 int addNone = 1;
1193 static PyObject *module;
1194 if (!module) {
1195 module = PyString_InternFromString("<module>");
1196 if (!module)
1197 return NULL;
1199 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1200 if (!compiler_enter_scope(c, module, mod, 0))
1201 return NULL;
1202 switch (mod->kind) {
1203 case Module_kind:
1204 if (!compiler_body(c, mod->v.Module.body)) {
1205 compiler_exit_scope(c);
1206 return 0;
1208 break;
1209 case Interactive_kind:
1210 c->c_interactive = 1;
1211 VISIT_SEQ_IN_SCOPE(c, stmt,
1212 mod->v.Interactive.body);
1213 break;
1214 case Expression_kind:
1215 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1216 addNone = 0;
1217 break;
1218 case Suite_kind:
1219 PyErr_SetString(PyExc_SystemError,
1220 "suite should not be possible");
1221 return 0;
1222 default:
1223 PyErr_Format(PyExc_SystemError,
1224 "module kind %d should not be possible",
1225 mod->kind);
1226 return 0;
1228 co = assemble(c, addNone);
1229 compiler_exit_scope(c);
1230 return co;
1233 /* The test for LOCAL must come before the test for FREE in order to
1234 handle classes where name is both local and free. The local var is
1235 a method and the free var is a free var referenced within a method.
1238 static int
1239 get_ref_type(struct compiler *c, PyObject *name)
1241 int scope = PyST_GetScope(c->u->u_ste, name);
1242 if (scope == 0) {
1243 char buf[350];
1244 PyOS_snprintf(buf, sizeof(buf),
1245 "unknown scope for %.100s in %.100s(%s) in %s\n"
1246 "symbols: %s\nlocals: %s\nglobals: %s",
1247 PyString_AS_STRING(name),
1248 PyString_AS_STRING(c->u->u_name),
1249 PyObject_REPR(c->u->u_ste->ste_id),
1250 c->c_filename,
1251 PyObject_REPR(c->u->u_ste->ste_symbols),
1252 PyObject_REPR(c->u->u_varnames),
1253 PyObject_REPR(c->u->u_names)
1255 Py_FatalError(buf);
1258 return scope;
1261 static int
1262 compiler_lookup_arg(PyObject *dict, PyObject *name)
1264 PyObject *k, *v;
1265 k = PyTuple_Pack(2, name, name->ob_type);
1266 if (k == NULL)
1267 return -1;
1268 v = PyDict_GetItem(dict, k);
1269 Py_DECREF(k);
1270 if (v == NULL)
1271 return -1;
1272 return PyInt_AS_LONG(v);
1275 static int
1276 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1278 int i, free = PyCode_GetNumFree(co);
1279 if (free == 0) {
1280 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1281 ADDOP_I(c, MAKE_FUNCTION, args);
1282 return 1;
1284 for (i = 0; i < free; ++i) {
1285 /* Bypass com_addop_varname because it will generate
1286 LOAD_DEREF but LOAD_CLOSURE is needed.
1288 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1289 int arg, reftype;
1291 /* Special case: If a class contains a method with a
1292 free variable that has the same name as a method,
1293 the name will be considered free *and* local in the
1294 class. It should be handled by the closure, as
1295 well as by the normal name loookup logic.
1297 reftype = get_ref_type(c, name);
1298 if (reftype == CELL)
1299 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1300 else /* (reftype == FREE) */
1301 arg = compiler_lookup_arg(c->u->u_freevars, name);
1302 if (arg == -1) {
1303 printf("lookup %s in %s %d %d\n"
1304 "freevars of %s: %s\n",
1305 PyObject_REPR(name),
1306 PyString_AS_STRING(c->u->u_name),
1307 reftype, arg,
1308 PyString_AS_STRING(co->co_name),
1309 PyObject_REPR(co->co_freevars));
1310 Py_FatalError("compiler_make_closure()");
1312 ADDOP_I(c, LOAD_CLOSURE, arg);
1314 ADDOP_I(c, BUILD_TUPLE, free);
1315 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1316 ADDOP_I(c, MAKE_CLOSURE, args);
1317 return 1;
1320 static int
1321 compiler_decorators(struct compiler *c, asdl_seq* decos)
1323 int i;
1325 if (!decos)
1326 return 1;
1328 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1329 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1331 return 1;
1334 static int
1335 compiler_arguments(struct compiler *c, arguments_ty args)
1337 int i;
1338 int n = asdl_seq_LEN(args->args);
1339 /* Correctly handle nested argument lists */
1340 for (i = 0; i < n; i++) {
1341 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
1342 if (arg->kind == Tuple_kind) {
1343 PyObject *id = PyString_FromFormat(".%d", i);
1344 if (id == NULL) {
1345 return 0;
1347 if (!compiler_nameop(c, id, Load)) {
1348 Py_DECREF(id);
1349 return 0;
1351 Py_DECREF(id);
1352 VISIT(c, expr, arg);
1355 return 1;
1358 static int
1359 compiler_function(struct compiler *c, stmt_ty s)
1361 PyCodeObject *co;
1362 PyObject *first_const = Py_None;
1363 arguments_ty args = s->v.FunctionDef.args;
1364 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1365 stmt_ty st;
1366 int i, n, docstring;
1368 assert(s->kind == FunctionDef_kind);
1370 if (!compiler_decorators(c, decos))
1371 return 0;
1372 if (args->defaults)
1373 VISIT_SEQ(c, expr, args->defaults);
1374 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1375 s->lineno))
1376 return 0;
1378 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1379 docstring = compiler_isdocstring(st);
1380 if (docstring && Py_OptimizeFlag < 2)
1381 first_const = st->v.Expr.value->v.Str.s;
1382 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1383 compiler_exit_scope(c);
1384 return 0;
1387 /* unpack nested arguments */
1388 compiler_arguments(c, args);
1390 c->u->u_argcount = asdl_seq_LEN(args->args);
1391 n = asdl_seq_LEN(s->v.FunctionDef.body);
1392 /* if there was a docstring, we need to skip the first statement */
1393 for (i = docstring; i < n; i++) {
1394 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1395 VISIT_IN_SCOPE(c, stmt, st);
1397 co = assemble(c, 1);
1398 compiler_exit_scope(c);
1399 if (co == NULL)
1400 return 0;
1402 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1403 Py_DECREF(co);
1405 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1406 ADDOP_I(c, CALL_FUNCTION, 1);
1409 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1412 static int
1413 compiler_class(struct compiler *c, stmt_ty s)
1415 int n, i;
1416 PyCodeObject *co;
1417 PyObject *str;
1418 asdl_seq* decos = s->v.ClassDef.decorator_list;
1420 if (!compiler_decorators(c, decos))
1421 return 0;
1423 /* push class name on stack, needed by BUILD_CLASS */
1424 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1425 /* push the tuple of base classes on the stack */
1426 n = asdl_seq_LEN(s->v.ClassDef.bases);
1427 if (n > 0)
1428 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1429 ADDOP_I(c, BUILD_TUPLE, n);
1430 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1431 s->lineno))
1432 return 0;
1433 Py_XDECREF(c->u->u_private);
1434 c->u->u_private = s->v.ClassDef.name;
1435 Py_INCREF(c->u->u_private);
1436 str = PyString_InternFromString("__name__");
1437 if (!str || !compiler_nameop(c, str, Load)) {
1438 Py_XDECREF(str);
1439 compiler_exit_scope(c);
1440 return 0;
1443 Py_DECREF(str);
1444 str = PyString_InternFromString("__module__");
1445 if (!str || !compiler_nameop(c, str, Store)) {
1446 Py_XDECREF(str);
1447 compiler_exit_scope(c);
1448 return 0;
1450 Py_DECREF(str);
1452 if (!compiler_body(c, s->v.ClassDef.body)) {
1453 compiler_exit_scope(c);
1454 return 0;
1457 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1458 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1459 co = assemble(c, 1);
1460 compiler_exit_scope(c);
1461 if (co == NULL)
1462 return 0;
1464 compiler_make_closure(c, co, 0);
1465 Py_DECREF(co);
1467 ADDOP_I(c, CALL_FUNCTION, 0);
1468 ADDOP(c, BUILD_CLASS);
1469 /* apply decorators */
1470 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1471 ADDOP_I(c, CALL_FUNCTION, 1);
1473 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1474 return 0;
1475 return 1;
1478 static int
1479 compiler_ifexp(struct compiler *c, expr_ty e)
1481 basicblock *end, *next;
1483 assert(e->kind == IfExp_kind);
1484 end = compiler_new_block(c);
1485 if (end == NULL)
1486 return 0;
1487 next = compiler_new_block(c);
1488 if (next == NULL)
1489 return 0;
1490 VISIT(c, expr, e->v.IfExp.test);
1491 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1492 VISIT(c, expr, e->v.IfExp.body);
1493 ADDOP_JREL(c, JUMP_FORWARD, end);
1494 compiler_use_next_block(c, next);
1495 VISIT(c, expr, e->v.IfExp.orelse);
1496 compiler_use_next_block(c, end);
1497 return 1;
1500 static int
1501 compiler_lambda(struct compiler *c, expr_ty e)
1503 PyCodeObject *co;
1504 static identifier name;
1505 arguments_ty args = e->v.Lambda.args;
1506 assert(e->kind == Lambda_kind);
1508 if (!name) {
1509 name = PyString_InternFromString("<lambda>");
1510 if (!name)
1511 return 0;
1514 if (args->defaults)
1515 VISIT_SEQ(c, expr, args->defaults);
1516 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1517 return 0;
1519 /* unpack nested arguments */
1520 compiler_arguments(c, args);
1522 c->u->u_argcount = asdl_seq_LEN(args->args);
1523 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1524 if (c->u->u_ste->ste_generator) {
1525 ADDOP_IN_SCOPE(c, POP_TOP);
1527 else {
1528 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1530 co = assemble(c, 1);
1531 compiler_exit_scope(c);
1532 if (co == NULL)
1533 return 0;
1535 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1536 Py_DECREF(co);
1538 return 1;
1541 static int
1542 compiler_print(struct compiler *c, stmt_ty s)
1544 int i, n;
1545 bool dest;
1547 assert(s->kind == Print_kind);
1548 n = asdl_seq_LEN(s->v.Print.values);
1549 dest = false;
1550 if (s->v.Print.dest) {
1551 VISIT(c, expr, s->v.Print.dest);
1552 dest = true;
1554 for (i = 0; i < n; i++) {
1555 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1556 if (dest) {
1557 ADDOP(c, DUP_TOP);
1558 VISIT(c, expr, e);
1559 ADDOP(c, ROT_TWO);
1560 ADDOP(c, PRINT_ITEM_TO);
1562 else {
1563 VISIT(c, expr, e);
1564 ADDOP(c, PRINT_ITEM);
1567 if (s->v.Print.nl) {
1568 if (dest)
1569 ADDOP(c, PRINT_NEWLINE_TO)
1570 else
1571 ADDOP(c, PRINT_NEWLINE)
1573 else if (dest)
1574 ADDOP(c, POP_TOP);
1575 return 1;
1578 static int
1579 compiler_if(struct compiler *c, stmt_ty s)
1581 basicblock *end, *next;
1582 int constant;
1583 assert(s->kind == If_kind);
1584 end = compiler_new_block(c);
1585 if (end == NULL)
1586 return 0;
1588 constant = expr_constant(s->v.If.test);
1589 /* constant = 0: "if 0"
1590 * constant = 1: "if 1", "if 2", ...
1591 * constant = -1: rest */
1592 if (constant == 0) {
1593 if (s->v.If.orelse)
1594 VISIT_SEQ(c, stmt, s->v.If.orelse);
1595 } else if (constant == 1) {
1596 VISIT_SEQ(c, stmt, s->v.If.body);
1597 } else {
1598 if (s->v.If.orelse) {
1599 next = compiler_new_block(c);
1600 if (next == NULL)
1601 return 0;
1603 else
1604 next = end;
1605 VISIT(c, expr, s->v.If.test);
1606 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1607 VISIT_SEQ(c, stmt, s->v.If.body);
1608 ADDOP_JREL(c, JUMP_FORWARD, end);
1609 if (s->v.If.orelse) {
1610 compiler_use_next_block(c, next);
1611 VISIT_SEQ(c, stmt, s->v.If.orelse);
1614 compiler_use_next_block(c, end);
1615 return 1;
1618 static int
1619 compiler_for(struct compiler *c, stmt_ty s)
1621 basicblock *start, *cleanup, *end;
1623 start = compiler_new_block(c);
1624 cleanup = compiler_new_block(c);
1625 end = compiler_new_block(c);
1626 if (start == NULL || end == NULL || cleanup == NULL)
1627 return 0;
1628 ADDOP_JREL(c, SETUP_LOOP, end);
1629 if (!compiler_push_fblock(c, LOOP, start))
1630 return 0;
1631 VISIT(c, expr, s->v.For.iter);
1632 ADDOP(c, GET_ITER);
1633 compiler_use_next_block(c, start);
1634 ADDOP_JREL(c, FOR_ITER, cleanup);
1635 VISIT(c, expr, s->v.For.target);
1636 VISIT_SEQ(c, stmt, s->v.For.body);
1637 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1638 compiler_use_next_block(c, cleanup);
1639 ADDOP(c, POP_BLOCK);
1640 compiler_pop_fblock(c, LOOP, start);
1641 VISIT_SEQ(c, stmt, s->v.For.orelse);
1642 compiler_use_next_block(c, end);
1643 return 1;
1646 static int
1647 compiler_while(struct compiler *c, stmt_ty s)
1649 basicblock *loop, *orelse, *end, *anchor = NULL;
1650 int constant = expr_constant(s->v.While.test);
1652 if (constant == 0) {
1653 if (s->v.While.orelse)
1654 VISIT_SEQ(c, stmt, s->v.While.orelse);
1655 return 1;
1657 loop = compiler_new_block(c);
1658 end = compiler_new_block(c);
1659 if (constant == -1) {
1660 anchor = compiler_new_block(c);
1661 if (anchor == NULL)
1662 return 0;
1664 if (loop == NULL || end == NULL)
1665 return 0;
1666 if (s->v.While.orelse) {
1667 orelse = compiler_new_block(c);
1668 if (orelse == NULL)
1669 return 0;
1671 else
1672 orelse = NULL;
1674 ADDOP_JREL(c, SETUP_LOOP, end);
1675 compiler_use_next_block(c, loop);
1676 if (!compiler_push_fblock(c, LOOP, loop))
1677 return 0;
1678 if (constant == -1) {
1679 VISIT(c, expr, s->v.While.test);
1680 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1682 VISIT_SEQ(c, stmt, s->v.While.body);
1683 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1685 /* XXX should the two POP instructions be in a separate block
1686 if there is no else clause ?
1689 if (constant == -1) {
1690 compiler_use_next_block(c, anchor);
1691 ADDOP(c, POP_BLOCK);
1693 compiler_pop_fblock(c, LOOP, loop);
1694 if (orelse != NULL) /* what if orelse is just pass? */
1695 VISIT_SEQ(c, stmt, s->v.While.orelse);
1696 compiler_use_next_block(c, end);
1698 return 1;
1701 static int
1702 compiler_continue(struct compiler *c)
1704 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1705 static const char IN_FINALLY_ERROR_MSG[] =
1706 "'continue' not supported inside 'finally' clause";
1707 int i;
1709 if (!c->u->u_nfblocks)
1710 return compiler_error(c, LOOP_ERROR_MSG);
1711 i = c->u->u_nfblocks - 1;
1712 switch (c->u->u_fblock[i].fb_type) {
1713 case LOOP:
1714 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1715 break;
1716 case EXCEPT:
1717 case FINALLY_TRY:
1718 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1719 /* Prevent continue anywhere under a finally
1720 even if hidden in a sub-try or except. */
1721 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1722 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1724 if (i == -1)
1725 return compiler_error(c, LOOP_ERROR_MSG);
1726 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1727 break;
1728 case FINALLY_END:
1729 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1732 return 1;
1735 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1737 SETUP_FINALLY L
1738 <code for body>
1739 POP_BLOCK
1740 LOAD_CONST <None>
1741 L: <code for finalbody>
1742 END_FINALLY
1744 The special instructions use the block stack. Each block
1745 stack entry contains the instruction that created it (here
1746 SETUP_FINALLY), the level of the value stack at the time the
1747 block stack entry was created, and a label (here L).
1749 SETUP_FINALLY:
1750 Pushes the current value stack level and the label
1751 onto the block stack.
1752 POP_BLOCK:
1753 Pops en entry from the block stack, and pops the value
1754 stack until its level is the same as indicated on the
1755 block stack. (The label is ignored.)
1756 END_FINALLY:
1757 Pops a variable number of entries from the *value* stack
1758 and re-raises the exception they specify. The number of
1759 entries popped depends on the (pseudo) exception type.
1761 The block stack is unwound when an exception is raised:
1762 when a SETUP_FINALLY entry is found, the exception is pushed
1763 onto the value stack (and the exception condition is cleared),
1764 and the interpreter jumps to the label gotten from the block
1765 stack.
1768 static int
1769 compiler_try_finally(struct compiler *c, stmt_ty s)
1771 basicblock *body, *end;
1772 body = compiler_new_block(c);
1773 end = compiler_new_block(c);
1774 if (body == NULL || end == NULL)
1775 return 0;
1777 ADDOP_JREL(c, SETUP_FINALLY, end);
1778 compiler_use_next_block(c, body);
1779 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1780 return 0;
1781 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1782 ADDOP(c, POP_BLOCK);
1783 compiler_pop_fblock(c, FINALLY_TRY, body);
1785 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1786 compiler_use_next_block(c, end);
1787 if (!compiler_push_fblock(c, FINALLY_END, end))
1788 return 0;
1789 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1790 ADDOP(c, END_FINALLY);
1791 compiler_pop_fblock(c, FINALLY_END, end);
1793 return 1;
1797 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1798 (The contents of the value stack is shown in [], with the top
1799 at the right; 'tb' is trace-back info, 'val' the exception's
1800 associated value, and 'exc' the exception.)
1802 Value stack Label Instruction Argument
1803 [] SETUP_EXCEPT L1
1804 [] <code for S>
1805 [] POP_BLOCK
1806 [] JUMP_FORWARD L0
1808 [tb, val, exc] L1: DUP )
1809 [tb, val, exc, exc] <evaluate E1> )
1810 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1811 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1812 [tb, val, exc] POP
1813 [tb, val] <assign to V1> (or POP if no V1)
1814 [tb] POP
1815 [] <code for S1>
1816 JUMP_FORWARD L0
1818 [tb, val, exc] L2: DUP
1819 .............................etc.......................
1821 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1823 [] L0: <next statement>
1825 Of course, parts are not generated if Vi or Ei is not present.
1827 static int
1828 compiler_try_except(struct compiler *c, stmt_ty s)
1830 basicblock *body, *orelse, *except, *end;
1831 int i, n;
1833 body = compiler_new_block(c);
1834 except = compiler_new_block(c);
1835 orelse = compiler_new_block(c);
1836 end = compiler_new_block(c);
1837 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1838 return 0;
1839 ADDOP_JREL(c, SETUP_EXCEPT, except);
1840 compiler_use_next_block(c, body);
1841 if (!compiler_push_fblock(c, EXCEPT, body))
1842 return 0;
1843 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1844 ADDOP(c, POP_BLOCK);
1845 compiler_pop_fblock(c, EXCEPT, body);
1846 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1847 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1848 compiler_use_next_block(c, except);
1849 for (i = 0; i < n; i++) {
1850 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1851 s->v.TryExcept.handlers, i);
1852 if (!handler->v.ExceptHandler.type && i < n-1)
1853 return compiler_error(c, "default 'except:' must be last");
1854 c->u->u_lineno_set = false;
1855 c->u->u_lineno = handler->lineno;
1856 except = compiler_new_block(c);
1857 if (except == NULL)
1858 return 0;
1859 if (handler->v.ExceptHandler.type) {
1860 ADDOP(c, DUP_TOP);
1861 VISIT(c, expr, handler->v.ExceptHandler.type);
1862 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1863 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1865 ADDOP(c, POP_TOP);
1866 if (handler->v.ExceptHandler.name) {
1867 VISIT(c, expr, handler->v.ExceptHandler.name);
1869 else {
1870 ADDOP(c, POP_TOP);
1872 ADDOP(c, POP_TOP);
1873 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
1874 ADDOP_JREL(c, JUMP_FORWARD, end);
1875 compiler_use_next_block(c, except);
1877 ADDOP(c, END_FINALLY);
1878 compiler_use_next_block(c, orelse);
1879 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
1880 compiler_use_next_block(c, end);
1881 return 1;
1884 static int
1885 compiler_import_as(struct compiler *c, identifier name, identifier asname)
1887 /* The IMPORT_NAME opcode was already generated. This function
1888 merely needs to bind the result to a name.
1890 If there is a dot in name, we need to split it and emit a
1891 LOAD_ATTR for each name.
1893 const char *src = PyString_AS_STRING(name);
1894 const char *dot = strchr(src, '.');
1895 if (dot) {
1896 /* Consume the base module name to get the first attribute */
1897 src = dot + 1;
1898 while (dot) {
1899 /* NB src is only defined when dot != NULL */
1900 PyObject *attr;
1901 dot = strchr(src, '.');
1902 attr = PyString_FromStringAndSize(src,
1903 dot ? dot - src : strlen(src));
1904 if (!attr)
1905 return -1;
1906 ADDOP_O(c, LOAD_ATTR, attr, names);
1907 Py_DECREF(attr);
1908 src = dot + 1;
1911 return compiler_nameop(c, asname, Store);
1914 static int
1915 compiler_import(struct compiler *c, stmt_ty s)
1917 /* The Import node stores a module name like a.b.c as a single
1918 string. This is convenient for all cases except
1919 import a.b.c as d
1920 where we need to parse that string to extract the individual
1921 module names.
1922 XXX Perhaps change the representation to make this case simpler?
1924 int i, n = asdl_seq_LEN(s->v.Import.names);
1926 for (i = 0; i < n; i++) {
1927 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
1928 int r;
1929 PyObject *level;
1931 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1932 level = PyInt_FromLong(0);
1933 else
1934 level = PyInt_FromLong(-1);
1936 if (level == NULL)
1937 return 0;
1939 ADDOP_O(c, LOAD_CONST, level, consts);
1940 Py_DECREF(level);
1941 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1942 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1944 if (alias->asname) {
1945 r = compiler_import_as(c, alias->name, alias->asname);
1946 if (!r)
1947 return r;
1949 else {
1950 identifier tmp = alias->name;
1951 const char *base = PyString_AS_STRING(alias->name);
1952 char *dot = strchr(base, '.');
1953 if (dot)
1954 tmp = PyString_FromStringAndSize(base,
1955 dot - base);
1956 r = compiler_nameop(c, tmp, Store);
1957 if (dot) {
1958 Py_DECREF(tmp);
1960 if (!r)
1961 return r;
1964 return 1;
1967 static int
1968 compiler_from_import(struct compiler *c, stmt_ty s)
1970 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
1972 PyObject *names = PyTuple_New(n);
1973 PyObject *level;
1974 static PyObject *empty_string;
1976 if (!empty_string) {
1977 empty_string = PyString_FromString("");
1978 if (!empty_string)
1979 return 0;
1982 if (!names)
1983 return 0;
1985 if (s->v.ImportFrom.level == 0 && c->c_flags &&
1986 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1987 level = PyInt_FromLong(-1);
1988 else
1989 level = PyInt_FromLong(s->v.ImportFrom.level);
1991 if (!level) {
1992 Py_DECREF(names);
1993 return 0;
1996 /* build up the names */
1997 for (i = 0; i < n; i++) {
1998 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
1999 Py_INCREF(alias->name);
2000 PyTuple_SET_ITEM(names, i, alias->name);
2003 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2004 !strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {
2005 Py_DECREF(level);
2006 Py_DECREF(names);
2007 return compiler_error(c, "from __future__ imports must occur "
2008 "at the beginning of the file");
2011 ADDOP_O(c, LOAD_CONST, level, consts);
2012 Py_DECREF(level);
2013 ADDOP_O(c, LOAD_CONST, names, consts);
2014 Py_DECREF(names);
2015 if (s->v.ImportFrom.module) {
2016 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2018 else {
2019 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2021 for (i = 0; i < n; i++) {
2022 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2023 identifier store_name;
2025 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2026 assert(n == 1);
2027 ADDOP(c, IMPORT_STAR);
2028 return 1;
2031 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2032 store_name = alias->name;
2033 if (alias->asname)
2034 store_name = alias->asname;
2036 if (!compiler_nameop(c, store_name, Store)) {
2037 Py_DECREF(names);
2038 return 0;
2041 /* remove imported module */
2042 ADDOP(c, POP_TOP);
2043 return 1;
2046 static int
2047 compiler_assert(struct compiler *c, stmt_ty s)
2049 static PyObject *assertion_error = NULL;
2050 basicblock *end;
2052 if (Py_OptimizeFlag)
2053 return 1;
2054 if (assertion_error == NULL) {
2055 assertion_error = PyString_InternFromString("AssertionError");
2056 if (assertion_error == NULL)
2057 return 0;
2059 if (s->v.Assert.test->kind == Tuple_kind &&
2060 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2061 const char* msg =
2062 "assertion is always true, perhaps remove parentheses?";
2063 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2064 c->u->u_lineno, NULL, NULL) == -1)
2065 return 0;
2067 VISIT(c, expr, s->v.Assert.test);
2068 end = compiler_new_block(c);
2069 if (end == NULL)
2070 return 0;
2071 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2072 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2073 if (s->v.Assert.msg) {
2074 VISIT(c, expr, s->v.Assert.msg);
2075 ADDOP_I(c, RAISE_VARARGS, 2);
2077 else {
2078 ADDOP_I(c, RAISE_VARARGS, 1);
2080 compiler_use_next_block(c, end);
2081 return 1;
2084 static int
2085 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2087 int i, n;
2089 /* Always assign a lineno to the next instruction for a stmt. */
2090 c->u->u_lineno = s->lineno;
2091 c->u->u_lineno_set = false;
2093 switch (s->kind) {
2094 case FunctionDef_kind:
2095 return compiler_function(c, s);
2096 case ClassDef_kind:
2097 return compiler_class(c, s);
2098 case Return_kind:
2099 if (c->u->u_ste->ste_type != FunctionBlock)
2100 return compiler_error(c, "'return' outside function");
2101 if (s->v.Return.value) {
2102 VISIT(c, expr, s->v.Return.value);
2104 else
2105 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2106 ADDOP(c, RETURN_VALUE);
2107 break;
2108 case Delete_kind:
2109 VISIT_SEQ(c, expr, s->v.Delete.targets)
2110 break;
2111 case Assign_kind:
2112 n = asdl_seq_LEN(s->v.Assign.targets);
2113 VISIT(c, expr, s->v.Assign.value);
2114 for (i = 0; i < n; i++) {
2115 if (i < n - 1)
2116 ADDOP(c, DUP_TOP);
2117 VISIT(c, expr,
2118 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2120 break;
2121 case AugAssign_kind:
2122 return compiler_augassign(c, s);
2123 case Print_kind:
2124 return compiler_print(c, s);
2125 case For_kind:
2126 return compiler_for(c, s);
2127 case While_kind:
2128 return compiler_while(c, s);
2129 case If_kind:
2130 return compiler_if(c, s);
2131 case Raise_kind:
2132 n = 0;
2133 if (s->v.Raise.type) {
2134 VISIT(c, expr, s->v.Raise.type);
2135 n++;
2136 if (s->v.Raise.inst) {
2137 VISIT(c, expr, s->v.Raise.inst);
2138 n++;
2139 if (s->v.Raise.tback) {
2140 VISIT(c, expr, s->v.Raise.tback);
2141 n++;
2145 ADDOP_I(c, RAISE_VARARGS, n);
2146 break;
2147 case TryExcept_kind:
2148 return compiler_try_except(c, s);
2149 case TryFinally_kind:
2150 return compiler_try_finally(c, s);
2151 case Assert_kind:
2152 return compiler_assert(c, s);
2153 case Import_kind:
2154 return compiler_import(c, s);
2155 case ImportFrom_kind:
2156 return compiler_from_import(c, s);
2157 case Exec_kind:
2158 VISIT(c, expr, s->v.Exec.body);
2159 if (s->v.Exec.globals) {
2160 VISIT(c, expr, s->v.Exec.globals);
2161 if (s->v.Exec.locals) {
2162 VISIT(c, expr, s->v.Exec.locals);
2163 } else {
2164 ADDOP(c, DUP_TOP);
2166 } else {
2167 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2168 ADDOP(c, DUP_TOP);
2170 ADDOP(c, EXEC_STMT);
2171 break;
2172 case Global_kind:
2173 break;
2174 case Expr_kind:
2175 if (c->c_interactive && c->c_nestlevel <= 1) {
2176 VISIT(c, expr, s->v.Expr.value);
2177 ADDOP(c, PRINT_EXPR);
2179 else if (s->v.Expr.value->kind != Str_kind &&
2180 s->v.Expr.value->kind != Num_kind) {
2181 VISIT(c, expr, s->v.Expr.value);
2182 ADDOP(c, POP_TOP);
2184 break;
2185 case Pass_kind:
2186 break;
2187 case Break_kind:
2188 if (!compiler_in_loop(c))
2189 return compiler_error(c, "'break' outside loop");
2190 ADDOP(c, BREAK_LOOP);
2191 break;
2192 case Continue_kind:
2193 return compiler_continue(c);
2194 case With_kind:
2195 return compiler_with(c, s);
2197 return 1;
2200 static int
2201 unaryop(unaryop_ty op)
2203 switch (op) {
2204 case Invert:
2205 return UNARY_INVERT;
2206 case Not:
2207 return UNARY_NOT;
2208 case UAdd:
2209 return UNARY_POSITIVE;
2210 case USub:
2211 return UNARY_NEGATIVE;
2212 default:
2213 PyErr_Format(PyExc_SystemError,
2214 "unary op %d should not be possible", op);
2215 return 0;
2219 static int
2220 binop(struct compiler *c, operator_ty op)
2222 switch (op) {
2223 case Add:
2224 return BINARY_ADD;
2225 case Sub:
2226 return BINARY_SUBTRACT;
2227 case Mult:
2228 return BINARY_MULTIPLY;
2229 case Div:
2230 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2231 return BINARY_TRUE_DIVIDE;
2232 else
2233 return BINARY_DIVIDE;
2234 case Mod:
2235 return BINARY_MODULO;
2236 case Pow:
2237 return BINARY_POWER;
2238 case LShift:
2239 return BINARY_LSHIFT;
2240 case RShift:
2241 return BINARY_RSHIFT;
2242 case BitOr:
2243 return BINARY_OR;
2244 case BitXor:
2245 return BINARY_XOR;
2246 case BitAnd:
2247 return BINARY_AND;
2248 case FloorDiv:
2249 return BINARY_FLOOR_DIVIDE;
2250 default:
2251 PyErr_Format(PyExc_SystemError,
2252 "binary op %d should not be possible", op);
2253 return 0;
2257 static int
2258 cmpop(cmpop_ty op)
2260 switch (op) {
2261 case Eq:
2262 return PyCmp_EQ;
2263 case NotEq:
2264 return PyCmp_NE;
2265 case Lt:
2266 return PyCmp_LT;
2267 case LtE:
2268 return PyCmp_LE;
2269 case Gt:
2270 return PyCmp_GT;
2271 case GtE:
2272 return PyCmp_GE;
2273 case Is:
2274 return PyCmp_IS;
2275 case IsNot:
2276 return PyCmp_IS_NOT;
2277 case In:
2278 return PyCmp_IN;
2279 case NotIn:
2280 return PyCmp_NOT_IN;
2281 default:
2282 return PyCmp_BAD;
2286 static int
2287 inplace_binop(struct compiler *c, operator_ty op)
2289 switch (op) {
2290 case Add:
2291 return INPLACE_ADD;
2292 case Sub:
2293 return INPLACE_SUBTRACT;
2294 case Mult:
2295 return INPLACE_MULTIPLY;
2296 case Div:
2297 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2298 return INPLACE_TRUE_DIVIDE;
2299 else
2300 return INPLACE_DIVIDE;
2301 case Mod:
2302 return INPLACE_MODULO;
2303 case Pow:
2304 return INPLACE_POWER;
2305 case LShift:
2306 return INPLACE_LSHIFT;
2307 case RShift:
2308 return INPLACE_RSHIFT;
2309 case BitOr:
2310 return INPLACE_OR;
2311 case BitXor:
2312 return INPLACE_XOR;
2313 case BitAnd:
2314 return INPLACE_AND;
2315 case FloorDiv:
2316 return INPLACE_FLOOR_DIVIDE;
2317 default:
2318 PyErr_Format(PyExc_SystemError,
2319 "inplace binary op %d should not be possible", op);
2320 return 0;
2324 static int
2325 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2327 int op, scope, arg;
2328 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2330 PyObject *dict = c->u->u_names;
2331 PyObject *mangled;
2332 /* XXX AugStore isn't used anywhere! */
2334 mangled = _Py_Mangle(c->u->u_private, name);
2335 if (!mangled)
2336 return 0;
2338 op = 0;
2339 optype = OP_NAME;
2340 scope = PyST_GetScope(c->u->u_ste, mangled);
2341 switch (scope) {
2342 case FREE:
2343 dict = c->u->u_freevars;
2344 optype = OP_DEREF;
2345 break;
2346 case CELL:
2347 dict = c->u->u_cellvars;
2348 optype = OP_DEREF;
2349 break;
2350 case LOCAL:
2351 if (c->u->u_ste->ste_type == FunctionBlock)
2352 optype = OP_FAST;
2353 break;
2354 case GLOBAL_IMPLICIT:
2355 if (c->u->u_ste->ste_type == FunctionBlock &&
2356 !c->u->u_ste->ste_unoptimized)
2357 optype = OP_GLOBAL;
2358 break;
2359 case GLOBAL_EXPLICIT:
2360 optype = OP_GLOBAL;
2361 break;
2362 default:
2363 /* scope can be 0 */
2364 break;
2367 /* XXX Leave assert here, but handle __doc__ and the like better */
2368 assert(scope || PyString_AS_STRING(name)[0] == '_');
2370 switch (optype) {
2371 case OP_DEREF:
2372 switch (ctx) {
2373 case Load: op = LOAD_DEREF; break;
2374 case Store: op = STORE_DEREF; break;
2375 case AugLoad:
2376 case AugStore:
2377 break;
2378 case Del:
2379 PyErr_Format(PyExc_SyntaxError,
2380 "can not delete variable '%s' referenced "
2381 "in nested scope",
2382 PyString_AS_STRING(name));
2383 Py_DECREF(mangled);
2384 return 0;
2385 case Param:
2386 default:
2387 PyErr_SetString(PyExc_SystemError,
2388 "param invalid for deref variable");
2389 return 0;
2391 break;
2392 case OP_FAST:
2393 switch (ctx) {
2394 case Load: op = LOAD_FAST; break;
2395 case Store: op = STORE_FAST; break;
2396 case Del: op = DELETE_FAST; break;
2397 case AugLoad:
2398 case AugStore:
2399 break;
2400 case Param:
2401 default:
2402 PyErr_SetString(PyExc_SystemError,
2403 "param invalid for local variable");
2404 return 0;
2406 ADDOP_O(c, op, mangled, varnames);
2407 Py_DECREF(mangled);
2408 return 1;
2409 case OP_GLOBAL:
2410 switch (ctx) {
2411 case Load: op = LOAD_GLOBAL; break;
2412 case Store: op = STORE_GLOBAL; break;
2413 case Del: op = DELETE_GLOBAL; break;
2414 case AugLoad:
2415 case AugStore:
2416 break;
2417 case Param:
2418 default:
2419 PyErr_SetString(PyExc_SystemError,
2420 "param invalid for global variable");
2421 return 0;
2423 break;
2424 case OP_NAME:
2425 switch (ctx) {
2426 case Load: op = LOAD_NAME; break;
2427 case Store: op = STORE_NAME; break;
2428 case Del: op = DELETE_NAME; break;
2429 case AugLoad:
2430 case AugStore:
2431 break;
2432 case Param:
2433 default:
2434 PyErr_SetString(PyExc_SystemError,
2435 "param invalid for name variable");
2436 return 0;
2438 break;
2441 assert(op);
2442 arg = compiler_add_o(c, dict, mangled);
2443 Py_DECREF(mangled);
2444 if (arg < 0)
2445 return 0;
2446 return compiler_addop_i(c, op, arg);
2449 static int
2450 compiler_boolop(struct compiler *c, expr_ty e)
2452 basicblock *end;
2453 int jumpi, i, n;
2454 asdl_seq *s;
2456 assert(e->kind == BoolOp_kind);
2457 if (e->v.BoolOp.op == And)
2458 jumpi = JUMP_IF_FALSE_OR_POP;
2459 else
2460 jumpi = JUMP_IF_TRUE_OR_POP;
2461 end = compiler_new_block(c);
2462 if (end == NULL)
2463 return 0;
2464 s = e->v.BoolOp.values;
2465 n = asdl_seq_LEN(s) - 1;
2466 assert(n >= 0);
2467 for (i = 0; i < n; ++i) {
2468 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2469 ADDOP_JABS(c, jumpi, end);
2471 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2472 compiler_use_next_block(c, end);
2473 return 1;
2476 static int
2477 compiler_list(struct compiler *c, expr_ty e)
2479 int n = asdl_seq_LEN(e->v.List.elts);
2480 if (e->v.List.ctx == Store) {
2481 ADDOP_I(c, UNPACK_SEQUENCE, n);
2483 VISIT_SEQ(c, expr, e->v.List.elts);
2484 if (e->v.List.ctx == Load) {
2485 ADDOP_I(c, BUILD_LIST, n);
2487 return 1;
2490 static int
2491 compiler_tuple(struct compiler *c, expr_ty e)
2493 int n = asdl_seq_LEN(e->v.Tuple.elts);
2494 if (e->v.Tuple.ctx == Store) {
2495 ADDOP_I(c, UNPACK_SEQUENCE, n);
2497 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2498 if (e->v.Tuple.ctx == Load) {
2499 ADDOP_I(c, BUILD_TUPLE, n);
2501 return 1;
2504 static int
2505 compiler_compare(struct compiler *c, expr_ty e)
2507 int i, n;
2508 basicblock *cleanup = NULL;
2510 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2511 VISIT(c, expr, e->v.Compare.left);
2512 n = asdl_seq_LEN(e->v.Compare.ops);
2513 assert(n > 0);
2514 if (n > 1) {
2515 cleanup = compiler_new_block(c);
2516 if (cleanup == NULL)
2517 return 0;
2518 VISIT(c, expr,
2519 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2521 for (i = 1; i < n; i++) {
2522 ADDOP(c, DUP_TOP);
2523 ADDOP(c, ROT_THREE);
2524 ADDOP_I(c, COMPARE_OP,
2525 cmpop((cmpop_ty)(asdl_seq_GET(
2526 e->v.Compare.ops, i - 1))));
2527 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2528 NEXT_BLOCK(c);
2529 if (i < (n - 1))
2530 VISIT(c, expr,
2531 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2533 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2534 ADDOP_I(c, COMPARE_OP,
2535 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2536 if (n > 1) {
2537 basicblock *end = compiler_new_block(c);
2538 if (end == NULL)
2539 return 0;
2540 ADDOP_JREL(c, JUMP_FORWARD, end);
2541 compiler_use_next_block(c, cleanup);
2542 ADDOP(c, ROT_TWO);
2543 ADDOP(c, POP_TOP);
2544 compiler_use_next_block(c, end);
2546 return 1;
2549 static int
2550 compiler_call(struct compiler *c, expr_ty e)
2552 int n, code = 0;
2554 VISIT(c, expr, e->v.Call.func);
2555 n = asdl_seq_LEN(e->v.Call.args);
2556 VISIT_SEQ(c, expr, e->v.Call.args);
2557 if (e->v.Call.keywords) {
2558 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2559 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2561 if (e->v.Call.starargs) {
2562 VISIT(c, expr, e->v.Call.starargs);
2563 code |= 1;
2565 if (e->v.Call.kwargs) {
2566 VISIT(c, expr, e->v.Call.kwargs);
2567 code |= 2;
2569 switch (code) {
2570 case 0:
2571 ADDOP_I(c, CALL_FUNCTION, n);
2572 break;
2573 case 1:
2574 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2575 break;
2576 case 2:
2577 ADDOP_I(c, CALL_FUNCTION_KW, n);
2578 break;
2579 case 3:
2580 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2581 break;
2583 return 1;
2586 static int
2587 compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
2588 int gen_index, expr_ty elt)
2590 /* generate code for the iterator, then each of the ifs,
2591 and then write to the element */
2593 comprehension_ty l;
2594 basicblock *start, *anchor, *skip, *if_cleanup;
2595 int i, n;
2597 start = compiler_new_block(c);
2598 skip = compiler_new_block(c);
2599 if_cleanup = compiler_new_block(c);
2600 anchor = compiler_new_block(c);
2602 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2603 anchor == NULL)
2604 return 0;
2606 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2607 VISIT(c, expr, l->iter);
2608 ADDOP(c, GET_ITER);
2609 compiler_use_next_block(c, start);
2610 ADDOP_JREL(c, FOR_ITER, anchor);
2611 NEXT_BLOCK(c);
2612 VISIT(c, expr, l->target);
2614 /* XXX this needs to be cleaned up...a lot! */
2615 n = asdl_seq_LEN(l->ifs);
2616 for (i = 0; i < n; i++) {
2617 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
2618 VISIT(c, expr, e);
2619 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2620 NEXT_BLOCK(c);
2623 if (++gen_index < asdl_seq_LEN(generators))
2624 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
2625 return 0;
2627 /* only append after the last for generator */
2628 if (gen_index >= asdl_seq_LEN(generators)) {
2629 VISIT(c, expr, elt);
2630 ADDOP_I(c, LIST_APPEND, gen_index+1);
2632 compiler_use_next_block(c, skip);
2634 compiler_use_next_block(c, if_cleanup);
2635 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2636 compiler_use_next_block(c, anchor);
2638 return 1;
2641 static int
2642 compiler_listcomp(struct compiler *c, expr_ty e)
2644 assert(e->kind == ListComp_kind);
2645 ADDOP_I(c, BUILD_LIST, 0);
2646 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2647 e->v.ListComp.elt);
2650 /* Dict and set comprehensions and generator expressions work by creating a
2651 nested function to perform the actual iteration. This means that the
2652 iteration variables don't leak into the current scope.
2653 The defined function is called immediately following its definition, with the
2654 result of that call being the result of the expression.
2655 The LC/SC version returns the populated container, while the GE version is
2656 flagged in symtable.c as a generator, so it returns the generator object
2657 when the function is called.
2658 This code *knows* that the loop cannot contain break, continue, or return,
2659 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2661 Possible cleanups:
2662 - iterate over the generator sequence instead of using recursion
2665 static int
2666 compiler_comprehension_generator(struct compiler *c,
2667 asdl_seq *generators, int gen_index,
2668 expr_ty elt, expr_ty val, int type)
2670 /* generate code for the iterator, then each of the ifs,
2671 and then write to the element */
2673 comprehension_ty gen;
2674 basicblock *start, *anchor, *skip, *if_cleanup;
2675 int i, n;
2677 start = compiler_new_block(c);
2678 skip = compiler_new_block(c);
2679 if_cleanup = compiler_new_block(c);
2680 anchor = compiler_new_block(c);
2682 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2683 anchor == NULL)
2684 return 0;
2686 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2688 if (gen_index == 0) {
2689 /* Receive outermost iter as an implicit argument */
2690 c->u->u_argcount = 1;
2691 ADDOP_I(c, LOAD_FAST, 0);
2693 else {
2694 /* Sub-iter - calculate on the fly */
2695 VISIT(c, expr, gen->iter);
2696 ADDOP(c, GET_ITER);
2698 compiler_use_next_block(c, start);
2699 ADDOP_JREL(c, FOR_ITER, anchor);
2700 NEXT_BLOCK(c);
2701 VISIT(c, expr, gen->target);
2703 /* XXX this needs to be cleaned up...a lot! */
2704 n = asdl_seq_LEN(gen->ifs);
2705 for (i = 0; i < n; i++) {
2706 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2707 VISIT(c, expr, e);
2708 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2709 NEXT_BLOCK(c);
2712 if (++gen_index < asdl_seq_LEN(generators))
2713 if (!compiler_comprehension_generator(c,
2714 generators, gen_index,
2715 elt, val, type))
2716 return 0;
2718 /* only append after the last for generator */
2719 if (gen_index >= asdl_seq_LEN(generators)) {
2720 /* comprehension specific code */
2721 switch (type) {
2722 case COMP_GENEXP:
2723 VISIT(c, expr, elt);
2724 ADDOP(c, YIELD_VALUE);
2725 ADDOP(c, POP_TOP);
2726 break;
2727 case COMP_SETCOMP:
2728 VISIT(c, expr, elt);
2729 ADDOP_I(c, SET_ADD, gen_index + 1);
2730 break;
2731 case COMP_DICTCOMP:
2732 /* With 'd[k] = v', v is evaluated before k, so we do
2733 the same. */
2734 VISIT(c, expr, val);
2735 VISIT(c, expr, elt);
2736 ADDOP_I(c, MAP_ADD, gen_index + 1);
2737 break;
2738 default:
2739 return 0;
2742 compiler_use_next_block(c, skip);
2744 compiler_use_next_block(c, if_cleanup);
2745 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2746 compiler_use_next_block(c, anchor);
2748 return 1;
2751 static int
2752 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2753 asdl_seq *generators, expr_ty elt, expr_ty val)
2755 PyCodeObject *co = NULL;
2756 expr_ty outermost_iter;
2758 outermost_iter = ((comprehension_ty)
2759 asdl_seq_GET(generators, 0))->iter;
2761 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2762 goto error;
2764 if (type != COMP_GENEXP) {
2765 int op;
2766 switch (type) {
2767 case COMP_SETCOMP:
2768 op = BUILD_SET;
2769 break;
2770 case COMP_DICTCOMP:
2771 op = BUILD_MAP;
2772 break;
2773 default:
2774 PyErr_Format(PyExc_SystemError,
2775 "unknown comprehension type %d", type);
2776 goto error_in_scope;
2779 ADDOP_I(c, op, 0);
2782 if (!compiler_comprehension_generator(c, generators, 0, elt,
2783 val, type))
2784 goto error_in_scope;
2786 if (type != COMP_GENEXP) {
2787 ADDOP(c, RETURN_VALUE);
2790 co = assemble(c, 1);
2791 compiler_exit_scope(c);
2792 if (co == NULL)
2793 goto error;
2795 if (!compiler_make_closure(c, co, 0))
2796 goto error;
2797 Py_DECREF(co);
2799 VISIT(c, expr, outermost_iter);
2800 ADDOP(c, GET_ITER);
2801 ADDOP_I(c, CALL_FUNCTION, 1);
2802 return 1;
2803 error_in_scope:
2804 compiler_exit_scope(c);
2805 error:
2806 Py_XDECREF(co);
2807 return 0;
2810 static int
2811 compiler_genexp(struct compiler *c, expr_ty e)
2813 static identifier name;
2814 if (!name) {
2815 name = PyString_FromString("<genexpr>");
2816 if (!name)
2817 return 0;
2819 assert(e->kind == GeneratorExp_kind);
2820 return compiler_comprehension(c, e, COMP_GENEXP, name,
2821 e->v.GeneratorExp.generators,
2822 e->v.GeneratorExp.elt, NULL);
2825 static int
2826 compiler_setcomp(struct compiler *c, expr_ty e)
2828 static identifier name;
2829 if (!name) {
2830 name = PyString_FromString("<setcomp>");
2831 if (!name)
2832 return 0;
2834 assert(e->kind == SetComp_kind);
2835 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2836 e->v.SetComp.generators,
2837 e->v.SetComp.elt, NULL);
2840 static int
2841 compiler_dictcomp(struct compiler *c, expr_ty e)
2843 static identifier name;
2844 if (!name) {
2845 name = PyString_FromString("<dictcomp>");
2846 if (!name)
2847 return 0;
2849 assert(e->kind == DictComp_kind);
2850 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2851 e->v.DictComp.generators,
2852 e->v.DictComp.key, e->v.DictComp.value);
2855 static int
2856 compiler_visit_keyword(struct compiler *c, keyword_ty k)
2858 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2859 VISIT(c, expr, k->value);
2860 return 1;
2863 /* Test whether expression is constant. For constants, report
2864 whether they are true or false.
2866 Return values: 1 for true, 0 for false, -1 for non-constant.
2869 static int
2870 expr_constant(expr_ty e)
2872 switch (e->kind) {
2873 case Num_kind:
2874 return PyObject_IsTrue(e->v.Num.n);
2875 case Str_kind:
2876 return PyObject_IsTrue(e->v.Str.s);
2877 case Name_kind:
2878 /* __debug__ is not assignable, so we can optimize
2879 * it away in if and while statements */
2880 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2881 "__debug__") == 0)
2882 return ! Py_OptimizeFlag;
2883 /* fall through */
2884 default:
2885 return -1;
2890 Implements the with statement from PEP 343.
2892 The semantics outlined in that PEP are as follows:
2894 with EXPR as VAR:
2895 BLOCK
2897 It is implemented roughly as:
2899 context = EXPR
2900 exit = context.__exit__ # not calling it
2901 value = context.__enter__()
2902 try:
2903 VAR = value # if VAR present in the syntax
2904 BLOCK
2905 finally:
2906 if an exception was raised:
2907 exc = copy of (exception, instance, traceback)
2908 else:
2909 exc = (None, None, None)
2910 exit(*exc)
2912 static int
2913 compiler_with(struct compiler *c, stmt_ty s)
2915 basicblock *block, *finally;
2917 assert(s->kind == With_kind);
2919 block = compiler_new_block(c);
2920 finally = compiler_new_block(c);
2921 if (!block || !finally)
2922 return 0;
2924 /* Evaluate EXPR */
2925 VISIT(c, expr, s->v.With.context_expr);
2926 ADDOP_JREL(c, SETUP_WITH, finally);
2928 /* SETUP_WITH pushes a finally block. */
2929 compiler_use_next_block(c, block);
2930 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
2931 return 0;
2934 if (s->v.With.optional_vars) {
2935 VISIT(c, expr, s->v.With.optional_vars);
2937 else {
2938 /* Discard result from context.__enter__() */
2939 ADDOP(c, POP_TOP);
2942 /* BLOCK code */
2943 VISIT_SEQ(c, stmt, s->v.With.body);
2945 /* End of try block; start the finally block */
2946 ADDOP(c, POP_BLOCK);
2947 compiler_pop_fblock(c, FINALLY_TRY, block);
2949 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2950 compiler_use_next_block(c, finally);
2951 if (!compiler_push_fblock(c, FINALLY_END, finally))
2952 return 0;
2954 /* Finally block starts; context.__exit__ is on the stack under
2955 the exception or return information. Just issue our magic
2956 opcode. */
2957 ADDOP(c, WITH_CLEANUP);
2959 /* Finally block ends. */
2960 ADDOP(c, END_FINALLY);
2961 compiler_pop_fblock(c, FINALLY_END, finally);
2962 return 1;
2965 static int
2966 compiler_visit_expr(struct compiler *c, expr_ty e)
2968 int i, n;
2970 /* If expr e has a different line number than the last expr/stmt,
2971 set a new line number for the next instruction.
2973 if (e->lineno > c->u->u_lineno) {
2974 c->u->u_lineno = e->lineno;
2975 c->u->u_lineno_set = false;
2977 switch (e->kind) {
2978 case BoolOp_kind:
2979 return compiler_boolop(c, e);
2980 case BinOp_kind:
2981 VISIT(c, expr, e->v.BinOp.left);
2982 VISIT(c, expr, e->v.BinOp.right);
2983 ADDOP(c, binop(c, e->v.BinOp.op));
2984 break;
2985 case UnaryOp_kind:
2986 VISIT(c, expr, e->v.UnaryOp.operand);
2987 ADDOP(c, unaryop(e->v.UnaryOp.op));
2988 break;
2989 case Lambda_kind:
2990 return compiler_lambda(c, e);
2991 case IfExp_kind:
2992 return compiler_ifexp(c, e);
2993 case Dict_kind:
2994 n = asdl_seq_LEN(e->v.Dict.values);
2995 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
2996 for (i = 0; i < n; i++) {
2997 VISIT(c, expr,
2998 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
2999 VISIT(c, expr,
3000 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3001 ADDOP(c, STORE_MAP);
3003 break;
3004 case Set_kind:
3005 n = asdl_seq_LEN(e->v.Set.elts);
3006 VISIT_SEQ(c, expr, e->v.Set.elts);
3007 ADDOP_I(c, BUILD_SET, n);
3008 break;
3009 case ListComp_kind:
3010 return compiler_listcomp(c, e);
3011 case SetComp_kind:
3012 return compiler_setcomp(c, e);
3013 case DictComp_kind:
3014 return compiler_dictcomp(c, e);
3015 case GeneratorExp_kind:
3016 return compiler_genexp(c, e);
3017 case Yield_kind:
3018 if (c->u->u_ste->ste_type != FunctionBlock)
3019 return compiler_error(c, "'yield' outside function");
3020 if (e->v.Yield.value) {
3021 VISIT(c, expr, e->v.Yield.value);
3023 else {
3024 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3026 ADDOP(c, YIELD_VALUE);
3027 break;
3028 case Compare_kind:
3029 return compiler_compare(c, e);
3030 case Call_kind:
3031 return compiler_call(c, e);
3032 case Repr_kind:
3033 VISIT(c, expr, e->v.Repr.value);
3034 ADDOP(c, UNARY_CONVERT);
3035 break;
3036 case Num_kind:
3037 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3038 break;
3039 case Str_kind:
3040 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3041 break;
3042 /* The following exprs can be assignment targets. */
3043 case Attribute_kind:
3044 if (e->v.Attribute.ctx != AugStore)
3045 VISIT(c, expr, e->v.Attribute.value);
3046 switch (e->v.Attribute.ctx) {
3047 case AugLoad:
3048 ADDOP(c, DUP_TOP);
3049 /* Fall through to load */
3050 case Load:
3051 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3052 break;
3053 case AugStore:
3054 ADDOP(c, ROT_TWO);
3055 /* Fall through to save */
3056 case Store:
3057 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3058 break;
3059 case Del:
3060 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3061 break;
3062 case Param:
3063 default:
3064 PyErr_SetString(PyExc_SystemError,
3065 "param invalid in attribute expression");
3066 return 0;
3068 break;
3069 case Subscript_kind:
3070 switch (e->v.Subscript.ctx) {
3071 case AugLoad:
3072 VISIT(c, expr, e->v.Subscript.value);
3073 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3074 break;
3075 case Load:
3076 VISIT(c, expr, e->v.Subscript.value);
3077 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3078 break;
3079 case AugStore:
3080 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3081 break;
3082 case Store:
3083 VISIT(c, expr, e->v.Subscript.value);
3084 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3085 break;
3086 case Del:
3087 VISIT(c, expr, e->v.Subscript.value);
3088 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3089 break;
3090 case Param:
3091 default:
3092 PyErr_SetString(PyExc_SystemError,
3093 "param invalid in subscript expression");
3094 return 0;
3096 break;
3097 case Name_kind:
3098 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3099 /* child nodes of List and Tuple will have expr_context set */
3100 case List_kind:
3101 return compiler_list(c, e);
3102 case Tuple_kind:
3103 return compiler_tuple(c, e);
3105 return 1;
3108 static int
3109 compiler_augassign(struct compiler *c, stmt_ty s)
3111 expr_ty e = s->v.AugAssign.target;
3112 expr_ty auge;
3114 assert(s->kind == AugAssign_kind);
3116 switch (e->kind) {
3117 case Attribute_kind:
3118 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3119 AugLoad, e->lineno, e->col_offset, c->c_arena);
3120 if (auge == NULL)
3121 return 0;
3122 VISIT(c, expr, auge);
3123 VISIT(c, expr, s->v.AugAssign.value);
3124 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3125 auge->v.Attribute.ctx = AugStore;
3126 VISIT(c, expr, auge);
3127 break;
3128 case Subscript_kind:
3129 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3130 AugLoad, e->lineno, e->col_offset, c->c_arena);
3131 if (auge == NULL)
3132 return 0;
3133 VISIT(c, expr, auge);
3134 VISIT(c, expr, s->v.AugAssign.value);
3135 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3136 auge->v.Subscript.ctx = AugStore;
3137 VISIT(c, expr, auge);
3138 break;
3139 case Name_kind:
3140 if (!compiler_nameop(c, e->v.Name.id, Load))
3141 return 0;
3142 VISIT(c, expr, s->v.AugAssign.value);
3143 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3144 return compiler_nameop(c, e->v.Name.id, Store);
3145 default:
3146 PyErr_Format(PyExc_SystemError,
3147 "invalid node type (%d) for augmented assignment",
3148 e->kind);
3149 return 0;
3151 return 1;
3154 static int
3155 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3157 struct fblockinfo *f;
3158 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3159 PyErr_SetString(PyExc_SystemError,
3160 "too many statically nested blocks");
3161 return 0;
3163 f = &c->u->u_fblock[c->u->u_nfblocks++];
3164 f->fb_type = t;
3165 f->fb_block = b;
3166 return 1;
3169 static void
3170 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3172 struct compiler_unit *u = c->u;
3173 assert(u->u_nfblocks > 0);
3174 u->u_nfblocks--;
3175 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3176 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3179 static int
3180 compiler_in_loop(struct compiler *c) {
3181 int i;
3182 struct compiler_unit *u = c->u;
3183 for (i = 0; i < u->u_nfblocks; ++i) {
3184 if (u->u_fblock[i].fb_type == LOOP)
3185 return 1;
3187 return 0;
3189 /* Raises a SyntaxError and returns 0.
3190 If something goes wrong, a different exception may be raised.
3193 static int
3194 compiler_error(struct compiler *c, const char *errstr)
3196 PyObject *loc;
3197 PyObject *u = NULL, *v = NULL;
3199 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3200 if (!loc) {
3201 Py_INCREF(Py_None);
3202 loc = Py_None;
3204 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3205 Py_None, loc);
3206 if (!u)
3207 goto exit;
3208 v = Py_BuildValue("(zO)", errstr, u);
3209 if (!v)
3210 goto exit;
3211 PyErr_SetObject(PyExc_SyntaxError, v);
3212 exit:
3213 Py_DECREF(loc);
3214 Py_XDECREF(u);
3215 Py_XDECREF(v);
3216 return 0;
3219 static int
3220 compiler_handle_subscr(struct compiler *c, const char *kind,
3221 expr_context_ty ctx)
3223 int op = 0;
3225 /* XXX this code is duplicated */
3226 switch (ctx) {
3227 case AugLoad: /* fall through to Load */
3228 case Load: op = BINARY_SUBSCR; break;
3229 case AugStore:/* fall through to Store */
3230 case Store: op = STORE_SUBSCR; break;
3231 case Del: op = DELETE_SUBSCR; break;
3232 case Param:
3233 PyErr_Format(PyExc_SystemError,
3234 "invalid %s kind %d in subscript\n",
3235 kind, ctx);
3236 return 0;
3238 if (ctx == AugLoad) {
3239 ADDOP_I(c, DUP_TOPX, 2);
3241 else if (ctx == AugStore) {
3242 ADDOP(c, ROT_THREE);
3244 ADDOP(c, op);
3245 return 1;
3248 static int
3249 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3251 int n = 2;
3252 assert(s->kind == Slice_kind);
3254 /* only handles the cases where BUILD_SLICE is emitted */
3255 if (s->v.Slice.lower) {
3256 VISIT(c, expr, s->v.Slice.lower);
3258 else {
3259 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3262 if (s->v.Slice.upper) {
3263 VISIT(c, expr, s->v.Slice.upper);
3265 else {
3266 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3269 if (s->v.Slice.step) {
3270 n++;
3271 VISIT(c, expr, s->v.Slice.step);
3273 ADDOP_I(c, BUILD_SLICE, n);
3274 return 1;
3277 static int
3278 compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3280 int op = 0, slice_offset = 0, stack_count = 0;
3282 assert(s->v.Slice.step == NULL);
3283 if (s->v.Slice.lower) {
3284 slice_offset++;
3285 stack_count++;
3286 if (ctx != AugStore)
3287 VISIT(c, expr, s->v.Slice.lower);
3289 if (s->v.Slice.upper) {
3290 slice_offset += 2;
3291 stack_count++;
3292 if (ctx != AugStore)
3293 VISIT(c, expr, s->v.Slice.upper);
3296 if (ctx == AugLoad) {
3297 switch (stack_count) {
3298 case 0: ADDOP(c, DUP_TOP); break;
3299 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3300 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3303 else if (ctx == AugStore) {
3304 switch (stack_count) {
3305 case 0: ADDOP(c, ROT_TWO); break;
3306 case 1: ADDOP(c, ROT_THREE); break;
3307 case 2: ADDOP(c, ROT_FOUR); break;
3311 switch (ctx) {
3312 case AugLoad: /* fall through to Load */
3313 case Load: op = SLICE; break;
3314 case AugStore:/* fall through to Store */
3315 case Store: op = STORE_SLICE; break;
3316 case Del: op = DELETE_SLICE; break;
3317 case Param:
3318 default:
3319 PyErr_SetString(PyExc_SystemError,
3320 "param invalid in simple slice");
3321 return 0;
3324 ADDOP(c, op + slice_offset);
3325 return 1;
3328 static int
3329 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3330 expr_context_ty ctx)
3332 switch (s->kind) {
3333 case Ellipsis_kind:
3334 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3335 break;
3336 case Slice_kind:
3337 return compiler_slice(c, s, ctx);
3338 case Index_kind:
3339 VISIT(c, expr, s->v.Index.value);
3340 break;
3341 case ExtSlice_kind:
3342 default:
3343 PyErr_SetString(PyExc_SystemError,
3344 "extended slice invalid in nested slice");
3345 return 0;
3347 return 1;
3350 static int
3351 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3353 char * kindname = NULL;
3354 switch (s->kind) {
3355 case Index_kind:
3356 kindname = "index";
3357 if (ctx != AugStore) {
3358 VISIT(c, expr, s->v.Index.value);
3360 break;
3361 case Ellipsis_kind:
3362 kindname = "ellipsis";
3363 if (ctx != AugStore) {
3364 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3366 break;
3367 case Slice_kind:
3368 kindname = "slice";
3369 if (!s->v.Slice.step)
3370 return compiler_simple_slice(c, s, ctx);
3371 if (ctx != AugStore) {
3372 if (!compiler_slice(c, s, ctx))
3373 return 0;
3375 break;
3376 case ExtSlice_kind:
3377 kindname = "extended slice";
3378 if (ctx != AugStore) {
3379 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3380 for (i = 0; i < n; i++) {
3381 slice_ty sub = (slice_ty)asdl_seq_GET(
3382 s->v.ExtSlice.dims, i);
3383 if (!compiler_visit_nested_slice(c, sub, ctx))
3384 return 0;
3386 ADDOP_I(c, BUILD_TUPLE, n);
3388 break;
3389 default:
3390 PyErr_Format(PyExc_SystemError,
3391 "invalid subscript kind %d", s->kind);
3392 return 0;
3394 return compiler_handle_subscr(c, kindname, ctx);
3398 /* End of the compiler section, beginning of the assembler section */
3400 /* do depth-first search of basic block graph, starting with block.
3401 post records the block indices in post-order.
3403 XXX must handle implicit jumps from one block to next
3406 struct assembler {
3407 PyObject *a_bytecode; /* string containing bytecode */
3408 int a_offset; /* offset into bytecode */
3409 int a_nblocks; /* number of reachable blocks */
3410 basicblock **a_postorder; /* list of blocks in dfs postorder */
3411 PyObject *a_lnotab; /* string containing lnotab */
3412 int a_lnotab_off; /* offset into lnotab */
3413 int a_lineno; /* last lineno of emitted instruction */
3414 int a_lineno_off; /* bytecode offset of last lineno */
3417 static void
3418 dfs(struct compiler *c, basicblock *b, struct assembler *a)
3420 int i;
3421 struct instr *instr = NULL;
3423 if (b->b_seen)
3424 return;
3425 b->b_seen = 1;
3426 if (b->b_next != NULL)
3427 dfs(c, b->b_next, a);
3428 for (i = 0; i < b->b_iused; i++) {
3429 instr = &b->b_instr[i];
3430 if (instr->i_jrel || instr->i_jabs)
3431 dfs(c, instr->i_target, a);
3433 a->a_postorder[a->a_nblocks++] = b;
3436 static int
3437 stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3439 int i, target_depth;
3440 struct instr *instr;
3441 if (b->b_seen || b->b_startdepth >= depth)
3442 return maxdepth;
3443 b->b_seen = 1;
3444 b->b_startdepth = depth;
3445 for (i = 0; i < b->b_iused; i++) {
3446 instr = &b->b_instr[i];
3447 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3448 if (depth > maxdepth)
3449 maxdepth = depth;
3450 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3451 if (instr->i_jrel || instr->i_jabs) {
3452 target_depth = depth;
3453 if (instr->i_opcode == FOR_ITER) {
3454 target_depth = depth-2;
3455 } else if (instr->i_opcode == SETUP_FINALLY ||
3456 instr->i_opcode == SETUP_EXCEPT) {
3457 target_depth = depth+3;
3458 if (target_depth > maxdepth)
3459 maxdepth = target_depth;
3461 maxdepth = stackdepth_walk(c, instr->i_target,
3462 target_depth, maxdepth);
3463 if (instr->i_opcode == JUMP_ABSOLUTE ||
3464 instr->i_opcode == JUMP_FORWARD) {
3465 goto out; /* remaining code is dead */
3469 if (b->b_next)
3470 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3471 out:
3472 b->b_seen = 0;
3473 return maxdepth;
3476 /* Find the flow path that needs the largest stack. We assume that
3477 * cycles in the flow graph have no net effect on the stack depth.
3479 static int
3480 stackdepth(struct compiler *c)
3482 basicblock *b, *entryblock;
3483 entryblock = NULL;
3484 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3485 b->b_seen = 0;
3486 b->b_startdepth = INT_MIN;
3487 entryblock = b;
3489 if (!entryblock)
3490 return 0;
3491 return stackdepth_walk(c, entryblock, 0, 0);
3494 static int
3495 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3497 memset(a, 0, sizeof(struct assembler));
3498 a->a_lineno = firstlineno;
3499 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3500 if (!a->a_bytecode)
3501 return 0;
3502 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3503 if (!a->a_lnotab)
3504 return 0;
3505 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3506 PyErr_NoMemory();
3507 return 0;
3509 a->a_postorder = (basicblock **)PyObject_Malloc(
3510 sizeof(basicblock *) * nblocks);
3511 if (!a->a_postorder) {
3512 PyErr_NoMemory();
3513 return 0;
3515 return 1;
3518 static void
3519 assemble_free(struct assembler *a)
3521 Py_XDECREF(a->a_bytecode);
3522 Py_XDECREF(a->a_lnotab);
3523 if (a->a_postorder)
3524 PyObject_Free(a->a_postorder);
3527 /* Return the size of a basic block in bytes. */
3529 static int
3530 instrsize(struct instr *instr)
3532 if (!instr->i_hasarg)
3533 return 1; /* 1 byte for the opcode*/
3534 if (instr->i_oparg > 0xffff)
3535 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3536 return 3; /* 1 (opcode) + 2 (oparg) */
3539 static int
3540 blocksize(basicblock *b)
3542 int i;
3543 int size = 0;
3545 for (i = 0; i < b->b_iused; i++)
3546 size += instrsize(&b->b_instr[i]);
3547 return size;
3550 /* Appends a pair to the end of the line number table, a_lnotab, representing
3551 the instruction's bytecode offset and line number. See
3552 Objects/lnotab_notes.txt for the description of the line number table. */
3554 static int
3555 assemble_lnotab(struct assembler *a, struct instr *i)
3557 int d_bytecode, d_lineno;
3558 int len;
3559 unsigned char *lnotab;
3561 d_bytecode = a->a_offset - a->a_lineno_off;
3562 d_lineno = i->i_lineno - a->a_lineno;
3564 assert(d_bytecode >= 0);
3565 assert(d_lineno >= 0);
3567 if(d_bytecode == 0 && d_lineno == 0)
3568 return 1;
3570 if (d_bytecode > 255) {
3571 int j, nbytes, ncodes = d_bytecode / 255;
3572 nbytes = a->a_lnotab_off + 2 * ncodes;
3573 len = PyString_GET_SIZE(a->a_lnotab);
3574 if (nbytes >= len) {
3575 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3576 len = nbytes;
3577 else if (len <= INT_MAX / 2)
3578 len *= 2;
3579 else {
3580 PyErr_NoMemory();
3581 return 0;
3583 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3584 return 0;
3586 lnotab = (unsigned char *)
3587 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3588 for (j = 0; j < ncodes; j++) {
3589 *lnotab++ = 255;
3590 *lnotab++ = 0;
3592 d_bytecode -= ncodes * 255;
3593 a->a_lnotab_off += ncodes * 2;
3595 assert(d_bytecode <= 255);
3596 if (d_lineno > 255) {
3597 int j, nbytes, ncodes = d_lineno / 255;
3598 nbytes = a->a_lnotab_off + 2 * ncodes;
3599 len = PyString_GET_SIZE(a->a_lnotab);
3600 if (nbytes >= len) {
3601 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3602 len = nbytes;
3603 else if (len <= INT_MAX / 2)
3604 len *= 2;
3605 else {
3606 PyErr_NoMemory();
3607 return 0;
3609 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3610 return 0;
3612 lnotab = (unsigned char *)
3613 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3614 *lnotab++ = d_bytecode;
3615 *lnotab++ = 255;
3616 d_bytecode = 0;
3617 for (j = 1; j < ncodes; j++) {
3618 *lnotab++ = 0;
3619 *lnotab++ = 255;
3621 d_lineno -= ncodes * 255;
3622 a->a_lnotab_off += ncodes * 2;
3625 len = PyString_GET_SIZE(a->a_lnotab);
3626 if (a->a_lnotab_off + 2 >= len) {
3627 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
3628 return 0;
3630 lnotab = (unsigned char *)
3631 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3633 a->a_lnotab_off += 2;
3634 if (d_bytecode) {
3635 *lnotab++ = d_bytecode;
3636 *lnotab++ = d_lineno;
3638 else { /* First line of a block; def stmt, etc. */
3639 *lnotab++ = 0;
3640 *lnotab++ = d_lineno;
3642 a->a_lineno = i->i_lineno;
3643 a->a_lineno_off = a->a_offset;
3644 return 1;
3647 /* assemble_emit()
3648 Extend the bytecode with a new instruction.
3649 Update lnotab if necessary.
3652 static int
3653 assemble_emit(struct assembler *a, struct instr *i)
3655 int size, arg = 0, ext = 0;
3656 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
3657 char *code;
3659 size = instrsize(i);
3660 if (i->i_hasarg) {
3661 arg = i->i_oparg;
3662 ext = arg >> 16;
3664 if (i->i_lineno && !assemble_lnotab(a, i))
3665 return 0;
3666 if (a->a_offset + size >= len) {
3667 if (len > PY_SSIZE_T_MAX / 2)
3668 return 0;
3669 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
3670 return 0;
3672 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3673 a->a_offset += size;
3674 if (size == 6) {
3675 assert(i->i_hasarg);
3676 *code++ = (char)EXTENDED_ARG;
3677 *code++ = ext & 0xff;
3678 *code++ = ext >> 8;
3679 arg &= 0xffff;
3681 *code++ = i->i_opcode;
3682 if (i->i_hasarg) {
3683 assert(size == 3 || size == 6);
3684 *code++ = arg & 0xff;
3685 *code++ = arg >> 8;
3687 return 1;
3690 static void
3691 assemble_jump_offsets(struct assembler *a, struct compiler *c)
3693 basicblock *b;
3694 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3695 int i;
3697 /* Compute the size of each block and fixup jump args.
3698 Replace block pointer with position in bytecode. */
3699 do {
3700 totsize = 0;
3701 for (i = a->a_nblocks - 1; i >= 0; i--) {
3702 b = a->a_postorder[i];
3703 bsize = blocksize(b);
3704 b->b_offset = totsize;
3705 totsize += bsize;
3707 last_extended_arg_count = extended_arg_count;
3708 extended_arg_count = 0;
3709 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3710 bsize = b->b_offset;
3711 for (i = 0; i < b->b_iused; i++) {
3712 struct instr *instr = &b->b_instr[i];
3713 /* Relative jumps are computed relative to
3714 the instruction pointer after fetching
3715 the jump instruction.
3717 bsize += instrsize(instr);
3718 if (instr->i_jabs)
3719 instr->i_oparg = instr->i_target->b_offset;
3720 else if (instr->i_jrel) {
3721 int delta = instr->i_target->b_offset - bsize;
3722 instr->i_oparg = delta;
3724 else
3725 continue;
3726 if (instr->i_oparg > 0xffff)
3727 extended_arg_count++;
3731 /* XXX: This is an awful hack that could hurt performance, but
3732 on the bright side it should work until we come up
3733 with a better solution.
3735 The issue is that in the first loop blocksize() is called
3736 which calls instrsize() which requires i_oparg be set
3737 appropriately. There is a bootstrap problem because
3738 i_oparg is calculated in the second loop above.
3740 So we loop until we stop seeing new EXTENDED_ARGs.
3741 The only EXTENDED_ARGs that could be popping up are
3742 ones in jump instructions. So this should converge
3743 fairly quickly.
3745 } while (last_extended_arg_count != extended_arg_count);
3748 static PyObject *
3749 dict_keys_inorder(PyObject *dict, int offset)
3751 PyObject *tuple, *k, *v;
3752 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
3754 tuple = PyTuple_New(size);
3755 if (tuple == NULL)
3756 return NULL;
3757 while (PyDict_Next(dict, &pos, &k, &v)) {
3758 i = PyInt_AS_LONG(v);
3759 /* The keys of the dictionary are tuples. (see compiler_add_o)
3760 The object we want is always first, though. */
3761 k = PyTuple_GET_ITEM(k, 0);
3762 Py_INCREF(k);
3763 assert((i - offset) < size);
3764 assert((i - offset) >= 0);
3765 PyTuple_SET_ITEM(tuple, i - offset, k);
3767 return tuple;
3770 static int
3771 compute_code_flags(struct compiler *c)
3773 PySTEntryObject *ste = c->u->u_ste;
3774 int flags = 0, n;
3775 if (ste->ste_type != ModuleBlock)
3776 flags |= CO_NEWLOCALS;
3777 if (ste->ste_type == FunctionBlock) {
3778 if (!ste->ste_unoptimized)
3779 flags |= CO_OPTIMIZED;
3780 if (ste->ste_nested)
3781 flags |= CO_NESTED;
3782 if (ste->ste_generator)
3783 flags |= CO_GENERATOR;
3784 if (ste->ste_varargs)
3785 flags |= CO_VARARGS;
3786 if (ste->ste_varkeywords)
3787 flags |= CO_VARKEYWORDS;
3790 /* (Only) inherit compilerflags in PyCF_MASK */
3791 flags |= (c->c_flags->cf_flags & PyCF_MASK);
3793 n = PyDict_Size(c->u->u_freevars);
3794 if (n < 0)
3795 return -1;
3796 if (n == 0) {
3797 n = PyDict_Size(c->u->u_cellvars);
3798 if (n < 0)
3799 return -1;
3800 if (n == 0) {
3801 flags |= CO_NOFREE;
3805 return flags;
3808 static PyCodeObject *
3809 makecode(struct compiler *c, struct assembler *a)
3811 PyObject *tmp;
3812 PyCodeObject *co = NULL;
3813 PyObject *consts = NULL;
3814 PyObject *names = NULL;
3815 PyObject *varnames = NULL;
3816 PyObject *filename = NULL;
3817 PyObject *name = NULL;
3818 PyObject *freevars = NULL;
3819 PyObject *cellvars = NULL;
3820 PyObject *bytecode = NULL;
3821 int nlocals, flags;
3823 tmp = dict_keys_inorder(c->u->u_consts, 0);
3824 if (!tmp)
3825 goto error;
3826 consts = PySequence_List(tmp); /* optimize_code requires a list */
3827 Py_DECREF(tmp);
3829 names = dict_keys_inorder(c->u->u_names, 0);
3830 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3831 if (!consts || !names || !varnames)
3832 goto error;
3834 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3835 if (!cellvars)
3836 goto error;
3837 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3838 if (!freevars)
3839 goto error;
3840 filename = PyString_FromString(c->c_filename);
3841 if (!filename)
3842 goto error;
3844 nlocals = PyDict_Size(c->u->u_varnames);
3845 flags = compute_code_flags(c);
3846 if (flags < 0)
3847 goto error;
3849 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3850 if (!bytecode)
3851 goto error;
3853 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3854 if (!tmp)
3855 goto error;
3856 Py_DECREF(consts);
3857 consts = tmp;
3859 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3860 bytecode, consts, names, varnames,
3861 freevars, cellvars,
3862 filename, c->u->u_name,
3863 c->u->u_firstlineno,
3864 a->a_lnotab);
3865 error:
3866 Py_XDECREF(consts);
3867 Py_XDECREF(names);
3868 Py_XDECREF(varnames);
3869 Py_XDECREF(filename);
3870 Py_XDECREF(name);
3871 Py_XDECREF(freevars);
3872 Py_XDECREF(cellvars);
3873 Py_XDECREF(bytecode);
3874 return co;
3878 /* For debugging purposes only */
3879 #if 0
3880 static void
3881 dump_instr(const struct instr *i)
3883 const char *jrel = i->i_jrel ? "jrel " : "";
3884 const char *jabs = i->i_jabs ? "jabs " : "";
3885 char arg[128];
3887 *arg = '\0';
3888 if (i->i_hasarg)
3889 sprintf(arg, "arg: %d ", i->i_oparg);
3891 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3892 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3895 static void
3896 dump_basicblock(const basicblock *b)
3898 const char *seen = b->b_seen ? "seen " : "";
3899 const char *b_return = b->b_return ? "return " : "";
3900 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3901 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3902 if (b->b_instr) {
3903 int i;
3904 for (i = 0; i < b->b_iused; i++) {
3905 fprintf(stderr, " [%02d] ", i);
3906 dump_instr(b->b_instr + i);
3910 #endif
3912 static PyCodeObject *
3913 assemble(struct compiler *c, int addNone)
3915 basicblock *b, *entryblock;
3916 struct assembler a;
3917 int i, j, nblocks;
3918 PyCodeObject *co = NULL;
3920 /* Make sure every block that falls off the end returns None.
3921 XXX NEXT_BLOCK() isn't quite right, because if the last
3922 block ends with a jump or return b_next shouldn't set.
3924 if (!c->u->u_curblock->b_return) {
3925 NEXT_BLOCK(c);
3926 if (addNone)
3927 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3928 ADDOP(c, RETURN_VALUE);
3931 nblocks = 0;
3932 entryblock = NULL;
3933 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3934 nblocks++;
3935 entryblock = b;
3938 /* Set firstlineno if it wasn't explicitly set. */
3939 if (!c->u->u_firstlineno) {
3940 if (entryblock && entryblock->b_instr)
3941 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3942 else
3943 c->u->u_firstlineno = 1;
3945 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3946 goto error;
3947 dfs(c, entryblock, &a);
3949 /* Can't modify the bytecode after computing jump offsets. */
3950 assemble_jump_offsets(&a, c);
3952 /* Emit code in reverse postorder from dfs. */
3953 for (i = a.a_nblocks - 1; i >= 0; i--) {
3954 b = a.a_postorder[i];
3955 for (j = 0; j < b->b_iused; j++)
3956 if (!assemble_emit(&a, &b->b_instr[j]))
3957 goto error;
3960 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
3961 goto error;
3962 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
3963 goto error;
3965 co = makecode(c, &a);
3966 error:
3967 assemble_free(&a);
3968 return co;