Merged revisions 78965 via svnmerge from
[python/dscho.git] / Python / compile.c
blobab5420867bda89c1f6da96dcab02148d7d30ada2
1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in
11 * this file.
12 * 5. Optimize the byte code (peephole optimizations). See peephole.c
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
17 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
20 * structure takes care of releasing those. Use the arena to manage
21 * objects.
24 #include "Python.h"
26 #include "Python-ast.h"
27 #include "node.h"
28 #include "pyarena.h"
29 #include "ast.h"
30 #include "code.h"
31 #include "compile.h"
32 #include "symtable.h"
33 #include "opcode.h"
35 int Py_OptimizeFlag = 0;
37 #define DEFAULT_BLOCK_SIZE 16
38 #define DEFAULT_BLOCKS 8
39 #define DEFAULT_CODE_SIZE 128
40 #define DEFAULT_LNOTAB_SIZE 16
42 #define COMP_GENEXP 0
43 #define COMP_LISTCOMP 1
44 #define COMP_SETCOMP 2
45 #define COMP_DICTCOMP 3
47 struct instr {
48 unsigned i_jabs : 1;
49 unsigned i_jrel : 1;
50 unsigned i_hasarg : 1;
51 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
57 typedef struct basicblock_ {
58 /* Each basicblock in a compilation unit is linked via b_list in the
59 reverse order that the block are allocated. b_list points to the next
60 block, not to be confused with b_next, which is next by control flow. */
61 struct basicblock_ *b_list;
62 /* number of instructions used */
63 int b_iused;
64 /* length of instruction array (b_instr) */
65 int b_ialloc;
66 /* pointer to an array of instructions, initially NULL */
67 struct instr *b_instr;
68 /* If b_next is non-NULL, it is a pointer to the next
69 block reached by normal control flow. */
70 struct basicblock_ *b_next;
71 /* b_seen is used to perform a DFS of basicblocks. */
72 unsigned b_seen : 1;
73 /* b_return is true if a RETURN_VALUE opcode is inserted. */
74 unsigned b_return : 1;
75 /* depth of stack upon entry of block, computed by stackdepth() */
76 int b_startdepth;
77 /* instruction offset for block, computed by assemble_jump_offsets() */
78 int b_offset;
79 } basicblock;
81 /* fblockinfo tracks the current frame block.
83 A frame block is used to handle loops, try/except, and try/finally.
84 It's called a frame block to distinguish it from a basic block in the
85 compiler IR.
88 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
90 struct fblockinfo {
91 enum fblocktype fb_type;
92 basicblock *fb_block;
95 /* The following items change on entry and exit of code blocks.
96 They must be saved and restored when returning to a block.
98 struct compiler_unit {
99 PySTEntryObject *u_ste;
101 PyObject *u_name;
102 /* The following fields are dicts that map objects to
103 the index of them in co_XXX. The index is used as
104 the argument for opcodes that refer to those collections.
106 PyObject *u_consts; /* all constants */
107 PyObject *u_names; /* all names */
108 PyObject *u_varnames; /* local variables */
109 PyObject *u_cellvars; /* cell variables */
110 PyObject *u_freevars; /* free variables */
112 PyObject *u_private; /* for private name mangling */
114 int u_argcount; /* number of arguments for block */
115 int u_kwonlyargcount; /* number of keyword only arguments for block */
116 /* Pointer to the most recently allocated block. By following b_list
117 members, you can reach all early allocated blocks. */
118 basicblock *u_blocks;
119 basicblock *u_curblock; /* pointer to current block */
120 int u_tmpname; /* temporary variables for list comps */
122 int u_nfblocks;
123 struct fblockinfo u_fblock[CO_MAXBLOCKS];
125 int u_firstlineno; /* the first lineno of the block */
126 int u_lineno; /* the lineno for the current stmt */
127 int u_lineno_set; /* boolean to indicate whether instr
128 has been generated with current lineno */
131 /* This struct captures the global state of a compilation.
133 The u pointer points to the current compilation unit, while units
134 for enclosing blocks are stored in c_stack. The u and c_stack are
135 managed by compiler_enter_scope() and compiler_exit_scope().
138 struct compiler {
139 const char *c_filename;
140 struct symtable *c_st;
141 PyFutureFeatures *c_future; /* pointer to module's __future__ */
142 PyCompilerFlags *c_flags;
144 int c_interactive; /* true if in interactive mode */
145 int c_nestlevel;
147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
149 char *c_encoding; /* source encoding (a borrowed reference) */
150 PyArena *c_arena; /* pointer to memory allocation arena */
153 static int compiler_enter_scope(struct compiler *, identifier, void *, int);
154 static void compiler_free(struct compiler *);
155 static basicblock *compiler_new_block(struct compiler *);
156 static int compiler_next_instr(struct compiler *, basicblock *);
157 static int compiler_addop(struct compiler *, int);
158 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
159 static int compiler_addop_i(struct compiler *, int, int);
160 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
161 static basicblock *compiler_use_new_block(struct compiler *);
162 static int compiler_error(struct compiler *, const char *);
163 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
165 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
166 static int compiler_visit_stmt(struct compiler *, stmt_ty);
167 static int compiler_visit_keyword(struct compiler *, keyword_ty);
168 static int compiler_visit_expr(struct compiler *, expr_ty);
169 static int compiler_augassign(struct compiler *, stmt_ty);
170 static int compiler_visit_slice(struct compiler *, slice_ty,
171 expr_context_ty);
173 static int compiler_push_fblock(struct compiler *, enum fblocktype,
174 basicblock *);
175 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
176 basicblock *);
177 /* Returns true if there is a loop on the fblock stack. */
178 static int compiler_in_loop(struct compiler *);
180 static int inplace_binop(struct compiler *, operator_ty);
181 static int expr_constant(expr_ty e);
183 static int compiler_with(struct compiler *, stmt_ty);
184 static int compiler_call_helper(struct compiler *c, int n,
185 asdl_seq *args,
186 asdl_seq *keywords,
187 expr_ty starargs,
188 expr_ty kwargs);
190 static PyCodeObject *assemble(struct compiler *, int addNone);
191 static PyObject *__doc__;
193 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
195 PyObject *
196 _Py_Mangle(PyObject *privateobj, PyObject *ident)
198 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */
200 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
201 Py_UNICODE *buffer;
202 size_t nlen, plen;
203 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
204 name == NULL || name[0] != '_' || name[1] != '_') {
205 Py_INCREF(ident);
206 return ident;
208 p = PyUnicode_AS_UNICODE(privateobj);
209 nlen = Py_UNICODE_strlen(name);
210 /* Don't mangle __id__ or names with dots.
212 The only time a name with a dot can occur is when
213 we are compiling an import statement that has a
214 package name.
216 TODO(jhylton): Decide whether we want to support
217 mangling of the module name, e.g. __M.X.
219 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
220 || Py_UNICODE_strchr(name, '.')) {
221 Py_INCREF(ident);
222 return ident; /* Don't mangle __whatever__ */
224 /* Strip leading underscores from class name */
225 while (*p == '_')
226 p++;
227 if (*p == 0) {
228 Py_INCREF(ident);
229 return ident; /* Don't mangle if class is just underscores */
231 plen = Py_UNICODE_strlen(p);
233 assert(1 <= PY_SSIZE_T_MAX - nlen);
234 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
236 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
237 if (!ident)
238 return 0;
239 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
240 buffer = PyUnicode_AS_UNICODE(ident);
241 buffer[0] = '_';
242 Py_UNICODE_strncpy(buffer+1, p, plen);
243 Py_UNICODE_strcpy(buffer+1+plen, name);
244 return ident;
247 static int
248 compiler_init(struct compiler *c)
250 memset(c, 0, sizeof(struct compiler));
252 c->c_stack = PyList_New(0);
253 if (!c->c_stack)
254 return 0;
256 return 1;
259 PyCodeObject *
260 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
261 PyArena *arena)
263 struct compiler c;
264 PyCodeObject *co = NULL;
265 PyCompilerFlags local_flags;
266 int merged;
268 if (!__doc__) {
269 __doc__ = PyUnicode_InternFromString("__doc__");
270 if (!__doc__)
271 return NULL;
274 if (!compiler_init(&c))
275 return NULL;
276 c.c_filename = filename;
277 c.c_arena = arena;
278 c.c_future = PyFuture_FromAST(mod, filename);
279 if (c.c_future == NULL)
280 goto finally;
281 if (!flags) {
282 local_flags.cf_flags = 0;
283 flags = &local_flags;
285 merged = c.c_future->ff_features | flags->cf_flags;
286 c.c_future->ff_features = merged;
287 flags->cf_flags = merged;
288 c.c_flags = flags;
289 c.c_nestlevel = 0;
291 c.c_st = PySymtable_Build(mod, filename, c.c_future);
292 if (c.c_st == NULL) {
293 if (!PyErr_Occurred())
294 PyErr_SetString(PyExc_SystemError, "no symtable");
295 goto finally;
298 /* XXX initialize to NULL for now, need to handle */
299 c.c_encoding = NULL;
301 co = compiler_mod(&c, mod);
303 finally:
304 compiler_free(&c);
305 assert(co || PyErr_Occurred());
306 return co;
309 PyCodeObject *
310 PyNode_Compile(struct _node *n, const char *filename)
312 PyCodeObject *co = NULL;
313 mod_ty mod;
314 PyArena *arena = PyArena_New();
315 if (!arena)
316 return NULL;
317 mod = PyAST_FromNode(n, NULL, filename, arena);
318 if (mod)
319 co = PyAST_Compile(mod, filename, NULL, arena);
320 PyArena_Free(arena);
321 return co;
324 static void
325 compiler_free(struct compiler *c)
327 if (c->c_st)
328 PySymtable_Free(c->c_st);
329 if (c->c_future)
330 PyObject_Free(c->c_future);
331 Py_DECREF(c->c_stack);
334 static PyObject *
335 list2dict(PyObject *list)
337 Py_ssize_t i, n;
338 PyObject *v, *k;
339 PyObject *dict = PyDict_New();
340 if (!dict) return NULL;
342 n = PyList_Size(list);
343 for (i = 0; i < n; i++) {
344 v = PyLong_FromLong(i);
345 if (!v) {
346 Py_DECREF(dict);
347 return NULL;
349 k = PyList_GET_ITEM(list, i);
350 k = PyTuple_Pack(2, k, k->ob_type);
351 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
352 Py_XDECREF(k);
353 Py_DECREF(v);
354 Py_DECREF(dict);
355 return NULL;
357 Py_DECREF(k);
358 Py_DECREF(v);
360 return dict;
363 /* Return new dict containing names from src that match scope(s).
365 src is a symbol table dictionary. If the scope of a name matches
366 either scope_type or flag is set, insert it into the new dict. The
367 values are integers, starting at offset and increasing by one for
368 each key.
371 static PyObject *
372 dictbytype(PyObject *src, int scope_type, int flag, int offset)
374 Py_ssize_t pos = 0, i = offset, scope;
375 PyObject *k, *v, *dest = PyDict_New();
377 assert(offset >= 0);
378 if (dest == NULL)
379 return NULL;
381 while (PyDict_Next(src, &pos, &k, &v)) {
382 /* XXX this should probably be a macro in symtable.h */
383 long vi;
384 assert(PyLong_Check(v));
385 vi = PyLong_AS_LONG(v);
386 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
388 if (scope == scope_type || vi & flag) {
389 PyObject *tuple, *item = PyLong_FromLong(i);
390 if (item == NULL) {
391 Py_DECREF(dest);
392 return NULL;
394 i++;
395 tuple = PyTuple_Pack(2, k, k->ob_type);
396 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
397 Py_DECREF(item);
398 Py_DECREF(dest);
399 Py_XDECREF(tuple);
400 return NULL;
402 Py_DECREF(item);
403 Py_DECREF(tuple);
406 return dest;
409 static void
410 compiler_unit_check(struct compiler_unit *u)
412 basicblock *block;
413 for (block = u->u_blocks; block != NULL; block = block->b_list) {
414 assert((void *)block != (void *)0xcbcbcbcb);
415 assert((void *)block != (void *)0xfbfbfbfb);
416 assert((void *)block != (void *)0xdbdbdbdb);
417 if (block->b_instr != NULL) {
418 assert(block->b_ialloc > 0);
419 assert(block->b_iused > 0);
420 assert(block->b_ialloc >= block->b_iused);
422 else {
423 assert (block->b_iused == 0);
424 assert (block->b_ialloc == 0);
429 static void
430 compiler_unit_free(struct compiler_unit *u)
432 basicblock *b, *next;
434 compiler_unit_check(u);
435 b = u->u_blocks;
436 while (b != NULL) {
437 if (b->b_instr)
438 PyObject_Free((void *)b->b_instr);
439 next = b->b_list;
440 PyObject_Free((void *)b);
441 b = next;
443 Py_CLEAR(u->u_ste);
444 Py_CLEAR(u->u_name);
445 Py_CLEAR(u->u_consts);
446 Py_CLEAR(u->u_names);
447 Py_CLEAR(u->u_varnames);
448 Py_CLEAR(u->u_freevars);
449 Py_CLEAR(u->u_cellvars);
450 Py_CLEAR(u->u_private);
451 PyObject_Free(u);
454 static int
455 compiler_enter_scope(struct compiler *c, identifier name, void *key,
456 int lineno)
458 struct compiler_unit *u;
460 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
461 struct compiler_unit));
462 if (!u) {
463 PyErr_NoMemory();
464 return 0;
466 memset(u, 0, sizeof(struct compiler_unit));
467 u->u_argcount = 0;
468 u->u_kwonlyargcount = 0;
469 u->u_ste = PySymtable_Lookup(c->c_st, key);
470 if (!u->u_ste) {
471 compiler_unit_free(u);
472 return 0;
474 Py_INCREF(name);
475 u->u_name = name;
476 u->u_varnames = list2dict(u->u_ste->ste_varnames);
477 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
478 if (!u->u_varnames || !u->u_cellvars) {
479 compiler_unit_free(u);
480 return 0;
483 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
484 PyDict_Size(u->u_cellvars));
485 if (!u->u_freevars) {
486 compiler_unit_free(u);
487 return 0;
490 u->u_blocks = NULL;
491 u->u_tmpname = 0;
492 u->u_nfblocks = 0;
493 u->u_firstlineno = lineno;
494 u->u_lineno = 0;
495 u->u_lineno_set = 0;
496 u->u_consts = PyDict_New();
497 if (!u->u_consts) {
498 compiler_unit_free(u);
499 return 0;
501 u->u_names = PyDict_New();
502 if (!u->u_names) {
503 compiler_unit_free(u);
504 return 0;
507 u->u_private = NULL;
509 /* Push the old compiler_unit on the stack. */
510 if (c->u) {
511 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
512 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
513 Py_XDECREF(capsule);
514 compiler_unit_free(u);
515 return 0;
517 Py_DECREF(capsule);
518 u->u_private = c->u->u_private;
519 Py_XINCREF(u->u_private);
521 c->u = u;
523 c->c_nestlevel++;
524 if (compiler_use_new_block(c) == NULL)
525 return 0;
527 return 1;
530 static void
531 compiler_exit_scope(struct compiler *c)
533 int n;
534 PyObject *capsule;
536 c->c_nestlevel--;
537 compiler_unit_free(c->u);
538 /* Restore c->u to the parent unit. */
539 n = PyList_GET_SIZE(c->c_stack) - 1;
540 if (n >= 0) {
541 capsule = PyList_GET_ITEM(c->c_stack, n);
542 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
543 assert(c->u);
544 /* we are deleting from a list so this really shouldn't fail */
545 if (PySequence_DelItem(c->c_stack, n) < 0)
546 Py_FatalError("compiler_exit_scope()");
547 compiler_unit_check(c->u);
549 else
550 c->u = NULL;
554 /* Allocate a new "anonymous" local variable. Used by with statements. */
556 static PyObject *
557 compiler_new_tmpname(struct compiler *c)
559 char tmpname[256];
560 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
561 return PyUnicode_FromString(tmpname);
564 /* Allocate a new block and return a pointer to it.
565 Returns NULL on error.
568 static basicblock *
569 compiler_new_block(struct compiler *c)
571 basicblock *b;
572 struct compiler_unit *u;
574 u = c->u;
575 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
576 if (b == NULL) {
577 PyErr_NoMemory();
578 return NULL;
580 memset((void *)b, 0, sizeof(basicblock));
581 /* Extend the singly linked list of blocks with new block. */
582 b->b_list = u->u_blocks;
583 u->u_blocks = b;
584 return b;
587 static basicblock *
588 compiler_use_new_block(struct compiler *c)
590 basicblock *block = compiler_new_block(c);
591 if (block == NULL)
592 return NULL;
593 c->u->u_curblock = block;
594 return block;
597 static basicblock *
598 compiler_next_block(struct compiler *c)
600 basicblock *block = compiler_new_block(c);
601 if (block == NULL)
602 return NULL;
603 c->u->u_curblock->b_next = block;
604 c->u->u_curblock = block;
605 return block;
608 static basicblock *
609 compiler_use_next_block(struct compiler *c, basicblock *block)
611 assert(block != NULL);
612 c->u->u_curblock->b_next = block;
613 c->u->u_curblock = block;
614 return block;
617 /* Returns the offset of the next instruction in the current block's
618 b_instr array. Resizes the b_instr as necessary.
619 Returns -1 on failure.
622 static int
623 compiler_next_instr(struct compiler *c, basicblock *b)
625 assert(b != NULL);
626 if (b->b_instr == NULL) {
627 b->b_instr = (struct instr *)PyObject_Malloc(
628 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
629 if (b->b_instr == NULL) {
630 PyErr_NoMemory();
631 return -1;
633 b->b_ialloc = DEFAULT_BLOCK_SIZE;
634 memset((char *)b->b_instr, 0,
635 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
637 else if (b->b_iused == b->b_ialloc) {
638 struct instr *tmp;
639 size_t oldsize, newsize;
640 oldsize = b->b_ialloc * sizeof(struct instr);
641 newsize = oldsize << 1;
643 if (oldsize > (PY_SIZE_MAX >> 1)) {
644 PyErr_NoMemory();
645 return -1;
648 if (newsize == 0) {
649 PyErr_NoMemory();
650 return -1;
652 b->b_ialloc <<= 1;
653 tmp = (struct instr *)PyObject_Realloc(
654 (void *)b->b_instr, newsize);
655 if (tmp == NULL) {
656 PyErr_NoMemory();
657 return -1;
659 b->b_instr = tmp;
660 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
662 return b->b_iused++;
665 /* Set the i_lineno member of the instruction at offset off if the
666 line number for the current expression/statement has not
667 already been set. If it has been set, the call has no effect.
669 The line number is reset in the following cases:
670 - when entering a new scope
671 - on each statement
672 - on each expression that start a new line
673 - before the "except" clause
674 - before the "for" and "while" expressions
677 static void
678 compiler_set_lineno(struct compiler *c, int off)
680 basicblock *b;
681 if (c->u->u_lineno_set)
682 return;
683 c->u->u_lineno_set = 1;
684 b = c->u->u_curblock;
685 b->b_instr[off].i_lineno = c->u->u_lineno;
688 static int
689 opcode_stack_effect(int opcode, int oparg)
691 switch (opcode) {
692 case POP_TOP:
693 return -1;
694 case ROT_TWO:
695 case ROT_THREE:
696 return 0;
697 case DUP_TOP:
698 return 1;
699 case ROT_FOUR:
700 return 0;
702 case UNARY_POSITIVE:
703 case UNARY_NEGATIVE:
704 case UNARY_NOT:
705 case UNARY_INVERT:
706 return 0;
708 case SET_ADD:
709 case LIST_APPEND:
710 return -1;
711 case MAP_ADD:
712 return -2;
714 case BINARY_POWER:
715 case BINARY_MULTIPLY:
716 case BINARY_MODULO:
717 case BINARY_ADD:
718 case BINARY_SUBTRACT:
719 case BINARY_SUBSCR:
720 case BINARY_FLOOR_DIVIDE:
721 case BINARY_TRUE_DIVIDE:
722 return -1;
723 case INPLACE_FLOOR_DIVIDE:
724 case INPLACE_TRUE_DIVIDE:
725 return -1;
727 case INPLACE_ADD:
728 case INPLACE_SUBTRACT:
729 case INPLACE_MULTIPLY:
730 case INPLACE_MODULO:
731 return -1;
732 case STORE_SUBSCR:
733 return -3;
734 case STORE_MAP:
735 return -2;
736 case DELETE_SUBSCR:
737 return -2;
739 case BINARY_LSHIFT:
740 case BINARY_RSHIFT:
741 case BINARY_AND:
742 case BINARY_XOR:
743 case BINARY_OR:
744 return -1;
745 case INPLACE_POWER:
746 return -1;
747 case GET_ITER:
748 return 0;
750 case PRINT_EXPR:
751 return -1;
752 case LOAD_BUILD_CLASS:
753 return 1;
754 case INPLACE_LSHIFT:
755 case INPLACE_RSHIFT:
756 case INPLACE_AND:
757 case INPLACE_XOR:
758 case INPLACE_OR:
759 return -1;
760 case BREAK_LOOP:
761 return 0;
762 case WITH_CLEANUP:
763 return -1; /* XXX Sometimes more */
764 case STORE_LOCALS:
765 return -1;
766 case RETURN_VALUE:
767 return -1;
768 case IMPORT_STAR:
769 return -1;
770 case YIELD_VALUE:
771 return 0;
773 case POP_BLOCK:
774 return 0;
775 case POP_EXCEPT:
776 return 0; /* -3 except if bad bytecode */
777 case END_FINALLY:
778 return -1; /* or -2 or -3 if exception occurred */
780 case STORE_NAME:
781 return -1;
782 case DELETE_NAME:
783 return 0;
784 case UNPACK_SEQUENCE:
785 return oparg-1;
786 case UNPACK_EX:
787 return (oparg&0xFF) + (oparg>>8);
788 case FOR_ITER:
789 return 1;
791 case STORE_ATTR:
792 return -2;
793 case DELETE_ATTR:
794 return -1;
795 case STORE_GLOBAL:
796 return -1;
797 case DELETE_GLOBAL:
798 return 0;
799 case DUP_TOPX:
800 return oparg;
801 case LOAD_CONST:
802 return 1;
803 case LOAD_NAME:
804 return 1;
805 case BUILD_TUPLE:
806 case BUILD_LIST:
807 case BUILD_SET:
808 return 1-oparg;
809 case BUILD_MAP:
810 return 1;
811 case LOAD_ATTR:
812 return 0;
813 case COMPARE_OP:
814 return -1;
815 case IMPORT_NAME:
816 return 0;
817 case IMPORT_FROM:
818 return 1;
820 case JUMP_FORWARD:
821 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
822 case JUMP_IF_FALSE_OR_POP: /* "" */
823 case JUMP_ABSOLUTE:
824 return 0;
826 case POP_JUMP_IF_FALSE:
827 case POP_JUMP_IF_TRUE:
828 return -1;
830 case LOAD_GLOBAL:
831 return 1;
833 case CONTINUE_LOOP:
834 return 0;
835 case SETUP_LOOP:
836 return 0;
837 case SETUP_EXCEPT:
838 case SETUP_FINALLY:
839 return 6; /* can push 3 values for the new exception
840 + 3 others for the previous exception state */
842 case LOAD_FAST:
843 return 1;
844 case STORE_FAST:
845 return -1;
846 case DELETE_FAST:
847 return 0;
849 case RAISE_VARARGS:
850 return -oparg;
851 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
852 case CALL_FUNCTION:
853 return -NARGS(oparg);
854 case CALL_FUNCTION_VAR:
855 case CALL_FUNCTION_KW:
856 return -NARGS(oparg)-1;
857 case CALL_FUNCTION_VAR_KW:
858 return -NARGS(oparg)-2;
859 case MAKE_FUNCTION:
860 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
861 case MAKE_CLOSURE:
862 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
863 #undef NARGS
864 case BUILD_SLICE:
865 if (oparg == 3)
866 return -2;
867 else
868 return -1;
870 case LOAD_CLOSURE:
871 return 1;
872 case LOAD_DEREF:
873 return 1;
874 case STORE_DEREF:
875 return -1;
876 default:
877 fprintf(stderr, "opcode = %d\n", opcode);
878 Py_FatalError("opcode_stack_effect()");
881 return 0; /* not reachable */
884 /* Add an opcode with no argument.
885 Returns 0 on failure, 1 on success.
888 static int
889 compiler_addop(struct compiler *c, int opcode)
891 basicblock *b;
892 struct instr *i;
893 int off;
894 off = compiler_next_instr(c, c->u->u_curblock);
895 if (off < 0)
896 return 0;
897 b = c->u->u_curblock;
898 i = &b->b_instr[off];
899 i->i_opcode = opcode;
900 i->i_hasarg = 0;
901 if (opcode == RETURN_VALUE)
902 b->b_return = 1;
903 compiler_set_lineno(c, off);
904 return 1;
907 static int
908 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
910 PyObject *t, *v;
911 Py_ssize_t arg;
912 double d;
914 /* necessary to make sure types aren't coerced (e.g., int and long) */
915 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
916 if (PyFloat_Check(o)) {
917 d = PyFloat_AS_DOUBLE(o);
918 /* all we need is to make the tuple different in either the 0.0
919 * or -0.0 case from all others, just to avoid the "coercion".
921 if (d == 0.0 && copysign(1.0, d) < 0.0)
922 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
923 else
924 t = PyTuple_Pack(2, o, o->ob_type);
926 #ifndef WITHOUT_COMPLEX
927 else if (PyComplex_Check(o)) {
928 Py_complex z;
929 int real_negzero, imag_negzero;
930 /* For the complex case we must make complex(x, 0.)
931 different from complex(x, -0.) and complex(0., y)
932 different from complex(-0., y), for any x and y.
933 All four complex zeros must be distinguished.*/
934 z = PyComplex_AsCComplex(o);
935 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
936 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
937 if (real_negzero && imag_negzero) {
938 t = PyTuple_Pack(5, o, o->ob_type,
939 Py_None, Py_None, Py_None);
941 else if (imag_negzero) {
942 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
944 else if (real_negzero) {
945 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
947 else {
948 t = PyTuple_Pack(2, o, o->ob_type);
951 #endif /* WITHOUT_COMPLEX */
952 else {
953 t = PyTuple_Pack(2, o, o->ob_type);
955 if (t == NULL)
956 return -1;
958 v = PyDict_GetItem(dict, t);
959 if (!v) {
960 if (PyErr_Occurred())
961 return -1;
962 arg = PyDict_Size(dict);
963 v = PyLong_FromLong(arg);
964 if (!v) {
965 Py_DECREF(t);
966 return -1;
968 if (PyDict_SetItem(dict, t, v) < 0) {
969 Py_DECREF(t);
970 Py_DECREF(v);
971 return -1;
973 Py_DECREF(v);
975 else
976 arg = PyLong_AsLong(v);
977 Py_DECREF(t);
978 return arg;
981 static int
982 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
983 PyObject *o)
985 int arg = compiler_add_o(c, dict, o);
986 if (arg < 0)
987 return 0;
988 return compiler_addop_i(c, opcode, arg);
991 static int
992 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
993 PyObject *o)
995 int arg;
996 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
997 if (!mangled)
998 return 0;
999 arg = compiler_add_o(c, dict, mangled);
1000 Py_DECREF(mangled);
1001 if (arg < 0)
1002 return 0;
1003 return compiler_addop_i(c, opcode, arg);
1006 /* Add an opcode with an integer argument.
1007 Returns 0 on failure, 1 on success.
1010 static int
1011 compiler_addop_i(struct compiler *c, int opcode, int oparg)
1013 struct instr *i;
1014 int off;
1015 off = compiler_next_instr(c, c->u->u_curblock);
1016 if (off < 0)
1017 return 0;
1018 i = &c->u->u_curblock->b_instr[off];
1019 i->i_opcode = opcode;
1020 i->i_oparg = oparg;
1021 i->i_hasarg = 1;
1022 compiler_set_lineno(c, off);
1023 return 1;
1026 static int
1027 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1029 struct instr *i;
1030 int off;
1032 assert(b != NULL);
1033 off = compiler_next_instr(c, c->u->u_curblock);
1034 if (off < 0)
1035 return 0;
1036 i = &c->u->u_curblock->b_instr[off];
1037 i->i_opcode = opcode;
1038 i->i_target = b;
1039 i->i_hasarg = 1;
1040 if (absolute)
1041 i->i_jabs = 1;
1042 else
1043 i->i_jrel = 1;
1044 compiler_set_lineno(c, off);
1045 return 1;
1048 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1049 like to find better names.) NEW_BLOCK() creates a new block and sets
1050 it as the current block. NEXT_BLOCK() also creates an implicit jump
1051 from the current block to the new block.
1054 /* The returns inside these macros make it impossible to decref objects
1055 created in the local function. Local objects should use the arena.
1059 #define NEW_BLOCK(C) { \
1060 if (compiler_use_new_block((C)) == NULL) \
1061 return 0; \
1064 #define NEXT_BLOCK(C) { \
1065 if (compiler_next_block((C)) == NULL) \
1066 return 0; \
1069 #define ADDOP(C, OP) { \
1070 if (!compiler_addop((C), (OP))) \
1071 return 0; \
1074 #define ADDOP_IN_SCOPE(C, OP) { \
1075 if (!compiler_addop((C), (OP))) { \
1076 compiler_exit_scope(c); \
1077 return 0; \
1081 #define ADDOP_O(C, OP, O, TYPE) { \
1082 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1083 return 0; \
1086 #define ADDOP_NAME(C, OP, O, TYPE) { \
1087 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1088 return 0; \
1091 #define ADDOP_I(C, OP, O) { \
1092 if (!compiler_addop_i((C), (OP), (O))) \
1093 return 0; \
1096 #define ADDOP_JABS(C, OP, O) { \
1097 if (!compiler_addop_j((C), (OP), (O), 1)) \
1098 return 0; \
1101 #define ADDOP_JREL(C, OP, O) { \
1102 if (!compiler_addop_j((C), (OP), (O), 0)) \
1103 return 0; \
1106 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1107 the ASDL name to synthesize the name of the C type and the visit function.
1110 #define VISIT(C, TYPE, V) {\
1111 if (!compiler_visit_ ## TYPE((C), (V))) \
1112 return 0; \
1115 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1116 if (!compiler_visit_ ## TYPE((C), (V))) { \
1117 compiler_exit_scope(c); \
1118 return 0; \
1122 #define VISIT_SLICE(C, V, CTX) {\
1123 if (!compiler_visit_slice((C), (V), (CTX))) \
1124 return 0; \
1127 #define VISIT_SEQ(C, TYPE, SEQ) { \
1128 int _i; \
1129 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1130 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1131 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1132 if (!compiler_visit_ ## TYPE((C), elt)) \
1133 return 0; \
1137 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1138 int _i; \
1139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1142 if (!compiler_visit_ ## TYPE((C), elt)) { \
1143 compiler_exit_scope(c); \
1144 return 0; \
1149 static int
1150 compiler_isdocstring(stmt_ty s)
1152 if (s->kind != Expr_kind)
1153 return 0;
1154 return s->v.Expr.value->kind == Str_kind;
1157 /* Compile a sequence of statements, checking for a docstring. */
1159 static int
1160 compiler_body(struct compiler *c, asdl_seq *stmts)
1162 int i = 0;
1163 stmt_ty st;
1165 if (!asdl_seq_LEN(stmts))
1166 return 1;
1167 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1168 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1169 /* don't generate docstrings if -OO */
1170 i = 1;
1171 VISIT(c, expr, st->v.Expr.value);
1172 if (!compiler_nameop(c, __doc__, Store))
1173 return 0;
1175 for (; i < asdl_seq_LEN(stmts); i++)
1176 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1177 return 1;
1180 static PyCodeObject *
1181 compiler_mod(struct compiler *c, mod_ty mod)
1183 PyCodeObject *co;
1184 int addNone = 1;
1185 static PyObject *module;
1186 if (!module) {
1187 module = PyUnicode_InternFromString("<module>");
1188 if (!module)
1189 return NULL;
1191 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1192 if (!compiler_enter_scope(c, module, mod, 0))
1193 return NULL;
1194 switch (mod->kind) {
1195 case Module_kind:
1196 if (!compiler_body(c, mod->v.Module.body)) {
1197 compiler_exit_scope(c);
1198 return 0;
1200 break;
1201 case Interactive_kind:
1202 c->c_interactive = 1;
1203 VISIT_SEQ_IN_SCOPE(c, stmt,
1204 mod->v.Interactive.body);
1205 break;
1206 case Expression_kind:
1207 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1208 addNone = 0;
1209 break;
1210 case Suite_kind:
1211 PyErr_SetString(PyExc_SystemError,
1212 "suite should not be possible");
1213 return 0;
1214 default:
1215 PyErr_Format(PyExc_SystemError,
1216 "module kind %d should not be possible",
1217 mod->kind);
1218 return 0;
1220 co = assemble(c, addNone);
1221 compiler_exit_scope(c);
1222 return co;
1225 /* The test for LOCAL must come before the test for FREE in order to
1226 handle classes where name is both local and free. The local var is
1227 a method and the free var is a free var referenced within a method.
1230 static int
1231 get_ref_type(struct compiler *c, PyObject *name)
1233 int scope = PyST_GetScope(c->u->u_ste, name);
1234 if (scope == 0) {
1235 char buf[350];
1236 PyOS_snprintf(buf, sizeof(buf),
1237 "unknown scope for %.100s in %.100s(%s) in %s\n"
1238 "symbols: %s\nlocals: %s\nglobals: %s",
1239 PyBytes_AS_STRING(name),
1240 PyBytes_AS_STRING(c->u->u_name),
1241 PyObject_REPR(c->u->u_ste->ste_id),
1242 c->c_filename,
1243 PyObject_REPR(c->u->u_ste->ste_symbols),
1244 PyObject_REPR(c->u->u_varnames),
1245 PyObject_REPR(c->u->u_names)
1247 Py_FatalError(buf);
1250 return scope;
1253 static int
1254 compiler_lookup_arg(PyObject *dict, PyObject *name)
1256 PyObject *k, *v;
1257 k = PyTuple_Pack(2, name, name->ob_type);
1258 if (k == NULL)
1259 return -1;
1260 v = PyDict_GetItem(dict, k);
1261 Py_DECREF(k);
1262 if (v == NULL)
1263 return -1;
1264 return PyLong_AS_LONG(v);
1267 static int
1268 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1270 int i, free = PyCode_GetNumFree(co);
1271 if (free == 0) {
1272 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1273 ADDOP_I(c, MAKE_FUNCTION, args);
1274 return 1;
1276 for (i = 0; i < free; ++i) {
1277 /* Bypass com_addop_varname because it will generate
1278 LOAD_DEREF but LOAD_CLOSURE is needed.
1280 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1281 int arg, reftype;
1283 /* Special case: If a class contains a method with a
1284 free variable that has the same name as a method,
1285 the name will be considered free *and* local in the
1286 class. It should be handled by the closure, as
1287 well as by the normal name loookup logic.
1289 reftype = get_ref_type(c, name);
1290 if (reftype == CELL)
1291 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1292 else /* (reftype == FREE) */
1293 arg = compiler_lookup_arg(c->u->u_freevars, name);
1294 if (arg == -1) {
1295 fprintf(stderr,
1296 "lookup %s in %s %d %d\n"
1297 "freevars of %s: %s\n",
1298 PyObject_REPR(name),
1299 PyBytes_AS_STRING(c->u->u_name),
1300 reftype, arg,
1301 _PyUnicode_AsString(co->co_name),
1302 PyObject_REPR(co->co_freevars));
1303 Py_FatalError("compiler_make_closure()");
1305 ADDOP_I(c, LOAD_CLOSURE, arg);
1307 ADDOP_I(c, BUILD_TUPLE, free);
1308 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1309 ADDOP_I(c, MAKE_CLOSURE, args);
1310 return 1;
1313 static int
1314 compiler_decorators(struct compiler *c, asdl_seq* decos)
1316 int i;
1318 if (!decos)
1319 return 1;
1321 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1322 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1324 return 1;
1327 static int
1328 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1329 asdl_seq *kw_defaults)
1331 int i, default_count = 0;
1332 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1333 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1334 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1335 if (default_) {
1336 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1337 if (!compiler_visit_expr(c, default_)) {
1338 return -1;
1340 default_count++;
1343 return default_count;
1346 static int
1347 compiler_visit_argannotation(struct compiler *c, identifier id,
1348 expr_ty annotation, PyObject *names)
1350 if (annotation) {
1351 VISIT(c, expr, annotation);
1352 if (PyList_Append(names, id))
1353 return -1;
1355 return 0;
1358 static int
1359 compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1360 PyObject *names)
1362 int i, error;
1363 for (i = 0; i < asdl_seq_LEN(args); i++) {
1364 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1365 error = compiler_visit_argannotation(
1367 arg->arg,
1368 arg->annotation,
1369 names);
1370 if (error)
1371 return error;
1373 return 0;
1376 static int
1377 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1378 expr_ty returns)
1380 /* Push arg annotations and a list of the argument names. Return the #
1381 of items pushed. The expressions are evaluated out-of-order wrt the
1382 source code.
1384 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1386 static identifier return_str;
1387 PyObject *names;
1388 int len;
1389 names = PyList_New(0);
1390 if (!names)
1391 return -1;
1393 if (compiler_visit_argannotations(c, args->args, names))
1394 goto error;
1395 if (args->varargannotation &&
1396 compiler_visit_argannotation(c, args->vararg,
1397 args->varargannotation, names))
1398 goto error;
1399 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1400 goto error;
1401 if (args->kwargannotation &&
1402 compiler_visit_argannotation(c, args->kwarg,
1403 args->kwargannotation, names))
1404 goto error;
1406 if (!return_str) {
1407 return_str = PyUnicode_InternFromString("return");
1408 if (!return_str)
1409 goto error;
1411 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1412 goto error;
1415 len = PyList_GET_SIZE(names);
1416 if (len > 65534) {
1417 /* len must fit in 16 bits, and len is incremented below */
1418 PyErr_SetString(PyExc_SyntaxError,
1419 "too many annotations");
1420 goto error;
1422 if (len) {
1423 /* convert names to a tuple and place on stack */
1424 PyObject *elt;
1425 int i;
1426 PyObject *s = PyTuple_New(len);
1427 if (!s)
1428 goto error;
1429 for (i = 0; i < len; i++) {
1430 elt = PyList_GET_ITEM(names, i);
1431 Py_INCREF(elt);
1432 PyTuple_SET_ITEM(s, i, elt);
1434 ADDOP_O(c, LOAD_CONST, s, consts);
1435 Py_DECREF(s);
1436 len++; /* include the just-pushed tuple */
1438 Py_DECREF(names);
1439 return len;
1441 error:
1442 Py_DECREF(names);
1443 return -1;
1446 static int
1447 compiler_function(struct compiler *c, stmt_ty s)
1449 PyCodeObject *co;
1450 PyObject *first_const = Py_None;
1451 arguments_ty args = s->v.FunctionDef.args;
1452 expr_ty returns = s->v.FunctionDef.returns;
1453 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1454 stmt_ty st;
1455 int i, n, docstring, kw_default_count = 0, arglength;
1456 int num_annotations;
1458 assert(s->kind == FunctionDef_kind);
1460 if (!compiler_decorators(c, decos))
1461 return 0;
1462 if (args->kwonlyargs) {
1463 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1464 args->kw_defaults);
1465 if (res < 0)
1466 return 0;
1467 kw_default_count = res;
1469 if (args->defaults)
1470 VISIT_SEQ(c, expr, args->defaults);
1471 num_annotations = compiler_visit_annotations(c, args, returns);
1472 if (num_annotations < 0)
1473 return 0;
1474 assert((num_annotations & 0xFFFF) == num_annotations);
1476 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1477 s->lineno))
1478 return 0;
1480 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1481 docstring = compiler_isdocstring(st);
1482 if (docstring && Py_OptimizeFlag < 2)
1483 first_const = st->v.Expr.value->v.Str.s;
1484 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1485 compiler_exit_scope(c);
1486 return 0;
1489 c->u->u_argcount = asdl_seq_LEN(args->args);
1490 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1491 n = asdl_seq_LEN(s->v.FunctionDef.body);
1492 /* if there was a docstring, we need to skip the first statement */
1493 for (i = docstring; i < n; i++) {
1494 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1495 VISIT_IN_SCOPE(c, stmt, st);
1497 co = assemble(c, 1);
1498 compiler_exit_scope(c);
1499 if (co == NULL)
1500 return 0;
1502 arglength = asdl_seq_LEN(args->defaults);
1503 arglength |= kw_default_count << 8;
1504 arglength |= num_annotations << 16;
1505 compiler_make_closure(c, co, arglength);
1506 Py_DECREF(co);
1508 /* decorators */
1509 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1510 ADDOP_I(c, CALL_FUNCTION, 1);
1513 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1516 static int
1517 compiler_class(struct compiler *c, stmt_ty s)
1519 PyCodeObject *co;
1520 PyObject *str;
1521 int i;
1522 asdl_seq* decos = s->v.ClassDef.decorator_list;
1524 if (!compiler_decorators(c, decos))
1525 return 0;
1527 /* ultimately generate code for:
1528 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1529 where:
1530 <func> is a function/closure created from the class body;
1531 it has a single argument (__locals__) where the dict
1532 (or MutableSequence) representing the locals is passed
1533 <name> is the class name
1534 <bases> is the positional arguments and *varargs argument
1535 <keywords> is the keyword arguments and **kwds argument
1536 This borrows from compiler_call.
1539 /* 1. compile the class body into a code object */
1540 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1541 return 0;
1542 /* this block represents what we do in the new scope */
1544 /* use the class name for name mangling */
1545 Py_INCREF(s->v.ClassDef.name);
1546 Py_XDECREF(c->u->u_private);
1547 c->u->u_private = s->v.ClassDef.name;
1548 /* force it to have one mandatory argument */
1549 c->u->u_argcount = 1;
1550 /* load the first argument (__locals__) ... */
1551 ADDOP_I(c, LOAD_FAST, 0);
1552 /* ... and store it into f_locals */
1553 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1554 /* load (global) __name__ ... */
1555 str = PyUnicode_InternFromString("__name__");
1556 if (!str || !compiler_nameop(c, str, Load)) {
1557 Py_XDECREF(str);
1558 compiler_exit_scope(c);
1559 return 0;
1561 Py_DECREF(str);
1562 /* ... and store it as __module__ */
1563 str = PyUnicode_InternFromString("__module__");
1564 if (!str || !compiler_nameop(c, str, Store)) {
1565 Py_XDECREF(str);
1566 compiler_exit_scope(c);
1567 return 0;
1569 Py_DECREF(str);
1570 /* compile the body proper */
1571 if (!compiler_body(c, s->v.ClassDef.body)) {
1572 compiler_exit_scope(c);
1573 return 0;
1575 /* return the (empty) __class__ cell */
1576 str = PyUnicode_InternFromString("__class__");
1577 if (str == NULL) {
1578 compiler_exit_scope(c);
1579 return 0;
1581 i = compiler_lookup_arg(c->u->u_cellvars, str);
1582 Py_DECREF(str);
1583 if (i == -1) {
1584 /* This happens when nobody references the cell */
1585 PyErr_Clear();
1586 /* Return None */
1587 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1589 else {
1590 /* Return the cell where to store __class__ */
1591 ADDOP_I(c, LOAD_CLOSURE, i);
1593 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1594 /* create the code object */
1595 co = assemble(c, 1);
1597 /* leave the new scope */
1598 compiler_exit_scope(c);
1599 if (co == NULL)
1600 return 0;
1602 /* 2. load the 'build_class' function */
1603 ADDOP(c, LOAD_BUILD_CLASS);
1605 /* 3. load a function (or closure) made from the code object */
1606 compiler_make_closure(c, co, 0);
1607 Py_DECREF(co);
1609 /* 4. load class name */
1610 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1612 /* 5. generate the rest of the code for the call */
1613 if (!compiler_call_helper(c, 2,
1614 s->v.ClassDef.bases,
1615 s->v.ClassDef.keywords,
1616 s->v.ClassDef.starargs,
1617 s->v.ClassDef.kwargs))
1618 return 0;
1620 /* 6. apply decorators */
1621 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1622 ADDOP_I(c, CALL_FUNCTION, 1);
1625 /* 7. store into <name> */
1626 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1627 return 0;
1628 return 1;
1631 static int
1632 compiler_ifexp(struct compiler *c, expr_ty e)
1634 basicblock *end, *next;
1636 assert(e->kind == IfExp_kind);
1637 end = compiler_new_block(c);
1638 if (end == NULL)
1639 return 0;
1640 next = compiler_new_block(c);
1641 if (next == NULL)
1642 return 0;
1643 VISIT(c, expr, e->v.IfExp.test);
1644 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1645 VISIT(c, expr, e->v.IfExp.body);
1646 ADDOP_JREL(c, JUMP_FORWARD, end);
1647 compiler_use_next_block(c, next);
1648 VISIT(c, expr, e->v.IfExp.orelse);
1649 compiler_use_next_block(c, end);
1650 return 1;
1653 static int
1654 compiler_lambda(struct compiler *c, expr_ty e)
1656 PyCodeObject *co;
1657 static identifier name;
1658 int kw_default_count = 0, arglength;
1659 arguments_ty args = e->v.Lambda.args;
1660 assert(e->kind == Lambda_kind);
1662 if (!name) {
1663 name = PyUnicode_InternFromString("<lambda>");
1664 if (!name)
1665 return 0;
1668 if (args->kwonlyargs) {
1669 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1670 args->kw_defaults);
1671 if (res < 0) return 0;
1672 kw_default_count = res;
1674 if (args->defaults)
1675 VISIT_SEQ(c, expr, args->defaults);
1676 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1677 return 0;
1679 c->u->u_argcount = asdl_seq_LEN(args->args);
1680 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1681 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1682 if (c->u->u_ste->ste_generator) {
1683 ADDOP_IN_SCOPE(c, POP_TOP);
1685 else {
1686 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1688 co = assemble(c, 1);
1689 compiler_exit_scope(c);
1690 if (co == NULL)
1691 return 0;
1693 arglength = asdl_seq_LEN(args->defaults);
1694 arglength |= kw_default_count << 8;
1695 compiler_make_closure(c, co, arglength);
1696 Py_DECREF(co);
1698 return 1;
1701 static int
1702 compiler_if(struct compiler *c, stmt_ty s)
1704 basicblock *end, *next;
1705 int constant;
1706 assert(s->kind == If_kind);
1707 end = compiler_new_block(c);
1708 if (end == NULL)
1709 return 0;
1711 constant = expr_constant(s->v.If.test);
1712 /* constant = 0: "if 0"
1713 * constant = 1: "if 1", "if 2", ...
1714 * constant = -1: rest */
1715 if (constant == 0) {
1716 if (s->v.If.orelse)
1717 VISIT_SEQ(c, stmt, s->v.If.orelse);
1718 } else if (constant == 1) {
1719 VISIT_SEQ(c, stmt, s->v.If.body);
1720 } else {
1721 if (s->v.If.orelse) {
1722 next = compiler_new_block(c);
1723 if (next == NULL)
1724 return 0;
1726 else
1727 next = end;
1728 VISIT(c, expr, s->v.If.test);
1729 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1730 VISIT_SEQ(c, stmt, s->v.If.body);
1731 ADDOP_JREL(c, JUMP_FORWARD, end);
1732 if (s->v.If.orelse) {
1733 compiler_use_next_block(c, next);
1734 VISIT_SEQ(c, stmt, s->v.If.orelse);
1737 compiler_use_next_block(c, end);
1738 return 1;
1741 static int
1742 compiler_for(struct compiler *c, stmt_ty s)
1744 basicblock *start, *cleanup, *end;
1746 start = compiler_new_block(c);
1747 cleanup = compiler_new_block(c);
1748 end = compiler_new_block(c);
1749 if (start == NULL || end == NULL || cleanup == NULL)
1750 return 0;
1751 ADDOP_JREL(c, SETUP_LOOP, end);
1752 if (!compiler_push_fblock(c, LOOP, start))
1753 return 0;
1754 VISIT(c, expr, s->v.For.iter);
1755 ADDOP(c, GET_ITER);
1756 compiler_use_next_block(c, start);
1757 /* for expressions must be traced on each iteration,
1758 so we need to set an extra line number. */
1759 c->u->u_lineno_set = 0;
1760 ADDOP_JREL(c, FOR_ITER, cleanup);
1761 VISIT(c, expr, s->v.For.target);
1762 VISIT_SEQ(c, stmt, s->v.For.body);
1763 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1764 compiler_use_next_block(c, cleanup);
1765 ADDOP(c, POP_BLOCK);
1766 compiler_pop_fblock(c, LOOP, start);
1767 VISIT_SEQ(c, stmt, s->v.For.orelse);
1768 compiler_use_next_block(c, end);
1769 return 1;
1772 static int
1773 compiler_while(struct compiler *c, stmt_ty s)
1775 basicblock *loop, *orelse, *end, *anchor = NULL;
1776 int constant = expr_constant(s->v.While.test);
1778 if (constant == 0) {
1779 if (s->v.While.orelse)
1780 VISIT_SEQ(c, stmt, s->v.While.orelse);
1781 return 1;
1783 loop = compiler_new_block(c);
1784 end = compiler_new_block(c);
1785 if (constant == -1) {
1786 anchor = compiler_new_block(c);
1787 if (anchor == NULL)
1788 return 0;
1790 if (loop == NULL || end == NULL)
1791 return 0;
1792 if (s->v.While.orelse) {
1793 orelse = compiler_new_block(c);
1794 if (orelse == NULL)
1795 return 0;
1797 else
1798 orelse = NULL;
1800 ADDOP_JREL(c, SETUP_LOOP, end);
1801 compiler_use_next_block(c, loop);
1802 if (!compiler_push_fblock(c, LOOP, loop))
1803 return 0;
1804 if (constant == -1) {
1805 /* while expressions must be traced on each iteration,
1806 so we need to set an extra line number. */
1807 c->u->u_lineno_set = 0;
1808 VISIT(c, expr, s->v.While.test);
1809 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1811 VISIT_SEQ(c, stmt, s->v.While.body);
1812 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1814 /* XXX should the two POP instructions be in a separate block
1815 if there is no else clause ?
1818 if (constant == -1) {
1819 compiler_use_next_block(c, anchor);
1820 ADDOP(c, POP_BLOCK);
1822 compiler_pop_fblock(c, LOOP, loop);
1823 if (orelse != NULL) /* what if orelse is just pass? */
1824 VISIT_SEQ(c, stmt, s->v.While.orelse);
1825 compiler_use_next_block(c, end);
1827 return 1;
1830 static int
1831 compiler_continue(struct compiler *c)
1833 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1834 static const char IN_FINALLY_ERROR_MSG[] =
1835 "'continue' not supported inside 'finally' clause";
1836 int i;
1838 if (!c->u->u_nfblocks)
1839 return compiler_error(c, LOOP_ERROR_MSG);
1840 i = c->u->u_nfblocks - 1;
1841 switch (c->u->u_fblock[i].fb_type) {
1842 case LOOP:
1843 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1844 break;
1845 case EXCEPT:
1846 case FINALLY_TRY:
1847 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1848 /* Prevent continue anywhere under a finally
1849 even if hidden in a sub-try or except. */
1850 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1851 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1853 if (i == -1)
1854 return compiler_error(c, LOOP_ERROR_MSG);
1855 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1856 break;
1857 case FINALLY_END:
1858 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1861 return 1;
1864 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1866 SETUP_FINALLY L
1867 <code for body>
1868 POP_BLOCK
1869 LOAD_CONST <None>
1870 L: <code for finalbody>
1871 END_FINALLY
1873 The special instructions use the block stack. Each block
1874 stack entry contains the instruction that created it (here
1875 SETUP_FINALLY), the level of the value stack at the time the
1876 block stack entry was created, and a label (here L).
1878 SETUP_FINALLY:
1879 Pushes the current value stack level and the label
1880 onto the block stack.
1881 POP_BLOCK:
1882 Pops en entry from the block stack, and pops the value
1883 stack until its level is the same as indicated on the
1884 block stack. (The label is ignored.)
1885 END_FINALLY:
1886 Pops a variable number of entries from the *value* stack
1887 and re-raises the exception they specify. The number of
1888 entries popped depends on the (pseudo) exception type.
1890 The block stack is unwound when an exception is raised:
1891 when a SETUP_FINALLY entry is found, the exception is pushed
1892 onto the value stack (and the exception condition is cleared),
1893 and the interpreter jumps to the label gotten from the block
1894 stack.
1897 static int
1898 compiler_try_finally(struct compiler *c, stmt_ty s)
1900 basicblock *body, *end;
1901 body = compiler_new_block(c);
1902 end = compiler_new_block(c);
1903 if (body == NULL || end == NULL)
1904 return 0;
1906 ADDOP_JREL(c, SETUP_FINALLY, end);
1907 compiler_use_next_block(c, body);
1908 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1909 return 0;
1910 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1911 ADDOP(c, POP_BLOCK);
1912 compiler_pop_fblock(c, FINALLY_TRY, body);
1914 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1915 compiler_use_next_block(c, end);
1916 if (!compiler_push_fblock(c, FINALLY_END, end))
1917 return 0;
1918 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1919 ADDOP(c, END_FINALLY);
1920 compiler_pop_fblock(c, FINALLY_END, end);
1922 return 1;
1926 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
1927 (The contents of the value stack is shown in [], with the top
1928 at the right; 'tb' is trace-back info, 'val' the exception's
1929 associated value, and 'exc' the exception.)
1931 Value stack Label Instruction Argument
1932 [] SETUP_EXCEPT L1
1933 [] <code for S>
1934 [] POP_BLOCK
1935 [] JUMP_FORWARD L0
1937 [tb, val, exc] L1: DUP )
1938 [tb, val, exc, exc] <evaluate E1> )
1939 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1940 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1941 [tb, val, exc] POP
1942 [tb, val] <assign to V1> (or POP if no V1)
1943 [tb] POP
1944 [] <code for S1>
1945 JUMP_FORWARD L0
1947 [tb, val, exc] L2: DUP
1948 .............................etc.......................
1950 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1952 [] L0: <next statement>
1954 Of course, parts are not generated if Vi or Ei is not present.
1956 static int
1957 compiler_try_except(struct compiler *c, stmt_ty s)
1959 basicblock *body, *orelse, *except, *end;
1960 int i, n;
1962 body = compiler_new_block(c);
1963 except = compiler_new_block(c);
1964 orelse = compiler_new_block(c);
1965 end = compiler_new_block(c);
1966 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1967 return 0;
1968 ADDOP_JREL(c, SETUP_EXCEPT, except);
1969 compiler_use_next_block(c, body);
1970 if (!compiler_push_fblock(c, EXCEPT, body))
1971 return 0;
1972 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1973 ADDOP(c, POP_BLOCK);
1974 compiler_pop_fblock(c, EXCEPT, body);
1975 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1976 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1977 compiler_use_next_block(c, except);
1978 for (i = 0; i < n; i++) {
1979 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1980 s->v.TryExcept.handlers, i);
1981 if (!handler->v.ExceptHandler.type && i < n-1)
1982 return compiler_error(c, "default 'except:' must be last");
1983 c->u->u_lineno_set = 0;
1984 c->u->u_lineno = handler->lineno;
1985 except = compiler_new_block(c);
1986 if (except == NULL)
1987 return 0;
1988 if (handler->v.ExceptHandler.type) {
1989 ADDOP(c, DUP_TOP);
1990 VISIT(c, expr, handler->v.ExceptHandler.type);
1991 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1992 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1994 ADDOP(c, POP_TOP);
1995 if (handler->v.ExceptHandler.name) {
1996 basicblock *cleanup_end, *cleanup_body;
1998 cleanup_end = compiler_new_block(c);
1999 cleanup_body = compiler_new_block(c);
2000 if(!(cleanup_end || cleanup_body))
2001 return 0;
2003 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2004 ADDOP(c, POP_TOP);
2007 try:
2008 # body
2009 except type as name:
2010 try:
2011 # body
2012 finally:
2013 name = None
2014 del name
2017 /* second try: */
2018 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2019 compiler_use_next_block(c, cleanup_body);
2020 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2021 return 0;
2023 /* second # body */
2024 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2025 ADDOP(c, POP_BLOCK);
2026 ADDOP(c, POP_EXCEPT);
2027 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2029 /* finally: */
2030 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2031 compiler_use_next_block(c, cleanup_end);
2032 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2033 return 0;
2035 /* name = None */
2036 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2037 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2039 /* del name */
2040 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2042 ADDOP(c, END_FINALLY);
2043 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2045 else {
2046 basicblock *cleanup_body;
2048 cleanup_body = compiler_new_block(c);
2049 if(!cleanup_body)
2050 return 0;
2052 ADDOP(c, POP_TOP);
2053 ADDOP(c, POP_TOP);
2054 compiler_use_next_block(c, cleanup_body);
2055 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2056 return 0;
2057 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2058 ADDOP(c, POP_EXCEPT);
2059 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2061 ADDOP_JREL(c, JUMP_FORWARD, end);
2062 compiler_use_next_block(c, except);
2064 ADDOP(c, END_FINALLY);
2065 compiler_use_next_block(c, orelse);
2066 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2067 compiler_use_next_block(c, end);
2068 return 1;
2071 static int
2072 compiler_import_as(struct compiler *c, identifier name, identifier asname)
2074 /* The IMPORT_NAME opcode was already generated. This function
2075 merely needs to bind the result to a name.
2077 If there is a dot in name, we need to split it and emit a
2078 LOAD_ATTR for each name.
2080 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2081 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
2082 if (dot) {
2083 /* Consume the base module name to get the first attribute */
2084 src = dot + 1;
2085 while (dot) {
2086 /* NB src is only defined when dot != NULL */
2087 PyObject *attr;
2088 dot = Py_UNICODE_strchr(src, '.');
2089 attr = PyUnicode_FromUnicode(src,
2090 dot ? dot - src : Py_UNICODE_strlen(src));
2091 if (!attr)
2092 return -1;
2093 ADDOP_O(c, LOAD_ATTR, attr, names);
2094 Py_DECREF(attr);
2095 src = dot + 1;
2098 return compiler_nameop(c, asname, Store);
2101 static int
2102 compiler_import(struct compiler *c, stmt_ty s)
2104 /* The Import node stores a module name like a.b.c as a single
2105 string. This is convenient for all cases except
2106 import a.b.c as d
2107 where we need to parse that string to extract the individual
2108 module names.
2109 XXX Perhaps change the representation to make this case simpler?
2111 int i, n = asdl_seq_LEN(s->v.Import.names);
2113 for (i = 0; i < n; i++) {
2114 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2115 int r;
2116 PyObject *level;
2118 level = PyLong_FromLong(0);
2119 if (level == NULL)
2120 return 0;
2122 ADDOP_O(c, LOAD_CONST, level, consts);
2123 Py_DECREF(level);
2124 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2125 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2127 if (alias->asname) {
2128 r = compiler_import_as(c, alias->name, alias->asname);
2129 if (!r)
2130 return r;
2132 else {
2133 identifier tmp = alias->name;
2134 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2135 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
2136 if (dot)
2137 tmp = PyUnicode_FromUnicode(base,
2138 dot - base);
2139 r = compiler_nameop(c, tmp, Store);
2140 if (dot) {
2141 Py_DECREF(tmp);
2143 if (!r)
2144 return r;
2147 return 1;
2150 static int
2151 compiler_from_import(struct compiler *c, stmt_ty s)
2153 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2155 PyObject *names = PyTuple_New(n);
2156 PyObject *level;
2158 if (!names)
2159 return 0;
2161 level = PyLong_FromLong(s->v.ImportFrom.level);
2162 if (!level) {
2163 Py_DECREF(names);
2164 return 0;
2167 /* build up the names */
2168 for (i = 0; i < n; i++) {
2169 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2170 Py_INCREF(alias->name);
2171 PyTuple_SET_ITEM(names, i, alias->name);
2174 if (s->lineno > c->c_future->ff_lineno) {
2175 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2176 "__future__")) {
2177 Py_DECREF(level);
2178 Py_DECREF(names);
2179 return compiler_error(c,
2180 "from __future__ imports must occur "
2181 "at the beginning of the file");
2186 ADDOP_O(c, LOAD_CONST, level, consts);
2187 Py_DECREF(level);
2188 ADDOP_O(c, LOAD_CONST, names, consts);
2189 Py_DECREF(names);
2190 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2191 for (i = 0; i < n; i++) {
2192 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2193 identifier store_name;
2195 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
2196 assert(n == 1);
2197 ADDOP(c, IMPORT_STAR);
2198 return 1;
2201 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2202 store_name = alias->name;
2203 if (alias->asname)
2204 store_name = alias->asname;
2206 if (!compiler_nameop(c, store_name, Store)) {
2207 Py_DECREF(names);
2208 return 0;
2211 /* remove imported module */
2212 ADDOP(c, POP_TOP);
2213 return 1;
2216 static int
2217 compiler_assert(struct compiler *c, stmt_ty s)
2219 static PyObject *assertion_error = NULL;
2220 basicblock *end;
2222 if (Py_OptimizeFlag)
2223 return 1;
2224 if (assertion_error == NULL) {
2225 assertion_error = PyUnicode_InternFromString("AssertionError");
2226 if (assertion_error == NULL)
2227 return 0;
2229 if (s->v.Assert.test->kind == Tuple_kind &&
2230 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2231 const char* msg =
2232 "assertion is always true, perhaps remove parentheses?";
2233 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2234 c->u->u_lineno, NULL, NULL) == -1)
2235 return 0;
2237 VISIT(c, expr, s->v.Assert.test);
2238 end = compiler_new_block(c);
2239 if (end == NULL)
2240 return 0;
2241 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2242 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2243 if (s->v.Assert.msg) {
2244 VISIT(c, expr, s->v.Assert.msg);
2245 ADDOP_I(c, CALL_FUNCTION, 1);
2247 ADDOP_I(c, RAISE_VARARGS, 1);
2248 compiler_use_next_block(c, end);
2249 return 1;
2252 static int
2253 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2255 int i, n;
2257 /* Always assign a lineno to the next instruction for a stmt. */
2258 c->u->u_lineno = s->lineno;
2259 c->u->u_lineno_set = 0;
2261 switch (s->kind) {
2262 case FunctionDef_kind:
2263 return compiler_function(c, s);
2264 case ClassDef_kind:
2265 return compiler_class(c, s);
2266 case Return_kind:
2267 if (c->u->u_ste->ste_type != FunctionBlock)
2268 return compiler_error(c, "'return' outside function");
2269 if (s->v.Return.value) {
2270 VISIT(c, expr, s->v.Return.value);
2272 else
2273 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2274 ADDOP(c, RETURN_VALUE);
2275 break;
2276 case Delete_kind:
2277 VISIT_SEQ(c, expr, s->v.Delete.targets)
2278 break;
2279 case Assign_kind:
2280 n = asdl_seq_LEN(s->v.Assign.targets);
2281 VISIT(c, expr, s->v.Assign.value);
2282 for (i = 0; i < n; i++) {
2283 if (i < n - 1)
2284 ADDOP(c, DUP_TOP);
2285 VISIT(c, expr,
2286 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2288 break;
2289 case AugAssign_kind:
2290 return compiler_augassign(c, s);
2291 case For_kind:
2292 return compiler_for(c, s);
2293 case While_kind:
2294 return compiler_while(c, s);
2295 case If_kind:
2296 return compiler_if(c, s);
2297 case Raise_kind:
2298 n = 0;
2299 if (s->v.Raise.exc) {
2300 VISIT(c, expr, s->v.Raise.exc);
2301 n++;
2302 if (s->v.Raise.cause) {
2303 VISIT(c, expr, s->v.Raise.cause);
2304 n++;
2307 ADDOP_I(c, RAISE_VARARGS, n);
2308 break;
2309 case TryExcept_kind:
2310 return compiler_try_except(c, s);
2311 case TryFinally_kind:
2312 return compiler_try_finally(c, s);
2313 case Assert_kind:
2314 return compiler_assert(c, s);
2315 case Import_kind:
2316 return compiler_import(c, s);
2317 case ImportFrom_kind:
2318 return compiler_from_import(c, s);
2319 case Global_kind:
2320 case Nonlocal_kind:
2321 break;
2322 case Expr_kind:
2323 if (c->c_interactive && c->c_nestlevel <= 1) {
2324 VISIT(c, expr, s->v.Expr.value);
2325 ADDOP(c, PRINT_EXPR);
2327 else if (s->v.Expr.value->kind != Str_kind &&
2328 s->v.Expr.value->kind != Num_kind) {
2329 VISIT(c, expr, s->v.Expr.value);
2330 ADDOP(c, POP_TOP);
2332 break;
2333 case Pass_kind:
2334 break;
2335 case Break_kind:
2336 if (!compiler_in_loop(c))
2337 return compiler_error(c, "'break' outside loop");
2338 ADDOP(c, BREAK_LOOP);
2339 break;
2340 case Continue_kind:
2341 return compiler_continue(c);
2342 case With_kind:
2343 return compiler_with(c, s);
2345 return 1;
2348 static int
2349 unaryop(unaryop_ty op)
2351 switch (op) {
2352 case Invert:
2353 return UNARY_INVERT;
2354 case Not:
2355 return UNARY_NOT;
2356 case UAdd:
2357 return UNARY_POSITIVE;
2358 case USub:
2359 return UNARY_NEGATIVE;
2360 default:
2361 PyErr_Format(PyExc_SystemError,
2362 "unary op %d should not be possible", op);
2363 return 0;
2367 static int
2368 binop(struct compiler *c, operator_ty op)
2370 switch (op) {
2371 case Add:
2372 return BINARY_ADD;
2373 case Sub:
2374 return BINARY_SUBTRACT;
2375 case Mult:
2376 return BINARY_MULTIPLY;
2377 case Div:
2378 return BINARY_TRUE_DIVIDE;
2379 case Mod:
2380 return BINARY_MODULO;
2381 case Pow:
2382 return BINARY_POWER;
2383 case LShift:
2384 return BINARY_LSHIFT;
2385 case RShift:
2386 return BINARY_RSHIFT;
2387 case BitOr:
2388 return BINARY_OR;
2389 case BitXor:
2390 return BINARY_XOR;
2391 case BitAnd:
2392 return BINARY_AND;
2393 case FloorDiv:
2394 return BINARY_FLOOR_DIVIDE;
2395 default:
2396 PyErr_Format(PyExc_SystemError,
2397 "binary op %d should not be possible", op);
2398 return 0;
2402 static int
2403 cmpop(cmpop_ty op)
2405 switch (op) {
2406 case Eq:
2407 return PyCmp_EQ;
2408 case NotEq:
2409 return PyCmp_NE;
2410 case Lt:
2411 return PyCmp_LT;
2412 case LtE:
2413 return PyCmp_LE;
2414 case Gt:
2415 return PyCmp_GT;
2416 case GtE:
2417 return PyCmp_GE;
2418 case Is:
2419 return PyCmp_IS;
2420 case IsNot:
2421 return PyCmp_IS_NOT;
2422 case In:
2423 return PyCmp_IN;
2424 case NotIn:
2425 return PyCmp_NOT_IN;
2426 default:
2427 return PyCmp_BAD;
2431 static int
2432 inplace_binop(struct compiler *c, operator_ty op)
2434 switch (op) {
2435 case Add:
2436 return INPLACE_ADD;
2437 case Sub:
2438 return INPLACE_SUBTRACT;
2439 case Mult:
2440 return INPLACE_MULTIPLY;
2441 case Div:
2442 return INPLACE_TRUE_DIVIDE;
2443 case Mod:
2444 return INPLACE_MODULO;
2445 case Pow:
2446 return INPLACE_POWER;
2447 case LShift:
2448 return INPLACE_LSHIFT;
2449 case RShift:
2450 return INPLACE_RSHIFT;
2451 case BitOr:
2452 return INPLACE_OR;
2453 case BitXor:
2454 return INPLACE_XOR;
2455 case BitAnd:
2456 return INPLACE_AND;
2457 case FloorDiv:
2458 return INPLACE_FLOOR_DIVIDE;
2459 default:
2460 PyErr_Format(PyExc_SystemError,
2461 "inplace binary op %d should not be possible", op);
2462 return 0;
2466 static int
2467 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2469 int op, scope, arg;
2470 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2472 PyObject *dict = c->u->u_names;
2473 PyObject *mangled;
2474 /* XXX AugStore isn't used anywhere! */
2476 mangled = _Py_Mangle(c->u->u_private, name);
2477 if (!mangled)
2478 return 0;
2480 op = 0;
2481 optype = OP_NAME;
2482 scope = PyST_GetScope(c->u->u_ste, mangled);
2483 switch (scope) {
2484 case FREE:
2485 dict = c->u->u_freevars;
2486 optype = OP_DEREF;
2487 break;
2488 case CELL:
2489 dict = c->u->u_cellvars;
2490 optype = OP_DEREF;
2491 break;
2492 case LOCAL:
2493 if (c->u->u_ste->ste_type == FunctionBlock)
2494 optype = OP_FAST;
2495 break;
2496 case GLOBAL_IMPLICIT:
2497 if (c->u->u_ste->ste_type == FunctionBlock &&
2498 !c->u->u_ste->ste_unoptimized)
2499 optype = OP_GLOBAL;
2500 break;
2501 case GLOBAL_EXPLICIT:
2502 optype = OP_GLOBAL;
2503 break;
2504 default:
2505 /* scope can be 0 */
2506 break;
2509 /* XXX Leave assert here, but handle __doc__ and the like better */
2510 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
2512 switch (optype) {
2513 case OP_DEREF:
2514 switch (ctx) {
2515 case Load: op = LOAD_DEREF; break;
2516 case Store: op = STORE_DEREF; break;
2517 case AugLoad:
2518 case AugStore:
2519 break;
2520 case Del:
2521 PyErr_Format(PyExc_SyntaxError,
2522 "can not delete variable '%S' referenced "
2523 "in nested scope",
2524 name);
2525 Py_DECREF(mangled);
2526 return 0;
2527 case Param:
2528 default:
2529 PyErr_SetString(PyExc_SystemError,
2530 "param invalid for deref variable");
2531 return 0;
2533 break;
2534 case OP_FAST:
2535 switch (ctx) {
2536 case Load: op = LOAD_FAST; break;
2537 case Store: op = STORE_FAST; break;
2538 case Del: op = DELETE_FAST; break;
2539 case AugLoad:
2540 case AugStore:
2541 break;
2542 case Param:
2543 default:
2544 PyErr_SetString(PyExc_SystemError,
2545 "param invalid for local variable");
2546 return 0;
2548 ADDOP_O(c, op, mangled, varnames);
2549 Py_DECREF(mangled);
2550 return 1;
2551 case OP_GLOBAL:
2552 switch (ctx) {
2553 case Load: op = LOAD_GLOBAL; break;
2554 case Store: op = STORE_GLOBAL; break;
2555 case Del: op = DELETE_GLOBAL; break;
2556 case AugLoad:
2557 case AugStore:
2558 break;
2559 case Param:
2560 default:
2561 PyErr_SetString(PyExc_SystemError,
2562 "param invalid for global variable");
2563 return 0;
2565 break;
2566 case OP_NAME:
2567 switch (ctx) {
2568 case Load: op = LOAD_NAME; break;
2569 case Store: op = STORE_NAME; break;
2570 case Del: op = DELETE_NAME; break;
2571 case AugLoad:
2572 case AugStore:
2573 break;
2574 case Param:
2575 default:
2576 PyErr_SetString(PyExc_SystemError,
2577 "param invalid for name variable");
2578 return 0;
2580 break;
2583 assert(op);
2584 arg = compiler_add_o(c, dict, mangled);
2585 Py_DECREF(mangled);
2586 if (arg < 0)
2587 return 0;
2588 return compiler_addop_i(c, op, arg);
2591 static int
2592 compiler_boolop(struct compiler *c, expr_ty e)
2594 basicblock *end;
2595 int jumpi, i, n;
2596 asdl_seq *s;
2598 assert(e->kind == BoolOp_kind);
2599 if (e->v.BoolOp.op == And)
2600 jumpi = JUMP_IF_FALSE_OR_POP;
2601 else
2602 jumpi = JUMP_IF_TRUE_OR_POP;
2603 end = compiler_new_block(c);
2604 if (end == NULL)
2605 return 0;
2606 s = e->v.BoolOp.values;
2607 n = asdl_seq_LEN(s) - 1;
2608 assert(n >= 0);
2609 for (i = 0; i < n; ++i) {
2610 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2611 ADDOP_JABS(c, jumpi, end);
2613 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2614 compiler_use_next_block(c, end);
2615 return 1;
2618 static int
2619 compiler_list(struct compiler *c, expr_ty e)
2621 int n = asdl_seq_LEN(e->v.List.elts);
2622 if (e->v.List.ctx == Store) {
2623 int i, seen_star = 0;
2624 for (i = 0; i < n; i++) {
2625 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2626 if (elt->kind == Starred_kind && !seen_star) {
2627 if ((i >= (1 << 8)) ||
2628 (n-i-1 >= (INT_MAX >> 8)))
2629 return compiler_error(c,
2630 "too many expressions in "
2631 "star-unpacking assignment");
2632 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2633 seen_star = 1;
2634 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2635 } else if (elt->kind == Starred_kind) {
2636 return compiler_error(c,
2637 "two starred expressions in assignment");
2640 if (!seen_star) {
2641 ADDOP_I(c, UNPACK_SEQUENCE, n);
2644 VISIT_SEQ(c, expr, e->v.List.elts);
2645 if (e->v.List.ctx == Load) {
2646 ADDOP_I(c, BUILD_LIST, n);
2648 return 1;
2651 static int
2652 compiler_tuple(struct compiler *c, expr_ty e)
2654 int n = asdl_seq_LEN(e->v.Tuple.elts);
2655 if (e->v.Tuple.ctx == Store) {
2656 int i, seen_star = 0;
2657 for (i = 0; i < n; i++) {
2658 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2659 if (elt->kind == Starred_kind && !seen_star) {
2660 if ((i >= (1 << 8)) ||
2661 (n-i-1 >= (INT_MAX >> 8)))
2662 return compiler_error(c,
2663 "too many expressions in "
2664 "star-unpacking assignment");
2665 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2666 seen_star = 1;
2667 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2668 } else if (elt->kind == Starred_kind) {
2669 return compiler_error(c,
2670 "two starred expressions in assignment");
2673 if (!seen_star) {
2674 ADDOP_I(c, UNPACK_SEQUENCE, n);
2677 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2678 if (e->v.Tuple.ctx == Load) {
2679 ADDOP_I(c, BUILD_TUPLE, n);
2681 return 1;
2684 static int
2685 compiler_compare(struct compiler *c, expr_ty e)
2687 int i, n;
2688 basicblock *cleanup = NULL;
2690 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2691 VISIT(c, expr, e->v.Compare.left);
2692 n = asdl_seq_LEN(e->v.Compare.ops);
2693 assert(n > 0);
2694 if (n > 1) {
2695 cleanup = compiler_new_block(c);
2696 if (cleanup == NULL)
2697 return 0;
2698 VISIT(c, expr,
2699 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2701 for (i = 1; i < n; i++) {
2702 ADDOP(c, DUP_TOP);
2703 ADDOP(c, ROT_THREE);
2704 ADDOP_I(c, COMPARE_OP,
2705 cmpop((cmpop_ty)(asdl_seq_GET(
2706 e->v.Compare.ops, i - 1))));
2707 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2708 NEXT_BLOCK(c);
2709 if (i < (n - 1))
2710 VISIT(c, expr,
2711 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2713 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2714 ADDOP_I(c, COMPARE_OP,
2715 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2716 if (n > 1) {
2717 basicblock *end = compiler_new_block(c);
2718 if (end == NULL)
2719 return 0;
2720 ADDOP_JREL(c, JUMP_FORWARD, end);
2721 compiler_use_next_block(c, cleanup);
2722 ADDOP(c, ROT_TWO);
2723 ADDOP(c, POP_TOP);
2724 compiler_use_next_block(c, end);
2726 return 1;
2729 static int
2730 compiler_call(struct compiler *c, expr_ty e)
2732 VISIT(c, expr, e->v.Call.func);
2733 return compiler_call_helper(c, 0,
2734 e->v.Call.args,
2735 e->v.Call.keywords,
2736 e->v.Call.starargs,
2737 e->v.Call.kwargs);
2740 /* shared code between compiler_call and compiler_class */
2741 static int
2742 compiler_call_helper(struct compiler *c,
2743 int n, /* Args already pushed */
2744 asdl_seq *args,
2745 asdl_seq *keywords,
2746 expr_ty starargs,
2747 expr_ty kwargs)
2749 int code = 0;
2751 n += asdl_seq_LEN(args);
2752 VISIT_SEQ(c, expr, args);
2753 if (keywords) {
2754 VISIT_SEQ(c, keyword, keywords);
2755 n |= asdl_seq_LEN(keywords) << 8;
2757 if (starargs) {
2758 VISIT(c, expr, starargs);
2759 code |= 1;
2761 if (kwargs) {
2762 VISIT(c, expr, kwargs);
2763 code |= 2;
2765 switch (code) {
2766 case 0:
2767 ADDOP_I(c, CALL_FUNCTION, n);
2768 break;
2769 case 1:
2770 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2771 break;
2772 case 2:
2773 ADDOP_I(c, CALL_FUNCTION_KW, n);
2774 break;
2775 case 3:
2776 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2777 break;
2779 return 1;
2783 /* List and set comprehensions and generator expressions work by creating a
2784 nested function to perform the actual iteration. This means that the
2785 iteration variables don't leak into the current scope.
2786 The defined function is called immediately following its definition, with the
2787 result of that call being the result of the expression.
2788 The LC/SC version returns the populated container, while the GE version is
2789 flagged in symtable.c as a generator, so it returns the generator object
2790 when the function is called.
2791 This code *knows* that the loop cannot contain break, continue, or return,
2792 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2794 Possible cleanups:
2795 - iterate over the generator sequence instead of using recursion
2798 static int
2799 compiler_comprehension_generator(struct compiler *c,
2800 asdl_seq *generators, int gen_index,
2801 expr_ty elt, expr_ty val, int type)
2803 /* generate code for the iterator, then each of the ifs,
2804 and then write to the element */
2806 comprehension_ty gen;
2807 basicblock *start, *anchor, *skip, *if_cleanup;
2808 int i, n;
2810 start = compiler_new_block(c);
2811 skip = compiler_new_block(c);
2812 if_cleanup = compiler_new_block(c);
2813 anchor = compiler_new_block(c);
2815 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2816 anchor == NULL)
2817 return 0;
2819 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2821 if (gen_index == 0) {
2822 /* Receive outermost iter as an implicit argument */
2823 c->u->u_argcount = 1;
2824 ADDOP_I(c, LOAD_FAST, 0);
2826 else {
2827 /* Sub-iter - calculate on the fly */
2828 VISIT(c, expr, gen->iter);
2829 ADDOP(c, GET_ITER);
2831 compiler_use_next_block(c, start);
2832 ADDOP_JREL(c, FOR_ITER, anchor);
2833 NEXT_BLOCK(c);
2834 VISIT(c, expr, gen->target);
2836 /* XXX this needs to be cleaned up...a lot! */
2837 n = asdl_seq_LEN(gen->ifs);
2838 for (i = 0; i < n; i++) {
2839 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2840 VISIT(c, expr, e);
2841 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2842 NEXT_BLOCK(c);
2845 if (++gen_index < asdl_seq_LEN(generators))
2846 if (!compiler_comprehension_generator(c,
2847 generators, gen_index,
2848 elt, val, type))
2849 return 0;
2851 /* only append after the last for generator */
2852 if (gen_index >= asdl_seq_LEN(generators)) {
2853 /* comprehension specific code */
2854 switch (type) {
2855 case COMP_GENEXP:
2856 VISIT(c, expr, elt);
2857 ADDOP(c, YIELD_VALUE);
2858 ADDOP(c, POP_TOP);
2859 break;
2860 case COMP_LISTCOMP:
2861 VISIT(c, expr, elt);
2862 ADDOP_I(c, LIST_APPEND, gen_index + 1);
2863 break;
2864 case COMP_SETCOMP:
2865 VISIT(c, expr, elt);
2866 ADDOP_I(c, SET_ADD, gen_index + 1);
2867 break;
2868 case COMP_DICTCOMP:
2869 /* With 'd[k] = v', v is evaluated before k, so we do
2870 the same. */
2871 VISIT(c, expr, val);
2872 VISIT(c, expr, elt);
2873 ADDOP_I(c, MAP_ADD, gen_index + 1);
2874 break;
2875 default:
2876 return 0;
2879 compiler_use_next_block(c, skip);
2881 compiler_use_next_block(c, if_cleanup);
2882 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2883 compiler_use_next_block(c, anchor);
2885 return 1;
2888 static int
2889 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2890 asdl_seq *generators, expr_ty elt, expr_ty val)
2892 PyCodeObject *co = NULL;
2893 expr_ty outermost_iter;
2895 outermost_iter = ((comprehension_ty)
2896 asdl_seq_GET(generators, 0))->iter;
2898 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2899 goto error;
2901 if (type != COMP_GENEXP) {
2902 int op;
2903 switch (type) {
2904 case COMP_LISTCOMP:
2905 op = BUILD_LIST;
2906 break;
2907 case COMP_SETCOMP:
2908 op = BUILD_SET;
2909 break;
2910 case COMP_DICTCOMP:
2911 op = BUILD_MAP;
2912 break;
2913 default:
2914 PyErr_Format(PyExc_SystemError,
2915 "unknown comprehension type %d", type);
2916 goto error_in_scope;
2919 ADDOP_I(c, op, 0);
2922 if (!compiler_comprehension_generator(c, generators, 0, elt,
2923 val, type))
2924 goto error_in_scope;
2926 if (type != COMP_GENEXP) {
2927 ADDOP(c, RETURN_VALUE);
2930 co = assemble(c, 1);
2931 compiler_exit_scope(c);
2932 if (co == NULL)
2933 goto error;
2935 if (!compiler_make_closure(c, co, 0))
2936 goto error;
2937 Py_DECREF(co);
2939 VISIT(c, expr, outermost_iter);
2940 ADDOP(c, GET_ITER);
2941 ADDOP_I(c, CALL_FUNCTION, 1);
2942 return 1;
2943 error_in_scope:
2944 compiler_exit_scope(c);
2945 error:
2946 Py_XDECREF(co);
2947 return 0;
2950 static int
2951 compiler_genexp(struct compiler *c, expr_ty e)
2953 static identifier name;
2954 if (!name) {
2955 name = PyUnicode_FromString("<genexpr>");
2956 if (!name)
2957 return 0;
2959 assert(e->kind == GeneratorExp_kind);
2960 return compiler_comprehension(c, e, COMP_GENEXP, name,
2961 e->v.GeneratorExp.generators,
2962 e->v.GeneratorExp.elt, NULL);
2965 static int
2966 compiler_listcomp(struct compiler *c, expr_ty e)
2968 static identifier name;
2969 if (!name) {
2970 name = PyUnicode_FromString("<listcomp>");
2971 if (!name)
2972 return 0;
2974 assert(e->kind == ListComp_kind);
2975 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2976 e->v.ListComp.generators,
2977 e->v.ListComp.elt, NULL);
2980 static int
2981 compiler_setcomp(struct compiler *c, expr_ty e)
2983 static identifier name;
2984 if (!name) {
2985 name = PyUnicode_FromString("<setcomp>");
2986 if (!name)
2987 return 0;
2989 assert(e->kind == SetComp_kind);
2990 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2991 e->v.SetComp.generators,
2992 e->v.SetComp.elt, NULL);
2996 static int
2997 compiler_dictcomp(struct compiler *c, expr_ty e)
2999 static identifier name;
3000 if (!name) {
3001 name = PyUnicode_FromString("<dictcomp>");
3002 if (!name)
3003 return 0;
3005 assert(e->kind == DictComp_kind);
3006 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3007 e->v.DictComp.generators,
3008 e->v.DictComp.key, e->v.DictComp.value);
3012 static int
3013 compiler_visit_keyword(struct compiler *c, keyword_ty k)
3015 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3016 VISIT(c, expr, k->value);
3017 return 1;
3020 /* Test whether expression is constant. For constants, report
3021 whether they are true or false.
3023 Return values: 1 for true, 0 for false, -1 for non-constant.
3026 static int
3027 expr_constant(expr_ty e)
3029 char *id;
3030 switch (e->kind) {
3031 case Ellipsis_kind:
3032 return 1;
3033 case Num_kind:
3034 return PyObject_IsTrue(e->v.Num.n);
3035 case Str_kind:
3036 return PyObject_IsTrue(e->v.Str.s);
3037 case Name_kind:
3038 /* optimize away names that can't be reassigned */
3039 id = PyBytes_AS_STRING(
3040 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
3041 if (strcmp(id, "True") == 0) return 1;
3042 if (strcmp(id, "False") == 0) return 0;
3043 if (strcmp(id, "None") == 0) return 0;
3044 if (strcmp(id, "__debug__") == 0)
3045 return ! Py_OptimizeFlag;
3046 /* fall through */
3047 default:
3048 return -1;
3053 Implements the with statement from PEP 343.
3055 The semantics outlined in that PEP are as follows:
3057 with EXPR as VAR:
3058 BLOCK
3060 It is implemented roughly as:
3062 context = EXPR
3063 exit = context.__exit__ # not calling it
3064 value = context.__enter__()
3065 try:
3066 VAR = value # if VAR present in the syntax
3067 BLOCK
3068 finally:
3069 if an exception was raised:
3070 exc = copy of (exception, instance, traceback)
3071 else:
3072 exc = (None, None, None)
3073 exit(*exc)
3075 static int
3076 compiler_with(struct compiler *c, stmt_ty s)
3078 static identifier enter_attr, exit_attr;
3079 basicblock *block, *finally;
3080 identifier tmpvalue = NULL, tmpexit = NULL;
3082 assert(s->kind == With_kind);
3084 if (!enter_attr) {
3085 enter_attr = PyUnicode_InternFromString("__enter__");
3086 if (!enter_attr)
3087 return 0;
3089 if (!exit_attr) {
3090 exit_attr = PyUnicode_InternFromString("__exit__");
3091 if (!exit_attr)
3092 return 0;
3095 block = compiler_new_block(c);
3096 finally = compiler_new_block(c);
3097 if (!block || !finally)
3098 return 0;
3100 if (s->v.With.optional_vars) {
3101 /* Create a temporary variable to hold context.__enter__().
3102 We need to do this rather than preserving it on the stack
3103 because SETUP_FINALLY remembers the stack level.
3104 We need to do the assignment *inside* the try/finally
3105 so that context.__exit__() is called when the assignment
3106 fails. But we need to call context.__enter__() *before*
3107 the try/finally so that if it fails we won't call
3108 context.__exit__().
3110 tmpvalue = compiler_new_tmpname(c);
3111 if (tmpvalue == NULL)
3112 return 0;
3113 PyArena_AddPyObject(c->c_arena, tmpvalue);
3115 tmpexit = compiler_new_tmpname(c);
3116 if (tmpexit == NULL)
3117 return 0;
3118 PyArena_AddPyObject(c->c_arena, tmpexit);
3120 /* Evaluate EXPR */
3121 VISIT(c, expr, s->v.With.context_expr);
3123 /* Squirrel away context.__exit__ by stuffing it under context */
3124 ADDOP(c, DUP_TOP);
3125 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3126 if (!compiler_nameop(c, tmpexit, Store))
3127 return 0;
3129 /* Call context.__enter__() */
3130 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3131 ADDOP_I(c, CALL_FUNCTION, 0);
3133 if (s->v.With.optional_vars) {
3134 /* Store it in tmpvalue */
3135 if (!compiler_nameop(c, tmpvalue, Store))
3136 return 0;
3138 else {
3139 /* Discard result from context.__enter__() */
3140 ADDOP(c, POP_TOP);
3143 /* Start the try block */
3144 ADDOP_JREL(c, SETUP_FINALLY, finally);
3146 compiler_use_next_block(c, block);
3147 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3148 return 0;
3151 if (s->v.With.optional_vars) {
3152 /* Bind saved result of context.__enter__() to VAR */
3153 if (!compiler_nameop(c, tmpvalue, Load) ||
3154 !compiler_nameop(c, tmpvalue, Del))
3155 return 0;
3156 VISIT(c, expr, s->v.With.optional_vars);
3159 /* BLOCK code */
3160 VISIT_SEQ(c, stmt, s->v.With.body);
3162 /* End of try block; start the finally block */
3163 ADDOP(c, POP_BLOCK);
3164 compiler_pop_fblock(c, FINALLY_TRY, block);
3166 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3167 compiler_use_next_block(c, finally);
3168 if (!compiler_push_fblock(c, FINALLY_END, finally))
3169 return 0;
3171 /* Finally block starts; context.__exit__ is on the stack under
3172 the exception or return information. Just issue our magic
3173 opcode. */
3174 if (!compiler_nameop(c, tmpexit, Load) ||
3175 !compiler_nameop(c, tmpexit, Del))
3176 return 0;
3177 ADDOP(c, WITH_CLEANUP);
3179 /* Finally block ends. */
3180 ADDOP(c, END_FINALLY);
3181 compiler_pop_fblock(c, FINALLY_END, finally);
3182 return 1;
3185 static int
3186 compiler_visit_expr(struct compiler *c, expr_ty e)
3188 int i, n;
3190 /* If expr e has a different line number than the last expr/stmt,
3191 set a new line number for the next instruction.
3193 if (e->lineno > c->u->u_lineno) {
3194 c->u->u_lineno = e->lineno;
3195 c->u->u_lineno_set = 0;
3197 switch (e->kind) {
3198 case BoolOp_kind:
3199 return compiler_boolop(c, e);
3200 case BinOp_kind:
3201 VISIT(c, expr, e->v.BinOp.left);
3202 VISIT(c, expr, e->v.BinOp.right);
3203 ADDOP(c, binop(c, e->v.BinOp.op));
3204 break;
3205 case UnaryOp_kind:
3206 VISIT(c, expr, e->v.UnaryOp.operand);
3207 ADDOP(c, unaryop(e->v.UnaryOp.op));
3208 break;
3209 case Lambda_kind:
3210 return compiler_lambda(c, e);
3211 case IfExp_kind:
3212 return compiler_ifexp(c, e);
3213 case Dict_kind:
3214 n = asdl_seq_LEN(e->v.Dict.values);
3215 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3216 for (i = 0; i < n; i++) {
3217 VISIT(c, expr,
3218 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3219 VISIT(c, expr,
3220 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3221 ADDOP(c, STORE_MAP);
3223 break;
3224 case Set_kind:
3225 n = asdl_seq_LEN(e->v.Set.elts);
3226 VISIT_SEQ(c, expr, e->v.Set.elts);
3227 ADDOP_I(c, BUILD_SET, n);
3228 break;
3229 case GeneratorExp_kind:
3230 return compiler_genexp(c, e);
3231 case ListComp_kind:
3232 return compiler_listcomp(c, e);
3233 case SetComp_kind:
3234 return compiler_setcomp(c, e);
3235 case DictComp_kind:
3236 return compiler_dictcomp(c, e);
3237 case Yield_kind:
3238 if (c->u->u_ste->ste_type != FunctionBlock)
3239 return compiler_error(c, "'yield' outside function");
3240 if (e->v.Yield.value) {
3241 VISIT(c, expr, e->v.Yield.value);
3243 else {
3244 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3246 ADDOP(c, YIELD_VALUE);
3247 break;
3248 case Compare_kind:
3249 return compiler_compare(c, e);
3250 case Call_kind:
3251 return compiler_call(c, e);
3252 case Num_kind:
3253 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3254 break;
3255 case Str_kind:
3256 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3257 break;
3258 case Bytes_kind:
3259 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3260 break;
3261 case Ellipsis_kind:
3262 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3263 break;
3264 /* The following exprs can be assignment targets. */
3265 case Attribute_kind:
3266 if (e->v.Attribute.ctx != AugStore)
3267 VISIT(c, expr, e->v.Attribute.value);
3268 switch (e->v.Attribute.ctx) {
3269 case AugLoad:
3270 ADDOP(c, DUP_TOP);
3271 /* Fall through to load */
3272 case Load:
3273 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3274 break;
3275 case AugStore:
3276 ADDOP(c, ROT_TWO);
3277 /* Fall through to save */
3278 case Store:
3279 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3280 break;
3281 case Del:
3282 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3283 break;
3284 case Param:
3285 default:
3286 PyErr_SetString(PyExc_SystemError,
3287 "param invalid in attribute expression");
3288 return 0;
3290 break;
3291 case Subscript_kind:
3292 switch (e->v.Subscript.ctx) {
3293 case AugLoad:
3294 VISIT(c, expr, e->v.Subscript.value);
3295 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3296 break;
3297 case Load:
3298 VISIT(c, expr, e->v.Subscript.value);
3299 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3300 break;
3301 case AugStore:
3302 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3303 break;
3304 case Store:
3305 VISIT(c, expr, e->v.Subscript.value);
3306 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3307 break;
3308 case Del:
3309 VISIT(c, expr, e->v.Subscript.value);
3310 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3311 break;
3312 case Param:
3313 default:
3314 PyErr_SetString(PyExc_SystemError,
3315 "param invalid in subscript expression");
3316 return 0;
3318 break;
3319 case Starred_kind:
3320 switch (e->v.Starred.ctx) {
3321 case Store:
3322 /* In all legitimate cases, the Starred node was already replaced
3323 * by compiler_list/compiler_tuple. XXX: is that okay? */
3324 return compiler_error(c,
3325 "starred assignment target must be in a list or tuple");
3326 default:
3327 return compiler_error(c,
3328 "can use starred expression only as assignment target");
3330 break;
3331 case Name_kind:
3332 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3333 /* child nodes of List and Tuple will have expr_context set */
3334 case List_kind:
3335 return compiler_list(c, e);
3336 case Tuple_kind:
3337 return compiler_tuple(c, e);
3339 return 1;
3342 static int
3343 compiler_augassign(struct compiler *c, stmt_ty s)
3345 expr_ty e = s->v.AugAssign.target;
3346 expr_ty auge;
3348 assert(s->kind == AugAssign_kind);
3350 switch (e->kind) {
3351 case Attribute_kind:
3352 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3353 AugLoad, e->lineno, e->col_offset, c->c_arena);
3354 if (auge == NULL)
3355 return 0;
3356 VISIT(c, expr, auge);
3357 VISIT(c, expr, s->v.AugAssign.value);
3358 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3359 auge->v.Attribute.ctx = AugStore;
3360 VISIT(c, expr, auge);
3361 break;
3362 case Subscript_kind:
3363 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3364 AugLoad, e->lineno, e->col_offset, c->c_arena);
3365 if (auge == NULL)
3366 return 0;
3367 VISIT(c, expr, auge);
3368 VISIT(c, expr, s->v.AugAssign.value);
3369 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3370 auge->v.Subscript.ctx = AugStore;
3371 VISIT(c, expr, auge);
3372 break;
3373 case Name_kind:
3374 if (!compiler_nameop(c, e->v.Name.id, Load))
3375 return 0;
3376 VISIT(c, expr, s->v.AugAssign.value);
3377 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3378 return compiler_nameop(c, e->v.Name.id, Store);
3379 default:
3380 PyErr_Format(PyExc_SystemError,
3381 "invalid node type (%d) for augmented assignment",
3382 e->kind);
3383 return 0;
3385 return 1;
3388 static int
3389 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3391 struct fblockinfo *f;
3392 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3393 PyErr_SetString(PyExc_SystemError,
3394 "too many statically nested blocks");
3395 return 0;
3397 f = &c->u->u_fblock[c->u->u_nfblocks++];
3398 f->fb_type = t;
3399 f->fb_block = b;
3400 return 1;
3403 static void
3404 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3406 struct compiler_unit *u = c->u;
3407 assert(u->u_nfblocks > 0);
3408 u->u_nfblocks--;
3409 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3410 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3413 static int
3414 compiler_in_loop(struct compiler *c) {
3415 int i;
3416 struct compiler_unit *u = c->u;
3417 for (i = 0; i < u->u_nfblocks; ++i) {
3418 if (u->u_fblock[i].fb_type == LOOP)
3419 return 1;
3421 return 0;
3423 /* Raises a SyntaxError and returns 0.
3424 If something goes wrong, a different exception may be raised.
3427 static int
3428 compiler_error(struct compiler *c, const char *errstr)
3430 PyObject *loc;
3431 PyObject *u = NULL, *v = NULL;
3433 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3434 if (!loc) {
3435 Py_INCREF(Py_None);
3436 loc = Py_None;
3438 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3439 Py_None, loc);
3440 if (!u)
3441 goto exit;
3442 v = Py_BuildValue("(zO)", errstr, u);
3443 if (!v)
3444 goto exit;
3445 PyErr_SetObject(PyExc_SyntaxError, v);
3446 exit:
3447 Py_DECREF(loc);
3448 Py_XDECREF(u);
3449 Py_XDECREF(v);
3450 return 0;
3453 static int
3454 compiler_handle_subscr(struct compiler *c, const char *kind,
3455 expr_context_ty ctx)
3457 int op = 0;
3459 /* XXX this code is duplicated */
3460 switch (ctx) {
3461 case AugLoad: /* fall through to Load */
3462 case Load: op = BINARY_SUBSCR; break;
3463 case AugStore:/* fall through to Store */
3464 case Store: op = STORE_SUBSCR; break;
3465 case Del: op = DELETE_SUBSCR; break;
3466 case Param:
3467 PyErr_Format(PyExc_SystemError,
3468 "invalid %s kind %d in subscript\n",
3469 kind, ctx);
3470 return 0;
3472 if (ctx == AugLoad) {
3473 ADDOP_I(c, DUP_TOPX, 2);
3475 else if (ctx == AugStore) {
3476 ADDOP(c, ROT_THREE);
3478 ADDOP(c, op);
3479 return 1;
3482 static int
3483 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3485 int n = 2;
3486 assert(s->kind == Slice_kind);
3488 /* only handles the cases where BUILD_SLICE is emitted */
3489 if (s->v.Slice.lower) {
3490 VISIT(c, expr, s->v.Slice.lower);
3492 else {
3493 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3496 if (s->v.Slice.upper) {
3497 VISIT(c, expr, s->v.Slice.upper);
3499 else {
3500 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3503 if (s->v.Slice.step) {
3504 n++;
3505 VISIT(c, expr, s->v.Slice.step);
3507 ADDOP_I(c, BUILD_SLICE, n);
3508 return 1;
3511 static int
3512 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3513 expr_context_ty ctx)
3515 switch (s->kind) {
3516 case Slice_kind:
3517 return compiler_slice(c, s, ctx);
3518 case Index_kind:
3519 VISIT(c, expr, s->v.Index.value);
3520 break;
3521 case ExtSlice_kind:
3522 default:
3523 PyErr_SetString(PyExc_SystemError,
3524 "extended slice invalid in nested slice");
3525 return 0;
3527 return 1;
3530 static int
3531 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3533 char * kindname = NULL;
3534 switch (s->kind) {
3535 case Index_kind:
3536 kindname = "index";
3537 if (ctx != AugStore) {
3538 VISIT(c, expr, s->v.Index.value);
3540 break;
3541 case Slice_kind:
3542 kindname = "slice";
3543 if (ctx != AugStore) {
3544 if (!compiler_slice(c, s, ctx))
3545 return 0;
3547 break;
3548 case ExtSlice_kind:
3549 kindname = "extended slice";
3550 if (ctx != AugStore) {
3551 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3552 for (i = 0; i < n; i++) {
3553 slice_ty sub = (slice_ty)asdl_seq_GET(
3554 s->v.ExtSlice.dims, i);
3555 if (!compiler_visit_nested_slice(c, sub, ctx))
3556 return 0;
3558 ADDOP_I(c, BUILD_TUPLE, n);
3560 break;
3561 default:
3562 PyErr_Format(PyExc_SystemError,
3563 "invalid subscript kind %d", s->kind);
3564 return 0;
3566 return compiler_handle_subscr(c, kindname, ctx);
3569 /* End of the compiler section, beginning of the assembler section */
3571 /* do depth-first search of basic block graph, starting with block.
3572 post records the block indices in post-order.
3574 XXX must handle implicit jumps from one block to next
3577 struct assembler {
3578 PyObject *a_bytecode; /* string containing bytecode */
3579 int a_offset; /* offset into bytecode */
3580 int a_nblocks; /* number of reachable blocks */
3581 basicblock **a_postorder; /* list of blocks in dfs postorder */
3582 PyObject *a_lnotab; /* string containing lnotab */
3583 int a_lnotab_off; /* offset into lnotab */
3584 int a_lineno; /* last lineno of emitted instruction */
3585 int a_lineno_off; /* bytecode offset of last lineno */
3588 static void
3589 dfs(struct compiler *c, basicblock *b, struct assembler *a)
3591 int i;
3592 struct instr *instr = NULL;
3594 if (b->b_seen)
3595 return;
3596 b->b_seen = 1;
3597 if (b->b_next != NULL)
3598 dfs(c, b->b_next, a);
3599 for (i = 0; i < b->b_iused; i++) {
3600 instr = &b->b_instr[i];
3601 if (instr->i_jrel || instr->i_jabs)
3602 dfs(c, instr->i_target, a);
3604 a->a_postorder[a->a_nblocks++] = b;
3607 static int
3608 stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3610 int i;
3611 struct instr *instr;
3612 if (b->b_seen || b->b_startdepth >= depth)
3613 return maxdepth;
3614 b->b_seen = 1;
3615 b->b_startdepth = depth;
3616 for (i = 0; i < b->b_iused; i++) {
3617 instr = &b->b_instr[i];
3618 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3619 if (depth > maxdepth)
3620 maxdepth = depth;
3621 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3622 if (instr->i_jrel || instr->i_jabs) {
3623 maxdepth = stackdepth_walk(c, instr->i_target,
3624 depth, maxdepth);
3625 if (instr->i_opcode == JUMP_ABSOLUTE ||
3626 instr->i_opcode == JUMP_FORWARD) {
3627 goto out; /* remaining code is dead */
3631 if (b->b_next)
3632 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3633 out:
3634 b->b_seen = 0;
3635 return maxdepth;
3638 /* Find the flow path that needs the largest stack. We assume that
3639 * cycles in the flow graph have no net effect on the stack depth.
3641 static int
3642 stackdepth(struct compiler *c)
3644 basicblock *b, *entryblock;
3645 entryblock = NULL;
3646 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3647 b->b_seen = 0;
3648 b->b_startdepth = INT_MIN;
3649 entryblock = b;
3651 if (!entryblock)
3652 return 0;
3653 return stackdepth_walk(c, entryblock, 0, 0);
3656 static int
3657 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3659 memset(a, 0, sizeof(struct assembler));
3660 a->a_lineno = firstlineno;
3661 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3662 if (!a->a_bytecode)
3663 return 0;
3664 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3665 if (!a->a_lnotab)
3666 return 0;
3667 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3668 PyErr_NoMemory();
3669 return 0;
3671 a->a_postorder = (basicblock **)PyObject_Malloc(
3672 sizeof(basicblock *) * nblocks);
3673 if (!a->a_postorder) {
3674 PyErr_NoMemory();
3675 return 0;
3677 return 1;
3680 static void
3681 assemble_free(struct assembler *a)
3683 Py_XDECREF(a->a_bytecode);
3684 Py_XDECREF(a->a_lnotab);
3685 if (a->a_postorder)
3686 PyObject_Free(a->a_postorder);
3689 /* Return the size of a basic block in bytes. */
3691 static int
3692 instrsize(struct instr *instr)
3694 if (!instr->i_hasarg)
3695 return 1; /* 1 byte for the opcode*/
3696 if (instr->i_oparg > 0xffff)
3697 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3698 return 3; /* 1 (opcode) + 2 (oparg) */
3701 static int
3702 blocksize(basicblock *b)
3704 int i;
3705 int size = 0;
3707 for (i = 0; i < b->b_iused; i++)
3708 size += instrsize(&b->b_instr[i]);
3709 return size;
3712 /* All about a_lnotab.
3714 c_lnotab is an array of unsigned bytes disguised as a Python string.
3715 It is used to map bytecode offsets to source code line #s (when needed
3716 for tracebacks).
3718 The array is conceptually a list of
3719 (bytecode offset increment, line number increment)
3720 pairs. The details are important and delicate, best illustrated by example:
3722 byte code offset source code line number
3725 50 7
3726 350 307
3727 361 308
3729 The first trick is that these numbers aren't stored, only the increments
3730 from one row to the next (this doesn't really work, but it's a start):
3732 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3734 The second trick is that an unsigned byte can't hold negative values, or
3735 values larger than 255, so (a) there's a deep assumption that byte code
3736 offsets and their corresponding line #s both increase monotonically, and (b)
3737 if at least one column jumps by more than 255 from one row to the next, more
3738 than one pair is written to the table. In case #b, there's no way to know
3739 from looking at the table later how many were written. That's the delicate
3740 part. A user of c_lnotab desiring to find the source line number
3741 corresponding to a bytecode address A should do something like this
3743 lineno = addr = 0
3744 for addr_incr, line_incr in c_lnotab:
3745 addr += addr_incr
3746 if addr > A:
3747 return lineno
3748 lineno += line_incr
3750 In order for this to work, when the addr field increments by more than 255,
3751 the line # increment in each pair generated must be 0 until the remaining addr
3752 increment is < 256. So, in the example above, assemble_lnotab (it used
3753 to be called com_set_lineno) should not (as was actually done until 2.2)
3754 expand 300, 300 to 255, 255, 45, 45,
3755 but to 255, 0, 45, 255, 0, 45.
3758 static int
3759 assemble_lnotab(struct assembler *a, struct instr *i)
3761 int d_bytecode, d_lineno;
3762 int len;
3763 unsigned char *lnotab;
3765 d_bytecode = a->a_offset - a->a_lineno_off;
3766 d_lineno = i->i_lineno - a->a_lineno;
3768 assert(d_bytecode >= 0);
3769 assert(d_lineno >= 0);
3771 if(d_bytecode == 0 && d_lineno == 0)
3772 return 1;
3774 if (d_bytecode > 255) {
3775 int j, nbytes, ncodes = d_bytecode / 255;
3776 nbytes = a->a_lnotab_off + 2 * ncodes;
3777 len = PyBytes_GET_SIZE(a->a_lnotab);
3778 if (nbytes >= len) {
3779 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3780 len = nbytes;
3781 else if (len <= INT_MAX / 2)
3782 len *= 2;
3783 else {
3784 PyErr_NoMemory();
3785 return 0;
3787 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3788 return 0;
3790 lnotab = (unsigned char *)
3791 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3792 for (j = 0; j < ncodes; j++) {
3793 *lnotab++ = 255;
3794 *lnotab++ = 0;
3796 d_bytecode -= ncodes * 255;
3797 a->a_lnotab_off += ncodes * 2;
3799 assert(d_bytecode <= 255);
3800 if (d_lineno > 255) {
3801 int j, nbytes, ncodes = d_lineno / 255;
3802 nbytes = a->a_lnotab_off + 2 * ncodes;
3803 len = PyBytes_GET_SIZE(a->a_lnotab);
3804 if (nbytes >= len) {
3805 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3806 len = nbytes;
3807 else if (len <= INT_MAX / 2)
3808 len *= 2;
3809 else {
3810 PyErr_NoMemory();
3811 return 0;
3813 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3814 return 0;
3816 lnotab = (unsigned char *)
3817 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3818 *lnotab++ = d_bytecode;
3819 *lnotab++ = 255;
3820 d_bytecode = 0;
3821 for (j = 1; j < ncodes; j++) {
3822 *lnotab++ = 0;
3823 *lnotab++ = 255;
3825 d_lineno -= ncodes * 255;
3826 a->a_lnotab_off += ncodes * 2;
3829 len = PyBytes_GET_SIZE(a->a_lnotab);
3830 if (a->a_lnotab_off + 2 >= len) {
3831 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3832 return 0;
3834 lnotab = (unsigned char *)
3835 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3837 a->a_lnotab_off += 2;
3838 if (d_bytecode) {
3839 *lnotab++ = d_bytecode;
3840 *lnotab++ = d_lineno;
3842 else { /* First line of a block; def stmt, etc. */
3843 *lnotab++ = 0;
3844 *lnotab++ = d_lineno;
3846 a->a_lineno = i->i_lineno;
3847 a->a_lineno_off = a->a_offset;
3848 return 1;
3851 /* assemble_emit()
3852 Extend the bytecode with a new instruction.
3853 Update lnotab if necessary.
3856 static int
3857 assemble_emit(struct assembler *a, struct instr *i)
3859 int size, arg = 0, ext = 0;
3860 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3861 char *code;
3863 size = instrsize(i);
3864 if (i->i_hasarg) {
3865 arg = i->i_oparg;
3866 ext = arg >> 16;
3868 if (i->i_lineno && !assemble_lnotab(a, i))
3869 return 0;
3870 if (a->a_offset + size >= len) {
3871 if (len > PY_SSIZE_T_MAX / 2)
3872 return 0;
3873 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3874 return 0;
3876 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3877 a->a_offset += size;
3878 if (size == 6) {
3879 assert(i->i_hasarg);
3880 *code++ = (char)EXTENDED_ARG;
3881 *code++ = ext & 0xff;
3882 *code++ = ext >> 8;
3883 arg &= 0xffff;
3885 *code++ = i->i_opcode;
3886 if (i->i_hasarg) {
3887 assert(size == 3 || size == 6);
3888 *code++ = arg & 0xff;
3889 *code++ = arg >> 8;
3891 return 1;
3894 static void
3895 assemble_jump_offsets(struct assembler *a, struct compiler *c)
3897 basicblock *b;
3898 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
3899 int i;
3901 /* Compute the size of each block and fixup jump args.
3902 Replace block pointer with position in bytecode. */
3903 start:
3904 totsize = 0;
3905 for (i = a->a_nblocks - 1; i >= 0; i--) {
3906 b = a->a_postorder[i];
3907 bsize = blocksize(b);
3908 b->b_offset = totsize;
3909 totsize += bsize;
3911 extended_arg_count = 0;
3912 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3913 bsize = b->b_offset;
3914 for (i = 0; i < b->b_iused; i++) {
3915 struct instr *instr = &b->b_instr[i];
3916 /* Relative jumps are computed relative to
3917 the instruction pointer after fetching
3918 the jump instruction.
3920 bsize += instrsize(instr);
3921 if (instr->i_jabs)
3922 instr->i_oparg = instr->i_target->b_offset;
3923 else if (instr->i_jrel) {
3924 int delta = instr->i_target->b_offset - bsize;
3925 instr->i_oparg = delta;
3927 else
3928 continue;
3929 if (instr->i_oparg > 0xffff)
3930 extended_arg_count++;
3934 /* XXX: This is an awful hack that could hurt performance, but
3935 on the bright side it should work until we come up
3936 with a better solution.
3938 In the meantime, should the goto be dropped in favor
3939 of a loop?
3941 The issue is that in the first loop blocksize() is called
3942 which calls instrsize() which requires i_oparg be set
3943 appropriately. There is a bootstrap problem because
3944 i_oparg is calculated in the second loop above.
3946 So we loop until we stop seeing new EXTENDED_ARGs.
3947 The only EXTENDED_ARGs that could be popping up are
3948 ones in jump instructions. So this should converge
3949 fairly quickly.
3951 if (last_extended_arg_count != extended_arg_count) {
3952 last_extended_arg_count = extended_arg_count;
3953 goto start;
3957 static PyObject *
3958 dict_keys_inorder(PyObject *dict, int offset)
3960 PyObject *tuple, *k, *v;
3961 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
3963 tuple = PyTuple_New(size);
3964 if (tuple == NULL)
3965 return NULL;
3966 while (PyDict_Next(dict, &pos, &k, &v)) {
3967 i = PyLong_AS_LONG(v);
3968 /* The keys of the dictionary are tuples. (see compiler_add_o)
3969 The object we want is always first, though. */
3970 k = PyTuple_GET_ITEM(k, 0);
3971 Py_INCREF(k);
3972 assert((i - offset) < size);
3973 assert((i - offset) >= 0);
3974 PyTuple_SET_ITEM(tuple, i - offset, k);
3976 return tuple;
3979 static int
3980 compute_code_flags(struct compiler *c)
3982 PySTEntryObject *ste = c->u->u_ste;
3983 int flags = 0, n;
3984 if (ste->ste_type != ModuleBlock)
3985 flags |= CO_NEWLOCALS;
3986 if (ste->ste_type == FunctionBlock) {
3987 if (!ste->ste_unoptimized)
3988 flags |= CO_OPTIMIZED;
3989 if (ste->ste_nested)
3990 flags |= CO_NESTED;
3991 if (ste->ste_generator)
3992 flags |= CO_GENERATOR;
3993 if (ste->ste_varargs)
3994 flags |= CO_VARARGS;
3995 if (ste->ste_varkeywords)
3996 flags |= CO_VARKEYWORDS;
3999 /* (Only) inherit compilerflags in PyCF_MASK */
4000 flags |= (c->c_flags->cf_flags & PyCF_MASK);
4002 n = PyDict_Size(c->u->u_freevars);
4003 if (n < 0)
4004 return -1;
4005 if (n == 0) {
4006 n = PyDict_Size(c->u->u_cellvars);
4007 if (n < 0)
4008 return -1;
4009 if (n == 0) {
4010 flags |= CO_NOFREE;
4014 return flags;
4017 static PyCodeObject *
4018 makecode(struct compiler *c, struct assembler *a)
4020 PyObject *tmp;
4021 PyCodeObject *co = NULL;
4022 PyObject *consts = NULL;
4023 PyObject *names = NULL;
4024 PyObject *varnames = NULL;
4025 PyObject *filename = NULL;
4026 PyObject *name = NULL;
4027 PyObject *freevars = NULL;
4028 PyObject *cellvars = NULL;
4029 PyObject *bytecode = NULL;
4030 int nlocals, flags;
4032 tmp = dict_keys_inorder(c->u->u_consts, 0);
4033 if (!tmp)
4034 goto error;
4035 consts = PySequence_List(tmp); /* optimize_code requires a list */
4036 Py_DECREF(tmp);
4038 names = dict_keys_inorder(c->u->u_names, 0);
4039 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4040 if (!consts || !names || !varnames)
4041 goto error;
4043 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4044 if (!cellvars)
4045 goto error;
4046 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4047 if (!freevars)
4048 goto error;
4049 filename = PyUnicode_DecodeFSDefault(c->c_filename);
4050 if (!filename)
4051 goto error;
4053 nlocals = PyDict_Size(c->u->u_varnames);
4054 flags = compute_code_flags(c);
4055 if (flags < 0)
4056 goto error;
4058 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4059 if (!bytecode)
4060 goto error;
4062 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4063 if (!tmp)
4064 goto error;
4065 Py_DECREF(consts);
4066 consts = tmp;
4068 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4069 nlocals, stackdepth(c), flags,
4070 bytecode, consts, names, varnames,
4071 freevars, cellvars,
4072 filename, c->u->u_name,
4073 c->u->u_firstlineno,
4074 a->a_lnotab);
4075 error:
4076 Py_XDECREF(consts);
4077 Py_XDECREF(names);
4078 Py_XDECREF(varnames);
4079 Py_XDECREF(filename);
4080 Py_XDECREF(name);
4081 Py_XDECREF(freevars);
4082 Py_XDECREF(cellvars);
4083 Py_XDECREF(bytecode);
4084 return co;
4088 /* For debugging purposes only */
4089 #if 0
4090 static void
4091 dump_instr(const struct instr *i)
4093 const char *jrel = i->i_jrel ? "jrel " : "";
4094 const char *jabs = i->i_jabs ? "jabs " : "";
4095 char arg[128];
4097 *arg = '\0';
4098 if (i->i_hasarg)
4099 sprintf(arg, "arg: %d ", i->i_oparg);
4101 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4102 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4105 static void
4106 dump_basicblock(const basicblock *b)
4108 const char *seen = b->b_seen ? "seen " : "";
4109 const char *b_return = b->b_return ? "return " : "";
4110 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4111 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4112 if (b->b_instr) {
4113 int i;
4114 for (i = 0; i < b->b_iused; i++) {
4115 fprintf(stderr, " [%02d] ", i);
4116 dump_instr(b->b_instr + i);
4120 #endif
4122 static PyCodeObject *
4123 assemble(struct compiler *c, int addNone)
4125 basicblock *b, *entryblock;
4126 struct assembler a;
4127 int i, j, nblocks;
4128 PyCodeObject *co = NULL;
4130 /* Make sure every block that falls off the end returns None.
4131 XXX NEXT_BLOCK() isn't quite right, because if the last
4132 block ends with a jump or return b_next shouldn't set.
4134 if (!c->u->u_curblock->b_return) {
4135 NEXT_BLOCK(c);
4136 if (addNone)
4137 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4138 ADDOP(c, RETURN_VALUE);
4141 nblocks = 0;
4142 entryblock = NULL;
4143 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4144 nblocks++;
4145 entryblock = b;
4148 /* Set firstlineno if it wasn't explicitly set. */
4149 if (!c->u->u_firstlineno) {
4150 if (entryblock && entryblock->b_instr)
4151 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4152 else
4153 c->u->u_firstlineno = 1;
4155 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4156 goto error;
4157 dfs(c, entryblock, &a);
4159 /* Can't modify the bytecode after computing jump offsets. */
4160 assemble_jump_offsets(&a, c);
4162 /* Emit code in reverse postorder from dfs. */
4163 for (i = a.a_nblocks - 1; i >= 0; i--) {
4164 b = a.a_postorder[i];
4165 for (j = 0; j < b->b_iused; j++)
4166 if (!assemble_emit(&a, &b->b_instr[j]))
4167 goto error;
4170 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4171 goto error;
4172 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4173 goto error;
4175 co = makecode(c, &a);
4176 error:
4177 assemble_free(&a);
4178 return co;