initialize weakref some weakref types
[python.git] / Python / compile.c
blob69321ae0c41cccd8b770ce37f21a0a40c790f0ce
1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in
11 * this file.
12 * 5. Optimize the byte code (peephole optimizations). See peephole.c
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
17 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
20 * structure takes care of releasing those. Use the arena to manage
21 * objects.
24 #include "Python.h"
26 #include "Python-ast.h"
27 #include "node.h"
28 #include "pyarena.h"
29 #include "ast.h"
30 #include "code.h"
31 #include "compile.h"
32 #include "symtable.h"
33 #include "opcode.h"
35 int Py_OptimizeFlag = 0;
37 #define DEFAULT_BLOCK_SIZE 16
38 #define DEFAULT_BLOCKS 8
39 #define DEFAULT_CODE_SIZE 128
40 #define DEFAULT_LNOTAB_SIZE 16
42 struct instr {
43 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
46 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
52 typedef struct basicblock_ {
53 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
56 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
67 unsigned b_seen : 1;
68 /* b_return is true if a RETURN_VALUE opcode is inserted. */
69 unsigned b_return : 1;
70 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
73 int b_offset;
74 } basicblock;
76 /* fblockinfo tracks the current frame block.
78 A frame block is used to handle loops, try/except, and try/finally.
79 It's called a frame block to distinguish it from a basic block in the
80 compiler IR.
83 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
85 struct fblockinfo {
86 enum fblocktype fb_type;
87 basicblock *fb_block;
90 /* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
93 struct compiler_unit {
94 PySTEntryObject *u_ste;
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
98 the index of them in co_XXX. The index is used as
99 the argument for opcodes that refer to those collections.
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
107 PyObject *u_private; /* for private name mangling */
109 int u_argcount; /* number of arguments for block */
110 /* Pointer to the most recently allocated block. By following b_list
111 members, you can reach all early allocated blocks. */
112 basicblock *u_blocks;
113 basicblock *u_curblock; /* pointer to current block */
114 int u_tmpname; /* temporary variables for list comps */
116 int u_nfblocks;
117 struct fblockinfo u_fblock[CO_MAXBLOCKS];
119 int u_firstlineno; /* the first lineno of the block */
120 int u_lineno; /* the lineno for the current stmt */
121 bool u_lineno_set; /* boolean to indicate whether instr
122 has been generated with current lineno */
125 /* This struct captures the global state of a compilation.
127 The u pointer points to the current compilation unit, while units
128 for enclosing blocks are stored in c_stack. The u and c_stack are
129 managed by compiler_enter_scope() and compiler_exit_scope().
132 struct compiler {
133 const char *c_filename;
134 struct symtable *c_st;
135 PyFutureFeatures *c_future; /* pointer to module's __future__ */
136 PyCompilerFlags *c_flags;
138 int c_interactive; /* true if in interactive mode */
139 int c_nestlevel;
141 struct compiler_unit *u; /* compiler state for current block */
142 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
143 char *c_encoding; /* source encoding (a borrowed reference) */
144 PyArena *c_arena; /* pointer to memory allocation arena */
147 static int compiler_enter_scope(struct compiler *, identifier, void *, int);
148 static void compiler_free(struct compiler *);
149 static basicblock *compiler_new_block(struct compiler *);
150 static int compiler_next_instr(struct compiler *, basicblock *);
151 static int compiler_addop(struct compiler *, int);
152 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
153 static int compiler_addop_i(struct compiler *, int, int);
154 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
155 static basicblock *compiler_use_new_block(struct compiler *);
156 static int compiler_error(struct compiler *, const char *);
157 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
159 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
160 static int compiler_visit_stmt(struct compiler *, stmt_ty);
161 static int compiler_visit_keyword(struct compiler *, keyword_ty);
162 static int compiler_visit_expr(struct compiler *, expr_ty);
163 static int compiler_augassign(struct compiler *, stmt_ty);
164 static int compiler_visit_slice(struct compiler *, slice_ty,
165 expr_context_ty);
167 static int compiler_push_fblock(struct compiler *, enum fblocktype,
168 basicblock *);
169 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
171 /* Returns true if there is a loop on the fblock stack. */
172 static int compiler_in_loop(struct compiler *);
174 static int inplace_binop(struct compiler *, operator_ty);
175 static int expr_constant(expr_ty e);
177 static int compiler_with(struct compiler *, stmt_ty);
179 static PyCodeObject *assemble(struct compiler *, int addNone);
180 static PyObject *__doc__;
182 PyObject *
183 _Py_Mangle(PyObject *privateobj, PyObject *ident)
185 /* Name mangling: __private becomes _classname__private.
186 This is independent from how the name is used. */
187 const char *p, *name = PyString_AsString(ident);
188 char *buffer;
189 size_t nlen, plen;
190 if (privateobj == NULL || !PyString_Check(privateobj) ||
191 name == NULL || name[0] != '_' || name[1] != '_') {
192 Py_INCREF(ident);
193 return ident;
195 p = PyString_AsString(privateobj);
196 nlen = strlen(name);
197 /* Don't mangle __id__ or names with dots.
199 The only time a name with a dot can occur is when
200 we are compiling an import statement that has a
201 package name.
203 TODO(jhylton): Decide whether we want to support
204 mangling of the module name, e.g. __M.X.
206 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
207 || strchr(name, '.')) {
208 Py_INCREF(ident);
209 return ident; /* Don't mangle __whatever__ */
211 /* Strip leading underscores from class name */
212 while (*p == '_')
213 p++;
214 if (*p == '\0') {
215 Py_INCREF(ident);
216 return ident; /* Don't mangle if class is just underscores */
218 plen = strlen(p);
220 assert(1 <= PY_SSIZE_T_MAX - nlen);
221 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
223 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
224 if (!ident)
225 return 0;
226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
227 buffer = PyString_AS_STRING(ident);
228 buffer[0] = '_';
229 strncpy(buffer+1, p, plen);
230 strcpy(buffer+1+plen, name);
231 return ident;
234 static int
235 compiler_init(struct compiler *c)
237 memset(c, 0, sizeof(struct compiler));
239 c->c_stack = PyList_New(0);
240 if (!c->c_stack)
241 return 0;
243 return 1;
246 PyCodeObject *
247 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
248 PyArena *arena)
250 struct compiler c;
251 PyCodeObject *co = NULL;
252 PyCompilerFlags local_flags;
253 int merged;
255 if (!__doc__) {
256 __doc__ = PyString_InternFromString("__doc__");
257 if (!__doc__)
258 return NULL;
261 if (!compiler_init(&c))
262 return NULL;
263 c.c_filename = filename;
264 c.c_arena = arena;
265 c.c_future = PyFuture_FromAST(mod, filename);
266 if (c.c_future == NULL)
267 goto finally;
268 if (!flags) {
269 local_flags.cf_flags = 0;
270 flags = &local_flags;
272 merged = c.c_future->ff_features | flags->cf_flags;
273 c.c_future->ff_features = merged;
274 flags->cf_flags = merged;
275 c.c_flags = flags;
276 c.c_nestlevel = 0;
278 c.c_st = PySymtable_Build(mod, filename, c.c_future);
279 if (c.c_st == NULL) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError, "no symtable");
282 goto finally;
285 /* XXX initialize to NULL for now, need to handle */
286 c.c_encoding = NULL;
288 co = compiler_mod(&c, mod);
290 finally:
291 compiler_free(&c);
292 assert(co || PyErr_Occurred());
293 return co;
296 PyCodeObject *
297 PyNode_Compile(struct _node *n, const char *filename)
299 PyCodeObject *co = NULL;
300 mod_ty mod;
301 PyArena *arena = PyArena_New();
302 if (!arena)
303 return NULL;
304 mod = PyAST_FromNode(n, NULL, filename, arena);
305 if (mod)
306 co = PyAST_Compile(mod, filename, NULL, arena);
307 PyArena_Free(arena);
308 return co;
311 static void
312 compiler_free(struct compiler *c)
314 if (c->c_st)
315 PySymtable_Free(c->c_st);
316 if (c->c_future)
317 PyObject_Free(c->c_future);
318 Py_DECREF(c->c_stack);
321 static PyObject *
322 list2dict(PyObject *list)
324 Py_ssize_t i, n;
325 PyObject *v, *k;
326 PyObject *dict = PyDict_New();
327 if (!dict) return NULL;
329 n = PyList_Size(list);
330 for (i = 0; i < n; i++) {
331 v = PyInt_FromLong(i);
332 if (!v) {
333 Py_DECREF(dict);
334 return NULL;
336 k = PyList_GET_ITEM(list, i);
337 k = PyTuple_Pack(2, k, k->ob_type);
338 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
339 Py_XDECREF(k);
340 Py_DECREF(v);
341 Py_DECREF(dict);
342 return NULL;
344 Py_DECREF(k);
345 Py_DECREF(v);
347 return dict;
350 /* Return new dict containing names from src that match scope(s).
352 src is a symbol table dictionary. If the scope of a name matches
353 either scope_type or flag is set, insert it into the new dict. The
354 values are integers, starting at offset and increasing by one for
355 each key.
358 static PyObject *
359 dictbytype(PyObject *src, int scope_type, int flag, int offset)
361 Py_ssize_t pos = 0, i = offset, scope;
362 PyObject *k, *v, *dest = PyDict_New();
364 assert(offset >= 0);
365 if (dest == NULL)
366 return NULL;
368 while (PyDict_Next(src, &pos, &k, &v)) {
369 /* XXX this should probably be a macro in symtable.h */
370 assert(PyInt_Check(v));
371 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
373 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
374 PyObject *tuple, *item = PyInt_FromLong(i);
375 if (item == NULL) {
376 Py_DECREF(dest);
377 return NULL;
379 i++;
380 tuple = PyTuple_Pack(2, k, k->ob_type);
381 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
382 Py_DECREF(item);
383 Py_DECREF(dest);
384 Py_XDECREF(tuple);
385 return NULL;
387 Py_DECREF(item);
388 Py_DECREF(tuple);
391 return dest;
394 static void
395 compiler_unit_check(struct compiler_unit *u)
397 basicblock *block;
398 for (block = u->u_blocks; block != NULL; block = block->b_list) {
399 assert((void *)block != (void *)0xcbcbcbcb);
400 assert((void *)block != (void *)0xfbfbfbfb);
401 assert((void *)block != (void *)0xdbdbdbdb);
402 if (block->b_instr != NULL) {
403 assert(block->b_ialloc > 0);
404 assert(block->b_iused > 0);
405 assert(block->b_ialloc >= block->b_iused);
407 else {
408 assert (block->b_iused == 0);
409 assert (block->b_ialloc == 0);
414 static void
415 compiler_unit_free(struct compiler_unit *u)
417 basicblock *b, *next;
419 compiler_unit_check(u);
420 b = u->u_blocks;
421 while (b != NULL) {
422 if (b->b_instr)
423 PyObject_Free((void *)b->b_instr);
424 next = b->b_list;
425 PyObject_Free((void *)b);
426 b = next;
428 Py_CLEAR(u->u_ste);
429 Py_CLEAR(u->u_name);
430 Py_CLEAR(u->u_consts);
431 Py_CLEAR(u->u_names);
432 Py_CLEAR(u->u_varnames);
433 Py_CLEAR(u->u_freevars);
434 Py_CLEAR(u->u_cellvars);
435 Py_CLEAR(u->u_private);
436 PyObject_Free(u);
439 static int
440 compiler_enter_scope(struct compiler *c, identifier name, void *key,
441 int lineno)
443 struct compiler_unit *u;
445 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
446 struct compiler_unit));
447 if (!u) {
448 PyErr_NoMemory();
449 return 0;
451 memset(u, 0, sizeof(struct compiler_unit));
452 u->u_argcount = 0;
453 u->u_ste = PySymtable_Lookup(c->c_st, key);
454 if (!u->u_ste) {
455 compiler_unit_free(u);
456 return 0;
458 Py_INCREF(name);
459 u->u_name = name;
460 u->u_varnames = list2dict(u->u_ste->ste_varnames);
461 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
462 if (!u->u_varnames || !u->u_cellvars) {
463 compiler_unit_free(u);
464 return 0;
467 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
468 PyDict_Size(u->u_cellvars));
469 if (!u->u_freevars) {
470 compiler_unit_free(u);
471 return 0;
474 u->u_blocks = NULL;
475 u->u_tmpname = 0;
476 u->u_nfblocks = 0;
477 u->u_firstlineno = lineno;
478 u->u_lineno = 0;
479 u->u_lineno_set = false;
480 u->u_consts = PyDict_New();
481 if (!u->u_consts) {
482 compiler_unit_free(u);
483 return 0;
485 u->u_names = PyDict_New();
486 if (!u->u_names) {
487 compiler_unit_free(u);
488 return 0;
491 u->u_private = NULL;
493 /* Push the old compiler_unit on the stack. */
494 if (c->u) {
495 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
496 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
497 Py_XDECREF(wrapper);
498 compiler_unit_free(u);
499 return 0;
501 Py_DECREF(wrapper);
502 u->u_private = c->u->u_private;
503 Py_XINCREF(u->u_private);
505 c->u = u;
507 c->c_nestlevel++;
508 if (compiler_use_new_block(c) == NULL)
509 return 0;
511 return 1;
514 static void
515 compiler_exit_scope(struct compiler *c)
517 int n;
518 PyObject *wrapper;
520 c->c_nestlevel--;
521 compiler_unit_free(c->u);
522 /* Restore c->u to the parent unit. */
523 n = PyList_GET_SIZE(c->c_stack) - 1;
524 if (n >= 0) {
525 wrapper = PyList_GET_ITEM(c->c_stack, n);
526 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
527 assert(c->u);
528 /* we are deleting from a list so this really shouldn't fail */
529 if (PySequence_DelItem(c->c_stack, n) < 0)
530 Py_FatalError("compiler_exit_scope()");
531 compiler_unit_check(c->u);
533 else
534 c->u = NULL;
538 /* Allocate a new "anonymous" local variable.
539 Used by list comprehensions and with statements.
542 static PyObject *
543 compiler_new_tmpname(struct compiler *c)
545 char tmpname[256];
546 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
547 return PyString_FromString(tmpname);
550 /* Allocate a new block and return a pointer to it.
551 Returns NULL on error.
554 static basicblock *
555 compiler_new_block(struct compiler *c)
557 basicblock *b;
558 struct compiler_unit *u;
560 u = c->u;
561 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
562 if (b == NULL) {
563 PyErr_NoMemory();
564 return NULL;
566 memset((void *)b, 0, sizeof(basicblock));
567 /* Extend the singly linked list of blocks with new block. */
568 b->b_list = u->u_blocks;
569 u->u_blocks = b;
570 return b;
573 static basicblock *
574 compiler_use_new_block(struct compiler *c)
576 basicblock *block = compiler_new_block(c);
577 if (block == NULL)
578 return NULL;
579 c->u->u_curblock = block;
580 return block;
583 static basicblock *
584 compiler_next_block(struct compiler *c)
586 basicblock *block = compiler_new_block(c);
587 if (block == NULL)
588 return NULL;
589 c->u->u_curblock->b_next = block;
590 c->u->u_curblock = block;
591 return block;
594 static basicblock *
595 compiler_use_next_block(struct compiler *c, basicblock *block)
597 assert(block != NULL);
598 c->u->u_curblock->b_next = block;
599 c->u->u_curblock = block;
600 return block;
603 /* Returns the offset of the next instruction in the current block's
604 b_instr array. Resizes the b_instr as necessary.
605 Returns -1 on failure.
608 static int
609 compiler_next_instr(struct compiler *c, basicblock *b)
611 assert(b != NULL);
612 if (b->b_instr == NULL) {
613 b->b_instr = (struct instr *)PyObject_Malloc(
614 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
615 if (b->b_instr == NULL) {
616 PyErr_NoMemory();
617 return -1;
619 b->b_ialloc = DEFAULT_BLOCK_SIZE;
620 memset((char *)b->b_instr, 0,
621 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
623 else if (b->b_iused == b->b_ialloc) {
624 struct instr *tmp;
625 size_t oldsize, newsize;
626 oldsize = b->b_ialloc * sizeof(struct instr);
627 newsize = oldsize << 1;
629 if (oldsize > (PY_SIZE_MAX >> 1)) {
630 PyErr_NoMemory();
631 return -1;
634 if (newsize == 0) {
635 PyErr_NoMemory();
636 return -1;
638 b->b_ialloc <<= 1;
639 tmp = (struct instr *)PyObject_Realloc(
640 (void *)b->b_instr, newsize);
641 if (tmp == NULL) {
642 PyErr_NoMemory();
643 return -1;
645 b->b_instr = tmp;
646 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
648 return b->b_iused++;
651 /* Set the i_lineno member of the instruction at offset off if the
652 line number for the current expression/statement has not
653 already been set. If it has been set, the call has no effect.
655 The line number is reset in the following cases:
656 - when entering a new scope
657 - on each statement
658 - on each expression that start a new line
659 - before the "except" clause
660 - before the "for" and "while" expressions
663 static void
664 compiler_set_lineno(struct compiler *c, int off)
666 basicblock *b;
667 if (c->u->u_lineno_set)
668 return;
669 c->u->u_lineno_set = true;
670 b = c->u->u_curblock;
671 b->b_instr[off].i_lineno = c->u->u_lineno;
674 static int
675 opcode_stack_effect(int opcode, int oparg)
677 switch (opcode) {
678 case POP_TOP:
679 return -1;
680 case ROT_TWO:
681 case ROT_THREE:
682 return 0;
683 case DUP_TOP:
684 return 1;
685 case ROT_FOUR:
686 return 0;
688 case UNARY_POSITIVE:
689 case UNARY_NEGATIVE:
690 case UNARY_NOT:
691 case UNARY_CONVERT:
692 case UNARY_INVERT:
693 return 0;
695 case LIST_APPEND:
696 return -1;
698 case BINARY_POWER:
699 case BINARY_MULTIPLY:
700 case BINARY_DIVIDE:
701 case BINARY_MODULO:
702 case BINARY_ADD:
703 case BINARY_SUBTRACT:
704 case BINARY_SUBSCR:
705 case BINARY_FLOOR_DIVIDE:
706 case BINARY_TRUE_DIVIDE:
707 return -1;
708 case INPLACE_FLOOR_DIVIDE:
709 case INPLACE_TRUE_DIVIDE:
710 return -1;
712 case SLICE+0:
713 return 1;
714 case SLICE+1:
715 return 0;
716 case SLICE+2:
717 return 0;
718 case SLICE+3:
719 return -1;
721 case STORE_SLICE+0:
722 return -2;
723 case STORE_SLICE+1:
724 return -3;
725 case STORE_SLICE+2:
726 return -3;
727 case STORE_SLICE+3:
728 return -4;
730 case DELETE_SLICE+0:
731 return -1;
732 case DELETE_SLICE+1:
733 return -2;
734 case DELETE_SLICE+2:
735 return -2;
736 case DELETE_SLICE+3:
737 return -3;
739 case INPLACE_ADD:
740 case INPLACE_SUBTRACT:
741 case INPLACE_MULTIPLY:
742 case INPLACE_DIVIDE:
743 case INPLACE_MODULO:
744 return -1;
745 case STORE_SUBSCR:
746 return -3;
747 case STORE_MAP:
748 return -2;
749 case DELETE_SUBSCR:
750 return -2;
752 case BINARY_LSHIFT:
753 case BINARY_RSHIFT:
754 case BINARY_AND:
755 case BINARY_XOR:
756 case BINARY_OR:
757 return -1;
758 case INPLACE_POWER:
759 return -1;
760 case GET_ITER:
761 return 0;
763 case PRINT_EXPR:
764 return -1;
765 case PRINT_ITEM:
766 return -1;
767 case PRINT_NEWLINE:
768 return 0;
769 case PRINT_ITEM_TO:
770 return -2;
771 case PRINT_NEWLINE_TO:
772 return -1;
773 case INPLACE_LSHIFT:
774 case INPLACE_RSHIFT:
775 case INPLACE_AND:
776 case INPLACE_XOR:
777 case INPLACE_OR:
778 return -1;
779 case BREAK_LOOP:
780 return 0;
781 case WITH_CLEANUP:
782 return -1; /* XXX Sometimes more */
783 case LOAD_LOCALS:
784 return 1;
785 case RETURN_VALUE:
786 return -1;
787 case IMPORT_STAR:
788 return -1;
789 case EXEC_STMT:
790 return -3;
791 case YIELD_VALUE:
792 return 0;
794 case POP_BLOCK:
795 return 0;
796 case END_FINALLY:
797 return -1; /* or -2 or -3 if exception occurred */
798 case BUILD_CLASS:
799 return -2;
801 case STORE_NAME:
802 return -1;
803 case DELETE_NAME:
804 return 0;
805 case UNPACK_SEQUENCE:
806 return oparg-1;
807 case FOR_ITER:
808 return 1;
810 case STORE_ATTR:
811 return -2;
812 case DELETE_ATTR:
813 return -1;
814 case STORE_GLOBAL:
815 return -1;
816 case DELETE_GLOBAL:
817 return 0;
818 case DUP_TOPX:
819 return oparg;
820 case LOAD_CONST:
821 return 1;
822 case LOAD_NAME:
823 return 1;
824 case BUILD_TUPLE:
825 case BUILD_LIST:
826 return 1-oparg;
827 case BUILD_MAP:
828 return 1;
829 case LOAD_ATTR:
830 return 0;
831 case COMPARE_OP:
832 return -1;
833 case IMPORT_NAME:
834 return 0;
835 case IMPORT_FROM:
836 return 1;
838 case JUMP_FORWARD:
839 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
840 case JUMP_IF_FALSE_OR_POP: /* "" */
841 case JUMP_ABSOLUTE:
842 return 0;
844 case POP_JUMP_IF_FALSE:
845 case POP_JUMP_IF_TRUE:
846 return -1;
848 case LOAD_GLOBAL:
849 return 1;
851 case CONTINUE_LOOP:
852 return 0;
853 case SETUP_LOOP:
854 return 0;
855 case SETUP_EXCEPT:
856 case SETUP_FINALLY:
857 return 3; /* actually pushed by an exception */
859 case LOAD_FAST:
860 return 1;
861 case STORE_FAST:
862 return -1;
863 case DELETE_FAST:
864 return 0;
866 case RAISE_VARARGS:
867 return -oparg;
868 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
869 case CALL_FUNCTION:
870 return -NARGS(oparg);
871 case CALL_FUNCTION_VAR:
872 case CALL_FUNCTION_KW:
873 return -NARGS(oparg)-1;
874 case CALL_FUNCTION_VAR_KW:
875 return -NARGS(oparg)-2;
876 #undef NARGS
877 case MAKE_FUNCTION:
878 return -oparg;
879 case BUILD_SLICE:
880 if (oparg == 3)
881 return -2;
882 else
883 return -1;
885 case MAKE_CLOSURE:
886 return -oparg;
887 case LOAD_CLOSURE:
888 return 1;
889 case LOAD_DEREF:
890 return 1;
891 case STORE_DEREF:
892 return -1;
893 default:
894 fprintf(stderr, "opcode = %d\n", opcode);
895 Py_FatalError("opcode_stack_effect()");
898 return 0; /* not reachable */
901 /* Add an opcode with no argument.
902 Returns 0 on failure, 1 on success.
905 static int
906 compiler_addop(struct compiler *c, int opcode)
908 basicblock *b;
909 struct instr *i;
910 int off;
911 off = compiler_next_instr(c, c->u->u_curblock);
912 if (off < 0)
913 return 0;
914 b = c->u->u_curblock;
915 i = &b->b_instr[off];
916 i->i_opcode = opcode;
917 i->i_hasarg = 0;
918 if (opcode == RETURN_VALUE)
919 b->b_return = 1;
920 compiler_set_lineno(c, off);
921 return 1;
924 static int
925 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
927 PyObject *t, *v;
928 Py_ssize_t arg;
929 unsigned char *p, *q;
930 Py_complex z;
931 double d;
932 int real_part_zero, imag_part_zero;
934 /* necessary to make sure types aren't coerced (e.g., int and long) */
935 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
936 if (PyFloat_Check(o)) {
937 d = PyFloat_AS_DOUBLE(o);
938 p = (unsigned char*) &d;
939 /* all we need is to make the tuple different in either the 0.0
940 * or -0.0 case from all others, just to avoid the "coercion".
942 if (*p==0 && p[sizeof(double)-1]==0)
943 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
944 else
945 t = PyTuple_Pack(2, o, o->ob_type);
947 else if (PyComplex_Check(o)) {
948 /* complex case is even messier: we need to make complex(x,
949 0.) different from complex(x, -0.) and complex(0., y)
950 different from complex(-0., y), for any x and y. In
951 particular, all four complex zeros should be
952 distinguished.*/
953 z = PyComplex_AsCComplex(o);
954 p = (unsigned char*) &(z.real);
955 q = (unsigned char*) &(z.imag);
956 /* all that matters here is that on IEEE platforms
957 real_part_zero will be true if z.real == 0., and false if
958 z.real == -0. In fact, real_part_zero will also be true
959 for some other rarely occurring nonzero floats, but this
960 doesn't matter. Similar comments apply to
961 imag_part_zero. */
962 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
963 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
964 if (real_part_zero && imag_part_zero) {
965 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
967 else if (real_part_zero && !imag_part_zero) {
968 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
970 else if (!real_part_zero && imag_part_zero) {
971 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
973 else {
974 t = PyTuple_Pack(2, o, o->ob_type);
977 else {
978 t = PyTuple_Pack(2, o, o->ob_type);
980 if (t == NULL)
981 return -1;
983 v = PyDict_GetItem(dict, t);
984 if (!v) {
985 arg = PyDict_Size(dict);
986 v = PyInt_FromLong(arg);
987 if (!v) {
988 Py_DECREF(t);
989 return -1;
991 if (PyDict_SetItem(dict, t, v) < 0) {
992 Py_DECREF(t);
993 Py_DECREF(v);
994 return -1;
996 Py_DECREF(v);
998 else
999 arg = PyInt_AsLong(v);
1000 Py_DECREF(t);
1001 return arg;
1004 static int
1005 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1006 PyObject *o)
1008 int arg = compiler_add_o(c, dict, o);
1009 if (arg < 0)
1010 return 0;
1011 return compiler_addop_i(c, opcode, arg);
1014 static int
1015 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1016 PyObject *o)
1018 int arg;
1019 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1020 if (!mangled)
1021 return 0;
1022 arg = compiler_add_o(c, dict, mangled);
1023 Py_DECREF(mangled);
1024 if (arg < 0)
1025 return 0;
1026 return compiler_addop_i(c, opcode, arg);
1029 /* Add an opcode with an integer argument.
1030 Returns 0 on failure, 1 on success.
1033 static int
1034 compiler_addop_i(struct compiler *c, int opcode, int oparg)
1036 struct instr *i;
1037 int off;
1038 off = compiler_next_instr(c, c->u->u_curblock);
1039 if (off < 0)
1040 return 0;
1041 i = &c->u->u_curblock->b_instr[off];
1042 i->i_opcode = opcode;
1043 i->i_oparg = oparg;
1044 i->i_hasarg = 1;
1045 compiler_set_lineno(c, off);
1046 return 1;
1049 static int
1050 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1052 struct instr *i;
1053 int off;
1055 assert(b != NULL);
1056 off = compiler_next_instr(c, c->u->u_curblock);
1057 if (off < 0)
1058 return 0;
1059 i = &c->u->u_curblock->b_instr[off];
1060 i->i_opcode = opcode;
1061 i->i_target = b;
1062 i->i_hasarg = 1;
1063 if (absolute)
1064 i->i_jabs = 1;
1065 else
1066 i->i_jrel = 1;
1067 compiler_set_lineno(c, off);
1068 return 1;
1071 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1072 like to find better names.) NEW_BLOCK() creates a new block and sets
1073 it as the current block. NEXT_BLOCK() also creates an implicit jump
1074 from the current block to the new block.
1077 /* The returns inside these macros make it impossible to decref objects
1078 created in the local function. Local objects should use the arena.
1082 #define NEW_BLOCK(C) { \
1083 if (compiler_use_new_block((C)) == NULL) \
1084 return 0; \
1087 #define NEXT_BLOCK(C) { \
1088 if (compiler_next_block((C)) == NULL) \
1089 return 0; \
1092 #define ADDOP(C, OP) { \
1093 if (!compiler_addop((C), (OP))) \
1094 return 0; \
1097 #define ADDOP_IN_SCOPE(C, OP) { \
1098 if (!compiler_addop((C), (OP))) { \
1099 compiler_exit_scope(c); \
1100 return 0; \
1104 #define ADDOP_O(C, OP, O, TYPE) { \
1105 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1106 return 0; \
1109 #define ADDOP_NAME(C, OP, O, TYPE) { \
1110 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1111 return 0; \
1114 #define ADDOP_I(C, OP, O) { \
1115 if (!compiler_addop_i((C), (OP), (O))) \
1116 return 0; \
1119 #define ADDOP_JABS(C, OP, O) { \
1120 if (!compiler_addop_j((C), (OP), (O), 1)) \
1121 return 0; \
1124 #define ADDOP_JREL(C, OP, O) { \
1125 if (!compiler_addop_j((C), (OP), (O), 0)) \
1126 return 0; \
1129 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1130 the ASDL name to synthesize the name of the C type and the visit function.
1133 #define VISIT(C, TYPE, V) {\
1134 if (!compiler_visit_ ## TYPE((C), (V))) \
1135 return 0; \
1138 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1139 if (!compiler_visit_ ## TYPE((C), (V))) { \
1140 compiler_exit_scope(c); \
1141 return 0; \
1145 #define VISIT_SLICE(C, V, CTX) {\
1146 if (!compiler_visit_slice((C), (V), (CTX))) \
1147 return 0; \
1150 #define VISIT_SEQ(C, TYPE, SEQ) { \
1151 int _i; \
1152 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1153 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1154 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1155 if (!compiler_visit_ ## TYPE((C), elt)) \
1156 return 0; \
1160 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1161 int _i; \
1162 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1163 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1164 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1165 if (!compiler_visit_ ## TYPE((C), elt)) { \
1166 compiler_exit_scope(c); \
1167 return 0; \
1172 static int
1173 compiler_isdocstring(stmt_ty s)
1175 if (s->kind != Expr_kind)
1176 return 0;
1177 return s->v.Expr.value->kind == Str_kind;
1180 /* Compile a sequence of statements, checking for a docstring. */
1182 static int
1183 compiler_body(struct compiler *c, asdl_seq *stmts)
1185 int i = 0;
1186 stmt_ty st;
1188 if (!asdl_seq_LEN(stmts))
1189 return 1;
1190 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1191 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1192 /* don't generate docstrings if -OO */
1193 i = 1;
1194 VISIT(c, expr, st->v.Expr.value);
1195 if (!compiler_nameop(c, __doc__, Store))
1196 return 0;
1198 for (; i < asdl_seq_LEN(stmts); i++)
1199 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1200 return 1;
1203 static PyCodeObject *
1204 compiler_mod(struct compiler *c, mod_ty mod)
1206 PyCodeObject *co;
1207 int addNone = 1;
1208 static PyObject *module;
1209 if (!module) {
1210 module = PyString_InternFromString("<module>");
1211 if (!module)
1212 return NULL;
1214 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1215 if (!compiler_enter_scope(c, module, mod, 0))
1216 return NULL;
1217 switch (mod->kind) {
1218 case Module_kind:
1219 if (!compiler_body(c, mod->v.Module.body)) {
1220 compiler_exit_scope(c);
1221 return 0;
1223 break;
1224 case Interactive_kind:
1225 c->c_interactive = 1;
1226 VISIT_SEQ_IN_SCOPE(c, stmt,
1227 mod->v.Interactive.body);
1228 break;
1229 case Expression_kind:
1230 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1231 addNone = 0;
1232 break;
1233 case Suite_kind:
1234 PyErr_SetString(PyExc_SystemError,
1235 "suite should not be possible");
1236 return 0;
1237 default:
1238 PyErr_Format(PyExc_SystemError,
1239 "module kind %d should not be possible",
1240 mod->kind);
1241 return 0;
1243 co = assemble(c, addNone);
1244 compiler_exit_scope(c);
1245 return co;
1248 /* The test for LOCAL must come before the test for FREE in order to
1249 handle classes where name is both local and free. The local var is
1250 a method and the free var is a free var referenced within a method.
1253 static int
1254 get_ref_type(struct compiler *c, PyObject *name)
1256 int scope = PyST_GetScope(c->u->u_ste, name);
1257 if (scope == 0) {
1258 char buf[350];
1259 PyOS_snprintf(buf, sizeof(buf),
1260 "unknown scope for %.100s in %.100s(%s) in %s\n"
1261 "symbols: %s\nlocals: %s\nglobals: %s",
1262 PyString_AS_STRING(name),
1263 PyString_AS_STRING(c->u->u_name),
1264 PyObject_REPR(c->u->u_ste->ste_id),
1265 c->c_filename,
1266 PyObject_REPR(c->u->u_ste->ste_symbols),
1267 PyObject_REPR(c->u->u_varnames),
1268 PyObject_REPR(c->u->u_names)
1270 Py_FatalError(buf);
1273 return scope;
1276 static int
1277 compiler_lookup_arg(PyObject *dict, PyObject *name)
1279 PyObject *k, *v;
1280 k = PyTuple_Pack(2, name, name->ob_type);
1281 if (k == NULL)
1282 return -1;
1283 v = PyDict_GetItem(dict, k);
1284 Py_DECREF(k);
1285 if (v == NULL)
1286 return -1;
1287 return PyInt_AS_LONG(v);
1290 static int
1291 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1293 int i, free = PyCode_GetNumFree(co);
1294 if (free == 0) {
1295 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1296 ADDOP_I(c, MAKE_FUNCTION, args);
1297 return 1;
1299 for (i = 0; i < free; ++i) {
1300 /* Bypass com_addop_varname because it will generate
1301 LOAD_DEREF but LOAD_CLOSURE is needed.
1303 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1304 int arg, reftype;
1306 /* Special case: If a class contains a method with a
1307 free variable that has the same name as a method,
1308 the name will be considered free *and* local in the
1309 class. It should be handled by the closure, as
1310 well as by the normal name loookup logic.
1312 reftype = get_ref_type(c, name);
1313 if (reftype == CELL)
1314 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1315 else /* (reftype == FREE) */
1316 arg = compiler_lookup_arg(c->u->u_freevars, name);
1317 if (arg == -1) {
1318 printf("lookup %s in %s %d %d\n"
1319 "freevars of %s: %s\n",
1320 PyObject_REPR(name),
1321 PyString_AS_STRING(c->u->u_name),
1322 reftype, arg,
1323 PyString_AS_STRING(co->co_name),
1324 PyObject_REPR(co->co_freevars));
1325 Py_FatalError("compiler_make_closure()");
1327 ADDOP_I(c, LOAD_CLOSURE, arg);
1329 ADDOP_I(c, BUILD_TUPLE, free);
1330 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1331 ADDOP_I(c, MAKE_CLOSURE, args);
1332 return 1;
1335 static int
1336 compiler_decorators(struct compiler *c, asdl_seq* decos)
1338 int i;
1340 if (!decos)
1341 return 1;
1343 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1344 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1346 return 1;
1349 static int
1350 compiler_arguments(struct compiler *c, arguments_ty args)
1352 int i;
1353 int n = asdl_seq_LEN(args->args);
1354 /* Correctly handle nested argument lists */
1355 for (i = 0; i < n; i++) {
1356 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
1357 if (arg->kind == Tuple_kind) {
1358 PyObject *id = PyString_FromFormat(".%d", i);
1359 if (id == NULL) {
1360 return 0;
1362 if (!compiler_nameop(c, id, Load)) {
1363 Py_DECREF(id);
1364 return 0;
1366 Py_DECREF(id);
1367 VISIT(c, expr, arg);
1370 return 1;
1373 static int
1374 compiler_function(struct compiler *c, stmt_ty s)
1376 PyCodeObject *co;
1377 PyObject *first_const = Py_None;
1378 arguments_ty args = s->v.FunctionDef.args;
1379 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1380 stmt_ty st;
1381 int i, n, docstring;
1383 assert(s->kind == FunctionDef_kind);
1385 if (!compiler_decorators(c, decos))
1386 return 0;
1387 if (args->defaults)
1388 VISIT_SEQ(c, expr, args->defaults);
1389 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1390 s->lineno))
1391 return 0;
1393 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1394 docstring = compiler_isdocstring(st);
1395 if (docstring && Py_OptimizeFlag < 2)
1396 first_const = st->v.Expr.value->v.Str.s;
1397 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1398 compiler_exit_scope(c);
1399 return 0;
1402 /* unpack nested arguments */
1403 compiler_arguments(c, args);
1405 c->u->u_argcount = asdl_seq_LEN(args->args);
1406 n = asdl_seq_LEN(s->v.FunctionDef.body);
1407 /* if there was a docstring, we need to skip the first statement */
1408 for (i = docstring; i < n; i++) {
1409 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1410 VISIT_IN_SCOPE(c, stmt, st);
1412 co = assemble(c, 1);
1413 compiler_exit_scope(c);
1414 if (co == NULL)
1415 return 0;
1417 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1418 Py_DECREF(co);
1420 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1421 ADDOP_I(c, CALL_FUNCTION, 1);
1424 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1427 static int
1428 compiler_class(struct compiler *c, stmt_ty s)
1430 int n, i;
1431 PyCodeObject *co;
1432 PyObject *str;
1433 asdl_seq* decos = s->v.ClassDef.decorator_list;
1435 if (!compiler_decorators(c, decos))
1436 return 0;
1438 /* push class name on stack, needed by BUILD_CLASS */
1439 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1440 /* push the tuple of base classes on the stack */
1441 n = asdl_seq_LEN(s->v.ClassDef.bases);
1442 if (n > 0)
1443 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1444 ADDOP_I(c, BUILD_TUPLE, n);
1445 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1446 s->lineno))
1447 return 0;
1448 Py_XDECREF(c->u->u_private);
1449 c->u->u_private = s->v.ClassDef.name;
1450 Py_INCREF(c->u->u_private);
1451 str = PyString_InternFromString("__name__");
1452 if (!str || !compiler_nameop(c, str, Load)) {
1453 Py_XDECREF(str);
1454 compiler_exit_scope(c);
1455 return 0;
1458 Py_DECREF(str);
1459 str = PyString_InternFromString("__module__");
1460 if (!str || !compiler_nameop(c, str, Store)) {
1461 Py_XDECREF(str);
1462 compiler_exit_scope(c);
1463 return 0;
1465 Py_DECREF(str);
1467 if (!compiler_body(c, s->v.ClassDef.body)) {
1468 compiler_exit_scope(c);
1469 return 0;
1472 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1473 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1474 co = assemble(c, 1);
1475 compiler_exit_scope(c);
1476 if (co == NULL)
1477 return 0;
1479 compiler_make_closure(c, co, 0);
1480 Py_DECREF(co);
1482 ADDOP_I(c, CALL_FUNCTION, 0);
1483 ADDOP(c, BUILD_CLASS);
1484 /* apply decorators */
1485 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1486 ADDOP_I(c, CALL_FUNCTION, 1);
1488 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1489 return 0;
1490 return 1;
1493 static int
1494 compiler_ifexp(struct compiler *c, expr_ty e)
1496 basicblock *end, *next;
1498 assert(e->kind == IfExp_kind);
1499 end = compiler_new_block(c);
1500 if (end == NULL)
1501 return 0;
1502 next = compiler_new_block(c);
1503 if (next == NULL)
1504 return 0;
1505 VISIT(c, expr, e->v.IfExp.test);
1506 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1507 VISIT(c, expr, e->v.IfExp.body);
1508 ADDOP_JREL(c, JUMP_FORWARD, end);
1509 compiler_use_next_block(c, next);
1510 VISIT(c, expr, e->v.IfExp.orelse);
1511 compiler_use_next_block(c, end);
1512 return 1;
1515 static int
1516 compiler_lambda(struct compiler *c, expr_ty e)
1518 PyCodeObject *co;
1519 static identifier name;
1520 arguments_ty args = e->v.Lambda.args;
1521 assert(e->kind == Lambda_kind);
1523 if (!name) {
1524 name = PyString_InternFromString("<lambda>");
1525 if (!name)
1526 return 0;
1529 if (args->defaults)
1530 VISIT_SEQ(c, expr, args->defaults);
1531 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1532 return 0;
1534 /* unpack nested arguments */
1535 compiler_arguments(c, args);
1537 c->u->u_argcount = asdl_seq_LEN(args->args);
1538 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1539 if (c->u->u_ste->ste_generator) {
1540 ADDOP_IN_SCOPE(c, POP_TOP);
1542 else {
1543 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1545 co = assemble(c, 1);
1546 compiler_exit_scope(c);
1547 if (co == NULL)
1548 return 0;
1550 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1551 Py_DECREF(co);
1553 return 1;
1556 static int
1557 compiler_print(struct compiler *c, stmt_ty s)
1559 int i, n;
1560 bool dest;
1562 assert(s->kind == Print_kind);
1563 n = asdl_seq_LEN(s->v.Print.values);
1564 dest = false;
1565 if (s->v.Print.dest) {
1566 VISIT(c, expr, s->v.Print.dest);
1567 dest = true;
1569 for (i = 0; i < n; i++) {
1570 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1571 if (dest) {
1572 ADDOP(c, DUP_TOP);
1573 VISIT(c, expr, e);
1574 ADDOP(c, ROT_TWO);
1575 ADDOP(c, PRINT_ITEM_TO);
1577 else {
1578 VISIT(c, expr, e);
1579 ADDOP(c, PRINT_ITEM);
1582 if (s->v.Print.nl) {
1583 if (dest)
1584 ADDOP(c, PRINT_NEWLINE_TO)
1585 else
1586 ADDOP(c, PRINT_NEWLINE)
1588 else if (dest)
1589 ADDOP(c, POP_TOP);
1590 return 1;
1593 static int
1594 compiler_if(struct compiler *c, stmt_ty s)
1596 basicblock *end, *next;
1597 int constant;
1598 assert(s->kind == If_kind);
1599 end = compiler_new_block(c);
1600 if (end == NULL)
1601 return 0;
1603 constant = expr_constant(s->v.If.test);
1604 /* constant = 0: "if 0"
1605 * constant = 1: "if 1", "if 2", ...
1606 * constant = -1: rest */
1607 if (constant == 0) {
1608 if (s->v.If.orelse)
1609 VISIT_SEQ(c, stmt, s->v.If.orelse);
1610 } else if (constant == 1) {
1611 VISIT_SEQ(c, stmt, s->v.If.body);
1612 } else {
1613 if (s->v.If.orelse) {
1614 next = compiler_new_block(c);
1615 if (next == NULL)
1616 return 0;
1618 else
1619 next = end;
1620 VISIT(c, expr, s->v.If.test);
1621 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1622 VISIT_SEQ(c, stmt, s->v.If.body);
1623 ADDOP_JREL(c, JUMP_FORWARD, end);
1624 if (s->v.If.orelse) {
1625 compiler_use_next_block(c, next);
1626 VISIT_SEQ(c, stmt, s->v.If.orelse);
1629 compiler_use_next_block(c, end);
1630 return 1;
1633 static int
1634 compiler_for(struct compiler *c, stmt_ty s)
1636 basicblock *start, *cleanup, *end;
1638 start = compiler_new_block(c);
1639 cleanup = compiler_new_block(c);
1640 end = compiler_new_block(c);
1641 if (start == NULL || end == NULL || cleanup == NULL)
1642 return 0;
1643 ADDOP_JREL(c, SETUP_LOOP, end);
1644 if (!compiler_push_fblock(c, LOOP, start))
1645 return 0;
1646 VISIT(c, expr, s->v.For.iter);
1647 ADDOP(c, GET_ITER);
1648 compiler_use_next_block(c, start);
1649 /* for expressions must be traced on each iteration,
1650 so we need to set an extra line number. */
1651 c->u->u_lineno_set = false;
1652 ADDOP_JREL(c, FOR_ITER, cleanup);
1653 VISIT(c, expr, s->v.For.target);
1654 VISIT_SEQ(c, stmt, s->v.For.body);
1655 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1656 compiler_use_next_block(c, cleanup);
1657 ADDOP(c, POP_BLOCK);
1658 compiler_pop_fblock(c, LOOP, start);
1659 VISIT_SEQ(c, stmt, s->v.For.orelse);
1660 compiler_use_next_block(c, end);
1661 return 1;
1664 static int
1665 compiler_while(struct compiler *c, stmt_ty s)
1667 basicblock *loop, *orelse, *end, *anchor = NULL;
1668 int constant = expr_constant(s->v.While.test);
1670 if (constant == 0) {
1671 if (s->v.While.orelse)
1672 VISIT_SEQ(c, stmt, s->v.While.orelse);
1673 return 1;
1675 loop = compiler_new_block(c);
1676 end = compiler_new_block(c);
1677 if (constant == -1) {
1678 anchor = compiler_new_block(c);
1679 if (anchor == NULL)
1680 return 0;
1682 if (loop == NULL || end == NULL)
1683 return 0;
1684 if (s->v.While.orelse) {
1685 orelse = compiler_new_block(c);
1686 if (orelse == NULL)
1687 return 0;
1689 else
1690 orelse = NULL;
1692 ADDOP_JREL(c, SETUP_LOOP, end);
1693 compiler_use_next_block(c, loop);
1694 if (!compiler_push_fblock(c, LOOP, loop))
1695 return 0;
1696 if (constant == -1) {
1697 /* while expressions must be traced on each iteration,
1698 so we need to set an extra line number. */
1699 c->u->u_lineno_set = false;
1700 VISIT(c, expr, s->v.While.test);
1701 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1703 VISIT_SEQ(c, stmt, s->v.While.body);
1704 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1706 /* XXX should the two POP instructions be in a separate block
1707 if there is no else clause ?
1710 if (constant == -1) {
1711 compiler_use_next_block(c, anchor);
1712 ADDOP(c, POP_BLOCK);
1714 compiler_pop_fblock(c, LOOP, loop);
1715 if (orelse != NULL) /* what if orelse is just pass? */
1716 VISIT_SEQ(c, stmt, s->v.While.orelse);
1717 compiler_use_next_block(c, end);
1719 return 1;
1722 static int
1723 compiler_continue(struct compiler *c)
1725 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1726 static const char IN_FINALLY_ERROR_MSG[] =
1727 "'continue' not supported inside 'finally' clause";
1728 int i;
1730 if (!c->u->u_nfblocks)
1731 return compiler_error(c, LOOP_ERROR_MSG);
1732 i = c->u->u_nfblocks - 1;
1733 switch (c->u->u_fblock[i].fb_type) {
1734 case LOOP:
1735 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1736 break;
1737 case EXCEPT:
1738 case FINALLY_TRY:
1739 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1740 /* Prevent continue anywhere under a finally
1741 even if hidden in a sub-try or except. */
1742 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1743 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1745 if (i == -1)
1746 return compiler_error(c, LOOP_ERROR_MSG);
1747 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1748 break;
1749 case FINALLY_END:
1750 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1753 return 1;
1756 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1758 SETUP_FINALLY L
1759 <code for body>
1760 POP_BLOCK
1761 LOAD_CONST <None>
1762 L: <code for finalbody>
1763 END_FINALLY
1765 The special instructions use the block stack. Each block
1766 stack entry contains the instruction that created it (here
1767 SETUP_FINALLY), the level of the value stack at the time the
1768 block stack entry was created, and a label (here L).
1770 SETUP_FINALLY:
1771 Pushes the current value stack level and the label
1772 onto the block stack.
1773 POP_BLOCK:
1774 Pops en entry from the block stack, and pops the value
1775 stack until its level is the same as indicated on the
1776 block stack. (The label is ignored.)
1777 END_FINALLY:
1778 Pops a variable number of entries from the *value* stack
1779 and re-raises the exception they specify. The number of
1780 entries popped depends on the (pseudo) exception type.
1782 The block stack is unwound when an exception is raised:
1783 when a SETUP_FINALLY entry is found, the exception is pushed
1784 onto the value stack (and the exception condition is cleared),
1785 and the interpreter jumps to the label gotten from the block
1786 stack.
1789 static int
1790 compiler_try_finally(struct compiler *c, stmt_ty s)
1792 basicblock *body, *end;
1793 body = compiler_new_block(c);
1794 end = compiler_new_block(c);
1795 if (body == NULL || end == NULL)
1796 return 0;
1798 ADDOP_JREL(c, SETUP_FINALLY, end);
1799 compiler_use_next_block(c, body);
1800 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1801 return 0;
1802 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1803 ADDOP(c, POP_BLOCK);
1804 compiler_pop_fblock(c, FINALLY_TRY, body);
1806 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1807 compiler_use_next_block(c, end);
1808 if (!compiler_push_fblock(c, FINALLY_END, end))
1809 return 0;
1810 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1811 ADDOP(c, END_FINALLY);
1812 compiler_pop_fblock(c, FINALLY_END, end);
1814 return 1;
1818 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1819 (The contents of the value stack is shown in [], with the top
1820 at the right; 'tb' is trace-back info, 'val' the exception's
1821 associated value, and 'exc' the exception.)
1823 Value stack Label Instruction Argument
1824 [] SETUP_EXCEPT L1
1825 [] <code for S>
1826 [] POP_BLOCK
1827 [] JUMP_FORWARD L0
1829 [tb, val, exc] L1: DUP )
1830 [tb, val, exc, exc] <evaluate E1> )
1831 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1832 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1833 [tb, val, exc] POP
1834 [tb, val] <assign to V1> (or POP if no V1)
1835 [tb] POP
1836 [] <code for S1>
1837 JUMP_FORWARD L0
1839 [tb, val, exc] L2: DUP
1840 .............................etc.......................
1842 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1844 [] L0: <next statement>
1846 Of course, parts are not generated if Vi or Ei is not present.
1848 static int
1849 compiler_try_except(struct compiler *c, stmt_ty s)
1851 basicblock *body, *orelse, *except, *end;
1852 int i, n;
1854 body = compiler_new_block(c);
1855 except = compiler_new_block(c);
1856 orelse = compiler_new_block(c);
1857 end = compiler_new_block(c);
1858 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1859 return 0;
1860 ADDOP_JREL(c, SETUP_EXCEPT, except);
1861 compiler_use_next_block(c, body);
1862 if (!compiler_push_fblock(c, EXCEPT, body))
1863 return 0;
1864 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1865 ADDOP(c, POP_BLOCK);
1866 compiler_pop_fblock(c, EXCEPT, body);
1867 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1868 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1869 compiler_use_next_block(c, except);
1870 for (i = 0; i < n; i++) {
1871 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1872 s->v.TryExcept.handlers, i);
1873 if (!handler->v.ExceptHandler.type && i < n-1)
1874 return compiler_error(c, "default 'except:' must be last");
1875 c->u->u_lineno_set = false;
1876 c->u->u_lineno = handler->lineno;
1877 except = compiler_new_block(c);
1878 if (except == NULL)
1879 return 0;
1880 if (handler->v.ExceptHandler.type) {
1881 ADDOP(c, DUP_TOP);
1882 VISIT(c, expr, handler->v.ExceptHandler.type);
1883 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1884 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1886 ADDOP(c, POP_TOP);
1887 if (handler->v.ExceptHandler.name) {
1888 VISIT(c, expr, handler->v.ExceptHandler.name);
1890 else {
1891 ADDOP(c, POP_TOP);
1893 ADDOP(c, POP_TOP);
1894 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
1895 ADDOP_JREL(c, JUMP_FORWARD, end);
1896 compiler_use_next_block(c, except);
1898 ADDOP(c, END_FINALLY);
1899 compiler_use_next_block(c, orelse);
1900 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
1901 compiler_use_next_block(c, end);
1902 return 1;
1905 static int
1906 compiler_import_as(struct compiler *c, identifier name, identifier asname)
1908 /* The IMPORT_NAME opcode was already generated. This function
1909 merely needs to bind the result to a name.
1911 If there is a dot in name, we need to split it and emit a
1912 LOAD_ATTR for each name.
1914 const char *src = PyString_AS_STRING(name);
1915 const char *dot = strchr(src, '.');
1916 if (dot) {
1917 /* Consume the base module name to get the first attribute */
1918 src = dot + 1;
1919 while (dot) {
1920 /* NB src is only defined when dot != NULL */
1921 PyObject *attr;
1922 dot = strchr(src, '.');
1923 attr = PyString_FromStringAndSize(src,
1924 dot ? dot - src : strlen(src));
1925 if (!attr)
1926 return -1;
1927 ADDOP_O(c, LOAD_ATTR, attr, names);
1928 Py_DECREF(attr);
1929 src = dot + 1;
1932 return compiler_nameop(c, asname, Store);
1935 static int
1936 compiler_import(struct compiler *c, stmt_ty s)
1938 /* The Import node stores a module name like a.b.c as a single
1939 string. This is convenient for all cases except
1940 import a.b.c as d
1941 where we need to parse that string to extract the individual
1942 module names.
1943 XXX Perhaps change the representation to make this case simpler?
1945 int i, n = asdl_seq_LEN(s->v.Import.names);
1947 for (i = 0; i < n; i++) {
1948 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
1949 int r;
1950 PyObject *level;
1952 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1953 level = PyInt_FromLong(0);
1954 else
1955 level = PyInt_FromLong(-1);
1957 if (level == NULL)
1958 return 0;
1960 ADDOP_O(c, LOAD_CONST, level, consts);
1961 Py_DECREF(level);
1962 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1963 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1965 if (alias->asname) {
1966 r = compiler_import_as(c, alias->name, alias->asname);
1967 if (!r)
1968 return r;
1970 else {
1971 identifier tmp = alias->name;
1972 const char *base = PyString_AS_STRING(alias->name);
1973 char *dot = strchr(base, '.');
1974 if (dot)
1975 tmp = PyString_FromStringAndSize(base,
1976 dot - base);
1977 r = compiler_nameop(c, tmp, Store);
1978 if (dot) {
1979 Py_DECREF(tmp);
1981 if (!r)
1982 return r;
1985 return 1;
1988 static int
1989 compiler_from_import(struct compiler *c, stmt_ty s)
1991 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
1993 PyObject *names = PyTuple_New(n);
1994 PyObject *level;
1996 if (!names)
1997 return 0;
1999 if (s->v.ImportFrom.level == 0 && c->c_flags &&
2000 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
2001 level = PyInt_FromLong(-1);
2002 else
2003 level = PyInt_FromLong(s->v.ImportFrom.level);
2005 if (!level) {
2006 Py_DECREF(names);
2007 return 0;
2010 /* build up the names */
2011 for (i = 0; i < n; i++) {
2012 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2013 Py_INCREF(alias->name);
2014 PyTuple_SET_ITEM(names, i, alias->name);
2017 if (s->lineno > c->c_future->ff_lineno) {
2018 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2019 "__future__")) {
2020 Py_DECREF(level);
2021 Py_DECREF(names);
2022 return compiler_error(c,
2023 "from __future__ imports must occur "
2024 "at the beginning of the file");
2029 ADDOP_O(c, LOAD_CONST, level, consts);
2030 Py_DECREF(level);
2031 ADDOP_O(c, LOAD_CONST, names, consts);
2032 Py_DECREF(names);
2033 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2034 for (i = 0; i < n; i++) {
2035 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2036 identifier store_name;
2038 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2039 assert(n == 1);
2040 ADDOP(c, IMPORT_STAR);
2041 return 1;
2044 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2045 store_name = alias->name;
2046 if (alias->asname)
2047 store_name = alias->asname;
2049 if (!compiler_nameop(c, store_name, Store)) {
2050 Py_DECREF(names);
2051 return 0;
2054 /* remove imported module */
2055 ADDOP(c, POP_TOP);
2056 return 1;
2059 static int
2060 compiler_assert(struct compiler *c, stmt_ty s)
2062 static PyObject *assertion_error = NULL;
2063 basicblock *end;
2065 if (Py_OptimizeFlag)
2066 return 1;
2067 if (assertion_error == NULL) {
2068 assertion_error = PyString_InternFromString("AssertionError");
2069 if (assertion_error == NULL)
2070 return 0;
2072 if (s->v.Assert.test->kind == Tuple_kind &&
2073 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2074 const char* msg =
2075 "assertion is always true, perhaps remove parentheses?";
2076 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2077 c->u->u_lineno, NULL, NULL) == -1)
2078 return 0;
2080 VISIT(c, expr, s->v.Assert.test);
2081 end = compiler_new_block(c);
2082 if (end == NULL)
2083 return 0;
2084 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2085 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2086 if (s->v.Assert.msg) {
2087 VISIT(c, expr, s->v.Assert.msg);
2088 ADDOP_I(c, RAISE_VARARGS, 2);
2090 else {
2091 ADDOP_I(c, RAISE_VARARGS, 1);
2093 compiler_use_next_block(c, end);
2094 return 1;
2097 static int
2098 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2100 int i, n;
2102 /* Always assign a lineno to the next instruction for a stmt. */
2103 c->u->u_lineno = s->lineno;
2104 c->u->u_lineno_set = false;
2106 switch (s->kind) {
2107 case FunctionDef_kind:
2108 return compiler_function(c, s);
2109 case ClassDef_kind:
2110 return compiler_class(c, s);
2111 case Return_kind:
2112 if (c->u->u_ste->ste_type != FunctionBlock)
2113 return compiler_error(c, "'return' outside function");
2114 if (s->v.Return.value) {
2115 VISIT(c, expr, s->v.Return.value);
2117 else
2118 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2119 ADDOP(c, RETURN_VALUE);
2120 break;
2121 case Delete_kind:
2122 VISIT_SEQ(c, expr, s->v.Delete.targets)
2123 break;
2124 case Assign_kind:
2125 n = asdl_seq_LEN(s->v.Assign.targets);
2126 VISIT(c, expr, s->v.Assign.value);
2127 for (i = 0; i < n; i++) {
2128 if (i < n - 1)
2129 ADDOP(c, DUP_TOP);
2130 VISIT(c, expr,
2131 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2133 break;
2134 case AugAssign_kind:
2135 return compiler_augassign(c, s);
2136 case Print_kind:
2137 return compiler_print(c, s);
2138 case For_kind:
2139 return compiler_for(c, s);
2140 case While_kind:
2141 return compiler_while(c, s);
2142 case If_kind:
2143 return compiler_if(c, s);
2144 case Raise_kind:
2145 n = 0;
2146 if (s->v.Raise.type) {
2147 VISIT(c, expr, s->v.Raise.type);
2148 n++;
2149 if (s->v.Raise.inst) {
2150 VISIT(c, expr, s->v.Raise.inst);
2151 n++;
2152 if (s->v.Raise.tback) {
2153 VISIT(c, expr, s->v.Raise.tback);
2154 n++;
2158 ADDOP_I(c, RAISE_VARARGS, n);
2159 break;
2160 case TryExcept_kind:
2161 return compiler_try_except(c, s);
2162 case TryFinally_kind:
2163 return compiler_try_finally(c, s);
2164 case Assert_kind:
2165 return compiler_assert(c, s);
2166 case Import_kind:
2167 return compiler_import(c, s);
2168 case ImportFrom_kind:
2169 return compiler_from_import(c, s);
2170 case Exec_kind:
2171 VISIT(c, expr, s->v.Exec.body);
2172 if (s->v.Exec.globals) {
2173 VISIT(c, expr, s->v.Exec.globals);
2174 if (s->v.Exec.locals) {
2175 VISIT(c, expr, s->v.Exec.locals);
2176 } else {
2177 ADDOP(c, DUP_TOP);
2179 } else {
2180 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2181 ADDOP(c, DUP_TOP);
2183 ADDOP(c, EXEC_STMT);
2184 break;
2185 case Global_kind:
2186 break;
2187 case Expr_kind:
2188 if (c->c_interactive && c->c_nestlevel <= 1) {
2189 VISIT(c, expr, s->v.Expr.value);
2190 ADDOP(c, PRINT_EXPR);
2192 else if (s->v.Expr.value->kind != Str_kind &&
2193 s->v.Expr.value->kind != Num_kind) {
2194 VISIT(c, expr, s->v.Expr.value);
2195 ADDOP(c, POP_TOP);
2197 break;
2198 case Pass_kind:
2199 break;
2200 case Break_kind:
2201 if (!compiler_in_loop(c))
2202 return compiler_error(c, "'break' outside loop");
2203 ADDOP(c, BREAK_LOOP);
2204 break;
2205 case Continue_kind:
2206 return compiler_continue(c);
2207 case With_kind:
2208 return compiler_with(c, s);
2210 return 1;
2213 static int
2214 unaryop(unaryop_ty op)
2216 switch (op) {
2217 case Invert:
2218 return UNARY_INVERT;
2219 case Not:
2220 return UNARY_NOT;
2221 case UAdd:
2222 return UNARY_POSITIVE;
2223 case USub:
2224 return UNARY_NEGATIVE;
2225 default:
2226 PyErr_Format(PyExc_SystemError,
2227 "unary op %d should not be possible", op);
2228 return 0;
2232 static int
2233 binop(struct compiler *c, operator_ty op)
2235 switch (op) {
2236 case Add:
2237 return BINARY_ADD;
2238 case Sub:
2239 return BINARY_SUBTRACT;
2240 case Mult:
2241 return BINARY_MULTIPLY;
2242 case Div:
2243 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2244 return BINARY_TRUE_DIVIDE;
2245 else
2246 return BINARY_DIVIDE;
2247 case Mod:
2248 return BINARY_MODULO;
2249 case Pow:
2250 return BINARY_POWER;
2251 case LShift:
2252 return BINARY_LSHIFT;
2253 case RShift:
2254 return BINARY_RSHIFT;
2255 case BitOr:
2256 return BINARY_OR;
2257 case BitXor:
2258 return BINARY_XOR;
2259 case BitAnd:
2260 return BINARY_AND;
2261 case FloorDiv:
2262 return BINARY_FLOOR_DIVIDE;
2263 default:
2264 PyErr_Format(PyExc_SystemError,
2265 "binary op %d should not be possible", op);
2266 return 0;
2270 static int
2271 cmpop(cmpop_ty op)
2273 switch (op) {
2274 case Eq:
2275 return PyCmp_EQ;
2276 case NotEq:
2277 return PyCmp_NE;
2278 case Lt:
2279 return PyCmp_LT;
2280 case LtE:
2281 return PyCmp_LE;
2282 case Gt:
2283 return PyCmp_GT;
2284 case GtE:
2285 return PyCmp_GE;
2286 case Is:
2287 return PyCmp_IS;
2288 case IsNot:
2289 return PyCmp_IS_NOT;
2290 case In:
2291 return PyCmp_IN;
2292 case NotIn:
2293 return PyCmp_NOT_IN;
2294 default:
2295 return PyCmp_BAD;
2299 static int
2300 inplace_binop(struct compiler *c, operator_ty op)
2302 switch (op) {
2303 case Add:
2304 return INPLACE_ADD;
2305 case Sub:
2306 return INPLACE_SUBTRACT;
2307 case Mult:
2308 return INPLACE_MULTIPLY;
2309 case Div:
2310 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2311 return INPLACE_TRUE_DIVIDE;
2312 else
2313 return INPLACE_DIVIDE;
2314 case Mod:
2315 return INPLACE_MODULO;
2316 case Pow:
2317 return INPLACE_POWER;
2318 case LShift:
2319 return INPLACE_LSHIFT;
2320 case RShift:
2321 return INPLACE_RSHIFT;
2322 case BitOr:
2323 return INPLACE_OR;
2324 case BitXor:
2325 return INPLACE_XOR;
2326 case BitAnd:
2327 return INPLACE_AND;
2328 case FloorDiv:
2329 return INPLACE_FLOOR_DIVIDE;
2330 default:
2331 PyErr_Format(PyExc_SystemError,
2332 "inplace binary op %d should not be possible", op);
2333 return 0;
2337 static int
2338 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2340 int op, scope, arg;
2341 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2343 PyObject *dict = c->u->u_names;
2344 PyObject *mangled;
2345 /* XXX AugStore isn't used anywhere! */
2347 mangled = _Py_Mangle(c->u->u_private, name);
2348 if (!mangled)
2349 return 0;
2351 op = 0;
2352 optype = OP_NAME;
2353 scope = PyST_GetScope(c->u->u_ste, mangled);
2354 switch (scope) {
2355 case FREE:
2356 dict = c->u->u_freevars;
2357 optype = OP_DEREF;
2358 break;
2359 case CELL:
2360 dict = c->u->u_cellvars;
2361 optype = OP_DEREF;
2362 break;
2363 case LOCAL:
2364 if (c->u->u_ste->ste_type == FunctionBlock)
2365 optype = OP_FAST;
2366 break;
2367 case GLOBAL_IMPLICIT:
2368 if (c->u->u_ste->ste_type == FunctionBlock &&
2369 !c->u->u_ste->ste_unoptimized)
2370 optype = OP_GLOBAL;
2371 break;
2372 case GLOBAL_EXPLICIT:
2373 optype = OP_GLOBAL;
2374 break;
2375 default:
2376 /* scope can be 0 */
2377 break;
2380 /* XXX Leave assert here, but handle __doc__ and the like better */
2381 assert(scope || PyString_AS_STRING(name)[0] == '_');
2383 switch (optype) {
2384 case OP_DEREF:
2385 switch (ctx) {
2386 case Load: op = LOAD_DEREF; break;
2387 case Store: op = STORE_DEREF; break;
2388 case AugLoad:
2389 case AugStore:
2390 break;
2391 case Del:
2392 PyErr_Format(PyExc_SyntaxError,
2393 "can not delete variable '%s' referenced "
2394 "in nested scope",
2395 PyString_AS_STRING(name));
2396 Py_DECREF(mangled);
2397 return 0;
2398 case Param:
2399 default:
2400 PyErr_SetString(PyExc_SystemError,
2401 "param invalid for deref variable");
2402 return 0;
2404 break;
2405 case OP_FAST:
2406 switch (ctx) {
2407 case Load: op = LOAD_FAST; break;
2408 case Store: op = STORE_FAST; break;
2409 case Del: op = DELETE_FAST; break;
2410 case AugLoad:
2411 case AugStore:
2412 break;
2413 case Param:
2414 default:
2415 PyErr_SetString(PyExc_SystemError,
2416 "param invalid for local variable");
2417 return 0;
2419 ADDOP_O(c, op, mangled, varnames);
2420 Py_DECREF(mangled);
2421 return 1;
2422 case OP_GLOBAL:
2423 switch (ctx) {
2424 case Load: op = LOAD_GLOBAL; break;
2425 case Store: op = STORE_GLOBAL; break;
2426 case Del: op = DELETE_GLOBAL; break;
2427 case AugLoad:
2428 case AugStore:
2429 break;
2430 case Param:
2431 default:
2432 PyErr_SetString(PyExc_SystemError,
2433 "param invalid for global variable");
2434 return 0;
2436 break;
2437 case OP_NAME:
2438 switch (ctx) {
2439 case Load: op = LOAD_NAME; break;
2440 case Store: op = STORE_NAME; break;
2441 case Del: op = DELETE_NAME; break;
2442 case AugLoad:
2443 case AugStore:
2444 break;
2445 case Param:
2446 default:
2447 PyErr_SetString(PyExc_SystemError,
2448 "param invalid for name variable");
2449 return 0;
2451 break;
2454 assert(op);
2455 arg = compiler_add_o(c, dict, mangled);
2456 Py_DECREF(mangled);
2457 if (arg < 0)
2458 return 0;
2459 return compiler_addop_i(c, op, arg);
2462 static int
2463 compiler_boolop(struct compiler *c, expr_ty e)
2465 basicblock *end;
2466 int jumpi, i, n;
2467 asdl_seq *s;
2469 assert(e->kind == BoolOp_kind);
2470 if (e->v.BoolOp.op == And)
2471 jumpi = JUMP_IF_FALSE_OR_POP;
2472 else
2473 jumpi = JUMP_IF_TRUE_OR_POP;
2474 end = compiler_new_block(c);
2475 if (end == NULL)
2476 return 0;
2477 s = e->v.BoolOp.values;
2478 n = asdl_seq_LEN(s) - 1;
2479 assert(n >= 0);
2480 for (i = 0; i < n; ++i) {
2481 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2482 ADDOP_JABS(c, jumpi, end);
2484 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2485 compiler_use_next_block(c, end);
2486 return 1;
2489 static int
2490 compiler_list(struct compiler *c, expr_ty e)
2492 int n = asdl_seq_LEN(e->v.List.elts);
2493 if (e->v.List.ctx == Store) {
2494 ADDOP_I(c, UNPACK_SEQUENCE, n);
2496 VISIT_SEQ(c, expr, e->v.List.elts);
2497 if (e->v.List.ctx == Load) {
2498 ADDOP_I(c, BUILD_LIST, n);
2500 return 1;
2503 static int
2504 compiler_tuple(struct compiler *c, expr_ty e)
2506 int n = asdl_seq_LEN(e->v.Tuple.elts);
2507 if (e->v.Tuple.ctx == Store) {
2508 ADDOP_I(c, UNPACK_SEQUENCE, n);
2510 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2511 if (e->v.Tuple.ctx == Load) {
2512 ADDOP_I(c, BUILD_TUPLE, n);
2514 return 1;
2517 static int
2518 compiler_compare(struct compiler *c, expr_ty e)
2520 int i, n;
2521 basicblock *cleanup = NULL;
2523 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2524 VISIT(c, expr, e->v.Compare.left);
2525 n = asdl_seq_LEN(e->v.Compare.ops);
2526 assert(n > 0);
2527 if (n > 1) {
2528 cleanup = compiler_new_block(c);
2529 if (cleanup == NULL)
2530 return 0;
2531 VISIT(c, expr,
2532 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2534 for (i = 1; i < n; i++) {
2535 ADDOP(c, DUP_TOP);
2536 ADDOP(c, ROT_THREE);
2537 ADDOP_I(c, COMPARE_OP,
2538 cmpop((cmpop_ty)(asdl_seq_GET(
2539 e->v.Compare.ops, i - 1))));
2540 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2541 NEXT_BLOCK(c);
2542 if (i < (n - 1))
2543 VISIT(c, expr,
2544 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2546 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2547 ADDOP_I(c, COMPARE_OP,
2548 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2549 if (n > 1) {
2550 basicblock *end = compiler_new_block(c);
2551 if (end == NULL)
2552 return 0;
2553 ADDOP_JREL(c, JUMP_FORWARD, end);
2554 compiler_use_next_block(c, cleanup);
2555 ADDOP(c, ROT_TWO);
2556 ADDOP(c, POP_TOP);
2557 compiler_use_next_block(c, end);
2559 return 1;
2562 static int
2563 compiler_call(struct compiler *c, expr_ty e)
2565 int n, code = 0;
2567 VISIT(c, expr, e->v.Call.func);
2568 n = asdl_seq_LEN(e->v.Call.args);
2569 VISIT_SEQ(c, expr, e->v.Call.args);
2570 if (e->v.Call.keywords) {
2571 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2572 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2574 if (e->v.Call.starargs) {
2575 VISIT(c, expr, e->v.Call.starargs);
2576 code |= 1;
2578 if (e->v.Call.kwargs) {
2579 VISIT(c, expr, e->v.Call.kwargs);
2580 code |= 2;
2582 switch (code) {
2583 case 0:
2584 ADDOP_I(c, CALL_FUNCTION, n);
2585 break;
2586 case 1:
2587 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2588 break;
2589 case 2:
2590 ADDOP_I(c, CALL_FUNCTION_KW, n);
2591 break;
2592 case 3:
2593 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2594 break;
2596 return 1;
2599 static int
2600 compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
2601 int gen_index, expr_ty elt)
2603 /* generate code for the iterator, then each of the ifs,
2604 and then write to the element */
2606 comprehension_ty l;
2607 basicblock *start, *anchor, *skip, *if_cleanup;
2608 int i, n;
2610 start = compiler_new_block(c);
2611 skip = compiler_new_block(c);
2612 if_cleanup = compiler_new_block(c);
2613 anchor = compiler_new_block(c);
2615 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2616 anchor == NULL)
2617 return 0;
2619 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2620 VISIT(c, expr, l->iter);
2621 ADDOP(c, GET_ITER);
2622 compiler_use_next_block(c, start);
2623 ADDOP_JREL(c, FOR_ITER, anchor);
2624 NEXT_BLOCK(c);
2625 VISIT(c, expr, l->target);
2627 /* XXX this needs to be cleaned up...a lot! */
2628 n = asdl_seq_LEN(l->ifs);
2629 for (i = 0; i < n; i++) {
2630 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
2631 VISIT(c, expr, e);
2632 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2633 NEXT_BLOCK(c);
2636 if (++gen_index < asdl_seq_LEN(generators))
2637 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
2638 return 0;
2640 /* only append after the last for generator */
2641 if (gen_index >= asdl_seq_LEN(generators)) {
2642 VISIT(c, expr, elt);
2643 ADDOP_I(c, LIST_APPEND, gen_index+1);
2645 compiler_use_next_block(c, skip);
2647 compiler_use_next_block(c, if_cleanup);
2648 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2649 compiler_use_next_block(c, anchor);
2651 return 1;
2654 static int
2655 compiler_listcomp(struct compiler *c, expr_ty e)
2657 assert(e->kind == ListComp_kind);
2658 ADDOP_I(c, BUILD_LIST, 0);
2659 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2660 e->v.ListComp.elt);
2663 static int
2664 compiler_genexp_generator(struct compiler *c,
2665 asdl_seq *generators, int gen_index,
2666 expr_ty elt)
2668 /* generate code for the iterator, then each of the ifs,
2669 and then write to the element */
2671 comprehension_ty ge;
2672 basicblock *start, *anchor, *skip, *if_cleanup, *end;
2673 int i, n;
2675 start = compiler_new_block(c);
2676 skip = compiler_new_block(c);
2677 if_cleanup = compiler_new_block(c);
2678 anchor = compiler_new_block(c);
2679 end = compiler_new_block(c);
2681 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2682 anchor == NULL || end == NULL)
2683 return 0;
2685 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2686 ADDOP_JREL(c, SETUP_LOOP, end);
2687 if (!compiler_push_fblock(c, LOOP, start))
2688 return 0;
2690 if (gen_index == 0) {
2691 /* Receive outermost iter as an implicit argument */
2692 c->u->u_argcount = 1;
2693 ADDOP_I(c, LOAD_FAST, 0);
2695 else {
2696 /* Sub-iter - calculate on the fly */
2697 VISIT(c, expr, ge->iter);
2698 ADDOP(c, GET_ITER);
2700 compiler_use_next_block(c, start);
2701 ADDOP_JREL(c, FOR_ITER, anchor);
2702 NEXT_BLOCK(c);
2703 VISIT(c, expr, ge->target);
2705 /* XXX this needs to be cleaned up...a lot! */
2706 n = asdl_seq_LEN(ge->ifs);
2707 for (i = 0; i < n; i++) {
2708 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
2709 VISIT(c, expr, e);
2710 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2711 NEXT_BLOCK(c);
2714 if (++gen_index < asdl_seq_LEN(generators))
2715 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2716 return 0;
2718 /* only append after the last 'for' generator */
2719 if (gen_index >= asdl_seq_LEN(generators)) {
2720 VISIT(c, expr, elt);
2721 ADDOP(c, YIELD_VALUE);
2722 ADDOP(c, POP_TOP);
2724 compiler_use_next_block(c, skip);
2726 compiler_use_next_block(c, if_cleanup);
2727 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2728 compiler_use_next_block(c, anchor);
2729 ADDOP(c, POP_BLOCK);
2730 compiler_pop_fblock(c, LOOP, start);
2731 compiler_use_next_block(c, end);
2733 return 1;
2736 static int
2737 compiler_genexp(struct compiler *c, expr_ty e)
2739 static identifier name;
2740 PyCodeObject *co;
2741 expr_ty outermost_iter = ((comprehension_ty)
2742 (asdl_seq_GET(e->v.GeneratorExp.generators,
2743 0)))->iter;
2745 if (!name) {
2746 name = PyString_FromString("<genexpr>");
2747 if (!name)
2748 return 0;
2751 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2752 return 0;
2753 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2754 e->v.GeneratorExp.elt);
2755 co = assemble(c, 1);
2756 compiler_exit_scope(c);
2757 if (co == NULL)
2758 return 0;
2760 compiler_make_closure(c, co, 0);
2761 Py_DECREF(co);
2763 VISIT(c, expr, outermost_iter);
2764 ADDOP(c, GET_ITER);
2765 ADDOP_I(c, CALL_FUNCTION, 1);
2767 return 1;
2770 static int
2771 compiler_visit_keyword(struct compiler *c, keyword_ty k)
2773 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2774 VISIT(c, expr, k->value);
2775 return 1;
2778 /* Test whether expression is constant. For constants, report
2779 whether they are true or false.
2781 Return values: 1 for true, 0 for false, -1 for non-constant.
2784 static int
2785 expr_constant(expr_ty e)
2787 switch (e->kind) {
2788 case Num_kind:
2789 return PyObject_IsTrue(e->v.Num.n);
2790 case Str_kind:
2791 return PyObject_IsTrue(e->v.Str.s);
2792 case Name_kind:
2793 /* __debug__ is not assignable, so we can optimize
2794 * it away in if and while statements */
2795 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2796 "__debug__") == 0)
2797 return ! Py_OptimizeFlag;
2798 /* fall through */
2799 default:
2800 return -1;
2805 Implements the with statement from PEP 343.
2807 The semantics outlined in that PEP are as follows:
2809 with EXPR as VAR:
2810 BLOCK
2812 It is implemented roughly as:
2814 context = EXPR
2815 exit = context.__exit__ # not calling it
2816 value = context.__enter__()
2817 try:
2818 VAR = value # if VAR present in the syntax
2819 BLOCK
2820 finally:
2821 if an exception was raised:
2822 exc = copy of (exception, instance, traceback)
2823 else:
2824 exc = (None, None, None)
2825 exit(*exc)
2827 static int
2828 compiler_with(struct compiler *c, stmt_ty s)
2830 static identifier enter_attr, exit_attr;
2831 basicblock *block, *finally;
2832 identifier tmpvalue = NULL;
2834 assert(s->kind == With_kind);
2836 if (!enter_attr) {
2837 enter_attr = PyString_InternFromString("__enter__");
2838 if (!enter_attr)
2839 return 0;
2841 if (!exit_attr) {
2842 exit_attr = PyString_InternFromString("__exit__");
2843 if (!exit_attr)
2844 return 0;
2847 block = compiler_new_block(c);
2848 finally = compiler_new_block(c);
2849 if (!block || !finally)
2850 return 0;
2852 if (s->v.With.optional_vars) {
2853 /* Create a temporary variable to hold context.__enter__().
2854 We need to do this rather than preserving it on the stack
2855 because SETUP_FINALLY remembers the stack level.
2856 We need to do the assignment *inside* the try/finally
2857 so that context.__exit__() is called when the assignment
2858 fails. But we need to call context.__enter__() *before*
2859 the try/finally so that if it fails we won't call
2860 context.__exit__().
2862 tmpvalue = compiler_new_tmpname(c);
2863 if (tmpvalue == NULL)
2864 return 0;
2865 PyArena_AddPyObject(c->c_arena, tmpvalue);
2868 /* Evaluate EXPR */
2869 VISIT(c, expr, s->v.With.context_expr);
2871 /* Squirrel away context.__exit__ by stuffing it under context */
2872 ADDOP(c, DUP_TOP);
2873 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
2874 ADDOP(c, ROT_TWO);
2876 /* Call context.__enter__() */
2877 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2878 ADDOP_I(c, CALL_FUNCTION, 0);
2880 if (s->v.With.optional_vars) {
2881 /* Store it in tmpvalue */
2882 if (!compiler_nameop(c, tmpvalue, Store))
2883 return 0;
2885 else {
2886 /* Discard result from context.__enter__() */
2887 ADDOP(c, POP_TOP);
2890 /* Start the try block */
2891 ADDOP_JREL(c, SETUP_FINALLY, finally);
2893 compiler_use_next_block(c, block);
2894 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
2895 return 0;
2898 if (s->v.With.optional_vars) {
2899 /* Bind saved result of context.__enter__() to VAR */
2900 if (!compiler_nameop(c, tmpvalue, Load) ||
2901 !compiler_nameop(c, tmpvalue, Del))
2902 return 0;
2903 VISIT(c, expr, s->v.With.optional_vars);
2906 /* BLOCK code */
2907 VISIT_SEQ(c, stmt, s->v.With.body);
2909 /* End of try block; start the finally block */
2910 ADDOP(c, POP_BLOCK);
2911 compiler_pop_fblock(c, FINALLY_TRY, block);
2913 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2914 compiler_use_next_block(c, finally);
2915 if (!compiler_push_fblock(c, FINALLY_END, finally))
2916 return 0;
2918 /* Finally block starts; context.__exit__ is on the stack under
2919 the exception or return information. Just issue our magic
2920 opcode. */
2921 ADDOP(c, WITH_CLEANUP);
2923 /* Finally block ends. */
2924 ADDOP(c, END_FINALLY);
2925 compiler_pop_fblock(c, FINALLY_END, finally);
2926 return 1;
2929 static int
2930 compiler_visit_expr(struct compiler *c, expr_ty e)
2932 int i, n;
2934 /* If expr e has a different line number than the last expr/stmt,
2935 set a new line number for the next instruction.
2937 if (e->lineno > c->u->u_lineno) {
2938 c->u->u_lineno = e->lineno;
2939 c->u->u_lineno_set = false;
2941 switch (e->kind) {
2942 case BoolOp_kind:
2943 return compiler_boolop(c, e);
2944 case BinOp_kind:
2945 VISIT(c, expr, e->v.BinOp.left);
2946 VISIT(c, expr, e->v.BinOp.right);
2947 ADDOP(c, binop(c, e->v.BinOp.op));
2948 break;
2949 case UnaryOp_kind:
2950 VISIT(c, expr, e->v.UnaryOp.operand);
2951 ADDOP(c, unaryop(e->v.UnaryOp.op));
2952 break;
2953 case Lambda_kind:
2954 return compiler_lambda(c, e);
2955 case IfExp_kind:
2956 return compiler_ifexp(c, e);
2957 case Dict_kind:
2958 n = asdl_seq_LEN(e->v.Dict.values);
2959 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
2960 for (i = 0; i < n; i++) {
2961 VISIT(c, expr,
2962 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
2963 VISIT(c, expr,
2964 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
2965 ADDOP(c, STORE_MAP);
2967 break;
2968 case ListComp_kind:
2969 return compiler_listcomp(c, e);
2970 case GeneratorExp_kind:
2971 return compiler_genexp(c, e);
2972 case Yield_kind:
2973 if (c->u->u_ste->ste_type != FunctionBlock)
2974 return compiler_error(c, "'yield' outside function");
2975 if (e->v.Yield.value) {
2976 VISIT(c, expr, e->v.Yield.value);
2978 else {
2979 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2981 ADDOP(c, YIELD_VALUE);
2982 break;
2983 case Compare_kind:
2984 return compiler_compare(c, e);
2985 case Call_kind:
2986 return compiler_call(c, e);
2987 case Repr_kind:
2988 VISIT(c, expr, e->v.Repr.value);
2989 ADDOP(c, UNARY_CONVERT);
2990 break;
2991 case Num_kind:
2992 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
2993 break;
2994 case Str_kind:
2995 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
2996 break;
2997 /* The following exprs can be assignment targets. */
2998 case Attribute_kind:
2999 if (e->v.Attribute.ctx != AugStore)
3000 VISIT(c, expr, e->v.Attribute.value);
3001 switch (e->v.Attribute.ctx) {
3002 case AugLoad:
3003 ADDOP(c, DUP_TOP);
3004 /* Fall through to load */
3005 case Load:
3006 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3007 break;
3008 case AugStore:
3009 ADDOP(c, ROT_TWO);
3010 /* Fall through to save */
3011 case Store:
3012 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3013 break;
3014 case Del:
3015 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3016 break;
3017 case Param:
3018 default:
3019 PyErr_SetString(PyExc_SystemError,
3020 "param invalid in attribute expression");
3021 return 0;
3023 break;
3024 case Subscript_kind:
3025 switch (e->v.Subscript.ctx) {
3026 case AugLoad:
3027 VISIT(c, expr, e->v.Subscript.value);
3028 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3029 break;
3030 case Load:
3031 VISIT(c, expr, e->v.Subscript.value);
3032 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3033 break;
3034 case AugStore:
3035 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3036 break;
3037 case Store:
3038 VISIT(c, expr, e->v.Subscript.value);
3039 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3040 break;
3041 case Del:
3042 VISIT(c, expr, e->v.Subscript.value);
3043 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3044 break;
3045 case Param:
3046 default:
3047 PyErr_SetString(PyExc_SystemError,
3048 "param invalid in subscript expression");
3049 return 0;
3051 break;
3052 case Name_kind:
3053 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3054 /* child nodes of List and Tuple will have expr_context set */
3055 case List_kind:
3056 return compiler_list(c, e);
3057 case Tuple_kind:
3058 return compiler_tuple(c, e);
3060 return 1;
3063 static int
3064 compiler_augassign(struct compiler *c, stmt_ty s)
3066 expr_ty e = s->v.AugAssign.target;
3067 expr_ty auge;
3069 assert(s->kind == AugAssign_kind);
3071 switch (e->kind) {
3072 case Attribute_kind:
3073 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3074 AugLoad, e->lineno, e->col_offset, c->c_arena);
3075 if (auge == NULL)
3076 return 0;
3077 VISIT(c, expr, auge);
3078 VISIT(c, expr, s->v.AugAssign.value);
3079 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3080 auge->v.Attribute.ctx = AugStore;
3081 VISIT(c, expr, auge);
3082 break;
3083 case Subscript_kind:
3084 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3085 AugLoad, e->lineno, e->col_offset, c->c_arena);
3086 if (auge == NULL)
3087 return 0;
3088 VISIT(c, expr, auge);
3089 VISIT(c, expr, s->v.AugAssign.value);
3090 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3091 auge->v.Subscript.ctx = AugStore;
3092 VISIT(c, expr, auge);
3093 break;
3094 case Name_kind:
3095 if (!compiler_nameop(c, e->v.Name.id, Load))
3096 return 0;
3097 VISIT(c, expr, s->v.AugAssign.value);
3098 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3099 return compiler_nameop(c, e->v.Name.id, Store);
3100 default:
3101 PyErr_Format(PyExc_SystemError,
3102 "invalid node type (%d) for augmented assignment",
3103 e->kind);
3104 return 0;
3106 return 1;
3109 static int
3110 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3112 struct fblockinfo *f;
3113 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3114 PyErr_SetString(PyExc_SystemError,
3115 "too many statically nested blocks");
3116 return 0;
3118 f = &c->u->u_fblock[c->u->u_nfblocks++];
3119 f->fb_type = t;
3120 f->fb_block = b;
3121 return 1;
3124 static void
3125 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3127 struct compiler_unit *u = c->u;
3128 assert(u->u_nfblocks > 0);
3129 u->u_nfblocks--;
3130 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3131 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3134 static int
3135 compiler_in_loop(struct compiler *c) {
3136 int i;
3137 struct compiler_unit *u = c->u;
3138 for (i = 0; i < u->u_nfblocks; ++i) {
3139 if (u->u_fblock[i].fb_type == LOOP)
3140 return 1;
3142 return 0;
3144 /* Raises a SyntaxError and returns 0.
3145 If something goes wrong, a different exception may be raised.
3148 static int
3149 compiler_error(struct compiler *c, const char *errstr)
3151 PyObject *loc;
3152 PyObject *u = NULL, *v = NULL;
3154 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3155 if (!loc) {
3156 Py_INCREF(Py_None);
3157 loc = Py_None;
3159 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3160 Py_None, loc);
3161 if (!u)
3162 goto exit;
3163 v = Py_BuildValue("(zO)", errstr, u);
3164 if (!v)
3165 goto exit;
3166 PyErr_SetObject(PyExc_SyntaxError, v);
3167 exit:
3168 Py_DECREF(loc);
3169 Py_XDECREF(u);
3170 Py_XDECREF(v);
3171 return 0;
3174 static int
3175 compiler_handle_subscr(struct compiler *c, const char *kind,
3176 expr_context_ty ctx)
3178 int op = 0;
3180 /* XXX this code is duplicated */
3181 switch (ctx) {
3182 case AugLoad: /* fall through to Load */
3183 case Load: op = BINARY_SUBSCR; break;
3184 case AugStore:/* fall through to Store */
3185 case Store: op = STORE_SUBSCR; break;
3186 case Del: op = DELETE_SUBSCR; break;
3187 case Param:
3188 PyErr_Format(PyExc_SystemError,
3189 "invalid %s kind %d in subscript\n",
3190 kind, ctx);
3191 return 0;
3193 if (ctx == AugLoad) {
3194 ADDOP_I(c, DUP_TOPX, 2);
3196 else if (ctx == AugStore) {
3197 ADDOP(c, ROT_THREE);
3199 ADDOP(c, op);
3200 return 1;
3203 static int
3204 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3206 int n = 2;
3207 assert(s->kind == Slice_kind);
3209 /* only handles the cases where BUILD_SLICE is emitted */
3210 if (s->v.Slice.lower) {
3211 VISIT(c, expr, s->v.Slice.lower);
3213 else {
3214 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3217 if (s->v.Slice.upper) {
3218 VISIT(c, expr, s->v.Slice.upper);
3220 else {
3221 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3224 if (s->v.Slice.step) {
3225 n++;
3226 VISIT(c, expr, s->v.Slice.step);
3228 ADDOP_I(c, BUILD_SLICE, n);
3229 return 1;
3232 static int
3233 compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3235 int op = 0, slice_offset = 0, stack_count = 0;
3237 assert(s->v.Slice.step == NULL);
3238 if (s->v.Slice.lower) {
3239 slice_offset++;
3240 stack_count++;
3241 if (ctx != AugStore)
3242 VISIT(c, expr, s->v.Slice.lower);
3244 if (s->v.Slice.upper) {
3245 slice_offset += 2;
3246 stack_count++;
3247 if (ctx != AugStore)
3248 VISIT(c, expr, s->v.Slice.upper);
3251 if (ctx == AugLoad) {
3252 switch (stack_count) {
3253 case 0: ADDOP(c, DUP_TOP); break;
3254 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3255 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3258 else if (ctx == AugStore) {
3259 switch (stack_count) {
3260 case 0: ADDOP(c, ROT_TWO); break;
3261 case 1: ADDOP(c, ROT_THREE); break;
3262 case 2: ADDOP(c, ROT_FOUR); break;
3266 switch (ctx) {
3267 case AugLoad: /* fall through to Load */
3268 case Load: op = SLICE; break;
3269 case AugStore:/* fall through to Store */
3270 case Store: op = STORE_SLICE; break;
3271 case Del: op = DELETE_SLICE; break;
3272 case Param:
3273 default:
3274 PyErr_SetString(PyExc_SystemError,
3275 "param invalid in simple slice");
3276 return 0;
3279 ADDOP(c, op + slice_offset);
3280 return 1;
3283 static int
3284 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3285 expr_context_ty ctx)
3287 switch (s->kind) {
3288 case Ellipsis_kind:
3289 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3290 break;
3291 case Slice_kind:
3292 return compiler_slice(c, s, ctx);
3293 case Index_kind:
3294 VISIT(c, expr, s->v.Index.value);
3295 break;
3296 case ExtSlice_kind:
3297 default:
3298 PyErr_SetString(PyExc_SystemError,
3299 "extended slice invalid in nested slice");
3300 return 0;
3302 return 1;
3305 static int
3306 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3308 char * kindname = NULL;
3309 switch (s->kind) {
3310 case Index_kind:
3311 kindname = "index";
3312 if (ctx != AugStore) {
3313 VISIT(c, expr, s->v.Index.value);
3315 break;
3316 case Ellipsis_kind:
3317 kindname = "ellipsis";
3318 if (ctx != AugStore) {
3319 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3321 break;
3322 case Slice_kind:
3323 kindname = "slice";
3324 if (!s->v.Slice.step)
3325 return compiler_simple_slice(c, s, ctx);
3326 if (ctx != AugStore) {
3327 if (!compiler_slice(c, s, ctx))
3328 return 0;
3330 break;
3331 case ExtSlice_kind:
3332 kindname = "extended slice";
3333 if (ctx != AugStore) {
3334 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3335 for (i = 0; i < n; i++) {
3336 slice_ty sub = (slice_ty)asdl_seq_GET(
3337 s->v.ExtSlice.dims, i);
3338 if (!compiler_visit_nested_slice(c, sub, ctx))
3339 return 0;
3341 ADDOP_I(c, BUILD_TUPLE, n);
3343 break;
3344 default:
3345 PyErr_Format(PyExc_SystemError,
3346 "invalid subscript kind %d", s->kind);
3347 return 0;
3349 return compiler_handle_subscr(c, kindname, ctx);
3353 /* End of the compiler section, beginning of the assembler section */
3355 /* do depth-first search of basic block graph, starting with block.
3356 post records the block indices in post-order.
3358 XXX must handle implicit jumps from one block to next
3361 struct assembler {
3362 PyObject *a_bytecode; /* string containing bytecode */
3363 int a_offset; /* offset into bytecode */
3364 int a_nblocks; /* number of reachable blocks */
3365 basicblock **a_postorder; /* list of blocks in dfs postorder */
3366 PyObject *a_lnotab; /* string containing lnotab */
3367 int a_lnotab_off; /* offset into lnotab */
3368 int a_lineno; /* last lineno of emitted instruction */
3369 int a_lineno_off; /* bytecode offset of last lineno */
3372 static void
3373 dfs(struct compiler *c, basicblock *b, struct assembler *a)
3375 int i;
3376 struct instr *instr = NULL;
3378 if (b->b_seen)
3379 return;
3380 b->b_seen = 1;
3381 if (b->b_next != NULL)
3382 dfs(c, b->b_next, a);
3383 for (i = 0; i < b->b_iused; i++) {
3384 instr = &b->b_instr[i];
3385 if (instr->i_jrel || instr->i_jabs)
3386 dfs(c, instr->i_target, a);
3388 a->a_postorder[a->a_nblocks++] = b;
3391 static int
3392 stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3394 int i;
3395 struct instr *instr;
3396 if (b->b_seen || b->b_startdepth >= depth)
3397 return maxdepth;
3398 b->b_seen = 1;
3399 b->b_startdepth = depth;
3400 for (i = 0; i < b->b_iused; i++) {
3401 instr = &b->b_instr[i];
3402 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3403 if (depth > maxdepth)
3404 maxdepth = depth;
3405 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3406 if (instr->i_jrel || instr->i_jabs) {
3407 maxdepth = stackdepth_walk(c, instr->i_target,
3408 depth, maxdepth);
3409 if (instr->i_opcode == JUMP_ABSOLUTE ||
3410 instr->i_opcode == JUMP_FORWARD) {
3411 goto out; /* remaining code is dead */
3415 if (b->b_next)
3416 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3417 out:
3418 b->b_seen = 0;
3419 return maxdepth;
3422 /* Find the flow path that needs the largest stack. We assume that
3423 * cycles in the flow graph have no net effect on the stack depth.
3425 static int
3426 stackdepth(struct compiler *c)
3428 basicblock *b, *entryblock;
3429 entryblock = NULL;
3430 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3431 b->b_seen = 0;
3432 b->b_startdepth = INT_MIN;
3433 entryblock = b;
3435 if (!entryblock)
3436 return 0;
3437 return stackdepth_walk(c, entryblock, 0, 0);
3440 static int
3441 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3443 memset(a, 0, sizeof(struct assembler));
3444 a->a_lineno = firstlineno;
3445 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3446 if (!a->a_bytecode)
3447 return 0;
3448 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3449 if (!a->a_lnotab)
3450 return 0;
3451 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3452 PyErr_NoMemory();
3453 return 0;
3455 a->a_postorder = (basicblock **)PyObject_Malloc(
3456 sizeof(basicblock *) * nblocks);
3457 if (!a->a_postorder) {
3458 PyErr_NoMemory();
3459 return 0;
3461 return 1;
3464 static void
3465 assemble_free(struct assembler *a)
3467 Py_XDECREF(a->a_bytecode);
3468 Py_XDECREF(a->a_lnotab);
3469 if (a->a_postorder)
3470 PyObject_Free(a->a_postorder);
3473 /* Return the size of a basic block in bytes. */
3475 static int
3476 instrsize(struct instr *instr)
3478 if (!instr->i_hasarg)
3479 return 1; /* 1 byte for the opcode*/
3480 if (instr->i_oparg > 0xffff)
3481 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3482 return 3; /* 1 (opcode) + 2 (oparg) */
3485 static int
3486 blocksize(basicblock *b)
3488 int i;
3489 int size = 0;
3491 for (i = 0; i < b->b_iused; i++)
3492 size += instrsize(&b->b_instr[i]);
3493 return size;
3496 /* All about a_lnotab.
3498 c_lnotab is an array of unsigned bytes disguised as a Python string.
3499 It is used to map bytecode offsets to source code line #s (when needed
3500 for tracebacks).
3502 The array is conceptually a list of
3503 (bytecode offset increment, line number increment)
3504 pairs. The details are important and delicate, best illustrated by example:
3506 byte code offset source code line number
3509 50 7
3510 350 307
3511 361 308
3513 The first trick is that these numbers aren't stored, only the increments
3514 from one row to the next (this doesn't really work, but it's a start):
3516 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3518 The second trick is that an unsigned byte can't hold negative values, or
3519 values larger than 255, so (a) there's a deep assumption that byte code
3520 offsets and their corresponding line #s both increase monotonically, and (b)
3521 if at least one column jumps by more than 255 from one row to the next, more
3522 than one pair is written to the table. In case #b, there's no way to know
3523 from looking at the table later how many were written. That's the delicate
3524 part. A user of c_lnotab desiring to find the source line number
3525 corresponding to a bytecode address A should do something like this
3527 lineno = addr = 0
3528 for addr_incr, line_incr in c_lnotab:
3529 addr += addr_incr
3530 if addr > A:
3531 return lineno
3532 lineno += line_incr
3534 In order for this to work, when the addr field increments by more than 255,
3535 the line # increment in each pair generated must be 0 until the remaining addr
3536 increment is < 256. So, in the example above, assemble_lnotab (it used
3537 to be called com_set_lineno) should not (as was actually done until 2.2)
3538 expand 300, 300 to 255, 255, 45, 45,
3539 but to 255, 0, 45, 255, 0, 45.
3542 static int
3543 assemble_lnotab(struct assembler *a, struct instr *i)
3545 int d_bytecode, d_lineno;
3546 int len;
3547 unsigned char *lnotab;
3549 d_bytecode = a->a_offset - a->a_lineno_off;
3550 d_lineno = i->i_lineno - a->a_lineno;
3552 assert(d_bytecode >= 0);
3553 assert(d_lineno >= 0);
3555 if(d_bytecode == 0 && d_lineno == 0)
3556 return 1;
3558 if (d_bytecode > 255) {
3559 int j, nbytes, ncodes = d_bytecode / 255;
3560 nbytes = a->a_lnotab_off + 2 * ncodes;
3561 len = PyString_GET_SIZE(a->a_lnotab);
3562 if (nbytes >= len) {
3563 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3564 len = nbytes;
3565 else if (len <= INT_MAX / 2)
3566 len *= 2;
3567 else {
3568 PyErr_NoMemory();
3569 return 0;
3571 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3572 return 0;
3574 lnotab = (unsigned char *)
3575 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3576 for (j = 0; j < ncodes; j++) {
3577 *lnotab++ = 255;
3578 *lnotab++ = 0;
3580 d_bytecode -= ncodes * 255;
3581 a->a_lnotab_off += ncodes * 2;
3583 assert(d_bytecode <= 255);
3584 if (d_lineno > 255) {
3585 int j, nbytes, ncodes = d_lineno / 255;
3586 nbytes = a->a_lnotab_off + 2 * ncodes;
3587 len = PyString_GET_SIZE(a->a_lnotab);
3588 if (nbytes >= len) {
3589 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3590 len = nbytes;
3591 else if (len <= INT_MAX / 2)
3592 len *= 2;
3593 else {
3594 PyErr_NoMemory();
3595 return 0;
3597 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3598 return 0;
3600 lnotab = (unsigned char *)
3601 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3602 *lnotab++ = d_bytecode;
3603 *lnotab++ = 255;
3604 d_bytecode = 0;
3605 for (j = 1; j < ncodes; j++) {
3606 *lnotab++ = 0;
3607 *lnotab++ = 255;
3609 d_lineno -= ncodes * 255;
3610 a->a_lnotab_off += ncodes * 2;
3613 len = PyString_GET_SIZE(a->a_lnotab);
3614 if (a->a_lnotab_off + 2 >= len) {
3615 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
3616 return 0;
3618 lnotab = (unsigned char *)
3619 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3621 a->a_lnotab_off += 2;
3622 if (d_bytecode) {
3623 *lnotab++ = d_bytecode;
3624 *lnotab++ = d_lineno;
3626 else { /* First line of a block; def stmt, etc. */
3627 *lnotab++ = 0;
3628 *lnotab++ = d_lineno;
3630 a->a_lineno = i->i_lineno;
3631 a->a_lineno_off = a->a_offset;
3632 return 1;
3635 /* assemble_emit()
3636 Extend the bytecode with a new instruction.
3637 Update lnotab if necessary.
3640 static int
3641 assemble_emit(struct assembler *a, struct instr *i)
3643 int size, arg = 0, ext = 0;
3644 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
3645 char *code;
3647 size = instrsize(i);
3648 if (i->i_hasarg) {
3649 arg = i->i_oparg;
3650 ext = arg >> 16;
3652 if (i->i_lineno && !assemble_lnotab(a, i))
3653 return 0;
3654 if (a->a_offset + size >= len) {
3655 if (len > PY_SSIZE_T_MAX / 2)
3656 return 0;
3657 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
3658 return 0;
3660 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3661 a->a_offset += size;
3662 if (size == 6) {
3663 assert(i->i_hasarg);
3664 *code++ = (char)EXTENDED_ARG;
3665 *code++ = ext & 0xff;
3666 *code++ = ext >> 8;
3667 arg &= 0xffff;
3669 *code++ = i->i_opcode;
3670 if (i->i_hasarg) {
3671 assert(size == 3 || size == 6);
3672 *code++ = arg & 0xff;
3673 *code++ = arg >> 8;
3675 return 1;
3678 static void
3679 assemble_jump_offsets(struct assembler *a, struct compiler *c)
3681 basicblock *b;
3682 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
3683 int i;
3685 /* Compute the size of each block and fixup jump args.
3686 Replace block pointer with position in bytecode. */
3687 start:
3688 totsize = 0;
3689 for (i = a->a_nblocks - 1; i >= 0; i--) {
3690 b = a->a_postorder[i];
3691 bsize = blocksize(b);
3692 b->b_offset = totsize;
3693 totsize += bsize;
3695 extended_arg_count = 0;
3696 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3697 bsize = b->b_offset;
3698 for (i = 0; i < b->b_iused; i++) {
3699 struct instr *instr = &b->b_instr[i];
3700 /* Relative jumps are computed relative to
3701 the instruction pointer after fetching
3702 the jump instruction.
3704 bsize += instrsize(instr);
3705 if (instr->i_jabs)
3706 instr->i_oparg = instr->i_target->b_offset;
3707 else if (instr->i_jrel) {
3708 int delta = instr->i_target->b_offset - bsize;
3709 instr->i_oparg = delta;
3711 else
3712 continue;
3713 if (instr->i_oparg > 0xffff)
3714 extended_arg_count++;
3718 /* XXX: This is an awful hack that could hurt performance, but
3719 on the bright side it should work until we come up
3720 with a better solution.
3722 In the meantime, should the goto be dropped in favor
3723 of a loop?
3725 The issue is that in the first loop blocksize() is called
3726 which calls instrsize() which requires i_oparg be set
3727 appropriately. There is a bootstrap problem because
3728 i_oparg is calculated in the second loop above.
3730 So we loop until we stop seeing new EXTENDED_ARGs.
3731 The only EXTENDED_ARGs that could be popping up are
3732 ones in jump instructions. So this should converge
3733 fairly quickly.
3735 if (last_extended_arg_count != extended_arg_count) {
3736 last_extended_arg_count = extended_arg_count;
3737 goto start;
3741 static PyObject *
3742 dict_keys_inorder(PyObject *dict, int offset)
3744 PyObject *tuple, *k, *v;
3745 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
3747 tuple = PyTuple_New(size);
3748 if (tuple == NULL)
3749 return NULL;
3750 while (PyDict_Next(dict, &pos, &k, &v)) {
3751 i = PyInt_AS_LONG(v);
3752 /* The keys of the dictionary are tuples. (see compiler_add_o)
3753 The object we want is always first, though. */
3754 k = PyTuple_GET_ITEM(k, 0);
3755 Py_INCREF(k);
3756 assert((i - offset) < size);
3757 assert((i - offset) >= 0);
3758 PyTuple_SET_ITEM(tuple, i - offset, k);
3760 return tuple;
3763 static int
3764 compute_code_flags(struct compiler *c)
3766 PySTEntryObject *ste = c->u->u_ste;
3767 int flags = 0, n;
3768 if (ste->ste_type != ModuleBlock)
3769 flags |= CO_NEWLOCALS;
3770 if (ste->ste_type == FunctionBlock) {
3771 if (!ste->ste_unoptimized)
3772 flags |= CO_OPTIMIZED;
3773 if (ste->ste_nested)
3774 flags |= CO_NESTED;
3775 if (ste->ste_generator)
3776 flags |= CO_GENERATOR;
3777 if (ste->ste_varargs)
3778 flags |= CO_VARARGS;
3779 if (ste->ste_varkeywords)
3780 flags |= CO_VARKEYWORDS;
3783 /* (Only) inherit compilerflags in PyCF_MASK */
3784 flags |= (c->c_flags->cf_flags & PyCF_MASK);
3786 n = PyDict_Size(c->u->u_freevars);
3787 if (n < 0)
3788 return -1;
3789 if (n == 0) {
3790 n = PyDict_Size(c->u->u_cellvars);
3791 if (n < 0)
3792 return -1;
3793 if (n == 0) {
3794 flags |= CO_NOFREE;
3798 return flags;
3801 static PyCodeObject *
3802 makecode(struct compiler *c, struct assembler *a)
3804 PyObject *tmp;
3805 PyCodeObject *co = NULL;
3806 PyObject *consts = NULL;
3807 PyObject *names = NULL;
3808 PyObject *varnames = NULL;
3809 PyObject *filename = NULL;
3810 PyObject *name = NULL;
3811 PyObject *freevars = NULL;
3812 PyObject *cellvars = NULL;
3813 PyObject *bytecode = NULL;
3814 int nlocals, flags;
3816 tmp = dict_keys_inorder(c->u->u_consts, 0);
3817 if (!tmp)
3818 goto error;
3819 consts = PySequence_List(tmp); /* optimize_code requires a list */
3820 Py_DECREF(tmp);
3822 names = dict_keys_inorder(c->u->u_names, 0);
3823 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3824 if (!consts || !names || !varnames)
3825 goto error;
3827 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3828 if (!cellvars)
3829 goto error;
3830 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3831 if (!freevars)
3832 goto error;
3833 filename = PyString_FromString(c->c_filename);
3834 if (!filename)
3835 goto error;
3837 nlocals = PyDict_Size(c->u->u_varnames);
3838 flags = compute_code_flags(c);
3839 if (flags < 0)
3840 goto error;
3842 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3843 if (!bytecode)
3844 goto error;
3846 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3847 if (!tmp)
3848 goto error;
3849 Py_DECREF(consts);
3850 consts = tmp;
3852 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3853 bytecode, consts, names, varnames,
3854 freevars, cellvars,
3855 filename, c->u->u_name,
3856 c->u->u_firstlineno,
3857 a->a_lnotab);
3858 error:
3859 Py_XDECREF(consts);
3860 Py_XDECREF(names);
3861 Py_XDECREF(varnames);
3862 Py_XDECREF(filename);
3863 Py_XDECREF(name);
3864 Py_XDECREF(freevars);
3865 Py_XDECREF(cellvars);
3866 Py_XDECREF(bytecode);
3867 return co;
3871 /* For debugging purposes only */
3872 #if 0
3873 static void
3874 dump_instr(const struct instr *i)
3876 const char *jrel = i->i_jrel ? "jrel " : "";
3877 const char *jabs = i->i_jabs ? "jabs " : "";
3878 char arg[128];
3880 *arg = '\0';
3881 if (i->i_hasarg)
3882 sprintf(arg, "arg: %d ", i->i_oparg);
3884 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3885 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3888 static void
3889 dump_basicblock(const basicblock *b)
3891 const char *seen = b->b_seen ? "seen " : "";
3892 const char *b_return = b->b_return ? "return " : "";
3893 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3894 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3895 if (b->b_instr) {
3896 int i;
3897 for (i = 0; i < b->b_iused; i++) {
3898 fprintf(stderr, " [%02d] ", i);
3899 dump_instr(b->b_instr + i);
3903 #endif
3905 static PyCodeObject *
3906 assemble(struct compiler *c, int addNone)
3908 basicblock *b, *entryblock;
3909 struct assembler a;
3910 int i, j, nblocks;
3911 PyCodeObject *co = NULL;
3913 /* Make sure every block that falls off the end returns None.
3914 XXX NEXT_BLOCK() isn't quite right, because if the last
3915 block ends with a jump or return b_next shouldn't set.
3917 if (!c->u->u_curblock->b_return) {
3918 NEXT_BLOCK(c);
3919 if (addNone)
3920 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3921 ADDOP(c, RETURN_VALUE);
3924 nblocks = 0;
3925 entryblock = NULL;
3926 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3927 nblocks++;
3928 entryblock = b;
3931 /* Set firstlineno if it wasn't explicitly set. */
3932 if (!c->u->u_firstlineno) {
3933 if (entryblock && entryblock->b_instr)
3934 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3935 else
3936 c->u->u_firstlineno = 1;
3938 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3939 goto error;
3940 dfs(c, entryblock, &a);
3942 /* Can't modify the bytecode after computing jump offsets. */
3943 assemble_jump_offsets(&a, c);
3945 /* Emit code in reverse postorder from dfs. */
3946 for (i = a.a_nblocks - 1; i >= 0; i--) {
3947 b = a.a_postorder[i];
3948 for (j = 0; j < b->b_iused; j++)
3949 if (!assemble_emit(&a, &b->b_instr[j]))
3950 goto error;
3953 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
3954 goto error;
3955 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
3956 goto error;
3958 co = makecode(c, &a);
3959 error:
3960 assemble_free(&a);
3961 return co;