Added a test for the ability to specify a class attribute in Formatter configuration...
[python.git] / Python / compile.c
blob0754ea508a7aedab93c6159447c06383b54f5c7b
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.
13 * Note that compiler_mod() suggests module, but the module ast type
14 * (mod_ty) has cases for expressions and interactive statements.
16 * CAUTION: The VISIT_* macros abort the current function when they encounter
17 * a problem. So don't invoke them when there is memory which needs to be
18 * released. Code blocks are OK, as the compiler structure takes care of
19 * releasing those.
22 #include "Python.h"
24 #include "Python-ast.h"
25 #include "node.h"
26 #include "pyarena.h"
27 #include "ast.h"
28 #include "code.h"
29 #include "compile.h"
30 #include "symtable.h"
31 #include "opcode.h"
33 int Py_OptimizeFlag = 0;
36 ISSUES:
38 character encodings aren't handled
40 ref leaks in interpreter when press return on empty line
42 opcode_stack_effect() function should be reviewed since stack depth bugs
43 could be really hard to find later.
45 Dead code is being generated (i.e. after unconditional jumps).
48 #define DEFAULT_BLOCK_SIZE 16
49 #define DEFAULT_BLOCKS 8
50 #define DEFAULT_CODE_SIZE 128
51 #define DEFAULT_LNOTAB_SIZE 16
53 struct instr {
54 unsigned i_jabs : 1;
55 unsigned i_jrel : 1;
56 unsigned i_hasarg : 1;
57 unsigned char i_opcode;
58 int i_oparg;
59 struct basicblock_ *i_target; /* target block (if jump instruction) */
60 int i_lineno;
63 typedef struct basicblock_ {
64 /* next block in the list of blocks for a unit (don't confuse with
65 * b_next) */
66 struct basicblock_ *b_list;
67 /* number of instructions used */
68 int b_iused;
69 /* length of instruction array (b_instr) */
70 int b_ialloc;
71 /* pointer to an array of instructions, initially NULL */
72 struct instr *b_instr;
73 /* If b_next is non-NULL, it is a pointer to the next
74 block reached by normal control flow. */
75 struct basicblock_ *b_next;
76 /* b_seen is used to perform a DFS of basicblocks. */
77 unsigned b_seen : 1;
78 /* b_return is true if a RETURN_VALUE opcode is inserted. */
79 unsigned b_return : 1;
80 /* depth of stack upon entry of block, computed by stackdepth() */
81 int b_startdepth;
82 /* instruction offset for block, computed by assemble_jump_offsets() */
83 int b_offset;
84 } basicblock;
86 /* fblockinfo tracks the current frame block.
88 A frame block is used to handle loops, try/except, and try/finally.
89 It's called a frame block to distinguish it from a basic block in the
90 compiler IR.
93 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
95 struct fblockinfo {
96 enum fblocktype fb_type;
97 basicblock *fb_block;
100 /* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block.
103 struct compiler_unit {
104 PySTEntryObject *u_ste;
106 PyObject *u_name;
107 /* The following fields are dicts that map objects to
108 the index of them in co_XXX. The index is used as
109 the argument for opcodes that refer to those collections.
111 PyObject *u_consts; /* all constants */
112 PyObject *u_names; /* all names */
113 PyObject *u_varnames; /* local variables */
114 PyObject *u_cellvars; /* cell variables */
115 PyObject *u_freevars; /* free variables */
117 PyObject *u_private; /* for private name mangling */
119 int u_argcount; /* number of arguments for block */
120 basicblock *u_blocks; /* pointer to list of blocks */
121 basicblock *u_curblock; /* pointer to current block */
122 int u_tmpname; /* temporary variables for list comps */
124 int u_nfblocks;
125 struct fblockinfo u_fblock[CO_MAXBLOCKS];
127 int u_firstlineno; /* the first lineno of the block */
128 int u_lineno; /* the lineno for the current stmt */
129 bool u_lineno_set; /* boolean to indicate whether instr
130 has been generated with current lineno */
133 /* This struct captures the global state of a compilation.
135 The u pointer points to the current compilation unit, while units
136 for enclosing blocks are stored in c_stack. The u and c_stack are
137 managed by compiler_enter_scope() and compiler_exit_scope().
140 struct compiler {
141 const char *c_filename;
142 struct symtable *c_st;
143 PyFutureFeatures *c_future; /* pointer to module's __future__ */
144 PyCompilerFlags *c_flags;
146 int c_interactive;
147 int c_nestlevel;
149 struct compiler_unit *u; /* compiler state for current block */
150 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
151 char *c_encoding; /* source encoding (a borrowed reference) */
152 PyArena *c_arena; /* pointer to memory allocation arena */
155 struct assembler {
156 PyObject *a_bytecode; /* string containing bytecode */
157 int a_offset; /* offset into bytecode */
158 int a_nblocks; /* number of reachable blocks */
159 basicblock **a_postorder; /* list of blocks in dfs postorder */
160 PyObject *a_lnotab; /* string containing lnotab */
161 int a_lnotab_off; /* offset into lnotab */
162 int a_lineno; /* last lineno of emitted instruction */
163 int a_lineno_off; /* bytecode offset of last lineno */
166 static int compiler_enter_scope(struct compiler *, identifier, void *, int);
167 static void compiler_free(struct compiler *);
168 static basicblock *compiler_new_block(struct compiler *);
169 static int compiler_next_instr(struct compiler *, basicblock *);
170 static int compiler_addop(struct compiler *, int);
171 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
172 static int compiler_addop_i(struct compiler *, int, int);
173 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
174 static basicblock *compiler_use_new_block(struct compiler *);
175 static int compiler_error(struct compiler *, const char *);
176 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
178 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
179 static int compiler_visit_stmt(struct compiler *, stmt_ty);
180 static int compiler_visit_keyword(struct compiler *, keyword_ty);
181 static int compiler_visit_expr(struct compiler *, expr_ty);
182 static int compiler_augassign(struct compiler *, stmt_ty);
183 static int compiler_visit_slice(struct compiler *, slice_ty,
184 expr_context_ty);
186 static int compiler_push_fblock(struct compiler *, enum fblocktype,
187 basicblock *);
188 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
189 basicblock *);
191 static int inplace_binop(struct compiler *, operator_ty);
192 static int expr_constant(expr_ty e);
194 static PyCodeObject *assemble(struct compiler *, int addNone);
195 static PyObject *__doc__;
197 PyObject *
198 _Py_Mangle(PyObject *private, PyObject *ident)
200 /* Name mangling: __private becomes _classname__private.
201 This is independent from how the name is used. */
202 const char *p, *name = PyString_AsString(ident);
203 char *buffer;
204 size_t nlen, plen;
205 if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
206 Py_INCREF(ident);
207 return ident;
209 p = PyString_AsString(private);
210 nlen = strlen(name);
211 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
212 Py_INCREF(ident);
213 return ident; /* Don't mangle __whatever__ */
215 /* Strip leading underscores from class name */
216 while (*p == '_')
217 p++;
218 if (*p == '\0') {
219 Py_INCREF(ident);
220 return ident; /* Don't mangle if class is just underscores */
222 plen = strlen(p);
223 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
224 if (!ident)
225 return 0;
226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
227 buffer = PyString_AS_STRING(ident);
228 buffer[0] = '_';
229 strncpy(buffer+1, p, plen);
230 strcpy(buffer+1+plen, name);
231 return ident;
234 static int
235 compiler_init(struct compiler *c)
237 memset(c, 0, sizeof(struct compiler));
239 c->c_stack = PyList_New(0);
240 if (!c->c_stack)
241 return 0;
243 return 1;
246 PyCodeObject *
247 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
248 PyArena *arena)
250 struct compiler c;
251 PyCodeObject *co = NULL;
252 PyCompilerFlags local_flags;
253 int merged;
255 if (!__doc__) {
256 __doc__ = PyString_InternFromString("__doc__");
257 if (!__doc__)
258 goto error;
261 if (!compiler_init(&c))
262 goto error;
263 c.c_filename = filename;
264 c.c_arena = arena;
265 c.c_future = PyFuture_FromAST(mod, filename);
266 if (c.c_future == NULL)
267 goto error;
268 if (!flags) {
269 local_flags.cf_flags = 0;
270 flags = &local_flags;
272 merged = c.c_future->ff_features | flags->cf_flags;
273 c.c_future->ff_features = merged;
274 flags->cf_flags = merged;
275 c.c_flags = flags;
276 c.c_nestlevel = 0;
278 c.c_st = PySymtable_Build(mod, filename, c.c_future);
279 if (c.c_st == NULL) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError, "no symtable");
282 goto error;
285 /* XXX initialize to NULL for now, need to handle */
286 c.c_encoding = NULL;
288 co = compiler_mod(&c, mod);
290 error:
291 compiler_free(&c);
292 return co;
295 PyCodeObject *
296 PyNode_Compile(struct _node *n, const char *filename)
298 PyCodeObject *co = NULL;
299 PyArena *arena = PyArena_New();
300 mod_ty mod = PyAST_FromNode(n, NULL, filename, arena);
301 if (mod)
302 co = PyAST_Compile(mod, filename, NULL, arena);
303 PyArena_Free(arena);
304 return co;
307 static void
308 compiler_free(struct compiler *c)
310 if (c->c_st)
311 PySymtable_Free(c->c_st);
312 if (c->c_future)
313 PyMem_Free(c->c_future);
314 Py_DECREF(c->c_stack);
317 static PyObject *
318 list2dict(PyObject *list)
320 int i, n;
321 PyObject *v, *k, *dict = PyDict_New();
323 n = PyList_Size(list);
324 for (i = 0; i < n; i++) {
325 v = PyInt_FromLong(i);
326 if (!v) {
327 Py_DECREF(dict);
328 return NULL;
330 k = PyList_GET_ITEM(list, i);
331 k = Py_BuildValue("(OO)", k, k->ob_type);
332 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
333 Py_XDECREF(k);
334 Py_DECREF(v);
335 Py_DECREF(dict);
336 return NULL;
338 Py_DECREF(k);
339 Py_DECREF(v);
341 return dict;
344 /* Return new dict containing names from src that match scope(s).
346 src is a symbol table dictionary. If the scope of a name matches
347 either scope_type or flag is set, insert it into the new dict. The
348 values are integers, starting at offset and increasing by one for
349 each key.
352 static PyObject *
353 dictbytype(PyObject *src, int scope_type, int flag, int offset)
355 int pos = 0, i = offset, scope;
356 PyObject *k, *v, *dest = PyDict_New();
358 assert(offset >= 0);
359 if (dest == NULL)
360 return NULL;
362 while (PyDict_Next(src, &pos, &k, &v)) {
363 /* XXX this should probably be a macro in symtable.h */
364 assert(PyInt_Check(v));
365 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
367 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
368 PyObject *tuple, *item = PyInt_FromLong(i);
369 if (item == NULL) {
370 Py_DECREF(dest);
371 return NULL;
373 i++;
374 tuple = Py_BuildValue("(OO)", k, k->ob_type);
375 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
376 Py_DECREF(item);
377 Py_DECREF(dest);
378 Py_XDECREF(tuple);
379 return NULL;
381 Py_DECREF(item);
382 Py_DECREF(tuple);
385 return dest;
388 /* Begin: Peephole optimizations ----------------------------------------- */
390 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
391 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
392 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
393 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
394 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
395 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
396 #define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
398 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
399 with LOAD_CONST (c1, c2, ... cn).
400 The consts table must still be in list form so that the
401 new constant (c1, c2, ... cn) can be appended.
402 Called with codestr pointing to the first LOAD_CONST.
403 Bails out with no change if one or more of the LOAD_CONSTs is missing.
404 Also works for BUILD_LIST when followed by an "in" or "not in" test.
406 static int
407 tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
409 PyObject *newconst, *constant;
410 int i, arg, len_consts;
412 /* Pre-conditions */
413 assert(PyList_CheckExact(consts));
414 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
415 assert(GETARG(codestr, (n*3)) == n);
416 for (i=0 ; i<n ; i++)
417 assert(codestr[i*3] == LOAD_CONST);
419 /* Buildup new tuple of constants */
420 newconst = PyTuple_New(n);
421 if (newconst == NULL)
422 return 0;
423 len_consts = PyList_GET_SIZE(consts);
424 for (i=0 ; i<n ; i++) {
425 arg = GETARG(codestr, (i*3));
426 assert(arg < len_consts);
427 constant = PyList_GET_ITEM(consts, arg);
428 Py_INCREF(constant);
429 PyTuple_SET_ITEM(newconst, i, constant);
432 /* Append folded constant onto consts */
433 if (PyList_Append(consts, newconst)) {
434 Py_DECREF(newconst);
435 return 0;
437 Py_DECREF(newconst);
439 /* Write NOPs over old LOAD_CONSTS and
440 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
441 memset(codestr, NOP, n*3);
442 codestr[n*3] = LOAD_CONST;
443 SETARG(codestr, (n*3), len_consts);
444 return 1;
447 /* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
448 with LOAD_CONST binop(c1,c2)
449 The consts table must still be in list form so that the
450 new constant can be appended.
451 Called with codestr pointing to the first LOAD_CONST.
452 Abandons the transformation if the folding fails (i.e. 1+'a').
453 If the new constant is a sequence, only folds when the size
454 is below a threshold value. That keeps pyc files from
455 becoming large in the presence of code like: (None,)*1000.
457 static int
458 fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
460 PyObject *newconst, *v, *w;
461 int len_consts, opcode, size;
463 /* Pre-conditions */
464 assert(PyList_CheckExact(consts));
465 assert(codestr[0] == LOAD_CONST);
466 assert(codestr[3] == LOAD_CONST);
468 /* Create new constant */
469 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
470 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
471 opcode = codestr[6];
472 switch (opcode) {
473 case BINARY_POWER:
474 newconst = PyNumber_Power(v, w, Py_None);
475 break;
476 case BINARY_MULTIPLY:
477 newconst = PyNumber_Multiply(v, w);
478 break;
479 case BINARY_DIVIDE:
480 /* Cannot fold this operation statically since
481 the result can depend on the run-time presence of the -Qnew flag */
482 return 0;
483 case BINARY_TRUE_DIVIDE:
484 newconst = PyNumber_TrueDivide(v, w);
485 break;
486 case BINARY_FLOOR_DIVIDE:
487 newconst = PyNumber_FloorDivide(v, w);
488 break;
489 case BINARY_MODULO:
490 newconst = PyNumber_Remainder(v, w);
491 break;
492 case BINARY_ADD:
493 newconst = PyNumber_Add(v, w);
494 break;
495 case BINARY_SUBTRACT:
496 newconst = PyNumber_Subtract(v, w);
497 break;
498 case BINARY_SUBSCR:
499 newconst = PyObject_GetItem(v, w);
500 break;
501 case BINARY_LSHIFT:
502 newconst = PyNumber_Lshift(v, w);
503 break;
504 case BINARY_RSHIFT:
505 newconst = PyNumber_Rshift(v, w);
506 break;
507 case BINARY_AND:
508 newconst = PyNumber_And(v, w);
509 break;
510 case BINARY_XOR:
511 newconst = PyNumber_Xor(v, w);
512 break;
513 case BINARY_OR:
514 newconst = PyNumber_Or(v, w);
515 break;
516 default:
517 /* Called with an unknown opcode */
518 PyErr_Format(PyExc_SystemError,
519 "unexpected binary operation %d on a constant",
520 opcode);
521 return 0;
523 if (newconst == NULL) {
524 PyErr_Clear();
525 return 0;
527 size = PyObject_Size(newconst);
528 if (size == -1)
529 PyErr_Clear();
530 else if (size > 20) {
531 Py_DECREF(newconst);
532 return 0;
535 /* Append folded constant into consts table */
536 len_consts = PyList_GET_SIZE(consts);
537 if (PyList_Append(consts, newconst)) {
538 Py_DECREF(newconst);
539 return 0;
541 Py_DECREF(newconst);
543 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
544 memset(codestr, NOP, 4);
545 codestr[4] = LOAD_CONST;
546 SETARG(codestr, 4, len_consts);
547 return 1;
550 static int
551 fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
553 PyObject *newconst=NULL, *v;
554 int len_consts, opcode;
556 /* Pre-conditions */
557 assert(PyList_CheckExact(consts));
558 assert(codestr[0] == LOAD_CONST);
560 /* Create new constant */
561 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
562 opcode = codestr[3];
563 switch (opcode) {
564 case UNARY_NEGATIVE:
565 /* Preserve the sign of -0.0 */
566 if (PyObject_IsTrue(v) == 1)
567 newconst = PyNumber_Negative(v);
568 break;
569 case UNARY_CONVERT:
570 newconst = PyObject_Repr(v);
571 break;
572 case UNARY_INVERT:
573 newconst = PyNumber_Invert(v);
574 break;
575 default:
576 /* Called with an unknown opcode */
577 PyErr_Format(PyExc_SystemError,
578 "unexpected unary operation %d on a constant",
579 opcode);
580 return 0;
582 if (newconst == NULL) {
583 PyErr_Clear();
584 return 0;
587 /* Append folded constant into consts table */
588 len_consts = PyList_GET_SIZE(consts);
589 if (PyList_Append(consts, newconst)) {
590 Py_DECREF(newconst);
591 return 0;
593 Py_DECREF(newconst);
595 /* Write NOP LOAD_CONST newconst */
596 codestr[0] = NOP;
597 codestr[1] = LOAD_CONST;
598 SETARG(codestr, 1, len_consts);
599 return 1;
602 static unsigned int *
603 markblocks(unsigned char *code, int len)
605 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
606 int i,j, opcode, blockcnt = 0;
608 if (blocks == NULL)
609 return NULL;
610 memset(blocks, 0, len*sizeof(int));
612 /* Mark labels in the first pass */
613 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
614 opcode = code[i];
615 switch (opcode) {
616 case FOR_ITER:
617 case JUMP_FORWARD:
618 case JUMP_IF_FALSE:
619 case JUMP_IF_TRUE:
620 case JUMP_ABSOLUTE:
621 case CONTINUE_LOOP:
622 case SETUP_LOOP:
623 case SETUP_EXCEPT:
624 case SETUP_FINALLY:
625 j = GETJUMPTGT(code, i);
626 blocks[j] = 1;
627 break;
630 /* Build block numbers in the second pass */
631 for (i=0 ; i<len ; i++) {
632 blockcnt += blocks[i]; /* increment blockcnt over labels */
633 blocks[i] = blockcnt;
635 return blocks;
638 /* Perform basic peephole optimizations to components of a code object.
639 The consts object should still be in list form to allow new constants
640 to be appended.
642 To keep the optimizer simple, it bails out (does nothing) for code
643 containing extended arguments or that has a length over 32,700. That
644 allows us to avoid overflow and sign issues. Likewise, it bails when
645 the lineno table has complex encoding for gaps >= 255.
647 Optimizations are restricted to simple transformations occuring within a
648 single basic block. All transformations keep the code size the same or
649 smaller. For those that reduce size, the gaps are initially filled with
650 NOPs. Later those NOPs are removed and the jump addresses retargeted in
651 a single pass. Line numbering is adjusted accordingly. */
653 static PyObject *
654 optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
656 int i, j, codelen, nops, h, adj;
657 int tgt, tgttgt, opcode;
658 unsigned char *codestr = NULL;
659 unsigned char *lineno;
660 int *addrmap = NULL;
661 int new_line, cum_orig_line, last_line, tabsiz;
662 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
663 unsigned int *blocks = NULL;
664 char *name;
666 /* Bail out if an exception is set */
667 if (PyErr_Occurred())
668 goto exitUnchanged;
670 /* Bypass optimization when the lineno table is too complex */
671 assert(PyString_Check(lineno_obj));
672 lineno = (unsigned char*)PyString_AS_STRING(lineno_obj);
673 tabsiz = PyString_GET_SIZE(lineno_obj);
674 if (memchr(lineno, 255, tabsiz) != NULL)
675 goto exitUnchanged;
677 /* Avoid situations where jump retargeting could overflow */
678 assert(PyString_Check(code));
679 codelen = PyString_Size(code);
680 if (codelen > 32700)
681 goto exitUnchanged;
683 /* Make a modifiable copy of the code string */
684 codestr = PyMem_Malloc(codelen);
685 if (codestr == NULL)
686 goto exitUnchanged;
687 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
689 /* Verify that RETURN_VALUE terminates the codestring. This allows
690 the various transformation patterns to look ahead several
691 instructions without additional checks to make sure they are not
692 looking beyond the end of the code string.
694 if (codestr[codelen-1] != RETURN_VALUE)
695 goto exitUnchanged;
697 /* Mapping to new jump targets after NOPs are removed */
698 addrmap = PyMem_Malloc(codelen * sizeof(int));
699 if (addrmap == NULL)
700 goto exitUnchanged;
702 blocks = markblocks(codestr, codelen);
703 if (blocks == NULL)
704 goto exitUnchanged;
705 assert(PyList_Check(consts));
707 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
708 opcode = codestr[i];
710 lastlc = cumlc;
711 cumlc = 0;
713 switch (opcode) {
715 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
716 with JUMP_IF_TRUE POP_TOP */
717 case UNARY_NOT:
718 if (codestr[i+1] != JUMP_IF_FALSE ||
719 codestr[i+4] != POP_TOP ||
720 !ISBASICBLOCK(blocks,i,5))
721 continue;
722 tgt = GETJUMPTGT(codestr, (i+1));
723 if (codestr[tgt] != POP_TOP)
724 continue;
725 j = GETARG(codestr, i+1) + 1;
726 codestr[i] = JUMP_IF_TRUE;
727 SETARG(codestr, i, j);
728 codestr[i+3] = POP_TOP;
729 codestr[i+4] = NOP;
730 break;
732 /* not a is b --> a is not b
733 not a in b --> a not in b
734 not a is not b --> a is b
735 not a not in b --> a in b
737 case COMPARE_OP:
738 j = GETARG(codestr, i);
739 if (j < 6 || j > 9 ||
740 codestr[i+3] != UNARY_NOT ||
741 !ISBASICBLOCK(blocks,i,4))
742 continue;
743 SETARG(codestr, i, (j^1));
744 codestr[i+3] = NOP;
745 break;
747 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
748 case LOAD_NAME:
749 case LOAD_GLOBAL:
750 j = GETARG(codestr, i);
751 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
752 if (name == NULL || strcmp(name, "None") != 0)
753 continue;
754 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
755 if (PyList_GET_ITEM(consts, j) == Py_None) {
756 codestr[i] = LOAD_CONST;
757 SETARG(codestr, i, j);
758 cumlc = lastlc + 1;
759 break;
762 break;
764 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
765 case LOAD_CONST:
766 cumlc = lastlc + 1;
767 j = GETARG(codestr, i);
768 if (codestr[i+3] != JUMP_IF_FALSE ||
769 codestr[i+6] != POP_TOP ||
770 !ISBASICBLOCK(blocks,i,7) ||
771 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
772 continue;
773 memset(codestr+i, NOP, 7);
774 cumlc = 0;
775 break;
777 /* Try to fold tuples of constants (includes a case for lists
778 which are only used for "in" and "not in" tests).
779 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
780 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
781 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
782 case BUILD_TUPLE:
783 case BUILD_LIST:
784 j = GETARG(codestr, i);
785 h = i - 3 * j;
786 if (h >= 0 &&
787 j <= lastlc &&
788 ((opcode == BUILD_TUPLE &&
789 ISBASICBLOCK(blocks, h, 3*(j+1))) ||
790 (opcode == BUILD_LIST &&
791 codestr[i+3]==COMPARE_OP &&
792 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
793 (GETARG(codestr,i+3)==6 ||
794 GETARG(codestr,i+3)==7))) &&
795 tuple_of_constants(&codestr[h], j, consts)) {
796 assert(codestr[i] == LOAD_CONST);
797 cumlc = 1;
798 break;
800 if (codestr[i+3] != UNPACK_SEQUENCE ||
801 !ISBASICBLOCK(blocks,i,6) ||
802 j != GETARG(codestr, i+3))
803 continue;
804 if (j == 1) {
805 memset(codestr+i, NOP, 6);
806 } else if (j == 2) {
807 codestr[i] = ROT_TWO;
808 memset(codestr+i+1, NOP, 5);
809 } else if (j == 3) {
810 codestr[i] = ROT_THREE;
811 codestr[i+1] = ROT_TWO;
812 memset(codestr+i+2, NOP, 4);
814 break;
816 /* Fold binary ops on constants.
817 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
818 case BINARY_POWER:
819 case BINARY_MULTIPLY:
820 case BINARY_TRUE_DIVIDE:
821 case BINARY_FLOOR_DIVIDE:
822 case BINARY_MODULO:
823 case BINARY_ADD:
824 case BINARY_SUBTRACT:
825 case BINARY_SUBSCR:
826 case BINARY_LSHIFT:
827 case BINARY_RSHIFT:
828 case BINARY_AND:
829 case BINARY_XOR:
830 case BINARY_OR:
831 if (lastlc >= 2 &&
832 ISBASICBLOCK(blocks, i-6, 7) &&
833 fold_binops_on_constants(&codestr[i-6], consts)) {
834 i -= 2;
835 assert(codestr[i] == LOAD_CONST);
836 cumlc = 1;
838 break;
840 /* Fold unary ops on constants.
841 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
842 case UNARY_NEGATIVE:
843 case UNARY_CONVERT:
844 case UNARY_INVERT:
845 if (lastlc >= 1 &&
846 ISBASICBLOCK(blocks, i-3, 4) &&
847 fold_unaryops_on_constants(&codestr[i-3], consts)) {
848 i -= 2;
849 assert(codestr[i] == LOAD_CONST);
850 cumlc = 1;
852 break;
854 /* Simplify conditional jump to conditional jump where the
855 result of the first test implies the success of a similar
856 test or the failure of the opposite test.
857 Arises in code like:
858 "if a and b:"
859 "if a or b:"
860 "a and b or c"
861 "(a and b) and c"
862 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
863 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
864 where y+3 is the instruction following the second test.
866 case JUMP_IF_FALSE:
867 case JUMP_IF_TRUE:
868 tgt = GETJUMPTGT(codestr, i);
869 j = codestr[tgt];
870 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
871 if (j == opcode) {
872 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
873 SETARG(codestr, i, tgttgt);
874 } else {
875 tgt -= i;
876 SETARG(codestr, i, tgt);
878 break;
880 /* Intentional fallthrough */
882 /* Replace jumps to unconditional jumps */
883 case FOR_ITER:
884 case JUMP_FORWARD:
885 case JUMP_ABSOLUTE:
886 case CONTINUE_LOOP:
887 case SETUP_LOOP:
888 case SETUP_EXCEPT:
889 case SETUP_FINALLY:
890 tgt = GETJUMPTGT(codestr, i);
891 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
892 continue;
893 tgttgt = GETJUMPTGT(codestr, tgt);
894 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
895 opcode = JUMP_ABSOLUTE;
896 if (!ABSOLUTE_JUMP(opcode))
897 tgttgt -= i + 3; /* Calc relative jump addr */
898 if (tgttgt < 0) /* No backward relative jumps */
899 continue;
900 codestr[i] = opcode;
901 SETARG(codestr, i, tgttgt);
902 break;
904 case EXTENDED_ARG:
905 goto exitUnchanged;
907 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
908 case RETURN_VALUE:
909 if (i+4 >= codelen ||
910 codestr[i+4] != RETURN_VALUE ||
911 !ISBASICBLOCK(blocks,i,5))
912 continue;
913 memset(codestr+i+1, NOP, 4);
914 break;
918 /* Fixup linenotab */
919 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
920 addrmap[i] = i - nops;
921 if (codestr[i] == NOP)
922 nops++;
924 cum_orig_line = 0;
925 last_line = 0;
926 for (i=0 ; i < tabsiz ; i+=2) {
927 cum_orig_line += lineno[i];
928 new_line = addrmap[cum_orig_line];
929 assert (new_line - last_line < 255);
930 lineno[i] =((unsigned char)(new_line - last_line));
931 last_line = new_line;
934 /* Remove NOPs and fixup jump targets */
935 for (i=0, h=0 ; i<codelen ; ) {
936 opcode = codestr[i];
937 switch (opcode) {
938 case NOP:
939 i++;
940 continue;
942 case JUMP_ABSOLUTE:
943 case CONTINUE_LOOP:
944 j = addrmap[GETARG(codestr, i)];
945 SETARG(codestr, i, j);
946 break;
948 case FOR_ITER:
949 case JUMP_FORWARD:
950 case JUMP_IF_FALSE:
951 case JUMP_IF_TRUE:
952 case SETUP_LOOP:
953 case SETUP_EXCEPT:
954 case SETUP_FINALLY:
955 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
956 SETARG(codestr, i, j);
957 break;
959 adj = CODESIZE(opcode);
960 while (adj--)
961 codestr[h++] = codestr[i++];
963 assert(h + nops == codelen);
965 code = PyString_FromStringAndSize((char *)codestr, h);
966 PyMem_Free(addrmap);
967 PyMem_Free(codestr);
968 PyMem_Free(blocks);
969 return code;
971 exitUnchanged:
972 if (blocks != NULL)
973 PyMem_Free(blocks);
974 if (addrmap != NULL)
975 PyMem_Free(addrmap);
976 if (codestr != NULL)
977 PyMem_Free(codestr);
978 Py_INCREF(code);
979 return code;
982 /* End: Peephole optimizations ----------------------------------------- */
986 Leave this debugging code for just a little longer.
988 static void
989 compiler_display_symbols(PyObject *name, PyObject *symbols)
991 PyObject *key, *value;
992 int flags, pos = 0;
994 fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
995 while (PyDict_Next(symbols, &pos, &key, &value)) {
996 flags = PyInt_AsLong(value);
997 fprintf(stderr, "var %s:", PyString_AS_STRING(key));
998 if (flags & DEF_GLOBAL)
999 fprintf(stderr, " declared_global");
1000 if (flags & DEF_LOCAL)
1001 fprintf(stderr, " local");
1002 if (flags & DEF_PARAM)
1003 fprintf(stderr, " param");
1004 if (flags & DEF_STAR)
1005 fprintf(stderr, " stararg");
1006 if (flags & DEF_DOUBLESTAR)
1007 fprintf(stderr, " starstar");
1008 if (flags & DEF_INTUPLE)
1009 fprintf(stderr, " tuple");
1010 if (flags & DEF_FREE)
1011 fprintf(stderr, " free");
1012 if (flags & DEF_FREE_GLOBAL)
1013 fprintf(stderr, " global");
1014 if (flags & DEF_FREE_CLASS)
1015 fprintf(stderr, " free/class");
1016 if (flags & DEF_IMPORT)
1017 fprintf(stderr, " import");
1018 fprintf(stderr, "\n");
1020 fprintf(stderr, "\n");
1024 static void
1025 compiler_unit_check(struct compiler_unit *u)
1027 basicblock *block;
1028 for (block = u->u_blocks; block != NULL; block = block->b_list) {
1029 assert(block != (void *)0xcbcbcbcb);
1030 assert(block != (void *)0xfbfbfbfb);
1031 assert(block != (void *)0xdbdbdbdb);
1032 if (block->b_instr != NULL) {
1033 assert(block->b_ialloc > 0);
1034 assert(block->b_iused > 0);
1035 assert(block->b_ialloc >= block->b_iused);
1037 else {
1038 assert (block->b_iused == 0);
1039 assert (block->b_ialloc == 0);
1044 static void
1045 compiler_unit_free(struct compiler_unit *u)
1047 basicblock *b, *next;
1049 compiler_unit_check(u);
1050 b = u->u_blocks;
1051 while (b != NULL) {
1052 if (b->b_instr)
1053 PyObject_Free((void *)b->b_instr);
1054 next = b->b_list;
1055 PyObject_Free((void *)b);
1056 b = next;
1058 Py_XDECREF(u->u_ste);
1059 Py_XDECREF(u->u_name);
1060 Py_XDECREF(u->u_consts);
1061 Py_XDECREF(u->u_names);
1062 Py_XDECREF(u->u_varnames);
1063 Py_XDECREF(u->u_freevars);
1064 Py_XDECREF(u->u_cellvars);
1065 Py_XDECREF(u->u_private);
1066 PyObject_Free(u);
1069 static int
1070 compiler_enter_scope(struct compiler *c, identifier name, void *key,
1071 int lineno)
1073 struct compiler_unit *u;
1075 u = PyObject_Malloc(sizeof(struct compiler_unit));
1076 if (!u) {
1077 PyErr_NoMemory();
1078 return 0;
1080 memset(u, 0, sizeof(struct compiler_unit));
1081 u->u_argcount = 0;
1082 u->u_ste = PySymtable_Lookup(c->c_st, key);
1083 if (!u->u_ste) {
1084 compiler_unit_free(u);
1085 return 0;
1087 Py_INCREF(name);
1088 u->u_name = name;
1089 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1090 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1091 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1092 PyDict_Size(u->u_cellvars));
1094 u->u_blocks = NULL;
1095 u->u_tmpname = 0;
1096 u->u_nfblocks = 0;
1097 u->u_firstlineno = lineno;
1098 u->u_lineno = 0;
1099 u->u_lineno_set = false;
1100 u->u_consts = PyDict_New();
1101 if (!u->u_consts) {
1102 compiler_unit_free(u);
1103 return 0;
1105 u->u_names = PyDict_New();
1106 if (!u->u_names) {
1107 compiler_unit_free(u);
1108 return 0;
1111 u->u_private = NULL;
1113 /* Push the old compiler_unit on the stack. */
1114 if (c->u) {
1115 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
1116 if (PyList_Append(c->c_stack, wrapper) < 0) {
1117 compiler_unit_free(u);
1118 return 0;
1120 Py_DECREF(wrapper);
1121 u->u_private = c->u->u_private;
1122 Py_XINCREF(u->u_private);
1124 c->u = u;
1126 c->c_nestlevel++;
1127 if (compiler_use_new_block(c) == NULL)
1128 return 0;
1130 return 1;
1133 static void
1134 compiler_exit_scope(struct compiler *c)
1136 int n;
1137 PyObject *wrapper;
1139 c->c_nestlevel--;
1140 compiler_unit_free(c->u);
1141 /* Restore c->u to the parent unit. */
1142 n = PyList_GET_SIZE(c->c_stack) - 1;
1143 if (n >= 0) {
1144 wrapper = PyList_GET_ITEM(c->c_stack, n);
1145 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
1146 /* we are deleting from a list so this really shouldn't fail */
1147 if (PySequence_DelItem(c->c_stack, n) < 0)
1148 Py_FatalError("compiler_exit_scope()");
1149 compiler_unit_check(c->u);
1151 else
1152 c->u = NULL;
1156 /* Allocate a new block and return a pointer to it.
1157 Returns NULL on error.
1160 static basicblock *
1161 compiler_new_block(struct compiler *c)
1163 basicblock *b;
1164 struct compiler_unit *u;
1166 u = c->u;
1167 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
1168 if (b == NULL) {
1169 PyErr_NoMemory();
1170 return NULL;
1172 memset((void *)b, 0, sizeof(basicblock));
1173 assert (b->b_next == NULL);
1174 b->b_list = u->u_blocks;
1175 u->u_blocks = b;
1176 return b;
1179 static basicblock *
1180 compiler_use_new_block(struct compiler *c)
1182 basicblock *block = compiler_new_block(c);
1183 if (block == NULL)
1184 return NULL;
1185 c->u->u_curblock = block;
1186 return block;
1189 static basicblock *
1190 compiler_next_block(struct compiler *c)
1192 basicblock *block = compiler_new_block(c);
1193 if (block == NULL)
1194 return NULL;
1195 c->u->u_curblock->b_next = block;
1196 c->u->u_curblock = block;
1197 return block;
1200 static basicblock *
1201 compiler_use_next_block(struct compiler *c, basicblock *block)
1203 assert(block != NULL);
1204 c->u->u_curblock->b_next = block;
1205 c->u->u_curblock = block;
1206 return block;
1209 /* Returns the offset of the next instruction in the current block's
1210 b_instr array. Resizes the b_instr as necessary.
1211 Returns -1 on failure.
1214 static int
1215 compiler_next_instr(struct compiler *c, basicblock *b)
1217 assert(b != NULL);
1218 if (b->b_instr == NULL) {
1219 b->b_instr = PyObject_Malloc(sizeof(struct instr) *
1220 DEFAULT_BLOCK_SIZE);
1221 if (b->b_instr == NULL) {
1222 PyErr_NoMemory();
1223 return -1;
1225 b->b_ialloc = DEFAULT_BLOCK_SIZE;
1226 memset((char *)b->b_instr, 0,
1227 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
1229 else if (b->b_iused == b->b_ialloc) {
1230 size_t oldsize, newsize;
1231 oldsize = b->b_ialloc * sizeof(struct instr);
1232 newsize = oldsize << 1;
1233 if (newsize == 0) {
1234 PyErr_NoMemory();
1235 return -1;
1237 b->b_ialloc <<= 1;
1238 b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
1239 if (b->b_instr == NULL)
1240 return -1;
1241 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
1243 return b->b_iused++;
1246 static void
1247 compiler_set_lineno(struct compiler *c, int off)
1249 basicblock *b;
1250 if (c->u->u_lineno_set)
1251 return;
1252 c->u->u_lineno_set = true;
1253 b = c->u->u_curblock;
1254 b->b_instr[off].i_lineno = c->u->u_lineno;
1257 static int
1258 opcode_stack_effect(int opcode, int oparg)
1260 switch (opcode) {
1261 case POP_TOP:
1262 return -1;
1263 case ROT_TWO:
1264 case ROT_THREE:
1265 return 0;
1266 case DUP_TOP:
1267 return 1;
1268 case ROT_FOUR:
1269 return 0;
1271 case UNARY_POSITIVE:
1272 case UNARY_NEGATIVE:
1273 case UNARY_NOT:
1274 case UNARY_CONVERT:
1275 case UNARY_INVERT:
1276 return 0;
1278 case BINARY_POWER:
1279 case BINARY_MULTIPLY:
1280 case BINARY_DIVIDE:
1281 case BINARY_MODULO:
1282 case BINARY_ADD:
1283 case BINARY_SUBTRACT:
1284 case BINARY_SUBSCR:
1285 case BINARY_FLOOR_DIVIDE:
1286 case BINARY_TRUE_DIVIDE:
1287 return -1;
1288 case INPLACE_FLOOR_DIVIDE:
1289 case INPLACE_TRUE_DIVIDE:
1290 return -1;
1292 case SLICE+0:
1293 return 1;
1294 case SLICE+1:
1295 return 0;
1296 case SLICE+2:
1297 return 0;
1298 case SLICE+3:
1299 return -1;
1301 case STORE_SLICE+0:
1302 return -2;
1303 case STORE_SLICE+1:
1304 return -3;
1305 case STORE_SLICE+2:
1306 return -3;
1307 case STORE_SLICE+3:
1308 return -4;
1310 case DELETE_SLICE+0:
1311 return -1;
1312 case DELETE_SLICE+1:
1313 return -2;
1314 case DELETE_SLICE+2:
1315 return -2;
1316 case DELETE_SLICE+3:
1317 return -3;
1319 case INPLACE_ADD:
1320 case INPLACE_SUBTRACT:
1321 case INPLACE_MULTIPLY:
1322 case INPLACE_DIVIDE:
1323 case INPLACE_MODULO:
1324 return -1;
1325 case STORE_SUBSCR:
1326 return -3;
1327 case DELETE_SUBSCR:
1328 return -2;
1330 case BINARY_LSHIFT:
1331 case BINARY_RSHIFT:
1332 case BINARY_AND:
1333 case BINARY_XOR:
1334 case BINARY_OR:
1335 return -1;
1336 case INPLACE_POWER:
1337 return -1;
1338 case GET_ITER:
1339 return 0;
1341 case PRINT_EXPR:
1342 return -1;
1343 case PRINT_ITEM:
1344 return -1;
1345 case PRINT_NEWLINE:
1346 return 0;
1347 case PRINT_ITEM_TO:
1348 return -2;
1349 case PRINT_NEWLINE_TO:
1350 return -1;
1351 case INPLACE_LSHIFT:
1352 case INPLACE_RSHIFT:
1353 case INPLACE_AND:
1354 case INPLACE_XOR:
1355 case INPLACE_OR:
1356 return -1;
1357 case BREAK_LOOP:
1358 return 0;
1360 case LOAD_LOCALS:
1361 return 1;
1362 case RETURN_VALUE:
1363 return -1;
1364 case IMPORT_STAR:
1365 return -1;
1366 case EXEC_STMT:
1367 return -3;
1368 case YIELD_VALUE:
1369 return 0;
1371 case POP_BLOCK:
1372 return 0;
1373 case END_FINALLY:
1374 return -1; /* or -2 or -3 if exception occurred */
1375 case BUILD_CLASS:
1376 return -2;
1378 case STORE_NAME:
1379 return -1;
1380 case DELETE_NAME:
1381 return 0;
1382 case UNPACK_SEQUENCE:
1383 return oparg-1;
1384 case FOR_ITER:
1385 return 1;
1387 case STORE_ATTR:
1388 return -2;
1389 case DELETE_ATTR:
1390 return -1;
1391 case STORE_GLOBAL:
1392 return -1;
1393 case DELETE_GLOBAL:
1394 return 0;
1395 case DUP_TOPX:
1396 return oparg;
1397 case LOAD_CONST:
1398 return 1;
1399 case LOAD_NAME:
1400 return 1;
1401 case BUILD_TUPLE:
1402 case BUILD_LIST:
1403 return 1-oparg;
1404 case BUILD_MAP:
1405 return 1;
1406 case LOAD_ATTR:
1407 return 0;
1408 case COMPARE_OP:
1409 return -1;
1410 case IMPORT_NAME:
1411 return 0;
1412 case IMPORT_FROM:
1413 return 1;
1415 case JUMP_FORWARD:
1416 case JUMP_IF_FALSE:
1417 case JUMP_IF_TRUE:
1418 case JUMP_ABSOLUTE:
1419 return 0;
1421 case LOAD_GLOBAL:
1422 return 1;
1424 case CONTINUE_LOOP:
1425 return 0;
1426 case SETUP_LOOP:
1427 return 0;
1428 case SETUP_EXCEPT:
1429 case SETUP_FINALLY:
1430 return 3; /* actually pushed by an exception */
1432 case LOAD_FAST:
1433 return 1;
1434 case STORE_FAST:
1435 return -1;
1436 case DELETE_FAST:
1437 return 0;
1439 case RAISE_VARARGS:
1440 return -oparg;
1441 #define NARGS(o) (((o) % 256) + 2*((o) / 256))
1442 case CALL_FUNCTION:
1443 return -NARGS(oparg);
1444 case CALL_FUNCTION_VAR:
1445 case CALL_FUNCTION_KW:
1446 return -NARGS(oparg)-1;
1447 case CALL_FUNCTION_VAR_KW:
1448 return -NARGS(oparg)-2;
1449 #undef NARGS
1450 case MAKE_FUNCTION:
1451 return -oparg;
1452 case BUILD_SLICE:
1453 if (oparg == 3)
1454 return -2;
1455 else
1456 return -1;
1458 case MAKE_CLOSURE:
1459 return -oparg;
1460 case LOAD_CLOSURE:
1461 return 1;
1462 case LOAD_DEREF:
1463 return 1;
1464 case STORE_DEREF:
1465 return -1;
1466 default:
1467 fprintf(stderr, "opcode = %d\n", opcode);
1468 Py_FatalError("opcode_stack_effect()");
1471 return 0; /* not reachable */
1474 /* Add an opcode with no argument.
1475 Returns 0 on failure, 1 on success.
1478 static int
1479 compiler_addop(struct compiler *c, int opcode)
1481 basicblock *b;
1482 struct instr *i;
1483 int off;
1484 off = compiler_next_instr(c, c->u->u_curblock);
1485 if (off < 0)
1486 return 0;
1487 b = c->u->u_curblock;
1488 i = &b->b_instr[off];
1489 i->i_opcode = opcode;
1490 i->i_hasarg = 0;
1491 if (opcode == RETURN_VALUE)
1492 b->b_return = 1;
1493 compiler_set_lineno(c, off);
1494 return 1;
1497 static int
1498 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1500 PyObject *t, *v;
1501 int arg;
1503 /* necessary to make sure types aren't coerced (e.g., int and long) */
1504 t = PyTuple_Pack(2, o, o->ob_type);
1505 if (t == NULL)
1506 return -1;
1508 v = PyDict_GetItem(dict, t);
1509 if (!v) {
1510 arg = PyDict_Size(dict);
1511 v = PyInt_FromLong(arg);
1512 if (!v) {
1513 Py_DECREF(t);
1514 return -1;
1516 if (PyDict_SetItem(dict, t, v) < 0) {
1517 Py_DECREF(t);
1518 Py_DECREF(v);
1519 return -1;
1521 Py_DECREF(v);
1523 else
1524 arg = PyInt_AsLong(v);
1525 Py_DECREF(t);
1526 return arg;
1529 static int
1530 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1531 PyObject *o)
1533 int arg = compiler_add_o(c, dict, o);
1534 if (arg < 0)
1535 return 0;
1536 return compiler_addop_i(c, opcode, arg);
1539 static int
1540 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1541 PyObject *o)
1543 int arg;
1544 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1545 if (!mangled)
1546 return 0;
1547 arg = compiler_add_o(c, dict, mangled);
1548 Py_DECREF(mangled);
1549 if (arg < 0)
1550 return 0;
1551 return compiler_addop_i(c, opcode, arg);
1554 /* Add an opcode with an integer argument.
1555 Returns 0 on failure, 1 on success.
1558 static int
1559 compiler_addop_i(struct compiler *c, int opcode, int oparg)
1561 struct instr *i;
1562 int off;
1563 off = compiler_next_instr(c, c->u->u_curblock);
1564 if (off < 0)
1565 return 0;
1566 i = &c->u->u_curblock->b_instr[off];
1567 i->i_opcode = opcode;
1568 i->i_oparg = oparg;
1569 i->i_hasarg = 1;
1570 compiler_set_lineno(c, off);
1571 return 1;
1574 static int
1575 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1577 struct instr *i;
1578 int off;
1580 assert(b != NULL);
1581 off = compiler_next_instr(c, c->u->u_curblock);
1582 if (off < 0)
1583 return 0;
1584 compiler_set_lineno(c, off);
1585 i = &c->u->u_curblock->b_instr[off];
1586 i->i_opcode = opcode;
1587 i->i_target = b;
1588 i->i_hasarg = 1;
1589 if (absolute)
1590 i->i_jabs = 1;
1591 else
1592 i->i_jrel = 1;
1593 return 1;
1596 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1597 like to find better names.) NEW_BLOCK() creates a new block and sets
1598 it as the current block. NEXT_BLOCK() also creates an implicit jump
1599 from the current block to the new block.
1602 /* XXX The returns inside these macros make it impossible to decref
1603 objects created in the local function.
1607 #define NEW_BLOCK(C) { \
1608 if (compiler_use_new_block((C)) == NULL) \
1609 return 0; \
1612 #define NEXT_BLOCK(C) { \
1613 if (compiler_next_block((C)) == NULL) \
1614 return 0; \
1617 #define ADDOP(C, OP) { \
1618 if (!compiler_addop((C), (OP))) \
1619 return 0; \
1622 #define ADDOP_IN_SCOPE(C, OP) { \
1623 if (!compiler_addop((C), (OP))) { \
1624 compiler_exit_scope(c); \
1625 return 0; \
1629 #define ADDOP_O(C, OP, O, TYPE) { \
1630 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1631 return 0; \
1634 #define ADDOP_NAME(C, OP, O, TYPE) { \
1635 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1636 return 0; \
1639 #define ADDOP_I(C, OP, O) { \
1640 if (!compiler_addop_i((C), (OP), (O))) \
1641 return 0; \
1644 #define ADDOP_JABS(C, OP, O) { \
1645 if (!compiler_addop_j((C), (OP), (O), 1)) \
1646 return 0; \
1649 #define ADDOP_JREL(C, OP, O) { \
1650 if (!compiler_addop_j((C), (OP), (O), 0)) \
1651 return 0; \
1654 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1655 the ASDL name to synthesize the name of the C type and the visit function.
1658 #define VISIT(C, TYPE, V) {\
1659 if (!compiler_visit_ ## TYPE((C), (V))) \
1660 return 0; \
1663 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1664 if (!compiler_visit_ ## TYPE((C), (V))) { \
1665 compiler_exit_scope(c); \
1666 return 0; \
1670 #define VISIT_SLICE(C, V, CTX) {\
1671 if (!compiler_visit_slice((C), (V), (CTX))) \
1672 return 0; \
1675 #define VISIT_SEQ(C, TYPE, SEQ) { \
1676 int _i; \
1677 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1678 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1679 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
1680 if (!compiler_visit_ ## TYPE((C), elt)) \
1681 return 0; \
1685 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1686 int _i; \
1687 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1688 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1689 TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
1690 if (!compiler_visit_ ## TYPE((C), elt)) { \
1691 compiler_exit_scope(c); \
1692 return 0; \
1697 static int
1698 compiler_isdocstring(stmt_ty s)
1700 if (s->kind != Expr_kind)
1701 return 0;
1702 return s->v.Expr.value->kind == Str_kind;
1705 /* Compile a sequence of statements, checking for a docstring. */
1707 static int
1708 compiler_body(struct compiler *c, asdl_seq *stmts)
1710 int i = 0;
1711 stmt_ty st;
1713 if (!asdl_seq_LEN(stmts))
1714 return 1;
1715 st = asdl_seq_GET(stmts, 0);
1716 if (compiler_isdocstring(st)) {
1717 i = 1;
1718 VISIT(c, expr, st->v.Expr.value);
1719 if (!compiler_nameop(c, __doc__, Store))
1720 return 0;
1722 for (; i < asdl_seq_LEN(stmts); i++)
1723 VISIT(c, stmt, asdl_seq_GET(stmts, i));
1724 return 1;
1727 static PyCodeObject *
1728 compiler_mod(struct compiler *c, mod_ty mod)
1730 PyCodeObject *co;
1731 int addNone = 1;
1732 static PyObject *module;
1733 if (!module) {
1734 module = PyString_FromString("<module>");
1735 if (!module)
1736 return NULL;
1738 if (!compiler_enter_scope(c, module, mod, 1))
1739 return NULL;
1740 switch (mod->kind) {
1741 case Module_kind:
1742 if (!compiler_body(c, mod->v.Module.body)) {
1743 compiler_exit_scope(c);
1744 return 0;
1746 break;
1747 case Interactive_kind:
1748 c->c_interactive = 1;
1749 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
1750 break;
1751 case Expression_kind:
1752 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1753 addNone = 0;
1754 break;
1755 case Suite_kind:
1756 PyErr_SetString(PyExc_SystemError,
1757 "suite should not be possible");
1758 return 0;
1759 default:
1760 PyErr_Format(PyExc_SystemError,
1761 "module kind %d should not be possible",
1762 mod->kind);
1763 return 0;
1765 co = assemble(c, addNone);
1766 compiler_exit_scope(c);
1767 return co;
1770 /* The test for LOCAL must come before the test for FREE in order to
1771 handle classes where name is both local and free. The local var is
1772 a method and the free var is a free var referenced within a method.
1775 static int
1776 get_ref_type(struct compiler *c, PyObject *name)
1778 int scope = PyST_GetScope(c->u->u_ste, name);
1779 if (scope == 0) {
1780 char buf[350];
1781 PyOS_snprintf(buf, sizeof(buf),
1782 "unknown scope for %.100s in %.100s(%s) in %s\n"
1783 "symbols: %s\nlocals: %s\nglobals: %s\n",
1784 PyString_AS_STRING(name),
1785 PyString_AS_STRING(c->u->u_name),
1786 PyObject_REPR(c->u->u_ste->ste_id),
1787 c->c_filename,
1788 PyObject_REPR(c->u->u_ste->ste_symbols),
1789 PyObject_REPR(c->u->u_varnames),
1790 PyObject_REPR(c->u->u_names)
1792 Py_FatalError(buf);
1795 return scope;
1798 static int
1799 compiler_lookup_arg(PyObject *dict, PyObject *name)
1801 PyObject *k, *v;
1802 k = Py_BuildValue("(OO)", name, name->ob_type);
1803 if (k == NULL)
1804 return -1;
1805 v = PyDict_GetItem(dict, k);
1806 Py_DECREF(k);
1807 if (v == NULL)
1808 return -1;
1809 return PyInt_AS_LONG(v);
1812 static int
1813 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1815 int i, free = PyCode_GetNumFree(co);
1816 if (free == 0) {
1817 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1818 ADDOP_I(c, MAKE_FUNCTION, args);
1819 return 1;
1821 for (i = 0; i < free; ++i) {
1822 /* Bypass com_addop_varname because it will generate
1823 LOAD_DEREF but LOAD_CLOSURE is needed.
1825 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1826 int arg, reftype;
1828 /* Special case: If a class contains a method with a
1829 free variable that has the same name as a method,
1830 the name will be considered free *and* local in the
1831 class. It should be handled by the closure, as
1832 well as by the normal name loookup logic.
1834 reftype = get_ref_type(c, name);
1835 if (reftype == CELL)
1836 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1837 else /* (reftype == FREE) */
1838 arg = compiler_lookup_arg(c->u->u_freevars, name);
1839 if (arg == -1) {
1840 printf("lookup %s in %s %d %d\n"
1841 "freevars of %s: %s\n",
1842 PyObject_REPR(name),
1843 PyString_AS_STRING(c->u->u_name),
1844 reftype, arg,
1845 PyString_AS_STRING(co->co_name),
1846 PyObject_REPR(co->co_freevars));
1847 Py_FatalError("compiler_make_closure()");
1849 ADDOP_I(c, LOAD_CLOSURE, arg);
1851 ADDOP_I(c, BUILD_TUPLE, free);
1852 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1853 ADDOP_I(c, MAKE_CLOSURE, args);
1854 return 1;
1857 static int
1858 compiler_decorators(struct compiler *c, asdl_seq* decos)
1860 int i;
1862 if (!decos)
1863 return 1;
1865 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1866 VISIT(c, expr, asdl_seq_GET(decos, i));
1868 return 1;
1871 static int
1872 compiler_arguments(struct compiler *c, arguments_ty args)
1874 int i;
1875 int n = asdl_seq_LEN(args->args);
1876 /* Correctly handle nested argument lists */
1877 for (i = 0; i < n; i++) {
1878 expr_ty arg = asdl_seq_GET(args->args, i);
1879 if (arg->kind == Tuple_kind) {
1880 PyObject *id = PyString_FromFormat(".%d", i);
1881 if (id == NULL) {
1882 return 0;
1884 if (!compiler_nameop(c, id, Load)) {
1885 Py_DECREF(id);
1886 return 0;
1888 Py_DECREF(id);
1889 VISIT(c, expr, arg);
1892 return 1;
1895 static int
1896 compiler_function(struct compiler *c, stmt_ty s)
1898 PyCodeObject *co;
1899 PyObject *first_const = Py_None;
1900 arguments_ty args = s->v.FunctionDef.args;
1901 asdl_seq* decos = s->v.FunctionDef.decorators;
1902 stmt_ty st;
1903 int i, n, docstring;
1905 assert(s->kind == FunctionDef_kind);
1907 if (!compiler_decorators(c, decos))
1908 return 0;
1909 if (args->defaults)
1910 VISIT_SEQ(c, expr, args->defaults);
1911 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1912 s->lineno))
1913 return 0;
1915 st = asdl_seq_GET(s->v.FunctionDef.body, 0);
1916 docstring = compiler_isdocstring(st);
1917 if (docstring)
1918 first_const = st->v.Expr.value->v.Str.s;
1919 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1920 compiler_exit_scope(c);
1921 return 0;
1924 /* unpack nested arguments */
1925 compiler_arguments(c, args);
1927 c->u->u_argcount = asdl_seq_LEN(args->args);
1928 n = asdl_seq_LEN(s->v.FunctionDef.body);
1929 /* if there was a docstring, we need to skip the first statement */
1930 for (i = docstring; i < n; i++) {
1931 stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
1932 if (i == 0 && s2->kind == Expr_kind &&
1933 s2->v.Expr.value->kind == Str_kind)
1934 continue;
1935 VISIT_IN_SCOPE(c, stmt, s2);
1937 co = assemble(c, 1);
1938 compiler_exit_scope(c);
1939 if (co == NULL)
1940 return 0;
1942 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1943 Py_DECREF(co);
1945 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1946 ADDOP_I(c, CALL_FUNCTION, 1);
1949 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1952 static int
1953 compiler_class(struct compiler *c, stmt_ty s)
1955 int n;
1956 PyCodeObject *co;
1957 PyObject *str;
1958 /* push class name on stack, needed by BUILD_CLASS */
1959 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1960 /* push the tuple of base classes on the stack */
1961 n = asdl_seq_LEN(s->v.ClassDef.bases);
1962 if (n > 0)
1963 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1964 ADDOP_I(c, BUILD_TUPLE, n);
1965 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1966 s->lineno))
1967 return 0;
1968 c->u->u_private = s->v.ClassDef.name;
1969 Py_INCREF(c->u->u_private);
1970 str = PyString_InternFromString("__name__");
1971 if (!str || !compiler_nameop(c, str, Load)) {
1972 Py_XDECREF(str);
1973 compiler_exit_scope(c);
1974 return 0;
1977 Py_DECREF(str);
1978 str = PyString_InternFromString("__module__");
1979 if (!str || !compiler_nameop(c, str, Store)) {
1980 Py_XDECREF(str);
1981 compiler_exit_scope(c);
1982 return 0;
1984 Py_DECREF(str);
1986 if (!compiler_body(c, s->v.ClassDef.body)) {
1987 compiler_exit_scope(c);
1988 return 0;
1991 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1992 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1993 co = assemble(c, 1);
1994 compiler_exit_scope(c);
1995 if (co == NULL)
1996 return 0;
1998 compiler_make_closure(c, co, 0);
1999 Py_DECREF(co);
2001 ADDOP_I(c, CALL_FUNCTION, 0);
2002 ADDOP(c, BUILD_CLASS);
2003 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2004 return 0;
2005 return 1;
2008 static int
2009 compiler_lambda(struct compiler *c, expr_ty e)
2011 PyCodeObject *co;
2012 static identifier name;
2013 arguments_ty args = e->v.Lambda.args;
2014 assert(e->kind == Lambda_kind);
2016 if (!name) {
2017 name = PyString_InternFromString("<lambda>");
2018 if (!name)
2019 return 0;
2022 if (args->defaults)
2023 VISIT_SEQ(c, expr, args->defaults);
2024 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2025 return 0;
2027 /* unpack nested arguments */
2028 compiler_arguments(c, args);
2030 c->u->u_argcount = asdl_seq_LEN(args->args);
2031 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2032 ADDOP_IN_SCOPE(c, RETURN_VALUE);
2033 co = assemble(c, 1);
2034 compiler_exit_scope(c);
2035 if (co == NULL)
2036 return 0;
2038 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
2039 Py_DECREF(co);
2041 return 1;
2044 static int
2045 compiler_print(struct compiler *c, stmt_ty s)
2047 int i, n;
2048 bool dest;
2050 assert(s->kind == Print_kind);
2051 n = asdl_seq_LEN(s->v.Print.values);
2052 dest = false;
2053 if (s->v.Print.dest) {
2054 VISIT(c, expr, s->v.Print.dest);
2055 dest = true;
2057 for (i = 0; i < n; i++) {
2058 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
2059 if (dest) {
2060 ADDOP(c, DUP_TOP);
2061 VISIT(c, expr, e);
2062 ADDOP(c, ROT_TWO);
2063 ADDOP(c, PRINT_ITEM_TO);
2065 else {
2066 VISIT(c, expr, e);
2067 ADDOP(c, PRINT_ITEM);
2070 if (s->v.Print.nl) {
2071 if (dest)
2072 ADDOP(c, PRINT_NEWLINE_TO)
2073 else
2074 ADDOP(c, PRINT_NEWLINE)
2076 else if (dest)
2077 ADDOP(c, POP_TOP);
2078 return 1;
2081 static int
2082 compiler_if(struct compiler *c, stmt_ty s)
2084 basicblock *end, *next;
2086 assert(s->kind == If_kind);
2087 end = compiler_new_block(c);
2088 if (end == NULL)
2089 return 0;
2090 next = compiler_new_block(c);
2091 if (next == NULL)
2092 return 0;
2093 VISIT(c, expr, s->v.If.test);
2094 ADDOP_JREL(c, JUMP_IF_FALSE, next);
2095 ADDOP(c, POP_TOP);
2096 VISIT_SEQ(c, stmt, s->v.If.body);
2097 ADDOP_JREL(c, JUMP_FORWARD, end);
2098 compiler_use_next_block(c, next);
2099 ADDOP(c, POP_TOP);
2100 if (s->v.If.orelse)
2101 VISIT_SEQ(c, stmt, s->v.If.orelse);
2102 compiler_use_next_block(c, end);
2103 return 1;
2106 static int
2107 compiler_for(struct compiler *c, stmt_ty s)
2109 basicblock *start, *cleanup, *end;
2111 start = compiler_new_block(c);
2112 cleanup = compiler_new_block(c);
2113 end = compiler_new_block(c);
2114 if (start == NULL || end == NULL || cleanup == NULL)
2115 return 0;
2116 ADDOP_JREL(c, SETUP_LOOP, end);
2117 if (!compiler_push_fblock(c, LOOP, start))
2118 return 0;
2119 VISIT(c, expr, s->v.For.iter);
2120 ADDOP(c, GET_ITER);
2121 compiler_use_next_block(c, start);
2122 ADDOP_JREL(c, FOR_ITER, cleanup);
2123 VISIT(c, expr, s->v.For.target);
2124 VISIT_SEQ(c, stmt, s->v.For.body);
2125 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2126 compiler_use_next_block(c, cleanup);
2127 ADDOP(c, POP_BLOCK);
2128 compiler_pop_fblock(c, LOOP, start);
2129 VISIT_SEQ(c, stmt, s->v.For.orelse);
2130 compiler_use_next_block(c, end);
2131 return 1;
2134 static int
2135 compiler_while(struct compiler *c, stmt_ty s)
2137 basicblock *loop, *orelse, *end, *anchor = NULL;
2138 int constant = expr_constant(s->v.While.test);
2140 if (constant == 0)
2141 return 1;
2142 loop = compiler_new_block(c);
2143 end = compiler_new_block(c);
2144 if (constant == -1) {
2145 anchor = compiler_new_block(c);
2146 if (anchor == NULL)
2147 return 0;
2149 if (loop == NULL || end == NULL)
2150 return 0;
2151 if (s->v.While.orelse) {
2152 orelse = compiler_new_block(c);
2153 if (orelse == NULL)
2154 return 0;
2156 else
2157 orelse = NULL;
2159 ADDOP_JREL(c, SETUP_LOOP, end);
2160 compiler_use_next_block(c, loop);
2161 if (!compiler_push_fblock(c, LOOP, loop))
2162 return 0;
2163 if (constant == -1) {
2164 VISIT(c, expr, s->v.While.test);
2165 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
2166 ADDOP(c, POP_TOP);
2168 VISIT_SEQ(c, stmt, s->v.While.body);
2169 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2171 /* XXX should the two POP instructions be in a separate block
2172 if there is no else clause ?
2175 if (constant == -1) {
2176 compiler_use_next_block(c, anchor);
2177 ADDOP(c, POP_TOP);
2178 ADDOP(c, POP_BLOCK);
2180 compiler_pop_fblock(c, LOOP, loop);
2181 if (orelse != NULL)
2182 VISIT_SEQ(c, stmt, s->v.While.orelse);
2183 compiler_use_next_block(c, end);
2185 return 1;
2188 static int
2189 compiler_continue(struct compiler *c)
2191 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2192 int i;
2194 if (!c->u->u_nfblocks)
2195 return compiler_error(c, LOOP_ERROR_MSG);
2196 i = c->u->u_nfblocks - 1;
2197 switch (c->u->u_fblock[i].fb_type) {
2198 case LOOP:
2199 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2200 break;
2201 case EXCEPT:
2202 case FINALLY_TRY:
2203 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP)
2205 if (i == -1)
2206 return compiler_error(c, LOOP_ERROR_MSG);
2207 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2208 break;
2209 case FINALLY_END:
2210 return compiler_error(c,
2211 "'continue' not supported inside 'finally' clause");
2214 return 1;
2217 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
2219 SETUP_FINALLY L
2220 <code for body>
2221 POP_BLOCK
2222 LOAD_CONST <None>
2223 L: <code for finalbody>
2224 END_FINALLY
2226 The special instructions use the block stack. Each block
2227 stack entry contains the instruction that created it (here
2228 SETUP_FINALLY), the level of the value stack at the time the
2229 block stack entry was created, and a label (here L).
2231 SETUP_FINALLY:
2232 Pushes the current value stack level and the label
2233 onto the block stack.
2234 POP_BLOCK:
2235 Pops en entry from the block stack, and pops the value
2236 stack until its level is the same as indicated on the
2237 block stack. (The label is ignored.)
2238 END_FINALLY:
2239 Pops a variable number of entries from the *value* stack
2240 and re-raises the exception they specify. The number of
2241 entries popped depends on the (pseudo) exception type.
2243 The block stack is unwound when an exception is raised:
2244 when a SETUP_FINALLY entry is found, the exception is pushed
2245 onto the value stack (and the exception condition is cleared),
2246 and the interpreter jumps to the label gotten from the block
2247 stack.
2250 static int
2251 compiler_try_finally(struct compiler *c, stmt_ty s)
2253 basicblock *body, *end;
2254 body = compiler_new_block(c);
2255 end = compiler_new_block(c);
2256 if (body == NULL || end == NULL)
2257 return 0;
2259 ADDOP_JREL(c, SETUP_FINALLY, end);
2260 compiler_use_next_block(c, body);
2261 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2262 return 0;
2263 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
2264 ADDOP(c, POP_BLOCK);
2265 compiler_pop_fblock(c, FINALLY_TRY, body);
2267 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2268 compiler_use_next_block(c, end);
2269 if (!compiler_push_fblock(c, FINALLY_END, end))
2270 return 0;
2271 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
2272 ADDOP(c, END_FINALLY);
2273 compiler_pop_fblock(c, FINALLY_END, end);
2275 return 1;
2279 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2280 (The contents of the value stack is shown in [], with the top
2281 at the right; 'tb' is trace-back info, 'val' the exception's
2282 associated value, and 'exc' the exception.)
2284 Value stack Label Instruction Argument
2285 [] SETUP_EXCEPT L1
2286 [] <code for S>
2287 [] POP_BLOCK
2288 [] JUMP_FORWARD L0
2290 [tb, val, exc] L1: DUP )
2291 [tb, val, exc, exc] <evaluate E1> )
2292 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2293 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2294 [tb, val, exc, 1] POP )
2295 [tb, val, exc] POP
2296 [tb, val] <assign to V1> (or POP if no V1)
2297 [tb] POP
2298 [] <code for S1>
2299 JUMP_FORWARD L0
2301 [tb, val, exc, 0] L2: POP
2302 [tb, val, exc] DUP
2303 .............................etc.......................
2305 [tb, val, exc, 0] Ln+1: POP
2306 [tb, val, exc] END_FINALLY # re-raise exception
2308 [] L0: <next statement>
2310 Of course, parts are not generated if Vi or Ei is not present.
2312 static int
2313 compiler_try_except(struct compiler *c, stmt_ty s)
2315 basicblock *body, *orelse, *except, *end;
2316 int i, n;
2318 body = compiler_new_block(c);
2319 except = compiler_new_block(c);
2320 orelse = compiler_new_block(c);
2321 end = compiler_new_block(c);
2322 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2323 return 0;
2324 ADDOP_JREL(c, SETUP_EXCEPT, except);
2325 compiler_use_next_block(c, body);
2326 if (!compiler_push_fblock(c, EXCEPT, body))
2327 return 0;
2328 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
2329 ADDOP(c, POP_BLOCK);
2330 compiler_pop_fblock(c, EXCEPT, body);
2331 ADDOP_JREL(c, JUMP_FORWARD, orelse);
2332 n = asdl_seq_LEN(s->v.TryExcept.handlers);
2333 compiler_use_next_block(c, except);
2334 for (i = 0; i < n; i++) {
2335 excepthandler_ty handler = asdl_seq_GET(
2336 s->v.TryExcept.handlers, i);
2337 if (!handler->type && i < n-1)
2338 return compiler_error(c, "default 'except:' must be last");
2339 except = compiler_new_block(c);
2340 if (except == NULL)
2341 return 0;
2342 if (handler->type) {
2343 ADDOP(c, DUP_TOP);
2344 VISIT(c, expr, handler->type);
2345 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2346 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2347 ADDOP(c, POP_TOP);
2349 ADDOP(c, POP_TOP);
2350 if (handler->name) {
2351 VISIT(c, expr, handler->name);
2353 else {
2354 ADDOP(c, POP_TOP);
2356 ADDOP(c, POP_TOP);
2357 VISIT_SEQ(c, stmt, handler->body);
2358 ADDOP_JREL(c, JUMP_FORWARD, end);
2359 compiler_use_next_block(c, except);
2360 if (handler->type)
2361 ADDOP(c, POP_TOP);
2363 ADDOP(c, END_FINALLY);
2364 compiler_use_next_block(c, orelse);
2365 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2366 compiler_use_next_block(c, end);
2367 return 1;
2370 static int
2371 compiler_import_as(struct compiler *c, identifier name, identifier asname)
2373 /* The IMPORT_NAME opcode was already generated. This function
2374 merely needs to bind the result to a name.
2376 If there is a dot in name, we need to split it and emit a
2377 LOAD_ATTR for each name.
2379 const char *src = PyString_AS_STRING(name);
2380 const char *dot = strchr(src, '.');
2381 if (dot) {
2382 /* Consume the base module name to get the first attribute */
2383 src = dot + 1;
2384 while (dot) {
2385 /* NB src is only defined when dot != NULL */
2386 PyObject *attr;
2387 dot = strchr(src, '.');
2388 attr = PyString_FromStringAndSize(src,
2389 dot ? dot - src : strlen(src));
2390 if (!attr)
2391 return -1;
2392 ADDOP_O(c, LOAD_ATTR, attr, names);
2393 Py_DECREF(attr);
2394 src = dot + 1;
2397 return compiler_nameop(c, asname, Store);
2400 static int
2401 compiler_import(struct compiler *c, stmt_ty s)
2403 /* The Import node stores a module name like a.b.c as a single
2404 string. This is convenient for all cases except
2405 import a.b.c as d
2406 where we need to parse that string to extract the individual
2407 module names.
2408 XXX Perhaps change the representation to make this case simpler?
2410 int i, n = asdl_seq_LEN(s->v.Import.names);
2411 for (i = 0; i < n; i++) {
2412 alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
2413 int r;
2415 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2416 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2418 if (alias->asname) {
2419 r = compiler_import_as(c, alias->name, alias->asname);
2420 if (!r)
2421 return r;
2423 else {
2424 identifier tmp = alias->name;
2425 const char *base = PyString_AS_STRING(alias->name);
2426 char *dot = strchr(base, '.');
2427 if (dot)
2428 tmp = PyString_FromStringAndSize(base,
2429 dot - base);
2430 r = compiler_nameop(c, tmp, Store);
2431 if (dot) {
2432 Py_DECREF(tmp);
2434 if (!r)
2435 return r;
2438 return 1;
2441 static int
2442 compiler_from_import(struct compiler *c, stmt_ty s)
2444 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2446 PyObject *names = PyTuple_New(n);
2447 if (!names)
2448 return 0;
2450 /* build up the names */
2451 for (i = 0; i < n; i++) {
2452 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2453 Py_INCREF(alias->name);
2454 PyTuple_SET_ITEM(names, i, alias->name);
2457 if (s->lineno > c->c_future->ff_lineno) {
2458 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2459 "__future__")) {
2460 Py_DECREF(names);
2461 return compiler_error(c,
2462 "from __future__ imports must occur "
2463 "at the beginning of the file");
2468 ADDOP_O(c, LOAD_CONST, names, consts);
2469 Py_DECREF(names);
2470 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2471 for (i = 0; i < n; i++) {
2472 alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
2473 identifier store_name;
2475 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2476 assert(n == 1);
2477 ADDOP(c, IMPORT_STAR);
2478 return 1;
2481 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2482 store_name = alias->name;
2483 if (alias->asname)
2484 store_name = alias->asname;
2486 if (!compiler_nameop(c, store_name, Store)) {
2487 Py_DECREF(names);
2488 return 0;
2491 /* remove imported module */
2492 ADDOP(c, POP_TOP);
2493 return 1;
2496 static int
2497 compiler_assert(struct compiler *c, stmt_ty s)
2499 static PyObject *assertion_error = NULL;
2500 basicblock *end;
2502 if (Py_OptimizeFlag)
2503 return 1;
2504 if (assertion_error == NULL) {
2505 assertion_error = PyString_FromString("AssertionError");
2506 if (assertion_error == NULL)
2507 return 0;
2509 VISIT(c, expr, s->v.Assert.test);
2510 end = compiler_new_block(c);
2511 if (end == NULL)
2512 return 0;
2513 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2514 ADDOP(c, POP_TOP);
2515 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2516 if (s->v.Assert.msg) {
2517 VISIT(c, expr, s->v.Assert.msg);
2518 ADDOP_I(c, RAISE_VARARGS, 2);
2520 else {
2521 ADDOP_I(c, RAISE_VARARGS, 1);
2523 compiler_use_next_block(c, end);
2524 ADDOP(c, POP_TOP);
2525 return 1;
2528 static int
2529 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2531 int i, n;
2533 c->u->u_lineno = s->lineno;
2534 c->u->u_lineno_set = false;
2535 switch (s->kind) {
2536 case FunctionDef_kind:
2537 return compiler_function(c, s);
2538 case ClassDef_kind:
2539 return compiler_class(c, s);
2540 case Return_kind:
2541 if (c->u->u_ste->ste_type != FunctionBlock)
2542 return compiler_error(c, "'return' outside function");
2543 if (s->v.Return.value) {
2544 if (c->u->u_ste->ste_generator) {
2545 return compiler_error(c,
2546 "'return' with argument inside generator");
2548 VISIT(c, expr, s->v.Return.value);
2550 else
2551 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2552 ADDOP(c, RETURN_VALUE);
2553 break;
2554 case Delete_kind:
2555 VISIT_SEQ(c, expr, s->v.Delete.targets)
2556 break;
2557 case Assign_kind:
2558 n = asdl_seq_LEN(s->v.Assign.targets);
2559 VISIT(c, expr, s->v.Assign.value);
2560 for (i = 0; i < n; i++) {
2561 if (i < n - 1)
2562 ADDOP(c, DUP_TOP);
2563 VISIT(c, expr,
2564 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2566 break;
2567 case AugAssign_kind:
2568 return compiler_augassign(c, s);
2569 case Print_kind:
2570 return compiler_print(c, s);
2571 case For_kind:
2572 return compiler_for(c, s);
2573 case While_kind:
2574 return compiler_while(c, s);
2575 case If_kind:
2576 return compiler_if(c, s);
2577 case Raise_kind:
2578 n = 0;
2579 if (s->v.Raise.type) {
2580 VISIT(c, expr, s->v.Raise.type);
2581 n++;
2582 if (s->v.Raise.inst) {
2583 VISIT(c, expr, s->v.Raise.inst);
2584 n++;
2585 if (s->v.Raise.tback) {
2586 VISIT(c, expr, s->v.Raise.tback);
2587 n++;
2591 ADDOP_I(c, RAISE_VARARGS, n);
2592 break;
2593 case TryExcept_kind:
2594 return compiler_try_except(c, s);
2595 case TryFinally_kind:
2596 return compiler_try_finally(c, s);
2597 case Assert_kind:
2598 return compiler_assert(c, s);
2599 case Import_kind:
2600 return compiler_import(c, s);
2601 case ImportFrom_kind:
2602 return compiler_from_import(c, s);
2603 case Exec_kind:
2604 VISIT(c, expr, s->v.Exec.body);
2605 if (s->v.Exec.globals) {
2606 VISIT(c, expr, s->v.Exec.globals);
2607 if (s->v.Exec.locals) {
2608 VISIT(c, expr, s->v.Exec.locals);
2609 } else {
2610 ADDOP(c, DUP_TOP);
2612 } else {
2613 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2614 ADDOP(c, DUP_TOP);
2616 ADDOP(c, EXEC_STMT);
2617 break;
2618 case Global_kind:
2619 break;
2620 case Expr_kind:
2621 VISIT(c, expr, s->v.Expr.value);
2622 if (c->c_interactive && c->c_nestlevel <= 1) {
2623 ADDOP(c, PRINT_EXPR);
2625 else {
2626 ADDOP(c, POP_TOP);
2628 break;
2629 case Pass_kind:
2630 break;
2631 case Break_kind:
2632 if (!c->u->u_nfblocks)
2633 return compiler_error(c, "'break' outside loop");
2634 ADDOP(c, BREAK_LOOP);
2635 break;
2636 case Continue_kind:
2637 return compiler_continue(c);
2639 return 1;
2642 static int
2643 unaryop(unaryop_ty op)
2645 switch (op) {
2646 case Invert:
2647 return UNARY_INVERT;
2648 case Not:
2649 return UNARY_NOT;
2650 case UAdd:
2651 return UNARY_POSITIVE;
2652 case USub:
2653 return UNARY_NEGATIVE;
2655 return 0;
2658 static int
2659 binop(struct compiler *c, operator_ty op)
2661 switch (op) {
2662 case Add:
2663 return BINARY_ADD;
2664 case Sub:
2665 return BINARY_SUBTRACT;
2666 case Mult:
2667 return BINARY_MULTIPLY;
2668 case Div:
2669 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2670 return BINARY_TRUE_DIVIDE;
2671 else
2672 return BINARY_DIVIDE;
2673 case Mod:
2674 return BINARY_MODULO;
2675 case Pow:
2676 return BINARY_POWER;
2677 case LShift:
2678 return BINARY_LSHIFT;
2679 case RShift:
2680 return BINARY_RSHIFT;
2681 case BitOr:
2682 return BINARY_OR;
2683 case BitXor:
2684 return BINARY_XOR;
2685 case BitAnd:
2686 return BINARY_AND;
2687 case FloorDiv:
2688 return BINARY_FLOOR_DIVIDE;
2690 return 0;
2693 static int
2694 cmpop(cmpop_ty op)
2696 switch (op) {
2697 case Eq:
2698 return PyCmp_EQ;
2699 case NotEq:
2700 return PyCmp_NE;
2701 case Lt:
2702 return PyCmp_LT;
2703 case LtE:
2704 return PyCmp_LE;
2705 case Gt:
2706 return PyCmp_GT;
2707 case GtE:
2708 return PyCmp_GE;
2709 case Is:
2710 return PyCmp_IS;
2711 case IsNot:
2712 return PyCmp_IS_NOT;
2713 case In:
2714 return PyCmp_IN;
2715 case NotIn:
2716 return PyCmp_NOT_IN;
2718 return PyCmp_BAD;
2721 static int
2722 inplace_binop(struct compiler *c, operator_ty op)
2724 switch (op) {
2725 case Add:
2726 return INPLACE_ADD;
2727 case Sub:
2728 return INPLACE_SUBTRACT;
2729 case Mult:
2730 return INPLACE_MULTIPLY;
2731 case Div:
2732 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2733 return INPLACE_TRUE_DIVIDE;
2734 else
2735 return INPLACE_DIVIDE;
2736 case Mod:
2737 return INPLACE_MODULO;
2738 case Pow:
2739 return INPLACE_POWER;
2740 case LShift:
2741 return INPLACE_LSHIFT;
2742 case RShift:
2743 return INPLACE_RSHIFT;
2744 case BitOr:
2745 return INPLACE_OR;
2746 case BitXor:
2747 return INPLACE_XOR;
2748 case BitAnd:
2749 return INPLACE_AND;
2750 case FloorDiv:
2751 return INPLACE_FLOOR_DIVIDE;
2753 PyErr_Format(PyExc_SystemError,
2754 "inplace binary op %d should not be possible", op);
2755 return 0;
2758 static int
2759 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2761 int op, scope, arg;
2762 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2764 PyObject *dict = c->u->u_names;
2765 PyObject *mangled;
2766 /* XXX AugStore isn't used anywhere! */
2768 /* First check for assignment to __debug__. Param? */
2769 if ((ctx == Store || ctx == AugStore || ctx == Del)
2770 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2771 return compiler_error(c, "can not assign to __debug__");
2774 mangled = _Py_Mangle(c->u->u_private, name);
2775 if (!mangled)
2776 return 0;
2778 op = 0;
2779 optype = OP_NAME;
2780 scope = PyST_GetScope(c->u->u_ste, mangled);
2781 switch (scope) {
2782 case FREE:
2783 dict = c->u->u_freevars;
2784 optype = OP_DEREF;
2785 break;
2786 case CELL:
2787 dict = c->u->u_cellvars;
2788 optype = OP_DEREF;
2789 break;
2790 case LOCAL:
2791 if (c->u->u_ste->ste_type == FunctionBlock)
2792 optype = OP_FAST;
2793 break;
2794 case GLOBAL_IMPLICIT:
2795 if (c->u->u_ste->ste_type == FunctionBlock &&
2796 !c->u->u_ste->ste_unoptimized)
2797 optype = OP_GLOBAL;
2798 break;
2799 case GLOBAL_EXPLICIT:
2800 optype = OP_GLOBAL;
2801 break;
2802 default:
2803 /* scope can be 0 */
2804 break;
2807 /* XXX Leave assert here, but handle __doc__ and the like better */
2808 assert(scope || PyString_AS_STRING(name)[0] == '_');
2810 switch (optype) {
2811 case OP_DEREF:
2812 switch (ctx) {
2813 case Load: op = LOAD_DEREF; break;
2814 case Store: op = STORE_DEREF; break;
2815 case AugLoad:
2816 case AugStore:
2817 break;
2818 case Del:
2819 PyErr_Format(PyExc_SyntaxError,
2820 "can not delete variable '%s' referenced "
2821 "in nested scope",
2822 PyString_AS_STRING(name));
2823 Py_DECREF(mangled);
2824 return 0;
2825 case Param:
2826 default:
2827 PyErr_SetString(PyExc_SystemError,
2828 "param invalid for deref variable");
2829 return 0;
2831 break;
2832 case OP_FAST:
2833 switch (ctx) {
2834 case Load: op = LOAD_FAST; break;
2835 case Store: op = STORE_FAST; break;
2836 case Del: op = DELETE_FAST; break;
2837 case AugLoad:
2838 case AugStore:
2839 break;
2840 case Param:
2841 default:
2842 PyErr_SetString(PyExc_SystemError,
2843 "param invalid for local variable");
2844 return 0;
2846 ADDOP_O(c, op, mangled, varnames);
2847 Py_DECREF(mangled);
2848 return 1;
2849 case OP_GLOBAL:
2850 switch (ctx) {
2851 case Load: op = LOAD_GLOBAL; break;
2852 case Store: op = STORE_GLOBAL; break;
2853 case Del: op = DELETE_GLOBAL; break;
2854 case AugLoad:
2855 case AugStore:
2856 break;
2857 case Param:
2858 default:
2859 PyErr_SetString(PyExc_SystemError,
2860 "param invalid for global variable");
2861 return 0;
2863 break;
2864 case OP_NAME:
2865 switch (ctx) {
2866 case Load: op = LOAD_NAME; break;
2867 case Store: op = STORE_NAME; break;
2868 case Del: op = DELETE_NAME; break;
2869 case AugLoad:
2870 case AugStore:
2871 break;
2872 case Param:
2873 default:
2874 PyErr_SetString(PyExc_SystemError,
2875 "param invalid for name variable");
2876 return 0;
2878 break;
2881 assert(op);
2882 arg = compiler_add_o(c, dict, mangled);
2883 Py_DECREF(mangled);
2884 if (arg < 0)
2885 return 0;
2886 return compiler_addop_i(c, op, arg);
2889 static int
2890 compiler_boolop(struct compiler *c, expr_ty e)
2892 basicblock *end;
2893 int jumpi, i, n;
2894 asdl_seq *s;
2896 assert(e->kind == BoolOp_kind);
2897 if (e->v.BoolOp.op == And)
2898 jumpi = JUMP_IF_FALSE;
2899 else
2900 jumpi = JUMP_IF_TRUE;
2901 end = compiler_new_block(c);
2902 if (end == NULL)
2903 return 0;
2904 s = e->v.BoolOp.values;
2905 n = asdl_seq_LEN(s) - 1;
2906 for (i = 0; i < n; ++i) {
2907 VISIT(c, expr, asdl_seq_GET(s, i));
2908 ADDOP_JREL(c, jumpi, end);
2909 ADDOP(c, POP_TOP)
2911 VISIT(c, expr, asdl_seq_GET(s, n));
2912 compiler_use_next_block(c, end);
2913 return 1;
2916 static int
2917 compiler_list(struct compiler *c, expr_ty e)
2919 int n = asdl_seq_LEN(e->v.List.elts);
2920 if (e->v.List.ctx == Store) {
2921 ADDOP_I(c, UNPACK_SEQUENCE, n);
2923 VISIT_SEQ(c, expr, e->v.List.elts);
2924 if (e->v.List.ctx == Load) {
2925 ADDOP_I(c, BUILD_LIST, n);
2927 return 1;
2930 static int
2931 compiler_tuple(struct compiler *c, expr_ty e)
2933 int n = asdl_seq_LEN(e->v.Tuple.elts);
2934 if (e->v.Tuple.ctx == Store) {
2935 ADDOP_I(c, UNPACK_SEQUENCE, n);
2937 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2938 if (e->v.Tuple.ctx == Load) {
2939 ADDOP_I(c, BUILD_TUPLE, n);
2941 return 1;
2944 static int
2945 compiler_compare(struct compiler *c, expr_ty e)
2947 int i, n;
2948 basicblock *cleanup = NULL;
2950 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2951 VISIT(c, expr, e->v.Compare.left);
2952 n = asdl_seq_LEN(e->v.Compare.ops);
2953 assert(n > 0);
2954 if (n > 1) {
2955 cleanup = compiler_new_block(c);
2956 if (cleanup == NULL)
2957 return 0;
2958 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
2960 for (i = 1; i < n; i++) {
2961 ADDOP(c, DUP_TOP);
2962 ADDOP(c, ROT_THREE);
2963 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2964 ADDOP_I(c, COMPARE_OP,
2965 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
2966 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2967 NEXT_BLOCK(c);
2968 ADDOP(c, POP_TOP);
2969 if (i < (n - 1))
2970 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
2972 VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
2973 ADDOP_I(c, COMPARE_OP,
2974 /* XXX We're casting a void* to cmpop_ty in the next stmt. */
2975 cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
2976 if (n > 1) {
2977 basicblock *end = compiler_new_block(c);
2978 if (end == NULL)
2979 return 0;
2980 ADDOP_JREL(c, JUMP_FORWARD, end);
2981 compiler_use_next_block(c, cleanup);
2982 ADDOP(c, ROT_TWO);
2983 ADDOP(c, POP_TOP);
2984 compiler_use_next_block(c, end);
2986 return 1;
2989 static int
2990 compiler_call(struct compiler *c, expr_ty e)
2992 int n, code = 0;
2994 VISIT(c, expr, e->v.Call.func);
2995 n = asdl_seq_LEN(e->v.Call.args);
2996 VISIT_SEQ(c, expr, e->v.Call.args);
2997 if (e->v.Call.keywords) {
2998 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2999 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
3001 if (e->v.Call.starargs) {
3002 VISIT(c, expr, e->v.Call.starargs);
3003 code |= 1;
3005 if (e->v.Call.kwargs) {
3006 VISIT(c, expr, e->v.Call.kwargs);
3007 code |= 2;
3009 switch (code) {
3010 case 0:
3011 ADDOP_I(c, CALL_FUNCTION, n);
3012 break;
3013 case 1:
3014 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3015 break;
3016 case 2:
3017 ADDOP_I(c, CALL_FUNCTION_KW, n);
3018 break;
3019 case 3:
3020 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3021 break;
3023 return 1;
3026 static int
3027 compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
3028 asdl_seq *generators, int gen_index,
3029 expr_ty elt)
3031 /* generate code for the iterator, then each of the ifs,
3032 and then write to the element */
3034 comprehension_ty l;
3035 basicblock *start, *anchor, *skip, *if_cleanup;
3036 int i, n;
3038 start = compiler_new_block(c);
3039 skip = compiler_new_block(c);
3040 if_cleanup = compiler_new_block(c);
3041 anchor = compiler_new_block(c);
3043 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3044 anchor == NULL)
3045 return 0;
3047 l = asdl_seq_GET(generators, gen_index);
3048 VISIT(c, expr, l->iter);
3049 ADDOP(c, GET_ITER);
3050 compiler_use_next_block(c, start);
3051 ADDOP_JREL(c, FOR_ITER, anchor);
3052 NEXT_BLOCK(c);
3053 VISIT(c, expr, l->target);
3055 /* XXX this needs to be cleaned up...a lot! */
3056 n = asdl_seq_LEN(l->ifs);
3057 for (i = 0; i < n; i++) {
3058 expr_ty e = asdl_seq_GET(l->ifs, i);
3059 VISIT(c, expr, e);
3060 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3061 NEXT_BLOCK(c);
3062 ADDOP(c, POP_TOP);
3065 if (++gen_index < asdl_seq_LEN(generators))
3066 if (!compiler_listcomp_generator(c, tmpname,
3067 generators, gen_index, elt))
3068 return 0;
3070 /* only append after the last for generator */
3071 if (gen_index >= asdl_seq_LEN(generators)) {
3072 if (!compiler_nameop(c, tmpname, Load))
3073 return 0;
3074 VISIT(c, expr, elt);
3075 ADDOP_I(c, CALL_FUNCTION, 1);
3076 ADDOP(c, POP_TOP);
3078 compiler_use_next_block(c, skip);
3080 for (i = 0; i < n; i++) {
3081 ADDOP_I(c, JUMP_FORWARD, 1);
3082 if (i == 0)
3083 compiler_use_next_block(c, if_cleanup);
3084 ADDOP(c, POP_TOP);
3086 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3087 compiler_use_next_block(c, anchor);
3088 /* delete the append method added to locals */
3089 if (gen_index == 1)
3090 if (!compiler_nameop(c, tmpname, Del))
3091 return 0;
3093 return 1;
3096 static int
3097 compiler_listcomp(struct compiler *c, expr_ty e)
3099 char tmpname[256];
3100 identifier tmp;
3101 int rc = 0;
3102 static identifier append;
3103 asdl_seq *generators = e->v.ListComp.generators;
3105 assert(e->kind == ListComp_kind);
3106 if (!append) {
3107 append = PyString_InternFromString("append");
3108 if (!append)
3109 return 0;
3111 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
3112 tmp = PyString_FromString(tmpname);
3113 if (!tmp)
3114 return 0;
3115 ADDOP_I(c, BUILD_LIST, 0);
3116 ADDOP(c, DUP_TOP);
3117 ADDOP_O(c, LOAD_ATTR, append, names);
3118 if (compiler_nameop(c, tmp, Store))
3119 rc = compiler_listcomp_generator(c, tmp, generators, 0,
3120 e->v.ListComp.elt);
3121 Py_DECREF(tmp);
3122 return rc;
3125 static int
3126 compiler_genexp_generator(struct compiler *c,
3127 asdl_seq *generators, int gen_index,
3128 expr_ty elt)
3130 /* generate code for the iterator, then each of the ifs,
3131 and then write to the element */
3133 comprehension_ty ge;
3134 basicblock *start, *anchor, *skip, *if_cleanup, *end;
3135 int i, n;
3137 start = compiler_new_block(c);
3138 skip = compiler_new_block(c);
3139 if_cleanup = compiler_new_block(c);
3140 anchor = compiler_new_block(c);
3141 end = compiler_new_block(c);
3143 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3144 anchor == NULL || end == NULL)
3145 return 0;
3147 ge = asdl_seq_GET(generators, gen_index);
3148 ADDOP_JREL(c, SETUP_LOOP, end);
3149 if (!compiler_push_fblock(c, LOOP, start))
3150 return 0;
3152 if (gen_index == 0) {
3153 /* Receive outermost iter as an implicit argument */
3154 c->u->u_argcount = 1;
3155 ADDOP_I(c, LOAD_FAST, 0);
3157 else {
3158 /* Sub-iter - calculate on the fly */
3159 VISIT(c, expr, ge->iter);
3160 ADDOP(c, GET_ITER);
3162 compiler_use_next_block(c, start);
3163 ADDOP_JREL(c, FOR_ITER, anchor);
3164 NEXT_BLOCK(c);
3165 VISIT(c, expr, ge->target);
3167 /* XXX this needs to be cleaned up...a lot! */
3168 n = asdl_seq_LEN(ge->ifs);
3169 for (i = 0; i < n; i++) {
3170 expr_ty e = asdl_seq_GET(ge->ifs, i);
3171 VISIT(c, expr, e);
3172 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
3173 NEXT_BLOCK(c);
3174 ADDOP(c, POP_TOP);
3177 if (++gen_index < asdl_seq_LEN(generators))
3178 if (!compiler_genexp_generator(c, generators, gen_index, elt))
3179 return 0;
3181 /* only append after the last 'for' generator */
3182 if (gen_index >= asdl_seq_LEN(generators)) {
3183 VISIT(c, expr, elt);
3184 ADDOP(c, YIELD_VALUE);
3185 ADDOP(c, POP_TOP);
3187 compiler_use_next_block(c, skip);
3189 for (i = 0; i < n; i++) {
3190 ADDOP_I(c, JUMP_FORWARD, 1);
3191 if (i == 0)
3192 compiler_use_next_block(c, if_cleanup);
3194 ADDOP(c, POP_TOP);
3196 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3197 compiler_use_next_block(c, anchor);
3198 ADDOP(c, POP_BLOCK);
3199 compiler_pop_fblock(c, LOOP, start);
3200 compiler_use_next_block(c, end);
3202 return 1;
3205 static int
3206 compiler_genexp(struct compiler *c, expr_ty e)
3208 static identifier name;
3209 PyCodeObject *co;
3210 expr_ty outermost_iter = ((comprehension_ty)
3211 (asdl_seq_GET(e->v.GeneratorExp.generators,
3212 0)))->iter;
3214 if (!name) {
3215 name = PyString_FromString("<genexpr>");
3216 if (!name)
3217 return 0;
3220 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
3221 return 0;
3222 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
3223 e->v.GeneratorExp.elt);
3224 co = assemble(c, 1);
3225 compiler_exit_scope(c);
3226 if (co == NULL)
3227 return 0;
3229 compiler_make_closure(c, co, 0);
3230 Py_DECREF(co);
3232 VISIT(c, expr, outermost_iter);
3233 ADDOP(c, GET_ITER);
3234 ADDOP_I(c, CALL_FUNCTION, 1);
3236 return 1;
3239 static int
3240 compiler_visit_keyword(struct compiler *c, keyword_ty k)
3242 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3243 VISIT(c, expr, k->value);
3244 return 1;
3247 /* Test whether expression is constant. For constants, report
3248 whether they are true or false.
3250 Return values: 1 for true, 0 for false, -1 for non-constant.
3253 static int
3254 expr_constant(expr_ty e)
3256 switch (e->kind) {
3257 case Num_kind:
3258 return PyObject_IsTrue(e->v.Num.n);
3259 case Str_kind:
3260 return PyObject_IsTrue(e->v.Str.s);
3261 default:
3262 return -1;
3266 static int
3267 compiler_visit_expr(struct compiler *c, expr_ty e)
3269 int i, n;
3271 if (e->lineno > c->u->u_lineno) {
3272 c->u->u_lineno = e->lineno;
3273 c->u->u_lineno_set = false;
3275 switch (e->kind) {
3276 case BoolOp_kind:
3277 return compiler_boolop(c, e);
3278 case BinOp_kind:
3279 VISIT(c, expr, e->v.BinOp.left);
3280 VISIT(c, expr, e->v.BinOp.right);
3281 ADDOP(c, binop(c, e->v.BinOp.op));
3282 break;
3283 case UnaryOp_kind:
3284 VISIT(c, expr, e->v.UnaryOp.operand);
3285 ADDOP(c, unaryop(e->v.UnaryOp.op));
3286 break;
3287 case Lambda_kind:
3288 return compiler_lambda(c, e);
3289 case Dict_kind:
3290 /* XXX get rid of arg? */
3291 ADDOP_I(c, BUILD_MAP, 0);
3292 n = asdl_seq_LEN(e->v.Dict.values);
3293 /* We must arrange things just right for STORE_SUBSCR.
3294 It wants the stack to look like (value) (dict) (key) */
3295 for (i = 0; i < n; i++) {
3296 ADDOP(c, DUP_TOP);
3297 VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
3298 ADDOP(c, ROT_TWO);
3299 VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
3300 ADDOP(c, STORE_SUBSCR);
3302 break;
3303 case ListComp_kind:
3304 return compiler_listcomp(c, e);
3305 case GeneratorExp_kind:
3306 return compiler_genexp(c, e);
3307 case Yield_kind:
3308 if (c->u->u_ste->ste_type != FunctionBlock)
3309 return compiler_error(c, "'yield' outside function");
3311 for (i = 0; i < c->u->u_nfblocks; i++) {
3312 if (c->u->u_fblock[i].fb_type == FINALLY_TRY)
3313 return compiler_error(
3314 c, "'yield' not allowed in a 'try' "
3315 "block with a 'finally' clause");
3318 if (e->v.Yield.value) {
3319 VISIT(c, expr, e->v.Yield.value);
3321 else {
3322 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3324 ADDOP(c, YIELD_VALUE);
3325 break;
3326 case Compare_kind:
3327 return compiler_compare(c, e);
3328 case Call_kind:
3329 return compiler_call(c, e);
3330 case Repr_kind:
3331 VISIT(c, expr, e->v.Repr.value);
3332 ADDOP(c, UNARY_CONVERT);
3333 break;
3334 case Num_kind:
3335 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3336 break;
3337 case Str_kind:
3338 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3339 break;
3340 /* The following exprs can be assignment targets. */
3341 case Attribute_kind:
3342 if (e->v.Attribute.ctx != AugStore)
3343 VISIT(c, expr, e->v.Attribute.value);
3344 switch (e->v.Attribute.ctx) {
3345 case AugLoad:
3346 ADDOP(c, DUP_TOP);
3347 /* Fall through to load */
3348 case Load:
3349 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3350 break;
3351 case AugStore:
3352 ADDOP(c, ROT_TWO);
3353 /* Fall through to save */
3354 case Store:
3355 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3356 break;
3357 case Del:
3358 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3359 break;
3360 case Param:
3361 default:
3362 PyErr_SetString(PyExc_SystemError,
3363 "param invalid in attribute expression");
3364 return 0;
3366 break;
3367 case Subscript_kind:
3368 switch (e->v.Subscript.ctx) {
3369 case AugLoad:
3370 VISIT(c, expr, e->v.Subscript.value);
3371 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3372 break;
3373 case Load:
3374 VISIT(c, expr, e->v.Subscript.value);
3375 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3376 break;
3377 case AugStore:
3378 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3379 break;
3380 case Store:
3381 VISIT(c, expr, e->v.Subscript.value);
3382 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3383 break;
3384 case Del:
3385 VISIT(c, expr, e->v.Subscript.value);
3386 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3387 break;
3388 case Param:
3389 default:
3390 PyErr_SetString(PyExc_SystemError,
3391 "param invalid in subscript expression");
3392 return 0;
3394 break;
3395 case Name_kind:
3396 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3397 /* child nodes of List and Tuple will have expr_context set */
3398 case List_kind:
3399 return compiler_list(c, e);
3400 case Tuple_kind:
3401 return compiler_tuple(c, e);
3403 return 1;
3406 static int
3407 compiler_augassign(struct compiler *c, stmt_ty s)
3409 expr_ty e = s->v.AugAssign.target;
3410 expr_ty auge;
3412 assert(s->kind == AugAssign_kind);
3414 switch (e->kind) {
3415 case Attribute_kind:
3416 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3417 AugLoad, e->lineno, c->c_arena);
3418 if (auge == NULL)
3419 return 0;
3420 VISIT(c, expr, auge);
3421 VISIT(c, expr, s->v.AugAssign.value);
3422 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3423 auge->v.Attribute.ctx = AugStore;
3424 VISIT(c, expr, auge);
3425 break;
3426 case Subscript_kind:
3427 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3428 AugLoad, e->lineno, c->c_arena);
3429 if (auge == NULL)
3430 return 0;
3431 VISIT(c, expr, auge);
3432 VISIT(c, expr, s->v.AugAssign.value);
3433 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3434 auge->v.Subscript.ctx = AugStore;
3435 VISIT(c, expr, auge);
3436 break;
3437 case Name_kind:
3438 VISIT(c, expr, s->v.AugAssign.target);
3439 VISIT(c, expr, s->v.AugAssign.value);
3440 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3441 return compiler_nameop(c, e->v.Name.id, Store);
3442 default:
3443 PyErr_Format(PyExc_SystemError,
3444 "invalid node type (%d) for augmented assignment",
3445 e->kind);
3446 return 0;
3448 return 1;
3451 static int
3452 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3454 struct fblockinfo *f;
3455 if (c->u->u_nfblocks >= CO_MAXBLOCKS)
3456 return 0;
3457 f = &c->u->u_fblock[c->u->u_nfblocks++];
3458 f->fb_type = t;
3459 f->fb_block = b;
3460 return 1;
3463 static void
3464 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3466 struct compiler_unit *u = c->u;
3467 assert(u->u_nfblocks > 0);
3468 u->u_nfblocks--;
3469 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3470 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3473 /* Raises a SyntaxError and returns 0.
3474 If something goes wrong, a different exception may be raised.
3477 static int
3478 compiler_error(struct compiler *c, const char *errstr)
3480 PyObject *loc;
3481 PyObject *u = NULL, *v = NULL;
3483 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3484 if (!loc) {
3485 Py_INCREF(Py_None);
3486 loc = Py_None;
3488 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3489 Py_None, loc);
3490 if (!u)
3491 goto exit;
3492 v = Py_BuildValue("(zO)", errstr, u);
3493 if (!v)
3494 goto exit;
3495 PyErr_SetObject(PyExc_SyntaxError, v);
3496 exit:
3497 Py_DECREF(loc);
3498 Py_XDECREF(u);
3499 Py_XDECREF(v);
3500 return 0;
3503 static int
3504 compiler_handle_subscr(struct compiler *c, const char *kind,
3505 expr_context_ty ctx)
3507 int op = 0;
3509 /* XXX this code is duplicated */
3510 switch (ctx) {
3511 case AugLoad: /* fall through to Load */
3512 case Load: op = BINARY_SUBSCR; break;
3513 case AugStore:/* fall through to Store */
3514 case Store: op = STORE_SUBSCR; break;
3515 case Del: op = DELETE_SUBSCR; break;
3516 case Param:
3517 PyErr_Format(PyExc_SystemError,
3518 "invalid %s kind %d in subscript\n",
3519 kind, ctx);
3520 return 0;
3522 if (ctx == AugLoad) {
3523 ADDOP_I(c, DUP_TOPX, 2);
3525 else if (ctx == AugStore) {
3526 ADDOP(c, ROT_THREE);
3528 ADDOP(c, op);
3529 return 1;
3532 static int
3533 compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3535 int n = 2;
3536 assert(s->kind == Slice_kind);
3538 /* only handles the cases where BUILD_SLICE is emitted */
3539 if (s->v.Slice.lower) {
3540 VISIT(c, expr, s->v.Slice.lower);
3542 else {
3543 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3546 if (s->v.Slice.upper) {
3547 VISIT(c, expr, s->v.Slice.upper);
3549 else {
3550 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3553 if (s->v.Slice.step) {
3554 n++;
3555 VISIT(c, expr, s->v.Slice.step);
3557 ADDOP_I(c, BUILD_SLICE, n);
3558 return 1;
3561 static int
3562 compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3564 int op = 0, slice_offset = 0, stack_count = 0;
3566 assert(s->v.Slice.step == NULL);
3567 if (s->v.Slice.lower) {
3568 slice_offset++;
3569 stack_count++;
3570 if (ctx != AugStore)
3571 VISIT(c, expr, s->v.Slice.lower);
3573 if (s->v.Slice.upper) {
3574 slice_offset += 2;
3575 stack_count++;
3576 if (ctx != AugStore)
3577 VISIT(c, expr, s->v.Slice.upper);
3580 if (ctx == AugLoad) {
3581 switch (stack_count) {
3582 case 0: ADDOP(c, DUP_TOP); break;
3583 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3584 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3587 else if (ctx == AugStore) {
3588 switch (stack_count) {
3589 case 0: ADDOP(c, ROT_TWO); break;
3590 case 1: ADDOP(c, ROT_THREE); break;
3591 case 2: ADDOP(c, ROT_FOUR); break;
3595 switch (ctx) {
3596 case AugLoad: /* fall through to Load */
3597 case Load: op = SLICE; break;
3598 case AugStore:/* fall through to Store */
3599 case Store: op = STORE_SLICE; break;
3600 case Del: op = DELETE_SLICE; break;
3601 case Param:
3602 default:
3603 PyErr_SetString(PyExc_SystemError,
3604 "param invalid in simple slice");
3605 return 0;
3608 ADDOP(c, op + slice_offset);
3609 return 1;
3612 static int
3613 compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3614 expr_context_ty ctx)
3616 switch (s->kind) {
3617 case Ellipsis_kind:
3618 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3619 break;
3620 case Slice_kind:
3621 return compiler_slice(c, s, ctx);
3622 case Index_kind:
3623 VISIT(c, expr, s->v.Index.value);
3624 break;
3625 case ExtSlice_kind:
3626 default:
3627 PyErr_SetString(PyExc_SystemError,
3628 "extended slice invalid in nested slice");
3629 return 0;
3631 return 1;
3635 static int
3636 compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3638 switch (s->kind) {
3639 case Ellipsis_kind:
3640 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3641 break;
3642 case Slice_kind:
3643 if (!s->v.Slice.step)
3644 return compiler_simple_slice(c, s, ctx);
3645 if (!compiler_slice(c, s, ctx))
3646 return 0;
3647 if (ctx == AugLoad) {
3648 ADDOP_I(c, DUP_TOPX, 2);
3650 else if (ctx == AugStore) {
3651 ADDOP(c, ROT_THREE);
3653 return compiler_handle_subscr(c, "slice", ctx);
3654 case ExtSlice_kind: {
3655 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3656 for (i = 0; i < n; i++) {
3657 slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
3658 if (!compiler_visit_nested_slice(c, sub, ctx))
3659 return 0;
3661 ADDOP_I(c, BUILD_TUPLE, n);
3662 return compiler_handle_subscr(c, "extended slice", ctx);
3664 case Index_kind:
3665 if (ctx != AugStore)
3666 VISIT(c, expr, s->v.Index.value);
3667 return compiler_handle_subscr(c, "index", ctx);
3668 default:
3669 PyErr_Format(PyExc_SystemError,
3670 "invalid slice %d", s->kind);
3671 return 0;
3673 return 1;
3676 /* do depth-first search of basic block graph, starting with block.
3677 post records the block indices in post-order.
3679 XXX must handle implicit jumps from one block to next
3682 static void
3683 dfs(struct compiler *c, basicblock *b, struct assembler *a)
3685 int i;
3686 struct instr *instr = NULL;
3688 if (b->b_seen)
3689 return;
3690 b->b_seen = 1;
3691 if (b->b_next != NULL)
3692 dfs(c, b->b_next, a);
3693 for (i = 0; i < b->b_iused; i++) {
3694 instr = &b->b_instr[i];
3695 if (instr->i_jrel || instr->i_jabs)
3696 dfs(c, instr->i_target, a);
3698 a->a_postorder[a->a_nblocks++] = b;
3701 static int
3702 stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3704 int i;
3705 struct instr *instr;
3706 if (b->b_seen || b->b_startdepth >= depth)
3707 return maxdepth;
3708 b->b_seen = 1;
3709 b->b_startdepth = depth;
3710 for (i = 0; i < b->b_iused; i++) {
3711 instr = &b->b_instr[i];
3712 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3713 if (depth > maxdepth)
3714 maxdepth = depth;
3715 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3716 if (instr->i_jrel || instr->i_jabs) {
3717 maxdepth = stackdepth_walk(c, instr->i_target,
3718 depth, maxdepth);
3719 if (instr->i_opcode == JUMP_ABSOLUTE ||
3720 instr->i_opcode == JUMP_FORWARD) {
3721 goto out; /* remaining code is dead */
3725 if (b->b_next)
3726 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3727 out:
3728 b->b_seen = 0;
3729 return maxdepth;
3732 /* Find the flow path that needs the largest stack. We assume that
3733 * cycles in the flow graph have no net effect on the stack depth.
3735 static int
3736 stackdepth(struct compiler *c)
3738 basicblock *b, *entryblock;
3739 entryblock = NULL;
3740 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3741 b->b_seen = 0;
3742 b->b_startdepth = INT_MIN;
3743 entryblock = b;
3745 return stackdepth_walk(c, entryblock, 0, 0);
3748 static int
3749 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3751 memset(a, 0, sizeof(struct assembler));
3752 a->a_lineno = firstlineno;
3753 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3754 if (!a->a_bytecode)
3755 return 0;
3756 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3757 if (!a->a_lnotab)
3758 return 0;
3759 a->a_postorder = (basicblock **)PyObject_Malloc(
3760 sizeof(basicblock *) * nblocks);
3761 if (!a->a_postorder) {
3762 PyErr_NoMemory();
3763 return 0;
3765 return 1;
3768 static void
3769 assemble_free(struct assembler *a)
3771 Py_XDECREF(a->a_bytecode);
3772 Py_XDECREF(a->a_lnotab);
3773 if (a->a_postorder)
3774 PyObject_Free(a->a_postorder);
3777 /* Return the size of a basic block in bytes. */
3779 static int
3780 instrsize(struct instr *instr)
3782 if (!instr->i_hasarg)
3783 return 1;
3784 if (instr->i_oparg > 0xffff)
3785 return 6;
3786 return 3;
3789 static int
3790 blocksize(basicblock *b)
3792 int i;
3793 int size = 0;
3795 for (i = 0; i < b->b_iused; i++)
3796 size += instrsize(&b->b_instr[i]);
3797 return size;
3800 /* All about a_lnotab.
3802 c_lnotab is an array of unsigned bytes disguised as a Python string.
3803 It is used to map bytecode offsets to source code line #s (when needed
3804 for tracebacks).
3806 The array is conceptually a list of
3807 (bytecode offset increment, line number increment)
3808 pairs. The details are important and delicate, best illustrated by example:
3810 byte code offset source code line number
3813 50 7
3814 350 307
3815 361 308
3817 The first trick is that these numbers aren't stored, only the increments
3818 from one row to the next (this doesn't really work, but it's a start):
3820 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3822 The second trick is that an unsigned byte can't hold negative values, or
3823 values larger than 255, so (a) there's a deep assumption that byte code
3824 offsets and their corresponding line #s both increase monotonically, and (b)
3825 if at least one column jumps by more than 255 from one row to the next, more
3826 than one pair is written to the table. In case #b, there's no way to know
3827 from looking at the table later how many were written. That's the delicate
3828 part. A user of c_lnotab desiring to find the source line number
3829 corresponding to a bytecode address A should do something like this
3831 lineno = addr = 0
3832 for addr_incr, line_incr in c_lnotab:
3833 addr += addr_incr
3834 if addr > A:
3835 return lineno
3836 lineno += line_incr
3838 In order for this to work, when the addr field increments by more than 255,
3839 the line # increment in each pair generated must be 0 until the remaining addr
3840 increment is < 256. So, in the example above, com_set_lineno should not (as
3841 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
3842 255, 0, 45, 255, 0, 45.
3845 static int
3846 assemble_lnotab(struct assembler *a, struct instr *i)
3848 int d_bytecode, d_lineno;
3849 int len;
3850 char *lnotab;
3852 d_bytecode = a->a_offset - a->a_lineno_off;
3853 d_lineno = i->i_lineno - a->a_lineno;
3855 assert(d_bytecode >= 0);
3856 assert(d_lineno >= 0);
3858 if (d_lineno == 0)
3859 return 1;
3861 if (d_bytecode > 255) {
3862 int j, nbytes, ncodes = d_bytecode / 255;
3863 nbytes = a->a_lnotab_off + 2 * ncodes;
3864 len = PyString_GET_SIZE(a->a_lnotab);
3865 if (nbytes >= len) {
3866 if (len * 2 < nbytes)
3867 len = nbytes;
3868 else
3869 len *= 2;
3870 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3871 return 0;
3873 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3874 for (j = 0; j < ncodes; j++) {
3875 *lnotab++ = 255;
3876 *lnotab++ = 0;
3878 d_bytecode -= ncodes * 255;
3879 a->a_lnotab_off += ncodes * 2;
3881 assert(d_bytecode <= 255);
3882 if (d_lineno > 255) {
3883 int j, nbytes, ncodes = d_lineno / 255;
3884 nbytes = a->a_lnotab_off + 2 * ncodes;
3885 len = PyString_GET_SIZE(a->a_lnotab);
3886 if (nbytes >= len) {
3887 if (len * 2 < nbytes)
3888 len = nbytes;
3889 else
3890 len *= 2;
3891 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3892 return 0;
3894 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3895 *lnotab++ = 255;
3896 *lnotab++ = d_bytecode;
3897 d_bytecode = 0;
3898 for (j = 1; j < ncodes; j++) {
3899 *lnotab++ = 255;
3900 *lnotab++ = 0;
3902 d_lineno -= ncodes * 255;
3903 a->a_lnotab_off += ncodes * 2;
3906 len = PyString_GET_SIZE(a->a_lnotab);
3907 if (a->a_lnotab_off + 2 >= len) {
3908 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
3909 return 0;
3911 lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3913 a->a_lnotab_off += 2;
3914 if (d_bytecode) {
3915 *lnotab++ = d_bytecode;
3916 *lnotab++ = d_lineno;
3918 else { /* First line of a block; def stmt, etc. */
3919 *lnotab++ = 0;
3920 *lnotab++ = d_lineno;
3922 a->a_lineno = i->i_lineno;
3923 a->a_lineno_off = a->a_offset;
3924 return 1;
3927 /* assemble_emit()
3928 Extend the bytecode with a new instruction.
3929 Update lnotab if necessary.
3932 static int
3933 assemble_emit(struct assembler *a, struct instr *i)
3935 int size, arg = 0, ext = 0;
3936 int len = PyString_GET_SIZE(a->a_bytecode);
3937 char *code;
3939 size = instrsize(i);
3940 if (i->i_hasarg) {
3941 arg = i->i_oparg;
3942 ext = arg >> 16;
3944 if (i->i_lineno && !assemble_lnotab(a, i))
3945 return 0;
3946 if (a->a_offset + size >= len) {
3947 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
3948 return 0;
3950 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3951 a->a_offset += size;
3952 if (size == 6) {
3953 assert(i->i_hasarg);
3954 *code++ = (char)EXTENDED_ARG;
3955 *code++ = ext & 0xff;
3956 *code++ = ext >> 8;
3957 arg &= 0xffff;
3959 *code++ = i->i_opcode;
3960 if (i->i_hasarg) {
3961 assert(size == 3 || size == 6);
3962 *code++ = arg & 0xff;
3963 *code++ = arg >> 8;
3965 return 1;
3968 static void
3969 assemble_jump_offsets(struct assembler *a, struct compiler *c)
3971 basicblock *b;
3972 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
3973 int i;
3975 /* Compute the size of each block and fixup jump args.
3976 Replace block pointer with position in bytecode. */
3977 start:
3978 totsize = 0;
3979 for (i = a->a_nblocks - 1; i >= 0; i--) {
3980 b = a->a_postorder[i];
3981 bsize = blocksize(b);
3982 b->b_offset = totsize;
3983 totsize += bsize;
3985 extended_arg_count = 0;
3986 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3987 bsize = b->b_offset;
3988 for (i = 0; i < b->b_iused; i++) {
3989 struct instr *instr = &b->b_instr[i];
3990 /* Relative jumps are computed relative to
3991 the instruction pointer after fetching
3992 the jump instruction.
3994 bsize += instrsize(instr);
3995 if (instr->i_jabs)
3996 instr->i_oparg = instr->i_target->b_offset;
3997 else if (instr->i_jrel) {
3998 int delta = instr->i_target->b_offset - bsize;
3999 instr->i_oparg = delta;
4001 else
4002 continue;
4003 if (instr->i_oparg > 0xffff)
4004 extended_arg_count++;
4008 /* XXX: This is an awful hack that could hurt performance, but
4009 on the bright side it should work until we come up
4010 with a better solution.
4012 In the meantime, should the goto be dropped in favor
4013 of a loop?
4015 The issue is that in the first loop blocksize() is called
4016 which calls instrsize() which requires i_oparg be set
4017 appropriately. There is a bootstrap problem because
4018 i_oparg is calculated in the second loop above.
4020 So we loop until we stop seeing new EXTENDED_ARGs.
4021 The only EXTENDED_ARGs that could be popping up are
4022 ones in jump instructions. So this should converge
4023 fairly quickly.
4025 if (last_extended_arg_count != extended_arg_count) {
4026 last_extended_arg_count = extended_arg_count;
4027 goto start;
4031 static PyObject *
4032 dict_keys_inorder(PyObject *dict, int offset)
4034 PyObject *tuple, *k, *v;
4035 int i, pos = 0, size = PyDict_Size(dict);
4037 tuple = PyTuple_New(size);
4038 if (tuple == NULL)
4039 return NULL;
4040 while (PyDict_Next(dict, &pos, &k, &v)) {
4041 i = PyInt_AS_LONG(v);
4042 k = PyTuple_GET_ITEM(k, 0);
4043 Py_INCREF(k);
4044 assert((i - offset) < size);
4045 assert((i - offset) >= 0);
4046 PyTuple_SET_ITEM(tuple, i - offset, k);
4048 return tuple;
4051 static int
4052 compute_code_flags(struct compiler *c)
4054 PySTEntryObject *ste = c->u->u_ste;
4055 int flags = 0, n;
4056 if (ste->ste_type != ModuleBlock)
4057 flags |= CO_NEWLOCALS;
4058 if (ste->ste_type == FunctionBlock) {
4059 if (!ste->ste_unoptimized)
4060 flags |= CO_OPTIMIZED;
4061 if (ste->ste_nested)
4062 flags |= CO_NESTED;
4063 if (ste->ste_generator)
4064 flags |= CO_GENERATOR;
4066 if (ste->ste_varargs)
4067 flags |= CO_VARARGS;
4068 if (ste->ste_varkeywords)
4069 flags |= CO_VARKEYWORDS;
4070 if (ste->ste_generator)
4071 flags |= CO_GENERATOR;
4072 if (c->c_flags->cf_flags & CO_FUTURE_DIVISION)
4073 flags |= CO_FUTURE_DIVISION;
4074 n = PyDict_Size(c->u->u_freevars);
4075 if (n < 0)
4076 return -1;
4077 if (n == 0) {
4078 n = PyDict_Size(c->u->u_cellvars);
4079 if (n < 0)
4080 return -1;
4081 if (n == 0) {
4082 flags |= CO_NOFREE;
4086 return flags;
4089 static PyCodeObject *
4090 makecode(struct compiler *c, struct assembler *a)
4092 PyObject *tmp;
4093 PyCodeObject *co = NULL;
4094 PyObject *consts = NULL;
4095 PyObject *names = NULL;
4096 PyObject *varnames = NULL;
4097 PyObject *filename = NULL;
4098 PyObject *name = NULL;
4099 PyObject *freevars = NULL;
4100 PyObject *cellvars = NULL;
4101 PyObject *bytecode = NULL;
4102 int nlocals, flags;
4104 tmp = dict_keys_inorder(c->u->u_consts, 0);
4105 if (!tmp)
4106 goto error;
4107 consts = PySequence_List(tmp); /* optimize_code requires a list */
4108 Py_DECREF(tmp);
4110 names = dict_keys_inorder(c->u->u_names, 0);
4111 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4112 if (!consts || !names || !varnames)
4113 goto error;
4115 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4116 if (!cellvars)
4117 goto error;
4118 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4119 if (!freevars)
4120 goto error;
4121 filename = PyString_FromString(c->c_filename);
4122 if (!filename)
4123 goto error;
4125 nlocals = PyDict_Size(c->u->u_varnames);
4126 flags = compute_code_flags(c);
4127 if (flags < 0)
4128 goto error;
4130 bytecode = optimize_code(a->a_bytecode, consts, names, a->a_lnotab);
4131 if (!bytecode)
4132 goto error;
4134 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4135 if (!tmp)
4136 goto error;
4137 Py_DECREF(consts);
4138 consts = tmp;
4140 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
4141 bytecode, consts, names, varnames,
4142 freevars, cellvars,
4143 filename, c->u->u_name,
4144 c->u->u_firstlineno,
4145 a->a_lnotab);
4146 error:
4147 Py_XDECREF(consts);
4148 Py_XDECREF(names);
4149 Py_XDECREF(varnames);
4150 Py_XDECREF(filename);
4151 Py_XDECREF(name);
4152 Py_XDECREF(freevars);
4153 Py_XDECREF(cellvars);
4154 Py_XDECREF(bytecode);
4155 return co;
4158 static PyCodeObject *
4159 assemble(struct compiler *c, int addNone)
4161 basicblock *b, *entryblock;
4162 struct assembler a;
4163 int i, j, nblocks;
4164 PyCodeObject *co = NULL;
4166 /* Make sure every block that falls off the end returns None.
4167 XXX NEXT_BLOCK() isn't quite right, because if the last
4168 block ends with a jump or return b_next shouldn't set.
4170 if (!c->u->u_curblock->b_return) {
4171 NEXT_BLOCK(c);
4172 if (addNone)
4173 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4174 ADDOP(c, RETURN_VALUE);
4177 nblocks = 0;
4178 entryblock = NULL;
4179 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4180 nblocks++;
4181 entryblock = b;
4184 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4185 goto error;
4186 dfs(c, entryblock, &a);
4188 /* Can't modify the bytecode after computing jump offsets. */
4189 assemble_jump_offsets(&a, c);
4191 /* Emit code in reverse postorder from dfs. */
4192 for (i = a.a_nblocks - 1; i >= 0; i--) {
4193 b = a.a_postorder[i];
4194 for (j = 0; j < b->b_iused; j++)
4195 if (!assemble_emit(&a, &b->b_instr[j]))
4196 goto error;
4199 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4200 goto error;
4201 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4202 goto error;
4204 co = makecode(c, &a);
4205 error:
4206 assemble_free(&a);
4207 return co;