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