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).
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 <stdbool.h>
25 
26 #include "Python.h"
27 #include "pycore_ast.h"           // _PyAST_GetDocString()
28 #include "pycore_compile.h"       // _PyFuture_FromAST()
29 #include "pycore_pymem.h"         // _PyMem_IsPtrFreed()
30 #include "pycore_long.h"          // _PyLong_GetZero()
31 #include "pycore_symtable.h"      // PySTEntryObject
32 
33 #define NEED_OPCODE_JUMP_TABLES
34 #include "opcode.h"               // EXTENDED_ARG
35 #include "wordcode_helpers.h"     // instrsize()
36 
37 
38 #define DEFAULT_BLOCK_SIZE 16
39 #define DEFAULT_BLOCKS 8
40 #define DEFAULT_CODE_SIZE 128
41 #define DEFAULT_LNOTAB_SIZE 16
42 
43 #define COMP_GENEXP   0
44 #define COMP_LISTCOMP 1
45 #define COMP_SETCOMP  2
46 #define COMP_DICTCOMP 3
47 
48 /* A soft limit for stack use, to avoid excessive
49  * memory use for large constants, etc.
50  *
51  * The value 30 is plucked out of thin air.
52  * Code that could use more stack than this is
53  * rare, so the exact value is unimportant.
54  */
55 #define STACK_USE_GUIDELINE 30
56 
57 /* If we exceed this limit, it should
58  * be considered a compiler bug.
59  * Currently it should be impossible
60  * to exceed STACK_USE_GUIDELINE * 100,
61  * as 100 is the maximum parse depth.
62  * For performance reasons we will
63  * want to reduce this to a
64  * few hundred in the future.
65  *
66  * NOTE: Whatever MAX_ALLOWED_STACK_USE is
67  * set to, it should never restrict what Python
68  * we can write, just how we compile it.
69  */
70 #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)
71 
72 #define IS_TOP_LEVEL_AWAIT(c) ( \
73         (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
74         && (c->u->u_ste->ste_type == ModuleBlock))
75 
76 struct instr {
77     unsigned char i_opcode;
78     int i_oparg;
79     struct basicblock_ *i_target; /* target block (if jump instruction) */
80     int i_lineno;
81 };
82 
83 #define LOG_BITS_PER_INT 5
84 #define MASK_LOW_LOG_BITS 31
85 
86 static inline int
is_bit_set_in_table(uint32_t * table,int bitindex)87 is_bit_set_in_table(uint32_t *table, int bitindex) {
88     /* Is the relevant bit set in the relevant word? */
89     /* 256 bits fit into 8 32-bits words.
90      * Word is indexed by (bitindex>>ln(size of int in bits)).
91      * Bit within word is the low bits of bitindex.
92      */
93     uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
94     return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
95 }
96 
97 static inline int
is_relative_jump(struct instr * i)98 is_relative_jump(struct instr *i)
99 {
100     return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
101 }
102 
103 static inline int
is_jump(struct instr * i)104 is_jump(struct instr *i)
105 {
106     return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
107 }
108 
109 typedef struct basicblock_ {
110     /* Each basicblock in a compilation unit is linked via b_list in the
111        reverse order that the block are allocated.  b_list points to the next
112        block, not to be confused with b_next, which is next by control flow. */
113     struct basicblock_ *b_list;
114     /* number of instructions used */
115     int b_iused;
116     /* length of instruction array (b_instr) */
117     int b_ialloc;
118     /* pointer to an array of instructions, initially NULL */
119     struct instr *b_instr;
120     /* If b_next is non-NULL, it is a pointer to the next
121        block reached by normal control flow. */
122     struct basicblock_ *b_next;
123     /* b_return is true if a RETURN_VALUE opcode is inserted. */
124     unsigned b_return : 1;
125     /* Number of predecssors that a block has. */
126     int b_predecessors;
127     /* Basic block has no fall through (it ends with a return, raise or jump) */
128     unsigned b_nofallthrough : 1;
129     /* Basic block exits scope (it ends with a return or raise) */
130     unsigned b_exit : 1;
131     /* depth of stack upon entry of block, computed by stackdepth() */
132     int b_startdepth;
133     /* instruction offset for block, computed by assemble_jump_offsets() */
134     int b_offset;
135 } basicblock;
136 
137 /* fblockinfo tracks the current frame block.
138 
139 A frame block is used to handle loops, try/except, and try/finally.
140 It's called a frame block to distinguish it from a basic block in the
141 compiler IR.
142 */
143 
144 enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
145                   WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
146                   ASYNC_COMPREHENSION_GENERATOR };
147 
148 struct fblockinfo {
149     enum fblocktype fb_type;
150     basicblock *fb_block;
151     /* (optional) type-specific exit or cleanup block */
152     basicblock *fb_exit;
153     /* (optional) additional information required for unwinding */
154     void *fb_datum;
155 };
156 
157 enum {
158     COMPILER_SCOPE_MODULE,
159     COMPILER_SCOPE_CLASS,
160     COMPILER_SCOPE_FUNCTION,
161     COMPILER_SCOPE_ASYNC_FUNCTION,
162     COMPILER_SCOPE_LAMBDA,
163     COMPILER_SCOPE_COMPREHENSION,
164 };
165 
166 /* The following items change on entry and exit of code blocks.
167    They must be saved and restored when returning to a block.
168 */
169 struct compiler_unit {
170     PySTEntryObject *u_ste;
171 
172     PyObject *u_name;
173     PyObject *u_qualname;  /* dot-separated qualified name (lazy) */
174     int u_scope_type;
175 
176     /* The following fields are dicts that map objects to
177        the index of them in co_XXX.      The index is used as
178        the argument for opcodes that refer to those collections.
179     */
180     PyObject *u_consts;    /* all constants */
181     PyObject *u_names;     /* all names */
182     PyObject *u_varnames;  /* local variables */
183     PyObject *u_cellvars;  /* cell variables */
184     PyObject *u_freevars;  /* free variables */
185 
186     PyObject *u_private;        /* for private name mangling */
187 
188     Py_ssize_t u_argcount;        /* number of arguments for block */
189     Py_ssize_t u_posonlyargcount;        /* number of positional only arguments for block */
190     Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
191     /* Pointer to the most recently allocated block.  By following b_list
192        members, you can reach all early allocated blocks. */
193     basicblock *u_blocks;
194     basicblock *u_curblock; /* pointer to current block */
195 
196     int u_nfblocks;
197     struct fblockinfo u_fblock[CO_MAXBLOCKS];
198 
199     int u_firstlineno; /* the first lineno of the block */
200     int u_lineno;          /* the lineno for the current stmt */
201     int u_col_offset;      /* the offset of the current stmt */
202     int u_end_lineno;      /* the end line of the current stmt */
203     int u_end_col_offset;  /* the end offset of the current stmt */
204 };
205 
206 /* This struct captures the global state of a compilation.
207 
208 The u pointer points to the current compilation unit, while units
209 for enclosing blocks are stored in c_stack.     The u and c_stack are
210 managed by compiler_enter_scope() and compiler_exit_scope().
211 
212 Note that we don't track recursion levels during compilation - the
213 task of detecting and rejecting excessive levels of nesting is
214 handled by the symbol analysis pass.
215 
216 */
217 
218 struct compiler {
219     PyObject *c_filename;
220     struct symtable *c_st;
221     PyFutureFeatures *c_future; /* pointer to module's __future__ */
222     PyCompilerFlags *c_flags;
223 
224     int c_optimize;              /* optimization level */
225     int c_interactive;           /* true if in interactive mode */
226     int c_nestlevel;
227     PyObject *c_const_cache;     /* Python dict holding all constants,
228                                     including names tuple */
229     struct compiler_unit *u; /* compiler state for current block */
230     PyObject *c_stack;           /* Python list holding compiler_unit ptrs */
231     PyArena *c_arena;            /* pointer to memory allocation arena */
232 };
233 
234 typedef struct {
235     // A list of strings corresponding to name captures. It is used to track:
236     // - Repeated name assignments in the same pattern.
237     // - Different name assignments in alternatives.
238     // - The order of name assignments in alternatives.
239     PyObject *stores;
240     // If 0, any name captures against our subject will raise.
241     int allow_irrefutable;
242     // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
243     // i items off of the stack. The end result looks like this (with each block
244     // falling through to the next):
245     // fail_pop[4]: POP_TOP
246     // fail_pop[3]: POP_TOP
247     // fail_pop[2]: POP_TOP
248     // fail_pop[1]: POP_TOP
249     // fail_pop[0]: NOP
250     basicblock **fail_pop;
251     // The current length of fail_pop.
252     Py_ssize_t fail_pop_size;
253     // The number of items on top of the stack that need to *stay* on top of the
254     // stack. Variable captures go beneath these. All of them will be popped on
255     // failure.
256     Py_ssize_t on_top;
257 } pattern_context;
258 
259 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
260 static void compiler_free(struct compiler *);
261 static basicblock *compiler_new_block(struct compiler *);
262 static int compiler_next_instr(basicblock *);
263 static int compiler_addop(struct compiler *, int);
264 static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
265 static int compiler_addop_j(struct compiler *, int, basicblock *);
266 static int compiler_addop_j_noline(struct compiler *, int, basicblock *);
267 static int compiler_error(struct compiler *, const char *, ...);
268 static int compiler_warn(struct compiler *, const char *, ...);
269 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
270 
271 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
272 static int compiler_visit_stmt(struct compiler *, stmt_ty);
273 static int compiler_visit_keyword(struct compiler *, keyword_ty);
274 static int compiler_visit_expr(struct compiler *, expr_ty);
275 static int compiler_augassign(struct compiler *, stmt_ty);
276 static int compiler_annassign(struct compiler *, stmt_ty);
277 static int compiler_subscript(struct compiler *, expr_ty);
278 static int compiler_slice(struct compiler *, expr_ty);
279 
280 static int inplace_binop(operator_ty);
281 static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
282 
283 
284 static int compiler_with(struct compiler *, stmt_ty, int);
285 static int compiler_async_with(struct compiler *, stmt_ty, int);
286 static int compiler_async_for(struct compiler *, stmt_ty);
287 static int compiler_call_helper(struct compiler *c, int n,
288                                 asdl_expr_seq *args,
289                                 asdl_keyword_seq *keywords);
290 static int compiler_try_except(struct compiler *, stmt_ty);
291 static int compiler_set_qualname(struct compiler *);
292 
293 static int compiler_sync_comprehension_generator(
294                                       struct compiler *c,
295                                       asdl_comprehension_seq *generators, int gen_index,
296                                       int depth,
297                                       expr_ty elt, expr_ty val, int type);
298 
299 static int compiler_async_comprehension_generator(
300                                       struct compiler *c,
301                                       asdl_comprehension_seq *generators, int gen_index,
302                                       int depth,
303                                       expr_ty elt, expr_ty val, int type);
304 
305 static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
306 static int compiler_match(struct compiler *, stmt_ty);
307 static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
308                                        pattern_context *);
309 
310 static PyCodeObject *assemble(struct compiler *, int addNone);
311 static PyObject *__doc__, *__annotations__;
312 
313 #define CAPSULE_NAME "compile.c compiler unit"
314 
315 PyObject *
_Py_Mangle(PyObject * privateobj,PyObject * ident)316 _Py_Mangle(PyObject *privateobj, PyObject *ident)
317 {
318     /* Name mangling: __private becomes _classname__private.
319        This is independent from how the name is used. */
320     PyObject *result;
321     size_t nlen, plen, ipriv;
322     Py_UCS4 maxchar;
323     if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
324         PyUnicode_READ_CHAR(ident, 0) != '_' ||
325         PyUnicode_READ_CHAR(ident, 1) != '_') {
326         Py_INCREF(ident);
327         return ident;
328     }
329     nlen = PyUnicode_GET_LENGTH(ident);
330     plen = PyUnicode_GET_LENGTH(privateobj);
331     /* Don't mangle __id__ or names with dots.
332 
333        The only time a name with a dot can occur is when
334        we are compiling an import statement that has a
335        package name.
336 
337        TODO(jhylton): Decide whether we want to support
338        mangling of the module name, e.g. __M.X.
339     */
340     if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
341          PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
342         PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
343         Py_INCREF(ident);
344         return ident; /* Don't mangle __whatever__ */
345     }
346     /* Strip leading underscores from class name */
347     ipriv = 0;
348     while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
349         ipriv++;
350     if (ipriv == plen) {
351         Py_INCREF(ident);
352         return ident; /* Don't mangle if class is just underscores */
353     }
354     plen -= ipriv;
355 
356     if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
357         PyErr_SetString(PyExc_OverflowError,
358                         "private identifier too large to be mangled");
359         return NULL;
360     }
361 
362     maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
363     if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
364         maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
365 
366     result = PyUnicode_New(1 + nlen + plen, maxchar);
367     if (!result)
368         return 0;
369     /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
370     PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
371     if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
372         Py_DECREF(result);
373         return NULL;
374     }
375     if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
376         Py_DECREF(result);
377         return NULL;
378     }
379     assert(_PyUnicode_CheckConsistency(result, 1));
380     return result;
381 }
382 
383 static int
compiler_init(struct compiler * c)384 compiler_init(struct compiler *c)
385 {
386     memset(c, 0, sizeof(struct compiler));
387 
388     c->c_const_cache = PyDict_New();
389     if (!c->c_const_cache) {
390         return 0;
391     }
392 
393     c->c_stack = PyList_New(0);
394     if (!c->c_stack) {
395         Py_CLEAR(c->c_const_cache);
396         return 0;
397     }
398 
399     return 1;
400 }
401 
402 PyCodeObject *
_PyAST_Compile(mod_ty mod,PyObject * filename,PyCompilerFlags * flags,int optimize,PyArena * arena)403 _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
404                int optimize, PyArena *arena)
405 {
406     struct compiler c;
407     PyCodeObject *co = NULL;
408     PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
409     int merged;
410 
411     if (!__doc__) {
412         __doc__ = PyUnicode_InternFromString("__doc__");
413         if (!__doc__)
414             return NULL;
415     }
416     if (!__annotations__) {
417         __annotations__ = PyUnicode_InternFromString("__annotations__");
418         if (!__annotations__)
419             return NULL;
420     }
421     if (!compiler_init(&c))
422         return NULL;
423     Py_INCREF(filename);
424     c.c_filename = filename;
425     c.c_arena = arena;
426     c.c_future = _PyFuture_FromAST(mod, filename);
427     if (c.c_future == NULL)
428         goto finally;
429     if (!flags) {
430         flags = &local_flags;
431     }
432     merged = c.c_future->ff_features | flags->cf_flags;
433     c.c_future->ff_features = merged;
434     flags->cf_flags = merged;
435     c.c_flags = flags;
436     c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
437     c.c_nestlevel = 0;
438 
439     _PyASTOptimizeState state;
440     state.optimize = c.c_optimize;
441     state.ff_features = merged;
442 
443     if (!_PyAST_Optimize(mod, arena, &state)) {
444         goto finally;
445     }
446 
447     c.c_st = _PySymtable_Build(mod, filename, c.c_future);
448     if (c.c_st == NULL) {
449         if (!PyErr_Occurred())
450             PyErr_SetString(PyExc_SystemError, "no symtable");
451         goto finally;
452     }
453 
454     co = compiler_mod(&c, mod);
455 
456  finally:
457     compiler_free(&c);
458     assert(co || PyErr_Occurred());
459     return co;
460 }
461 
462 static void
compiler_free(struct compiler * c)463 compiler_free(struct compiler *c)
464 {
465     if (c->c_st)
466         _PySymtable_Free(c->c_st);
467     if (c->c_future)
468         PyObject_Free(c->c_future);
469     Py_XDECREF(c->c_filename);
470     Py_DECREF(c->c_const_cache);
471     Py_DECREF(c->c_stack);
472 }
473 
474 static PyObject *
list2dict(PyObject * list)475 list2dict(PyObject *list)
476 {
477     Py_ssize_t i, n;
478     PyObject *v, *k;
479     PyObject *dict = PyDict_New();
480     if (!dict) return NULL;
481 
482     n = PyList_Size(list);
483     for (i = 0; i < n; i++) {
484         v = PyLong_FromSsize_t(i);
485         if (!v) {
486             Py_DECREF(dict);
487             return NULL;
488         }
489         k = PyList_GET_ITEM(list, i);
490         if (PyDict_SetItem(dict, k, v) < 0) {
491             Py_DECREF(v);
492             Py_DECREF(dict);
493             return NULL;
494         }
495         Py_DECREF(v);
496     }
497     return dict;
498 }
499 
500 /* Return new dict containing names from src that match scope(s).
501 
502 src is a symbol table dictionary.  If the scope of a name matches
503 either scope_type or flag is set, insert it into the new dict.  The
504 values are integers, starting at offset and increasing by one for
505 each key.
506 */
507 
508 static PyObject *
dictbytype(PyObject * src,int scope_type,int flag,Py_ssize_t offset)509 dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
510 {
511     Py_ssize_t i = offset, scope, num_keys, key_i;
512     PyObject *k, *v, *dest = PyDict_New();
513     PyObject *sorted_keys;
514 
515     assert(offset >= 0);
516     if (dest == NULL)
517         return NULL;
518 
519     /* Sort the keys so that we have a deterministic order on the indexes
520        saved in the returned dictionary.  These indexes are used as indexes
521        into the free and cell var storage.  Therefore if they aren't
522        deterministic, then the generated bytecode is not deterministic.
523     */
524     sorted_keys = PyDict_Keys(src);
525     if (sorted_keys == NULL)
526         return NULL;
527     if (PyList_Sort(sorted_keys) != 0) {
528         Py_DECREF(sorted_keys);
529         return NULL;
530     }
531     num_keys = PyList_GET_SIZE(sorted_keys);
532 
533     for (key_i = 0; key_i < num_keys; key_i++) {
534         /* XXX this should probably be a macro in symtable.h */
535         long vi;
536         k = PyList_GET_ITEM(sorted_keys, key_i);
537         v = PyDict_GetItemWithError(src, k);
538         assert(v && PyLong_Check(v));
539         vi = PyLong_AS_LONG(v);
540         scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
541 
542         if (scope == scope_type || vi & flag) {
543             PyObject *item = PyLong_FromSsize_t(i);
544             if (item == NULL) {
545                 Py_DECREF(sorted_keys);
546                 Py_DECREF(dest);
547                 return NULL;
548             }
549             i++;
550             if (PyDict_SetItem(dest, k, item) < 0) {
551                 Py_DECREF(sorted_keys);
552                 Py_DECREF(item);
553                 Py_DECREF(dest);
554                 return NULL;
555             }
556             Py_DECREF(item);
557         }
558     }
559     Py_DECREF(sorted_keys);
560     return dest;
561 }
562 
563 static void
compiler_unit_check(struct compiler_unit * u)564 compiler_unit_check(struct compiler_unit *u)
565 {
566     basicblock *block;
567     for (block = u->u_blocks; block != NULL; block = block->b_list) {
568         assert(!_PyMem_IsPtrFreed(block));
569         if (block->b_instr != NULL) {
570             assert(block->b_ialloc > 0);
571             assert(block->b_iused >= 0);
572             assert(block->b_ialloc >= block->b_iused);
573         }
574         else {
575             assert (block->b_iused == 0);
576             assert (block->b_ialloc == 0);
577         }
578     }
579 }
580 
581 static void
compiler_unit_free(struct compiler_unit * u)582 compiler_unit_free(struct compiler_unit *u)
583 {
584     basicblock *b, *next;
585 
586     compiler_unit_check(u);
587     b = u->u_blocks;
588     while (b != NULL) {
589         if (b->b_instr)
590             PyObject_Free((void *)b->b_instr);
591         next = b->b_list;
592         PyObject_Free((void *)b);
593         b = next;
594     }
595     Py_CLEAR(u->u_ste);
596     Py_CLEAR(u->u_name);
597     Py_CLEAR(u->u_qualname);
598     Py_CLEAR(u->u_consts);
599     Py_CLEAR(u->u_names);
600     Py_CLEAR(u->u_varnames);
601     Py_CLEAR(u->u_freevars);
602     Py_CLEAR(u->u_cellvars);
603     Py_CLEAR(u->u_private);
604     PyObject_Free(u);
605 }
606 
607 static int
compiler_enter_scope(struct compiler * c,identifier name,int scope_type,void * key,int lineno)608 compiler_enter_scope(struct compiler *c, identifier name,
609                      int scope_type, void *key, int lineno)
610 {
611     struct compiler_unit *u;
612     basicblock *block;
613 
614     u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
615                                             struct compiler_unit));
616     if (!u) {
617         PyErr_NoMemory();
618         return 0;
619     }
620     u->u_scope_type = scope_type;
621     u->u_argcount = 0;
622     u->u_posonlyargcount = 0;
623     u->u_kwonlyargcount = 0;
624     u->u_ste = PySymtable_Lookup(c->c_st, key);
625     if (!u->u_ste) {
626         compiler_unit_free(u);
627         return 0;
628     }
629     Py_INCREF(name);
630     u->u_name = name;
631     u->u_varnames = list2dict(u->u_ste->ste_varnames);
632     u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
633     if (!u->u_varnames || !u->u_cellvars) {
634         compiler_unit_free(u);
635         return 0;
636     }
637     if (u->u_ste->ste_needs_class_closure) {
638         /* Cook up an implicit __class__ cell. */
639         _Py_IDENTIFIER(__class__);
640         PyObject *name;
641         int res;
642         assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
643         assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
644         name = _PyUnicode_FromId(&PyId___class__);
645         if (!name) {
646             compiler_unit_free(u);
647             return 0;
648         }
649         res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero());
650         if (res < 0) {
651             compiler_unit_free(u);
652             return 0;
653         }
654     }
655 
656     u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
657                                PyDict_GET_SIZE(u->u_cellvars));
658     if (!u->u_freevars) {
659         compiler_unit_free(u);
660         return 0;
661     }
662 
663     u->u_blocks = NULL;
664     u->u_nfblocks = 0;
665     u->u_firstlineno = lineno;
666     u->u_lineno = 0;
667     u->u_col_offset = 0;
668     u->u_end_lineno = 0;
669     u->u_end_col_offset = 0;
670     u->u_consts = PyDict_New();
671     if (!u->u_consts) {
672         compiler_unit_free(u);
673         return 0;
674     }
675     u->u_names = PyDict_New();
676     if (!u->u_names) {
677         compiler_unit_free(u);
678         return 0;
679     }
680 
681     u->u_private = NULL;
682 
683     /* Push the old compiler_unit on the stack. */
684     if (c->u) {
685         PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
686         if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
687             Py_XDECREF(capsule);
688             compiler_unit_free(u);
689             return 0;
690         }
691         Py_DECREF(capsule);
692         u->u_private = c->u->u_private;
693         Py_XINCREF(u->u_private);
694     }
695     c->u = u;
696 
697     c->c_nestlevel++;
698 
699     block = compiler_new_block(c);
700     if (block == NULL)
701         return 0;
702     c->u->u_curblock = block;
703 
704     if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
705         if (!compiler_set_qualname(c))
706             return 0;
707     }
708 
709     return 1;
710 }
711 
712 static void
compiler_exit_scope(struct compiler * c)713 compiler_exit_scope(struct compiler *c)
714 {
715     // Don't call PySequence_DelItem() with an exception raised
716     PyObject *exc_type, *exc_val, *exc_tb;
717     PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
718 
719     c->c_nestlevel--;
720     compiler_unit_free(c->u);
721     /* Restore c->u to the parent unit. */
722     Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
723     if (n >= 0) {
724         PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
725         c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
726         assert(c->u);
727         /* we are deleting from a list so this really shouldn't fail */
728         if (PySequence_DelItem(c->c_stack, n) < 0) {
729             _PyErr_WriteUnraisableMsg("on removing the last compiler "
730                                       "stack item", NULL);
731         }
732         compiler_unit_check(c->u);
733     }
734     else {
735         c->u = NULL;
736     }
737 
738     PyErr_Restore(exc_type, exc_val, exc_tb);
739 }
740 
741 static int
compiler_set_qualname(struct compiler * c)742 compiler_set_qualname(struct compiler *c)
743 {
744     _Py_static_string(dot, ".");
745     _Py_static_string(dot_locals, ".<locals>");
746     Py_ssize_t stack_size;
747     struct compiler_unit *u = c->u;
748     PyObject *name, *base, *dot_str, *dot_locals_str;
749 
750     base = NULL;
751     stack_size = PyList_GET_SIZE(c->c_stack);
752     assert(stack_size >= 1);
753     if (stack_size > 1) {
754         int scope, force_global = 0;
755         struct compiler_unit *parent;
756         PyObject *mangled, *capsule;
757 
758         capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
759         parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
760         assert(parent);
761 
762         if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
763             || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
764             || u->u_scope_type == COMPILER_SCOPE_CLASS) {
765             assert(u->u_name);
766             mangled = _Py_Mangle(parent->u_private, u->u_name);
767             if (!mangled)
768                 return 0;
769             scope = _PyST_GetScope(parent->u_ste, mangled);
770             Py_DECREF(mangled);
771             assert(scope != GLOBAL_IMPLICIT);
772             if (scope == GLOBAL_EXPLICIT)
773                 force_global = 1;
774         }
775 
776         if (!force_global) {
777             if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
778                 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
779                 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
780                 dot_locals_str = _PyUnicode_FromId(&dot_locals);
781                 if (dot_locals_str == NULL)
782                     return 0;
783                 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
784                 if (base == NULL)
785                     return 0;
786             }
787             else {
788                 Py_INCREF(parent->u_qualname);
789                 base = parent->u_qualname;
790             }
791         }
792     }
793 
794     if (base != NULL) {
795         dot_str = _PyUnicode_FromId(&dot);
796         if (dot_str == NULL) {
797             Py_DECREF(base);
798             return 0;
799         }
800         name = PyUnicode_Concat(base, dot_str);
801         Py_DECREF(base);
802         if (name == NULL)
803             return 0;
804         PyUnicode_Append(&name, u->u_name);
805         if (name == NULL)
806             return 0;
807     }
808     else {
809         Py_INCREF(u->u_name);
810         name = u->u_name;
811     }
812     u->u_qualname = name;
813 
814     return 1;
815 }
816 
817 
818 /* Allocate a new block and return a pointer to it.
819    Returns NULL on error.
820 */
821 
822 static basicblock *
compiler_new_block(struct compiler * c)823 compiler_new_block(struct compiler *c)
824 {
825     basicblock *b;
826     struct compiler_unit *u;
827 
828     u = c->u;
829     b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
830     if (b == NULL) {
831         PyErr_NoMemory();
832         return NULL;
833     }
834     /* Extend the singly linked list of blocks with new block. */
835     b->b_list = u->u_blocks;
836     u->u_blocks = b;
837     return b;
838 }
839 
840 static basicblock *
compiler_next_block(struct compiler * c)841 compiler_next_block(struct compiler *c)
842 {
843     basicblock *block = compiler_new_block(c);
844     if (block == NULL)
845         return NULL;
846     c->u->u_curblock->b_next = block;
847     c->u->u_curblock = block;
848     return block;
849 }
850 
851 static basicblock *
compiler_use_next_block(struct compiler * c,basicblock * block)852 compiler_use_next_block(struct compiler *c, basicblock *block)
853 {
854     assert(block != NULL);
855     c->u->u_curblock->b_next = block;
856     c->u->u_curblock = block;
857     return block;
858 }
859 
860 static basicblock *
compiler_copy_block(struct compiler * c,basicblock * block)861 compiler_copy_block(struct compiler *c, basicblock *block)
862 {
863     /* Cannot copy a block if it has a fallthrough, since
864      * a block can only have one fallthrough predecessor.
865      */
866     assert(block->b_nofallthrough);
867     basicblock *result = compiler_new_block(c);
868     if (result == NULL) {
869         return NULL;
870     }
871     for (int i = 0; i < block->b_iused; i++) {
872         int n = compiler_next_instr(result);
873         if (n < 0) {
874             return NULL;
875         }
876         result->b_instr[n] = block->b_instr[i];
877     }
878     result->b_exit = block->b_exit;
879     result->b_nofallthrough = 1;
880     return result;
881 }
882 
883 /* Returns the offset of the next instruction in the current block's
884    b_instr array.  Resizes the b_instr as necessary.
885    Returns -1 on failure.
886 */
887 
888 static int
compiler_next_instr(basicblock * b)889 compiler_next_instr(basicblock *b)
890 {
891     assert(b != NULL);
892     if (b->b_instr == NULL) {
893         b->b_instr = (struct instr *)PyObject_Calloc(
894                          DEFAULT_BLOCK_SIZE, sizeof(struct instr));
895         if (b->b_instr == NULL) {
896             PyErr_NoMemory();
897             return -1;
898         }
899         b->b_ialloc = DEFAULT_BLOCK_SIZE;
900     }
901     else if (b->b_iused == b->b_ialloc) {
902         struct instr *tmp;
903         size_t oldsize, newsize;
904         oldsize = b->b_ialloc * sizeof(struct instr);
905         newsize = oldsize << 1;
906 
907         if (oldsize > (SIZE_MAX >> 1)) {
908             PyErr_NoMemory();
909             return -1;
910         }
911 
912         if (newsize == 0) {
913             PyErr_NoMemory();
914             return -1;
915         }
916         b->b_ialloc <<= 1;
917         tmp = (struct instr *)PyObject_Realloc(
918                                         (void *)b->b_instr, newsize);
919         if (tmp == NULL) {
920             PyErr_NoMemory();
921             return -1;
922         }
923         b->b_instr = tmp;
924         memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
925     }
926     return b->b_iused++;
927 }
928 
929 /* Set the line number and column offset for the following instructions.
930 
931    The line number is reset in the following cases:
932    - when entering a new scope
933    - on each statement
934    - on each expression and sub-expression
935    - before the "except" and "finally" clauses
936 */
937 
938 #define SET_LOC(c, x)                           \
939     (c)->u->u_lineno = (x)->lineno;             \
940     (c)->u->u_col_offset = (x)->col_offset;     \
941     (c)->u->u_end_lineno = (x)->end_lineno;     \
942     (c)->u->u_end_col_offset = (x)->end_col_offset;
943 
944 /* Return the stack effect of opcode with argument oparg.
945 
946    Some opcodes have different stack effect when jump to the target and
947    when not jump. The 'jump' parameter specifies the case:
948 
949    * 0 -- when not jump
950    * 1 -- when jump
951    * -1 -- maximal
952  */
953 static int
stack_effect(int opcode,int oparg,int jump)954 stack_effect(int opcode, int oparg, int jump)
955 {
956     switch (opcode) {
957         case NOP:
958         case EXTENDED_ARG:
959             return 0;
960 
961         /* Stack manipulation */
962         case POP_TOP:
963             return -1;
964         case ROT_TWO:
965         case ROT_THREE:
966         case ROT_FOUR:
967             return 0;
968         case DUP_TOP:
969             return 1;
970         case DUP_TOP_TWO:
971             return 2;
972 
973         /* Unary operators */
974         case UNARY_POSITIVE:
975         case UNARY_NEGATIVE:
976         case UNARY_NOT:
977         case UNARY_INVERT:
978             return 0;
979 
980         case SET_ADD:
981         case LIST_APPEND:
982             return -1;
983         case MAP_ADD:
984             return -2;
985 
986         /* Binary operators */
987         case BINARY_POWER:
988         case BINARY_MULTIPLY:
989         case BINARY_MATRIX_MULTIPLY:
990         case BINARY_MODULO:
991         case BINARY_ADD:
992         case BINARY_SUBTRACT:
993         case BINARY_SUBSCR:
994         case BINARY_FLOOR_DIVIDE:
995         case BINARY_TRUE_DIVIDE:
996             return -1;
997         case INPLACE_FLOOR_DIVIDE:
998         case INPLACE_TRUE_DIVIDE:
999             return -1;
1000 
1001         case INPLACE_ADD:
1002         case INPLACE_SUBTRACT:
1003         case INPLACE_MULTIPLY:
1004         case INPLACE_MATRIX_MULTIPLY:
1005         case INPLACE_MODULO:
1006             return -1;
1007         case STORE_SUBSCR:
1008             return -3;
1009         case DELETE_SUBSCR:
1010             return -2;
1011 
1012         case BINARY_LSHIFT:
1013         case BINARY_RSHIFT:
1014         case BINARY_AND:
1015         case BINARY_XOR:
1016         case BINARY_OR:
1017             return -1;
1018         case INPLACE_POWER:
1019             return -1;
1020         case GET_ITER:
1021             return 0;
1022 
1023         case PRINT_EXPR:
1024             return -1;
1025         case LOAD_BUILD_CLASS:
1026             return 1;
1027         case INPLACE_LSHIFT:
1028         case INPLACE_RSHIFT:
1029         case INPLACE_AND:
1030         case INPLACE_XOR:
1031         case INPLACE_OR:
1032             return -1;
1033 
1034         case SETUP_WITH:
1035             /* 1 in the normal flow.
1036              * Restore the stack position and push 6 values before jumping to
1037              * the handler if an exception be raised. */
1038             return jump ? 6 : 1;
1039         case RETURN_VALUE:
1040             return -1;
1041         case IMPORT_STAR:
1042             return -1;
1043         case SETUP_ANNOTATIONS:
1044             return 0;
1045         case YIELD_VALUE:
1046             return 0;
1047         case YIELD_FROM:
1048             return -1;
1049         case POP_BLOCK:
1050             return 0;
1051         case POP_EXCEPT:
1052             return -3;
1053 
1054         case STORE_NAME:
1055             return -1;
1056         case DELETE_NAME:
1057             return 0;
1058         case UNPACK_SEQUENCE:
1059             return oparg-1;
1060         case UNPACK_EX:
1061             return (oparg&0xFF) + (oparg>>8);
1062         case FOR_ITER:
1063             /* -1 at end of iterator, 1 if continue iterating. */
1064             return jump > 0 ? -1 : 1;
1065 
1066         case STORE_ATTR:
1067             return -2;
1068         case DELETE_ATTR:
1069             return -1;
1070         case STORE_GLOBAL:
1071             return -1;
1072         case DELETE_GLOBAL:
1073             return 0;
1074         case LOAD_CONST:
1075             return 1;
1076         case LOAD_NAME:
1077             return 1;
1078         case BUILD_TUPLE:
1079         case BUILD_LIST:
1080         case BUILD_SET:
1081         case BUILD_STRING:
1082             return 1-oparg;
1083         case BUILD_MAP:
1084             return 1 - 2*oparg;
1085         case BUILD_CONST_KEY_MAP:
1086             return -oparg;
1087         case LOAD_ATTR:
1088             return 0;
1089         case COMPARE_OP:
1090         case IS_OP:
1091         case CONTAINS_OP:
1092             return -1;
1093         case JUMP_IF_NOT_EXC_MATCH:
1094             return -2;
1095         case IMPORT_NAME:
1096             return -1;
1097         case IMPORT_FROM:
1098             return 1;
1099 
1100         /* Jumps */
1101         case JUMP_FORWARD:
1102         case JUMP_ABSOLUTE:
1103             return 0;
1104 
1105         case JUMP_IF_TRUE_OR_POP:
1106         case JUMP_IF_FALSE_OR_POP:
1107             return jump ? 0 : -1;
1108 
1109         case POP_JUMP_IF_FALSE:
1110         case POP_JUMP_IF_TRUE:
1111             return -1;
1112 
1113         case LOAD_GLOBAL:
1114             return 1;
1115 
1116         /* Exception handling */
1117         case SETUP_FINALLY:
1118             /* 0 in the normal flow.
1119              * Restore the stack position and push 6 values before jumping to
1120              * the handler if an exception be raised. */
1121             return jump ? 6 : 0;
1122         case RERAISE:
1123             return -3;
1124 
1125         case WITH_EXCEPT_START:
1126             return 1;
1127 
1128         case LOAD_FAST:
1129             return 1;
1130         case STORE_FAST:
1131             return -1;
1132         case DELETE_FAST:
1133             return 0;
1134 
1135         case RAISE_VARARGS:
1136             return -oparg;
1137 
1138         /* Functions and calls */
1139         case CALL_FUNCTION:
1140             return -oparg;
1141         case CALL_METHOD:
1142             return -oparg-1;
1143         case CALL_FUNCTION_KW:
1144             return -oparg-1;
1145         case CALL_FUNCTION_EX:
1146             return -1 - ((oparg & 0x01) != 0);
1147         case MAKE_FUNCTION:
1148             return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1149                 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1150         case BUILD_SLICE:
1151             if (oparg == 3)
1152                 return -2;
1153             else
1154                 return -1;
1155 
1156         /* Closures */
1157         case LOAD_CLOSURE:
1158             return 1;
1159         case LOAD_DEREF:
1160         case LOAD_CLASSDEREF:
1161             return 1;
1162         case STORE_DEREF:
1163             return -1;
1164         case DELETE_DEREF:
1165             return 0;
1166 
1167         /* Iterators and generators */
1168         case GET_AWAITABLE:
1169             return 0;
1170         case SETUP_ASYNC_WITH:
1171             /* 0 in the normal flow.
1172              * Restore the stack position to the position before the result
1173              * of __aenter__ and push 6 values before jumping to the handler
1174              * if an exception be raised. */
1175             return jump ? -1 + 6 : 0;
1176         case BEFORE_ASYNC_WITH:
1177             return 1;
1178         case GET_AITER:
1179             return 0;
1180         case GET_ANEXT:
1181             return 1;
1182         case GET_YIELD_FROM_ITER:
1183             return 0;
1184         case END_ASYNC_FOR:
1185             return -7;
1186         case FORMAT_VALUE:
1187             /* If there's a fmt_spec on the stack, we go from 2->1,
1188                else 1->1. */
1189             return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
1190         case LOAD_METHOD:
1191             return 1;
1192         case LOAD_ASSERTION_ERROR:
1193             return 1;
1194         case LIST_TO_TUPLE:
1195             return 0;
1196         case GEN_START:
1197             return -1;
1198         case LIST_EXTEND:
1199         case SET_UPDATE:
1200         case DICT_MERGE:
1201         case DICT_UPDATE:
1202             return -1;
1203         case COPY_DICT_WITHOUT_KEYS:
1204             return 0;
1205         case MATCH_CLASS:
1206             return -1;
1207         case GET_LEN:
1208         case MATCH_MAPPING:
1209         case MATCH_SEQUENCE:
1210             return 1;
1211         case MATCH_KEYS:
1212             return 2;
1213         case ROT_N:
1214             return 0;
1215         default:
1216             return PY_INVALID_STACK_EFFECT;
1217     }
1218     return PY_INVALID_STACK_EFFECT; /* not reachable */
1219 }
1220 
1221 int
PyCompile_OpcodeStackEffectWithJump(int opcode,int oparg,int jump)1222 PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1223 {
1224     return stack_effect(opcode, oparg, jump);
1225 }
1226 
1227 int
PyCompile_OpcodeStackEffect(int opcode,int oparg)1228 PyCompile_OpcodeStackEffect(int opcode, int oparg)
1229 {
1230     return stack_effect(opcode, oparg, -1);
1231 }
1232 
1233 /* Add an opcode with no argument.
1234    Returns 0 on failure, 1 on success.
1235 */
1236 
1237 static int
compiler_addop_line(struct compiler * c,int opcode,int line)1238 compiler_addop_line(struct compiler *c, int opcode, int line)
1239 {
1240     basicblock *b;
1241     struct instr *i;
1242     int off;
1243     assert(!HAS_ARG(opcode));
1244     off = compiler_next_instr(c->u->u_curblock);
1245     if (off < 0)
1246         return 0;
1247     b = c->u->u_curblock;
1248     i = &b->b_instr[off];
1249     i->i_opcode = opcode;
1250     i->i_oparg = 0;
1251     if (opcode == RETURN_VALUE)
1252         b->b_return = 1;
1253     i->i_lineno = line;
1254     return 1;
1255 }
1256 
1257 static int
compiler_addop(struct compiler * c,int opcode)1258 compiler_addop(struct compiler *c, int opcode)
1259 {
1260     return compiler_addop_line(c, opcode, c->u->u_lineno);
1261 }
1262 
1263 static int
compiler_addop_noline(struct compiler * c,int opcode)1264 compiler_addop_noline(struct compiler *c, int opcode)
1265 {
1266     return compiler_addop_line(c, opcode, -1);
1267 }
1268 
1269 
1270 static Py_ssize_t
compiler_add_o(PyObject * dict,PyObject * o)1271 compiler_add_o(PyObject *dict, PyObject *o)
1272 {
1273     PyObject *v;
1274     Py_ssize_t arg;
1275 
1276     v = PyDict_GetItemWithError(dict, o);
1277     if (!v) {
1278         if (PyErr_Occurred()) {
1279             return -1;
1280         }
1281         arg = PyDict_GET_SIZE(dict);
1282         v = PyLong_FromSsize_t(arg);
1283         if (!v) {
1284             return -1;
1285         }
1286         if (PyDict_SetItem(dict, o, v) < 0) {
1287             Py_DECREF(v);
1288             return -1;
1289         }
1290         Py_DECREF(v);
1291     }
1292     else
1293         arg = PyLong_AsLong(v);
1294     return arg;
1295 }
1296 
1297 // Merge const *o* recursively and return constant key object.
1298 static PyObject*
merge_consts_recursive(struct compiler * c,PyObject * o)1299 merge_consts_recursive(struct compiler *c, PyObject *o)
1300 {
1301     // None and Ellipsis are singleton, and key is the singleton.
1302     // No need to merge object and key.
1303     if (o == Py_None || o == Py_Ellipsis) {
1304         Py_INCREF(o);
1305         return o;
1306     }
1307 
1308     PyObject *key = _PyCode_ConstantKey(o);
1309     if (key == NULL) {
1310         return NULL;
1311     }
1312 
1313     // t is borrowed reference
1314     PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1315     if (t != key) {
1316         // o is registered in c_const_cache.  Just use it.
1317         Py_XINCREF(t);
1318         Py_DECREF(key);
1319         return t;
1320     }
1321 
1322     // We registered o in c_const_cache.
1323     // When o is a tuple or frozenset, we want to merge its
1324     // items too.
1325     if (PyTuple_CheckExact(o)) {
1326         Py_ssize_t len = PyTuple_GET_SIZE(o);
1327         for (Py_ssize_t i = 0; i < len; i++) {
1328             PyObject *item = PyTuple_GET_ITEM(o, i);
1329             PyObject *u = merge_consts_recursive(c, item);
1330             if (u == NULL) {
1331                 Py_DECREF(key);
1332                 return NULL;
1333             }
1334 
1335             // See _PyCode_ConstantKey()
1336             PyObject *v;  // borrowed
1337             if (PyTuple_CheckExact(u)) {
1338                 v = PyTuple_GET_ITEM(u, 1);
1339             }
1340             else {
1341                 v = u;
1342             }
1343             if (v != item) {
1344                 Py_INCREF(v);
1345                 PyTuple_SET_ITEM(o, i, v);
1346                 Py_DECREF(item);
1347             }
1348 
1349             Py_DECREF(u);
1350         }
1351     }
1352     else if (PyFrozenSet_CheckExact(o)) {
1353         // *key* is tuple. And its first item is frozenset of
1354         // constant keys.
1355         // See _PyCode_ConstantKey() for detail.
1356         assert(PyTuple_CheckExact(key));
1357         assert(PyTuple_GET_SIZE(key) == 2);
1358 
1359         Py_ssize_t len = PySet_GET_SIZE(o);
1360         if (len == 0) {  // empty frozenset should not be re-created.
1361             return key;
1362         }
1363         PyObject *tuple = PyTuple_New(len);
1364         if (tuple == NULL) {
1365             Py_DECREF(key);
1366             return NULL;
1367         }
1368         Py_ssize_t i = 0, pos = 0;
1369         PyObject *item;
1370         Py_hash_t hash;
1371         while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1372             PyObject *k = merge_consts_recursive(c, item);
1373             if (k == NULL) {
1374                 Py_DECREF(tuple);
1375                 Py_DECREF(key);
1376                 return NULL;
1377             }
1378             PyObject *u;
1379             if (PyTuple_CheckExact(k)) {
1380                 u = PyTuple_GET_ITEM(k, 1);
1381                 Py_INCREF(u);
1382                 Py_DECREF(k);
1383             }
1384             else {
1385                 u = k;
1386             }
1387             PyTuple_SET_ITEM(tuple, i, u);  // Steals reference of u.
1388             i++;
1389         }
1390 
1391         // Instead of rewriting o, we create new frozenset and embed in the
1392         // key tuple.  Caller should get merged frozenset from the key tuple.
1393         PyObject *new = PyFrozenSet_New(tuple);
1394         Py_DECREF(tuple);
1395         if (new == NULL) {
1396             Py_DECREF(key);
1397             return NULL;
1398         }
1399         assert(PyTuple_GET_ITEM(key, 1) == o);
1400         Py_DECREF(o);
1401         PyTuple_SET_ITEM(key, 1, new);
1402     }
1403 
1404     return key;
1405 }
1406 
1407 static Py_ssize_t
compiler_add_const(struct compiler * c,PyObject * o)1408 compiler_add_const(struct compiler *c, PyObject *o)
1409 {
1410     PyObject *key = merge_consts_recursive(c, o);
1411     if (key == NULL) {
1412         return -1;
1413     }
1414 
1415     Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
1416     Py_DECREF(key);
1417     return arg;
1418 }
1419 
1420 static int
compiler_addop_load_const(struct compiler * c,PyObject * o)1421 compiler_addop_load_const(struct compiler *c, PyObject *o)
1422 {
1423     Py_ssize_t arg = compiler_add_const(c, o);
1424     if (arg < 0)
1425         return 0;
1426     return compiler_addop_i(c, LOAD_CONST, arg);
1427 }
1428 
1429 static int
compiler_addop_o(struct compiler * c,int opcode,PyObject * dict,PyObject * o)1430 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1431                      PyObject *o)
1432 {
1433     Py_ssize_t arg = compiler_add_o(dict, o);
1434     if (arg < 0)
1435         return 0;
1436     return compiler_addop_i(c, opcode, arg);
1437 }
1438 
1439 static int
compiler_addop_name(struct compiler * c,int opcode,PyObject * dict,PyObject * o)1440 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1441                     PyObject *o)
1442 {
1443     Py_ssize_t arg;
1444 
1445     PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1446     if (!mangled)
1447         return 0;
1448     arg = compiler_add_o(dict, mangled);
1449     Py_DECREF(mangled);
1450     if (arg < 0)
1451         return 0;
1452     return compiler_addop_i(c, opcode, arg);
1453 }
1454 
1455 /* Add an opcode with an integer argument.
1456    Returns 0 on failure, 1 on success.
1457 */
1458 
1459 static int
compiler_addop_i_line(struct compiler * c,int opcode,Py_ssize_t oparg,int lineno)1460 compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno)
1461 {
1462     struct instr *i;
1463     int off;
1464 
1465     /* oparg value is unsigned, but a signed C int is usually used to store
1466        it in the C code (like Python/ceval.c).
1467 
1468        Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1469 
1470        The argument of a concrete bytecode instruction is limited to 8-bit.
1471        EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1472     assert(HAS_ARG(opcode));
1473     assert(0 <= oparg && oparg <= 2147483647);
1474 
1475     off = compiler_next_instr(c->u->u_curblock);
1476     if (off < 0)
1477         return 0;
1478     i = &c->u->u_curblock->b_instr[off];
1479     i->i_opcode = opcode;
1480     i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1481     i->i_lineno = lineno;
1482     return 1;
1483 }
1484 
1485 static int
compiler_addop_i(struct compiler * c,int opcode,Py_ssize_t oparg)1486 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1487 {
1488     return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno);
1489 }
1490 
1491 static int
compiler_addop_i_noline(struct compiler * c,int opcode,Py_ssize_t oparg)1492 compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg)
1493 {
1494     return compiler_addop_i_line(c, opcode, oparg, -1);
1495 }
1496 
add_jump_to_block(basicblock * b,int opcode,int lineno,basicblock * target)1497 static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target)
1498 {
1499     assert(HAS_ARG(opcode));
1500     assert(b != NULL);
1501     assert(target != NULL);
1502 
1503     int off = compiler_next_instr(b);
1504     struct instr *i = &b->b_instr[off];
1505     if (off < 0) {
1506         return 0;
1507     }
1508     i->i_opcode = opcode;
1509     i->i_target = target;
1510     i->i_lineno = lineno;
1511     return 1;
1512 }
1513 
1514 static int
compiler_addop_j(struct compiler * c,int opcode,basicblock * b)1515 compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
1516 {
1517     return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b);
1518 }
1519 
1520 static int
compiler_addop_j_noline(struct compiler * c,int opcode,basicblock * b)1521 compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
1522 {
1523     return add_jump_to_block(c->u->u_curblock, opcode, -1, b);
1524 }
1525 
1526 /* NEXT_BLOCK() creates an implicit jump from the current block
1527    to the new block.
1528 
1529    The returns inside this macro make it impossible to decref objects
1530    created in the local function. Local objects should use the arena.
1531 */
1532 #define NEXT_BLOCK(C) { \
1533     if (compiler_next_block((C)) == NULL) \
1534         return 0; \
1535 }
1536 
1537 #define ADDOP(C, OP) { \
1538     if (!compiler_addop((C), (OP))) \
1539         return 0; \
1540 }
1541 
1542 #define ADDOP_NOLINE(C, OP) { \
1543     if (!compiler_addop_noline((C), (OP))) \
1544         return 0; \
1545 }
1546 
1547 #define ADDOP_IN_SCOPE(C, OP) { \
1548     if (!compiler_addop((C), (OP))) { \
1549         compiler_exit_scope(c); \
1550         return 0; \
1551     } \
1552 }
1553 
1554 #define ADDOP_LOAD_CONST(C, O) { \
1555     if (!compiler_addop_load_const((C), (O))) \
1556         return 0; \
1557 }
1558 
1559 /* Same as ADDOP_LOAD_CONST, but steals a reference. */
1560 #define ADDOP_LOAD_CONST_NEW(C, O) { \
1561     PyObject *__new_const = (O); \
1562     if (__new_const == NULL) { \
1563         return 0; \
1564     } \
1565     if (!compiler_addop_load_const((C), __new_const)) { \
1566         Py_DECREF(__new_const); \
1567         return 0; \
1568     } \
1569     Py_DECREF(__new_const); \
1570 }
1571 
1572 #define ADDOP_O(C, OP, O, TYPE) { \
1573     assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST */ \
1574     if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1575         return 0; \
1576 }
1577 
1578 /* Same as ADDOP_O, but steals a reference. */
1579 #define ADDOP_N(C, OP, O, TYPE) { \
1580     assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST_NEW */ \
1581     if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1582         Py_DECREF((O)); \
1583         return 0; \
1584     } \
1585     Py_DECREF((O)); \
1586 }
1587 
1588 #define ADDOP_NAME(C, OP, O, TYPE) { \
1589     if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1590         return 0; \
1591 }
1592 
1593 #define ADDOP_I(C, OP, O) { \
1594     if (!compiler_addop_i((C), (OP), (O))) \
1595         return 0; \
1596 }
1597 
1598 #define ADDOP_I_NOLINE(C, OP, O) { \
1599     if (!compiler_addop_i_noline((C), (OP), (O))) \
1600         return 0; \
1601 }
1602 
1603 #define ADDOP_JUMP(C, OP, O) { \
1604     if (!compiler_addop_j((C), (OP), (O))) \
1605         return 0; \
1606 }
1607 
1608 /* Add a jump with no line number.
1609  * Used for artificial jumps that have no corresponding
1610  * token in the source code. */
1611 #define ADDOP_JUMP_NOLINE(C, OP, O) { \
1612     if (!compiler_addop_j_noline((C), (OP), (O))) \
1613         return 0; \
1614 }
1615 
1616 #define ADDOP_COMPARE(C, CMP) { \
1617     if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1618         return 0; \
1619 }
1620 
1621 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument.  They use
1622    the ASDL name to synthesize the name of the C type and the visit function.
1623 */
1624 
1625 #define VISIT(C, TYPE, V) {\
1626     if (!compiler_visit_ ## TYPE((C), (V))) \
1627         return 0; \
1628 }
1629 
1630 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1631     if (!compiler_visit_ ## TYPE((C), (V))) { \
1632         compiler_exit_scope(c); \
1633         return 0; \
1634     } \
1635 }
1636 
1637 #define VISIT_SLICE(C, V, CTX) {\
1638     if (!compiler_visit_slice((C), (V), (CTX))) \
1639         return 0; \
1640 }
1641 
1642 #define VISIT_SEQ(C, TYPE, SEQ) { \
1643     int _i; \
1644     asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1645     for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1646         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1647         if (!compiler_visit_ ## TYPE((C), elt)) \
1648             return 0; \
1649     } \
1650 }
1651 
1652 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1653     int _i; \
1654     asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1655     for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1656         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1657         if (!compiler_visit_ ## TYPE((C), elt)) { \
1658             compiler_exit_scope(c); \
1659             return 0; \
1660         } \
1661     } \
1662 }
1663 
1664 #define RETURN_IF_FALSE(X)  \
1665     if (!(X)) {             \
1666         return 0;           \
1667     }
1668 
1669 /* Search if variable annotations are present statically in a block. */
1670 
1671 static int
find_ann(asdl_stmt_seq * stmts)1672 find_ann(asdl_stmt_seq *stmts)
1673 {
1674     int i, j, res = 0;
1675     stmt_ty st;
1676 
1677     for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1678         st = (stmt_ty)asdl_seq_GET(stmts, i);
1679         switch (st->kind) {
1680         case AnnAssign_kind:
1681             return 1;
1682         case For_kind:
1683             res = find_ann(st->v.For.body) ||
1684                   find_ann(st->v.For.orelse);
1685             break;
1686         case AsyncFor_kind:
1687             res = find_ann(st->v.AsyncFor.body) ||
1688                   find_ann(st->v.AsyncFor.orelse);
1689             break;
1690         case While_kind:
1691             res = find_ann(st->v.While.body) ||
1692                   find_ann(st->v.While.orelse);
1693             break;
1694         case If_kind:
1695             res = find_ann(st->v.If.body) ||
1696                   find_ann(st->v.If.orelse);
1697             break;
1698         case With_kind:
1699             res = find_ann(st->v.With.body);
1700             break;
1701         case AsyncWith_kind:
1702             res = find_ann(st->v.AsyncWith.body);
1703             break;
1704         case Try_kind:
1705             for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1706                 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1707                     st->v.Try.handlers, j);
1708                 if (find_ann(handler->v.ExceptHandler.body)) {
1709                     return 1;
1710                 }
1711             }
1712             res = find_ann(st->v.Try.body) ||
1713                   find_ann(st->v.Try.finalbody) ||
1714                   find_ann(st->v.Try.orelse);
1715             break;
1716         default:
1717             res = 0;
1718         }
1719         if (res) {
1720             break;
1721         }
1722     }
1723     return res;
1724 }
1725 
1726 /*
1727  * Frame block handling functions
1728  */
1729 
1730 static int
compiler_push_fblock(struct compiler * c,enum fblocktype t,basicblock * b,basicblock * exit,void * datum)1731 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
1732                      basicblock *exit, void *datum)
1733 {
1734     struct fblockinfo *f;
1735     if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1736         return compiler_error(c, "too many statically nested blocks");
1737     }
1738     f = &c->u->u_fblock[c->u->u_nfblocks++];
1739     f->fb_type = t;
1740     f->fb_block = b;
1741     f->fb_exit = exit;
1742     f->fb_datum = datum;
1743     return 1;
1744 }
1745 
1746 static void
compiler_pop_fblock(struct compiler * c,enum fblocktype t,basicblock * b)1747 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1748 {
1749     struct compiler_unit *u = c->u;
1750     assert(u->u_nfblocks > 0);
1751     u->u_nfblocks--;
1752     assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1753     assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1754 }
1755 
1756 static int
compiler_call_exit_with_nones(struct compiler * c)1757 compiler_call_exit_with_nones(struct compiler *c) {
1758     ADDOP_LOAD_CONST(c, Py_None);
1759     ADDOP(c, DUP_TOP);
1760     ADDOP(c, DUP_TOP);
1761     ADDOP_I(c, CALL_FUNCTION, 3);
1762     return 1;
1763 }
1764 
1765 /* Unwind a frame block.  If preserve_tos is true, the TOS before
1766  * popping the blocks will be restored afterwards, unless another
1767  * return, break or continue is found. In which case, the TOS will
1768  * be popped.
1769  */
1770 static int
compiler_unwind_fblock(struct compiler * c,struct fblockinfo * info,int preserve_tos)1771 compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1772                        int preserve_tos)
1773 {
1774     switch (info->fb_type) {
1775         case WHILE_LOOP:
1776         case EXCEPTION_HANDLER:
1777         case ASYNC_COMPREHENSION_GENERATOR:
1778             return 1;
1779 
1780         case FOR_LOOP:
1781             /* Pop the iterator */
1782             if (preserve_tos) {
1783                 ADDOP(c, ROT_TWO);
1784             }
1785             ADDOP(c, POP_TOP);
1786             return 1;
1787 
1788         case TRY_EXCEPT:
1789             ADDOP(c, POP_BLOCK);
1790             return 1;
1791 
1792         case FINALLY_TRY:
1793             /* This POP_BLOCK gets the line number of the unwinding statement */
1794             ADDOP(c, POP_BLOCK);
1795             if (preserve_tos) {
1796                 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1797                     return 0;
1798                 }
1799             }
1800             /* Emit the finally block */
1801             VISIT_SEQ(c, stmt, info->fb_datum);
1802             if (preserve_tos) {
1803                 compiler_pop_fblock(c, POP_VALUE, NULL);
1804             }
1805             /* The finally block should appear to execute after the
1806              * statement causing the unwinding, so make the unwinding
1807              * instruction artificial */
1808             c->u->u_lineno = -1;
1809             return 1;
1810 
1811         case FINALLY_END:
1812             if (preserve_tos) {
1813                 ADDOP(c, ROT_FOUR);
1814             }
1815             ADDOP(c, POP_TOP);
1816             ADDOP(c, POP_TOP);
1817             ADDOP(c, POP_TOP);
1818             if (preserve_tos) {
1819                 ADDOP(c, ROT_FOUR);
1820             }
1821             ADDOP(c, POP_EXCEPT);
1822             return 1;
1823 
1824         case WITH:
1825         case ASYNC_WITH:
1826             SET_LOC(c, (stmt_ty)info->fb_datum);
1827             ADDOP(c, POP_BLOCK);
1828             if (preserve_tos) {
1829                 ADDOP(c, ROT_TWO);
1830             }
1831             if(!compiler_call_exit_with_nones(c)) {
1832                 return 0;
1833             }
1834             if (info->fb_type == ASYNC_WITH) {
1835                 ADDOP(c, GET_AWAITABLE);
1836                 ADDOP_LOAD_CONST(c, Py_None);
1837                 ADDOP(c, YIELD_FROM);
1838             }
1839             ADDOP(c, POP_TOP);
1840             /* The exit block should appear to execute after the
1841              * statement causing the unwinding, so make the unwinding
1842              * instruction artificial */
1843             c->u->u_lineno = -1;
1844             return 1;
1845 
1846         case HANDLER_CLEANUP:
1847             if (info->fb_datum) {
1848                 ADDOP(c, POP_BLOCK);
1849             }
1850             if (preserve_tos) {
1851                 ADDOP(c, ROT_FOUR);
1852             }
1853             ADDOP(c, POP_EXCEPT);
1854             if (info->fb_datum) {
1855                 ADDOP_LOAD_CONST(c, Py_None);
1856                 compiler_nameop(c, info->fb_datum, Store);
1857                 compiler_nameop(c, info->fb_datum, Del);
1858             }
1859             return 1;
1860 
1861         case POP_VALUE:
1862             if (preserve_tos) {
1863                 ADDOP(c, ROT_TWO);
1864             }
1865             ADDOP(c, POP_TOP);
1866             return 1;
1867     }
1868     Py_UNREACHABLE();
1869 }
1870 
1871 /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1872 static int
compiler_unwind_fblock_stack(struct compiler * c,int preserve_tos,struct fblockinfo ** loop)1873 compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1874     if (c->u->u_nfblocks == 0) {
1875         return 1;
1876     }
1877     struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1878     if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1879         *loop = top;
1880         return 1;
1881     }
1882     struct fblockinfo copy = *top;
1883     c->u->u_nfblocks--;
1884     if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1885         return 0;
1886     }
1887     if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1888         return 0;
1889     }
1890     c->u->u_fblock[c->u->u_nfblocks] = copy;
1891     c->u->u_nfblocks++;
1892     return 1;
1893 }
1894 
1895 /* Compile a sequence of statements, checking for a docstring
1896    and for annotations. */
1897 
1898 static int
compiler_body(struct compiler * c,asdl_stmt_seq * stmts)1899 compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
1900 {
1901     int i = 0;
1902     stmt_ty st;
1903     PyObject *docstring;
1904 
1905     /* Set current line number to the line number of first statement.
1906        This way line number for SETUP_ANNOTATIONS will always
1907        coincide with the line number of first "real" statement in module.
1908        If body is empty, then lineno will be set later in assemble. */
1909     if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
1910         st = (stmt_ty)asdl_seq_GET(stmts, 0);
1911         SET_LOC(c, st);
1912     }
1913     /* Every annotated class and module should have __annotations__. */
1914     if (find_ann(stmts)) {
1915         ADDOP(c, SETUP_ANNOTATIONS);
1916     }
1917     if (!asdl_seq_LEN(stmts))
1918         return 1;
1919     /* if not -OO mode, set docstring */
1920     if (c->c_optimize < 2) {
1921         docstring = _PyAST_GetDocString(stmts);
1922         if (docstring) {
1923             i = 1;
1924             st = (stmt_ty)asdl_seq_GET(stmts, 0);
1925             assert(st->kind == Expr_kind);
1926             VISIT(c, expr, st->v.Expr.value);
1927             if (!compiler_nameop(c, __doc__, Store))
1928                 return 0;
1929         }
1930     }
1931     for (; i < asdl_seq_LEN(stmts); i++)
1932         VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1933     return 1;
1934 }
1935 
1936 static PyCodeObject *
compiler_mod(struct compiler * c,mod_ty mod)1937 compiler_mod(struct compiler *c, mod_ty mod)
1938 {
1939     PyCodeObject *co;
1940     int addNone = 1;
1941     static PyObject *module;
1942     if (!module) {
1943         module = PyUnicode_InternFromString("<module>");
1944         if (!module)
1945             return NULL;
1946     }
1947     /* Use 0 for firstlineno initially, will fixup in assemble(). */
1948     if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
1949         return NULL;
1950     switch (mod->kind) {
1951     case Module_kind:
1952         if (!compiler_body(c, mod->v.Module.body)) {
1953             compiler_exit_scope(c);
1954             return 0;
1955         }
1956         break;
1957     case Interactive_kind:
1958         if (find_ann(mod->v.Interactive.body)) {
1959             ADDOP(c, SETUP_ANNOTATIONS);
1960         }
1961         c->c_interactive = 1;
1962         VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
1963         break;
1964     case Expression_kind:
1965         VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1966         addNone = 0;
1967         break;
1968     default:
1969         PyErr_Format(PyExc_SystemError,
1970                      "module kind %d should not be possible",
1971                      mod->kind);
1972         return 0;
1973     }
1974     co = assemble(c, addNone);
1975     compiler_exit_scope(c);
1976     return co;
1977 }
1978 
1979 /* The test for LOCAL must come before the test for FREE in order to
1980    handle classes where name is both local and free.  The local var is
1981    a method and the free var is a free var referenced within a method.
1982 */
1983 
1984 static int
get_ref_type(struct compiler * c,PyObject * name)1985 get_ref_type(struct compiler *c, PyObject *name)
1986 {
1987     int scope;
1988     if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1989         _PyUnicode_EqualToASCIIString(name, "__class__"))
1990         return CELL;
1991     scope = _PyST_GetScope(c->u->u_ste, name);
1992     if (scope == 0) {
1993         PyErr_Format(PyExc_SystemError,
1994                      "_PyST_GetScope(name=%R) failed: "
1995                      "unknown scope in unit %S (%R); "
1996                      "symbols: %R; locals: %R; globals: %R",
1997                      name,
1998                      c->u->u_name, c->u->u_ste->ste_id,
1999                      c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
2000         return -1;
2001     }
2002     return scope;
2003 }
2004 
2005 static int
compiler_lookup_arg(PyObject * dict,PyObject * name)2006 compiler_lookup_arg(PyObject *dict, PyObject *name)
2007 {
2008     PyObject *v;
2009     v = PyDict_GetItemWithError(dict, name);
2010     if (v == NULL)
2011         return -1;
2012     return PyLong_AS_LONG(v);
2013 }
2014 
2015 static int
compiler_make_closure(struct compiler * c,PyCodeObject * co,Py_ssize_t flags,PyObject * qualname)2016 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
2017                       PyObject *qualname)
2018 {
2019     Py_ssize_t i, free = PyCode_GetNumFree(co);
2020     if (qualname == NULL)
2021         qualname = co->co_name;
2022 
2023     if (free) {
2024         for (i = 0; i < free; ++i) {
2025             /* Bypass com_addop_varname because it will generate
2026                LOAD_DEREF but LOAD_CLOSURE is needed.
2027             */
2028             PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2029 
2030             /* Special case: If a class contains a method with a
2031                free variable that has the same name as a method,
2032                the name will be considered free *and* local in the
2033                class.  It should be handled by the closure, as
2034                well as by the normal name lookup logic.
2035             */
2036             int reftype = get_ref_type(c, name);
2037             if (reftype == -1) {
2038                 return 0;
2039             }
2040             int arg;
2041             if (reftype == CELL) {
2042                 arg = compiler_lookup_arg(c->u->u_cellvars, name);
2043             }
2044             else {
2045                 arg = compiler_lookup_arg(c->u->u_freevars, name);
2046             }
2047             if (arg == -1) {
2048                 PyErr_Format(PyExc_SystemError,
2049                     "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2050                     "freevars of code %S: %R",
2051                     name,
2052                     reftype,
2053                     c->u->u_name,
2054                     co->co_name,
2055                     co->co_freevars);
2056                 return 0;
2057             }
2058             ADDOP_I(c, LOAD_CLOSURE, arg);
2059         }
2060         flags |= 0x08;
2061         ADDOP_I(c, BUILD_TUPLE, free);
2062     }
2063     ADDOP_LOAD_CONST(c, (PyObject*)co);
2064     ADDOP_LOAD_CONST(c, qualname);
2065     ADDOP_I(c, MAKE_FUNCTION, flags);
2066     return 1;
2067 }
2068 
2069 static int
compiler_decorators(struct compiler * c,asdl_expr_seq * decos)2070 compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
2071 {
2072     int i;
2073 
2074     if (!decos)
2075         return 1;
2076 
2077     for (i = 0; i < asdl_seq_LEN(decos); i++) {
2078         VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2079     }
2080     return 1;
2081 }
2082 
2083 static int
compiler_visit_kwonlydefaults(struct compiler * c,asdl_arg_seq * kwonlyargs,asdl_expr_seq * kw_defaults)2084 compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2085                               asdl_expr_seq *kw_defaults)
2086 {
2087     /* Push a dict of keyword-only default values.
2088 
2089        Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2090        */
2091     int i;
2092     PyObject *keys = NULL;
2093 
2094     for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2095         arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2096         expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2097         if (default_) {
2098             PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
2099             if (!mangled) {
2100                 goto error;
2101             }
2102             if (keys == NULL) {
2103                 keys = PyList_New(1);
2104                 if (keys == NULL) {
2105                     Py_DECREF(mangled);
2106                     return 0;
2107                 }
2108                 PyList_SET_ITEM(keys, 0, mangled);
2109             }
2110             else {
2111                 int res = PyList_Append(keys, mangled);
2112                 Py_DECREF(mangled);
2113                 if (res == -1) {
2114                     goto error;
2115                 }
2116             }
2117             if (!compiler_visit_expr(c, default_)) {
2118                 goto error;
2119             }
2120         }
2121     }
2122     if (keys != NULL) {
2123         Py_ssize_t default_count = PyList_GET_SIZE(keys);
2124         PyObject *keys_tuple = PyList_AsTuple(keys);
2125         Py_DECREF(keys);
2126         ADDOP_LOAD_CONST_NEW(c, keys_tuple);
2127         ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
2128         assert(default_count > 0);
2129         return 1;
2130     }
2131     else {
2132         return -1;
2133     }
2134 
2135 error:
2136     Py_XDECREF(keys);
2137     return 0;
2138 }
2139 
2140 static int
compiler_visit_annexpr(struct compiler * c,expr_ty annotation)2141 compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2142 {
2143     ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
2144     return 1;
2145 }
2146 
2147 static int
compiler_visit_argannotation(struct compiler * c,identifier id,expr_ty annotation,Py_ssize_t * annotations_len)2148 compiler_visit_argannotation(struct compiler *c, identifier id,
2149     expr_ty annotation, Py_ssize_t *annotations_len)
2150 {
2151     if (!annotation) {
2152         return 1;
2153     }
2154 
2155     PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2156     if (!mangled) {
2157         return 0;
2158     }
2159     ADDOP_LOAD_CONST(c, mangled);
2160     Py_DECREF(mangled);
2161 
2162     if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2163         VISIT(c, annexpr, annotation)
2164     }
2165     else {
2166         VISIT(c, expr, annotation);
2167     }
2168     *annotations_len += 2;
2169     return 1;
2170 }
2171 
2172 static int
compiler_visit_argannotations(struct compiler * c,asdl_arg_seq * args,Py_ssize_t * annotations_len)2173 compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
2174                               Py_ssize_t *annotations_len)
2175 {
2176     int i;
2177     for (i = 0; i < asdl_seq_LEN(args); i++) {
2178         arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
2179         if (!compiler_visit_argannotation(
2180                         c,
2181                         arg->arg,
2182                         arg->annotation,
2183                         annotations_len))
2184             return 0;
2185     }
2186     return 1;
2187 }
2188 
2189 static int
compiler_visit_annotations(struct compiler * c,arguments_ty args,expr_ty returns)2190 compiler_visit_annotations(struct compiler *c, arguments_ty args,
2191                            expr_ty returns)
2192 {
2193     /* Push arg annotation names and values.
2194        The expressions are evaluated out-of-order wrt the source code.
2195 
2196        Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
2197        */
2198     static identifier return_str;
2199     Py_ssize_t annotations_len = 0;
2200 
2201     if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2202         return 0;
2203     if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2204         return 0;
2205     if (args->vararg && args->vararg->annotation &&
2206         !compiler_visit_argannotation(c, args->vararg->arg,
2207                                      args->vararg->annotation, &annotations_len))
2208         return 0;
2209     if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2210         return 0;
2211     if (args->kwarg && args->kwarg->annotation &&
2212         !compiler_visit_argannotation(c, args->kwarg->arg,
2213                                      args->kwarg->annotation, &annotations_len))
2214         return 0;
2215 
2216     if (!return_str) {
2217         return_str = PyUnicode_InternFromString("return");
2218         if (!return_str)
2219             return 0;
2220     }
2221     if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
2222         return 0;
2223     }
2224 
2225     if (annotations_len) {
2226         ADDOP_I(c, BUILD_TUPLE, annotations_len);
2227         return 1;
2228     }
2229 
2230     return -1;
2231 }
2232 
2233 static int
compiler_visit_defaults(struct compiler * c,arguments_ty args)2234 compiler_visit_defaults(struct compiler *c, arguments_ty args)
2235 {
2236     VISIT_SEQ(c, expr, args->defaults);
2237     ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2238     return 1;
2239 }
2240 
2241 static Py_ssize_t
compiler_default_arguments(struct compiler * c,arguments_ty args)2242 compiler_default_arguments(struct compiler *c, arguments_ty args)
2243 {
2244     Py_ssize_t funcflags = 0;
2245     if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
2246         if (!compiler_visit_defaults(c, args))
2247             return -1;
2248         funcflags |= 0x01;
2249     }
2250     if (args->kwonlyargs) {
2251         int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
2252                                                 args->kw_defaults);
2253         if (res == 0) {
2254             return -1;
2255         }
2256         else if (res > 0) {
2257             funcflags |= 0x02;
2258         }
2259     }
2260     return funcflags;
2261 }
2262 
2263 static int
forbidden_name(struct compiler * c,identifier name,expr_context_ty ctx)2264 forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2265 {
2266 
2267     if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2268         compiler_error(c, "cannot assign to __debug__");
2269         return 1;
2270     }
2271     if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2272         compiler_error(c, "cannot delete __debug__");
2273         return 1;
2274     }
2275     return 0;
2276 }
2277 
2278 static int
compiler_check_debug_one_arg(struct compiler * c,arg_ty arg)2279 compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2280 {
2281     if (arg != NULL) {
2282         if (forbidden_name(c, arg->arg, Store))
2283             return 0;
2284     }
2285     return 1;
2286 }
2287 
2288 static int
compiler_check_debug_args_seq(struct compiler * c,asdl_arg_seq * args)2289 compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
2290 {
2291     if (args != NULL) {
2292         for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
2293             if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2294                 return 0;
2295         }
2296     }
2297     return 1;
2298 }
2299 
2300 static int
compiler_check_debug_args(struct compiler * c,arguments_ty args)2301 compiler_check_debug_args(struct compiler *c, arguments_ty args)
2302 {
2303     if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2304         return 0;
2305     if (!compiler_check_debug_args_seq(c, args->args))
2306         return 0;
2307     if (!compiler_check_debug_one_arg(c, args->vararg))
2308         return 0;
2309     if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2310         return 0;
2311     if (!compiler_check_debug_one_arg(c, args->kwarg))
2312         return 0;
2313     return 1;
2314 }
2315 
2316 static int
compiler_function(struct compiler * c,stmt_ty s,int is_async)2317 compiler_function(struct compiler *c, stmt_ty s, int is_async)
2318 {
2319     PyCodeObject *co;
2320     PyObject *qualname, *docstring = NULL;
2321     arguments_ty args;
2322     expr_ty returns;
2323     identifier name;
2324     asdl_expr_seq* decos;
2325     asdl_stmt_seq *body;
2326     Py_ssize_t i, funcflags;
2327     int annotations;
2328     int scope_type;
2329     int firstlineno;
2330 
2331     if (is_async) {
2332         assert(s->kind == AsyncFunctionDef_kind);
2333 
2334         args = s->v.AsyncFunctionDef.args;
2335         returns = s->v.AsyncFunctionDef.returns;
2336         decos = s->v.AsyncFunctionDef.decorator_list;
2337         name = s->v.AsyncFunctionDef.name;
2338         body = s->v.AsyncFunctionDef.body;
2339 
2340         scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2341     } else {
2342         assert(s->kind == FunctionDef_kind);
2343 
2344         args = s->v.FunctionDef.args;
2345         returns = s->v.FunctionDef.returns;
2346         decos = s->v.FunctionDef.decorator_list;
2347         name = s->v.FunctionDef.name;
2348         body = s->v.FunctionDef.body;
2349 
2350         scope_type = COMPILER_SCOPE_FUNCTION;
2351     }
2352 
2353     if (!compiler_check_debug_args(c, args))
2354         return 0;
2355 
2356     if (!compiler_decorators(c, decos))
2357         return 0;
2358 
2359     firstlineno = s->lineno;
2360     if (asdl_seq_LEN(decos)) {
2361         firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2362     }
2363 
2364     funcflags = compiler_default_arguments(c, args);
2365     if (funcflags == -1) {
2366         return 0;
2367     }
2368 
2369     annotations = compiler_visit_annotations(c, args, returns);
2370     if (annotations == 0) {
2371         return 0;
2372     }
2373     else if (annotations > 0) {
2374         funcflags |= 0x04;
2375     }
2376 
2377     if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
2378         return 0;
2379     }
2380 
2381     /* if not -OO mode, add docstring */
2382     if (c->c_optimize < 2) {
2383         docstring = _PyAST_GetDocString(body);
2384     }
2385     if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
2386         compiler_exit_scope(c);
2387         return 0;
2388     }
2389 
2390     c->u->u_argcount = asdl_seq_LEN(args->args);
2391     c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2392     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2393     for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
2394         VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
2395     }
2396     co = assemble(c, 1);
2397     qualname = c->u->u_qualname;
2398     Py_INCREF(qualname);
2399     compiler_exit_scope(c);
2400     if (co == NULL) {
2401         Py_XDECREF(qualname);
2402         Py_XDECREF(co);
2403         return 0;
2404     }
2405 
2406     if (!compiler_make_closure(c, co, funcflags, qualname)) {
2407         Py_DECREF(qualname);
2408         Py_DECREF(co);
2409         return 0;
2410     }
2411     Py_DECREF(qualname);
2412     Py_DECREF(co);
2413 
2414     /* decorators */
2415     for (i = 0; i < asdl_seq_LEN(decos); i++) {
2416         ADDOP_I(c, CALL_FUNCTION, 1);
2417     }
2418 
2419     return compiler_nameop(c, name, Store);
2420 }
2421 
2422 static int
compiler_class(struct compiler * c,stmt_ty s)2423 compiler_class(struct compiler *c, stmt_ty s)
2424 {
2425     PyCodeObject *co;
2426     PyObject *str;
2427     int i, firstlineno;
2428     asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
2429 
2430     if (!compiler_decorators(c, decos))
2431         return 0;
2432 
2433     firstlineno = s->lineno;
2434     if (asdl_seq_LEN(decos)) {
2435         firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2436     }
2437 
2438     /* ultimately generate code for:
2439          <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2440        where:
2441          <func> is a function/closure created from the class body;
2442             it has a single argument (__locals__) where the dict
2443             (or MutableSequence) representing the locals is passed
2444          <name> is the class name
2445          <bases> is the positional arguments and *varargs argument
2446          <keywords> is the keyword arguments and **kwds argument
2447        This borrows from compiler_call.
2448     */
2449 
2450     /* 1. compile the class body into a code object */
2451     if (!compiler_enter_scope(c, s->v.ClassDef.name,
2452                               COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
2453         return 0;
2454     /* this block represents what we do in the new scope */
2455     {
2456         /* use the class name for name mangling */
2457         Py_INCREF(s->v.ClassDef.name);
2458         Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
2459         /* load (global) __name__ ... */
2460         str = PyUnicode_InternFromString("__name__");
2461         if (!str || !compiler_nameop(c, str, Load)) {
2462             Py_XDECREF(str);
2463             compiler_exit_scope(c);
2464             return 0;
2465         }
2466         Py_DECREF(str);
2467         /* ... and store it as __module__ */
2468         str = PyUnicode_InternFromString("__module__");
2469         if (!str || !compiler_nameop(c, str, Store)) {
2470             Py_XDECREF(str);
2471             compiler_exit_scope(c);
2472             return 0;
2473         }
2474         Py_DECREF(str);
2475         assert(c->u->u_qualname);
2476         ADDOP_LOAD_CONST(c, c->u->u_qualname);
2477         str = PyUnicode_InternFromString("__qualname__");
2478         if (!str || !compiler_nameop(c, str, Store)) {
2479             Py_XDECREF(str);
2480             compiler_exit_scope(c);
2481             return 0;
2482         }
2483         Py_DECREF(str);
2484         /* compile the body proper */
2485         if (!compiler_body(c, s->v.ClassDef.body)) {
2486             compiler_exit_scope(c);
2487             return 0;
2488         }
2489         /* The following code is artificial */
2490         c->u->u_lineno = -1;
2491         /* Return __classcell__ if it is referenced, otherwise return None */
2492         if (c->u->u_ste->ste_needs_class_closure) {
2493             /* Store __classcell__ into class namespace & return it */
2494             str = PyUnicode_InternFromString("__class__");
2495             if (str == NULL) {
2496                 compiler_exit_scope(c);
2497                 return 0;
2498             }
2499             i = compiler_lookup_arg(c->u->u_cellvars, str);
2500             Py_DECREF(str);
2501             if (i < 0) {
2502                 compiler_exit_scope(c);
2503                 return 0;
2504             }
2505             assert(i == 0);
2506 
2507             ADDOP_I(c, LOAD_CLOSURE, i);
2508             ADDOP(c, DUP_TOP);
2509             str = PyUnicode_InternFromString("__classcell__");
2510             if (!str || !compiler_nameop(c, str, Store)) {
2511                 Py_XDECREF(str);
2512                 compiler_exit_scope(c);
2513                 return 0;
2514             }
2515             Py_DECREF(str);
2516         }
2517         else {
2518             /* No methods referenced __class__, so just return None */
2519             assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
2520             ADDOP_LOAD_CONST(c, Py_None);
2521         }
2522         ADDOP_IN_SCOPE(c, RETURN_VALUE);
2523         /* create the code object */
2524         co = assemble(c, 1);
2525     }
2526     /* leave the new scope */
2527     compiler_exit_scope(c);
2528     if (co == NULL)
2529         return 0;
2530 
2531     /* 2. load the 'build_class' function */
2532     ADDOP(c, LOAD_BUILD_CLASS);
2533 
2534     /* 3. load a function (or closure) made from the code object */
2535     if (!compiler_make_closure(c, co, 0, NULL)) {
2536         Py_DECREF(co);
2537         return 0;
2538     }
2539     Py_DECREF(co);
2540 
2541     /* 4. load class name */
2542     ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
2543 
2544     /* 5. generate the rest of the code for the call */
2545     if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
2546         return 0;
2547 
2548     /* 6. apply decorators */
2549     for (i = 0; i < asdl_seq_LEN(decos); i++) {
2550         ADDOP_I(c, CALL_FUNCTION, 1);
2551     }
2552 
2553     /* 7. store into <name> */
2554     if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2555         return 0;
2556     return 1;
2557 }
2558 
2559 /* Return 0 if the expression is a constant value except named singletons.
2560    Return 1 otherwise. */
2561 static int
check_is_arg(expr_ty e)2562 check_is_arg(expr_ty e)
2563 {
2564     if (e->kind != Constant_kind) {
2565         return 1;
2566     }
2567     PyObject *value = e->v.Constant.value;
2568     return (value == Py_None
2569          || value == Py_False
2570          || value == Py_True
2571          || value == Py_Ellipsis);
2572 }
2573 
2574 /* Check operands of identity chacks ("is" and "is not").
2575    Emit a warning if any operand is a constant except named singletons.
2576    Return 0 on error.
2577  */
2578 static int
check_compare(struct compiler * c,expr_ty e)2579 check_compare(struct compiler *c, expr_ty e)
2580 {
2581     Py_ssize_t i, n;
2582     int left = check_is_arg(e->v.Compare.left);
2583     n = asdl_seq_LEN(e->v.Compare.ops);
2584     for (i = 0; i < n; i++) {
2585         cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2586         int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2587         if (op == Is || op == IsNot) {
2588             if (!right || !left) {
2589                 const char *msg = (op == Is)
2590                         ? "\"is\" with a literal. Did you mean \"==\"?"
2591                         : "\"is not\" with a literal. Did you mean \"!=\"?";
2592                 return compiler_warn(c, msg);
2593             }
2594         }
2595         left = right;
2596     }
2597     return 1;
2598 }
2599 
compiler_addcompare(struct compiler * c,cmpop_ty op)2600 static int compiler_addcompare(struct compiler *c, cmpop_ty op)
2601 {
2602     int cmp;
2603     switch (op) {
2604     case Eq:
2605         cmp = Py_EQ;
2606         break;
2607     case NotEq:
2608         cmp = Py_NE;
2609         break;
2610     case Lt:
2611         cmp = Py_LT;
2612         break;
2613     case LtE:
2614         cmp = Py_LE;
2615         break;
2616     case Gt:
2617         cmp = Py_GT;
2618         break;
2619     case GtE:
2620         cmp = Py_GE;
2621         break;
2622     case Is:
2623         ADDOP_I(c, IS_OP, 0);
2624         return 1;
2625     case IsNot:
2626         ADDOP_I(c, IS_OP, 1);
2627         return 1;
2628     case In:
2629         ADDOP_I(c, CONTAINS_OP, 0);
2630         return 1;
2631     case NotIn:
2632         ADDOP_I(c, CONTAINS_OP, 1);
2633         return 1;
2634     default:
2635         Py_UNREACHABLE();
2636     }
2637     ADDOP_I(c, COMPARE_OP, cmp);
2638     return 1;
2639 }
2640 
2641 
2642 
2643 static int
compiler_jump_if(struct compiler * c,expr_ty e,basicblock * next,int cond)2644 compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2645 {
2646     switch (e->kind) {
2647     case UnaryOp_kind:
2648         if (e->v.UnaryOp.op == Not)
2649             return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2650         /* fallback to general implementation */
2651         break;
2652     case BoolOp_kind: {
2653         asdl_expr_seq *s = e->v.BoolOp.values;
2654         Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2655         assert(n >= 0);
2656         int cond2 = e->v.BoolOp.op == Or;
2657         basicblock *next2 = next;
2658         if (!cond2 != !cond) {
2659             next2 = compiler_new_block(c);
2660             if (next2 == NULL)
2661                 return 0;
2662         }
2663         for (i = 0; i < n; ++i) {
2664             if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2665                 return 0;
2666         }
2667         if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2668             return 0;
2669         if (next2 != next)
2670             compiler_use_next_block(c, next2);
2671         return 1;
2672     }
2673     case IfExp_kind: {
2674         basicblock *end, *next2;
2675         end = compiler_new_block(c);
2676         if (end == NULL)
2677             return 0;
2678         next2 = compiler_new_block(c);
2679         if (next2 == NULL)
2680             return 0;
2681         if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2682             return 0;
2683         if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2684             return 0;
2685         ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
2686         compiler_use_next_block(c, next2);
2687         if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2688             return 0;
2689         compiler_use_next_block(c, end);
2690         return 1;
2691     }
2692     case Compare_kind: {
2693         Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2694         if (n > 0) {
2695             if (!check_compare(c, e)) {
2696                 return 0;
2697             }
2698             basicblock *cleanup = compiler_new_block(c);
2699             if (cleanup == NULL)
2700                 return 0;
2701             VISIT(c, expr, e->v.Compare.left);
2702             for (i = 0; i < n; i++) {
2703                 VISIT(c, expr,
2704                     (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2705                 ADDOP(c, DUP_TOP);
2706                 ADDOP(c, ROT_THREE);
2707                 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
2708                 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
2709                 NEXT_BLOCK(c);
2710             }
2711             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2712             ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
2713             ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2714             NEXT_BLOCK(c);
2715             basicblock *end = compiler_new_block(c);
2716             if (end == NULL)
2717                 return 0;
2718             ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
2719             compiler_use_next_block(c, cleanup);
2720             ADDOP(c, POP_TOP);
2721             if (!cond) {
2722                 ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next);
2723             }
2724             compiler_use_next_block(c, end);
2725             return 1;
2726         }
2727         /* fallback to general implementation */
2728         break;
2729     }
2730     default:
2731         /* fallback to general implementation */
2732         break;
2733     }
2734 
2735     /* general implementation */
2736     VISIT(c, expr, e);
2737     ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2738     NEXT_BLOCK(c);
2739     return 1;
2740 }
2741 
2742 static int
compiler_ifexp(struct compiler * c,expr_ty e)2743 compiler_ifexp(struct compiler *c, expr_ty e)
2744 {
2745     basicblock *end, *next;
2746 
2747     assert(e->kind == IfExp_kind);
2748     end = compiler_new_block(c);
2749     if (end == NULL)
2750         return 0;
2751     next = compiler_new_block(c);
2752     if (next == NULL)
2753         return 0;
2754     if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2755         return 0;
2756     VISIT(c, expr, e->v.IfExp.body);
2757     ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
2758     compiler_use_next_block(c, next);
2759     VISIT(c, expr, e->v.IfExp.orelse);
2760     compiler_use_next_block(c, end);
2761     return 1;
2762 }
2763 
2764 static int
compiler_lambda(struct compiler * c,expr_ty e)2765 compiler_lambda(struct compiler *c, expr_ty e)
2766 {
2767     PyCodeObject *co;
2768     PyObject *qualname;
2769     static identifier name;
2770     Py_ssize_t funcflags;
2771     arguments_ty args = e->v.Lambda.args;
2772     assert(e->kind == Lambda_kind);
2773 
2774     if (!compiler_check_debug_args(c, args))
2775         return 0;
2776 
2777     if (!name) {
2778         name = PyUnicode_InternFromString("<lambda>");
2779         if (!name)
2780             return 0;
2781     }
2782 
2783     funcflags = compiler_default_arguments(c, args);
2784     if (funcflags == -1) {
2785         return 0;
2786     }
2787 
2788     if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
2789                               (void *)e, e->lineno))
2790         return 0;
2791 
2792     /* Make None the first constant, so the lambda can't have a
2793        docstring. */
2794     if (compiler_add_const(c, Py_None) < 0)
2795         return 0;
2796 
2797     c->u->u_argcount = asdl_seq_LEN(args->args);
2798     c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2799     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2800     VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2801     if (c->u->u_ste->ste_generator) {
2802         co = assemble(c, 0);
2803     }
2804     else {
2805         ADDOP_IN_SCOPE(c, RETURN_VALUE);
2806         co = assemble(c, 1);
2807     }
2808     qualname = c->u->u_qualname;
2809     Py_INCREF(qualname);
2810     compiler_exit_scope(c);
2811     if (co == NULL) {
2812         Py_DECREF(qualname);
2813         return 0;
2814     }
2815 
2816     if (!compiler_make_closure(c, co, funcflags, qualname)) {
2817         Py_DECREF(qualname);
2818         Py_DECREF(co);
2819         return 0;
2820     }
2821     Py_DECREF(qualname);
2822     Py_DECREF(co);
2823 
2824     return 1;
2825 }
2826 
2827 static int
compiler_if(struct compiler * c,stmt_ty s)2828 compiler_if(struct compiler *c, stmt_ty s)
2829 {
2830     basicblock *end, *next;
2831     assert(s->kind == If_kind);
2832     end = compiler_new_block(c);
2833     if (end == NULL) {
2834         return 0;
2835     }
2836     if (asdl_seq_LEN(s->v.If.orelse)) {
2837         next = compiler_new_block(c);
2838         if (next == NULL) {
2839             return 0;
2840         }
2841     }
2842     else {
2843         next = end;
2844     }
2845     if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
2846         return 0;
2847     }
2848     VISIT_SEQ(c, stmt, s->v.If.body);
2849     if (asdl_seq_LEN(s->v.If.orelse)) {
2850         ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
2851         compiler_use_next_block(c, next);
2852         VISIT_SEQ(c, stmt, s->v.If.orelse);
2853     }
2854     compiler_use_next_block(c, end);
2855     return 1;
2856 }
2857 
2858 static int
compiler_for(struct compiler * c,stmt_ty s)2859 compiler_for(struct compiler *c, stmt_ty s)
2860 {
2861     basicblock *start, *body, *cleanup, *end;
2862 
2863     start = compiler_new_block(c);
2864     body = compiler_new_block(c);
2865     cleanup = compiler_new_block(c);
2866     end = compiler_new_block(c);
2867     if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
2868         return 0;
2869     }
2870     if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
2871         return 0;
2872     }
2873     VISIT(c, expr, s->v.For.iter);
2874     ADDOP(c, GET_ITER);
2875     compiler_use_next_block(c, start);
2876     ADDOP_JUMP(c, FOR_ITER, cleanup);
2877     compiler_use_next_block(c, body);
2878     VISIT(c, expr, s->v.For.target);
2879     VISIT_SEQ(c, stmt, s->v.For.body);
2880     /* Mark jump as artificial */
2881     c->u->u_lineno = -1;
2882     ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
2883     compiler_use_next_block(c, cleanup);
2884 
2885     compiler_pop_fblock(c, FOR_LOOP, start);
2886 
2887     VISIT_SEQ(c, stmt, s->v.For.orelse);
2888     compiler_use_next_block(c, end);
2889     return 1;
2890 }
2891 
2892 
2893 static int
compiler_async_for(struct compiler * c,stmt_ty s)2894 compiler_async_for(struct compiler *c, stmt_ty s)
2895 {
2896     basicblock *start, *except, *end;
2897     if (IS_TOP_LEVEL_AWAIT(c)){
2898         c->u->u_ste->ste_coroutine = 1;
2899     } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
2900         return compiler_error(c, "'async for' outside async function");
2901     }
2902 
2903     start = compiler_new_block(c);
2904     except = compiler_new_block(c);
2905     end = compiler_new_block(c);
2906 
2907     if (start == NULL || except == NULL || end == NULL) {
2908         return 0;
2909     }
2910     VISIT(c, expr, s->v.AsyncFor.iter);
2911     ADDOP(c, GET_AITER);
2912 
2913     compiler_use_next_block(c, start);
2914     if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
2915         return 0;
2916     }
2917     /* SETUP_FINALLY to guard the __anext__ call */
2918     ADDOP_JUMP(c, SETUP_FINALLY, except);
2919     ADDOP(c, GET_ANEXT);
2920     ADDOP_LOAD_CONST(c, Py_None);
2921     ADDOP(c, YIELD_FROM);
2922     ADDOP(c, POP_BLOCK);  /* for SETUP_FINALLY */
2923 
2924     /* Success block for __anext__ */
2925     VISIT(c, expr, s->v.AsyncFor.target);
2926     VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2927     ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
2928 
2929     compiler_pop_fblock(c, FOR_LOOP, start);
2930 
2931     /* Except block for __anext__ */
2932     compiler_use_next_block(c, except);
2933 
2934     /* Use same line number as the iterator,
2935      * as the END_ASYNC_FOR succeeds the `for`, not the body. */
2936     SET_LOC(c, s->v.AsyncFor.iter);
2937     ADDOP(c, END_ASYNC_FOR);
2938 
2939     /* `else` block */
2940     VISIT_SEQ(c, stmt, s->v.For.orelse);
2941 
2942     compiler_use_next_block(c, end);
2943 
2944     return 1;
2945 }
2946 
2947 static int
compiler_while(struct compiler * c,stmt_ty s)2948 compiler_while(struct compiler *c, stmt_ty s)
2949 {
2950     basicblock *loop, *body, *end, *anchor = NULL;
2951     loop = compiler_new_block(c);
2952     body = compiler_new_block(c);
2953     anchor = compiler_new_block(c);
2954     end = compiler_new_block(c);
2955     if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
2956         return 0;
2957     }
2958     compiler_use_next_block(c, loop);
2959     if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
2960         return 0;
2961     }
2962     if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
2963         return 0;
2964     }
2965 
2966     compiler_use_next_block(c, body);
2967     VISIT_SEQ(c, stmt, s->v.While.body);
2968     SET_LOC(c, s);
2969     if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
2970         return 0;
2971     }
2972 
2973     compiler_pop_fblock(c, WHILE_LOOP, loop);
2974 
2975     compiler_use_next_block(c, anchor);
2976     if (s->v.While.orelse) {
2977         VISIT_SEQ(c, stmt, s->v.While.orelse);
2978     }
2979     compiler_use_next_block(c, end);
2980 
2981     return 1;
2982 }
2983 
2984 static int
compiler_return(struct compiler * c,stmt_ty s)2985 compiler_return(struct compiler *c, stmt_ty s)
2986 {
2987     int preserve_tos = ((s->v.Return.value != NULL) &&
2988                         (s->v.Return.value->kind != Constant_kind));
2989     if (c->u->u_ste->ste_type != FunctionBlock)
2990         return compiler_error(c, "'return' outside function");
2991     if (s->v.Return.value != NULL &&
2992         c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2993     {
2994             return compiler_error(
2995                 c, "'return' with value in async generator");
2996     }
2997     if (preserve_tos) {
2998         VISIT(c, expr, s->v.Return.value);
2999     } else {
3000         /* Emit instruction with line number for return value */
3001         if (s->v.Return.value != NULL) {
3002             SET_LOC(c, s->v.Return.value);
3003             ADDOP(c, NOP);
3004         }
3005     }
3006     if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3007         SET_LOC(c, s);
3008         ADDOP(c, NOP);
3009     }
3010 
3011     if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
3012         return 0;
3013     if (s->v.Return.value == NULL) {
3014         ADDOP_LOAD_CONST(c, Py_None);
3015     }
3016     else if (!preserve_tos) {
3017         ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
3018     }
3019     ADDOP(c, RETURN_VALUE);
3020     NEXT_BLOCK(c);
3021 
3022     return 1;
3023 }
3024 
3025 static int
compiler_break(struct compiler * c)3026 compiler_break(struct compiler *c)
3027 {
3028     struct fblockinfo *loop = NULL;
3029     /* Emit instruction with line number */
3030     ADDOP(c, NOP);
3031     if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3032         return 0;
3033     }
3034     if (loop == NULL) {
3035         return compiler_error(c, "'break' outside loop");
3036     }
3037     if (!compiler_unwind_fblock(c, loop, 0)) {
3038         return 0;
3039     }
3040     ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
3041     NEXT_BLOCK(c);
3042     return 1;
3043 }
3044 
3045 static int
compiler_continue(struct compiler * c)3046 compiler_continue(struct compiler *c)
3047 {
3048     struct fblockinfo *loop = NULL;
3049     /* Emit instruction with line number */
3050     ADDOP(c, NOP);
3051     if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3052         return 0;
3053     }
3054     if (loop == NULL) {
3055         return compiler_error(c, "'continue' not properly in loop");
3056     }
3057     ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
3058     NEXT_BLOCK(c)
3059     return 1;
3060 }
3061 
3062 
3063 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
3064 
3065         SETUP_FINALLY           L
3066         <code for body>
3067         POP_BLOCK
3068         <code for finalbody>
3069         JUMP E
3070     L:
3071         <code for finalbody>
3072     E:
3073 
3074    The special instructions use the block stack.  Each block
3075    stack entry contains the instruction that created it (here
3076    SETUP_FINALLY), the level of the value stack at the time the
3077    block stack entry was created, and a label (here L).
3078 
3079    SETUP_FINALLY:
3080     Pushes the current value stack level and the label
3081     onto the block stack.
3082    POP_BLOCK:
3083     Pops en entry from the block stack.
3084 
3085    The block stack is unwound when an exception is raised:
3086    when a SETUP_FINALLY entry is found, the raised and the caught
3087    exceptions are pushed onto the value stack (and the exception
3088    condition is cleared), and the interpreter jumps to the label
3089    gotten from the block stack.
3090 */
3091 
3092 static int
compiler_try_finally(struct compiler * c,stmt_ty s)3093 compiler_try_finally(struct compiler *c, stmt_ty s)
3094 {
3095     basicblock *body, *end, *exit;
3096 
3097     body = compiler_new_block(c);
3098     end = compiler_new_block(c);
3099     exit = compiler_new_block(c);
3100     if (body == NULL || end == NULL || exit == NULL)
3101         return 0;
3102 
3103     /* `try` block */
3104     ADDOP_JUMP(c, SETUP_FINALLY, end);
3105     compiler_use_next_block(c, body);
3106     if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
3107         return 0;
3108     if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3109         if (!compiler_try_except(c, s))
3110             return 0;
3111     }
3112     else {
3113         VISIT_SEQ(c, stmt, s->v.Try.body);
3114     }
3115     ADDOP_NOLINE(c, POP_BLOCK);
3116     compiler_pop_fblock(c, FINALLY_TRY, body);
3117     VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3118     ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit);
3119     /* `finally` block */
3120     compiler_use_next_block(c, end);
3121     if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3122         return 0;
3123     VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3124     compiler_pop_fblock(c, FINALLY_END, end);
3125     ADDOP_I(c, RERAISE, 0);
3126     compiler_use_next_block(c, exit);
3127     return 1;
3128 }
3129 
3130 /*
3131    Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
3132    (The contents of the value stack is shown in [], with the top
3133    at the right; 'tb' is trace-back info, 'val' the exception's
3134    associated value, and 'exc' the exception.)
3135 
3136    Value stack          Label   Instruction     Argument
3137    []                           SETUP_FINALLY   L1
3138    []                           <code for S>
3139    []                           POP_BLOCK
3140    []                           JUMP_FORWARD    L0
3141 
3142    [tb, val, exc]       L1:     DUP                             )
3143    [tb, val, exc, exc]          <evaluate E1>                   )
3144    [tb, val, exc, exc, E1]      JUMP_IF_NOT_EXC_MATCH L2        ) only if E1
3145    [tb, val, exc]               POP
3146    [tb, val]                    <assign to V1>  (or POP if no V1)
3147    [tb]                         POP
3148    []                           <code for S1>
3149                                 JUMP_FORWARD    L0
3150 
3151    [tb, val, exc]       L2:     DUP
3152    .............................etc.......................
3153 
3154    [tb, val, exc]       Ln+1:   RERAISE     # re-raise exception
3155 
3156    []                   L0:     <next statement>
3157 
3158    Of course, parts are not generated if Vi or Ei is not present.
3159 */
3160 static int
compiler_try_except(struct compiler * c,stmt_ty s)3161 compiler_try_except(struct compiler *c, stmt_ty s)
3162 {
3163     basicblock *body, *orelse, *except, *end;
3164     Py_ssize_t i, n;
3165 
3166     body = compiler_new_block(c);
3167     except = compiler_new_block(c);
3168     orelse = compiler_new_block(c);
3169     end = compiler_new_block(c);
3170     if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3171         return 0;
3172     ADDOP_JUMP(c, SETUP_FINALLY, except);
3173     compiler_use_next_block(c, body);
3174     if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
3175         return 0;
3176     VISIT_SEQ(c, stmt, s->v.Try.body);
3177     compiler_pop_fblock(c, TRY_EXCEPT, body);
3178     ADDOP_NOLINE(c, POP_BLOCK);
3179     ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse);
3180     n = asdl_seq_LEN(s->v.Try.handlers);
3181     compiler_use_next_block(c, except);
3182     /* Runtime will push a block here, so we need to account for that */
3183     if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3184         return 0;
3185     for (i = 0; i < n; i++) {
3186         excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
3187             s->v.Try.handlers, i);
3188         SET_LOC(c, handler);
3189         if (!handler->v.ExceptHandler.type && i < n-1)
3190             return compiler_error(c, "default 'except:' must be last");
3191         except = compiler_new_block(c);
3192         if (except == NULL)
3193             return 0;
3194         if (handler->v.ExceptHandler.type) {
3195             ADDOP(c, DUP_TOP);
3196             VISIT(c, expr, handler->v.ExceptHandler.type);
3197             ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
3198             NEXT_BLOCK(c);
3199         }
3200         ADDOP(c, POP_TOP);
3201         if (handler->v.ExceptHandler.name) {
3202             basicblock *cleanup_end, *cleanup_body;
3203 
3204             cleanup_end = compiler_new_block(c);
3205             cleanup_body = compiler_new_block(c);
3206             if (cleanup_end == NULL || cleanup_body == NULL) {
3207                 return 0;
3208             }
3209 
3210             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3211             ADDOP(c, POP_TOP);
3212 
3213             /*
3214               try:
3215                   # body
3216               except type as name:
3217                   try:
3218                       # body
3219                   finally:
3220                       name = None # in case body contains "del name"
3221                       del name
3222             */
3223 
3224             /* second try: */
3225             ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
3226             compiler_use_next_block(c, cleanup_body);
3227             if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
3228                 return 0;
3229 
3230             /* second # body */
3231             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3232             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3233             /* name = None; del name; # Mark as artificial */
3234             c->u->u_lineno = -1;
3235             ADDOP(c, POP_BLOCK);
3236             ADDOP(c, POP_EXCEPT);
3237             ADDOP_LOAD_CONST(c, Py_None);
3238             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3239             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3240             ADDOP_JUMP(c, JUMP_FORWARD, end);
3241 
3242             /* except: */
3243             compiler_use_next_block(c, cleanup_end);
3244 
3245             /* name = None; del name; # Mark as artificial */
3246             c->u->u_lineno = -1;
3247             ADDOP_LOAD_CONST(c, Py_None);
3248             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3249             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3250 
3251             ADDOP_I(c, RERAISE, 1);
3252         }
3253         else {
3254             basicblock *cleanup_body;
3255 
3256             cleanup_body = compiler_new_block(c);
3257             if (!cleanup_body)
3258                 return 0;
3259 
3260             ADDOP(c, POP_TOP);
3261             ADDOP(c, POP_TOP);
3262             compiler_use_next_block(c, cleanup_body);
3263             if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
3264                 return 0;
3265             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3266             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3267             c->u->u_lineno = -1;
3268             ADDOP(c, POP_EXCEPT);
3269             ADDOP_JUMP(c, JUMP_FORWARD, end);
3270         }
3271         compiler_use_next_block(c, except);
3272     }
3273     compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
3274     /* Mark as artificial */
3275     c->u->u_lineno = -1;
3276     ADDOP_I(c, RERAISE, 0);
3277     compiler_use_next_block(c, orelse);
3278     VISIT_SEQ(c, stmt, s->v.Try.orelse);
3279     compiler_use_next_block(c, end);
3280     return 1;
3281 }
3282 
3283 static int
compiler_try(struct compiler * c,stmt_ty s)3284 compiler_try(struct compiler *c, stmt_ty s) {
3285     if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3286         return compiler_try_finally(c, s);
3287     else
3288         return compiler_try_except(c, s);
3289 }
3290 
3291 
3292 static int
compiler_import_as(struct compiler * c,identifier name,identifier asname)3293 compiler_import_as(struct compiler *c, identifier name, identifier asname)
3294 {
3295     /* The IMPORT_NAME opcode was already generated.  This function
3296        merely needs to bind the result to a name.
3297 
3298        If there is a dot in name, we need to split it and emit a
3299        IMPORT_FROM for each name.
3300     */
3301     Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3302     Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
3303     if (dot == -2)
3304         return 0;
3305     if (dot != -1) {
3306         /* Consume the base module name to get the first attribute */
3307         while (1) {
3308             Py_ssize_t pos = dot + 1;
3309             PyObject *attr;
3310             dot = PyUnicode_FindChar(name, '.', pos, len, 1);
3311             if (dot == -2)
3312                 return 0;
3313             attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
3314             if (!attr)
3315                 return 0;
3316             ADDOP_N(c, IMPORT_FROM, attr, names);
3317             if (dot == -1) {
3318                 break;
3319             }
3320             ADDOP(c, ROT_TWO);
3321             ADDOP(c, POP_TOP);
3322         }
3323         if (!compiler_nameop(c, asname, Store)) {
3324             return 0;
3325         }
3326         ADDOP(c, POP_TOP);
3327         return 1;
3328     }
3329     return compiler_nameop(c, asname, Store);
3330 }
3331 
3332 static int
compiler_import(struct compiler * c,stmt_ty s)3333 compiler_import(struct compiler *c, stmt_ty s)
3334 {
3335     /* The Import node stores a module name like a.b.c as a single
3336        string.  This is convenient for all cases except
3337          import a.b.c as d
3338        where we need to parse that string to extract the individual
3339        module names.
3340        XXX Perhaps change the representation to make this case simpler?
3341      */
3342     Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
3343 
3344     PyObject *zero = _PyLong_GetZero();  // borrowed reference
3345     for (i = 0; i < n; i++) {
3346         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3347         int r;
3348 
3349         ADDOP_LOAD_CONST(c, zero);
3350         ADDOP_LOAD_CONST(c, Py_None);
3351         ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
3352 
3353         if (alias->asname) {
3354             r = compiler_import_as(c, alias->name, alias->asname);
3355             if (!r)
3356                 return r;
3357         }
3358         else {
3359             identifier tmp = alias->name;
3360             Py_ssize_t dot = PyUnicode_FindChar(
3361                 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
3362             if (dot != -1) {
3363                 tmp = PyUnicode_Substring(alias->name, 0, dot);
3364                 if (tmp == NULL)
3365                     return 0;
3366             }
3367             r = compiler_nameop(c, tmp, Store);
3368             if (dot != -1) {
3369                 Py_DECREF(tmp);
3370             }
3371             if (!r)
3372                 return r;
3373         }
3374     }
3375     return 1;
3376 }
3377 
3378 static int
compiler_from_import(struct compiler * c,stmt_ty s)3379 compiler_from_import(struct compiler *c, stmt_ty s)
3380 {
3381     Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
3382     PyObject *names;
3383     static PyObject *empty_string;
3384 
3385     if (!empty_string) {
3386         empty_string = PyUnicode_FromString("");
3387         if (!empty_string)
3388             return 0;
3389     }
3390 
3391     ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
3392 
3393     names = PyTuple_New(n);
3394     if (!names)
3395         return 0;
3396 
3397     /* build up the names */
3398     for (i = 0; i < n; i++) {
3399         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3400         Py_INCREF(alias->name);
3401         PyTuple_SET_ITEM(names, i, alias->name);
3402     }
3403 
3404     if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
3405         _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
3406         Py_DECREF(names);
3407         return compiler_error(c, "from __future__ imports must occur "
3408                               "at the beginning of the file");
3409     }
3410     ADDOP_LOAD_CONST_NEW(c, names);
3411 
3412     if (s->v.ImportFrom.module) {
3413         ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3414     }
3415     else {
3416         ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3417     }
3418     for (i = 0; i < n; i++) {
3419         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3420         identifier store_name;
3421 
3422         if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
3423             assert(n == 1);
3424             ADDOP(c, IMPORT_STAR);
3425             return 1;
3426         }
3427 
3428         ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3429         store_name = alias->name;
3430         if (alias->asname)
3431             store_name = alias->asname;
3432 
3433         if (!compiler_nameop(c, store_name, Store)) {
3434             return 0;
3435         }
3436     }
3437     /* remove imported module */
3438     ADDOP(c, POP_TOP);
3439     return 1;
3440 }
3441 
3442 static int
compiler_assert(struct compiler * c,stmt_ty s)3443 compiler_assert(struct compiler *c, stmt_ty s)
3444 {
3445     basicblock *end;
3446 
3447     /* Always emit a warning if the test is a non-zero length tuple */
3448     if ((s->v.Assert.test->kind == Tuple_kind &&
3449         asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3450         (s->v.Assert.test->kind == Constant_kind &&
3451          PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3452          PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
3453     {
3454         if (!compiler_warn(c, "assertion is always true, "
3455                               "perhaps remove parentheses?"))
3456         {
3457             return 0;
3458         }
3459     }
3460     if (c->c_optimize)
3461         return 1;
3462     end = compiler_new_block(c);
3463     if (end == NULL)
3464         return 0;
3465     if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3466         return 0;
3467     ADDOP(c, LOAD_ASSERTION_ERROR);
3468     if (s->v.Assert.msg) {
3469         VISIT(c, expr, s->v.Assert.msg);
3470         ADDOP_I(c, CALL_FUNCTION, 1);
3471     }
3472     ADDOP_I(c, RAISE_VARARGS, 1);
3473     compiler_use_next_block(c, end);
3474     return 1;
3475 }
3476 
3477 static int
compiler_visit_stmt_expr(struct compiler * c,expr_ty value)3478 compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3479 {
3480     if (c->c_interactive && c->c_nestlevel <= 1) {
3481         VISIT(c, expr, value);
3482         ADDOP(c, PRINT_EXPR);
3483         return 1;
3484     }
3485 
3486     if (value->kind == Constant_kind) {
3487         /* ignore constant statement */
3488         ADDOP(c, NOP);
3489         return 1;
3490     }
3491 
3492     VISIT(c, expr, value);
3493     /* Mark POP_TOP as artificial */
3494     c->u->u_lineno = -1;
3495     ADDOP(c, POP_TOP);
3496     return 1;
3497 }
3498 
3499 static int
compiler_visit_stmt(struct compiler * c,stmt_ty s)3500 compiler_visit_stmt(struct compiler *c, stmt_ty s)
3501 {
3502     Py_ssize_t i, n;
3503 
3504     /* Always assign a lineno to the next instruction for a stmt. */
3505     SET_LOC(c, s);
3506 
3507     switch (s->kind) {
3508     case FunctionDef_kind:
3509         return compiler_function(c, s, 0);
3510     case ClassDef_kind:
3511         return compiler_class(c, s);
3512     case Return_kind:
3513         return compiler_return(c, s);
3514     case Delete_kind:
3515         VISIT_SEQ(c, expr, s->v.Delete.targets)
3516         break;
3517     case Assign_kind:
3518         n = asdl_seq_LEN(s->v.Assign.targets);
3519         VISIT(c, expr, s->v.Assign.value);
3520         for (i = 0; i < n; i++) {
3521             if (i < n - 1)
3522                 ADDOP(c, DUP_TOP);
3523             VISIT(c, expr,
3524                   (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3525         }
3526         break;
3527     case AugAssign_kind:
3528         return compiler_augassign(c, s);
3529     case AnnAssign_kind:
3530         return compiler_annassign(c, s);
3531     case For_kind:
3532         return compiler_for(c, s);
3533     case While_kind:
3534         return compiler_while(c, s);
3535     case If_kind:
3536         return compiler_if(c, s);
3537     case Match_kind:
3538         return compiler_match(c, s);
3539     case Raise_kind:
3540         n = 0;
3541         if (s->v.Raise.exc) {
3542             VISIT(c, expr, s->v.Raise.exc);
3543             n++;
3544             if (s->v.Raise.cause) {
3545                 VISIT(c, expr, s->v.Raise.cause);
3546                 n++;
3547             }
3548         }
3549         ADDOP_I(c, RAISE_VARARGS, (int)n);
3550         NEXT_BLOCK(c);
3551         break;
3552     case Try_kind:
3553         return compiler_try(c, s);
3554     case Assert_kind:
3555         return compiler_assert(c, s);
3556     case Import_kind:
3557         return compiler_import(c, s);
3558     case ImportFrom_kind:
3559         return compiler_from_import(c, s);
3560     case Global_kind:
3561     case Nonlocal_kind:
3562         break;
3563     case Expr_kind:
3564         return compiler_visit_stmt_expr(c, s->v.Expr.value);
3565     case Pass_kind:
3566         ADDOP(c, NOP);
3567         break;
3568     case Break_kind:
3569         return compiler_break(c);
3570     case Continue_kind:
3571         return compiler_continue(c);
3572     case With_kind:
3573         return compiler_with(c, s, 0);
3574     case AsyncFunctionDef_kind:
3575         return compiler_function(c, s, 1);
3576     case AsyncWith_kind:
3577         return compiler_async_with(c, s, 0);
3578     case AsyncFor_kind:
3579         return compiler_async_for(c, s);
3580     }
3581 
3582     return 1;
3583 }
3584 
3585 static int
unaryop(unaryop_ty op)3586 unaryop(unaryop_ty op)
3587 {
3588     switch (op) {
3589     case Invert:
3590         return UNARY_INVERT;
3591     case Not:
3592         return UNARY_NOT;
3593     case UAdd:
3594         return UNARY_POSITIVE;
3595     case USub:
3596         return UNARY_NEGATIVE;
3597     default:
3598         PyErr_Format(PyExc_SystemError,
3599             "unary op %d should not be possible", op);
3600         return 0;
3601     }
3602 }
3603 
3604 static int
binop(operator_ty op)3605 binop(operator_ty op)
3606 {
3607     switch (op) {
3608     case Add:
3609         return BINARY_ADD;
3610     case Sub:
3611         return BINARY_SUBTRACT;
3612     case Mult:
3613         return BINARY_MULTIPLY;
3614     case MatMult:
3615         return BINARY_MATRIX_MULTIPLY;
3616     case Div:
3617         return BINARY_TRUE_DIVIDE;
3618     case Mod:
3619         return BINARY_MODULO;
3620     case Pow:
3621         return BINARY_POWER;
3622     case LShift:
3623         return BINARY_LSHIFT;
3624     case RShift:
3625         return BINARY_RSHIFT;
3626     case BitOr:
3627         return BINARY_OR;
3628     case BitXor:
3629         return BINARY_XOR;
3630     case BitAnd:
3631         return BINARY_AND;
3632     case FloorDiv:
3633         return BINARY_FLOOR_DIVIDE;
3634     default:
3635         PyErr_Format(PyExc_SystemError,
3636             "binary op %d should not be possible", op);
3637         return 0;
3638     }
3639 }
3640 
3641 static int
inplace_binop(operator_ty op)3642 inplace_binop(operator_ty op)
3643 {
3644     switch (op) {
3645     case Add:
3646         return INPLACE_ADD;
3647     case Sub:
3648         return INPLACE_SUBTRACT;
3649     case Mult:
3650         return INPLACE_MULTIPLY;
3651     case MatMult:
3652         return INPLACE_MATRIX_MULTIPLY;
3653     case Div:
3654         return INPLACE_TRUE_DIVIDE;
3655     case Mod:
3656         return INPLACE_MODULO;
3657     case Pow:
3658         return INPLACE_POWER;
3659     case LShift:
3660         return INPLACE_LSHIFT;
3661     case RShift:
3662         return INPLACE_RSHIFT;
3663     case BitOr:
3664         return INPLACE_OR;
3665     case BitXor:
3666         return INPLACE_XOR;
3667     case BitAnd:
3668         return INPLACE_AND;
3669     case FloorDiv:
3670         return INPLACE_FLOOR_DIVIDE;
3671     default:
3672         PyErr_Format(PyExc_SystemError,
3673             "inplace binary op %d should not be possible", op);
3674         return 0;
3675     }
3676 }
3677 
3678 static int
compiler_nameop(struct compiler * c,identifier name,expr_context_ty ctx)3679 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3680 {
3681     int op, scope;
3682     Py_ssize_t arg;
3683     enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
3684 
3685     PyObject *dict = c->u->u_names;
3686     PyObject *mangled;
3687 
3688     assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3689            !_PyUnicode_EqualToASCIIString(name, "True") &&
3690            !_PyUnicode_EqualToASCIIString(name, "False"));
3691 
3692     if (forbidden_name(c, name, ctx))
3693         return 0;
3694 
3695     mangled = _Py_Mangle(c->u->u_private, name);
3696     if (!mangled)
3697         return 0;
3698 
3699     op = 0;
3700     optype = OP_NAME;
3701     scope = _PyST_GetScope(c->u->u_ste, mangled);
3702     switch (scope) {
3703     case FREE:
3704         dict = c->u->u_freevars;
3705         optype = OP_DEREF;
3706         break;
3707     case CELL:
3708         dict = c->u->u_cellvars;
3709         optype = OP_DEREF;
3710         break;
3711     case LOCAL:
3712         if (c->u->u_ste->ste_type == FunctionBlock)
3713             optype = OP_FAST;
3714         break;
3715     case GLOBAL_IMPLICIT:
3716         if (c->u->u_ste->ste_type == FunctionBlock)
3717             optype = OP_GLOBAL;
3718         break;
3719     case GLOBAL_EXPLICIT:
3720         optype = OP_GLOBAL;
3721         break;
3722     default:
3723         /* scope can be 0 */
3724         break;
3725     }
3726 
3727     /* XXX Leave assert here, but handle __doc__ and the like better */
3728     assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
3729 
3730     switch (optype) {
3731     case OP_DEREF:
3732         switch (ctx) {
3733         case Load:
3734             op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3735             break;
3736         case Store: op = STORE_DEREF; break;
3737         case Del: op = DELETE_DEREF; break;
3738         }
3739         break;
3740     case OP_FAST:
3741         switch (ctx) {
3742         case Load: op = LOAD_FAST; break;
3743         case Store: op = STORE_FAST; break;
3744         case Del: op = DELETE_FAST; break;
3745         }
3746         ADDOP_N(c, op, mangled, varnames);
3747         return 1;
3748     case OP_GLOBAL:
3749         switch (ctx) {
3750         case Load: op = LOAD_GLOBAL; break;
3751         case Store: op = STORE_GLOBAL; break;
3752         case Del: op = DELETE_GLOBAL; break;
3753         }
3754         break;
3755     case OP_NAME:
3756         switch (ctx) {
3757         case Load: op = LOAD_NAME; break;
3758         case Store: op = STORE_NAME; break;
3759         case Del: op = DELETE_NAME; break;
3760         }
3761         break;
3762     }
3763 
3764     assert(op);
3765     arg = compiler_add_o(dict, mangled);
3766     Py_DECREF(mangled);
3767     if (arg < 0)
3768         return 0;
3769     return compiler_addop_i(c, op, arg);
3770 }
3771 
3772 static int
compiler_boolop(struct compiler * c,expr_ty e)3773 compiler_boolop(struct compiler *c, expr_ty e)
3774 {
3775     basicblock *end;
3776     int jumpi;
3777     Py_ssize_t i, n;
3778     asdl_expr_seq *s;
3779 
3780     assert(e->kind == BoolOp_kind);
3781     if (e->v.BoolOp.op == And)
3782         jumpi = JUMP_IF_FALSE_OR_POP;
3783     else
3784         jumpi = JUMP_IF_TRUE_OR_POP;
3785     end = compiler_new_block(c);
3786     if (end == NULL)
3787         return 0;
3788     s = e->v.BoolOp.values;
3789     n = asdl_seq_LEN(s) - 1;
3790     assert(n >= 0);
3791     for (i = 0; i < n; ++i) {
3792         VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3793         ADDOP_JUMP(c, jumpi, end);
3794         basicblock *next = compiler_new_block(c);
3795         if (next == NULL) {
3796             return 0;
3797         }
3798         compiler_use_next_block(c, next);
3799     }
3800     VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3801     compiler_use_next_block(c, end);
3802     return 1;
3803 }
3804 
3805 static int
starunpack_helper(struct compiler * c,asdl_expr_seq * elts,int pushed,int build,int add,int extend,int tuple)3806 starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
3807                   int build, int add, int extend, int tuple)
3808 {
3809     Py_ssize_t n = asdl_seq_LEN(elts);
3810     if (n > 2 && are_all_items_const(elts, 0, n)) {
3811         PyObject *folded = PyTuple_New(n);
3812         if (folded == NULL) {
3813             return 0;
3814         }
3815         PyObject *val;
3816         for (Py_ssize_t i = 0; i < n; i++) {
3817             val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3818             Py_INCREF(val);
3819             PyTuple_SET_ITEM(folded, i, val);
3820         }
3821         if (tuple) {
3822             ADDOP_LOAD_CONST_NEW(c, folded);
3823         } else {
3824             if (add == SET_ADD) {
3825                 Py_SETREF(folded, PyFrozenSet_New(folded));
3826                 if (folded == NULL) {
3827                     return 0;
3828                 }
3829             }
3830             ADDOP_I(c, build, pushed);
3831             ADDOP_LOAD_CONST_NEW(c, folded);
3832             ADDOP_I(c, extend, 1);
3833         }
3834         return 1;
3835     }
3836 
3837     int big = n+pushed > STACK_USE_GUIDELINE;
3838     int seen_star = 0;
3839     for (Py_ssize_t i = 0; i < n; i++) {
3840         expr_ty elt = asdl_seq_GET(elts, i);
3841         if (elt->kind == Starred_kind) {
3842             seen_star = 1;
3843         }
3844     }
3845     if (!seen_star && !big) {
3846         for (Py_ssize_t i = 0; i < n; i++) {
3847             expr_ty elt = asdl_seq_GET(elts, i);
3848             VISIT(c, expr, elt);
3849         }
3850         if (tuple) {
3851             ADDOP_I(c, BUILD_TUPLE, n+pushed);
3852         } else {
3853             ADDOP_I(c, build, n+pushed);
3854         }
3855         return 1;
3856     }
3857     int sequence_built = 0;
3858     if (big) {
3859         ADDOP_I(c, build, pushed);
3860         sequence_built = 1;
3861     }
3862     for (Py_ssize_t i = 0; i < n; i++) {
3863         expr_ty elt = asdl_seq_GET(elts, i);
3864         if (elt->kind == Starred_kind) {
3865             if (sequence_built == 0) {
3866                 ADDOP_I(c, build, i+pushed);
3867                 sequence_built = 1;
3868             }
3869             VISIT(c, expr, elt->v.Starred.value);
3870             ADDOP_I(c, extend, 1);
3871         }
3872         else {
3873             VISIT(c, expr, elt);
3874             if (sequence_built) {
3875                 ADDOP_I(c, add, 1);
3876             }
3877         }
3878     }
3879     assert(sequence_built);
3880     if (tuple) {
3881         ADDOP(c, LIST_TO_TUPLE);
3882     }
3883     return 1;
3884 }
3885 
3886 static int
unpack_helper(struct compiler * c,asdl_expr_seq * elts)3887 unpack_helper(struct compiler *c, asdl_expr_seq *elts)
3888 {
3889     Py_ssize_t n = asdl_seq_LEN(elts);
3890     int seen_star = 0;
3891     for (Py_ssize_t i = 0; i < n; i++) {
3892         expr_ty elt = asdl_seq_GET(elts, i);
3893         if (elt->kind == Starred_kind && !seen_star) {
3894             if ((i >= (1 << 8)) ||
3895                 (n-i-1 >= (INT_MAX >> 8)))
3896                 return compiler_error(c,
3897                     "too many expressions in "
3898                     "star-unpacking assignment");
3899             ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3900             seen_star = 1;
3901         }
3902         else if (elt->kind == Starred_kind) {
3903             return compiler_error(c,
3904                 "multiple starred expressions in assignment");
3905         }
3906     }
3907     if (!seen_star) {
3908         ADDOP_I(c, UNPACK_SEQUENCE, n);
3909     }
3910     return 1;
3911 }
3912 
3913 static int
assignment_helper(struct compiler * c,asdl_expr_seq * elts)3914 assignment_helper(struct compiler *c, asdl_expr_seq *elts)
3915 {
3916     Py_ssize_t n = asdl_seq_LEN(elts);
3917     RETURN_IF_FALSE(unpack_helper(c, elts));
3918     for (Py_ssize_t i = 0; i < n; i++) {
3919         expr_ty elt = asdl_seq_GET(elts, i);
3920         VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3921     }
3922     return 1;
3923 }
3924 
3925 static int
compiler_list(struct compiler * c,expr_ty e)3926 compiler_list(struct compiler *c, expr_ty e)
3927 {
3928     asdl_expr_seq *elts = e->v.List.elts;
3929     if (e->v.List.ctx == Store) {
3930         return assignment_helper(c, elts);
3931     }
3932     else if (e->v.List.ctx == Load) {
3933         return starunpack_helper(c, elts, 0, BUILD_LIST,
3934                                  LIST_APPEND, LIST_EXTEND, 0);
3935     }
3936     else
3937         VISIT_SEQ(c, expr, elts);
3938     return 1;
3939 }
3940 
3941 static int
compiler_tuple(struct compiler * c,expr_ty e)3942 compiler_tuple(struct compiler *c, expr_ty e)
3943 {
3944     asdl_expr_seq *elts = e->v.Tuple.elts;
3945     if (e->v.Tuple.ctx == Store) {
3946         return assignment_helper(c, elts);
3947     }
3948     else if (e->v.Tuple.ctx == Load) {
3949         return starunpack_helper(c, elts, 0, BUILD_LIST,
3950                                  LIST_APPEND, LIST_EXTEND, 1);
3951     }
3952     else
3953         VISIT_SEQ(c, expr, elts);
3954     return 1;
3955 }
3956 
3957 static int
compiler_set(struct compiler * c,expr_ty e)3958 compiler_set(struct compiler *c, expr_ty e)
3959 {
3960     return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3961                              SET_ADD, SET_UPDATE, 0);
3962 }
3963 
3964 static int
are_all_items_const(asdl_expr_seq * seq,Py_ssize_t begin,Py_ssize_t end)3965 are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3966 {
3967     Py_ssize_t i;
3968     for (i = begin; i < end; i++) {
3969         expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3970         if (key == NULL || key->kind != Constant_kind)
3971             return 0;
3972     }
3973     return 1;
3974 }
3975 
3976 static int
compiler_subdict(struct compiler * c,expr_ty e,Py_ssize_t begin,Py_ssize_t end)3977 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3978 {
3979     Py_ssize_t i, n = end - begin;
3980     PyObject *keys, *key;
3981     int big = n*2 > STACK_USE_GUIDELINE;
3982     if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
3983         for (i = begin; i < end; i++) {
3984             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3985         }
3986         keys = PyTuple_New(n);
3987         if (keys == NULL) {
3988             return 0;
3989         }
3990         for (i = begin; i < end; i++) {
3991             key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
3992             Py_INCREF(key);
3993             PyTuple_SET_ITEM(keys, i - begin, key);
3994         }
3995         ADDOP_LOAD_CONST_NEW(c, keys);
3996         ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3997         return 1;
3998     }
3999     if (big) {
4000         ADDOP_I(c, BUILD_MAP, 0);
4001     }
4002     for (i = begin; i < end; i++) {
4003         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4004         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4005         if (big) {
4006             ADDOP_I(c, MAP_ADD, 1);
4007         }
4008     }
4009     if (!big) {
4010         ADDOP_I(c, BUILD_MAP, n);
4011     }
4012     return 1;
4013 }
4014 
4015 static int
compiler_dict(struct compiler * c,expr_ty e)4016 compiler_dict(struct compiler *c, expr_ty e)
4017 {
4018     Py_ssize_t i, n, elements;
4019     int have_dict;
4020     int is_unpacking = 0;
4021     n = asdl_seq_LEN(e->v.Dict.values);
4022     have_dict = 0;
4023     elements = 0;
4024     for (i = 0; i < n; i++) {
4025         is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
4026         if (is_unpacking) {
4027             if (elements) {
4028                 if (!compiler_subdict(c, e, i - elements, i)) {
4029                     return 0;
4030                 }
4031                 if (have_dict) {
4032                     ADDOP_I(c, DICT_UPDATE, 1);
4033                 }
4034                 have_dict = 1;
4035                 elements = 0;
4036             }
4037             if (have_dict == 0) {
4038                 ADDOP_I(c, BUILD_MAP, 0);
4039                 have_dict = 1;
4040             }
4041             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4042             ADDOP_I(c, DICT_UPDATE, 1);
4043         }
4044         else {
4045             if (elements*2 > STACK_USE_GUIDELINE) {
4046                 if (!compiler_subdict(c, e, i - elements, i + 1)) {
4047                     return 0;
4048                 }
4049                 if (have_dict) {
4050                     ADDOP_I(c, DICT_UPDATE, 1);
4051                 }
4052                 have_dict = 1;
4053                 elements = 0;
4054             }
4055             else {
4056                 elements++;
4057             }
4058         }
4059     }
4060     if (elements) {
4061         if (!compiler_subdict(c, e, n - elements, n)) {
4062             return 0;
4063         }
4064         if (have_dict) {
4065             ADDOP_I(c, DICT_UPDATE, 1);
4066         }
4067         have_dict = 1;
4068     }
4069     if (!have_dict) {
4070         ADDOP_I(c, BUILD_MAP, 0);
4071     }
4072     return 1;
4073 }
4074 
4075 static int
compiler_compare(struct compiler * c,expr_ty e)4076 compiler_compare(struct compiler *c, expr_ty e)
4077 {
4078     Py_ssize_t i, n;
4079 
4080     if (!check_compare(c, e)) {
4081         return 0;
4082     }
4083     VISIT(c, expr, e->v.Compare.left);
4084     assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4085     n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4086     if (n == 0) {
4087         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
4088         ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
4089     }
4090     else {
4091         basicblock *cleanup = compiler_new_block(c);
4092         if (cleanup == NULL)
4093             return 0;
4094         for (i = 0; i < n; i++) {
4095             VISIT(c, expr,
4096                 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
4097             ADDOP(c, DUP_TOP);
4098             ADDOP(c, ROT_THREE);
4099             ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
4100             ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
4101             NEXT_BLOCK(c);
4102         }
4103         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
4104         ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
4105         basicblock *end = compiler_new_block(c);
4106         if (end == NULL)
4107             return 0;
4108         ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end);
4109         compiler_use_next_block(c, cleanup);
4110         ADDOP(c, ROT_TWO);
4111         ADDOP(c, POP_TOP);
4112         compiler_use_next_block(c, end);
4113     }
4114     return 1;
4115 }
4116 
4117 static PyTypeObject *
infer_type(expr_ty e)4118 infer_type(expr_ty e)
4119 {
4120     switch (e->kind) {
4121     case Tuple_kind:
4122         return &PyTuple_Type;
4123     case List_kind:
4124     case ListComp_kind:
4125         return &PyList_Type;
4126     case Dict_kind:
4127     case DictComp_kind:
4128         return &PyDict_Type;
4129     case Set_kind:
4130     case SetComp_kind:
4131         return &PySet_Type;
4132     case GeneratorExp_kind:
4133         return &PyGen_Type;
4134     case Lambda_kind:
4135         return &PyFunction_Type;
4136     case JoinedStr_kind:
4137     case FormattedValue_kind:
4138         return &PyUnicode_Type;
4139     case Constant_kind:
4140         return Py_TYPE(e->v.Constant.value);
4141     default:
4142         return NULL;
4143     }
4144 }
4145 
4146 static int
check_caller(struct compiler * c,expr_ty e)4147 check_caller(struct compiler *c, expr_ty e)
4148 {
4149     switch (e->kind) {
4150     case Constant_kind:
4151     case Tuple_kind:
4152     case List_kind:
4153     case ListComp_kind:
4154     case Dict_kind:
4155     case DictComp_kind:
4156     case Set_kind:
4157     case SetComp_kind:
4158     case GeneratorExp_kind:
4159     case JoinedStr_kind:
4160     case FormattedValue_kind:
4161         return compiler_warn(c, "'%.200s' object is not callable; "
4162                                 "perhaps you missed a comma?",
4163                                 infer_type(e)->tp_name);
4164     default:
4165         return 1;
4166     }
4167 }
4168 
4169 static int
check_subscripter(struct compiler * c,expr_ty e)4170 check_subscripter(struct compiler *c, expr_ty e)
4171 {
4172     PyObject *v;
4173 
4174     switch (e->kind) {
4175     case Constant_kind:
4176         v = e->v.Constant.value;
4177         if (!(v == Py_None || v == Py_Ellipsis ||
4178               PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4179               PyAnySet_Check(v)))
4180         {
4181             return 1;
4182         }
4183         /* fall through */
4184     case Set_kind:
4185     case SetComp_kind:
4186     case GeneratorExp_kind:
4187     case Lambda_kind:
4188         return compiler_warn(c, "'%.200s' object is not subscriptable; "
4189                                 "perhaps you missed a comma?",
4190                                 infer_type(e)->tp_name);
4191     default:
4192         return 1;
4193     }
4194 }
4195 
4196 static int
check_index(struct compiler * c,expr_ty e,expr_ty s)4197 check_index(struct compiler *c, expr_ty e, expr_ty s)
4198 {
4199     PyObject *v;
4200 
4201     PyTypeObject *index_type = infer_type(s);
4202     if (index_type == NULL
4203         || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4204         || index_type == &PySlice_Type) {
4205         return 1;
4206     }
4207 
4208     switch (e->kind) {
4209     case Constant_kind:
4210         v = e->v.Constant.value;
4211         if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4212             return 1;
4213         }
4214         /* fall through */
4215     case Tuple_kind:
4216     case List_kind:
4217     case ListComp_kind:
4218     case JoinedStr_kind:
4219     case FormattedValue_kind:
4220         return compiler_warn(c, "%.200s indices must be integers or slices, "
4221                                 "not %.200s; "
4222                                 "perhaps you missed a comma?",
4223                                 infer_type(e)->tp_name,
4224                                 index_type->tp_name);
4225     default:
4226         return 1;
4227     }
4228 }
4229 
4230 // Return 1 if the method call was optimized, -1 if not, and 0 on error.
4231 static int
maybe_optimize_method_call(struct compiler * c,expr_ty e)4232 maybe_optimize_method_call(struct compiler *c, expr_ty e)
4233 {
4234     Py_ssize_t argsl, i;
4235     expr_ty meth = e->v.Call.func;
4236     asdl_expr_seq *args = e->v.Call.args;
4237 
4238     /* Check that the call node is an attribute access, and that
4239        the call doesn't have keyword parameters. */
4240     if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4241             asdl_seq_LEN(e->v.Call.keywords)) {
4242         return -1;
4243     }
4244     /* Check that there aren't too many arguments */
4245     argsl = asdl_seq_LEN(args);
4246     if (argsl >= STACK_USE_GUIDELINE) {
4247         return -1;
4248     }
4249     /* Check that there are no *varargs types of arguments. */
4250     for (i = 0; i < argsl; i++) {
4251         expr_ty elt = asdl_seq_GET(args, i);
4252         if (elt->kind == Starred_kind) {
4253             return -1;
4254         }
4255     }
4256 
4257     /* Alright, we can optimize the code. */
4258     VISIT(c, expr, meth->v.Attribute.value);
4259     int old_lineno = c->u->u_lineno;
4260     c->u->u_lineno = meth->end_lineno;
4261     ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4262     VISIT_SEQ(c, expr, e->v.Call.args);
4263     ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4264     c->u->u_lineno = old_lineno;
4265     return 1;
4266 }
4267 
4268 static int
validate_keywords(struct compiler * c,asdl_keyword_seq * keywords)4269 validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
4270 {
4271     Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4272     for (Py_ssize_t i = 0; i < nkeywords; i++) {
4273         keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4274         if (key->arg == NULL) {
4275             continue;
4276         }
4277         if (forbidden_name(c, key->arg, Store)) {
4278             return -1;
4279         }
4280         for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
4281             keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4282             if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4283                 SET_LOC(c, other);
4284                 compiler_error(c, "keyword argument repeated: %U", key->arg);
4285                 return -1;
4286             }
4287         }
4288     }
4289     return 0;
4290 }
4291 
4292 static int
compiler_call(struct compiler * c,expr_ty e)4293 compiler_call(struct compiler *c, expr_ty e)
4294 {
4295     int ret = maybe_optimize_method_call(c, e);
4296     if (ret >= 0) {
4297         return ret;
4298     }
4299     if (!check_caller(c, e->v.Call.func)) {
4300         return 0;
4301     }
4302     VISIT(c, expr, e->v.Call.func);
4303     return compiler_call_helper(c, 0,
4304                                 e->v.Call.args,
4305                                 e->v.Call.keywords);
4306 }
4307 
4308 static int
compiler_joined_str(struct compiler * c,expr_ty e)4309 compiler_joined_str(struct compiler *c, expr_ty e)
4310 {
4311 
4312     Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4313     if (value_count > STACK_USE_GUIDELINE) {
4314         ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
4315         PyObject *join = _PyUnicode_FromASCII("join", 4);
4316         if (join == NULL) {
4317             return 0;
4318         }
4319         ADDOP_NAME(c, LOAD_METHOD, join, names);
4320         Py_DECREF(join);
4321         ADDOP_I(c, BUILD_LIST, 0);
4322         for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4323             VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4324             ADDOP_I(c, LIST_APPEND, 1);
4325         }
4326         ADDOP_I(c, CALL_METHOD, 1);
4327     }
4328     else {
4329         VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4330         if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4331             ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4332         }
4333     }
4334     return 1;
4335 }
4336 
4337 /* Used to implement f-strings. Format a single value. */
4338 static int
compiler_formatted_value(struct compiler * c,expr_ty e)4339 compiler_formatted_value(struct compiler *c, expr_ty e)
4340 {
4341     /* Our oparg encodes 2 pieces of information: the conversion
4342        character, and whether or not a format_spec was provided.
4343 
4344        Convert the conversion char to 3 bits:
4345            : 000  0x0  FVC_NONE   The default if nothing specified.
4346        !s  : 001  0x1  FVC_STR
4347        !r  : 010  0x2  FVC_REPR
4348        !a  : 011  0x3  FVC_ASCII
4349 
4350        next bit is whether or not we have a format spec:
4351        yes : 100  0x4
4352        no  : 000  0x0
4353     */
4354 
4355     int conversion = e->v.FormattedValue.conversion;
4356     int oparg;
4357 
4358     /* The expression to be formatted. */
4359     VISIT(c, expr, e->v.FormattedValue.value);
4360 
4361     switch (conversion) {
4362     case 's': oparg = FVC_STR;   break;
4363     case 'r': oparg = FVC_REPR;  break;
4364     case 'a': oparg = FVC_ASCII; break;
4365     case -1:  oparg = FVC_NONE;  break;
4366     default:
4367         PyErr_Format(PyExc_SystemError,
4368                      "Unrecognized conversion character %d", conversion);
4369         return 0;
4370     }
4371     if (e->v.FormattedValue.format_spec) {
4372         /* Evaluate the format spec, and update our opcode arg. */
4373         VISIT(c, expr, e->v.FormattedValue.format_spec);
4374         oparg |= FVS_HAVE_SPEC;
4375     }
4376 
4377     /* And push our opcode and oparg */
4378     ADDOP_I(c, FORMAT_VALUE, oparg);
4379 
4380     return 1;
4381 }
4382 
4383 static int
compiler_subkwargs(struct compiler * c,asdl_keyword_seq * keywords,Py_ssize_t begin,Py_ssize_t end)4384 compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
4385 {
4386     Py_ssize_t i, n = end - begin;
4387     keyword_ty kw;
4388     PyObject *keys, *key;
4389     assert(n > 0);
4390     int big = n*2 > STACK_USE_GUIDELINE;
4391     if (n > 1 && !big) {
4392         for (i = begin; i < end; i++) {
4393             kw = asdl_seq_GET(keywords, i);
4394             VISIT(c, expr, kw->value);
4395         }
4396         keys = PyTuple_New(n);
4397         if (keys == NULL) {
4398             return 0;
4399         }
4400         for (i = begin; i < end; i++) {
4401             key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4402             Py_INCREF(key);
4403             PyTuple_SET_ITEM(keys, i - begin, key);
4404         }
4405         ADDOP_LOAD_CONST_NEW(c, keys);
4406         ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4407         return 1;
4408     }
4409     if (big) {
4410         ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4411     }
4412     for (i = begin; i < end; i++) {
4413         kw = asdl_seq_GET(keywords, i);
4414         ADDOP_LOAD_CONST(c, kw->arg);
4415         VISIT(c, expr, kw->value);
4416         if (big) {
4417             ADDOP_I_NOLINE(c, MAP_ADD, 1);
4418         }
4419     }
4420     if (!big) {
4421         ADDOP_I(c, BUILD_MAP, n);
4422     }
4423     return 1;
4424 }
4425 
4426 /* shared code between compiler_call and compiler_class */
4427 static int
compiler_call_helper(struct compiler * c,int n,asdl_expr_seq * args,asdl_keyword_seq * keywords)4428 compiler_call_helper(struct compiler *c,
4429                      int n, /* Args already pushed */
4430                      asdl_expr_seq *args,
4431                      asdl_keyword_seq *keywords)
4432 {
4433     Py_ssize_t i, nseen, nelts, nkwelts;
4434 
4435     if (validate_keywords(c, keywords) == -1) {
4436         return 0;
4437     }
4438 
4439     nelts = asdl_seq_LEN(args);
4440     nkwelts = asdl_seq_LEN(keywords);
4441 
4442     if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
4443          goto ex_call;
4444     }
4445     for (i = 0; i < nelts; i++) {
4446         expr_ty elt = asdl_seq_GET(args, i);
4447         if (elt->kind == Starred_kind) {
4448             goto ex_call;
4449         }
4450     }
4451     for (i = 0; i < nkwelts; i++) {
4452         keyword_ty kw = asdl_seq_GET(keywords, i);
4453         if (kw->arg == NULL) {
4454             goto ex_call;
4455         }
4456     }
4457 
4458     /* No * or ** args, so can use faster calling sequence */
4459     for (i = 0; i < nelts; i++) {
4460         expr_ty elt = asdl_seq_GET(args, i);
4461         assert(elt->kind != Starred_kind);
4462         VISIT(c, expr, elt);
4463     }
4464     if (nkwelts) {
4465         PyObject *names;
4466         VISIT_SEQ(c, keyword, keywords);
4467         names = PyTuple_New(nkwelts);
4468         if (names == NULL) {
4469             return 0;
4470         }
4471         for (i = 0; i < nkwelts; i++) {
4472             keyword_ty kw = asdl_seq_GET(keywords, i);
4473             Py_INCREF(kw->arg);
4474             PyTuple_SET_ITEM(names, i, kw->arg);
4475         }
4476         ADDOP_LOAD_CONST_NEW(c, names);
4477         ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4478         return 1;
4479     }
4480     else {
4481         ADDOP_I(c, CALL_FUNCTION, n + nelts);
4482         return 1;
4483     }
4484 
4485 ex_call:
4486 
4487     /* Do positional arguments. */
4488     if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4489         VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4490     }
4491     else if (starunpack_helper(c, args, n, BUILD_LIST,
4492                                  LIST_APPEND, LIST_EXTEND, 1) == 0) {
4493         return 0;
4494     }
4495     /* Then keyword arguments */
4496     if (nkwelts) {
4497         /* Has a new dict been pushed */
4498         int have_dict = 0;
4499 
4500         nseen = 0;  /* the number of keyword arguments on the stack following */
4501         for (i = 0; i < nkwelts; i++) {
4502             keyword_ty kw = asdl_seq_GET(keywords, i);
4503             if (kw->arg == NULL) {
4504                 /* A keyword argument unpacking. */
4505                 if (nseen) {
4506                     if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
4507                         return 0;
4508                     }
4509                     if (have_dict) {
4510                         ADDOP_I(c, DICT_MERGE, 1);
4511                     }
4512                     have_dict = 1;
4513                     nseen = 0;
4514                 }
4515                 if (!have_dict) {
4516                     ADDOP_I(c, BUILD_MAP, 0);
4517                     have_dict = 1;
4518                 }
4519                 VISIT(c, expr, kw->value);
4520                 ADDOP_I(c, DICT_MERGE, 1);
4521             }
4522             else {
4523                 nseen++;
4524             }
4525         }
4526         if (nseen) {
4527             /* Pack up any trailing keyword arguments. */
4528             if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
4529                 return 0;
4530             }
4531             if (have_dict) {
4532                 ADDOP_I(c, DICT_MERGE, 1);
4533             }
4534             have_dict = 1;
4535         }
4536         assert(have_dict);
4537     }
4538     ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4539     return 1;
4540 }
4541 
4542 
4543 /* List and set comprehensions and generator expressions work by creating a
4544   nested function to perform the actual iteration. This means that the
4545   iteration variables don't leak into the current scope.
4546   The defined function is called immediately following its definition, with the
4547   result of that call being the result of the expression.
4548   The LC/SC version returns the populated container, while the GE version is
4549   flagged in symtable.c as a generator, so it returns the generator object
4550   when the function is called.
4551 
4552   Possible cleanups:
4553     - iterate over the generator sequence instead of using recursion
4554 */
4555 
4556 
4557 static int
compiler_comprehension_generator(struct compiler * c,asdl_comprehension_seq * generators,int gen_index,int depth,expr_ty elt,expr_ty val,int type)4558 compiler_comprehension_generator(struct compiler *c,
4559                                  asdl_comprehension_seq *generators, int gen_index,
4560                                  int depth,
4561                                  expr_ty elt, expr_ty val, int type)
4562 {
4563     comprehension_ty gen;
4564     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4565     if (gen->is_async) {
4566         return compiler_async_comprehension_generator(
4567             c, generators, gen_index, depth, elt, val, type);
4568     } else {
4569         return compiler_sync_comprehension_generator(
4570             c, generators, gen_index, depth, elt, val, type);
4571     }
4572 }
4573 
4574 static int
compiler_sync_comprehension_generator(struct compiler * c,asdl_comprehension_seq * generators,int gen_index,int depth,expr_ty elt,expr_ty val,int type)4575 compiler_sync_comprehension_generator(struct compiler *c,
4576                                       asdl_comprehension_seq *generators, int gen_index,
4577                                       int depth,
4578                                       expr_ty elt, expr_ty val, int type)
4579 {
4580     /* generate code for the iterator, then each of the ifs,
4581        and then write to the element */
4582 
4583     comprehension_ty gen;
4584     basicblock *start, *anchor, *skip, *if_cleanup;
4585     Py_ssize_t i, n;
4586 
4587     start = compiler_new_block(c);
4588     skip = compiler_new_block(c);
4589     if_cleanup = compiler_new_block(c);
4590     anchor = compiler_new_block(c);
4591 
4592     if (start == NULL || skip == NULL || if_cleanup == NULL ||
4593         anchor == NULL)
4594         return 0;
4595 
4596     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4597 
4598     if (gen_index == 0) {
4599         /* Receive outermost iter as an implicit argument */
4600         c->u->u_argcount = 1;
4601         ADDOP_I(c, LOAD_FAST, 0);
4602     }
4603     else {
4604         /* Sub-iter - calculate on the fly */
4605         /* Fast path for the temporary variable assignment idiom:
4606              for y in [f(x)]
4607          */
4608         asdl_expr_seq *elts;
4609         switch (gen->iter->kind) {
4610             case List_kind:
4611                 elts = gen->iter->v.List.elts;
4612                 break;
4613             case Tuple_kind:
4614                 elts = gen->iter->v.Tuple.elts;
4615                 break;
4616             default:
4617                 elts = NULL;
4618         }
4619         if (asdl_seq_LEN(elts) == 1) {
4620             expr_ty elt = asdl_seq_GET(elts, 0);
4621             if (elt->kind != Starred_kind) {
4622                 VISIT(c, expr, elt);
4623                 start = NULL;
4624             }
4625         }
4626         if (start) {
4627             VISIT(c, expr, gen->iter);
4628             ADDOP(c, GET_ITER);
4629         }
4630     }
4631     if (start) {
4632         depth++;
4633         compiler_use_next_block(c, start);
4634         ADDOP_JUMP(c, FOR_ITER, anchor);
4635         NEXT_BLOCK(c);
4636     }
4637     VISIT(c, expr, gen->target);
4638 
4639     /* XXX this needs to be cleaned up...a lot! */
4640     n = asdl_seq_LEN(gen->ifs);
4641     for (i = 0; i < n; i++) {
4642         expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
4643         if (!compiler_jump_if(c, e, if_cleanup, 0))
4644             return 0;
4645         NEXT_BLOCK(c);
4646     }
4647 
4648     if (++gen_index < asdl_seq_LEN(generators))
4649         if (!compiler_comprehension_generator(c,
4650                                               generators, gen_index, depth,
4651                                               elt, val, type))
4652         return 0;
4653 
4654     /* only append after the last for generator */
4655     if (gen_index >= asdl_seq_LEN(generators)) {
4656         /* comprehension specific code */
4657         switch (type) {
4658         case COMP_GENEXP:
4659             VISIT(c, expr, elt);
4660             ADDOP(c, YIELD_VALUE);
4661             ADDOP(c, POP_TOP);
4662             break;
4663         case COMP_LISTCOMP:
4664             VISIT(c, expr, elt);
4665             ADDOP_I(c, LIST_APPEND, depth + 1);
4666             break;
4667         case COMP_SETCOMP:
4668             VISIT(c, expr, elt);
4669             ADDOP_I(c, SET_ADD, depth + 1);
4670             break;
4671         case COMP_DICTCOMP:
4672             /* With '{k: v}', k is evaluated before v, so we do
4673                the same. */
4674             VISIT(c, expr, elt);
4675             VISIT(c, expr, val);
4676             ADDOP_I(c, MAP_ADD, depth + 1);
4677             break;
4678         default:
4679             return 0;
4680         }
4681 
4682         compiler_use_next_block(c, skip);
4683     }
4684     compiler_use_next_block(c, if_cleanup);
4685     if (start) {
4686         ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
4687         compiler_use_next_block(c, anchor);
4688     }
4689 
4690     return 1;
4691 }
4692 
4693 static int
compiler_async_comprehension_generator(struct compiler * c,asdl_comprehension_seq * generators,int gen_index,int depth,expr_ty elt,expr_ty val,int type)4694 compiler_async_comprehension_generator(struct compiler *c,
4695                                       asdl_comprehension_seq *generators, int gen_index,
4696                                       int depth,
4697                                       expr_ty elt, expr_ty val, int type)
4698 {
4699     comprehension_ty gen;
4700     basicblock *start, *if_cleanup, *except;
4701     Py_ssize_t i, n;
4702     start = compiler_new_block(c);
4703     except = compiler_new_block(c);
4704     if_cleanup = compiler_new_block(c);
4705 
4706     if (start == NULL || if_cleanup == NULL || except == NULL) {
4707         return 0;
4708     }
4709 
4710     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4711 
4712     if (gen_index == 0) {
4713         /* Receive outermost iter as an implicit argument */
4714         c->u->u_argcount = 1;
4715         ADDOP_I(c, LOAD_FAST, 0);
4716     }
4717     else {
4718         /* Sub-iter - calculate on the fly */
4719         VISIT(c, expr, gen->iter);
4720         ADDOP(c, GET_AITER);
4721     }
4722 
4723     compiler_use_next_block(c, start);
4724     /* Runtime will push a block here, so we need to account for that */
4725     if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
4726                               NULL, NULL)) {
4727         return 0;
4728     }
4729 
4730     ADDOP_JUMP(c, SETUP_FINALLY, except);
4731     ADDOP(c, GET_ANEXT);
4732     ADDOP_LOAD_CONST(c, Py_None);
4733     ADDOP(c, YIELD_FROM);
4734     ADDOP(c, POP_BLOCK);
4735     VISIT(c, expr, gen->target);
4736 
4737     n = asdl_seq_LEN(gen->ifs);
4738     for (i = 0; i < n; i++) {
4739         expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
4740         if (!compiler_jump_if(c, e, if_cleanup, 0))
4741             return 0;
4742         NEXT_BLOCK(c);
4743     }
4744 
4745     depth++;
4746     if (++gen_index < asdl_seq_LEN(generators))
4747         if (!compiler_comprehension_generator(c,
4748                                               generators, gen_index, depth,
4749                                               elt, val, type))
4750         return 0;
4751 
4752     /* only append after the last for generator */
4753     if (gen_index >= asdl_seq_LEN(generators)) {
4754         /* comprehension specific code */
4755         switch (type) {
4756         case COMP_GENEXP:
4757             VISIT(c, expr, elt);
4758             ADDOP(c, YIELD_VALUE);
4759             ADDOP(c, POP_TOP);
4760             break;
4761         case COMP_LISTCOMP:
4762             VISIT(c, expr, elt);
4763             ADDOP_I(c, LIST_APPEND, depth + 1);
4764             break;
4765         case COMP_SETCOMP:
4766             VISIT(c, expr, elt);
4767             ADDOP_I(c, SET_ADD, depth + 1);
4768             break;
4769         case COMP_DICTCOMP:
4770             /* With '{k: v}', k is evaluated before v, so we do
4771                the same. */
4772             VISIT(c, expr, elt);
4773             VISIT(c, expr, val);
4774             ADDOP_I(c, MAP_ADD, depth + 1);
4775             break;
4776         default:
4777             return 0;
4778         }
4779     }
4780     compiler_use_next_block(c, if_cleanup);
4781     ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
4782 
4783     compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
4784 
4785     compiler_use_next_block(c, except);
4786     ADDOP(c, END_ASYNC_FOR);
4787 
4788     return 1;
4789 }
4790 
4791 static int
compiler_comprehension(struct compiler * c,expr_ty e,int type,identifier name,asdl_comprehension_seq * generators,expr_ty elt,expr_ty val)4792 compiler_comprehension(struct compiler *c, expr_ty e, int type,
4793                        identifier name, asdl_comprehension_seq *generators, expr_ty elt,
4794                        expr_ty val)
4795 {
4796     PyCodeObject *co = NULL;
4797     comprehension_ty outermost;
4798     PyObject *qualname = NULL;
4799     int is_async_generator = 0;
4800     int top_level_await = IS_TOP_LEVEL_AWAIT(c);
4801 
4802 
4803     int is_async_function = c->u->u_ste->ste_coroutine;
4804 
4805     outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
4806     if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4807                               (void *)e, e->lineno))
4808     {
4809         goto error;
4810     }
4811     SET_LOC(c, e);
4812 
4813     is_async_generator = c->u->u_ste->ste_coroutine;
4814 
4815     if (is_async_generator && !is_async_function && type != COMP_GENEXP  && !top_level_await) {
4816         compiler_error(c, "asynchronous comprehension outside of "
4817                           "an asynchronous function");
4818         goto error_in_scope;
4819     }
4820 
4821     if (type != COMP_GENEXP) {
4822         int op;
4823         switch (type) {
4824         case COMP_LISTCOMP:
4825             op = BUILD_LIST;
4826             break;
4827         case COMP_SETCOMP:
4828             op = BUILD_SET;
4829             break;
4830         case COMP_DICTCOMP:
4831             op = BUILD_MAP;
4832             break;
4833         default:
4834             PyErr_Format(PyExc_SystemError,
4835                          "unknown comprehension type %d", type);
4836             goto error_in_scope;
4837         }
4838 
4839         ADDOP_I(c, op, 0);
4840     }
4841 
4842     if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
4843                                           val, type))
4844         goto error_in_scope;
4845 
4846     if (type != COMP_GENEXP) {
4847         ADDOP(c, RETURN_VALUE);
4848     }
4849 
4850     co = assemble(c, 1);
4851     qualname = c->u->u_qualname;
4852     Py_INCREF(qualname);
4853     compiler_exit_scope(c);
4854     if (top_level_await && is_async_generator){
4855         c->u->u_ste->ste_coroutine = 1;
4856     }
4857     if (co == NULL)
4858         goto error;
4859 
4860     if (!compiler_make_closure(c, co, 0, qualname)) {
4861         goto error;
4862     }
4863     Py_DECREF(qualname);
4864     Py_DECREF(co);
4865 
4866     VISIT(c, expr, outermost->iter);
4867 
4868     if (outermost->is_async) {
4869         ADDOP(c, GET_AITER);
4870     } else {
4871         ADDOP(c, GET_ITER);
4872     }
4873 
4874     ADDOP_I(c, CALL_FUNCTION, 1);
4875 
4876     if (is_async_generator && type != COMP_GENEXP) {
4877         ADDOP(c, GET_AWAITABLE);
4878         ADDOP_LOAD_CONST(c, Py_None);
4879         ADDOP(c, YIELD_FROM);
4880     }
4881 
4882     return 1;
4883 error_in_scope:
4884     compiler_exit_scope(c);
4885 error:
4886     Py_XDECREF(qualname);
4887     Py_XDECREF(co);
4888     return 0;
4889 }
4890 
4891 static int
compiler_genexp(struct compiler * c,expr_ty e)4892 compiler_genexp(struct compiler *c, expr_ty e)
4893 {
4894     static identifier name;
4895     if (!name) {
4896         name = PyUnicode_InternFromString("<genexpr>");
4897         if (!name)
4898             return 0;
4899     }
4900     assert(e->kind == GeneratorExp_kind);
4901     return compiler_comprehension(c, e, COMP_GENEXP, name,
4902                                   e->v.GeneratorExp.generators,
4903                                   e->v.GeneratorExp.elt, NULL);
4904 }
4905 
4906 static int
compiler_listcomp(struct compiler * c,expr_ty e)4907 compiler_listcomp(struct compiler *c, expr_ty e)
4908 {
4909     static identifier name;
4910     if (!name) {
4911         name = PyUnicode_InternFromString("<listcomp>");
4912         if (!name)
4913             return 0;
4914     }
4915     assert(e->kind == ListComp_kind);
4916     return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4917                                   e->v.ListComp.generators,
4918                                   e->v.ListComp.elt, NULL);
4919 }
4920 
4921 static int
compiler_setcomp(struct compiler * c,expr_ty e)4922 compiler_setcomp(struct compiler *c, expr_ty e)
4923 {
4924     static identifier name;
4925     if (!name) {
4926         name = PyUnicode_InternFromString("<setcomp>");
4927         if (!name)
4928             return 0;
4929     }
4930     assert(e->kind == SetComp_kind);
4931     return compiler_comprehension(c, e, COMP_SETCOMP, name,
4932                                   e->v.SetComp.generators,
4933                                   e->v.SetComp.elt, NULL);
4934 }
4935 
4936 
4937 static int
compiler_dictcomp(struct compiler * c,expr_ty e)4938 compiler_dictcomp(struct compiler *c, expr_ty e)
4939 {
4940     static identifier name;
4941     if (!name) {
4942         name = PyUnicode_InternFromString("<dictcomp>");
4943         if (!name)
4944             return 0;
4945     }
4946     assert(e->kind == DictComp_kind);
4947     return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4948                                   e->v.DictComp.generators,
4949                                   e->v.DictComp.key, e->v.DictComp.value);
4950 }
4951 
4952 
4953 static int
compiler_visit_keyword(struct compiler * c,keyword_ty k)4954 compiler_visit_keyword(struct compiler *c, keyword_ty k)
4955 {
4956     VISIT(c, expr, k->value);
4957     return 1;
4958 }
4959 
4960 /* Test whether expression is constant.  For constants, report
4961    whether they are true or false.
4962 
4963    Return values: 1 for true, 0 for false, -1 for non-constant.
4964  */
4965 
4966 static int
compiler_with_except_finish(struct compiler * c)4967 compiler_with_except_finish(struct compiler *c) {
4968     basicblock *exit;
4969     exit = compiler_new_block(c);
4970     if (exit == NULL)
4971         return 0;
4972     ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
4973     NEXT_BLOCK(c);
4974     ADDOP_I(c, RERAISE, 1);
4975     compiler_use_next_block(c, exit);
4976     ADDOP(c, POP_TOP);
4977     ADDOP(c, POP_TOP);
4978     ADDOP(c, POP_TOP);
4979     ADDOP(c, POP_EXCEPT);
4980     ADDOP(c, POP_TOP);
4981     return 1;
4982 }
4983 
4984 /*
4985    Implements the async with statement.
4986 
4987    The semantics outlined in that PEP are as follows:
4988 
4989    async with EXPR as VAR:
4990        BLOCK
4991 
4992    It is implemented roughly as:
4993 
4994    context = EXPR
4995    exit = context.__aexit__  # not calling it
4996    value = await context.__aenter__()
4997    try:
4998        VAR = value  # if VAR present in the syntax
4999        BLOCK
5000    finally:
5001        if an exception was raised:
5002            exc = copy of (exception, instance, traceback)
5003        else:
5004            exc = (None, None, None)
5005        if not (await exit(*exc)):
5006            raise
5007  */
5008 static int
compiler_async_with(struct compiler * c,stmt_ty s,int pos)5009 compiler_async_with(struct compiler *c, stmt_ty s, int pos)
5010 {
5011     basicblock *block, *final, *exit;
5012     withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
5013 
5014     assert(s->kind == AsyncWith_kind);
5015     if (IS_TOP_LEVEL_AWAIT(c)){
5016         c->u->u_ste->ste_coroutine = 1;
5017     } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
5018         return compiler_error(c, "'async with' outside async function");
5019     }
5020 
5021     block = compiler_new_block(c);
5022     final = compiler_new_block(c);
5023     exit = compiler_new_block(c);
5024     if (!block || !final || !exit)
5025         return 0;
5026 
5027     /* Evaluate EXPR */
5028     VISIT(c, expr, item->context_expr);
5029 
5030     ADDOP(c, BEFORE_ASYNC_WITH);
5031     ADDOP(c, GET_AWAITABLE);
5032     ADDOP_LOAD_CONST(c, Py_None);
5033     ADDOP(c, YIELD_FROM);
5034 
5035     ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
5036 
5037     /* SETUP_ASYNC_WITH pushes a finally block. */
5038     compiler_use_next_block(c, block);
5039     if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
5040         return 0;
5041     }
5042 
5043     if (item->optional_vars) {
5044         VISIT(c, expr, item->optional_vars);
5045     }
5046     else {
5047     /* Discard result from context.__aenter__() */
5048         ADDOP(c, POP_TOP);
5049     }
5050 
5051     pos++;
5052     if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5053         /* BLOCK code */
5054         VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5055     else if (!compiler_async_with(c, s, pos))
5056             return 0;
5057 
5058     compiler_pop_fblock(c, ASYNC_WITH, block);
5059     ADDOP(c, POP_BLOCK);
5060     /* End of body; start the cleanup */
5061 
5062     /* For successful outcome:
5063      * call __exit__(None, None, None)
5064      */
5065     SET_LOC(c, s);
5066     if(!compiler_call_exit_with_nones(c))
5067         return 0;
5068     ADDOP(c, GET_AWAITABLE);
5069     ADDOP_LOAD_CONST(c, Py_None);
5070     ADDOP(c, YIELD_FROM);
5071 
5072     ADDOP(c, POP_TOP);
5073 
5074     ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
5075 
5076     /* For exceptional outcome: */
5077     compiler_use_next_block(c, final);
5078     ADDOP(c, WITH_EXCEPT_START);
5079     ADDOP(c, GET_AWAITABLE);
5080     ADDOP_LOAD_CONST(c, Py_None);
5081     ADDOP(c, YIELD_FROM);
5082     compiler_with_except_finish(c);
5083 
5084 compiler_use_next_block(c, exit);
5085     return 1;
5086 }
5087 
5088 
5089 /*
5090    Implements the with statement from PEP 343.
5091    with EXPR as VAR:
5092        BLOCK
5093    is implemented as:
5094         <code for EXPR>
5095         SETUP_WITH  E
5096         <code to store to VAR> or POP_TOP
5097         <code for BLOCK>
5098         LOAD_CONST (None, None, None)
5099         CALL_FUNCTION_EX 0
5100         JUMP_FORWARD  EXIT
5101     E:  WITH_EXCEPT_START (calls EXPR.__exit__)
5102         POP_JUMP_IF_TRUE T:
5103         RERAISE
5104     T:  POP_TOP * 3 (remove exception from stack)
5105         POP_EXCEPT
5106         POP_TOP
5107     EXIT:
5108  */
5109 
5110 static int
compiler_with(struct compiler * c,stmt_ty s,int pos)5111 compiler_with(struct compiler *c, stmt_ty s, int pos)
5112 {
5113     basicblock *block, *final, *exit;
5114     withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
5115 
5116     assert(s->kind == With_kind);
5117 
5118     block = compiler_new_block(c);
5119     final = compiler_new_block(c);
5120     exit = compiler_new_block(c);
5121     if (!block || !final || !exit)
5122         return 0;
5123 
5124     /* Evaluate EXPR */
5125     VISIT(c, expr, item->context_expr);
5126     /* Will push bound __exit__ */
5127     ADDOP_JUMP(c, SETUP_WITH, final);
5128 
5129     /* SETUP_WITH pushes a finally block. */
5130     compiler_use_next_block(c, block);
5131     if (!compiler_push_fblock(c, WITH, block, final, s)) {
5132         return 0;
5133     }
5134 
5135     if (item->optional_vars) {
5136         VISIT(c, expr, item->optional_vars);
5137     }
5138     else {
5139     /* Discard result from context.__enter__() */
5140         ADDOP(c, POP_TOP);
5141     }
5142 
5143     pos++;
5144     if (pos == asdl_seq_LEN(s->v.With.items))
5145         /* BLOCK code */
5146         VISIT_SEQ(c, stmt, s->v.With.body)
5147     else if (!compiler_with(c, s, pos))
5148             return 0;
5149 
5150 
5151     /* Mark all following code as artificial */
5152     c->u->u_lineno = -1;
5153     ADDOP(c, POP_BLOCK);
5154     compiler_pop_fblock(c, WITH, block);
5155 
5156     /* End of body; start the cleanup. */
5157 
5158     /* For successful outcome:
5159      * call __exit__(None, None, None)
5160      */
5161     SET_LOC(c, s);
5162     if (!compiler_call_exit_with_nones(c))
5163         return 0;
5164     ADDOP(c, POP_TOP);
5165     ADDOP_JUMP(c, JUMP_FORWARD, exit);
5166 
5167     /* For exceptional outcome: */
5168     compiler_use_next_block(c, final);
5169     ADDOP(c, WITH_EXCEPT_START);
5170     compiler_with_except_finish(c);
5171 
5172     compiler_use_next_block(c, exit);
5173     return 1;
5174 }
5175 
5176 static int
compiler_visit_expr1(struct compiler * c,expr_ty e)5177 compiler_visit_expr1(struct compiler *c, expr_ty e)
5178 {
5179     switch (e->kind) {
5180     case NamedExpr_kind:
5181         VISIT(c, expr, e->v.NamedExpr.value);
5182         ADDOP(c, DUP_TOP);
5183         VISIT(c, expr, e->v.NamedExpr.target);
5184         break;
5185     case BoolOp_kind:
5186         return compiler_boolop(c, e);
5187     case BinOp_kind:
5188         VISIT(c, expr, e->v.BinOp.left);
5189         VISIT(c, expr, e->v.BinOp.right);
5190         ADDOP(c, binop(e->v.BinOp.op));
5191         break;
5192     case UnaryOp_kind:
5193         VISIT(c, expr, e->v.UnaryOp.operand);
5194         ADDOP(c, unaryop(e->v.UnaryOp.op));
5195         break;
5196     case Lambda_kind:
5197         return compiler_lambda(c, e);
5198     case IfExp_kind:
5199         return compiler_ifexp(c, e);
5200     case Dict_kind:
5201         return compiler_dict(c, e);
5202     case Set_kind:
5203         return compiler_set(c, e);
5204     case GeneratorExp_kind:
5205         return compiler_genexp(c, e);
5206     case ListComp_kind:
5207         return compiler_listcomp(c, e);
5208     case SetComp_kind:
5209         return compiler_setcomp(c, e);
5210     case DictComp_kind:
5211         return compiler_dictcomp(c, e);
5212     case Yield_kind:
5213         if (c->u->u_ste->ste_type != FunctionBlock)
5214             return compiler_error(c, "'yield' outside function");
5215         if (e->v.Yield.value) {
5216             VISIT(c, expr, e->v.Yield.value);
5217         }
5218         else {
5219             ADDOP_LOAD_CONST(c, Py_None);
5220         }
5221         ADDOP(c, YIELD_VALUE);
5222         break;
5223     case YieldFrom_kind:
5224         if (c->u->u_ste->ste_type != FunctionBlock)
5225             return compiler_error(c, "'yield' outside function");
5226 
5227         if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5228             return compiler_error(c, "'yield from' inside async function");
5229 
5230         VISIT(c, expr, e->v.YieldFrom.value);
5231         ADDOP(c, GET_YIELD_FROM_ITER);
5232         ADDOP_LOAD_CONST(c, Py_None);
5233         ADDOP(c, YIELD_FROM);
5234         break;
5235     case Await_kind:
5236         if (!IS_TOP_LEVEL_AWAIT(c)){
5237             if (c->u->u_ste->ste_type != FunctionBlock){
5238                 return compiler_error(c, "'await' outside function");
5239             }
5240 
5241             if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
5242                     c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5243                 return compiler_error(c, "'await' outside async function");
5244             }
5245         }
5246 
5247         VISIT(c, expr, e->v.Await.value);
5248         ADDOP(c, GET_AWAITABLE);
5249         ADDOP_LOAD_CONST(c, Py_None);
5250         ADDOP(c, YIELD_FROM);
5251         break;
5252     case Compare_kind:
5253         return compiler_compare(c, e);
5254     case Call_kind:
5255         return compiler_call(c, e);
5256     case Constant_kind:
5257         ADDOP_LOAD_CONST(c, e->v.Constant.value);
5258         break;
5259     case JoinedStr_kind:
5260         return compiler_joined_str(c, e);
5261     case FormattedValue_kind:
5262         return compiler_formatted_value(c, e);
5263     /* The following exprs can be assignment targets. */
5264     case Attribute_kind:
5265         VISIT(c, expr, e->v.Attribute.value);
5266         switch (e->v.Attribute.ctx) {
5267         case Load:
5268         {
5269             int old_lineno = c->u->u_lineno;
5270             c->u->u_lineno = e->end_lineno;
5271             ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5272             c->u->u_lineno = old_lineno;
5273             break;
5274         }
5275         case Store:
5276             if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
5277                 return 0;
5278             }
5279             int old_lineno = c->u->u_lineno;
5280             c->u->u_lineno = e->end_lineno;
5281             ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5282             c->u->u_lineno = old_lineno;
5283             break;
5284         case Del:
5285             ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5286             break;
5287         }
5288         break;
5289     case Subscript_kind:
5290         return compiler_subscript(c, e);
5291     case Starred_kind:
5292         switch (e->v.Starred.ctx) {
5293         case Store:
5294             /* In all legitimate cases, the Starred node was already replaced
5295              * by compiler_list/compiler_tuple. XXX: is that okay? */
5296             return compiler_error(c,
5297                 "starred assignment target must be in a list or tuple");
5298         default:
5299             return compiler_error(c,
5300                 "can't use starred expression here");
5301         }
5302         break;
5303     case Slice_kind:
5304         return compiler_slice(c, e);
5305     case Name_kind:
5306         return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5307     /* child nodes of List and Tuple will have expr_context set */
5308     case List_kind:
5309         return compiler_list(c, e);
5310     case Tuple_kind:
5311         return compiler_tuple(c, e);
5312     }
5313     return 1;
5314 }
5315 
5316 static int
compiler_visit_expr(struct compiler * c,expr_ty e)5317 compiler_visit_expr(struct compiler *c, expr_ty e)
5318 {
5319     int old_lineno = c->u->u_lineno;
5320     int old_end_lineno = c->u->u_end_lineno;
5321     int old_col_offset = c->u->u_col_offset;
5322     int old_end_col_offset = c->u->u_end_col_offset;
5323     SET_LOC(c, e);
5324     int res = compiler_visit_expr1(c, e);
5325     c->u->u_lineno = old_lineno;
5326     c->u->u_end_lineno = old_end_lineno;
5327     c->u->u_col_offset = old_col_offset;
5328     c->u->u_end_col_offset = old_end_col_offset;
5329     return res;
5330 }
5331 
5332 static int
compiler_augassign(struct compiler * c,stmt_ty s)5333 compiler_augassign(struct compiler *c, stmt_ty s)
5334 {
5335     assert(s->kind == AugAssign_kind);
5336     expr_ty e = s->v.AugAssign.target;
5337 
5338     int old_lineno = c->u->u_lineno;
5339     int old_end_lineno = c->u->u_end_lineno;
5340     int old_col_offset = c->u->u_col_offset;
5341     int old_end_col_offset = c->u->u_end_col_offset;
5342     SET_LOC(c, e);
5343 
5344     switch (e->kind) {
5345     case Attribute_kind:
5346         VISIT(c, expr, e->v.Attribute.value);
5347         ADDOP(c, DUP_TOP);
5348         int old_lineno = c->u->u_lineno;
5349         c->u->u_lineno = e->end_lineno;
5350         ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5351         c->u->u_lineno = old_lineno;
5352         break;
5353     case Subscript_kind:
5354         VISIT(c, expr, e->v.Subscript.value);
5355         VISIT(c, expr, e->v.Subscript.slice);
5356         ADDOP(c, DUP_TOP_TWO);
5357         ADDOP(c, BINARY_SUBSCR);
5358         break;
5359     case Name_kind:
5360         if (!compiler_nameop(c, e->v.Name.id, Load))
5361             return 0;
5362         break;
5363     default:
5364         PyErr_Format(PyExc_SystemError,
5365             "invalid node type (%d) for augmented assignment",
5366             e->kind);
5367         return 0;
5368     }
5369 
5370     c->u->u_lineno = old_lineno;
5371     c->u->u_end_lineno = old_end_lineno;
5372     c->u->u_col_offset = old_col_offset;
5373     c->u->u_end_col_offset = old_end_col_offset;
5374 
5375     VISIT(c, expr, s->v.AugAssign.value);
5376     ADDOP(c, inplace_binop(s->v.AugAssign.op));
5377 
5378     SET_LOC(c, e);
5379 
5380     switch (e->kind) {
5381     case Attribute_kind:
5382         c->u->u_lineno = e->end_lineno;
5383         ADDOP(c, ROT_TWO);
5384         ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5385         break;
5386     case Subscript_kind:
5387         ADDOP(c, ROT_THREE);
5388         ADDOP(c, STORE_SUBSCR);
5389         break;
5390     case Name_kind:
5391         return compiler_nameop(c, e->v.Name.id, Store);
5392     default:
5393         Py_UNREACHABLE();
5394     }
5395     return 1;
5396 }
5397 
5398 static int
check_ann_expr(struct compiler * c,expr_ty e)5399 check_ann_expr(struct compiler *c, expr_ty e)
5400 {
5401     VISIT(c, expr, e);
5402     ADDOP(c, POP_TOP);
5403     return 1;
5404 }
5405 
5406 static int
check_annotation(struct compiler * c,stmt_ty s)5407 check_annotation(struct compiler *c, stmt_ty s)
5408 {
5409     /* Annotations of complex targets does not produce anything
5410        under annotations future */
5411     if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5412         return 1;
5413     }
5414 
5415     /* Annotations are only evaluated in a module or class. */
5416     if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5417         c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5418         return check_ann_expr(c, s->v.AnnAssign.annotation);
5419     }
5420     return 1;
5421 }
5422 
5423 static int
check_ann_subscr(struct compiler * c,expr_ty e)5424 check_ann_subscr(struct compiler *c, expr_ty e)
5425 {
5426     /* We check that everything in a subscript is defined at runtime. */
5427     switch (e->kind) {
5428     case Slice_kind:
5429         if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
5430             return 0;
5431         }
5432         if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5433             return 0;
5434         }
5435         if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5436             return 0;
5437         }
5438         return 1;
5439     case Tuple_kind: {
5440         /* extended slice */
5441         asdl_expr_seq *elts = e->v.Tuple.elts;
5442         Py_ssize_t i, n = asdl_seq_LEN(elts);
5443         for (i = 0; i < n; i++) {
5444             if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
5445                 return 0;
5446             }
5447         }
5448         return 1;
5449     }
5450     default:
5451         return check_ann_expr(c, e);
5452     }
5453 }
5454 
5455 static int
compiler_annassign(struct compiler * c,stmt_ty s)5456 compiler_annassign(struct compiler *c, stmt_ty s)
5457 {
5458     expr_ty targ = s->v.AnnAssign.target;
5459     PyObject* mangled;
5460 
5461     assert(s->kind == AnnAssign_kind);
5462 
5463     /* We perform the actual assignment first. */
5464     if (s->v.AnnAssign.value) {
5465         VISIT(c, expr, s->v.AnnAssign.value);
5466         VISIT(c, expr, targ);
5467     }
5468     switch (targ->kind) {
5469     case Name_kind:
5470         if (forbidden_name(c, targ->v.Name.id, Store))
5471             return 0;
5472         /* If we have a simple name in a module or class, store annotation. */
5473         if (s->v.AnnAssign.simple &&
5474             (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5475              c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
5476             if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5477                 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5478             }
5479             else {
5480                 VISIT(c, expr, s->v.AnnAssign.annotation);
5481             }
5482             ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
5483             mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
5484             ADDOP_LOAD_CONST_NEW(c, mangled);
5485             ADDOP(c, STORE_SUBSCR);
5486         }
5487         break;
5488     case Attribute_kind:
5489         if (forbidden_name(c, targ->v.Attribute.attr, Store))
5490             return 0;
5491         if (!s->v.AnnAssign.value &&
5492             !check_ann_expr(c, targ->v.Attribute.value)) {
5493             return 0;
5494         }
5495         break;
5496     case Subscript_kind:
5497         if (!s->v.AnnAssign.value &&
5498             (!check_ann_expr(c, targ->v.Subscript.value) ||
5499              !check_ann_subscr(c, targ->v.Subscript.slice))) {
5500                 return 0;
5501         }
5502         break;
5503     default:
5504         PyErr_Format(PyExc_SystemError,
5505                      "invalid node type (%d) for annotated assignment",
5506                      targ->kind);
5507             return 0;
5508     }
5509     /* Annotation is evaluated last. */
5510     if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5511         return 0;
5512     }
5513     return 1;
5514 }
5515 
5516 /* Raises a SyntaxError and returns 0.
5517    If something goes wrong, a different exception may be raised.
5518 */
5519 
5520 static int
compiler_error(struct compiler * c,const char * format,...)5521 compiler_error(struct compiler *c, const char *format, ...)
5522 {
5523     va_list vargs;
5524 #ifdef HAVE_STDARG_PROTOTYPES
5525     va_start(vargs, format);
5526 #else
5527     va_start(vargs);
5528 #endif
5529     PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5530     va_end(vargs);
5531     if (msg == NULL) {
5532         return 0;
5533     }
5534     PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
5535     if (loc == NULL) {
5536         Py_INCREF(Py_None);
5537         loc = Py_None;
5538     }
5539     PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
5540                                    c->u->u_lineno, c->u->u_col_offset + 1, loc,
5541                                    c->u->u_end_lineno, c->u->u_end_col_offset + 1);
5542     Py_DECREF(msg);
5543     if (args == NULL) {
5544         goto exit;
5545     }
5546     PyErr_SetObject(PyExc_SyntaxError, args);
5547  exit:
5548     Py_DECREF(loc);
5549     Py_XDECREF(args);
5550     return 0;
5551 }
5552 
5553 /* Emits a SyntaxWarning and returns 1 on success.
5554    If a SyntaxWarning raised as error, replaces it with a SyntaxError
5555    and returns 0.
5556 */
5557 static int
compiler_warn(struct compiler * c,const char * format,...)5558 compiler_warn(struct compiler *c, const char *format, ...)
5559 {
5560     va_list vargs;
5561 #ifdef HAVE_STDARG_PROTOTYPES
5562     va_start(vargs, format);
5563 #else
5564     va_start(vargs);
5565 #endif
5566     PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5567     va_end(vargs);
5568     if (msg == NULL) {
5569         return 0;
5570     }
5571     if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5572                                  c->u->u_lineno, NULL, NULL) < 0)
5573     {
5574         if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
5575             /* Replace the SyntaxWarning exception with a SyntaxError
5576                to get a more accurate error report */
5577             PyErr_Clear();
5578             assert(PyUnicode_AsUTF8(msg) != NULL);
5579             compiler_error(c, PyUnicode_AsUTF8(msg));
5580         }
5581         Py_DECREF(msg);
5582         return 0;
5583     }
5584     Py_DECREF(msg);
5585     return 1;
5586 }
5587 
5588 static int
compiler_subscript(struct compiler * c,expr_ty e)5589 compiler_subscript(struct compiler *c, expr_ty e)
5590 {
5591     expr_context_ty ctx = e->v.Subscript.ctx;
5592     int op = 0;
5593 
5594     if (ctx == Load) {
5595         if (!check_subscripter(c, e->v.Subscript.value)) {
5596             return 0;
5597         }
5598         if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5599             return 0;
5600         }
5601     }
5602 
5603     switch (ctx) {
5604         case Load:    op = BINARY_SUBSCR; break;
5605         case Store:   op = STORE_SUBSCR; break;
5606         case Del:     op = DELETE_SUBSCR; break;
5607     }
5608     assert(op);
5609     VISIT(c, expr, e->v.Subscript.value);
5610     VISIT(c, expr, e->v.Subscript.slice);
5611     ADDOP(c, op);
5612     return 1;
5613 }
5614 
5615 static int
compiler_slice(struct compiler * c,expr_ty s)5616 compiler_slice(struct compiler *c, expr_ty s)
5617 {
5618     int n = 2;
5619     assert(s->kind == Slice_kind);
5620 
5621     /* only handles the cases where BUILD_SLICE is emitted */
5622     if (s->v.Slice.lower) {
5623         VISIT(c, expr, s->v.Slice.lower);
5624     }
5625     else {
5626         ADDOP_LOAD_CONST(c, Py_None);
5627     }
5628 
5629     if (s->v.Slice.upper) {
5630         VISIT(c, expr, s->v.Slice.upper);
5631     }
5632     else {
5633         ADDOP_LOAD_CONST(c, Py_None);
5634     }
5635 
5636     if (s->v.Slice.step) {
5637         n++;
5638         VISIT(c, expr, s->v.Slice.step);
5639     }
5640     ADDOP_I(c, BUILD_SLICE, n);
5641     return 1;
5642 }
5643 
5644 
5645 // PEP 634: Structural Pattern Matching
5646 
5647 // To keep things simple, all compiler_pattern_* and pattern_helper_* routines
5648 // follow the convention of consuming TOS (the subject for the given pattern)
5649 // and calling jump_to_fail_pop on failure (no match).
5650 
5651 // When calling into these routines, it's important that pc->on_top be kept
5652 // updated to reflect the current number of items that we are using on the top
5653 // of the stack: they will be popped on failure, and any name captures will be
5654 // stored *underneath* them on success. This lets us defer all names stores
5655 // until the *entire* pattern matches.
5656 
5657 #define WILDCARD_CHECK(N) \
5658     ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
5659 
5660 #define WILDCARD_STAR_CHECK(N) \
5661     ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
5662 
5663 // Limit permitted subexpressions, even if the parser & AST validator let them through
5664 #define MATCH_VALUE_EXPR(N) \
5665     ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
5666 
5667 // Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
5668 static int
ensure_fail_pop(struct compiler * c,pattern_context * pc,Py_ssize_t n)5669 ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
5670 {
5671     Py_ssize_t size = n + 1;
5672     if (size <= pc->fail_pop_size) {
5673         return 1;
5674     }
5675     Py_ssize_t needed = sizeof(basicblock*) * size;
5676     basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
5677     if (resized == NULL) {
5678         PyErr_NoMemory();
5679         return 0;
5680     }
5681     pc->fail_pop = resized;
5682     while (pc->fail_pop_size < size) {
5683         basicblock *new_block;
5684         RETURN_IF_FALSE(new_block = compiler_new_block(c));
5685         pc->fail_pop[pc->fail_pop_size++] = new_block;
5686     }
5687     return 1;
5688 }
5689 
5690 // Use op to jump to the correct fail_pop block.
5691 static int
jump_to_fail_pop(struct compiler * c,pattern_context * pc,int op)5692 jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
5693 {
5694     // Pop any items on the top of the stack, plus any objects we were going to
5695     // capture on success:
5696     Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
5697     RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
5698     ADDOP_JUMP(c, op, pc->fail_pop[pops]);
5699     NEXT_BLOCK(c);
5700     return 1;
5701 }
5702 
5703 // Build all of the fail_pop blocks and reset fail_pop.
5704 static int
emit_and_reset_fail_pop(struct compiler * c,pattern_context * pc)5705 emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
5706 {
5707     if (!pc->fail_pop_size) {
5708         assert(pc->fail_pop == NULL);
5709         NEXT_BLOCK(c);
5710         return 1;
5711     }
5712     while (--pc->fail_pop_size) {
5713         compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
5714         if (!compiler_addop(c, POP_TOP)) {
5715             pc->fail_pop_size = 0;
5716             PyObject_Free(pc->fail_pop);
5717             pc->fail_pop = NULL;
5718             return 0;
5719         }
5720     }
5721     compiler_use_next_block(c, pc->fail_pop[0]);
5722     PyObject_Free(pc->fail_pop);
5723     pc->fail_pop = NULL;
5724     return 1;
5725 }
5726 
5727 static int
compiler_error_duplicate_store(struct compiler * c,identifier n)5728 compiler_error_duplicate_store(struct compiler *c, identifier n)
5729 {
5730     return compiler_error(c, "multiple assignments to name %R in pattern", n);
5731 }
5732 
5733 static int
pattern_helper_store_name(struct compiler * c,identifier n,pattern_context * pc)5734 pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
5735 {
5736     if (n == NULL) {
5737         ADDOP(c, POP_TOP);
5738         return 1;
5739     }
5740     if (forbidden_name(c, n, Store)) {
5741         return 0;
5742     }
5743     // Can't assign to the same name twice:
5744     int duplicate = PySequence_Contains(pc->stores, n);
5745     if (duplicate < 0) {
5746         return 0;
5747     }
5748     if (duplicate) {
5749         return compiler_error_duplicate_store(c, n);
5750     }
5751     // Rotate this object underneath any items we need to preserve:
5752     ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1);
5753     return !PyList_Append(pc->stores, n);
5754 }
5755 
5756 
5757 static int
pattern_unpack_helper(struct compiler * c,asdl_pattern_seq * elts)5758 pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
5759 {
5760     Py_ssize_t n = asdl_seq_LEN(elts);
5761     int seen_star = 0;
5762     for (Py_ssize_t i = 0; i < n; i++) {
5763         pattern_ty elt = asdl_seq_GET(elts, i);
5764         if (elt->kind == MatchStar_kind && !seen_star) {
5765             if ((i >= (1 << 8)) ||
5766                 (n-i-1 >= (INT_MAX >> 8)))
5767                 return compiler_error(c,
5768                     "too many expressions in "
5769                     "star-unpacking sequence pattern");
5770             ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
5771             seen_star = 1;
5772         }
5773         else if (elt->kind == MatchStar_kind) {
5774             return compiler_error(c,
5775                 "multiple starred expressions in sequence pattern");
5776         }
5777     }
5778     if (!seen_star) {
5779         ADDOP_I(c, UNPACK_SEQUENCE, n);
5780     }
5781     return 1;
5782 }
5783 
5784 static int
pattern_helper_sequence_unpack(struct compiler * c,asdl_pattern_seq * patterns,Py_ssize_t star,pattern_context * pc)5785 pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
5786                                Py_ssize_t star, pattern_context *pc)
5787 {
5788     RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
5789     Py_ssize_t size = asdl_seq_LEN(patterns);
5790     // We've now got a bunch of new subjects on the stack. They need to remain
5791     // there after each subpattern match:
5792     pc->on_top += size;
5793     for (Py_ssize_t i = 0; i < size; i++) {
5794         // One less item to keep track of each time we loop through:
5795         pc->on_top--;
5796         pattern_ty pattern = asdl_seq_GET(patterns, i);
5797         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
5798     }
5799     return 1;
5800 }
5801 
5802 // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
5803 // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
5804 // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
5805 static int
pattern_helper_sequence_subscr(struct compiler * c,asdl_pattern_seq * patterns,Py_ssize_t star,pattern_context * pc)5806 pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
5807                                Py_ssize_t star, pattern_context *pc)
5808 {
5809     // We need to keep the subject around for extracting elements:
5810     pc->on_top++;
5811     Py_ssize_t size = asdl_seq_LEN(patterns);
5812     for (Py_ssize_t i = 0; i < size; i++) {
5813         pattern_ty pattern = asdl_seq_GET(patterns, i);
5814         if (WILDCARD_CHECK(pattern)) {
5815             continue;
5816         }
5817         if (i == star) {
5818             assert(WILDCARD_STAR_CHECK(pattern));
5819             continue;
5820         }
5821         ADDOP(c, DUP_TOP);
5822         if (i < star) {
5823             ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5824         }
5825         else {
5826             // The subject may not support negative indexing! Compute a
5827             // nonnegative index:
5828             ADDOP(c, GET_LEN);
5829             ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
5830             ADDOP(c, BINARY_SUBTRACT);
5831         }
5832         ADDOP(c, BINARY_SUBSCR);
5833         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
5834     }
5835     // Pop the subject, we're done with it:
5836     pc->on_top--;
5837     ADDOP(c, POP_TOP);
5838     return 1;
5839 }
5840 
5841 // Like compiler_pattern, but turn off checks for irrefutability.
5842 static int
compiler_pattern_subpattern(struct compiler * c,pattern_ty p,pattern_context * pc)5843 compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
5844 {
5845     int allow_irrefutable = pc->allow_irrefutable;
5846     pc->allow_irrefutable = 1;
5847     RETURN_IF_FALSE(compiler_pattern(c, p, pc));
5848     pc->allow_irrefutable = allow_irrefutable;
5849     return 1;
5850 }
5851 
5852 static int
compiler_pattern_as(struct compiler * c,pattern_ty p,pattern_context * pc)5853 compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
5854 {
5855     assert(p->kind == MatchAs_kind);
5856     if (p->v.MatchAs.pattern == NULL) {
5857         // An irrefutable match:
5858         if (!pc->allow_irrefutable) {
5859             if (p->v.MatchAs.name) {
5860                 const char *e = "name capture %R makes remaining patterns unreachable";
5861                 return compiler_error(c, e, p->v.MatchAs.name);
5862             }
5863             const char *e = "wildcard makes remaining patterns unreachable";
5864             return compiler_error(c, e);
5865         }
5866         return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
5867     }
5868     // Need to make a copy for (possibly) storing later:
5869     pc->on_top++;
5870     ADDOP(c, DUP_TOP);
5871     RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
5872     // Success! Store it:
5873     pc->on_top--;
5874     RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
5875     return 1;
5876 }
5877 
5878 static int
compiler_pattern_star(struct compiler * c,pattern_ty p,pattern_context * pc)5879 compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
5880 {
5881     assert(p->kind == MatchStar_kind);
5882     RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
5883     return 1;
5884 }
5885 
5886 static int
validate_kwd_attrs(struct compiler * c,asdl_identifier_seq * attrs,asdl_pattern_seq * patterns)5887 validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
5888 {
5889     // Any errors will point to the pattern rather than the arg name as the
5890     // parser is only supplying identifiers rather than Name or keyword nodes
5891     Py_ssize_t nattrs = asdl_seq_LEN(attrs);
5892     for (Py_ssize_t i = 0; i < nattrs; i++) {
5893         identifier attr = ((identifier)asdl_seq_GET(attrs, i));
5894         SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
5895         if (forbidden_name(c, attr, Store)) {
5896             return -1;
5897         }
5898         for (Py_ssize_t j = i + 1; j < nattrs; j++) {
5899             identifier other = ((identifier)asdl_seq_GET(attrs, j));
5900             if (!PyUnicode_Compare(attr, other)) {
5901                 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
5902                 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
5903                 return -1;
5904             }
5905         }
5906     }
5907     return 0;
5908 }
5909 
5910 static int
compiler_pattern_class(struct compiler * c,pattern_ty p,pattern_context * pc)5911 compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
5912 {
5913     assert(p->kind == MatchClass_kind);
5914     asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
5915     asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
5916     asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
5917     Py_ssize_t nargs = asdl_seq_LEN(patterns);
5918     Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
5919     Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
5920     if (nattrs != nkwd_patterns) {
5921         // AST validator shouldn't let this happen, but if it does,
5922         // just fail, don't crash out of the interpreter
5923         const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
5924         return compiler_error(c, e, nattrs, nkwd_patterns);
5925     }
5926     if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
5927         const char *e = "too many sub-patterns in class pattern %R";
5928         return compiler_error(c, e, p->v.MatchClass.cls);
5929     }
5930     if (nattrs) {
5931         RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
5932         SET_LOC(c, p);
5933     }
5934     VISIT(c, expr, p->v.MatchClass.cls);
5935     PyObject *attr_names;
5936     RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
5937     Py_ssize_t i;
5938     for (i = 0; i < nattrs; i++) {
5939         PyObject *name = asdl_seq_GET(kwd_attrs, i);
5940         Py_INCREF(name);
5941         PyTuple_SET_ITEM(attr_names, i, name);
5942     }
5943     ADDOP_LOAD_CONST_NEW(c, attr_names);
5944     ADDOP_I(c, MATCH_CLASS, nargs);
5945     // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it:
5946     pc->on_top++;
5947     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
5948     for (i = 0; i < nargs + nattrs; i++) {
5949         pattern_ty pattern;
5950         if (i < nargs) {
5951             // Positional:
5952             pattern = asdl_seq_GET(patterns, i);
5953         }
5954         else {
5955             // Keyword:
5956             pattern = asdl_seq_GET(kwd_patterns, i - nargs);
5957         }
5958         if (WILDCARD_CHECK(pattern)) {
5959             continue;
5960         }
5961         // Get the i-th attribute, and match it against the i-th pattern:
5962         ADDOP(c, DUP_TOP);
5963         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
5964         ADDOP(c, BINARY_SUBSCR);
5965         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
5966     }
5967     // Success! Pop the tuple of attributes:
5968     pc->on_top--;
5969     ADDOP(c, POP_TOP);
5970     return 1;
5971 }
5972 
5973 static int
compiler_pattern_mapping(struct compiler * c,pattern_ty p,pattern_context * pc)5974 compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
5975 {
5976     assert(p->kind == MatchMapping_kind);
5977     asdl_expr_seq *keys = p->v.MatchMapping.keys;
5978     asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
5979     Py_ssize_t size = asdl_seq_LEN(keys);
5980     Py_ssize_t npatterns = asdl_seq_LEN(patterns);
5981     if (size != npatterns) {
5982         // AST validator shouldn't let this happen, but if it does,
5983         // just fail, don't crash out of the interpreter
5984         const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
5985         return compiler_error(c, e, size, npatterns);
5986     }
5987     // We have a double-star target if "rest" is set
5988     PyObject *star_target = p->v.MatchMapping.rest;
5989     // We need to keep the subject on top during the mapping and length checks:
5990     pc->on_top++;
5991     ADDOP(c, MATCH_MAPPING);
5992     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
5993     if (!size && !star_target) {
5994         // If the pattern is just "{}", we're done! Pop the subject:
5995         pc->on_top--;
5996         ADDOP(c, POP_TOP);
5997         return 1;
5998     }
5999     if (size) {
6000         // If the pattern has any keys in it, perform a length check:
6001         ADDOP(c, GET_LEN);
6002         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6003         ADDOP_COMPARE(c, GtE);
6004         RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6005     }
6006     if (INT_MAX < size - 1) {
6007         return compiler_error(c, "too many sub-patterns in mapping pattern");
6008     }
6009     // Collect all of the keys into a tuple for MATCH_KEYS and
6010     // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals:
6011 
6012     // Maintaining a set of Constant_kind kind keys allows us to raise a
6013     // SyntaxError in the case of duplicates.
6014     PyObject *seen = PySet_New(NULL);
6015     if (seen == NULL) {
6016         return 0;
6017     }
6018 
6019     // NOTE: goto error on failure in the loop below to avoid leaking `seen`
6020     for (Py_ssize_t i = 0; i < size; i++) {
6021         expr_ty key = asdl_seq_GET(keys, i);
6022         if (key == NULL) {
6023             const char *e = "can't use NULL keys in MatchMapping "
6024                             "(set 'rest' parameter instead)";
6025             SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
6026             compiler_error(c, e);
6027             goto error;
6028         }
6029 
6030         if (key->kind == Constant_kind) {
6031             int in_seen = PySet_Contains(seen, key->v.Constant.value);
6032             if (in_seen < 0) {
6033                 goto error;
6034             }
6035             if (in_seen) {
6036                 const char *e = "mapping pattern checks duplicate key (%R)";
6037                 compiler_error(c, e, key->v.Constant.value);
6038                 goto error;
6039             }
6040             if (PySet_Add(seen, key->v.Constant.value)) {
6041                 goto error;
6042             }
6043         }
6044 
6045         else if (key->kind != Attribute_kind) {
6046             const char *e = "mapping pattern keys may only match literals and attribute lookups";
6047             compiler_error(c, e);
6048             goto error;
6049         }
6050         if (!compiler_visit_expr(c, key)) {
6051             goto error;
6052         }
6053     }
6054 
6055     // all keys have been checked; there are no duplicates
6056     Py_DECREF(seen);
6057 
6058     ADDOP_I(c, BUILD_TUPLE, size);
6059     ADDOP(c, MATCH_KEYS);
6060     // There's now a tuple of keys and a tuple of values on top of the subject:
6061     pc->on_top += 2;
6062     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6063     // So far so good. Use that tuple of values on the stack to match
6064     // sub-patterns against:
6065     for (Py_ssize_t i = 0; i < size; i++) {
6066         pattern_ty pattern = asdl_seq_GET(patterns, i);
6067         if (WILDCARD_CHECK(pattern)) {
6068             continue;
6069         }
6070         ADDOP(c, DUP_TOP);
6071         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6072         ADDOP(c, BINARY_SUBSCR);
6073         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
6074     }
6075     // If we get this far, it's a match! We're done with the tuple of values,
6076     // and whatever happens next should consume the tuple of keys underneath it:
6077     pc->on_top -= 2;
6078     ADDOP(c, POP_TOP);
6079     if (star_target) {
6080         // If we have a starred name, bind a dict of remaining items to it:
6081         ADDOP(c, COPY_DICT_WITHOUT_KEYS);
6082         RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
6083     }
6084     else {
6085         // Otherwise, we don't care about this tuple of keys anymore:
6086         ADDOP(c, POP_TOP);
6087     }
6088     // Pop the subject:
6089     pc->on_top--;
6090     ADDOP(c, POP_TOP);
6091     return 1;
6092 
6093 error:
6094     Py_DECREF(seen);
6095     return 0;
6096 }
6097 
6098 static int
compiler_pattern_or(struct compiler * c,pattern_ty p,pattern_context * pc)6099 compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
6100 {
6101     assert(p->kind == MatchOr_kind);
6102     basicblock *end;
6103     RETURN_IF_FALSE(end = compiler_new_block(c));
6104     Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6105     assert(size > 1);
6106     // We're going to be messing with pc. Keep the original info handy:
6107     pattern_context old_pc = *pc;
6108     Py_INCREF(pc->stores);
6109     // control is the list of names bound by the first alternative. It is used
6110     // for checking different name bindings in alternatives, and for correcting
6111     // the order in which extracted elements are placed on the stack.
6112     PyObject *control = NULL;
6113     // NOTE: We can't use returning macros anymore! goto error on error.
6114     for (Py_ssize_t i = 0; i < size; i++) {
6115         pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
6116         SET_LOC(c, alt);
6117         PyObject *pc_stores = PyList_New(0);
6118         if (pc_stores == NULL) {
6119             goto error;
6120         }
6121         Py_SETREF(pc->stores, pc_stores);
6122         // An irrefutable sub-pattern must be last, if it is allowed at all:
6123         pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6124         pc->fail_pop = NULL;
6125         pc->fail_pop_size = 0;
6126         pc->on_top = 0;
6127         if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) {
6128             goto error;
6129         }
6130         // Success!
6131         Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6132         if (!i) {
6133             // This is the first alternative, so save its stores as a "control"
6134             // for the others (they can't bind a different set of names, and
6135             // might need to be reordered):
6136             assert(control == NULL);
6137             control = pc->stores;
6138             Py_INCREF(control);
6139         }
6140         else if (nstores != PyList_GET_SIZE(control)) {
6141             goto diff;
6142         }
6143         else if (nstores) {
6144             // There were captures. Check to see if we differ from control:
6145             Py_ssize_t icontrol = nstores;
6146             while (icontrol--) {
6147                 PyObject *name = PyList_GET_ITEM(control, icontrol);
6148                 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6149                 if (istores < 0) {
6150                     PyErr_Clear();
6151                     goto diff;
6152                 }
6153                 if (icontrol != istores) {
6154                     // Reorder the names on the stack to match the order of the
6155                     // names in control. There's probably a better way of doing
6156                     // this; the current solution is potentially very
6157                     // inefficient when each alternative subpattern binds lots
6158                     // of names in different orders. It's fine for reasonable
6159                     // cases, though.
6160                     assert(istores < icontrol);
6161                     Py_ssize_t rotations = istores + 1;
6162                     // Perform the same rotation on pc->stores:
6163                     PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6164                                                         rotations);
6165                     if (rotated == NULL ||
6166                         PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6167                         PyList_SetSlice(pc->stores, icontrol - istores,
6168                                         icontrol - istores, rotated))
6169                     {
6170                         Py_XDECREF(rotated);
6171                         goto error;
6172                     }
6173                     Py_DECREF(rotated);
6174                     // That just did:
6175                     // rotated = pc_stores[:rotations]
6176                     // del pc_stores[:rotations]
6177                     // pc_stores[icontrol-istores:icontrol-istores] = rotated
6178                     // Do the same thing to the stack, using several ROT_Ns:
6179                     while (rotations--) {
6180                         if (!compiler_addop_i(c, ROT_N, icontrol + 1)) {
6181                             goto error;
6182                         }
6183                     }
6184                 }
6185             }
6186         }
6187         assert(control);
6188         if (!compiler_addop_j(c, JUMP_FORWARD, end) ||
6189             !compiler_next_block(c) ||
6190             !emit_and_reset_fail_pop(c, pc))
6191         {
6192             goto error;
6193         }
6194     }
6195     Py_DECREF(pc->stores);
6196     *pc = old_pc;
6197     Py_INCREF(pc->stores);
6198     // Need to NULL this for the PyObject_Free call in the error block.
6199     old_pc.fail_pop = NULL;
6200     // No match. Pop the remaining copy of the subject and fail:
6201     if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) {
6202         goto error;
6203     }
6204     compiler_use_next_block(c, end);
6205     Py_ssize_t nstores = PyList_GET_SIZE(control);
6206     // There's a bunch of stuff on the stack between any where the new stores
6207     // are and where they need to be:
6208     // - The other stores.
6209     // - A copy of the subject.
6210     // - Anything else that may be on top of the stack.
6211     // - Any previous stores we've already stashed away on the stack.
6212     Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
6213     for (Py_ssize_t i = 0; i < nstores; i++) {
6214         // Rotate this capture to its proper place on the stack:
6215         if (!compiler_addop_i(c, ROT_N, nrots)) {
6216             goto error;
6217         }
6218         // Update the list of previous stores with this new name, checking for
6219         // duplicates:
6220         PyObject *name = PyList_GET_ITEM(control, i);
6221         int dupe = PySequence_Contains(pc->stores, name);
6222         if (dupe < 0) {
6223             goto error;
6224         }
6225         if (dupe) {
6226             compiler_error_duplicate_store(c, name);
6227             goto error;
6228         }
6229         if (PyList_Append(pc->stores, name)) {
6230             goto error;
6231         }
6232     }
6233     Py_DECREF(old_pc.stores);
6234     Py_DECREF(control);
6235     // NOTE: Returning macros are safe again.
6236     // Pop the copy of the subject:
6237     ADDOP(c, POP_TOP);
6238     return 1;
6239 diff:
6240     compiler_error(c, "alternative patterns bind different names");
6241 error:
6242     PyObject_Free(old_pc.fail_pop);
6243     Py_DECREF(old_pc.stores);
6244     Py_XDECREF(control);
6245     return 0;
6246 }
6247 
6248 
6249 static int
compiler_pattern_sequence(struct compiler * c,pattern_ty p,pattern_context * pc)6250 compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
6251 {
6252     assert(p->kind == MatchSequence_kind);
6253     asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6254     Py_ssize_t size = asdl_seq_LEN(patterns);
6255     Py_ssize_t star = -1;
6256     int only_wildcard = 1;
6257     int star_wildcard = 0;
6258     // Find a starred name, if it exists. There may be at most one:
6259     for (Py_ssize_t i = 0; i < size; i++) {
6260         pattern_ty pattern = asdl_seq_GET(patterns, i);
6261         if (pattern->kind == MatchStar_kind) {
6262             if (star >= 0) {
6263                 const char *e = "multiple starred names in sequence pattern";
6264                 return compiler_error(c, e);
6265             }
6266             star_wildcard = WILDCARD_STAR_CHECK(pattern);
6267             only_wildcard &= star_wildcard;
6268             star = i;
6269             continue;
6270         }
6271         only_wildcard &= WILDCARD_CHECK(pattern);
6272     }
6273     // We need to keep the subject on top during the sequence and length checks:
6274     pc->on_top++;
6275     ADDOP(c, MATCH_SEQUENCE);
6276     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6277     if (star < 0) {
6278         // No star: len(subject) == size
6279         ADDOP(c, GET_LEN);
6280         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6281         ADDOP_COMPARE(c, Eq);
6282         RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6283     }
6284     else if (size > 1) {
6285         // Star: len(subject) >= size - 1
6286         ADDOP(c, GET_LEN);
6287         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6288         ADDOP_COMPARE(c, GtE);
6289         RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6290     }
6291     // Whatever comes next should consume the subject:
6292     pc->on_top--;
6293     if (only_wildcard) {
6294         // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6295         ADDOP(c, POP_TOP);
6296     }
6297     else if (star_wildcard) {
6298         RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
6299     }
6300     else {
6301         RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
6302     }
6303     return 1;
6304 }
6305 
6306 static int
compiler_pattern_value(struct compiler * c,pattern_ty p,pattern_context * pc)6307 compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
6308 {
6309     assert(p->kind == MatchValue_kind);
6310     expr_ty value = p->v.MatchValue.value;
6311     if (!MATCH_VALUE_EXPR(value)) {
6312         const char *e = "patterns may only match literals and attribute lookups";
6313         return compiler_error(c, e);
6314     }
6315     VISIT(c, expr, value);
6316     ADDOP_COMPARE(c, Eq);
6317     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6318     return 1;
6319 }
6320 
6321 static int
compiler_pattern_singleton(struct compiler * c,pattern_ty p,pattern_context * pc)6322 compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
6323 {
6324     assert(p->kind == MatchSingleton_kind);
6325     ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6326     ADDOP_COMPARE(c, Is);
6327     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6328     return 1;
6329 }
6330 
6331 static int
compiler_pattern(struct compiler * c,pattern_ty p,pattern_context * pc)6332 compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
6333 {
6334     SET_LOC(c, p);
6335     switch (p->kind) {
6336         case MatchValue_kind:
6337             return compiler_pattern_value(c, p, pc);
6338         case MatchSingleton_kind:
6339             return compiler_pattern_singleton(c, p, pc);
6340         case MatchSequence_kind:
6341             return compiler_pattern_sequence(c, p, pc);
6342         case MatchMapping_kind:
6343             return compiler_pattern_mapping(c, p, pc);
6344         case MatchClass_kind:
6345             return compiler_pattern_class(c, p, pc);
6346         case MatchStar_kind:
6347             return compiler_pattern_star(c, p, pc);
6348         case MatchAs_kind:
6349             return compiler_pattern_as(c, p, pc);
6350         case MatchOr_kind:
6351             return compiler_pattern_or(c, p, pc);
6352     }
6353     // AST validator shouldn't let this happen, but if it does,
6354     // just fail, don't crash out of the interpreter
6355     const char *e = "invalid match pattern node in AST (kind=%d)";
6356     return compiler_error(c, e, p->kind);
6357 }
6358 
6359 static int
compiler_match_inner(struct compiler * c,stmt_ty s,pattern_context * pc)6360 compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
6361 {
6362     VISIT(c, expr, s->v.Match.subject);
6363     basicblock *end;
6364     RETURN_IF_FALSE(end = compiler_new_block(c));
6365     Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6366     assert(cases > 0);
6367     match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6368     int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6369     for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6370         m = asdl_seq_GET(s->v.Match.cases, i);
6371         SET_LOC(c, m->pattern);
6372         // Only copy the subject if we're *not* on the last case:
6373         if (i != cases - has_default - 1) {
6374             ADDOP(c, DUP_TOP);
6375         }
6376         RETURN_IF_FALSE(pc->stores = PyList_New(0));
6377         // Irrefutable cases must be either guarded, last, or both:
6378         pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6379         pc->fail_pop = NULL;
6380         pc->fail_pop_size = 0;
6381         pc->on_top = 0;
6382         // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6383         if (!compiler_pattern(c, m->pattern, pc)) {
6384             Py_DECREF(pc->stores);
6385             return 0;
6386         }
6387         assert(!pc->on_top);
6388         // It's a match! Store all of the captured names (they're on the stack).
6389         Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6390         for (Py_ssize_t n = 0; n < nstores; n++) {
6391             PyObject *name = PyList_GET_ITEM(pc->stores, n);
6392             if (!compiler_nameop(c, name, Store)) {
6393                 Py_DECREF(pc->stores);
6394                 return 0;
6395             }
6396         }
6397         Py_DECREF(pc->stores);
6398         // NOTE: Returning macros are safe again.
6399         if (m->guard) {
6400             RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6401             RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
6402         }
6403         // Success! Pop the subject off, we're done with it:
6404         if (i != cases - has_default - 1) {
6405             ADDOP(c, POP_TOP);
6406         }
6407         VISIT_SEQ(c, stmt, m->body);
6408         ADDOP_JUMP(c, JUMP_FORWARD, end);
6409         // If the pattern fails to match, we want the line number of the
6410         // cleanup to be associated with the failed pattern, not the last line
6411         // of the body
6412         SET_LOC(c, m->pattern);
6413         RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
6414     }
6415     if (has_default) {
6416         // A trailing "case _" is common, and lets us save a bit of redundant
6417         // pushing and popping in the loop above:
6418         m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6419         SET_LOC(c, m->pattern);
6420         if (cases == 1) {
6421             // No matches. Done with the subject:
6422             ADDOP(c, POP_TOP);
6423         }
6424         else {
6425             // Show line coverage for default case (it doesn't create bytecode)
6426             ADDOP(c, NOP);
6427         }
6428         if (m->guard) {
6429             RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6430         }
6431         VISIT_SEQ(c, stmt, m->body);
6432     }
6433     compiler_use_next_block(c, end);
6434     return 1;
6435 }
6436 
6437 static int
compiler_match(struct compiler * c,stmt_ty s)6438 compiler_match(struct compiler *c, stmt_ty s)
6439 {
6440     pattern_context pc;
6441     pc.fail_pop = NULL;
6442     int result = compiler_match_inner(c, s, &pc);
6443     PyObject_Free(pc.fail_pop);
6444     return result;
6445 }
6446 
6447 #undef WILDCARD_CHECK
6448 #undef WILDCARD_STAR_CHECK
6449 
6450 /* End of the compiler section, beginning of the assembler section */
6451 
6452 /* do depth-first search of basic block graph, starting with block.
6453    post records the block indices in post-order.
6454 
6455    XXX must handle implicit jumps from one block to next
6456 */
6457 
6458 struct assembler {
6459     PyObject *a_bytecode;  /* string containing bytecode */
6460     int a_offset;              /* offset into bytecode */
6461     int a_nblocks;             /* number of reachable blocks */
6462     PyObject *a_lnotab;    /* string containing lnotab */
6463     int a_lnotab_off;      /* offset into lnotab */
6464     int a_prevlineno;     /* lineno of last emitted line in line table */
6465     int a_lineno;          /* lineno of last emitted instruction */
6466     int a_lineno_start;    /* bytecode start offset of current lineno */
6467     basicblock *a_entry;
6468 };
6469 
6470 Py_LOCAL_INLINE(void)
stackdepth_push(basicblock *** sp,basicblock * b,int depth)6471 stackdepth_push(basicblock ***sp, basicblock *b, int depth)
6472 {
6473     assert(b->b_startdepth < 0 || b->b_startdepth == depth);
6474     if (b->b_startdepth < depth && b->b_startdepth < 100) {
6475         assert(b->b_startdepth < 0);
6476         b->b_startdepth = depth;
6477         *(*sp)++ = b;
6478     }
6479 }
6480 
6481 /* Find the flow path that needs the largest stack.  We assume that
6482  * cycles in the flow graph have no net effect on the stack depth.
6483  */
6484 static int
stackdepth(struct compiler * c)6485 stackdepth(struct compiler *c)
6486 {
6487     basicblock *b, *entryblock = NULL;
6488     basicblock **stack, **sp;
6489     int nblocks = 0, maxdepth = 0;
6490     for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6491         b->b_startdepth = INT_MIN;
6492         entryblock = b;
6493         nblocks++;
6494     }
6495     assert(entryblock!= NULL);
6496     stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
6497     if (!stack) {
6498         PyErr_NoMemory();
6499         return -1;
6500     }
6501 
6502     sp = stack;
6503     if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) {
6504         stackdepth_push(&sp, entryblock, 1);
6505     } else {
6506         stackdepth_push(&sp, entryblock, 0);
6507     }
6508     while (sp != stack) {
6509         b = *--sp;
6510         int depth = b->b_startdepth;
6511         assert(depth >= 0);
6512         basicblock *next = b->b_next;
6513         for (int i = 0; i < b->b_iused; i++) {
6514             struct instr *instr = &b->b_instr[i];
6515             int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
6516             if (effect == PY_INVALID_STACK_EFFECT) {
6517                 PyErr_Format(PyExc_SystemError,
6518                              "compiler stack_effect(opcode=%d, arg=%i) failed",
6519                              instr->i_opcode, instr->i_oparg);
6520                 return -1;
6521             }
6522             int new_depth = depth + effect;
6523             if (new_depth > maxdepth) {
6524                 maxdepth = new_depth;
6525             }
6526             assert(depth >= 0); /* invalid code or bug in stackdepth() */
6527             if (is_jump(instr)) {
6528                 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
6529                 assert(effect != PY_INVALID_STACK_EFFECT);
6530                 int target_depth = depth + effect;
6531                 if (target_depth > maxdepth) {
6532                     maxdepth = target_depth;
6533                 }
6534                 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
6535                 stackdepth_push(&sp, instr->i_target, target_depth);
6536             }
6537             depth = new_depth;
6538             if (instr->i_opcode == JUMP_ABSOLUTE ||
6539                 instr->i_opcode == JUMP_FORWARD ||
6540                 instr->i_opcode == RETURN_VALUE ||
6541                 instr->i_opcode == RAISE_VARARGS ||
6542                 instr->i_opcode == RERAISE)
6543             {
6544                 /* remaining code is dead */
6545                 next = NULL;
6546                 break;
6547             }
6548         }
6549         if (next != NULL) {
6550             assert(b->b_nofallthrough == 0);
6551             stackdepth_push(&sp, next, depth);
6552         }
6553     }
6554     PyObject_Free(stack);
6555     return maxdepth;
6556 }
6557 
6558 static int
assemble_init(struct assembler * a,int nblocks,int firstlineno)6559 assemble_init(struct assembler *a, int nblocks, int firstlineno)
6560 {
6561     memset(a, 0, sizeof(struct assembler));
6562     a->a_prevlineno = a->a_lineno = firstlineno;
6563     a->a_lnotab = NULL;
6564     a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
6565     if (a->a_bytecode == NULL) {
6566         goto error;
6567     }
6568     a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
6569     if (a->a_lnotab == NULL) {
6570         goto error;
6571     }
6572     if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
6573         PyErr_NoMemory();
6574         goto error;
6575     }
6576     return 1;
6577 error:
6578     Py_XDECREF(a->a_bytecode);
6579     Py_XDECREF(a->a_lnotab);
6580     return 0;
6581 }
6582 
6583 static void
assemble_free(struct assembler * a)6584 assemble_free(struct assembler *a)
6585 {
6586     Py_XDECREF(a->a_bytecode);
6587     Py_XDECREF(a->a_lnotab);
6588 }
6589 
6590 static int
blocksize(basicblock * b)6591 blocksize(basicblock *b)
6592 {
6593     int i;
6594     int size = 0;
6595 
6596     for (i = 0; i < b->b_iused; i++)
6597         size += instrsize(b->b_instr[i].i_oparg);
6598     return size;
6599 }
6600 
6601 static int
assemble_emit_linetable_pair(struct assembler * a,int bdelta,int ldelta)6602 assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta)
6603 {
6604     Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab);
6605     if (a->a_lnotab_off + 2 >= len) {
6606         if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
6607             return 0;
6608     }
6609     unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab);
6610     lnotab += a->a_lnotab_off;
6611     a->a_lnotab_off += 2;
6612     *lnotab++ = bdelta;
6613     *lnotab++ = ldelta;
6614     return 1;
6615 }
6616 
6617 /* Appends a range to the end of the line number table. See
6618  *  Objects/lnotab_notes.txt for the description of the line number table. */
6619 
6620 static int
assemble_line_range(struct assembler * a)6621 assemble_line_range(struct assembler *a)
6622 {
6623     int ldelta, bdelta;
6624     bdelta =  (a->a_offset - a->a_lineno_start) * sizeof(_Py_CODEUNIT);
6625     if (bdelta == 0) {
6626         return 1;
6627     }
6628     if (a->a_lineno < 0) {
6629         ldelta = -128;
6630     }
6631     else {
6632         ldelta = a->a_lineno - a->a_prevlineno;
6633         a->a_prevlineno = a->a_lineno;
6634         while (ldelta > 127) {
6635             if (!assemble_emit_linetable_pair(a, 0, 127)) {
6636                 return 0;
6637             }
6638             ldelta -= 127;
6639         }
6640         while (ldelta < -127) {
6641             if (!assemble_emit_linetable_pair(a, 0, -127)) {
6642                 return 0;
6643             }
6644             ldelta += 127;
6645         }
6646     }
6647     assert(-128 <= ldelta && ldelta < 128);
6648     while (bdelta > 254) {
6649         if (!assemble_emit_linetable_pair(a, 254, ldelta)) {
6650             return 0;
6651         }
6652         ldelta = a->a_lineno < 0 ? -128 : 0;
6653         bdelta -= 254;
6654     }
6655     if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) {
6656         return 0;
6657     }
6658     a->a_lineno_start = a->a_offset;
6659     return 1;
6660 }
6661 
6662 static int
assemble_lnotab(struct assembler * a,struct instr * i)6663 assemble_lnotab(struct assembler *a, struct instr *i)
6664 {
6665     if (i->i_lineno == a->a_lineno) {
6666         return 1;
6667     }
6668     if (!assemble_line_range(a)) {
6669         return 0;
6670     }
6671     a->a_lineno = i->i_lineno;
6672     return 1;
6673 }
6674 
6675 
6676 /* assemble_emit()
6677    Extend the bytecode with a new instruction.
6678    Update lnotab if necessary.
6679 */
6680 
6681 static int
assemble_emit(struct assembler * a,struct instr * i)6682 assemble_emit(struct assembler *a, struct instr *i)
6683 {
6684     int size, arg = 0;
6685     Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
6686     _Py_CODEUNIT *code;
6687 
6688     arg = i->i_oparg;
6689     size = instrsize(arg);
6690     if (i->i_lineno && !assemble_lnotab(a, i))
6691         return 0;
6692     if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
6693         if (len > PY_SSIZE_T_MAX / 2)
6694             return 0;
6695         if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
6696             return 0;
6697     }
6698     code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
6699     a->a_offset += size;
6700     write_op_arg(code, i->i_opcode, arg, size);
6701     return 1;
6702 }
6703 
6704 static void
assemble_jump_offsets(struct assembler * a,struct compiler * c)6705 assemble_jump_offsets(struct assembler *a, struct compiler *c)
6706 {
6707     basicblock *b;
6708     int bsize, totsize, extended_arg_recompile;
6709     int i;
6710 
6711     /* Compute the size of each block and fixup jump args.
6712        Replace block pointer with position in bytecode. */
6713     do {
6714         totsize = 0;
6715         for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6716             bsize = blocksize(b);
6717             b->b_offset = totsize;
6718             totsize += bsize;
6719         }
6720         extended_arg_recompile = 0;
6721         for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6722             bsize = b->b_offset;
6723             for (i = 0; i < b->b_iused; i++) {
6724                 struct instr *instr = &b->b_instr[i];
6725                 int isize = instrsize(instr->i_oparg);
6726                 /* Relative jumps are computed relative to
6727                    the instruction pointer after fetching
6728                    the jump instruction.
6729                 */
6730                 bsize += isize;
6731                 if (is_jump(instr)) {
6732                     instr->i_oparg = instr->i_target->b_offset;
6733                     if (is_relative_jump(instr)) {
6734                         instr->i_oparg -= bsize;
6735                     }
6736                     if (instrsize(instr->i_oparg) != isize) {
6737                         extended_arg_recompile = 1;
6738                     }
6739                 }
6740             }
6741         }
6742 
6743     /* XXX: This is an awful hack that could hurt performance, but
6744         on the bright side it should work until we come up
6745         with a better solution.
6746 
6747         The issue is that in the first loop blocksize() is called
6748         which calls instrsize() which requires i_oparg be set
6749         appropriately. There is a bootstrap problem because
6750         i_oparg is calculated in the second loop above.
6751 
6752         So we loop until we stop seeing new EXTENDED_ARGs.
6753         The only EXTENDED_ARGs that could be popping up are
6754         ones in jump instructions.  So this should converge
6755         fairly quickly.
6756     */
6757     } while (extended_arg_recompile);
6758 }
6759 
6760 static PyObject *
dict_keys_inorder(PyObject * dict,Py_ssize_t offset)6761 dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
6762 {
6763     PyObject *tuple, *k, *v;
6764     Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6765 
6766     tuple = PyTuple_New(size);
6767     if (tuple == NULL)
6768         return NULL;
6769     while (PyDict_Next(dict, &pos, &k, &v)) {
6770         i = PyLong_AS_LONG(v);
6771         Py_INCREF(k);
6772         assert((i - offset) < size);
6773         assert((i - offset) >= 0);
6774         PyTuple_SET_ITEM(tuple, i - offset, k);
6775     }
6776     return tuple;
6777 }
6778 
6779 static PyObject *
consts_dict_keys_inorder(PyObject * dict)6780 consts_dict_keys_inorder(PyObject *dict)
6781 {
6782     PyObject *consts, *k, *v;
6783     Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
6784 
6785     consts = PyList_New(size);   /* PyCode_Optimize() requires a list */
6786     if (consts == NULL)
6787         return NULL;
6788     while (PyDict_Next(dict, &pos, &k, &v)) {
6789         i = PyLong_AS_LONG(v);
6790         /* The keys of the dictionary can be tuples wrapping a constant.
6791          * (see compiler_add_o and _PyCode_ConstantKey). In that case
6792          * the object we want is always second. */
6793         if (PyTuple_CheckExact(k)) {
6794             k = PyTuple_GET_ITEM(k, 1);
6795         }
6796         Py_INCREF(k);
6797         assert(i < size);
6798         assert(i >= 0);
6799         PyList_SET_ITEM(consts, i, k);
6800     }
6801     return consts;
6802 }
6803 
6804 static int
compute_code_flags(struct compiler * c)6805 compute_code_flags(struct compiler *c)
6806 {
6807     PySTEntryObject *ste = c->u->u_ste;
6808     int flags = 0;
6809     if (ste->ste_type == FunctionBlock) {
6810         flags |= CO_NEWLOCALS | CO_OPTIMIZED;
6811         if (ste->ste_nested)
6812             flags |= CO_NESTED;
6813         if (ste->ste_generator && !ste->ste_coroutine)
6814             flags |= CO_GENERATOR;
6815         if (!ste->ste_generator && ste->ste_coroutine)
6816             flags |= CO_COROUTINE;
6817         if (ste->ste_generator && ste->ste_coroutine)
6818             flags |= CO_ASYNC_GENERATOR;
6819         if (ste->ste_varargs)
6820             flags |= CO_VARARGS;
6821         if (ste->ste_varkeywords)
6822             flags |= CO_VARKEYWORDS;
6823     }
6824 
6825     /* (Only) inherit compilerflags in PyCF_MASK */
6826     flags |= (c->c_flags->cf_flags & PyCF_MASK);
6827 
6828     if ((IS_TOP_LEVEL_AWAIT(c)) &&
6829          ste->ste_coroutine &&
6830          !ste->ste_generator) {
6831         flags |= CO_COROUTINE;
6832     }
6833 
6834     return flags;
6835 }
6836 
6837 // Merge *obj* with constant cache.
6838 // Unlike merge_consts_recursive(), this function doesn't work recursively.
6839 static int
merge_const_one(struct compiler * c,PyObject ** obj)6840 merge_const_one(struct compiler *c, PyObject **obj)
6841 {
6842     PyObject *key = _PyCode_ConstantKey(*obj);
6843     if (key == NULL) {
6844         return 0;
6845     }
6846 
6847     // t is borrowed reference
6848     PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
6849     Py_DECREF(key);
6850     if (t == NULL) {
6851         return 0;
6852     }
6853     if (t == key) {  // obj is new constant.
6854         return 1;
6855     }
6856 
6857     if (PyTuple_CheckExact(t)) {
6858         // t is still borrowed reference
6859         t = PyTuple_GET_ITEM(t, 1);
6860     }
6861 
6862     Py_INCREF(t);
6863     Py_DECREF(*obj);
6864     *obj = t;
6865     return 1;
6866 }
6867 
6868 static PyCodeObject *
makecode(struct compiler * c,struct assembler * a,PyObject * consts)6869 makecode(struct compiler *c, struct assembler *a, PyObject *consts)
6870 {
6871     PyCodeObject *co = NULL;
6872     PyObject *names = NULL;
6873     PyObject *varnames = NULL;
6874     PyObject *name = NULL;
6875     PyObject *freevars = NULL;
6876     PyObject *cellvars = NULL;
6877     Py_ssize_t nlocals;
6878     int nlocals_int;
6879     int flags;
6880     int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
6881 
6882     names = dict_keys_inorder(c->u->u_names, 0);
6883     varnames = dict_keys_inorder(c->u->u_varnames, 0);
6884     if (!names || !varnames) {
6885         goto error;
6886     }
6887     cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
6888     if (!cellvars)
6889         goto error;
6890     freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
6891     if (!freevars)
6892         goto error;
6893 
6894     if (!merge_const_one(c, &names) ||
6895             !merge_const_one(c, &varnames) ||
6896             !merge_const_one(c, &cellvars) ||
6897             !merge_const_one(c, &freevars))
6898     {
6899         goto error;
6900     }
6901 
6902     nlocals = PyDict_GET_SIZE(c->u->u_varnames);
6903     assert(nlocals < INT_MAX);
6904     nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
6905 
6906     flags = compute_code_flags(c);
6907     if (flags < 0)
6908         goto error;
6909 
6910     consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
6911     if (consts == NULL) {
6912         goto error;
6913     }
6914     if (!merge_const_one(c, &consts)) {
6915         Py_DECREF(consts);
6916         goto error;
6917     }
6918 
6919     posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
6920     posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
6921     kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
6922     maxdepth = stackdepth(c);
6923     if (maxdepth < 0) {
6924         Py_DECREF(consts);
6925         goto error;
6926     }
6927     if (maxdepth > MAX_ALLOWED_STACK_USE) {
6928         PyErr_Format(PyExc_SystemError,
6929                      "excessive stack use: stack is %d deep",
6930                      maxdepth);
6931         Py_DECREF(consts);
6932         goto error;
6933     }
6934     co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
6935                                    posonlyargcount, kwonlyargcount, nlocals_int,
6936                                    maxdepth, flags, a->a_bytecode, consts, names,
6937                                    varnames, freevars, cellvars, c->c_filename,
6938                                    c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
6939     Py_DECREF(consts);
6940  error:
6941     Py_XDECREF(names);
6942     Py_XDECREF(varnames);
6943     Py_XDECREF(name);
6944     Py_XDECREF(freevars);
6945     Py_XDECREF(cellvars);
6946     return co;
6947 }
6948 
6949 
6950 /* For debugging purposes only */
6951 #if 0
6952 static void
6953 dump_instr(struct instr *i)
6954 {
6955     const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
6956     const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
6957 
6958     char arg[128];
6959 
6960     *arg = '\0';
6961     if (HAS_ARG(i->i_opcode)) {
6962         sprintf(arg, "arg: %d ", i->i_oparg);
6963     }
6964     fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
6965                     i->i_lineno, i->i_opcode, arg, jabs, jrel);
6966 }
6967 
6968 static void
6969 dump_basicblock(const basicblock *b)
6970 {
6971     const char *b_return = b->b_return ? "return " : "";
6972     fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
6973         b->b_iused, b->b_startdepth, b->b_offset, b_return);
6974     if (b->b_instr) {
6975         int i;
6976         for (i = 0; i < b->b_iused; i++) {
6977             fprintf(stderr, "  [%02d] ", i);
6978             dump_instr(b->b_instr + i);
6979         }
6980     }
6981 }
6982 #endif
6983 
6984 
6985 static int
6986 normalize_basic_block(basicblock *bb);
6987 
6988 static int
6989 optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);
6990 
6991 static int
6992 trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts);
6993 
6994 /* Duplicates exit BBs, so that line numbers can be propagated to them */
6995 static int
6996 duplicate_exits_without_lineno(struct compiler *c);
6997 
6998 static int
6999 extend_block(basicblock *bb);
7000 
7001 static int
insert_generator_prefix(struct compiler * c,basicblock * entryblock)7002 insert_generator_prefix(struct compiler *c, basicblock *entryblock) {
7003 
7004     int flags = compute_code_flags(c);
7005     if (flags < 0) {
7006         return -1;
7007     }
7008     int kind;
7009     if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
7010         if (flags & CO_COROUTINE) {
7011             kind = 1;
7012         }
7013         else if (flags & CO_ASYNC_GENERATOR) {
7014             kind = 2;
7015         }
7016         else {
7017             kind = 0;
7018         }
7019     }
7020     else {
7021         return 0;
7022     }
7023     if (compiler_next_instr(entryblock) < 0) {
7024         return -1;
7025     }
7026     for (int i = entryblock->b_iused-1; i > 0; i--) {
7027         entryblock->b_instr[i] = entryblock->b_instr[i-1];
7028     }
7029     entryblock->b_instr[0].i_opcode = GEN_START;
7030     entryblock->b_instr[0].i_oparg = kind;
7031     entryblock->b_instr[0].i_lineno = -1;
7032     entryblock->b_instr[0].i_target = NULL;
7033     return 0;
7034 }
7035 
7036 /* Make sure that all returns have a line number, even if early passes
7037  * have failed to propagate a correct line number.
7038  * The resulting line number may not be correct according to PEP 626,
7039  * but should be "good enough", and no worse than in older versions. */
7040 static void
guarantee_lineno_for_exits(struct assembler * a,int firstlineno)7041 guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
7042     int lineno = firstlineno;
7043     assert(lineno > 0);
7044     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7045         if (b->b_iused == 0) {
7046             continue;
7047         }
7048         struct instr *last = &b->b_instr[b->b_iused-1];
7049         if (last->i_lineno < 0) {
7050             if (last->i_opcode == RETURN_VALUE) {
7051                 for (int i = 0; i < b->b_iused; i++) {
7052                     assert(b->b_instr[i].i_lineno < 0);
7053 
7054                     b->b_instr[i].i_lineno = lineno;
7055                 }
7056             }
7057         }
7058         else {
7059             lineno = last->i_lineno;
7060         }
7061     }
7062 }
7063 
7064 static void
7065 propagate_line_numbers(struct assembler *a);
7066 
7067 static PyCodeObject *
assemble(struct compiler * c,int addNone)7068 assemble(struct compiler *c, int addNone)
7069 {
7070     basicblock *b, *entryblock;
7071     struct assembler a;
7072     int j, nblocks;
7073     PyCodeObject *co = NULL;
7074     PyObject *consts = NULL;
7075 
7076     /* Make sure every block that falls off the end returns None.
7077        XXX NEXT_BLOCK() isn't quite right, because if the last
7078        block ends with a jump or return b_next shouldn't set.
7079      */
7080     if (!c->u->u_curblock->b_return) {
7081         c->u->u_lineno = -1;
7082         if (addNone)
7083             ADDOP_LOAD_CONST(c, Py_None);
7084         ADDOP(c, RETURN_VALUE);
7085     }
7086 
7087     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7088         if (normalize_basic_block(b)) {
7089             return NULL;
7090         }
7091     }
7092 
7093     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7094         if (extend_block(b)) {
7095             return NULL;
7096         }
7097     }
7098 
7099     nblocks = 0;
7100     entryblock = NULL;
7101     for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
7102         nblocks++;
7103         entryblock = b;
7104     }
7105     assert(entryblock != NULL);
7106 
7107     if (insert_generator_prefix(c, entryblock)) {
7108         goto error;
7109     }
7110 
7111     /* Set firstlineno if it wasn't explicitly set. */
7112     if (!c->u->u_firstlineno) {
7113         if (entryblock->b_instr && entryblock->b_instr->i_lineno)
7114             c->u->u_firstlineno = entryblock->b_instr->i_lineno;
7115        else
7116             c->u->u_firstlineno = 1;
7117     }
7118 
7119     if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
7120         goto error;
7121     a.a_entry = entryblock;
7122     a.a_nblocks = nblocks;
7123 
7124     consts = consts_dict_keys_inorder(c->u->u_consts);
7125     if (consts == NULL) {
7126         goto error;
7127     }
7128 
7129     if (optimize_cfg(c, &a, consts)) {
7130         goto error;
7131     }
7132     if (duplicate_exits_without_lineno(c)) {
7133         return NULL;
7134     }
7135     if (trim_unused_consts(c, &a, consts)) {
7136         goto error;
7137     }
7138     propagate_line_numbers(&a);
7139     guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
7140     /* Can't modify the bytecode after computing jump offsets. */
7141     assemble_jump_offsets(&a, c);
7142 
7143     /* Emit code. */
7144     for(b = entryblock; b != NULL; b = b->b_next) {
7145         for (j = 0; j < b->b_iused; j++)
7146             if (!assemble_emit(&a, &b->b_instr[j]))
7147                 goto error;
7148     }
7149     if (!assemble_line_range(&a)) {
7150         return 0;
7151     }
7152 
7153     if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
7154         goto error;
7155     }
7156     if (!merge_const_one(c, &a.a_lnotab)) {
7157         goto error;
7158     }
7159     if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
7160         goto error;
7161     }
7162     if (!merge_const_one(c, &a.a_bytecode)) {
7163         goto error;
7164     }
7165 
7166     co = makecode(c, &a, consts);
7167  error:
7168     Py_XDECREF(consts);
7169     assemble_free(&a);
7170     return co;
7171 }
7172 
7173 /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
7174    with    LOAD_CONST (c1, c2, ... cn).
7175    The consts table must still be in list form so that the
7176    new constant (c1, c2, ... cn) can be appended.
7177    Called with codestr pointing to the first LOAD_CONST.
7178 */
7179 static int
fold_tuple_on_constants(struct compiler * c,struct instr * inst,int n,PyObject * consts)7180 fold_tuple_on_constants(struct compiler *c,
7181                         struct instr *inst,
7182                         int n, PyObject *consts)
7183 {
7184     /* Pre-conditions */
7185     assert(PyList_CheckExact(consts));
7186     assert(inst[n].i_opcode == BUILD_TUPLE);
7187     assert(inst[n].i_oparg == n);
7188 
7189     for (int i = 0; i < n; i++) {
7190         if (inst[i].i_opcode != LOAD_CONST) {
7191             return 0;
7192         }
7193     }
7194 
7195     /* Buildup new tuple of constants */
7196     PyObject *newconst = PyTuple_New(n);
7197     if (newconst == NULL) {
7198         return -1;
7199     }
7200     for (int i = 0; i < n; i++) {
7201         int arg = inst[i].i_oparg;
7202         PyObject *constant = PyList_GET_ITEM(consts, arg);
7203         Py_INCREF(constant);
7204         PyTuple_SET_ITEM(newconst, i, constant);
7205     }
7206     if (merge_const_one(c, &newconst) == 0) {
7207         Py_DECREF(newconst);
7208         return -1;
7209     }
7210 
7211     Py_ssize_t index;
7212     for (index = 0; index < PyList_GET_SIZE(consts); index++) {
7213         if (PyList_GET_ITEM(consts, index) == newconst) {
7214             break;
7215         }
7216     }
7217     if (index == PyList_GET_SIZE(consts)) {
7218         if ((size_t)index >= (size_t)INT_MAX - 1) {
7219             Py_DECREF(newconst);
7220             PyErr_SetString(PyExc_OverflowError, "too many constants");
7221             return -1;
7222         }
7223         if (PyList_Append(consts, newconst)) {
7224             Py_DECREF(newconst);
7225             return -1;
7226         }
7227     }
7228     Py_DECREF(newconst);
7229     for (int i = 0; i < n; i++) {
7230         inst[i].i_opcode = NOP;
7231     }
7232     inst[n].i_opcode = LOAD_CONST;
7233     inst[n].i_oparg = (int)index;
7234     return 0;
7235 }
7236 
7237 
7238 // Eliminate n * ROT_N(n).
7239 static void
fold_rotations(struct instr * inst,int n)7240 fold_rotations(struct instr *inst, int n)
7241 {
7242     for (int i = 0; i < n; i++) {
7243         int rot;
7244         switch (inst[i].i_opcode) {
7245             case ROT_N:
7246                 rot = inst[i].i_oparg;
7247                 break;
7248             case ROT_FOUR:
7249                 rot = 4;
7250                 break;
7251             case ROT_THREE:
7252                 rot = 3;
7253                 break;
7254             case ROT_TWO:
7255                 rot = 2;
7256                 break;
7257             default:
7258                 return;
7259         }
7260         if (rot != n) {
7261             return;
7262         }
7263     }
7264     for (int i = 0; i < n; i++) {
7265         inst[i].i_opcode = NOP;
7266     }
7267 }
7268 
7269 // Attempt to eliminate jumps to jumps by updating inst to jump to
7270 // target->i_target using the provided opcode. Return whether or not the
7271 // optimization was successful.
7272 static bool
jump_thread(struct instr * inst,struct instr * target,int opcode)7273 jump_thread(struct instr *inst, struct instr *target, int opcode)
7274 {
7275     assert(is_jump(inst));
7276     assert(is_jump(target));
7277     // bpo-45773: If inst->i_target == target->i_target, then nothing actually
7278     // changes (and we fall into an infinite loop):
7279     if (inst->i_lineno == target->i_lineno &&
7280         inst->i_target != target->i_target)
7281     {
7282         inst->i_target = target->i_target;
7283         inst->i_opcode = opcode;
7284         return true;
7285     }
7286     return false;
7287 }
7288 
7289 /* Maximum size of basic block that should be copied in optimizer */
7290 #define MAX_COPY_SIZE 4
7291 
7292 /* Optimization */
7293 static int
optimize_basic_block(struct compiler * c,basicblock * bb,PyObject * consts)7294 optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
7295 {
7296     assert(PyList_CheckExact(consts));
7297     struct instr nop;
7298     nop.i_opcode = NOP;
7299     struct instr *target;
7300     for (int i = 0; i < bb->b_iused; i++) {
7301         struct instr *inst = &bb->b_instr[i];
7302         int oparg = inst->i_oparg;
7303         int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
7304         if (is_jump(inst)) {
7305             /* Skip over empty basic blocks. */
7306             while (inst->i_target->b_iused == 0) {
7307                 inst->i_target = inst->i_target->b_next;
7308             }
7309             target = &inst->i_target->b_instr[0];
7310         }
7311         else {
7312             target = &nop;
7313         }
7314         switch (inst->i_opcode) {
7315             /* Remove LOAD_CONST const; conditional jump */
7316             case LOAD_CONST:
7317             {
7318                 PyObject* cnt;
7319                 int is_true;
7320                 int jump_if_true;
7321                 switch(nextop) {
7322                     case POP_JUMP_IF_FALSE:
7323                     case POP_JUMP_IF_TRUE:
7324                         cnt = PyList_GET_ITEM(consts, oparg);
7325                         is_true = PyObject_IsTrue(cnt);
7326                         if (is_true == -1) {
7327                             goto error;
7328                         }
7329                         inst->i_opcode = NOP;
7330                         jump_if_true = nextop == POP_JUMP_IF_TRUE;
7331                         if (is_true == jump_if_true) {
7332                             bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7333                             bb->b_nofallthrough = 1;
7334                         }
7335                         else {
7336                             bb->b_instr[i+1].i_opcode = NOP;
7337                         }
7338                         break;
7339                     case JUMP_IF_FALSE_OR_POP:
7340                     case JUMP_IF_TRUE_OR_POP:
7341                         cnt = PyList_GET_ITEM(consts, oparg);
7342                         is_true = PyObject_IsTrue(cnt);
7343                         if (is_true == -1) {
7344                             goto error;
7345                         }
7346                         jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
7347                         if (is_true == jump_if_true) {
7348                             bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE;
7349                             bb->b_nofallthrough = 1;
7350                         }
7351                         else {
7352                             inst->i_opcode = NOP;
7353                             bb->b_instr[i+1].i_opcode = NOP;
7354                         }
7355                         break;
7356                 }
7357                 break;
7358             }
7359 
7360                 /* Try to fold tuples of constants.
7361                    Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
7362                    Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
7363                    Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
7364             case BUILD_TUPLE:
7365                 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
7366                     switch(oparg) {
7367                         case 1:
7368                             inst->i_opcode = NOP;
7369                             bb->b_instr[i+1].i_opcode = NOP;
7370                             break;
7371                         case 2:
7372                             inst->i_opcode = ROT_TWO;
7373                             bb->b_instr[i+1].i_opcode = NOP;
7374                             break;
7375                         case 3:
7376                             inst->i_opcode = ROT_THREE;
7377                             bb->b_instr[i+1].i_opcode = ROT_TWO;
7378                     }
7379                     break;
7380                 }
7381                 if (i >= oparg) {
7382                     if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
7383                         goto error;
7384                     }
7385                 }
7386                 break;
7387 
7388                 /* Simplify conditional jump to conditional jump where the
7389                    result of the first test implies the success of a similar
7390                    test or the failure of the opposite test.
7391                    Arises in code like:
7392                    "a and b or c"
7393                    "(a and b) and c"
7394                    "(a or b) or c"
7395                    "(a or b) and c"
7396                    x:JUMP_IF_FALSE_OR_POP y   y:JUMP_IF_FALSE_OR_POP z
7397                       -->  x:JUMP_IF_FALSE_OR_POP z
7398                    x:JUMP_IF_FALSE_OR_POP y   y:JUMP_IF_TRUE_OR_POP z
7399                       -->  x:POP_JUMP_IF_FALSE y+1
7400                    where y+1 is the instruction following the second test.
7401                 */
7402             case JUMP_IF_FALSE_OR_POP:
7403                 switch (target->i_opcode) {
7404                     case POP_JUMP_IF_FALSE:
7405                         i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
7406                         break;
7407                     case JUMP_ABSOLUTE:
7408                     case JUMP_FORWARD:
7409                     case JUMP_IF_FALSE_OR_POP:
7410                         i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
7411                         break;
7412                     case JUMP_IF_TRUE_OR_POP:
7413                     case POP_JUMP_IF_TRUE:
7414                         if (inst->i_lineno == target->i_lineno) {
7415                             // We don't need to bother checking for loops here,
7416                             // since a block's b_next cannot point to itself:
7417                             assert(inst->i_target != inst->i_target->b_next);
7418                             inst->i_opcode = POP_JUMP_IF_FALSE;
7419                             inst->i_target = inst->i_target->b_next;
7420                             --i;
7421                         }
7422                         break;
7423                 }
7424                 break;
7425             case JUMP_IF_TRUE_OR_POP:
7426                 switch (target->i_opcode) {
7427                     case POP_JUMP_IF_TRUE:
7428                         i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
7429                         break;
7430                     case JUMP_ABSOLUTE:
7431                     case JUMP_FORWARD:
7432                     case JUMP_IF_TRUE_OR_POP:
7433                         i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
7434                         break;
7435                     case JUMP_IF_FALSE_OR_POP:
7436                     case POP_JUMP_IF_FALSE:
7437                         if (inst->i_lineno == target->i_lineno) {
7438                             // We don't need to bother checking for loops here,
7439                             // since a block's b_next cannot point to itself:
7440                             assert(inst->i_target != inst->i_target->b_next);
7441                             inst->i_opcode = POP_JUMP_IF_TRUE;
7442                             inst->i_target = inst->i_target->b_next;
7443                             --i;
7444                         }
7445                         break;
7446                 }
7447                 break;
7448             case POP_JUMP_IF_FALSE:
7449                 switch (target->i_opcode) {
7450                     case JUMP_ABSOLUTE:
7451                     case JUMP_FORWARD:
7452                     case JUMP_IF_FALSE_OR_POP:
7453                         i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
7454                 }
7455                 break;
7456             case POP_JUMP_IF_TRUE:
7457                 switch (target->i_opcode) {
7458                     case JUMP_ABSOLUTE:
7459                     case JUMP_FORWARD:
7460                     case JUMP_IF_TRUE_OR_POP:
7461                         i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
7462                 }
7463                 break;
7464             case JUMP_ABSOLUTE:
7465             case JUMP_FORWARD:
7466                 switch (target->i_opcode) {
7467                     case JUMP_ABSOLUTE:
7468                     case JUMP_FORWARD:
7469                         i -= jump_thread(inst, target, JUMP_ABSOLUTE);
7470                 }
7471                 break;
7472             case FOR_ITER:
7473                 if (target->i_opcode == JUMP_FORWARD) {
7474                     i -= jump_thread(inst, target, FOR_ITER);
7475                 }
7476                 break;
7477             case ROT_N:
7478                 switch (oparg) {
7479                     case 0:
7480                     case 1:
7481                         inst->i_opcode = NOP;
7482                         continue;
7483                     case 2:
7484                         inst->i_opcode = ROT_TWO;
7485                         break;
7486                     case 3:
7487                         inst->i_opcode = ROT_THREE;
7488                         break;
7489                     case 4:
7490                         inst->i_opcode = ROT_FOUR;
7491                         break;
7492                 }
7493                 if (i >= oparg - 1) {
7494                     fold_rotations(inst - oparg + 1, oparg);
7495                 }
7496                 break;
7497         }
7498     }
7499     return 0;
7500 error:
7501     return -1;
7502 }
7503 
7504 /* If this block ends with an unconditional jump to an exit block,
7505  * then remove the jump and extend this block with the target.
7506  */
7507 static int
extend_block(basicblock * bb)7508 extend_block(basicblock *bb) {
7509     if (bb->b_iused == 0) {
7510         return 0;
7511     }
7512     struct instr *last = &bb->b_instr[bb->b_iused-1];
7513     if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) {
7514         return 0;
7515     }
7516     if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) {
7517         basicblock *to_copy = last->i_target;
7518         last->i_opcode = NOP;
7519         for (int i = 0; i < to_copy->b_iused; i++) {
7520             int index = compiler_next_instr(bb);
7521             if (index < 0) {
7522                 return -1;
7523             }
7524             bb->b_instr[index] = to_copy->b_instr[i];
7525         }
7526         bb->b_exit = 1;
7527     }
7528     return 0;
7529 }
7530 
7531 static void
clean_basic_block(basicblock * bb,int prev_lineno)7532 clean_basic_block(basicblock *bb, int prev_lineno) {
7533     /* Remove NOPs when legal to do so. */
7534     int dest = 0;
7535     for (int src = 0; src < bb->b_iused; src++) {
7536         int lineno = bb->b_instr[src].i_lineno;
7537         if (bb->b_instr[src].i_opcode == NOP) {
7538             /* Eliminate no-op if it doesn't have a line number */
7539             if (lineno < 0) {
7540                 continue;
7541             }
7542             /* or, if the previous instruction had the same line number. */
7543             if (prev_lineno == lineno) {
7544                 continue;
7545             }
7546             /* or, if the next instruction has same line number or no line number */
7547             if (src < bb->b_iused - 1) {
7548                 int next_lineno = bb->b_instr[src+1].i_lineno;
7549                 if (next_lineno < 0 || next_lineno == lineno) {
7550                     bb->b_instr[src+1].i_lineno = lineno;
7551                     continue;
7552                 }
7553             }
7554             else {
7555                 basicblock* next = bb->b_next;
7556                 while (next && next->b_iused == 0) {
7557                     next = next->b_next;
7558                 }
7559                 /* or if last instruction in BB and next BB has same line number */
7560                 if (next) {
7561                     if (lineno == next->b_instr[0].i_lineno) {
7562                         continue;
7563                     }
7564                 }
7565             }
7566 
7567         }
7568         if (dest != src) {
7569             bb->b_instr[dest] = bb->b_instr[src];
7570         }
7571         dest++;
7572         prev_lineno = lineno;
7573     }
7574     assert(dest <= bb->b_iused);
7575     bb->b_iused = dest;
7576 }
7577 
7578 static int
normalize_basic_block(basicblock * bb)7579 normalize_basic_block(basicblock *bb) {
7580     /* Mark blocks as exit and/or nofallthrough.
7581      Raise SystemError if CFG is malformed. */
7582     for (int i = 0; i < bb->b_iused; i++) {
7583         switch(bb->b_instr[i].i_opcode) {
7584             case RETURN_VALUE:
7585             case RAISE_VARARGS:
7586             case RERAISE:
7587                 bb->b_exit = 1;
7588                 bb->b_nofallthrough = 1;
7589                 break;
7590             case JUMP_ABSOLUTE:
7591             case JUMP_FORWARD:
7592                 bb->b_nofallthrough = 1;
7593                 /* fall through */
7594             case POP_JUMP_IF_FALSE:
7595             case POP_JUMP_IF_TRUE:
7596             case JUMP_IF_FALSE_OR_POP:
7597             case JUMP_IF_TRUE_OR_POP:
7598             case FOR_ITER:
7599                 if (i != bb->b_iused-1) {
7600                     PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
7601                     return -1;
7602                 }
7603                 /* Skip over empty basic blocks. */
7604                 while (bb->b_instr[i].i_target->b_iused == 0) {
7605                     bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
7606                 }
7607 
7608         }
7609     }
7610     return 0;
7611 }
7612 
7613 static int
mark_reachable(struct assembler * a)7614 mark_reachable(struct assembler *a) {
7615     basicblock **stack, **sp;
7616     sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
7617     if (stack == NULL) {
7618         return -1;
7619     }
7620     a->a_entry->b_predecessors = 1;
7621     *sp++ = a->a_entry;
7622     while (sp > stack) {
7623         basicblock *b = *(--sp);
7624         if (b->b_next && !b->b_nofallthrough) {
7625             if (b->b_next->b_predecessors == 0) {
7626                 *sp++ = b->b_next;
7627             }
7628             b->b_next->b_predecessors++;
7629         }
7630         for (int i = 0; i < b->b_iused; i++) {
7631             basicblock *target;
7632             if (is_jump(&b->b_instr[i])) {
7633                 target = b->b_instr[i].i_target;
7634                 if (target->b_predecessors == 0) {
7635                     *sp++ = target;
7636                 }
7637                 target->b_predecessors++;
7638             }
7639         }
7640     }
7641     PyObject_Free(stack);
7642     return 0;
7643 }
7644 
7645 static void
eliminate_empty_basic_blocks(basicblock * entry)7646 eliminate_empty_basic_blocks(basicblock *entry) {
7647     /* Eliminate empty blocks */
7648     for (basicblock *b = entry; b != NULL; b = b->b_next) {
7649         basicblock *next = b->b_next;
7650         if (next) {
7651             while (next->b_iused == 0 && next->b_next) {
7652                 next = next->b_next;
7653             }
7654             b->b_next = next;
7655         }
7656     }
7657     for (basicblock *b = entry; b != NULL; b = b->b_next) {
7658         if (b->b_iused == 0) {
7659             continue;
7660         }
7661         if (is_jump(&b->b_instr[b->b_iused-1])) {
7662             basicblock *target = b->b_instr[b->b_iused-1].i_target;
7663             while (target->b_iused == 0) {
7664                 target = target->b_next;
7665             }
7666             b->b_instr[b->b_iused-1].i_target = target;
7667         }
7668     }
7669 }
7670 
7671 
7672 /* If an instruction has no line number, but it's predecessor in the BB does,
7673  * then copy the line number. If a successor block has no line number, and only
7674  * one predecessor, then inherit the line number.
7675  * This ensures that all exit blocks (with one predecessor) receive a line number.
7676  * Also reduces the size of the line number table,
7677  * but has no impact on the generated line number events.
7678  */
7679 static void
propagate_line_numbers(struct assembler * a)7680 propagate_line_numbers(struct assembler *a) {
7681     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7682         if (b->b_iused == 0) {
7683             continue;
7684         }
7685         int prev_lineno = -1;
7686         for (int i = 0; i < b->b_iused; i++) {
7687             if (b->b_instr[i].i_lineno < 0) {
7688                 b->b_instr[i].i_lineno = prev_lineno;
7689             }
7690             else {
7691                 prev_lineno = b->b_instr[i].i_lineno;
7692             }
7693         }
7694         if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) {
7695             assert(b->b_next->b_iused);
7696             if (b->b_next->b_instr[0].i_lineno < 0) {
7697                 b->b_next->b_instr[0].i_lineno = prev_lineno;
7698             }
7699         }
7700         if (is_jump(&b->b_instr[b->b_iused-1])) {
7701             switch (b->b_instr[b->b_iused-1].i_opcode) {
7702                 /* Note: Only actual jumps, not exception handlers */
7703                 case SETUP_ASYNC_WITH:
7704                 case SETUP_WITH:
7705                 case SETUP_FINALLY:
7706                     continue;
7707             }
7708             basicblock *target = b->b_instr[b->b_iused-1].i_target;
7709             if (target->b_predecessors == 1) {
7710                 if (target->b_instr[0].i_lineno < 0) {
7711                     target->b_instr[0].i_lineno = prev_lineno;
7712                 }
7713             }
7714         }
7715     }
7716 }
7717 
7718 /* Perform optimizations on a control flow graph.
7719    The consts object should still be in list form to allow new constants
7720    to be appended.
7721 
7722    All transformations keep the code size the same or smaller.
7723    For those that reduce size, the gaps are initially filled with
7724    NOPs.  Later those NOPs are removed.
7725 */
7726 
7727 static int
optimize_cfg(struct compiler * c,struct assembler * a,PyObject * consts)7728 optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
7729 {
7730     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7731         if (optimize_basic_block(c, b, consts)) {
7732             return -1;
7733         }
7734         clean_basic_block(b, -1);
7735         assert(b->b_predecessors == 0);
7736     }
7737     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7738         if (extend_block(b)) {
7739             return -1;
7740         }
7741     }
7742     if (mark_reachable(a)) {
7743         return -1;
7744     }
7745     /* Delete unreachable instructions */
7746     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7747        if (b->b_predecessors == 0) {
7748             b->b_iused = 0;
7749             b->b_nofallthrough = 0;
7750        }
7751     }
7752     basicblock *pred = NULL;
7753     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7754         int prev_lineno = -1;
7755         if (pred && pred->b_iused) {
7756             prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno;
7757         }
7758         clean_basic_block(b, prev_lineno);
7759         pred = b->b_nofallthrough ? NULL : b;
7760     }
7761     eliminate_empty_basic_blocks(a->a_entry);
7762     /* Delete jump instructions made redundant by previous step. If a non-empty
7763        block ends with a jump instruction, check if the next non-empty block
7764        reached through normal flow control is the target of that jump. If it
7765        is, then the jump instruction is redundant and can be deleted.
7766     */
7767     int maybe_empty_blocks = 0;
7768     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7769         if (b->b_iused > 0) {
7770             struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
7771             if (b_last_instr->i_opcode == JUMP_ABSOLUTE ||
7772                 b_last_instr->i_opcode == JUMP_FORWARD) {
7773                 if (b_last_instr->i_target == b->b_next) {
7774                     assert(b->b_next->b_iused);
7775                     b->b_nofallthrough = 0;
7776                     b_last_instr->i_opcode = NOP;
7777                     clean_basic_block(b, -1);
7778                     maybe_empty_blocks = 1;
7779                 }
7780             }
7781         }
7782     }
7783     if (maybe_empty_blocks) {
7784         eliminate_empty_basic_blocks(a->a_entry);
7785     }
7786     return 0;
7787 }
7788 
7789 // Remove trailing unused constants.
7790 static int
trim_unused_consts(struct compiler * c,struct assembler * a,PyObject * consts)7791 trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts)
7792 {
7793     assert(PyList_CheckExact(consts));
7794 
7795     // The first constant may be docstring; keep it always.
7796     int max_const_index = 0;
7797     for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
7798         for (int i = 0; i < b->b_iused; i++) {
7799             if (b->b_instr[i].i_opcode == LOAD_CONST &&
7800                     b->b_instr[i].i_oparg > max_const_index) {
7801                 max_const_index = b->b_instr[i].i_oparg;
7802             }
7803         }
7804     }
7805     if (max_const_index+1 < PyList_GET_SIZE(consts)) {
7806         //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
7807         //        max_const_index, (int)PyList_GET_SIZE(consts));
7808         if (PyList_SetSlice(consts, max_const_index+1,
7809                             PyList_GET_SIZE(consts), NULL) < 0) {
7810             return 1;
7811         }
7812     }
7813     return 0;
7814 }
7815 
7816 static inline int
is_exit_without_lineno(basicblock * b)7817 is_exit_without_lineno(basicblock *b) {
7818     return b->b_exit && b->b_instr[0].i_lineno < 0;
7819 }
7820 
7821 /* PEP 626 mandates that the f_lineno of a frame is correct
7822  * after a frame terminates. It would be prohibitively expensive
7823  * to continuously update the f_lineno field at runtime,
7824  * so we make sure that all exiting instruction (raises and returns)
7825  * have a valid line number, allowing us to compute f_lineno lazily.
7826  * We can do this by duplicating the exit blocks without line number
7827  * so that none have more than one predecessor. We can then safely
7828  * copy the line number from the sole predecessor block.
7829  */
7830 static int
duplicate_exits_without_lineno(struct compiler * c)7831 duplicate_exits_without_lineno(struct compiler *c)
7832 {
7833     /* Copy all exit blocks without line number that are targets of a jump.
7834      */
7835     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7836         if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
7837             switch (b->b_instr[b->b_iused-1].i_opcode) {
7838                 /* Note: Only actual jumps, not exception handlers */
7839                 case SETUP_ASYNC_WITH:
7840                 case SETUP_WITH:
7841                 case SETUP_FINALLY:
7842                     continue;
7843             }
7844             basicblock *target = b->b_instr[b->b_iused-1].i_target;
7845             if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
7846                 basicblock *new_target = compiler_copy_block(c, target);
7847                 if (new_target == NULL) {
7848                     return -1;
7849                 }
7850                 new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7851                 b->b_instr[b->b_iused-1].i_target = new_target;
7852                 target->b_predecessors--;
7853                 new_target->b_predecessors = 1;
7854                 new_target->b_next = target->b_next;
7855                 target->b_next = new_target;
7856             }
7857         }
7858     }
7859     /* Eliminate empty blocks */
7860     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7861         while (b->b_next && b->b_next->b_iused == 0) {
7862             b->b_next = b->b_next->b_next;
7863         }
7864     }
7865     /* Any remaining reachable exit blocks without line number can only be reached by
7866      * fall through, and thus can only have a single predecessor */
7867     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
7868         if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) {
7869             if (is_exit_without_lineno(b->b_next)) {
7870                 assert(b->b_next->b_iused > 0);
7871                 b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno;
7872             }
7873         }
7874     }
7875     return 0;
7876 }
7877 
7878 
7879 /* Retained for API compatibility.
7880  * Optimization is now done in optimize_cfg */
7881 
7882 PyObject *
PyCode_Optimize(PyObject * code,PyObject * Py_UNUSED (consts),PyObject * Py_UNUSED (names),PyObject * Py_UNUSED (lnotab_obj))7883 PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
7884                 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
7885 {
7886     Py_INCREF(code);
7887     return code;
7888 }
7889