LCOV - code coverage report
Current view: top level - Python - compile.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 4694 5216 90.0 %
Date: 2022-07-07 18:19:46 Functions: 238 239 99.6 %

          Line data    Source code
       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             : // Need _PyOpcode_RelativeJump of pycore_opcode.h
      27             : #define NEED_OPCODE_TABLES
      28             : 
      29             : #include "Python.h"
      30             : #include "pycore_ast.h"           // _PyAST_GetDocString()
      31             : #include "pycore_code.h"          // _PyCode_New()
      32             : #include "pycore_compile.h"       // _PyFuture_FromAST()
      33             : #include "pycore_long.h"          // _PyLong_GetZero()
      34             : #include "pycore_opcode.h"        // _PyOpcode_Caches
      35             : #include "pycore_pymem.h"         // _PyMem_IsPtrFreed()
      36             : #include "pycore_symtable.h"      // PySTEntryObject
      37             : 
      38             : 
      39             : #define DEFAULT_BLOCK_SIZE 16
      40             : #define DEFAULT_CODE_SIZE 128
      41             : #define DEFAULT_LNOTAB_SIZE 16
      42             : #define DEFAULT_CNOTAB_SIZE 32
      43             : 
      44             : #define COMP_GENEXP   0
      45             : #define COMP_LISTCOMP 1
      46             : #define COMP_SETCOMP  2
      47             : #define COMP_DICTCOMP 3
      48             : 
      49             : /* A soft limit for stack use, to avoid excessive
      50             :  * memory use for large constants, etc.
      51             :  *
      52             :  * The value 30 is plucked out of thin air.
      53             :  * Code that could use more stack than this is
      54             :  * rare, so the exact value is unimportant.
      55             :  */
      56             : #define STACK_USE_GUIDELINE 30
      57             : 
      58             : /* If we exceed this limit, it should
      59             :  * be considered a compiler bug.
      60             :  * Currently it should be impossible
      61             :  * to exceed STACK_USE_GUIDELINE * 100,
      62             :  * as 100 is the maximum parse depth.
      63             :  * For performance reasons we will
      64             :  * want to reduce this to a
      65             :  * few hundred in the future.
      66             :  *
      67             :  * NOTE: Whatever MAX_ALLOWED_STACK_USE is
      68             :  * set to, it should never restrict what Python
      69             :  * we can write, just how we compile it.
      70             :  */
      71             : #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)
      72             : 
      73             : 
      74             : #define MAX_REAL_OPCODE 254
      75             : 
      76             : #define IS_WITHIN_OPCODE_RANGE(opcode) \
      77             :         (((opcode) >= 0 && (opcode) <= MAX_REAL_OPCODE) || \
      78             :          IS_PSEUDO_OPCODE(opcode))
      79             : 
      80             : #define IS_JUMP_OPCODE(opcode) \
      81             :          is_bit_set_in_table(_PyOpcode_Jump, opcode)
      82             : 
      83             : #define IS_BLOCK_PUSH_OPCODE(opcode) \
      84             :         ((opcode) == SETUP_FINALLY || \
      85             :          (opcode) == SETUP_WITH || \
      86             :          (opcode) == SETUP_CLEANUP)
      87             : 
      88             : /* opcodes which are not emitted in codegen stage, only by the assembler */
      89             : #define IS_ASSEMBLER_OPCODE(opcode) \
      90             :         ((opcode) == JUMP_FORWARD || \
      91             :          (opcode) == JUMP_BACKWARD || \
      92             :          (opcode) == JUMP_BACKWARD_NO_INTERRUPT || \
      93             :          (opcode) == POP_JUMP_FORWARD_IF_NONE || \
      94             :          (opcode) == POP_JUMP_BACKWARD_IF_NONE || \
      95             :          (opcode) == POP_JUMP_FORWARD_IF_NOT_NONE || \
      96             :          (opcode) == POP_JUMP_BACKWARD_IF_NOT_NONE || \
      97             :          (opcode) == POP_JUMP_FORWARD_IF_TRUE || \
      98             :          (opcode) == POP_JUMP_BACKWARD_IF_TRUE || \
      99             :          (opcode) == POP_JUMP_FORWARD_IF_FALSE || \
     100             :          (opcode) == POP_JUMP_BACKWARD_IF_FALSE)
     101             : 
     102             : #define IS_BACKWARDS_JUMP_OPCODE(opcode) \
     103             :         ((opcode) == JUMP_BACKWARD || \
     104             :          (opcode) == JUMP_BACKWARD_NO_INTERRUPT || \
     105             :          (opcode) == POP_JUMP_BACKWARD_IF_NONE || \
     106             :          (opcode) == POP_JUMP_BACKWARD_IF_NOT_NONE || \
     107             :          (opcode) == POP_JUMP_BACKWARD_IF_TRUE || \
     108             :          (opcode) == POP_JUMP_BACKWARD_IF_FALSE)
     109             : 
     110             : #define IS_UNCONDITIONAL_JUMP_OPCODE(opcode) \
     111             :         ((opcode) == JUMP || \
     112             :          (opcode) == JUMP_NO_INTERRUPT || \
     113             :          (opcode) == JUMP_FORWARD || \
     114             :          (opcode) == JUMP_BACKWARD || \
     115             :          (opcode) == JUMP_BACKWARD_NO_INTERRUPT)
     116             : 
     117             : #define IS_SCOPE_EXIT_OPCODE(opcode) \
     118             :         ((opcode) == RETURN_VALUE || \
     119             :          (opcode) == RAISE_VARARGS || \
     120             :          (opcode) == RERAISE)
     121             : 
     122             : #define IS_TOP_LEVEL_AWAIT(c) ( \
     123             :         (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
     124             :         && (c->u->u_ste->ste_type == ModuleBlock))
     125             : 
     126             : struct location {
     127             :     int lineno;
     128             :     int end_lineno;
     129             :     int col_offset;
     130             :     int end_col_offset;
     131             : };
     132             : 
     133             : #define LOCATION(LNO, END_LNO, COL, END_COL) \
     134             :     ((const struct location){(LNO), (END_LNO), (COL), (END_COL)})
     135             : 
     136             : static struct location NO_LOCATION = {-1, -1, -1, -1};
     137             : 
     138             : struct instr {
     139             :     int i_opcode;
     140             :     int i_oparg;
     141             :     /* target block (if jump instruction) */
     142             :     struct basicblock_ *i_target;
     143             :      /* target block when exception is raised, should not be set by front-end. */
     144             :     struct basicblock_ *i_except;
     145             :     struct location i_loc;
     146             : };
     147             : 
     148             : typedef struct exceptstack {
     149             :     struct basicblock_ *handlers[CO_MAXBLOCKS+1];
     150             :     int depth;
     151             : } ExceptStack;
     152             : 
     153             : #define LOG_BITS_PER_INT 5
     154             : #define MASK_LOW_LOG_BITS 31
     155             : 
     156             : static inline int
     157   171648000 : is_bit_set_in_table(const uint32_t *table, int bitindex) {
     158             :     /* Is the relevant bit set in the relevant word? */
     159             :     /* 512 bits fit into 9 32-bits words.
     160             :      * Word is indexed by (bitindex>>ln(size of int in bits)).
     161             :      * Bit within word is the low bits of bitindex.
     162             :      */
     163   171648000 :     if (bitindex >= 0 && bitindex < 512) {
     164   171648000 :         uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
     165   171648000 :         return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
     166             :     }
     167             :     else {
     168           0 :         return 0;
     169             :     }
     170             : }
     171             : 
     172             : static inline int
     173     1069810 : is_relative_jump(struct instr *i)
     174             : {
     175     1069810 :     return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
     176             : }
     177             : 
     178             : static inline int
     179   107123000 : is_block_push(struct instr *i)
     180             : {
     181   107123000 :     return IS_BLOCK_PUSH_OPCODE(i->i_opcode);
     182             : }
     183             : 
     184             : static inline int
     185   133168000 : is_jump(struct instr *i)
     186             : {
     187   133168000 :     return IS_JUMP_OPCODE(i->i_opcode);
     188             : }
     189             : 
     190             : static int
     191    92666700 : instr_size(struct instr *instruction)
     192             : {
     193    92666700 :     int opcode = instruction->i_opcode;
     194    92666700 :     assert(!IS_PSEUDO_OPCODE(opcode));
     195    92666700 :     int oparg = HAS_ARG(opcode) ? instruction->i_oparg : 0;
     196    92666700 :     int extended_args = (0xFFFFFF < oparg) + (0xFFFF < oparg) + (0xFF < oparg);
     197    92666700 :     int caches = _PyOpcode_Caches[opcode];
     198    92666700 :     return extended_args + 1 + caches;
     199             : }
     200             : 
     201             : static void
     202    17368800 : write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
     203             : {
     204    17368800 :     int opcode = instruction->i_opcode;
     205    17368800 :     assert(!IS_PSEUDO_OPCODE(opcode));
     206    17368800 :     int oparg = HAS_ARG(opcode) ? instruction->i_oparg : 0;
     207    17368800 :     int caches = _PyOpcode_Caches[opcode];
     208    17368800 :     switch (ilen - caches) {
     209           0 :         case 4:
     210           0 :             *codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 24) & 0xFF);
     211             :             /* fall through */
     212           0 :         case 3:
     213           0 :             *codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 16) & 0xFF);
     214             :             /* fall through */
     215      401670 :         case 2:
     216      401670 :             *codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 8) & 0xFF);
     217             :             /* fall through */
     218    17368800 :         case 1:
     219    17368800 :             *codestr++ = _Py_MAKECODEUNIT(opcode, oparg & 0xFF);
     220    17368800 :             break;
     221           0 :         default:
     222           0 :             Py_UNREACHABLE();
     223             :     }
     224    38539100 :     while (caches--) {
     225    21170300 :         *codestr++ = _Py_MAKECODEUNIT(CACHE, 0);
     226             :     }
     227    17368800 : }
     228             : 
     229             : typedef struct basicblock_ {
     230             :     /* Each basicblock in a compilation unit is linked via b_list in the
     231             :        reverse order that the block are allocated.  b_list points to the next
     232             :        block, not to be confused with b_next, which is next by control flow. */
     233             :     struct basicblock_ *b_list;
     234             :     /* Exception stack at start of block, used by assembler to create the exception handling table */
     235             :     ExceptStack *b_exceptstack;
     236             :     /* pointer to an array of instructions, initially NULL */
     237             :     struct instr *b_instr;
     238             :     /* If b_next is non-NULL, it is a pointer to the next
     239             :        block reached by normal control flow. */
     240             :     struct basicblock_ *b_next;
     241             :     /* number of instructions used */
     242             :     int b_iused;
     243             :     /* length of instruction array (b_instr) */
     244             :     int b_ialloc;
     245             :     /* Number of predecssors that a block has. */
     246             :     int b_predecessors;
     247             :     /* Number of predecssors that a block has as an exception handler. */
     248             :     int b_except_predecessors;
     249             :     /* depth of stack upon entry of block, computed by stackdepth() */
     250             :     int b_startdepth;
     251             :     /* instruction offset for block, computed by assemble_jump_offsets() */
     252             :     int b_offset;
     253             :     /* Basic block is an exception handler that preserves lasti */
     254             :     unsigned b_preserve_lasti : 1;
     255             :     /* Used by compiler passes to mark whether they have visited a basic block. */
     256             :     unsigned b_visited : 1;
     257             :     /* b_cold is true if this block is not perf critical (like an exception handler) */
     258             :     unsigned b_cold : 1;
     259             :     /* b_warm is used by the cold-detection algorithm to mark blocks which are definitely not cold */
     260             :     unsigned b_warm : 1;
     261             : } basicblock;
     262             : 
     263             : 
     264             : static struct instr *
     265    35966900 : basicblock_last_instr(basicblock *b) {
     266    35966900 :     if (b->b_iused) {
     267    35861600 :         return &b->b_instr[b->b_iused - 1];
     268             :     }
     269      105255 :     return NULL;
     270             : }
     271             : 
     272             : static inline int
     273      465113 : basicblock_returns(basicblock *b) {
     274      465113 :     struct instr *last = basicblock_last_instr(b);
     275      465113 :     return last && last->i_opcode == RETURN_VALUE;
     276             : }
     277             : 
     278             : static inline int
     279     2763310 : basicblock_exits_scope(basicblock *b) {
     280     2763310 :     struct instr *last = basicblock_last_instr(b);
     281     2763310 :     return last && IS_SCOPE_EXIT_OPCODE(last->i_opcode);
     282             : }
     283             : 
     284             : static inline int
     285    16021300 : basicblock_nofallthrough(basicblock *b) {
     286    16021300 :     struct instr *last = basicblock_last_instr(b);
     287    31998100 :     return (last &&
     288    15976900 :             (IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
     289    12349500 :              IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
     290             : }
     291             : 
     292             : #define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
     293             : #define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
     294             : 
     295             : /* fblockinfo tracks the current frame block.
     296             : 
     297             : A frame block is used to handle loops, try/except, and try/finally.
     298             : It's called a frame block to distinguish it from a basic block in the
     299             : compiler IR.
     300             : */
     301             : 
     302             : enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
     303             :                   WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
     304             :                   EXCEPTION_GROUP_HANDLER, ASYNC_COMPREHENSION_GENERATOR };
     305             : 
     306             : struct fblockinfo {
     307             :     enum fblocktype fb_type;
     308             :     basicblock *fb_block;
     309             :     /* (optional) type-specific exit or cleanup block */
     310             :     basicblock *fb_exit;
     311             :     /* (optional) additional information required for unwinding */
     312             :     void *fb_datum;
     313             : };
     314             : 
     315             : enum {
     316             :     COMPILER_SCOPE_MODULE,
     317             :     COMPILER_SCOPE_CLASS,
     318             :     COMPILER_SCOPE_FUNCTION,
     319             :     COMPILER_SCOPE_ASYNC_FUNCTION,
     320             :     COMPILER_SCOPE_LAMBDA,
     321             :     COMPILER_SCOPE_COMPREHENSION,
     322             : };
     323             : 
     324             : /* The following items change on entry and exit of code blocks.
     325             :    They must be saved and restored when returning to a block.
     326             : */
     327             : struct compiler_unit {
     328             :     PySTEntryObject *u_ste;
     329             : 
     330             :     PyObject *u_name;
     331             :     PyObject *u_qualname;  /* dot-separated qualified name (lazy) */
     332             :     int u_scope_type;
     333             : 
     334             :     /* The following fields are dicts that map objects to
     335             :        the index of them in co_XXX.      The index is used as
     336             :        the argument for opcodes that refer to those collections.
     337             :     */
     338             :     PyObject *u_consts;    /* all constants */
     339             :     PyObject *u_names;     /* all names */
     340             :     PyObject *u_varnames;  /* local variables */
     341             :     PyObject *u_cellvars;  /* cell variables */
     342             :     PyObject *u_freevars;  /* free variables */
     343             : 
     344             :     PyObject *u_private;        /* for private name mangling */
     345             : 
     346             :     Py_ssize_t u_argcount;        /* number of arguments for block */
     347             :     Py_ssize_t u_posonlyargcount;        /* number of positional only arguments for block */
     348             :     Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
     349             :     /* Pointer to the most recently allocated block.  By following b_list
     350             :        members, you can reach all early allocated blocks. */
     351             :     basicblock *u_blocks;
     352             :     basicblock *u_curblock; /* pointer to current block */
     353             : 
     354             :     int u_nfblocks;
     355             :     struct fblockinfo u_fblock[CO_MAXBLOCKS];
     356             : 
     357             :     int u_firstlineno; /* the first lineno of the block */
     358             :     struct location u_loc;  /* line/column info of the current stmt */
     359             : };
     360             : 
     361             : /* This struct captures the global state of a compilation.
     362             : 
     363             : The u pointer points to the current compilation unit, while units
     364             : for enclosing blocks are stored in c_stack.     The u and c_stack are
     365             : managed by compiler_enter_scope() and compiler_exit_scope().
     366             : 
     367             : Note that we don't track recursion levels during compilation - the
     368             : task of detecting and rejecting excessive levels of nesting is
     369             : handled by the symbol analysis pass.
     370             : 
     371             : */
     372             : 
     373             : struct compiler {
     374             :     PyObject *c_filename;
     375             :     struct symtable *c_st;
     376             :     PyFutureFeatures *c_future; /* pointer to module's __future__ */
     377             :     PyCompilerFlags *c_flags;
     378             : 
     379             :     int c_optimize;              /* optimization level */
     380             :     int c_interactive;           /* true if in interactive mode */
     381             :     int c_nestlevel;
     382             :     PyObject *c_const_cache;     /* Python dict holding all constants,
     383             :                                     including names tuple */
     384             :     struct compiler_unit *u; /* compiler state for current block */
     385             :     PyObject *c_stack;           /* Python list holding compiler_unit ptrs */
     386             :     PyArena *c_arena;            /* pointer to memory allocation arena */
     387             : };
     388             : 
     389             : typedef struct {
     390             :     // A list of strings corresponding to name captures. It is used to track:
     391             :     // - Repeated name assignments in the same pattern.
     392             :     // - Different name assignments in alternatives.
     393             :     // - The order of name assignments in alternatives.
     394             :     PyObject *stores;
     395             :     // If 0, any name captures against our subject will raise.
     396             :     int allow_irrefutable;
     397             :     // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
     398             :     // i items off of the stack. The end result looks like this (with each block
     399             :     // falling through to the next):
     400             :     // fail_pop[4]: POP_TOP
     401             :     // fail_pop[3]: POP_TOP
     402             :     // fail_pop[2]: POP_TOP
     403             :     // fail_pop[1]: POP_TOP
     404             :     // fail_pop[0]: NOP
     405             :     basicblock **fail_pop;
     406             :     // The current length of fail_pop.
     407             :     Py_ssize_t fail_pop_size;
     408             :     // The number of items on top of the stack that need to *stay* on top of the
     409             :     // stack. Variable captures go beneath these. All of them will be popped on
     410             :     // failure.
     411             :     Py_ssize_t on_top;
     412             : } pattern_context;
     413             : 
     414             : static int basicblock_next_instr(basicblock *);
     415             : 
     416             : static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
     417             : static void compiler_free(struct compiler *);
     418             : static basicblock *compiler_new_block(struct compiler *);
     419             : static int compiler_addop(struct compiler *, int, bool);
     420             : static int compiler_addop_i(struct compiler *, int, Py_ssize_t, bool);
     421             : static int compiler_addop_j(struct compiler *, int, basicblock *, bool);
     422             : static int compiler_error(struct compiler *, const char *, ...);
     423             : static int compiler_warn(struct compiler *, const char *, ...);
     424             : static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
     425             : 
     426             : static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
     427             : static int compiler_visit_stmt(struct compiler *, stmt_ty);
     428             : static int compiler_visit_keyword(struct compiler *, keyword_ty);
     429             : static int compiler_visit_expr(struct compiler *, expr_ty);
     430             : static int compiler_augassign(struct compiler *, stmt_ty);
     431             : static int compiler_annassign(struct compiler *, stmt_ty);
     432             : static int compiler_subscript(struct compiler *, expr_ty);
     433             : static int compiler_slice(struct compiler *, expr_ty);
     434             : 
     435             : static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
     436             : 
     437             : 
     438             : static int compiler_with(struct compiler *, stmt_ty, int);
     439             : static int compiler_async_with(struct compiler *, stmt_ty, int);
     440             : static int compiler_async_for(struct compiler *, stmt_ty);
     441             : static int validate_keywords(struct compiler *c, asdl_keyword_seq *keywords);
     442             : static int compiler_call_simple_kw_helper(struct compiler *c,
     443             :                                           asdl_keyword_seq *keywords,
     444             :                                           Py_ssize_t nkwelts);
     445             : static int compiler_call_helper(struct compiler *c, int n,
     446             :                                 asdl_expr_seq *args,
     447             :                                 asdl_keyword_seq *keywords);
     448             : static int compiler_try_except(struct compiler *, stmt_ty);
     449             : static int compiler_try_star_except(struct compiler *, stmt_ty);
     450             : static int compiler_set_qualname(struct compiler *);
     451             : 
     452             : static int compiler_sync_comprehension_generator(
     453             :                                       struct compiler *c,
     454             :                                       asdl_comprehension_seq *generators, int gen_index,
     455             :                                       int depth,
     456             :                                       expr_ty elt, expr_ty val, int type);
     457             : 
     458             : static int compiler_async_comprehension_generator(
     459             :                                       struct compiler *c,
     460             :                                       asdl_comprehension_seq *generators, int gen_index,
     461             :                                       int depth,
     462             :                                       expr_ty elt, expr_ty val, int type);
     463             : 
     464             : static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
     465             : static int compiler_match(struct compiler *, stmt_ty);
     466             : static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
     467             :                                        pattern_context *);
     468             : 
     469             : static void clean_basic_block(basicblock *bb);
     470             : 
     471             : static PyCodeObject *assemble(struct compiler *, int addNone);
     472             : 
     473             : #define CAPSULE_NAME "compile.c compiler unit"
     474             : 
     475             : PyObject *
     476    13014000 : _Py_Mangle(PyObject *privateobj, PyObject *ident)
     477             : {
     478             :     /* Name mangling: __private becomes _classname__private.
     479             :        This is independent from how the name is used. */
     480             :     PyObject *result;
     481             :     size_t nlen, plen, ipriv;
     482             :     Py_UCS4 maxchar;
     483    20403700 :     if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
     484     8111060 :         PyUnicode_READ_CHAR(ident, 0) != '_' ||
     485      721364 :         PyUnicode_READ_CHAR(ident, 1) != '_') {
     486    12672900 :         Py_INCREF(ident);
     487    12672900 :         return ident;
     488             :     }
     489      341158 :     nlen = PyUnicode_GET_LENGTH(ident);
     490      341158 :     plen = PyUnicode_GET_LENGTH(privateobj);
     491             :     /* Don't mangle __id__ or names with dots.
     492             : 
     493             :        The only time a name with a dot can occur is when
     494             :        we are compiling an import statement that has a
     495             :        package name.
     496             : 
     497             :        TODO(jhylton): Decide whether we want to support
     498             :        mangling of the module name, e.g. __M.X.
     499             :     */
     500      671364 :     if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
     501      341160 :          PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
     502       10954 :         PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
     503      330209 :         Py_INCREF(ident);
     504      330209 :         return ident; /* Don't mangle __whatever__ */
     505             :     }
     506             :     /* Strip leading underscores from class name */
     507       10949 :     ipriv = 0;
     508       12111 :     while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
     509        1162 :         ipriv++;
     510       10949 :     if (ipriv == plen) {
     511           1 :         Py_INCREF(ident);
     512           1 :         return ident; /* Don't mangle if class is just underscores */
     513             :     }
     514       10948 :     plen -= ipriv;
     515             : 
     516       10948 :     if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
     517           0 :         PyErr_SetString(PyExc_OverflowError,
     518             :                         "private identifier too large to be mangled");
     519           0 :         return NULL;
     520             :     }
     521             : 
     522       10948 :     maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
     523       10948 :     if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
     524           0 :         maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
     525             : 
     526       10948 :     result = PyUnicode_New(1 + nlen + plen, maxchar);
     527       10948 :     if (!result)
     528           0 :         return 0;
     529             :     /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
     530       10948 :     PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
     531       10948 :     if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
     532           0 :         Py_DECREF(result);
     533           0 :         return NULL;
     534             :     }
     535       10948 :     if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
     536           0 :         Py_DECREF(result);
     537           0 :         return NULL;
     538             :     }
     539       10948 :     assert(_PyUnicode_CheckConsistency(result, 1));
     540       10948 :     return result;
     541             : }
     542             : 
     543             : static int
     544      114687 : compiler_init(struct compiler *c)
     545             : {
     546      114687 :     memset(c, 0, sizeof(struct compiler));
     547             : 
     548      114687 :     c->c_const_cache = PyDict_New();
     549      114687 :     if (!c->c_const_cache) {
     550           0 :         return 0;
     551             :     }
     552             : 
     553      114687 :     c->c_stack = PyList_New(0);
     554      114687 :     if (!c->c_stack) {
     555           0 :         Py_CLEAR(c->c_const_cache);
     556           0 :         return 0;
     557             :     }
     558             : 
     559      114687 :     return 1;
     560             : }
     561             : 
     562             : PyCodeObject *
     563      114687 : _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
     564             :                int optimize, PyArena *arena)
     565             : {
     566             :     struct compiler c;
     567      114687 :     PyCodeObject *co = NULL;
     568      114687 :     PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
     569             :     int merged;
     570      114687 :     if (!compiler_init(&c))
     571           0 :         return NULL;
     572      114687 :     Py_INCREF(filename);
     573      114687 :     c.c_filename = filename;
     574      114687 :     c.c_arena = arena;
     575      114687 :     c.c_future = _PyFuture_FromAST(mod, filename);
     576      114687 :     if (c.c_future == NULL)
     577          10 :         goto finally;
     578      114677 :     if (!flags) {
     579         180 :         flags = &local_flags;
     580             :     }
     581      114677 :     merged = c.c_future->ff_features | flags->cf_flags;
     582      114677 :     c.c_future->ff_features = merged;
     583      114677 :     flags->cf_flags = merged;
     584      114677 :     c.c_flags = flags;
     585      114677 :     c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
     586      114677 :     c.c_nestlevel = 0;
     587             : 
     588             :     _PyASTOptimizeState state;
     589      114677 :     state.optimize = c.c_optimize;
     590      114677 :     state.ff_features = merged;
     591             : 
     592      114677 :     if (!_PyAST_Optimize(mod, arena, &state)) {
     593           0 :         goto finally;
     594             :     }
     595             : 
     596      114677 :     c.c_st = _PySymtable_Build(mod, filename, c.c_future);
     597      114677 :     if (c.c_st == NULL) {
     598         152 :         if (!PyErr_Occurred())
     599           0 :             PyErr_SetString(PyExc_SystemError, "no symtable");
     600         152 :         goto finally;
     601             :     }
     602             : 
     603      114525 :     co = compiler_mod(&c, mod);
     604             : 
     605      114687 :  finally:
     606      114687 :     compiler_free(&c);
     607      114687 :     assert(co || PyErr_Occurred());
     608      114687 :     return co;
     609             : }
     610             : 
     611             : static void
     612      114687 : compiler_free(struct compiler *c)
     613             : {
     614      114687 :     if (c->c_st)
     615      114525 :         _PySymtable_Free(c->c_st);
     616      114687 :     if (c->c_future)
     617      114677 :         PyObject_Free(c->c_future);
     618      114687 :     Py_XDECREF(c->c_filename);
     619      114687 :     Py_DECREF(c->c_const_cache);
     620      114687 :     Py_DECREF(c->c_stack);
     621      114687 : }
     622             : 
     623             : static PyObject *
     624      465532 : list2dict(PyObject *list)
     625             : {
     626             :     Py_ssize_t i, n;
     627             :     PyObject *v, *k;
     628      465532 :     PyObject *dict = PyDict_New();
     629      465532 :     if (!dict) return NULL;
     630             : 
     631      465532 :     n = PyList_Size(list);
     632     1165720 :     for (i = 0; i < n; i++) {
     633      700185 :         v = PyLong_FromSsize_t(i);
     634      700185 :         if (!v) {
     635           0 :             Py_DECREF(dict);
     636           0 :             return NULL;
     637             :         }
     638      700185 :         k = PyList_GET_ITEM(list, i);
     639      700185 :         if (PyDict_SetItem(dict, k, v) < 0) {
     640           0 :             Py_DECREF(v);
     641           0 :             Py_DECREF(dict);
     642           0 :             return NULL;
     643             :         }
     644      700185 :         Py_DECREF(v);
     645             :     }
     646      465532 :     return dict;
     647             : }
     648             : 
     649             : /* Return new dict containing names from src that match scope(s).
     650             : 
     651             : src is a symbol table dictionary.  If the scope of a name matches
     652             : either scope_type or flag is set, insert it into the new dict.  The
     653             : values are integers, starting at offset and increasing by one for
     654             : each key.
     655             : */
     656             : 
     657             : static PyObject *
     658      931064 : dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
     659             : {
     660      931064 :     Py_ssize_t i = offset, scope, num_keys, key_i;
     661      931064 :     PyObject *k, *v, *dest = PyDict_New();
     662             :     PyObject *sorted_keys;
     663             : 
     664      931064 :     assert(offset >= 0);
     665      931064 :     if (dest == NULL)
     666           0 :         return NULL;
     667             : 
     668             :     /* Sort the keys so that we have a deterministic order on the indexes
     669             :        saved in the returned dictionary.  These indexes are used as indexes
     670             :        into the free and cell var storage.  Therefore if they aren't
     671             :        deterministic, then the generated bytecode is not deterministic.
     672             :     */
     673      931064 :     sorted_keys = PyDict_Keys(src);
     674      931064 :     if (sorted_keys == NULL)
     675           0 :         return NULL;
     676      931064 :     if (PyList_Sort(sorted_keys) != 0) {
     677           0 :         Py_DECREF(sorted_keys);
     678           0 :         return NULL;
     679             :     }
     680      931064 :     num_keys = PyList_GET_SIZE(sorted_keys);
     681             : 
     682     5403800 :     for (key_i = 0; key_i < num_keys; key_i++) {
     683             :         /* XXX this should probably be a macro in symtable.h */
     684             :         long vi;
     685     4472740 :         k = PyList_GET_ITEM(sorted_keys, key_i);
     686     4472740 :         v = PyDict_GetItemWithError(src, k);
     687     4472740 :         assert(v && PyLong_Check(v));
     688     4472740 :         vi = PyLong_AS_LONG(v);
     689     4472740 :         scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
     690             : 
     691     4472740 :         if (scope == scope_type || vi & flag) {
     692       56637 :             PyObject *item = PyLong_FromSsize_t(i);
     693       56637 :             if (item == NULL) {
     694           0 :                 Py_DECREF(sorted_keys);
     695           0 :                 Py_DECREF(dest);
     696           0 :                 return NULL;
     697             :             }
     698       56637 :             i++;
     699       56637 :             if (PyDict_SetItem(dest, k, item) < 0) {
     700           0 :                 Py_DECREF(sorted_keys);
     701           0 :                 Py_DECREF(item);
     702           0 :                 Py_DECREF(dest);
     703           0 :                 return NULL;
     704             :             }
     705       56637 :             Py_DECREF(item);
     706             :         }
     707             :     }
     708      931064 :     Py_DECREF(sorted_keys);
     709      931064 :     return dest;
     710             : }
     711             : 
     712             : static void
     713      816539 : compiler_unit_check(struct compiler_unit *u)
     714             : {
     715             :     basicblock *block;
     716     4521490 :     for (block = u->u_blocks; block != NULL; block = block->b_list) {
     717     3704950 :         assert(!_PyMem_IsPtrFreed(block));
     718     3704950 :         if (block->b_instr != NULL) {
     719     3478770 :             assert(block->b_ialloc > 0);
     720     3478770 :             assert(block->b_iused >= 0);
     721     3478770 :             assert(block->b_ialloc >= block->b_iused);
     722             :         }
     723             :         else {
     724      226174 :             assert (block->b_iused == 0);
     725      226174 :             assert (block->b_ialloc == 0);
     726             :         }
     727             :     }
     728      816539 : }
     729             : 
     730             : static void
     731      465532 : compiler_unit_free(struct compiler_unit *u)
     732             : {
     733             :     basicblock *b, *next;
     734             : 
     735      465532 :     compiler_unit_check(u);
     736      465532 :     b = u->u_blocks;
     737     2943840 :     while (b != NULL) {
     738     2478300 :         if (b->b_instr)
     739     2335430 :             PyObject_Free((void *)b->b_instr);
     740     2478300 :         next = b->b_list;
     741     2478300 :         PyObject_Free((void *)b);
     742     2478300 :         b = next;
     743             :     }
     744      465532 :     Py_CLEAR(u->u_ste);
     745      465532 :     Py_CLEAR(u->u_name);
     746      465532 :     Py_CLEAR(u->u_qualname);
     747      465532 :     Py_CLEAR(u->u_consts);
     748      465532 :     Py_CLEAR(u->u_names);
     749      465532 :     Py_CLEAR(u->u_varnames);
     750      465532 :     Py_CLEAR(u->u_freevars);
     751      465532 :     Py_CLEAR(u->u_cellvars);
     752      465532 :     Py_CLEAR(u->u_private);
     753      465532 :     PyObject_Free(u);
     754      465532 : }
     755             : 
     756             : static int
     757      351007 : compiler_set_qualname(struct compiler *c)
     758             : {
     759             :     Py_ssize_t stack_size;
     760      351007 :     struct compiler_unit *u = c->u;
     761             :     PyObject *name, *base;
     762             : 
     763      351007 :     base = NULL;
     764      351007 :     stack_size = PyList_GET_SIZE(c->c_stack);
     765      351007 :     assert(stack_size >= 1);
     766      351007 :     if (stack_size > 1) {
     767      214535 :         int scope, force_global = 0;
     768             :         struct compiler_unit *parent;
     769             :         PyObject *mangled, *capsule;
     770             : 
     771      214535 :         capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
     772      214535 :         parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
     773      214535 :         assert(parent);
     774             : 
     775      214535 :         if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
     776       29359 :             || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
     777       27558 :             || u->u_scope_type == COMPILER_SCOPE_CLASS) {
     778      193107 :             assert(u->u_name);
     779      193107 :             mangled = _Py_Mangle(parent->u_private, u->u_name);
     780      193107 :             if (!mangled)
     781           0 :                 return 0;
     782      193107 :             scope = _PyST_GetScope(parent->u_ste, mangled);
     783      193107 :             Py_DECREF(mangled);
     784      193107 :             assert(scope != GLOBAL_IMPLICIT);
     785      193107 :             if (scope == GLOBAL_EXPLICIT)
     786          25 :                 force_global = 1;
     787             :         }
     788             : 
     789      214535 :         if (!force_global) {
     790      214510 :             if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
     791      175572 :                 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
     792      175191 :                 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA)
     793             :             {
     794             :                 _Py_DECLARE_STR(dot_locals, ".<locals>");
     795       39392 :                 base = PyUnicode_Concat(parent->u_qualname,
     796             :                                         &_Py_STR(dot_locals));
     797       39392 :                 if (base == NULL)
     798           0 :                     return 0;
     799             :             }
     800             :             else {
     801      175118 :                 Py_INCREF(parent->u_qualname);
     802      175118 :                 base = parent->u_qualname;
     803             :             }
     804             :         }
     805             :     }
     806             : 
     807      351007 :     if (base != NULL) {
     808             :         _Py_DECLARE_STR(dot, ".");
     809      214510 :         name = PyUnicode_Concat(base, &_Py_STR(dot));
     810      214510 :         Py_DECREF(base);
     811      214510 :         if (name == NULL)
     812           0 :             return 0;
     813      214510 :         PyUnicode_Append(&name, u->u_name);
     814      214510 :         if (name == NULL)
     815           0 :             return 0;
     816             :     }
     817             :     else {
     818      136497 :         Py_INCREF(u->u_name);
     819      136497 :         name = u->u_name;
     820             :     }
     821      351007 :     u->u_qualname = name;
     822             : 
     823      351007 :     return 1;
     824             : }
     825             : 
     826             : 
     827             : /* Allocate a new block and return a pointer to it.
     828             :    Returns NULL on error.
     829             : */
     830             : static basicblock *
     831     2478300 : new_basicblock()
     832             : {
     833     2478300 :     basicblock *b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
     834     2478300 :     if (b == NULL) {
     835           0 :         PyErr_NoMemory();
     836           0 :         return NULL;
     837             :     }
     838     2478300 :     return b;
     839             : }
     840             : 
     841             : static basicblock *
     842     2462320 : compiler_new_block(struct compiler *c)
     843             : {
     844     2462320 :     basicblock *b = new_basicblock();
     845     2462320 :     if (b == NULL) {
     846           0 :         return NULL;
     847             :     }
     848             :     /* Extend the singly linked list of blocks with new block. */
     849     2462320 :     struct compiler_unit *u = c->u;
     850     2462320 :     b->b_list = u->u_blocks;
     851     2462320 :     u->u_blocks = b;
     852     2462320 :     return b;
     853             : }
     854             : 
     855             : static basicblock *
     856     1996470 : compiler_use_next_block(struct compiler *c, basicblock *block)
     857             : {
     858     1996470 :     assert(block != NULL);
     859     1996470 :     c->u->u_curblock->b_next = block;
     860     1996470 :     c->u->u_curblock = block;
     861     1996470 :     return block;
     862             : }
     863             : 
     864             : static basicblock *
     865       15980 : basicblock_new_b_list_successor(basicblock *prev)
     866             : {
     867       15980 :     basicblock *result = new_basicblock();
     868       15980 :     if (result == NULL) {
     869           0 :         return NULL;
     870             :     }
     871       15980 :     result->b_list = prev->b_list;
     872       15980 :     prev->b_list = result;
     873       15980 :     return result;
     874             : }
     875             : 
     876             : static basicblock *
     877       15879 : copy_basicblock(basicblock *block)
     878             : {
     879             :     /* Cannot copy a block if it has a fallthrough, since
     880             :      * a block can only have one fallthrough predecessor.
     881             :      */
     882       15879 :     assert(BB_NO_FALLTHROUGH(block));
     883       15879 :     basicblock *result = basicblock_new_b_list_successor(block);
     884       15879 :     if (result == NULL) {
     885           0 :         return NULL;
     886             :     }
     887       47953 :     for (int i = 0; i < block->b_iused; i++) {
     888       32074 :         int n = basicblock_next_instr(result);
     889       32074 :         if (n < 0) {
     890           0 :             return NULL;
     891             :         }
     892       32074 :         result->b_instr[n] = block->b_instr[i];
     893             :     }
     894       15879 :     return result;
     895             : }
     896             : 
     897             : /* Returns the offset of the next instruction in the current block's
     898             :    b_instr array.  Resizes the b_instr as necessary.
     899             :    Returns -1 on failure.
     900             : */
     901             : 
     902             : static int
     903    18594400 : basicblock_next_instr(basicblock *b)
     904             : {
     905    18594400 :     assert(b != NULL);
     906    18594400 :     if (b->b_instr == NULL) {
     907     2335430 :         b->b_instr = (struct instr *)PyObject_Calloc(
     908             :                          DEFAULT_BLOCK_SIZE, sizeof(struct instr));
     909     2335430 :         if (b->b_instr == NULL) {
     910           0 :             PyErr_NoMemory();
     911           0 :             return -1;
     912             :         }
     913     2335430 :         b->b_ialloc = DEFAULT_BLOCK_SIZE;
     914             :     }
     915    16259000 :     else if (b->b_iused == b->b_ialloc) {
     916             :         struct instr *tmp;
     917             :         size_t oldsize, newsize;
     918      236013 :         oldsize = b->b_ialloc * sizeof(struct instr);
     919      236013 :         newsize = oldsize << 1;
     920             : 
     921      236013 :         if (oldsize > (SIZE_MAX >> 1)) {
     922           0 :             PyErr_NoMemory();
     923           0 :             return -1;
     924             :         }
     925             : 
     926      236013 :         if (newsize == 0) {
     927           0 :             PyErr_NoMemory();
     928           0 :             return -1;
     929             :         }
     930      236013 :         b->b_ialloc <<= 1;
     931      236013 :         tmp = (struct instr *)PyObject_Realloc(
     932      236013 :                                         (void *)b->b_instr, newsize);
     933      236013 :         if (tmp == NULL) {
     934           0 :             PyErr_NoMemory();
     935           0 :             return -1;
     936             :         }
     937      236013 :         b->b_instr = tmp;
     938      236013 :         memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
     939             :     }
     940    18594400 :     return b->b_iused++;
     941             : }
     942             : 
     943             : /* Set the line number and column offset for the following instructions.
     944             : 
     945             :    The line number is reset in the following cases:
     946             :    - when entering a new scope
     947             :    - on each statement
     948             :    - on each expression and sub-expression
     949             :    - before the "except" and "finally" clauses
     950             : */
     951             : 
     952             : #define SET_LOC(c, x)                                   \
     953             :     (c)->u->u_loc.lineno = (x)->lineno;                 \
     954             :     (c)->u->u_loc.end_lineno = (x)->end_lineno;         \
     955             :     (c)->u->u_loc.col_offset = (x)->col_offset;         \
     956             :     (c)->u->u_loc.end_col_offset = (x)->end_col_offset;
     957             : 
     958             : // Artificial instructions
     959             : #define UNSET_LOC(c) \
     960             :     (c)->u->u_loc.lineno = -1;             \
     961             :     (c)->u->u_loc.end_lineno = -1;         \
     962             :     (c)->u->u_loc.col_offset = -1;         \
     963             :     (c)->u->u_loc.end_col_offset = -1;
     964             : 
     965             : 
     966             : /* Return the stack effect of opcode with argument oparg.
     967             : 
     968             :    Some opcodes have different stack effect when jump to the target and
     969             :    when not jump. The 'jump' parameter specifies the case:
     970             : 
     971             :    * 0 -- when not jump
     972             :    * 1 -- when jump
     973             :    * -1 -- maximal
     974             :  */
     975             : static int
     976    18688000 : stack_effect(int opcode, int oparg, int jump)
     977             : {
     978    18688000 :     switch (opcode) {
     979      518873 :         case NOP:
     980             :         case EXTENDED_ARG:
     981             :         case RESUME:
     982             :         case CACHE:
     983      518873 :             return 0;
     984             : 
     985             :         /* Stack manipulation */
     986      755031 :         case POP_TOP:
     987      755031 :             return -1;
     988       17699 :         case SWAP:
     989       17699 :             return 0;
     990             : 
     991             :         /* Unary operators */
     992        7516 :         case UNARY_POSITIVE:
     993             :         case UNARY_NEGATIVE:
     994             :         case UNARY_NOT:
     995             :         case UNARY_INVERT:
     996        7516 :             return 0;
     997             : 
     998      175598 :         case SET_ADD:
     999             :         case LIST_APPEND:
    1000      175598 :             return -1;
    1001      519329 :         case MAP_ADD:
    1002      519329 :             return -2;
    1003             : 
    1004      125482 :         case BINARY_SUBSCR:
    1005      125482 :             return -1;
    1006       27915 :         case BINARY_SLICE:
    1007       27915 :             return -2;
    1008       33390 :         case STORE_SUBSCR:
    1009       33390 :             return -3;
    1010        1491 :         case STORE_SLICE:
    1011        1491 :             return -4;
    1012        3930 :         case DELETE_SUBSCR:
    1013        3930 :             return -2;
    1014             : 
    1015       64712 :         case GET_ITER:
    1016       64712 :             return 0;
    1017             : 
    1018        2626 :         case PRINT_EXPR:
    1019        2626 :             return -1;
    1020       37725 :         case LOAD_BUILD_CLASS:
    1021       37725 :             return 1;
    1022             : 
    1023      559336 :         case RETURN_VALUE:
    1024      559336 :             return -1;
    1025        1023 :         case IMPORT_STAR:
    1026        1023 :             return -1;
    1027        2190 :         case SETUP_ANNOTATIONS:
    1028        2190 :             return 0;
    1029       23973 :         case ASYNC_GEN_WRAP:
    1030             :         case YIELD_VALUE:
    1031       23973 :             return 0;
    1032      121224 :         case POP_BLOCK:
    1033      121224 :             return 0;
    1034      113533 :         case POP_EXCEPT:
    1035      113533 :             return -1;
    1036             : 
    1037      592374 :         case STORE_NAME:
    1038      592374 :             return -1;
    1039        2521 :         case DELETE_NAME:
    1040        2521 :             return 0;
    1041       39611 :         case UNPACK_SEQUENCE:
    1042       39611 :             return oparg-1;
    1043         264 :         case UNPACK_EX:
    1044         264 :             return (oparg&0xFF) + (oparg>>8);
    1045      129451 :         case FOR_ITER:
    1046             :             /* -1 at end of iterator, 1 if continue iterating. */
    1047      129451 :             return jump > 0 ? -1 : 1;
    1048        9066 :         case SEND:
    1049        9066 :             return jump > 0 ? -1 : 0;
    1050      124803 :         case STORE_ATTR:
    1051      124803 :             return -2;
    1052        1056 :         case DELETE_ATTR:
    1053        1056 :             return -1;
    1054        3615 :         case STORE_GLOBAL:
    1055        3615 :             return -1;
    1056          17 :         case DELETE_GLOBAL:
    1057          17 :             return 0;
    1058     3335010 :         case LOAD_CONST:
    1059     3335010 :             return 1;
    1060      916253 :         case LOAD_NAME:
    1061      916253 :             return 1;
    1062      285099 :         case BUILD_TUPLE:
    1063             :         case BUILD_LIST:
    1064             :         case BUILD_SET:
    1065             :         case BUILD_STRING:
    1066      285099 :             return 1-oparg;
    1067       55946 :         case BUILD_MAP:
    1068       55946 :             return 1 - 2*oparg;
    1069       20033 :         case BUILD_CONST_KEY_MAP:
    1070       20033 :             return -oparg;
    1071     1210260 :         case LOAD_ATTR:
    1072     1210260 :             return (oparg & 1);
    1073      170566 :         case COMPARE_OP:
    1074             :         case IS_OP:
    1075             :         case CONTAINS_OP:
    1076      170566 :             return -1;
    1077       35436 :         case CHECK_EXC_MATCH:
    1078       35436 :             return 0;
    1079         124 :         case CHECK_EG_MATCH:
    1080         124 :             return 0;
    1081       79095 :         case IMPORT_NAME:
    1082       79095 :             return -1;
    1083       70904 :         case IMPORT_FROM:
    1084       70904 :             return 1;
    1085             : 
    1086             :         /* Jumps */
    1087      439175 :         case JUMP_FORWARD:
    1088             :         case JUMP_BACKWARD:
    1089             :         case JUMP:
    1090             :         case JUMP_BACKWARD_NO_INTERRUPT:
    1091             :         case JUMP_NO_INTERRUPT:
    1092      439175 :             return 0;
    1093             : 
    1094       38329 :         case JUMP_IF_TRUE_OR_POP:
    1095             :         case JUMP_IF_FALSE_OR_POP:
    1096       38329 :             return jump ? 0 : -1;
    1097             : 
    1098     1260260 :         case POP_JUMP_BACKWARD_IF_NONE:
    1099             :         case POP_JUMP_FORWARD_IF_NONE:
    1100             :         case POP_JUMP_IF_NONE:
    1101             :         case POP_JUMP_BACKWARD_IF_NOT_NONE:
    1102             :         case POP_JUMP_FORWARD_IF_NOT_NONE:
    1103             :         case POP_JUMP_IF_NOT_NONE:
    1104             :         case POP_JUMP_FORWARD_IF_FALSE:
    1105             :         case POP_JUMP_BACKWARD_IF_FALSE:
    1106             :         case POP_JUMP_IF_FALSE:
    1107             :         case POP_JUMP_FORWARD_IF_TRUE:
    1108             :         case POP_JUMP_BACKWARD_IF_TRUE:
    1109             :         case POP_JUMP_IF_TRUE:
    1110     1260260 :             return -1;
    1111             : 
    1112      761352 :         case LOAD_GLOBAL:
    1113      761352 :             return (oparg & 1) + 1;
    1114             : 
    1115             :         /* Exception handling pseudo-instructions */
    1116       81848 :         case SETUP_FINALLY:
    1117             :             /* 0 in the normal flow.
    1118             :              * Restore the stack position and push 1 value before jumping to
    1119             :              * the handler if an exception be raised. */
    1120       81848 :             return jump ? 1 : 0;
    1121      138764 :         case SETUP_CLEANUP:
    1122             :             /* As SETUP_FINALLY, but pushes lasti as well */
    1123      138764 :             return jump ? 2 : 0;
    1124       42518 :         case SETUP_WITH:
    1125             :             /* 0 in the normal flow.
    1126             :              * Restore the stack position to the position before the result
    1127             :              * of __(a)enter__ and push 2 values before jumping to the handler
    1128             :              * if an exception be raised. */
    1129       42518 :             return jump ? 1 : 0;
    1130             : 
    1131         104 :         case PREP_RERAISE_STAR:
    1132         104 :              return -1;
    1133      130532 :         case RERAISE:
    1134      130532 :             return -1;
    1135       62047 :         case PUSH_EXC_INFO:
    1136       62047 :             return 1;
    1137             : 
    1138       21261 :         case WITH_EXCEPT_START:
    1139       21261 :             return 1;
    1140             : 
    1141     2342630 :         case LOAD_FAST:
    1142             :         case LOAD_FAST_CHECK:
    1143     2342630 :             return 1;
    1144      570851 :         case STORE_FAST:
    1145      570851 :             return -1;
    1146       12677 :         case DELETE_FAST:
    1147       12677 :             return 0;
    1148             : 
    1149       16632 :         case RETURN_GENERATOR:
    1150       16632 :             return 0;
    1151             : 
    1152       60589 :         case RAISE_VARARGS:
    1153       60589 :             return -oparg;
    1154             : 
    1155             :         /* Functions and calls */
    1156       68011 :         case KW_NAMES:
    1157       68011 :             return 0;
    1158     1220160 :         case CALL:
    1159     1220160 :             return -1-oparg;
    1160             : 
    1161       12423 :         case CALL_FUNCTION_EX:
    1162       12423 :             return -2 - ((oparg & 0x01) != 0);
    1163      350853 :         case MAKE_FUNCTION:
    1164      350853 :             return 0 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
    1165      350853 :                 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
    1166        1749 :         case BUILD_SLICE:
    1167        1749 :             if (oparg == 3)
    1168         994 :                 return -2;
    1169             :             else
    1170         755 :                 return -1;
    1171             : 
    1172             :         /* Closures */
    1173       50867 :         case MAKE_CELL:
    1174             :         case COPY_FREE_VARS:
    1175       50867 :             return 0;
    1176       39838 :         case LOAD_CLOSURE:
    1177       39838 :             return 1;
    1178       75109 :         case LOAD_DEREF:
    1179             :         case LOAD_CLASSDEREF:
    1180       75109 :             return 1;
    1181       15448 :         case STORE_DEREF:
    1182       15448 :             return -1;
    1183          14 :         case DELETE_DEREF:
    1184          14 :             return 0;
    1185             : 
    1186             :         /* Iterators and generators */
    1187        2517 :         case GET_AWAITABLE:
    1188        2517 :             return 0;
    1189             : 
    1190       21291 :         case BEFORE_ASYNC_WITH:
    1191             :         case BEFORE_WITH:
    1192       21291 :             return 1;
    1193         140 :         case GET_AITER:
    1194         140 :             return 0;
    1195         146 :         case GET_ANEXT:
    1196         146 :             return 1;
    1197        1913 :         case GET_YIELD_FROM_ITER:
    1198        1913 :             return 0;
    1199         140 :         case END_ASYNC_FOR:
    1200         140 :             return -2;
    1201      118010 :         case FORMAT_VALUE:
    1202             :             /* If there's a fmt_spec on the stack, we go from 2->1,
    1203             :                else 1->1. */
    1204      118010 :             return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
    1205           4 :         case LOAD_METHOD:
    1206           4 :             return 1;
    1207        7959 :         case LOAD_ASSERTION_ERROR:
    1208        7959 :             return 1;
    1209        3962 :         case LIST_TO_TUPLE:
    1210        3962 :             return 0;
    1211       53290 :         case LIST_EXTEND:
    1212             :         case SET_UPDATE:
    1213             :         case DICT_MERGE:
    1214             :         case DICT_UPDATE:
    1215       53290 :             return -1;
    1216         184 :         case MATCH_CLASS:
    1217         184 :             return -2;
    1218        1585 :         case GET_LEN:
    1219             :         case MATCH_MAPPING:
    1220             :         case MATCH_SEQUENCE:
    1221             :         case MATCH_KEYS:
    1222        1585 :             return 1;
    1223      250983 :         case COPY:
    1224             :         case PUSH_NULL:
    1225      250983 :             return 1;
    1226      217564 :         case BINARY_OP:
    1227      217564 :             return -1;
    1228         145 :         default:
    1229         145 :             return PY_INVALID_STACK_EFFECT;
    1230             :     }
    1231             :     return PY_INVALID_STACK_EFFECT; /* not reachable */
    1232             : }
    1233             : 
    1234             : int
    1235         646 : PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
    1236             : {
    1237         646 :     return stack_effect(opcode, oparg, jump);
    1238             : }
    1239             : 
    1240             : int
    1241        5468 : PyCompile_OpcodeStackEffect(int opcode, int oparg)
    1242             : {
    1243        5468 :     return stack_effect(opcode, oparg, -1);
    1244             : }
    1245             : 
    1246             : static int
    1247    16717200 : is_end_of_basic_block(struct instr *instr)
    1248             : {
    1249    16717200 :     int opcode = instr->i_opcode;
    1250    16717200 :     return IS_JUMP_OPCODE(opcode) || IS_SCOPE_EXIT_OPCODE(opcode);
    1251             : }
    1252             : 
    1253             : static int
    1254    18365100 : compiler_use_new_implicit_block_if_needed(struct compiler *c)
    1255             : {
    1256    18365100 :     basicblock *b = c->u->u_curblock;
    1257    18365100 :     if (b->b_iused && is_end_of_basic_block(basicblock_last_instr(b))) {
    1258      671526 :         basicblock *b = compiler_new_block(c);
    1259      671526 :         if (b == NULL) {
    1260           0 :             return -1;
    1261             :         }
    1262      671526 :         compiler_use_next_block(c, b);
    1263             :     }
    1264    18365100 :     return 0;
    1265             : }
    1266             : 
    1267             : /* Add an opcode with no argument.
    1268             :    Returns 0 on failure, 1 on success.
    1269             : */
    1270             : 
    1271             : static int
    1272    18365200 : basicblock_addop(basicblock *b, int opcode, int oparg,
    1273             :                  basicblock *target, const struct location *loc)
    1274             : {
    1275    18365200 :     assert(IS_WITHIN_OPCODE_RANGE(opcode));
    1276    18365200 :     assert(!IS_ASSEMBLER_OPCODE(opcode));
    1277    18365200 :     assert(HAS_ARG(opcode) || oparg == 0);
    1278    18365200 :     assert(0 <= oparg && oparg < (1 << 30));
    1279    18365200 :     assert((target == NULL) ||
    1280             :            IS_JUMP_OPCODE(opcode) ||
    1281             :            IS_BLOCK_PUSH_OPCODE(opcode));
    1282    18365200 :     assert(oparg == 0 || target == NULL);
    1283             : 
    1284    18365200 :     int off = basicblock_next_instr(b);
    1285    18365200 :     if (off < 0) {
    1286           0 :         return 0;
    1287             :     }
    1288    18365200 :     struct instr *i = &b->b_instr[off];
    1289    18365200 :     i->i_opcode = opcode;
    1290    18365200 :     i->i_oparg = oparg;
    1291    18365200 :     i->i_target = target;
    1292    18365200 :     i->i_loc = loc ? *loc : NO_LOCATION;
    1293             : 
    1294    18365200 :     return 1;
    1295             : }
    1296             : 
    1297             : static int
    1298     2739540 : compiler_addop(struct compiler *c, int opcode, bool line)
    1299             : {
    1300     2739540 :     assert(!HAS_ARG(opcode));
    1301     2739540 :     if (compiler_use_new_implicit_block_if_needed(c) < 0) {
    1302           0 :         return -1;
    1303             :     }
    1304             : 
    1305     2739540 :     const struct location *loc = line ? &c->u->u_loc : NULL;
    1306     2739540 :     return basicblock_addop(c->u->u_curblock, opcode, 0, NULL, loc);
    1307             : }
    1308             : 
    1309             : static Py_ssize_t
    1310    10552700 : compiler_add_o(PyObject *dict, PyObject *o)
    1311             : {
    1312             :     PyObject *v;
    1313             :     Py_ssize_t arg;
    1314             : 
    1315    10552700 :     v = PyDict_GetItemWithError(dict, o);
    1316    10552700 :     if (!v) {
    1317     4717270 :         if (PyErr_Occurred()) {
    1318           0 :             return -1;
    1319             :         }
    1320     4717270 :         arg = PyDict_GET_SIZE(dict);
    1321     4717270 :         v = PyLong_FromSsize_t(arg);
    1322     4717270 :         if (!v) {
    1323           0 :             return -1;
    1324             :         }
    1325     4717270 :         if (PyDict_SetItem(dict, o, v) < 0) {
    1326           0 :             Py_DECREF(v);
    1327           0 :             return -1;
    1328             :         }
    1329     4717270 :         Py_DECREF(v);
    1330             :     }
    1331             :     else
    1332     5835390 :         arg = PyLong_AsLong(v);
    1333    10552700 :     return arg;
    1334             : }
    1335             : 
    1336             : // Merge const *o* recursively and return constant key object.
    1337             : static PyObject*
    1338     5082540 : merge_consts_recursive(PyObject *const_cache, PyObject *o)
    1339             : {
    1340     5082540 :     assert(PyDict_CheckExact(const_cache));
    1341             :     // None and Ellipsis are singleton, and key is the singleton.
    1342             :     // No need to merge object and key.
    1343     5082540 :     if (o == Py_None || o == Py_Ellipsis) {
    1344      746495 :         Py_INCREF(o);
    1345      746495 :         return o;
    1346             :     }
    1347             : 
    1348     4336040 :     PyObject *key = _PyCode_ConstantKey(o);
    1349     4336040 :     if (key == NULL) {
    1350           0 :         return NULL;
    1351             :     }
    1352             : 
    1353             :     // t is borrowed reference
    1354     4336040 :     PyObject *t = PyDict_SetDefault(const_cache, key, key);
    1355     4336040 :     if (t != key) {
    1356             :         // o is registered in const_cache.  Just use it.
    1357      506521 :         Py_XINCREF(t);
    1358      506521 :         Py_DECREF(key);
    1359      506521 :         return t;
    1360             :     }
    1361             : 
    1362             :     // We registered o in const_cache.
    1363             :     // When o is a tuple or frozenset, we want to merge its
    1364             :     // items too.
    1365     3829520 :     if (PyTuple_CheckExact(o)) {
    1366      190169 :         Py_ssize_t len = PyTuple_GET_SIZE(o);
    1367     1487050 :         for (Py_ssize_t i = 0; i < len; i++) {
    1368     1296880 :             PyObject *item = PyTuple_GET_ITEM(o, i);
    1369     1296880 :             PyObject *u = merge_consts_recursive(const_cache, item);
    1370     1296880 :             if (u == NULL) {
    1371           0 :                 Py_DECREF(key);
    1372           0 :                 return NULL;
    1373             :             }
    1374             : 
    1375             :             // See _PyCode_ConstantKey()
    1376             :             PyObject *v;  // borrowed
    1377     1296880 :             if (PyTuple_CheckExact(u)) {
    1378       78673 :                 v = PyTuple_GET_ITEM(u, 1);
    1379             :             }
    1380             :             else {
    1381     1218210 :                 v = u;
    1382             :             }
    1383     1296880 :             if (v != item) {
    1384       59132 :                 Py_INCREF(v);
    1385       59132 :                 PyTuple_SET_ITEM(o, i, v);
    1386       59132 :                 Py_DECREF(item);
    1387             :             }
    1388             : 
    1389     1296880 :             Py_DECREF(u);
    1390             :         }
    1391             :     }
    1392     3639350 :     else if (PyFrozenSet_CheckExact(o)) {
    1393             :         // *key* is tuple. And its first item is frozenset of
    1394             :         // constant keys.
    1395             :         // See _PyCode_ConstantKey() for detail.
    1396        1085 :         assert(PyTuple_CheckExact(key));
    1397        1085 :         assert(PyTuple_GET_SIZE(key) == 2);
    1398             : 
    1399        1085 :         Py_ssize_t len = PySet_GET_SIZE(o);
    1400        1085 :         if (len == 0) {  // empty frozenset should not be re-created.
    1401           1 :             return key;
    1402             :         }
    1403        1084 :         PyObject *tuple = PyTuple_New(len);
    1404        1084 :         if (tuple == NULL) {
    1405           0 :             Py_DECREF(key);
    1406           0 :             return NULL;
    1407             :         }
    1408        1084 :         Py_ssize_t i = 0, pos = 0;
    1409             :         PyObject *item;
    1410             :         Py_hash_t hash;
    1411        7625 :         while (_PySet_NextEntry(o, &pos, &item, &hash)) {
    1412        6541 :             PyObject *k = merge_consts_recursive(const_cache, item);
    1413        6541 :             if (k == NULL) {
    1414           0 :                 Py_DECREF(tuple);
    1415           0 :                 Py_DECREF(key);
    1416           0 :                 return NULL;
    1417             :             }
    1418             :             PyObject *u;
    1419        6541 :             if (PyTuple_CheckExact(k)) {
    1420          79 :                 u = PyTuple_GET_ITEM(k, 1);
    1421          79 :                 Py_INCREF(u);
    1422          79 :                 Py_DECREF(k);
    1423             :             }
    1424             :             else {
    1425        6462 :                 u = k;
    1426             :             }
    1427        6541 :             PyTuple_SET_ITEM(tuple, i, u);  // Steals reference of u.
    1428        6541 :             i++;
    1429             :         }
    1430             : 
    1431             :         // Instead of rewriting o, we create new frozenset and embed in the
    1432             :         // key tuple.  Caller should get merged frozenset from the key tuple.
    1433        1084 :         PyObject *new = PyFrozenSet_New(tuple);
    1434        1084 :         Py_DECREF(tuple);
    1435        1084 :         if (new == NULL) {
    1436           0 :             Py_DECREF(key);
    1437           0 :             return NULL;
    1438             :         }
    1439        1084 :         assert(PyTuple_GET_ITEM(key, 1) == o);
    1440        1084 :         Py_DECREF(o);
    1441        1084 :         PyTuple_SET_ITEM(key, 1, new);
    1442             :     }
    1443             : 
    1444     3829520 :     return key;
    1445             : }
    1446             : 
    1447             : static Py_ssize_t
    1448     3779120 : compiler_add_const(struct compiler *c, PyObject *o)
    1449             : {
    1450     3779120 :     PyObject *key = merge_consts_recursive(c->c_const_cache, o);
    1451     3779120 :     if (key == NULL) {
    1452           0 :         return -1;
    1453             :     }
    1454             : 
    1455     3779120 :     Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
    1456     3779120 :     Py_DECREF(key);
    1457     3779120 :     return arg;
    1458             : }
    1459             : 
    1460             : static int
    1461     3415690 : compiler_addop_load_const(struct compiler *c, PyObject *o)
    1462             : {
    1463     3415690 :     Py_ssize_t arg = compiler_add_const(c, o);
    1464     3415690 :     if (arg < 0)
    1465           0 :         return 0;
    1466     3415690 :     return compiler_addop_i(c, LOAD_CONST, arg, true);
    1467             : }
    1468             : 
    1469             : static int
    1470     2918390 : compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
    1471             :                      PyObject *o)
    1472             : {
    1473     2918390 :     Py_ssize_t arg = compiler_add_o(dict, o);
    1474     2918390 :     if (arg < 0)
    1475           0 :         return 0;
    1476     2918390 :     return compiler_addop_i(c, opcode, arg, true);
    1477             : }
    1478             : 
    1479             : static int
    1480     1492720 : compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
    1481             :                     PyObject *o)
    1482             : {
    1483             :     Py_ssize_t arg;
    1484             : 
    1485     1492720 :     PyObject *mangled = _Py_Mangle(c->u->u_private, o);
    1486     1492720 :     if (!mangled)
    1487           0 :         return 0;
    1488     1492720 :     arg = compiler_add_o(dict, mangled);
    1489     1492720 :     Py_DECREF(mangled);
    1490     1492720 :     if (arg < 0)
    1491           0 :         return 0;
    1492     1492720 :     if (opcode == LOAD_ATTR) {
    1493      677932 :         arg <<= 1;
    1494             :     }
    1495     1492720 :     if (opcode == LOAD_METHOD) {
    1496      533682 :         opcode = LOAD_ATTR;
    1497      533682 :         arg <<= 1;
    1498      533682 :         arg |= 1;
    1499             :     }
    1500     1492720 :     return compiler_addop_i(c, opcode, arg, true);
    1501             : }
    1502             : 
    1503             : /* Add an opcode with an integer argument.
    1504             :    Returns 0 on failure, 1 on success.
    1505             : */
    1506             : static int
    1507    14460800 : compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg, bool line)
    1508             : {
    1509    14460800 :     if (compiler_use_new_implicit_block_if_needed(c) < 0) {
    1510           0 :         return -1;
    1511             :     }
    1512             :     /* oparg value is unsigned, but a signed C int is usually used to store
    1513             :        it in the C code (like Python/ceval.c).
    1514             : 
    1515             :        Limit to 32-bit signed C int (rather than INT_MAX) for portability.
    1516             : 
    1517             :        The argument of a concrete bytecode instruction is limited to 8-bit.
    1518             :        EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
    1519             : 
    1520    14460800 :     int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
    1521             : 
    1522    14460800 :     const struct location *loc = line ? &c->u->u_loc : NULL;
    1523    14460800 :     return basicblock_addop(c->u->u_curblock, opcode, oparg_, NULL, loc);
    1524             : }
    1525             : 
    1526             : static int
    1527     1164750 : compiler_addop_j(struct compiler *c, int opcode, basicblock *target, bool line)
    1528             : {
    1529     1164750 :     if (compiler_use_new_implicit_block_if_needed(c) < 0) {
    1530           0 :         return -1;
    1531             :     }
    1532     1164750 :     const struct location *loc = line ? &c->u->u_loc : NULL;
    1533     1164750 :     assert(target != NULL);
    1534     1164750 :     assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
    1535     1164750 :     return basicblock_addop(c->u->u_curblock, opcode, 0, target, loc);
    1536             : }
    1537             : 
    1538             : #define ADDOP(C, OP) { \
    1539             :     if (!compiler_addop((C), (OP), true)) \
    1540             :         return 0; \
    1541             : }
    1542             : 
    1543             : #define ADDOP_NOLINE(C, OP) { \
    1544             :     if (!compiler_addop((C), (OP), false)) \
    1545             :         return 0; \
    1546             : }
    1547             : 
    1548             : #define ADDOP_IN_SCOPE(C, OP) { \
    1549             :     if (!compiler_addop((C), (OP), true)) { \
    1550             :         compiler_exit_scope(c); \
    1551             :         return 0; \
    1552             :     } \
    1553             : }
    1554             : 
    1555             : #define ADDOP_LOAD_CONST(C, O) { \
    1556             :     if (!compiler_addop_load_const((C), (O))) \
    1557             :         return 0; \
    1558             : }
    1559             : 
    1560             : /* Same as ADDOP_LOAD_CONST, but steals a reference. */
    1561             : #define ADDOP_LOAD_CONST_NEW(C, O) { \
    1562             :     PyObject *__new_const = (O); \
    1563             :     if (__new_const == NULL) { \
    1564             :         return 0; \
    1565             :     } \
    1566             :     if (!compiler_addop_load_const((C), __new_const)) { \
    1567             :         Py_DECREF(__new_const); \
    1568             :         return 0; \
    1569             :     } \
    1570             :     Py_DECREF(__new_const); \
    1571             : }
    1572             : 
    1573             : #define ADDOP_N(C, OP, O, TYPE) { \
    1574             :     assert(!HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
    1575             :     if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
    1576             :         Py_DECREF((O)); \
    1577             :         return 0; \
    1578             :     } \
    1579             :     Py_DECREF((O)); \
    1580             : }
    1581             : 
    1582             : #define ADDOP_NAME(C, OP, O, TYPE) { \
    1583             :     if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
    1584             :         return 0; \
    1585             : }
    1586             : 
    1587             : #define ADDOP_I(C, OP, O) { \
    1588             :     if (!compiler_addop_i((C), (OP), (O), true)) \
    1589             :         return 0; \
    1590             : }
    1591             : 
    1592             : #define ADDOP_I_NOLINE(C, OP, O) { \
    1593             :     if (!compiler_addop_i((C), (OP), (O), false)) \
    1594             :         return 0; \
    1595             : }
    1596             : 
    1597             : #define ADDOP_JUMP(C, OP, O) { \
    1598             :     if (!compiler_addop_j((C), (OP), (O), true)) \
    1599             :         return 0; \
    1600             : }
    1601             : 
    1602             : /* Add a jump with no line number.
    1603             :  * Used for artificial jumps that have no corresponding
    1604             :  * token in the source code. */
    1605             : #define ADDOP_JUMP_NOLINE(C, OP, O) { \
    1606             :     if (!compiler_addop_j((C), (OP), (O), false)) \
    1607             :         return 0; \
    1608             : }
    1609             : 
    1610             : #define ADDOP_COMPARE(C, CMP) { \
    1611             :     if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
    1612             :         return 0; \
    1613             : }
    1614             : 
    1615             : #define ADDOP_BINARY(C, BINOP) \
    1616             :     RETURN_IF_FALSE(addop_binary((C), (BINOP), false))
    1617             : 
    1618             : #define ADDOP_INPLACE(C, BINOP) \
    1619             :     RETURN_IF_FALSE(addop_binary((C), (BINOP), true))
    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 ADD_YIELD_FROM(C, await) \
    1626             :     RETURN_IF_FALSE(compiler_add_yield_from((C), (await)))
    1627             : 
    1628             : #define POP_EXCEPT_AND_RERAISE(C) \
    1629             :     RETURN_IF_FALSE(compiler_pop_except_and_reraise((C)))
    1630             : 
    1631             : #define ADDOP_YIELD(C) \
    1632             :     RETURN_IF_FALSE(addop_yield(C))
    1633             : 
    1634             : #define VISIT(C, TYPE, V) {\
    1635             :     if (!compiler_visit_ ## TYPE((C), (V))) \
    1636             :         return 0; \
    1637             : }
    1638             : 
    1639             : #define VISIT_IN_SCOPE(C, TYPE, V) {\
    1640             :     if (!compiler_visit_ ## TYPE((C), (V))) { \
    1641             :         compiler_exit_scope(c); \
    1642             :         return 0; \
    1643             :     } \
    1644             : }
    1645             : 
    1646             : #define VISIT_SEQ(C, TYPE, SEQ) { \
    1647             :     int _i; \
    1648             :     asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
    1649             :     for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
    1650             :         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
    1651             :         if (!compiler_visit_ ## TYPE((C), elt)) \
    1652             :             return 0; \
    1653             :     } \
    1654             : }
    1655             : 
    1656             : #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
    1657             :     int _i; \
    1658             :     asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
    1659             :     for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
    1660             :         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
    1661             :         if (!compiler_visit_ ## TYPE((C), elt)) { \
    1662             :             compiler_exit_scope(c); \
    1663             :             return 0; \
    1664             :         } \
    1665             :     } \
    1666             : }
    1667             : 
    1668             : #define RETURN_IF_FALSE(X)  \
    1669             :     if (!(X)) {             \
    1670             :         return 0;           \
    1671             :     }
    1672             : 
    1673             : static int
    1674      465532 : compiler_enter_scope(struct compiler *c, identifier name,
    1675             :                      int scope_type, void *key, int lineno)
    1676             : {
    1677             :     struct compiler_unit *u;
    1678             :     basicblock *block;
    1679             : 
    1680      465532 :     u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
    1681             :                                             struct compiler_unit));
    1682      465532 :     if (!u) {
    1683           0 :         PyErr_NoMemory();
    1684           0 :         return 0;
    1685             :     }
    1686      465532 :     u->u_scope_type = scope_type;
    1687      465532 :     u->u_argcount = 0;
    1688      465532 :     u->u_posonlyargcount = 0;
    1689      465532 :     u->u_kwonlyargcount = 0;
    1690      465532 :     u->u_ste = PySymtable_Lookup(c->c_st, key);
    1691      465532 :     if (!u->u_ste) {
    1692           0 :         compiler_unit_free(u);
    1693           0 :         return 0;
    1694             :     }
    1695      465532 :     Py_INCREF(name);
    1696      465532 :     u->u_name = name;
    1697      465532 :     u->u_varnames = list2dict(u->u_ste->ste_varnames);
    1698      465532 :     u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
    1699      465532 :     if (!u->u_varnames || !u->u_cellvars) {
    1700           0 :         compiler_unit_free(u);
    1701           0 :         return 0;
    1702             :     }
    1703      465532 :     if (u->u_ste->ste_needs_class_closure) {
    1704             :         /* Cook up an implicit __class__ cell. */
    1705             :         int res;
    1706        4795 :         assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
    1707        4795 :         assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
    1708        4795 :         res = PyDict_SetItem(u->u_cellvars, &_Py_ID(__class__),
    1709             :                              _PyLong_GetZero());
    1710        4795 :         if (res < 0) {
    1711           0 :             compiler_unit_free(u);
    1712           0 :             return 0;
    1713             :         }
    1714             :     }
    1715             : 
    1716      465532 :     u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
    1717             :                                PyDict_GET_SIZE(u->u_cellvars));
    1718      465532 :     if (!u->u_freevars) {
    1719           0 :         compiler_unit_free(u);
    1720           0 :         return 0;
    1721             :     }
    1722             : 
    1723      465532 :     u->u_blocks = NULL;
    1724      465532 :     u->u_nfblocks = 0;
    1725      465532 :     u->u_firstlineno = lineno;
    1726      465532 :     u->u_loc = LOCATION(lineno, lineno, 0, 0);
    1727      465532 :     u->u_consts = PyDict_New();
    1728      465532 :     if (!u->u_consts) {
    1729           0 :         compiler_unit_free(u);
    1730           0 :         return 0;
    1731             :     }
    1732      465532 :     u->u_names = PyDict_New();
    1733      465532 :     if (!u->u_names) {
    1734           0 :         compiler_unit_free(u);
    1735           0 :         return 0;
    1736             :     }
    1737             : 
    1738      465532 :     u->u_private = NULL;
    1739             : 
    1740             :     /* Push the old compiler_unit on the stack. */
    1741      465532 :     if (c->u) {
    1742      351007 :         PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
    1743      351007 :         if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
    1744           0 :             Py_XDECREF(capsule);
    1745           0 :             compiler_unit_free(u);
    1746           0 :             return 0;
    1747             :         }
    1748      351007 :         Py_DECREF(capsule);
    1749      351007 :         u->u_private = c->u->u_private;
    1750      351007 :         Py_XINCREF(u->u_private);
    1751             :     }
    1752      465532 :     c->u = u;
    1753             : 
    1754      465532 :     c->c_nestlevel++;
    1755             : 
    1756      465532 :     block = compiler_new_block(c);
    1757      465532 :     if (block == NULL)
    1758           0 :         return 0;
    1759      465532 :     c->u->u_curblock = block;
    1760             : 
    1761      465532 :     if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
    1762      114525 :         c->u->u_loc.lineno = 0;
    1763             :     }
    1764             :     else {
    1765      351007 :         if (!compiler_set_qualname(c))
    1766           0 :             return 0;
    1767             :     }
    1768      465532 :     ADDOP_I(c, RESUME, 0);
    1769             : 
    1770      465532 :     if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
    1771      114525 :         c->u->u_loc.lineno = -1;
    1772             :     }
    1773      465532 :     return 1;
    1774             : }
    1775             : 
    1776             : static void
    1777      465532 : compiler_exit_scope(struct compiler *c)
    1778             : {
    1779             :     // Don't call PySequence_DelItem() with an exception raised
    1780             :     PyObject *exc_type, *exc_val, *exc_tb;
    1781      465532 :     PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
    1782             : 
    1783      465532 :     c->c_nestlevel--;
    1784      465532 :     compiler_unit_free(c->u);
    1785             :     /* Restore c->u to the parent unit. */
    1786      465532 :     Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
    1787      465532 :     if (n >= 0) {
    1788      351007 :         PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
    1789      351007 :         c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
    1790      351007 :         assert(c->u);
    1791             :         /* we are deleting from a list so this really shouldn't fail */
    1792      351007 :         if (PySequence_DelItem(c->c_stack, n) < 0) {
    1793           0 :             _PyErr_WriteUnraisableMsg("on removing the last compiler "
    1794             :                                       "stack item", NULL);
    1795             :         }
    1796      351007 :         compiler_unit_check(c->u);
    1797             :     }
    1798             :     else {
    1799      114525 :         c->u = NULL;
    1800             :     }
    1801             : 
    1802      465532 :     PyErr_Restore(exc_type, exc_val, exc_tb);
    1803      465532 : }
    1804             : 
    1805             : /* Search if variable annotations are present statically in a block. */
    1806             : 
    1807             : static int
    1808      518491 : find_ann(asdl_stmt_seq *stmts)
    1809             : {
    1810      518491 :     int i, j, res = 0;
    1811             :     stmt_ty st;
    1812             : 
    1813     1436900 :     for (i = 0; i < asdl_seq_LEN(stmts); i++) {
    1814      920622 :         st = (stmt_ty)asdl_seq_GET(stmts, i);
    1815      920622 :         switch (st->kind) {
    1816        2188 :         case AnnAssign_kind:
    1817        2188 :             return 1;
    1818        1348 :         case For_kind:
    1819        2696 :             res = find_ann(st->v.For.body) ||
    1820        1348 :                   find_ann(st->v.For.orelse);
    1821        1348 :             break;
    1822           5 :         case AsyncFor_kind:
    1823          10 :             res = find_ann(st->v.AsyncFor.body) ||
    1824           5 :                   find_ann(st->v.AsyncFor.orelse);
    1825           5 :             break;
    1826         140 :         case While_kind:
    1827         280 :             res = find_ann(st->v.While.body) ||
    1828         140 :                   find_ann(st->v.While.orelse);
    1829         140 :             break;
    1830      210463 :         case If_kind:
    1831      420914 :             res = find_ann(st->v.If.body) ||
    1832      210451 :                   find_ann(st->v.If.orelse);
    1833      210463 :             break;
    1834         487 :         case With_kind:
    1835         487 :             res = find_ann(st->v.With.body);
    1836         487 :             break;
    1837           5 :         case AsyncWith_kind:
    1838           5 :             res = find_ann(st->v.AsyncWith.body);
    1839           5 :             break;
    1840        3551 :         case Try_kind:
    1841        7119 :             for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
    1842        3568 :                 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
    1843             :                     st->v.Try.handlers, j);
    1844        3568 :                 if (find_ann(handler->v.ExceptHandler.body)) {
    1845           0 :                     return 1;
    1846             :                 }
    1847             :             }
    1848        7102 :             res = find_ann(st->v.Try.body) ||
    1849        7102 :                   find_ann(st->v.Try.finalbody) ||
    1850        3551 :                   find_ann(st->v.Try.orelse);
    1851        3551 :             break;
    1852           8 :         case TryStar_kind:
    1853          16 :             for (j = 0; j < asdl_seq_LEN(st->v.TryStar.handlers); j++) {
    1854           8 :                 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
    1855             :                     st->v.TryStar.handlers, j);
    1856           8 :                 if (find_ann(handler->v.ExceptHandler.body)) {
    1857           0 :                     return 1;
    1858             :                 }
    1859             :             }
    1860          16 :             res = find_ann(st->v.TryStar.body) ||
    1861          16 :                   find_ann(st->v.TryStar.finalbody) ||
    1862           8 :                   find_ann(st->v.TryStar.orelse);
    1863           8 :             break;
    1864      702427 :         default:
    1865      702427 :             res = 0;
    1866             :         }
    1867      918434 :         if (res) {
    1868          25 :             break;
    1869             :         }
    1870             :     }
    1871      516303 :     return res;
    1872             : }
    1873             : 
    1874             : /*
    1875             :  * Frame block handling functions
    1876             :  */
    1877             : 
    1878             : static int
    1879      197983 : compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
    1880             :                      basicblock *exit, void *datum)
    1881             : {
    1882             :     struct fblockinfo *f;
    1883      197983 :     if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
    1884           2 :         return compiler_error(c, "too many statically nested blocks");
    1885             :     }
    1886      197981 :     f = &c->u->u_fblock[c->u->u_nfblocks++];
    1887      197981 :     f->fb_type = t;
    1888      197981 :     f->fb_block = b;
    1889      197981 :     f->fb_exit = exit;
    1890      197981 :     f->fb_datum = datum;
    1891      197981 :     return 1;
    1892             : }
    1893             : 
    1894             : static void
    1895      197907 : compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
    1896             : {
    1897      197907 :     struct compiler_unit *u = c->u;
    1898      197907 :     assert(u->u_nfblocks > 0);
    1899      197907 :     u->u_nfblocks--;
    1900      197907 :     assert(u->u_fblock[u->u_nfblocks].fb_type == t);
    1901      197907 :     assert(u->u_fblock[u->u_nfblocks].fb_block == b);
    1902      197907 : }
    1903             : 
    1904             : static int
    1905       23673 : compiler_call_exit_with_nones(struct compiler *c) {
    1906       23673 :     ADDOP_LOAD_CONST(c, Py_None);
    1907       23673 :     ADDOP_LOAD_CONST(c, Py_None);
    1908       23673 :     ADDOP_LOAD_CONST(c, Py_None);
    1909       23673 :     ADDOP_I(c, CALL, 2);
    1910       23673 :     return 1;
    1911             : }
    1912             : 
    1913             : static int
    1914        4577 : compiler_add_yield_from(struct compiler *c, int await)
    1915             : {
    1916             :     basicblock *start, *resume, *exit;
    1917        4577 :     start = compiler_new_block(c);
    1918        4577 :     resume = compiler_new_block(c);
    1919        4577 :     exit = compiler_new_block(c);
    1920        4577 :     if (start == NULL || resume == NULL || exit == NULL) {
    1921           0 :         return 0;
    1922             :     }
    1923        4577 :     compiler_use_next_block(c, start);
    1924        4577 :     ADDOP_JUMP(c, SEND, exit);
    1925        4577 :     compiler_use_next_block(c, resume);
    1926        4577 :     ADDOP_I(c, YIELD_VALUE, 0);
    1927        4577 :     ADDOP_I(c, RESUME, await ? 3 : 2);
    1928        4577 :     ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start);
    1929        4577 :     compiler_use_next_block(c, exit);
    1930        4577 :     return 1;
    1931             : }
    1932             : 
    1933             : static int
    1934       62084 : compiler_pop_except_and_reraise(struct compiler *c)
    1935             : {
    1936             :     /* Stack contents
    1937             :      * [exc_info, lasti, exc]            COPY        3
    1938             :      * [exc_info, lasti, exc, exc_info]  POP_EXCEPT
    1939             :      * [exc_info, lasti, exc]            RERAISE      1
    1940             :      * (exception_unwind clears the stack)
    1941             :      */
    1942             : 
    1943       62084 :     ADDOP_I(c, COPY, 3);
    1944       62084 :     ADDOP(c, POP_EXCEPT);
    1945       62084 :     ADDOP_I(c, RERAISE, 1);
    1946       62084 :     return 1;
    1947             : }
    1948             : 
    1949             : /* Unwind a frame block.  If preserve_tos is true, the TOS before
    1950             :  * popping the blocks will be restored afterwards, unless another
    1951             :  * return, break or continue is found. In which case, the TOS will
    1952             :  * be popped.
    1953             :  */
    1954             : static int
    1955       44850 : compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
    1956             :                        int preserve_tos)
    1957             : {
    1958       44850 :     switch (info->fb_type) {
    1959       17165 :         case WHILE_LOOP:
    1960             :         case EXCEPTION_HANDLER:
    1961             :         case EXCEPTION_GROUP_HANDLER:
    1962             :         case ASYNC_COMPREHENSION_GENERATOR:
    1963       17165 :             return 1;
    1964             : 
    1965        7525 :         case FOR_LOOP:
    1966             :             /* Pop the iterator */
    1967        7525 :             if (preserve_tos) {
    1968        2475 :                 ADDOP_I(c, SWAP, 2);
    1969             :             }
    1970        7525 :             ADDOP(c, POP_TOP);
    1971        7525 :             return 1;
    1972             : 
    1973        6442 :         case TRY_EXCEPT:
    1974        6442 :             ADDOP(c, POP_BLOCK);
    1975        6442 :             return 1;
    1976             : 
    1977        1533 :         case FINALLY_TRY:
    1978             :             /* This POP_BLOCK gets the line number of the unwinding statement */
    1979        1533 :             ADDOP(c, POP_BLOCK);
    1980        1533 :             if (preserve_tos) {
    1981        1267 :                 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
    1982           0 :                     return 0;
    1983             :                 }
    1984             :             }
    1985             :             /* Emit the finally block */
    1986        3286 :             VISIT_SEQ(c, stmt, info->fb_datum);
    1987        1533 :             if (preserve_tos) {
    1988        1267 :                 compiler_pop_fblock(c, POP_VALUE, NULL);
    1989             :             }
    1990             :             /* The finally block should appear to execute after the
    1991             :              * statement causing the unwinding, so make the unwinding
    1992             :              * instruction artificial */
    1993        1533 :             UNSET_LOC(c);
    1994        1533 :             return 1;
    1995             : 
    1996         116 :         case FINALLY_END:
    1997         116 :             if (preserve_tos) {
    1998          17 :                 ADDOP_I(c, SWAP, 2);
    1999             :             }
    2000         116 :             ADDOP(c, POP_TOP); /* exc_value */
    2001         116 :             if (preserve_tos) {
    2002          17 :                 ADDOP_I(c, SWAP, 2);
    2003             :             }
    2004         116 :             ADDOP(c, POP_BLOCK);
    2005         116 :             ADDOP(c, POP_EXCEPT);
    2006         116 :             return 1;
    2007             : 
    2008        2415 :         case WITH:
    2009             :         case ASYNC_WITH:
    2010        2415 :             SET_LOC(c, (stmt_ty)info->fb_datum);
    2011        2415 :             ADDOP(c, POP_BLOCK);
    2012        2415 :             if (preserve_tos) {
    2013        1787 :                 ADDOP_I(c, SWAP, 2);
    2014             :             }
    2015        2415 :             if(!compiler_call_exit_with_nones(c)) {
    2016           0 :                 return 0;
    2017             :             }
    2018        2415 :             if (info->fb_type == ASYNC_WITH) {
    2019          53 :                 ADDOP_I(c, GET_AWAITABLE, 2);
    2020          53 :                 ADDOP_LOAD_CONST(c, Py_None);
    2021          53 :                 ADD_YIELD_FROM(c, 1);
    2022             :             }
    2023        2415 :             ADDOP(c, POP_TOP);
    2024             :             /* The exit block should appear to execute after the
    2025             :              * statement causing the unwinding, so make the unwinding
    2026             :              * instruction artificial */
    2027        2415 :             UNSET_LOC(c);
    2028        2415 :             return 1;
    2029             : 
    2030        9646 :         case HANDLER_CLEANUP:
    2031        9646 :             if (info->fb_datum) {
    2032        1581 :                 ADDOP(c, POP_BLOCK);
    2033             :             }
    2034        9646 :             if (preserve_tos) {
    2035        2834 :                 ADDOP_I(c, SWAP, 2);
    2036             :             }
    2037        9646 :             ADDOP(c, POP_BLOCK);
    2038        9646 :             ADDOP(c, POP_EXCEPT);
    2039        9646 :             if (info->fb_datum) {
    2040        1581 :                 ADDOP_LOAD_CONST(c, Py_None);
    2041        1581 :                 compiler_nameop(c, info->fb_datum, Store);
    2042        1581 :                 compiler_nameop(c, info->fb_datum, Del);
    2043             :             }
    2044        9646 :             return 1;
    2045             : 
    2046           8 :         case POP_VALUE:
    2047           8 :             if (preserve_tos) {
    2048           0 :                 ADDOP_I(c, SWAP, 2);
    2049             :             }
    2050           8 :             ADDOP(c, POP_TOP);
    2051           8 :             return 1;
    2052             :     }
    2053           0 :     Py_UNREACHABLE();
    2054             : }
    2055             : 
    2056             : /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
    2057             : static int
    2058      264779 : compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
    2059      264779 :     if (c->u->u_nfblocks == 0) {
    2060      212398 :         return 1;
    2061             :     }
    2062       52381 :     struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
    2063       52381 :     if (top->fb_type == EXCEPTION_GROUP_HANDLER) {
    2064           8 :         return compiler_error(
    2065             :             c, "'break', 'continue' and 'return' cannot appear in an except* block");
    2066             :     }
    2067       52373 :     if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
    2068       16084 :         *loop = top;
    2069       16084 :         return 1;
    2070             :     }
    2071       36289 :     struct fblockinfo copy = *top;
    2072       36289 :     c->u->u_nfblocks--;
    2073       36289 :     if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
    2074           0 :         return 0;
    2075             :     }
    2076       36289 :     if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
    2077           8 :         return 0;
    2078             :     }
    2079       36281 :     c->u->u_fblock[c->u->u_nfblocks] = copy;
    2080       36281 :     c->u->u_nfblocks++;
    2081       36281 :     return 1;
    2082             : }
    2083             : 
    2084             : /* Compile a sequence of statements, checking for a docstring
    2085             :    and for annotations. */
    2086             : 
    2087             : static int
    2088       75715 : compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
    2089             : {
    2090       75715 :     int i = 0;
    2091             :     stmt_ty st;
    2092             :     PyObject *docstring;
    2093             : 
    2094             :     /* Set current line number to the line number of first statement.
    2095             :        This way line number for SETUP_ANNOTATIONS will always
    2096             :        coincide with the line number of first "real" statement in module.
    2097             :        If body is empty, then lineno will be set later in assemble. */
    2098       75715 :     if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
    2099       37296 :         st = (stmt_ty)asdl_seq_GET(stmts, 0);
    2100       37296 :         SET_LOC(c, st);
    2101             :     }
    2102             :     /* Every annotated class and module should have __annotations__. */
    2103       75715 :     if (find_ann(stmts)) {
    2104        2187 :         ADDOP(c, SETUP_ANNOTATIONS);
    2105             :     }
    2106       75715 :     if (!asdl_seq_LEN(stmts))
    2107         678 :         return 1;
    2108             :     /* if not -OO mode, set docstring */
    2109       75037 :     if (c->c_optimize < 2) {
    2110       73746 :         docstring = _PyAST_GetDocString(stmts);
    2111       73746 :         if (docstring) {
    2112       21416 :             i = 1;
    2113       21416 :             st = (stmt_ty)asdl_seq_GET(stmts, 0);
    2114       21416 :             assert(st->kind == Expr_kind);
    2115       21416 :             VISIT(c, expr, st->v.Expr.value);
    2116       21416 :             UNSET_LOC(c);
    2117       21416 :             if (!compiler_nameop(c, &_Py_ID(__doc__), Store))
    2118           0 :                 return 0;
    2119             :         }
    2120             :     }
    2121      737824 :     for (; i < asdl_seq_LEN(stmts); i++)
    2122      663042 :         VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
    2123       74782 :     return 1;
    2124             : }
    2125             : 
    2126             : static PyCodeObject *
    2127      114525 : compiler_mod(struct compiler *c, mod_ty mod)
    2128             : {
    2129             :     PyCodeObject *co;
    2130      114525 :     int addNone = 1;
    2131             :     _Py_DECLARE_STR(anon_module, "<module>");
    2132      114525 :     if (!compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
    2133             :                               mod, 1)) {
    2134           0 :         return NULL;
    2135             :     }
    2136      114525 :     c->u->u_loc.lineno = 1;
    2137      114525 :     switch (mod->kind) {
    2138       37974 :     case Module_kind:
    2139       37974 :         if (!compiler_body(c, mod->v.Module.body)) {
    2140         238 :             compiler_exit_scope(c);
    2141         238 :             return 0;
    2142             :         }
    2143       37736 :         break;
    2144        4131 :     case Interactive_kind:
    2145        4131 :         if (find_ann(mod->v.Interactive.body)) {
    2146           1 :             ADDOP(c, SETUP_ANNOTATIONS);
    2147             :         }
    2148        4131 :         c->c_interactive = 1;
    2149        8273 :         VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
    2150        4081 :         break;
    2151       72420 :     case Expression_kind:
    2152       72420 :         VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
    2153       72419 :         addNone = 0;
    2154       72419 :         break;
    2155           0 :     default:
    2156           0 :         PyErr_Format(PyExc_SystemError,
    2157             :                      "module kind %d should not be possible",
    2158           0 :                      mod->kind);
    2159           0 :         return 0;
    2160             :     }
    2161      114236 :     co = assemble(c, addNone);
    2162      114236 :     compiler_exit_scope(c);
    2163      114236 :     return co;
    2164             : }
    2165             : 
    2166             : /* The test for LOCAL must come before the test for FREE in order to
    2167             :    handle classes where name is both local and free.  The local var is
    2168             :    a method and the free var is a free var referenced within a method.
    2169             : */
    2170             : 
    2171             : static int
    2172       35036 : get_ref_type(struct compiler *c, PyObject *name)
    2173             : {
    2174             :     int scope;
    2175       43464 :     if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
    2176        8428 :         _PyUnicode_EqualToASCIIString(name, "__class__"))
    2177        6777 :         return CELL;
    2178       28259 :     scope = _PyST_GetScope(c->u->u_ste, name);
    2179       28259 :     if (scope == 0) {
    2180           0 :         PyErr_Format(PyExc_SystemError,
    2181             :                      "_PyST_GetScope(name=%R) failed: "
    2182             :                      "unknown scope in unit %S (%R); "
    2183             :                      "symbols: %R; locals: %R; globals: %R",
    2184             :                      name,
    2185           0 :                      c->u->u_name, c->u->u_ste->ste_id,
    2186           0 :                      c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
    2187           0 :         return -1;
    2188             :     }
    2189       28259 :     return scope;
    2190             : }
    2191             : 
    2192             : static int
    2193       39831 : compiler_lookup_arg(PyObject *dict, PyObject *name)
    2194             : {
    2195             :     PyObject *v;
    2196       39831 :     v = PyDict_GetItemWithError(dict, name);
    2197       39831 :     if (v == NULL)
    2198           0 :         return -1;
    2199       39831 :     return PyLong_AS_LONG(v);
    2200             : }
    2201             : 
    2202             : static int
    2203      350877 : compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
    2204             :                       PyObject *qualname)
    2205             : {
    2206      350877 :     if (qualname == NULL)
    2207       37724 :         qualname = co->co_name;
    2208             : 
    2209      350877 :     if (co->co_nfreevars) {
    2210       24461 :         int i = co->co_nlocals + co->co_nplaincellvars;
    2211       59497 :         for (; i < co->co_nlocalsplus; ++i) {
    2212             :             /* Bypass com_addop_varname because it will generate
    2213             :                LOAD_DEREF but LOAD_CLOSURE is needed.
    2214             :             */
    2215       35036 :             PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
    2216             : 
    2217             :             /* Special case: If a class contains a method with a
    2218             :                free variable that has the same name as a method,
    2219             :                the name will be considered free *and* local in the
    2220             :                class.  It should be handled by the closure, as
    2221             :                well as by the normal name lookup logic.
    2222             :             */
    2223       35036 :             int reftype = get_ref_type(c, name);
    2224       35036 :             if (reftype == -1) {
    2225           0 :                 return 0;
    2226             :             }
    2227             :             int arg;
    2228       35036 :             if (reftype == CELL) {
    2229       32763 :                 arg = compiler_lookup_arg(c->u->u_cellvars, name);
    2230             :             }
    2231             :             else {
    2232        2273 :                 arg = compiler_lookup_arg(c->u->u_freevars, name);
    2233             :             }
    2234       35036 :             if (arg == -1) {
    2235           0 :                 PyObject *freevars = _PyCode_GetFreevars(co);
    2236           0 :                 if (freevars == NULL) {
    2237           0 :                     PyErr_Clear();
    2238             :                 }
    2239           0 :                 PyErr_Format(PyExc_SystemError,
    2240             :                     "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
    2241             :                     "freevars of code %S: %R",
    2242             :                     name,
    2243             :                     reftype,
    2244           0 :                     c->u->u_name,
    2245             :                     co->co_name,
    2246             :                     freevars);
    2247           0 :                 Py_DECREF(freevars);
    2248           0 :                 return 0;
    2249             :             }
    2250       35036 :             ADDOP_I(c, LOAD_CLOSURE, arg);
    2251             :         }
    2252       24461 :         flags |= 0x08;
    2253       24461 :         ADDOP_I(c, BUILD_TUPLE, co->co_nfreevars);
    2254             :     }
    2255      350877 :     ADDOP_LOAD_CONST(c, (PyObject*)co);
    2256      350877 :     ADDOP_I(c, MAKE_FUNCTION, flags);
    2257      350877 :     return 1;
    2258             : }
    2259             : 
    2260             : static int
    2261      298407 : compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
    2262             : {
    2263             :     int i;
    2264             : 
    2265      298407 :     if (!decos)
    2266      265842 :         return 1;
    2267             : 
    2268       58613 :     for (i = 0; i < asdl_seq_LEN(decos); i++) {
    2269       26048 :         VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
    2270             :     }
    2271       32565 :     return 1;
    2272             : }
    2273             : 
    2274             : static int
    2275      298309 : compiler_apply_decorators(struct compiler *c, asdl_expr_seq* decos)
    2276             : {
    2277      298309 :     if (!decos)
    2278      265744 :         return 1;
    2279             : 
    2280       32565 :     struct location old_loc = c->u->u_loc;
    2281       58613 :     for (Py_ssize_t i = asdl_seq_LEN(decos) - 1; i > -1; i--) {
    2282       26048 :         SET_LOC(c, (expr_ty)asdl_seq_GET(decos, i));
    2283       26048 :         ADDOP_I(c, CALL, 0);
    2284             :     }
    2285       32565 :     c->u->u_loc = old_loc;
    2286       32565 :     return 1;
    2287             : }
    2288             : 
    2289             : static int
    2290      295425 : compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
    2291             :                               asdl_expr_seq *kw_defaults)
    2292             : {
    2293             :     /* Push a dict of keyword-only default values.
    2294             : 
    2295             :        Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
    2296             :        */
    2297             :     int i;
    2298      295425 :     PyObject *keys = NULL;
    2299             : 
    2300      311888 :     for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
    2301       16464 :         arg_ty arg = asdl_seq_GET(kwonlyargs, i);
    2302       16464 :         expr_ty default_ = asdl_seq_GET(kw_defaults, i);
    2303       16464 :         if (default_) {
    2304       13301 :             PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
    2305       13301 :             if (!mangled) {
    2306           0 :                 goto error;
    2307             :             }
    2308       13301 :             if (keys == NULL) {
    2309        4742 :                 keys = PyList_New(1);
    2310        4742 :                 if (keys == NULL) {
    2311           0 :                     Py_DECREF(mangled);
    2312           0 :                     return 0;
    2313             :                 }
    2314        4742 :                 PyList_SET_ITEM(keys, 0, mangled);
    2315             :             }
    2316             :             else {
    2317        8559 :                 int res = PyList_Append(keys, mangled);
    2318        8559 :                 Py_DECREF(mangled);
    2319        8559 :                 if (res == -1) {
    2320           0 :                     goto error;
    2321             :                 }
    2322             :             }
    2323       13301 :             if (!compiler_visit_expr(c, default_)) {
    2324           1 :                 goto error;
    2325             :             }
    2326             :         }
    2327             :     }
    2328      295424 :     if (keys != NULL) {
    2329        4741 :         Py_ssize_t default_count = PyList_GET_SIZE(keys);
    2330        4741 :         PyObject *keys_tuple = PyList_AsTuple(keys);
    2331        4741 :         Py_DECREF(keys);
    2332        4741 :         ADDOP_LOAD_CONST_NEW(c, keys_tuple);
    2333        4741 :         ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
    2334        4741 :         assert(default_count > 0);
    2335        4741 :         return 1;
    2336             :     }
    2337             :     else {
    2338      290683 :         return -1;
    2339             :     }
    2340             : 
    2341           1 : error:
    2342           1 :     Py_XDECREF(keys);
    2343           1 :     return 0;
    2344             : }
    2345             : 
    2346             : static int
    2347        3493 : compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
    2348             : {
    2349        3493 :     ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
    2350        3493 :     return 1;
    2351             : }
    2352             : 
    2353             : static int
    2354      729303 : compiler_visit_argannotation(struct compiler *c, identifier id,
    2355             :     expr_ty annotation, Py_ssize_t *annotations_len)
    2356             : {
    2357      729303 :     if (!annotation) {
    2358      645916 :         return 1;
    2359             :     }
    2360             : 
    2361       83387 :     PyObject *mangled = _Py_Mangle(c->u->u_private, id);
    2362       83387 :     if (!mangled) {
    2363           0 :         return 0;
    2364             :     }
    2365       83387 :     ADDOP_LOAD_CONST(c, mangled);
    2366       83387 :     Py_DECREF(mangled);
    2367             : 
    2368       83387 :     if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
    2369        2936 :         VISIT(c, annexpr, annotation);
    2370             :     }
    2371             :     else {
    2372       80451 :         if (annotation->kind == Starred_kind) {
    2373             :             // *args: *Ts (where Ts is a TypeVarTuple).
    2374             :             // Do [annotation_value] = [*Ts].
    2375             :             // (Note that in theory we could end up here even for an argument
    2376             :             // other than *args, but in practice the grammar doesn't allow it.)
    2377          10 :             VISIT(c, expr, annotation->v.Starred.value);
    2378          10 :             ADDOP_I(c, UNPACK_SEQUENCE, (Py_ssize_t) 1);
    2379             :         }
    2380             :         else {
    2381       80441 :             VISIT(c, expr, annotation);
    2382             :         }
    2383             :     }
    2384       83382 :     *annotations_len += 2;
    2385       83382 :     return 1;
    2386             : }
    2387             : 
    2388             : static int
    2389      781979 : compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
    2390             :                               Py_ssize_t *annotations_len)
    2391             : {
    2392             :     int i;
    2393     1249300 :     for (i = 0; i < asdl_seq_LEN(args); i++) {
    2394      467329 :         arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
    2395      467329 :         if (!compiler_visit_argannotation(
    2396             :                         c,
    2397             :                         arg->arg,
    2398             :                         arg->annotation,
    2399             :                         annotations_len))
    2400           3 :             return 0;
    2401             :     }
    2402      781976 :     return 1;
    2403             : }
    2404             : 
    2405             : static int
    2406      260662 : compiler_visit_annotations(struct compiler *c, arguments_ty args,
    2407             :                            expr_ty returns)
    2408             : {
    2409             :     /* Push arg annotation names and values.
    2410             :        The expressions are evaluated out-of-order wrt the source code.
    2411             : 
    2412             :        Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
    2413             :        */
    2414      260662 :     Py_ssize_t annotations_len = 0;
    2415             : 
    2416      260662 :     if (!compiler_visit_argannotations(c, args->args, &annotations_len))
    2417           3 :         return 0;
    2418      260659 :     if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
    2419           0 :         return 0;
    2420      261436 :     if (args->vararg && args->vararg->annotation &&
    2421         777 :         !compiler_visit_argannotation(c, args->vararg->arg,
    2422         777 :                                      args->vararg->annotation, &annotations_len))
    2423           1 :         return 0;
    2424      260658 :     if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
    2425           0 :         return 0;
    2426      261198 :     if (args->kwarg && args->kwarg->annotation &&
    2427         540 :         !compiler_visit_argannotation(c, args->kwarg->arg,
    2428         540 :                                      args->kwarg->annotation, &annotations_len))
    2429           1 :         return 0;
    2430             : 
    2431      260657 :     if (!compiler_visit_argannotation(c, &_Py_ID(return), returns,
    2432             :                                       &annotations_len)) {
    2433           0 :         return 0;
    2434             :     }
    2435             : 
    2436      260657 :     if (annotations_len) {
    2437       34066 :         ADDOP_I(c, BUILD_TUPLE, annotations_len);
    2438       34066 :         return 1;
    2439             :     }
    2440             : 
    2441      226591 :     return -1;
    2442             : }
    2443             : 
    2444             : static int
    2445       35291 : compiler_visit_defaults(struct compiler *c, arguments_ty args)
    2446             : {
    2447      101187 :     VISIT_SEQ(c, expr, args->defaults);
    2448       35288 :     ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
    2449       35288 :     return 1;
    2450             : }
    2451             : 
    2452             : static Py_ssize_t
    2453      295428 : compiler_default_arguments(struct compiler *c, arguments_ty args)
    2454             : {
    2455      295428 :     Py_ssize_t funcflags = 0;
    2456      295428 :     if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
    2457       35291 :         if (!compiler_visit_defaults(c, args))
    2458           3 :             return -1;
    2459       35288 :         funcflags |= 0x01;
    2460             :     }
    2461      295425 :     if (args->kwonlyargs) {
    2462      295425 :         int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
    2463             :                                                 args->kw_defaults);
    2464      295425 :         if (res == 0) {
    2465           1 :             return -1;
    2466             :         }
    2467      295424 :         else if (res > 0) {
    2468        4741 :             funcflags |= 0x02;
    2469             :         }
    2470             :     }
    2471      295424 :     return funcflags;
    2472             : }
    2473             : 
    2474             : static int
    2475     6306690 : forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
    2476             : {
    2477             : 
    2478     6306690 :     if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
    2479          15 :         compiler_error(c, "cannot assign to __debug__");
    2480          15 :         return 1;
    2481             :     }
    2482     6306680 :     if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
    2483           1 :         compiler_error(c, "cannot delete __debug__");
    2484           1 :         return 1;
    2485             :     }
    2486     6306680 :     return 0;
    2487             : }
    2488             : 
    2489             : static int
    2490     1225770 : compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
    2491             : {
    2492     1225770 :     if (arg != NULL) {
    2493      682353 :         if (forbidden_name(c, arg->arg, Store))
    2494           5 :             return 0;
    2495             :     }
    2496     1225760 :     return 1;
    2497             : }
    2498             : 
    2499             : static int
    2500      886296 : compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
    2501             : {
    2502      886296 :     if (args != NULL) {
    2503     1521200 :         for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
    2504      634911 :             if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
    2505           4 :                 return 0;
    2506             :         }
    2507             :     }
    2508      886292 :     return 1;
    2509             : }
    2510             : 
    2511             : static int
    2512      295433 : compiler_check_debug_args(struct compiler *c, arguments_ty args)
    2513             : {
    2514      295433 :     if (!compiler_check_debug_args_seq(c, args->posonlyargs))
    2515           0 :         return 0;
    2516      295433 :     if (!compiler_check_debug_args_seq(c, args->args))
    2517           3 :         return 0;
    2518      295430 :     if (!compiler_check_debug_one_arg(c, args->vararg))
    2519           0 :         return 0;
    2520      295430 :     if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
    2521           1 :         return 0;
    2522      295429 :     if (!compiler_check_debug_one_arg(c, args->kwarg))
    2523           1 :         return 0;
    2524      295428 :     return 1;
    2525             : }
    2526             : 
    2527             : static int
    2528      260668 : compiler_function(struct compiler *c, stmt_ty s, int is_async)
    2529             : {
    2530             :     PyCodeObject *co;
    2531      260668 :     PyObject *qualname, *docstring = NULL;
    2532             :     arguments_ty args;
    2533             :     expr_ty returns;
    2534             :     identifier name;
    2535             :     asdl_expr_seq* decos;
    2536             :     asdl_stmt_seq *body;
    2537             :     Py_ssize_t i, funcflags;
    2538             :     int annotations;
    2539             :     int scope_type;
    2540             :     int firstlineno;
    2541             : 
    2542      260668 :     if (is_async) {
    2543        2334 :         assert(s->kind == AsyncFunctionDef_kind);
    2544             : 
    2545        2334 :         args = s->v.AsyncFunctionDef.args;
    2546        2334 :         returns = s->v.AsyncFunctionDef.returns;
    2547        2334 :         decos = s->v.AsyncFunctionDef.decorator_list;
    2548        2334 :         name = s->v.AsyncFunctionDef.name;
    2549        2334 :         body = s->v.AsyncFunctionDef.body;
    2550             : 
    2551        2334 :         scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
    2552             :     } else {
    2553      258334 :         assert(s->kind == FunctionDef_kind);
    2554             : 
    2555      258334 :         args = s->v.FunctionDef.args;
    2556      258334 :         returns = s->v.FunctionDef.returns;
    2557      258334 :         decos = s->v.FunctionDef.decorator_list;
    2558      258334 :         name = s->v.FunctionDef.name;
    2559      258334 :         body = s->v.FunctionDef.body;
    2560             : 
    2561      258334 :         scope_type = COMPILER_SCOPE_FUNCTION;
    2562             :     }
    2563             : 
    2564      260668 :     if (!compiler_check_debug_args(c, args))
    2565           2 :         return 0;
    2566             : 
    2567      260666 :     if (!compiler_decorators(c, decos))
    2568           0 :         return 0;
    2569             : 
    2570      260666 :     firstlineno = s->lineno;
    2571      260666 :     if (asdl_seq_LEN(decos)) {
    2572       23450 :         firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
    2573             :     }
    2574             : 
    2575      260666 :     funcflags = compiler_default_arguments(c, args);
    2576      260666 :     if (funcflags == -1) {
    2577           4 :         return 0;
    2578             :     }
    2579             : 
    2580      260662 :     annotations = compiler_visit_annotations(c, args, returns);
    2581      260662 :     if (annotations == 0) {
    2582           5 :         return 0;
    2583             :     }
    2584      260657 :     else if (annotations > 0) {
    2585       34066 :         funcflags |= 0x04;
    2586             :     }
    2587             : 
    2588      260657 :     if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
    2589           0 :         return 0;
    2590             :     }
    2591             : 
    2592             :     /* if not -OO mode, add docstring */
    2593      260657 :     if (c->c_optimize < 2) {
    2594      252246 :         docstring = _PyAST_GetDocString(body);
    2595             :     }
    2596      260657 :     if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
    2597           0 :         compiler_exit_scope(c);
    2598           0 :         return 0;
    2599             :     }
    2600             : 
    2601      260657 :     c->u->u_argcount = asdl_seq_LEN(args->args);
    2602      260657 :     c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
    2603      260657 :     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
    2604     1078870 :     for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
    2605      818285 :         VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
    2606             :     }
    2607      260586 :     co = assemble(c, 1);
    2608      260586 :     qualname = c->u->u_qualname;
    2609      260586 :     Py_INCREF(qualname);
    2610      260586 :     compiler_exit_scope(c);
    2611      260586 :     if (co == NULL) {
    2612           0 :         Py_XDECREF(qualname);
    2613           0 :         Py_XDECREF(co);
    2614           0 :         return 0;
    2615             :     }
    2616             : 
    2617      260586 :     if (!compiler_make_closure(c, co, funcflags, qualname)) {
    2618           0 :         Py_DECREF(qualname);
    2619           0 :         Py_DECREF(co);
    2620           0 :         return 0;
    2621             :     }
    2622      260586 :     Py_DECREF(qualname);
    2623      260586 :     Py_DECREF(co);
    2624             : 
    2625      260586 :     if (!compiler_apply_decorators(c, decos))
    2626           0 :         return 0;
    2627      260586 :     return compiler_nameop(c, name, Store);
    2628             : }
    2629             : 
    2630             : static int
    2631       37741 : compiler_class(struct compiler *c, stmt_ty s)
    2632             : {
    2633             :     PyCodeObject *co;
    2634             :     int i, firstlineno;
    2635       37741 :     asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
    2636             : 
    2637       37741 :     if (!compiler_decorators(c, decos))
    2638           0 :         return 0;
    2639             : 
    2640       37741 :     firstlineno = s->lineno;
    2641       37741 :     if (asdl_seq_LEN(decos)) {
    2642        1498 :         firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
    2643             :     }
    2644             : 
    2645             :     /* ultimately generate code for:
    2646             :          <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
    2647             :        where:
    2648             :          <func> is a zero arg function/closure created from the class body.
    2649             :             It mutates its locals to build the class namespace.
    2650             :          <name> is the class name
    2651             :          <bases> is the positional arguments and *varargs argument
    2652             :          <keywords> is the keyword arguments and **kwds argument
    2653             :        This borrows from compiler_call.
    2654             :     */
    2655             : 
    2656             :     /* 1. compile the class body into a code object */
    2657       37741 :     if (!compiler_enter_scope(c, s->v.ClassDef.name,
    2658             :                               COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) {
    2659           0 :         return 0;
    2660             :     }
    2661             :     /* this block represents what we do in the new scope */
    2662             :     {
    2663             :         /* use the class name for name mangling */
    2664       37741 :         Py_INCREF(s->v.ClassDef.name);
    2665       37741 :         Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
    2666             :         /* load (global) __name__ ... */
    2667       37741 :         if (!compiler_nameop(c, &_Py_ID(__name__), Load)) {
    2668           0 :             compiler_exit_scope(c);
    2669           0 :             return 0;
    2670             :         }
    2671             :         /* ... and store it as __module__ */
    2672       37741 :         if (!compiler_nameop(c, &_Py_ID(__module__), Store)) {
    2673           0 :             compiler_exit_scope(c);
    2674           0 :             return 0;
    2675             :         }
    2676       37741 :         assert(c->u->u_qualname);
    2677       37741 :         ADDOP_LOAD_CONST(c, c->u->u_qualname);
    2678       37741 :         if (!compiler_nameop(c, &_Py_ID(__qualname__), Store)) {
    2679           0 :             compiler_exit_scope(c);
    2680           0 :             return 0;
    2681             :         }
    2682             :         /* compile the body proper */
    2683       37741 :         if (!compiler_body(c, s->v.ClassDef.body)) {
    2684          17 :             compiler_exit_scope(c);
    2685          17 :             return 0;
    2686             :         }
    2687             :         /* The following code is artificial */
    2688       37724 :         UNSET_LOC(c);
    2689             :         /* Return __classcell__ if it is referenced, otherwise return None */
    2690       37724 :         if (c->u->u_ste->ste_needs_class_closure) {
    2691             :             /* Store __classcell__ into class namespace & return it */
    2692        4795 :             i = compiler_lookup_arg(c->u->u_cellvars, &_Py_ID(__class__));
    2693        4795 :             if (i < 0) {
    2694           0 :                 compiler_exit_scope(c);
    2695           0 :                 return 0;
    2696             :             }
    2697        4795 :             assert(i == 0);
    2698             : 
    2699        4795 :             ADDOP_I(c, LOAD_CLOSURE, i);
    2700        4795 :             ADDOP_I(c, COPY, 1);
    2701        4795 :             if (!compiler_nameop(c, &_Py_ID(__classcell__), Store)) {
    2702           0 :                 compiler_exit_scope(c);
    2703           0 :                 return 0;
    2704             :             }
    2705             :         }
    2706             :         else {
    2707             :             /* No methods referenced __class__, so just return None */
    2708       32929 :             assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
    2709       32929 :             ADDOP_LOAD_CONST(c, Py_None);
    2710             :         }
    2711       37724 :         ADDOP_IN_SCOPE(c, RETURN_VALUE);
    2712             :         /* create the code object */
    2713       37724 :         co = assemble(c, 1);
    2714             :     }
    2715             :     /* leave the new scope */
    2716       37724 :     compiler_exit_scope(c);
    2717       37724 :     if (co == NULL)
    2718           0 :         return 0;
    2719             : 
    2720             :     /* 2. load the 'build_class' function */
    2721       37724 :     ADDOP(c, PUSH_NULL);
    2722       37724 :     ADDOP(c, LOAD_BUILD_CLASS);
    2723             : 
    2724             :     /* 3. load a function (or closure) made from the code object */
    2725       37724 :     if (!compiler_make_closure(c, co, 0, NULL)) {
    2726           0 :         Py_DECREF(co);
    2727           0 :         return 0;
    2728             :     }
    2729       37724 :     Py_DECREF(co);
    2730             : 
    2731             :     /* 4. load class name */
    2732       37724 :     ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
    2733             : 
    2734             :     /* 5. generate the rest of the code for the call */
    2735       37724 :     if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
    2736           1 :         return 0;
    2737             :     /* 6. apply decorators */
    2738       37723 :     if (!compiler_apply_decorators(c, decos))
    2739           0 :         return 0;
    2740             : 
    2741             :     /* 7. store into <name> */
    2742       37723 :     if (!compiler_nameop(c, s->v.ClassDef.name, Store))
    2743           0 :         return 0;
    2744       37723 :     return 1;
    2745             : }
    2746             : 
    2747             : /* Return 0 if the expression is a constant value except named singletons.
    2748             :    Return 1 otherwise. */
    2749             : static int
    2750      428236 : check_is_arg(expr_ty e)
    2751             : {
    2752      428236 :     if (e->kind != Constant_kind) {
    2753      292735 :         return 1;
    2754             :     }
    2755      135501 :     PyObject *value = e->v.Constant.value;
    2756             :     return (value == Py_None
    2757       87392 :          || value == Py_False
    2758       87094 :          || value == Py_True
    2759      222893 :          || value == Py_Ellipsis);
    2760             : }
    2761             : 
    2762             : /* Check operands of identity chacks ("is" and "is not").
    2763             :    Emit a warning if any operand is a constant except named singletons.
    2764             :    Return 0 on error.
    2765             :  */
    2766             : static int
    2767      212746 : check_compare(struct compiler *c, expr_ty e)
    2768             : {
    2769             :     Py_ssize_t i, n;
    2770      212746 :     int left = check_is_arg(e->v.Compare.left);
    2771      212746 :     n = asdl_seq_LEN(e->v.Compare.ops);
    2772      428215 :     for (i = 0; i < n; i++) {
    2773      215490 :         cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
    2774      215490 :         int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
    2775      215490 :         if (op == Is || op == IsNot) {
    2776       60707 :             if (!right || !left) {
    2777          21 :                 const char *msg = (op == Is)
    2778             :                         ? "\"is\" with a literal. Did you mean \"==\"?"
    2779          21 :                         : "\"is not\" with a literal. Did you mean \"!=\"?";
    2780          21 :                 return compiler_warn(c, msg);
    2781             :             }
    2782             :         }
    2783      215469 :         left = right;
    2784             :     }
    2785      212725 :     return 1;
    2786             : }
    2787             : 
    2788      216929 : static int compiler_addcompare(struct compiler *c, cmpop_ty op)
    2789             : {
    2790             :     int cmp;
    2791      216929 :     switch (op) {
    2792       62128 :     case Eq:
    2793       62128 :         cmp = Py_EQ;
    2794       62128 :         break;
    2795       15259 :     case NotEq:
    2796       15259 :         cmp = Py_NE;
    2797       15259 :         break;
    2798       13909 :     case Lt:
    2799       13909 :         cmp = Py_LT;
    2800       13909 :         break;
    2801        7484 :     case LtE:
    2802        7484 :         cmp = Py_LE;
    2803        7484 :         break;
    2804       11813 :     case Gt:
    2805       11813 :         cmp = Py_GT;
    2806       11813 :         break;
    2807        7255 :     case GtE:
    2808        7255 :         cmp = Py_GE;
    2809        7255 :         break;
    2810       34978 :     case Is:
    2811       34978 :         ADDOP_I(c, IS_OP, 0);
    2812       34978 :         return 1;
    2813       25757 :     case IsNot:
    2814       25757 :         ADDOP_I(c, IS_OP, 1);
    2815       25757 :         return 1;
    2816       27857 :     case In:
    2817       27857 :         ADDOP_I(c, CONTAINS_OP, 0);
    2818       27857 :         return 1;
    2819       10489 :     case NotIn:
    2820       10489 :         ADDOP_I(c, CONTAINS_OP, 1);
    2821       10489 :         return 1;
    2822           0 :     default:
    2823           0 :         Py_UNREACHABLE();
    2824             :     }
    2825      117848 :     ADDOP_I(c, COMPARE_OP, cmp);
    2826      117848 :     return 1;
    2827             : }
    2828             : 
    2829             : 
    2830             : 
    2831             : static int
    2832      666330 : compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
    2833             : {
    2834      666330 :     switch (e->kind) {
    2835       48248 :     case UnaryOp_kind:
    2836       48248 :         if (e->v.UnaryOp.op == Not)
    2837       48248 :             return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
    2838             :         /* fallback to general implementation */
    2839           0 :         break;
    2840       41998 :     case BoolOp_kind: {
    2841       41998 :         asdl_expr_seq *s = e->v.BoolOp.values;
    2842       41998 :         Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
    2843       41998 :         assert(n >= 0);
    2844       41998 :         int cond2 = e->v.BoolOp.op == Or;
    2845       41998 :         basicblock *next2 = next;
    2846       41998 :         if (!cond2 != !cond) {
    2847       13982 :             next2 = compiler_new_block(c);
    2848       13982 :             if (next2 == NULL)
    2849           0 :                 return 0;
    2850             :         }
    2851       89863 :         for (i = 0; i < n; ++i) {
    2852       47865 :             if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
    2853           0 :                 return 0;
    2854             :         }
    2855       41998 :         if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
    2856           0 :             return 0;
    2857       41998 :         if (next2 != next)
    2858       13982 :             compiler_use_next_block(c, next2);
    2859       41998 :         return 1;
    2860             :     }
    2861          30 :     case IfExp_kind: {
    2862             :         basicblock *end, *next2;
    2863          30 :         end = compiler_new_block(c);
    2864          30 :         if (end == NULL)
    2865           0 :             return 0;
    2866          30 :         next2 = compiler_new_block(c);
    2867          30 :         if (next2 == NULL)
    2868           0 :             return 0;
    2869          30 :         if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
    2870           0 :             return 0;
    2871          30 :         if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
    2872           0 :             return 0;
    2873          30 :         ADDOP_JUMP_NOLINE(c, JUMP, end);
    2874          30 :         compiler_use_next_block(c, next2);
    2875          30 :         if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
    2876           0 :             return 0;
    2877          30 :         compiler_use_next_block(c, end);
    2878          30 :         return 1;
    2879             :     }
    2880      189366 :     case Compare_kind: {
    2881      189366 :         Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
    2882      189366 :         if (n > 0) {
    2883        2250 :             if (!check_compare(c, e)) {
    2884           0 :                 return 0;
    2885             :             }
    2886        2250 :             basicblock *cleanup = compiler_new_block(c);
    2887        2250 :             if (cleanup == NULL)
    2888           0 :                 return 0;
    2889        2250 :             VISIT(c, expr, e->v.Compare.left);
    2890        4596 :             for (i = 0; i < n; i++) {
    2891        2346 :                 VISIT(c, expr,
    2892             :                     (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
    2893        2346 :                 ADDOP_I(c, SWAP, 2);
    2894        2346 :                 ADDOP_I(c, COPY, 2);
    2895        2346 :                 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
    2896        2346 :                 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
    2897             :             }
    2898        2250 :             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
    2899        2250 :             ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
    2900        2250 :             ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
    2901        2250 :             basicblock *end = compiler_new_block(c);
    2902        2250 :             if (end == NULL)
    2903           0 :                 return 0;
    2904        2250 :             ADDOP_JUMP_NOLINE(c, JUMP, end);
    2905        2250 :             compiler_use_next_block(c, cleanup);
    2906        2250 :             ADDOP(c, POP_TOP);
    2907        2250 :             if (!cond) {
    2908        1303 :                 ADDOP_JUMP_NOLINE(c, JUMP, next);
    2909             :             }
    2910        2250 :             compiler_use_next_block(c, end);
    2911        2250 :             return 1;
    2912             :         }
    2913             :         /* fallback to general implementation */
    2914      187116 :         break;
    2915             :     }
    2916      386688 :     default:
    2917             :         /* fallback to general implementation */
    2918      386688 :         break;
    2919             :     }
    2920             : 
    2921             :     /* general implementation */
    2922      573804 :     VISIT(c, expr, e);
    2923      573804 :     ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
    2924      573804 :     return 1;
    2925             : }
    2926             : 
    2927             : static int
    2928        9762 : compiler_ifexp(struct compiler *c, expr_ty e)
    2929             : {
    2930             :     basicblock *end, *next;
    2931             : 
    2932        9762 :     assert(e->kind == IfExp_kind);
    2933        9762 :     end = compiler_new_block(c);
    2934        9762 :     if (end == NULL)
    2935           0 :         return 0;
    2936        9762 :     next = compiler_new_block(c);
    2937        9762 :     if (next == NULL)
    2938           0 :         return 0;
    2939        9762 :     if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
    2940           0 :         return 0;
    2941        9762 :     VISIT(c, expr, e->v.IfExp.body);
    2942        9762 :     ADDOP_JUMP_NOLINE(c, JUMP, end);
    2943        9762 :     compiler_use_next_block(c, next);
    2944        9762 :     VISIT(c, expr, e->v.IfExp.orelse);
    2945        9762 :     compiler_use_next_block(c, end);
    2946        9762 :     return 1;
    2947             : }
    2948             : 
    2949             : static int
    2950       34765 : compiler_lambda(struct compiler *c, expr_ty e)
    2951             : {
    2952             :     PyCodeObject *co;
    2953             :     PyObject *qualname;
    2954             :     Py_ssize_t funcflags;
    2955       34765 :     arguments_ty args = e->v.Lambda.args;
    2956       34765 :     assert(e->kind == Lambda_kind);
    2957             : 
    2958       34765 :     if (!compiler_check_debug_args(c, args))
    2959           3 :         return 0;
    2960             : 
    2961       34762 :     funcflags = compiler_default_arguments(c, args);
    2962       34762 :     if (funcflags == -1) {
    2963           0 :         return 0;
    2964             :     }
    2965             : 
    2966             :     _Py_DECLARE_STR(anon_lambda, "<lambda>");
    2967       34762 :     if (!compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA,
    2968             :                               (void *)e, e->lineno)) {
    2969           0 :         return 0;
    2970             :     }
    2971             :     /* Make None the first constant, so the lambda can't have a
    2972             :        docstring. */
    2973       34762 :     if (compiler_add_const(c, Py_None) < 0)
    2974           0 :         return 0;
    2975             : 
    2976       34762 :     c->u->u_argcount = asdl_seq_LEN(args->args);
    2977       34762 :     c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
    2978       34762 :     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
    2979       34762 :     VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
    2980       34762 :     if (c->u->u_ste->ste_generator) {
    2981          32 :         co = assemble(c, 0);
    2982             :     }
    2983             :     else {
    2984       34730 :         ADDOP_IN_SCOPE(c, RETURN_VALUE);
    2985       34730 :         co = assemble(c, 1);
    2986             :     }
    2987       34762 :     qualname = c->u->u_qualname;
    2988       34762 :     Py_INCREF(qualname);
    2989       34762 :     compiler_exit_scope(c);
    2990       34762 :     if (co == NULL) {
    2991           0 :         Py_DECREF(qualname);
    2992           0 :         return 0;
    2993             :     }
    2994             : 
    2995       34762 :     if (!compiler_make_closure(c, co, funcflags, qualname)) {
    2996           0 :         Py_DECREF(qualname);
    2997           0 :         Py_DECREF(co);
    2998           0 :         return 0;
    2999             :     }
    3000       34762 :     Py_DECREF(qualname);
    3001       34762 :     Py_DECREF(co);
    3002             : 
    3003       34762 :     return 1;
    3004             : }
    3005             : 
    3006             : static int
    3007      486138 : compiler_if(struct compiler *c, stmt_ty s)
    3008             : {
    3009             :     basicblock *end, *next;
    3010      486138 :     assert(s->kind == If_kind);
    3011      486138 :     end = compiler_new_block(c);
    3012      486138 :     if (end == NULL) {
    3013           0 :         return 0;
    3014             :     }
    3015      486138 :     if (asdl_seq_LEN(s->v.If.orelse)) {
    3016       84001 :         next = compiler_new_block(c);
    3017       84001 :         if (next == NULL) {
    3018           0 :             return 0;
    3019             :         }
    3020             :     }
    3021             :     else {
    3022      402137 :         next = end;
    3023             :     }
    3024      486138 :     if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
    3025           0 :         return 0;
    3026             :     }
    3027     1112150 :     VISIT_SEQ(c, stmt, s->v.If.body);
    3028      486119 :     if (asdl_seq_LEN(s->v.If.orelse)) {
    3029       83996 :         ADDOP_JUMP_NOLINE(c, JUMP, end);
    3030       83996 :         compiler_use_next_block(c, next);
    3031      193190 :         VISIT_SEQ(c, stmt, s->v.If.orelse);
    3032             :     }
    3033      486111 :     compiler_use_next_block(c, end);
    3034      486111 :     return 1;
    3035             : }
    3036             : 
    3037             : static int
    3038       46446 : compiler_for(struct compiler *c, stmt_ty s)
    3039             : {
    3040             :     basicblock *start, *body, *cleanup, *end;
    3041             : 
    3042       46446 :     start = compiler_new_block(c);
    3043       46446 :     body = compiler_new_block(c);
    3044       46446 :     cleanup = compiler_new_block(c);
    3045       46446 :     end = compiler_new_block(c);
    3046       46446 :     if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
    3047           0 :         return 0;
    3048             :     }
    3049       46446 :     if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
    3050           0 :         return 0;
    3051             :     }
    3052       46446 :     VISIT(c, expr, s->v.For.iter);
    3053       46446 :     ADDOP(c, GET_ITER);
    3054       46446 :     compiler_use_next_block(c, start);
    3055       46446 :     ADDOP_JUMP(c, FOR_ITER, cleanup);
    3056       46446 :     compiler_use_next_block(c, body);
    3057       46446 :     VISIT(c, expr, s->v.For.target);
    3058      141877 :     VISIT_SEQ(c, stmt, s->v.For.body);
    3059             :     /* Mark jump as artificial */
    3060       46441 :     UNSET_LOC(c);
    3061       46441 :     ADDOP_JUMP(c, JUMP, start);
    3062       46441 :     compiler_use_next_block(c, cleanup);
    3063             : 
    3064       46441 :     compiler_pop_fblock(c, FOR_LOOP, start);
    3065             : 
    3066       48060 :     VISIT_SEQ(c, stmt, s->v.For.orelse);
    3067       46441 :     compiler_use_next_block(c, end);
    3068       46441 :     return 1;
    3069             : }
    3070             : 
    3071             : 
    3072             : static int
    3073          81 : compiler_async_for(struct compiler *c, stmt_ty s)
    3074             : {
    3075             :     basicblock *start, *except, *end;
    3076          81 :     if (IS_TOP_LEVEL_AWAIT(c)){
    3077           2 :         c->u->u_ste->ste_coroutine = 1;
    3078          79 :     } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
    3079           8 :         return compiler_error(c, "'async for' outside async function");
    3080             :     }
    3081             : 
    3082          73 :     start = compiler_new_block(c);
    3083          73 :     except = compiler_new_block(c);
    3084          73 :     end = compiler_new_block(c);
    3085             : 
    3086          73 :     if (start == NULL || except == NULL || end == NULL) {
    3087           0 :         return 0;
    3088             :     }
    3089          73 :     VISIT(c, expr, s->v.AsyncFor.iter);
    3090          73 :     ADDOP(c, GET_AITER);
    3091             : 
    3092          73 :     compiler_use_next_block(c, start);
    3093          73 :     if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
    3094           0 :         return 0;
    3095             :     }
    3096             :     /* SETUP_FINALLY to guard the __anext__ call */
    3097          73 :     ADDOP_JUMP(c, SETUP_FINALLY, except);
    3098          73 :     ADDOP(c, GET_ANEXT);
    3099          73 :     ADDOP_LOAD_CONST(c, Py_None);
    3100          73 :     ADD_YIELD_FROM(c, 1);
    3101          73 :     ADDOP(c, POP_BLOCK);  /* for SETUP_FINALLY */
    3102             : 
    3103             :     /* Success block for __anext__ */
    3104          73 :     VISIT(c, expr, s->v.AsyncFor.target);
    3105         151 :     VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
    3106             :     /* Mark jump as artificial */
    3107          73 :     UNSET_LOC(c);
    3108          73 :     ADDOP_JUMP(c, JUMP, start);
    3109             : 
    3110          73 :     compiler_pop_fblock(c, FOR_LOOP, start);
    3111             : 
    3112             :     /* Except block for __anext__ */
    3113          73 :     compiler_use_next_block(c, except);
    3114             : 
    3115             :     /* Use same line number as the iterator,
    3116             :      * as the END_ASYNC_FOR succeeds the `for`, not the body. */
    3117          73 :     SET_LOC(c, s->v.AsyncFor.iter);
    3118          73 :     ADDOP(c, END_ASYNC_FOR);
    3119             : 
    3120             :     /* `else` block */
    3121          90 :     VISIT_SEQ(c, stmt, s->v.For.orelse);
    3122             : 
    3123          73 :     compiler_use_next_block(c, end);
    3124             : 
    3125          73 :     return 1;
    3126             : }
    3127             : 
    3128             : static int
    3129        9960 : compiler_while(struct compiler *c, stmt_ty s)
    3130             : {
    3131        9960 :     basicblock *loop, *body, *end, *anchor = NULL;
    3132        9960 :     loop = compiler_new_block(c);
    3133        9960 :     body = compiler_new_block(c);
    3134        9960 :     anchor = compiler_new_block(c);
    3135        9960 :     end = compiler_new_block(c);
    3136        9960 :     if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
    3137           0 :         return 0;
    3138             :     }
    3139        9960 :     compiler_use_next_block(c, loop);
    3140        9960 :     if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
    3141           1 :         return 0;
    3142             :     }
    3143        9959 :     if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
    3144           0 :         return 0;
    3145             :     }
    3146             : 
    3147        9959 :     compiler_use_next_block(c, body);
    3148       42273 :     VISIT_SEQ(c, stmt, s->v.While.body);
    3149        9932 :     SET_LOC(c, s);
    3150        9932 :     if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
    3151           0 :         return 0;
    3152             :     }
    3153             : 
    3154        9932 :     compiler_pop_fblock(c, WHILE_LOOP, loop);
    3155             : 
    3156        9932 :     compiler_use_next_block(c, anchor);
    3157        9932 :     if (s->v.While.orelse) {
    3158         814 :         VISIT_SEQ(c, stmt, s->v.While.orelse);
    3159             :     }
    3160        9932 :     compiler_use_next_block(c, end);
    3161             : 
    3162        9932 :     return 1;
    3163             : }
    3164             : 
    3165             : static int
    3166      212398 : compiler_return(struct compiler *c, stmt_ty s)
    3167             : {
    3168      416084 :     int preserve_tos = ((s->v.Return.value != NULL) &&
    3169      203686 :                         (s->v.Return.value->kind != Constant_kind));
    3170      212398 :     if (c->u->u_ste->ste_type != FunctionBlock)
    3171          15 :         return compiler_error(c, "'return' outside function");
    3172      212383 :     if (s->v.Return.value != NULL &&
    3173      203682 :         c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
    3174             :     {
    3175           3 :             return compiler_error(
    3176             :                 c, "'return' with value in async generator");
    3177             :     }
    3178      212380 :     if (preserve_tos) {
    3179      175420 :         VISIT(c, expr, s->v.Return.value);
    3180             :     } else {
    3181             :         /* Emit instruction with line number for return value */
    3182       36960 :         if (s->v.Return.value != NULL) {
    3183       28259 :             SET_LOC(c, s->v.Return.value);
    3184       28259 :             ADDOP(c, NOP);
    3185             :         }
    3186             :     }
    3187      212380 :     if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
    3188        9261 :         SET_LOC(c, s);
    3189        9261 :         ADDOP(c, NOP);
    3190             :     }
    3191             : 
    3192      212380 :     if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
    3193           2 :         return 0;
    3194      212378 :     if (s->v.Return.value == NULL) {
    3195        8701 :         ADDOP_LOAD_CONST(c, Py_None);
    3196             :     }
    3197      203677 :     else if (!preserve_tos) {
    3198       28257 :         ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
    3199             :     }
    3200      212378 :     ADDOP(c, RETURN_VALUE);
    3201             : 
    3202      212378 :     return 1;
    3203             : }
    3204             : 
    3205             : static int
    3206        8573 : compiler_break(struct compiler *c)
    3207             : {
    3208        8573 :     struct fblockinfo *loop = NULL;
    3209             :     /* Emit instruction with line number */
    3210        8573 :     ADDOP(c, NOP);
    3211        8573 :     if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
    3212           3 :         return 0;
    3213             :     }
    3214        8570 :     if (loop == NULL) {
    3215           9 :         return compiler_error(c, "'break' outside loop");
    3216             :     }
    3217        8561 :     if (!compiler_unwind_fblock(c, loop, 0)) {
    3218           0 :         return 0;
    3219             :     }
    3220        8561 :     ADDOP_JUMP(c, JUMP, loop->fb_exit);
    3221        8561 :     return 1;
    3222             : }
    3223             : 
    3224             : static int
    3225        7537 : compiler_continue(struct compiler *c)
    3226             : {
    3227        7537 :     struct fblockinfo *loop = NULL;
    3228             :     /* Emit instruction with line number */
    3229        7537 :     ADDOP(c, NOP);
    3230        7537 :     if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
    3231           3 :         return 0;
    3232             :     }
    3233        7534 :     if (loop == NULL) {
    3234          11 :         return compiler_error(c, "'continue' not properly in loop");
    3235             :     }
    3236        7523 :     ADDOP_JUMP(c, JUMP, loop->fb_block);
    3237        7523 :     return 1;
    3238             : }
    3239             : 
    3240             : 
    3241             : /* Code generated for "try: <body> finally: <finalbody>" is as follows:
    3242             : 
    3243             :         SETUP_FINALLY           L
    3244             :         <code for body>
    3245             :         POP_BLOCK
    3246             :         <code for finalbody>
    3247             :         JUMP E
    3248             :     L:
    3249             :         <code for finalbody>
    3250             :     E:
    3251             : 
    3252             :    The special instructions use the block stack.  Each block
    3253             :    stack entry contains the instruction that created it (here
    3254             :    SETUP_FINALLY), the level of the value stack at the time the
    3255             :    block stack entry was created, and a label (here L).
    3256             : 
    3257             :    SETUP_FINALLY:
    3258             :     Pushes the current value stack level and the label
    3259             :     onto the block stack.
    3260             :    POP_BLOCK:
    3261             :     Pops en entry from the block stack.
    3262             : 
    3263             :    The block stack is unwound when an exception is raised:
    3264             :    when a SETUP_FINALLY entry is found, the raised and the caught
    3265             :    exceptions are pushed onto the value stack (and the exception
    3266             :    condition is cleared), and the interpreter jumps to the label
    3267             :    gotten from the block stack.
    3268             : */
    3269             : 
    3270             : static int
    3271        5933 : compiler_try_finally(struct compiler *c, stmt_ty s)
    3272             : {
    3273             :     basicblock *body, *end, *exit, *cleanup;
    3274             : 
    3275        5933 :     body = compiler_new_block(c);
    3276        5933 :     end = compiler_new_block(c);
    3277        5933 :     exit = compiler_new_block(c);
    3278        5933 :     cleanup = compiler_new_block(c);
    3279        5933 :     if (body == NULL || end == NULL || exit == NULL || cleanup == NULL) {
    3280           0 :         return 0;
    3281             :     }
    3282             :     /* `try` block */
    3283        5933 :     ADDOP_JUMP(c, SETUP_FINALLY, end);
    3284        5933 :     compiler_use_next_block(c, body);
    3285        5933 :     if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
    3286           0 :         return 0;
    3287        5933 :     if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
    3288         473 :         if (!compiler_try_except(c, s))
    3289           0 :             return 0;
    3290             :     }
    3291             :     else {
    3292       16142 :         VISIT_SEQ(c, stmt, s->v.Try.body);
    3293             :     }
    3294        5932 :     ADDOP_NOLINE(c, POP_BLOCK);
    3295        5932 :     compiler_pop_fblock(c, FINALLY_TRY, body);
    3296       13246 :     VISIT_SEQ(c, stmt, s->v.Try.finalbody);
    3297        5931 :     ADDOP_JUMP_NOLINE(c, JUMP, exit);
    3298             :     /* `finally` block */
    3299        5931 :     compiler_use_next_block(c, end);
    3300             : 
    3301        5931 :     UNSET_LOC(c);
    3302        5931 :     ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
    3303        5931 :     ADDOP(c, PUSH_EXC_INFO);
    3304        5931 :     if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
    3305           0 :         return 0;
    3306       13245 :     VISIT_SEQ(c, stmt, s->v.Try.finalbody);
    3307        5931 :     compiler_pop_fblock(c, FINALLY_END, end);
    3308        5931 :     ADDOP_I(c, RERAISE, 0);
    3309        5931 :     compiler_use_next_block(c, cleanup);
    3310        5931 :     POP_EXCEPT_AND_RERAISE(c);
    3311        5931 :     compiler_use_next_block(c, exit);
    3312        5931 :     return 1;
    3313             : }
    3314             : 
    3315             : static int
    3316          25 : compiler_try_star_finally(struct compiler *c, stmt_ty s)
    3317             : {
    3318          25 :     basicblock *body = compiler_new_block(c);
    3319          25 :     if (body == NULL) {
    3320           0 :         return 0;
    3321             :     }
    3322          25 :     basicblock *end = compiler_new_block(c);
    3323          25 :     if (!end) {
    3324           0 :         return 0;
    3325             :     }
    3326          25 :     basicblock *exit = compiler_new_block(c);
    3327          25 :     if (!exit) {
    3328           0 :         return 0;
    3329             :     }
    3330          25 :     basicblock *cleanup = compiler_new_block(c);
    3331          25 :     if (!cleanup) {
    3332           0 :         return 0;
    3333             :     }
    3334             :     /* `try` block */
    3335          25 :     ADDOP_JUMP(c, SETUP_FINALLY, end);
    3336          25 :     compiler_use_next_block(c, body);
    3337          25 :     if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.TryStar.finalbody)) {
    3338           0 :         return 0;
    3339             :     }
    3340          25 :     if (s->v.TryStar.handlers && asdl_seq_LEN(s->v.TryStar.handlers)) {
    3341          25 :         if (!compiler_try_star_except(c, s)) {
    3342           3 :             return 0;
    3343             :         }
    3344             :     }
    3345             :     else {
    3346           0 :         VISIT_SEQ(c, stmt, s->v.TryStar.body);
    3347             :     }
    3348          22 :     ADDOP_NOLINE(c, POP_BLOCK);
    3349          22 :     compiler_pop_fblock(c, FINALLY_TRY, body);
    3350          44 :     VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
    3351          22 :     ADDOP_JUMP_NOLINE(c, JUMP, exit);
    3352             :     /* `finally` block */
    3353          22 :     compiler_use_next_block(c, end);
    3354             : 
    3355          22 :     UNSET_LOC(c);
    3356          22 :     ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
    3357          22 :     ADDOP(c, PUSH_EXC_INFO);
    3358          22 :     if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) {
    3359           0 :         return 0;
    3360             :     }
    3361          44 :     VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
    3362          22 :     compiler_pop_fblock(c, FINALLY_END, end);
    3363          22 :     ADDOP_I(c, RERAISE, 0);
    3364          22 :     compiler_use_next_block(c, cleanup);
    3365          22 :     POP_EXCEPT_AND_RERAISE(c);
    3366          22 :     compiler_use_next_block(c, exit);
    3367          22 :     return 1;
    3368             : }
    3369             : 
    3370             : 
    3371             : /*
    3372             :    Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
    3373             :    (The contents of the value stack is shown in [], with the top
    3374             :    at the right; 'tb' is trace-back info, 'val' the exception's
    3375             :    associated value, and 'exc' the exception.)
    3376             : 
    3377             :    Value stack          Label   Instruction     Argument
    3378             :    []                           SETUP_FINALLY   L1
    3379             :    []                           <code for S>
    3380             :    []                           POP_BLOCK
    3381             :    []                           JUMP            L0
    3382             : 
    3383             :    [exc]                L1:     <evaluate E1>           )
    3384             :    [exc, E1]                    CHECK_EXC_MATCH         )
    3385             :    [exc, bool]                  POP_JUMP_IF_FALSE L2    ) only if E1
    3386             :    [exc]                        <assign to V1>  (or POP if no V1)
    3387             :    []                           <code for S1>
    3388             :                                 JUMP            L0
    3389             : 
    3390             :    [exc]                L2:     <evaluate E2>
    3391             :    .............................etc.......................
    3392             : 
    3393             :    [exc]                Ln+1:   RERAISE     # re-raise exception
    3394             : 
    3395             :    []                   L0:     <next statement>
    3396             : 
    3397             :    Of course, parts are not generated if Vi or Ei is not present.
    3398             : */
    3399             : static int
    3400       34786 : compiler_try_except(struct compiler *c, stmt_ty s)
    3401             : {
    3402             :     basicblock *body, *except, *end, *cleanup;
    3403             :     Py_ssize_t i, n;
    3404             : 
    3405       34786 :     body = compiler_new_block(c);
    3406       34786 :     except = compiler_new_block(c);
    3407       34786 :     end = compiler_new_block(c);
    3408       34786 :     cleanup = compiler_new_block(c);
    3409       34786 :     if (body == NULL || except == NULL || end == NULL || cleanup == NULL)
    3410           0 :         return 0;
    3411       34786 :     ADDOP_JUMP(c, SETUP_FINALLY, except);
    3412       34786 :     compiler_use_next_block(c, body);
    3413       34786 :     if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
    3414           1 :         return 0;
    3415       83230 :     VISIT_SEQ(c, stmt, s->v.Try.body);
    3416       34784 :     compiler_pop_fblock(c, TRY_EXCEPT, body);
    3417       34784 :     ADDOP_NOLINE(c, POP_BLOCK);
    3418       34784 :     if (s->v.Try.orelse && asdl_seq_LEN(s->v.Try.orelse)) {
    3419       12003 :         VISIT_SEQ(c, stmt, s->v.Try.orelse);
    3420             :     }
    3421       34784 :     ADDOP_JUMP_NOLINE(c, JUMP, end);
    3422       34784 :     n = asdl_seq_LEN(s->v.Try.handlers);
    3423       34784 :     compiler_use_next_block(c, except);
    3424             : 
    3425       34784 :     UNSET_LOC(c);
    3426       34784 :     ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
    3427       34784 :     ADDOP(c, PUSH_EXC_INFO);
    3428             :     /* Runtime will push a block here, so we need to account for that */
    3429       34784 :     if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
    3430           0 :         return 0;
    3431       71865 :     for (i = 0; i < n; i++) {
    3432       37092 :         excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
    3433             :             s->v.Try.handlers, i);
    3434       37092 :         SET_LOC(c, handler);
    3435       37092 :         if (!handler->v.ExceptHandler.type && i < n-1) {
    3436           1 :             return compiler_error(c, "default 'except:' must be last");
    3437             :         }
    3438       37091 :         except = compiler_new_block(c);
    3439       37091 :         if (except == NULL)
    3440           0 :             return 0;
    3441       37091 :         if (handler->v.ExceptHandler.type) {
    3442       35470 :             VISIT(c, expr, handler->v.ExceptHandler.type);
    3443       35470 :             ADDOP(c, CHECK_EXC_MATCH);
    3444       35470 :             ADDOP_JUMP(c, POP_JUMP_IF_FALSE, except);
    3445             :         }
    3446       37091 :         if (handler->v.ExceptHandler.name) {
    3447             :             basicblock *cleanup_end, *cleanup_body;
    3448             : 
    3449        7227 :             cleanup_end = compiler_new_block(c);
    3450        7227 :             cleanup_body = compiler_new_block(c);
    3451        7227 :             if (cleanup_end == NULL || cleanup_body == NULL) {
    3452           0 :                 return 0;
    3453             :             }
    3454             : 
    3455        7227 :             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
    3456             : 
    3457             :             /*
    3458             :               try:
    3459             :                   # body
    3460             :               except type as name:
    3461             :                   try:
    3462             :                       # body
    3463             :                   finally:
    3464             :                       name = None # in case body contains "del name"
    3465             :                       del name
    3466             :             */
    3467             : 
    3468             :             /* second try: */
    3469        7227 :             ADDOP_JUMP(c, SETUP_CLEANUP, cleanup_end);
    3470        7227 :             compiler_use_next_block(c, cleanup_body);
    3471        7227 :             if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
    3472           0 :                 return 0;
    3473             : 
    3474             :             /* second # body */
    3475       19078 :             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
    3476        7217 :             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
    3477             :             /* name = None; del name; # Mark as artificial */
    3478        7217 :             UNSET_LOC(c);
    3479        7217 :             ADDOP(c, POP_BLOCK);
    3480        7217 :             ADDOP(c, POP_BLOCK);
    3481        7217 :             ADDOP(c, POP_EXCEPT);
    3482        7217 :             ADDOP_LOAD_CONST(c, Py_None);
    3483        7217 :             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
    3484        7217 :             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
    3485        7217 :             ADDOP_JUMP(c, JUMP, end);
    3486             : 
    3487             :             /* except: */
    3488        7217 :             compiler_use_next_block(c, cleanup_end);
    3489             : 
    3490             :             /* name = None; del name; # Mark as artificial */
    3491        7217 :             UNSET_LOC(c);
    3492             : 
    3493        7217 :             ADDOP_LOAD_CONST(c, Py_None);
    3494        7217 :             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
    3495        7217 :             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
    3496             : 
    3497        7217 :             ADDOP_I(c, RERAISE, 1);
    3498             :         }
    3499             :         else {
    3500             :             basicblock *cleanup_body;
    3501             : 
    3502       29864 :             cleanup_body = compiler_new_block(c);
    3503       29864 :             if (!cleanup_body)
    3504           0 :                 return 0;
    3505             : 
    3506       29864 :             ADDOP(c, POP_TOP); /* exc_value */
    3507       29864 :             compiler_use_next_block(c, cleanup_body);
    3508       29864 :             if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
    3509           0 :                 return 0;
    3510       67747 :             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
    3511       29864 :             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
    3512       29864 :             UNSET_LOC(c);
    3513       29864 :             ADDOP(c, POP_BLOCK);
    3514       29864 :             ADDOP(c, POP_EXCEPT);
    3515       29864 :             ADDOP_JUMP(c, JUMP, end);
    3516             :         }
    3517       37081 :         compiler_use_next_block(c, except);
    3518             :     }
    3519             :     /* Mark as artificial */
    3520       34773 :     UNSET_LOC(c);
    3521       34773 :     compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
    3522       34773 :     ADDOP_I(c, RERAISE, 0);
    3523       34773 :     compiler_use_next_block(c, cleanup);
    3524       34773 :     POP_EXCEPT_AND_RERAISE(c);
    3525       34773 :     compiler_use_next_block(c, end);
    3526       34773 :     return 1;
    3527             : }
    3528             : 
    3529             : /*
    3530             :    Code generated for "try: S except* E1 as V1: S1 except* E2 as V2: S2 ...":
    3531             :    (The contents of the value stack is shown in [], with the top
    3532             :    at the right; 'tb' is trace-back info, 'val' the exception instance,
    3533             :    and 'typ' the exception's type.)
    3534             : 
    3535             :    Value stack                   Label         Instruction     Argument
    3536             :    []                                         SETUP_FINALLY         L1
    3537             :    []                                         <code for S>
    3538             :    []                                         POP_BLOCK
    3539             :    []                                         JUMP                  L0
    3540             : 
    3541             :    [exc]                            L1:       COPY 1       )  save copy of the original exception
    3542             :    [orig, exc]                                BUILD_LIST   )  list for raised/reraised excs ("result")
    3543             :    [orig, exc, res]                           SWAP 2
    3544             : 
    3545             :    [orig, res, exc]                           <evaluate E1>
    3546             :    [orig, res, exc, E1]                       CHECK_EG_MATCH
    3547             :    [orig, red, rest/exc, match?]              COPY 1
    3548             :    [orig, red, rest/exc, match?, match?]      POP_JUMP_IF_NOT_NONE  H1
    3549             :    [orig, red, exc, None]                     POP_TOP
    3550             :    [orig, red, exc]                           JUMP L2
    3551             : 
    3552             :    [orig, res, rest, match]         H1:       <assign to V1>  (or POP if no V1)
    3553             : 
    3554             :    [orig, res, rest]                          SETUP_FINALLY         R1
    3555             :    [orig, res, rest]                          <code for S1>
    3556             :    [orig, res, rest]                          JUMP                  L2
    3557             : 
    3558             :    [orig, res, rest, i, v]          R1:       LIST_APPEND   3 ) exc raised in except* body - add to res
    3559             :    [orig, res, rest, i]                       POP
    3560             : 
    3561             :    [orig, res, rest]                L2:       <evaluate E2>
    3562             :    .............................etc.......................
    3563             : 
    3564             :    [orig, res, rest]                Ln+1:     LIST_APPEND 1  ) add unhandled exc to res (could be None)
    3565             : 
    3566             :    [orig, res]                                PREP_RERAISE_STAR
    3567             :    [exc]                                      COPY 1
    3568             :    [exc, exc]                                 POP_JUMP_IF_NOT_NONE  RER
    3569             :    [exc]                                      POP_TOP
    3570             :    []                                         JUMP                  L0
    3571             : 
    3572             :    [exc]                            RER:      SWAP 2
    3573             :    [exc, prev_exc_info]                       POP_EXCEPT
    3574             :    [exc]                                      RERAISE               0
    3575             : 
    3576             :    []                               L0:       <next statement>
    3577             : */
    3578             : static int
    3579         108 : compiler_try_star_except(struct compiler *c, stmt_ty s)
    3580             : {
    3581         108 :     basicblock *body = compiler_new_block(c);
    3582         108 :     if (body == NULL) {
    3583           0 :         return 0;
    3584             :     }
    3585         108 :     basicblock *except = compiler_new_block(c);
    3586         108 :     if (except == NULL) {
    3587           0 :         return 0;
    3588             :     }
    3589         108 :     basicblock *orelse = compiler_new_block(c);
    3590         108 :      if (orelse == NULL) {
    3591           0 :         return 0;
    3592             :     }
    3593         108 :     basicblock *end = compiler_new_block(c);
    3594         108 :     if (end == NULL) {
    3595           0 :         return 0;
    3596             :     }
    3597         108 :     basicblock *cleanup = compiler_new_block(c);
    3598         108 :     if (cleanup == NULL) {
    3599           0 :         return 0;
    3600             :     }
    3601         108 :     basicblock *reraise_star = compiler_new_block(c);
    3602         108 :     if (reraise_star == NULL) {
    3603           0 :         return 0;
    3604             :     }
    3605             : 
    3606         108 :     ADDOP_JUMP(c, SETUP_FINALLY, except);
    3607         108 :     compiler_use_next_block(c, body);
    3608         108 :     if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) {
    3609           0 :         return 0;
    3610             :     }
    3611         218 :     VISIT_SEQ(c, stmt, s->v.TryStar.body);
    3612         108 :     compiler_pop_fblock(c, TRY_EXCEPT, body);
    3613         108 :     ADDOP_NOLINE(c, POP_BLOCK);
    3614         108 :     ADDOP_JUMP_NOLINE(c, JUMP, orelse);
    3615         108 :     Py_ssize_t n = asdl_seq_LEN(s->v.TryStar.handlers);
    3616         108 :     compiler_use_next_block(c, except);
    3617             : 
    3618         108 :     UNSET_LOC(c);
    3619         108 :     ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
    3620         108 :     ADDOP(c, PUSH_EXC_INFO);
    3621             :     /* Runtime will push a block here, so we need to account for that */
    3622         108 :     if (!compiler_push_fblock(c, EXCEPTION_GROUP_HANDLER,
    3623             :                                  NULL, NULL, "except handler")) {
    3624           0 :         return 0;
    3625             :     }
    3626         228 :     for (Py_ssize_t i = 0; i < n; i++) {
    3627         128 :         excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
    3628             :             s->v.TryStar.handlers, i);
    3629         128 :         SET_LOC(c, handler);
    3630         128 :         except = compiler_new_block(c);
    3631         128 :         if (except == NULL) {
    3632           0 :             return 0;
    3633             :         }
    3634         128 :         basicblock *handle_match = compiler_new_block(c);
    3635         128 :         if (handle_match == NULL) {
    3636           0 :             return 0;
    3637             :         }
    3638         128 :         if (i == 0) {
    3639             :             /* Push the original EG into the stack */
    3640             :             /*
    3641             :                [exc]            COPY 1
    3642             :                [orig, exc]
    3643             :             */
    3644         108 :             ADDOP_I(c, COPY, 1);
    3645             : 
    3646             :             /* create empty list for exceptions raised/reraise in the except* blocks */
    3647             :             /*
    3648             :                [orig, exc]       BUILD_LIST
    3649             :                [orig, exc, []]   SWAP 2
    3650             :                [orig, [], exc]
    3651             :             */
    3652         108 :             ADDOP_I(c, BUILD_LIST, 0);
    3653         108 :             ADDOP_I(c, SWAP, 2);
    3654             :         }
    3655         128 :         if (handler->v.ExceptHandler.type) {
    3656         128 :             VISIT(c, expr, handler->v.ExceptHandler.type);
    3657         128 :             ADDOP(c, CHECK_EG_MATCH);
    3658         128 :             ADDOP_I(c, COPY, 1);
    3659         128 :             ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, handle_match);
    3660         128 :             ADDOP(c, POP_TOP);  // match
    3661         128 :             ADDOP_JUMP(c, JUMP, except);
    3662             :         }
    3663             : 
    3664         128 :         compiler_use_next_block(c, handle_match);
    3665             : 
    3666         128 :         basicblock *cleanup_end = compiler_new_block(c);
    3667         128 :         if (cleanup_end == NULL) {
    3668           0 :             return 0;
    3669             :         }
    3670         128 :         basicblock *cleanup_body = compiler_new_block(c);
    3671         128 :         if (cleanup_body == NULL) {
    3672           0 :             return 0;
    3673             :         }
    3674             : 
    3675         128 :         if (handler->v.ExceptHandler.name) {
    3676          53 :             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
    3677             :         }
    3678             :         else {
    3679          75 :             ADDOP(c, POP_TOP);  // match
    3680             :         }
    3681             : 
    3682             :         /*
    3683             :           try:
    3684             :               # body
    3685             :           except type as name:
    3686             :               try:
    3687             :                   # body
    3688             :               finally:
    3689             :                   name = None # in case body contains "del name"
    3690             :                   del name
    3691             :         */
    3692             :         /* second try: */
    3693         128 :         ADDOP_JUMP(c, SETUP_CLEANUP, cleanup_end);
    3694         128 :         compiler_use_next_block(c, cleanup_body);
    3695         128 :         if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
    3696           0 :             return 0;
    3697             : 
    3698             :         /* second # body */
    3699         268 :         VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
    3700         120 :         compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
    3701             :         /* name = None; del name; # Mark as artificial */
    3702         120 :         UNSET_LOC(c);
    3703         120 :         ADDOP(c, POP_BLOCK);
    3704         120 :         if (handler->v.ExceptHandler.name) {
    3705          45 :             ADDOP_LOAD_CONST(c, Py_None);
    3706          45 :             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
    3707          45 :             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
    3708             :         }
    3709         120 :         ADDOP_JUMP(c, JUMP, except);
    3710             : 
    3711             :         /* except: */
    3712         120 :         compiler_use_next_block(c, cleanup_end);
    3713             : 
    3714             :         /* name = None; del name; # Mark as artificial */
    3715         120 :         UNSET_LOC(c);
    3716             : 
    3717         120 :         if (handler->v.ExceptHandler.name) {
    3718          45 :             ADDOP_LOAD_CONST(c, Py_None);
    3719          45 :             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
    3720          45 :             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
    3721             :         }
    3722             : 
    3723             :         /* add exception raised to the res list */
    3724         120 :         ADDOP_I(c, LIST_APPEND, 3); // exc
    3725         120 :         ADDOP(c, POP_TOP); // lasti
    3726             : 
    3727         120 :         ADDOP_JUMP(c, JUMP, except);
    3728         120 :         compiler_use_next_block(c, except);
    3729             : 
    3730         120 :         if (i == n - 1) {
    3731             :             /* Add exc to the list (if not None it's the unhandled part of the EG) */
    3732         100 :             ADDOP_I(c, LIST_APPEND, 1);
    3733         100 :             ADDOP_JUMP(c, JUMP, reraise_star);
    3734             :         }
    3735             :     }
    3736             :     /* Mark as artificial */
    3737         100 :     UNSET_LOC(c);
    3738         100 :     compiler_pop_fblock(c, EXCEPTION_GROUP_HANDLER, NULL);
    3739         100 :     basicblock *reraise = compiler_new_block(c);
    3740         100 :     if (!reraise) {
    3741           0 :         return 0;
    3742             :     }
    3743             : 
    3744         100 :     compiler_use_next_block(c, reraise_star);
    3745         100 :     ADDOP(c, PREP_RERAISE_STAR);
    3746         100 :     ADDOP_I(c, COPY, 1);
    3747         100 :     ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, reraise);
    3748             : 
    3749             :     /* Nothing to reraise */
    3750         100 :     ADDOP(c, POP_TOP);
    3751         100 :     ADDOP(c, POP_BLOCK);
    3752         100 :     ADDOP(c, POP_EXCEPT);
    3753         100 :     ADDOP_JUMP(c, JUMP, end);
    3754         100 :     compiler_use_next_block(c, reraise);
    3755         100 :     ADDOP(c, POP_BLOCK);
    3756         100 :     ADDOP_I(c, SWAP, 2);
    3757         100 :     ADDOP(c, POP_EXCEPT);
    3758         100 :     ADDOP_I(c, RERAISE, 0);
    3759         100 :     compiler_use_next_block(c, cleanup);
    3760         100 :     POP_EXCEPT_AND_RERAISE(c);
    3761         100 :     compiler_use_next_block(c, orelse);
    3762         144 :     VISIT_SEQ(c, stmt, s->v.TryStar.orelse);
    3763         100 :     compiler_use_next_block(c, end);
    3764         100 :     return 1;
    3765             : }
    3766             : 
    3767             : static int
    3768       40246 : compiler_try(struct compiler *c, stmt_ty s) {
    3769       40246 :     if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
    3770        5933 :         return compiler_try_finally(c, s);
    3771             :     else
    3772       34313 :         return compiler_try_except(c, s);
    3773             : }
    3774             : 
    3775             : static int
    3776         108 : compiler_try_star(struct compiler *c, stmt_ty s)
    3777             : {
    3778         108 :     if (s->v.TryStar.finalbody && asdl_seq_LEN(s->v.TryStar.finalbody)) {
    3779          25 :         return compiler_try_star_finally(c, s);
    3780             :     }
    3781             :     else {
    3782          83 :         return compiler_try_star_except(c, s);
    3783             :     }
    3784             : }
    3785             : 
    3786             : static int
    3787        1320 : compiler_import_as(struct compiler *c, identifier name, identifier asname)
    3788             : {
    3789             :     /* The IMPORT_NAME opcode was already generated.  This function
    3790             :        merely needs to bind the result to a name.
    3791             : 
    3792             :        If there is a dot in name, we need to split it and emit a
    3793             :        IMPORT_FROM for each name.
    3794             :     */
    3795        1320 :     Py_ssize_t len = PyUnicode_GET_LENGTH(name);
    3796        1320 :     Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
    3797        1320 :     if (dot == -2)
    3798           0 :         return 0;
    3799        1320 :     if (dot != -1) {
    3800             :         /* Consume the base module name to get the first attribute */
    3801         129 :         while (1) {
    3802         376 :             Py_ssize_t pos = dot + 1;
    3803             :             PyObject *attr;
    3804         376 :             dot = PyUnicode_FindChar(name, '.', pos, len, 1);
    3805         376 :             if (dot == -2)
    3806           0 :                 return 0;
    3807         376 :             attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
    3808         376 :             if (!attr)
    3809           0 :                 return 0;
    3810         376 :             ADDOP_N(c, IMPORT_FROM, attr, names);
    3811         376 :             if (dot == -1) {
    3812         247 :                 break;
    3813             :             }
    3814         129 :             ADDOP_I(c, SWAP, 2);
    3815         129 :             ADDOP(c, POP_TOP);
    3816             :         }
    3817         247 :         if (!compiler_nameop(c, asname, Store)) {
    3818           0 :             return 0;
    3819             :         }
    3820         247 :         ADDOP(c, POP_TOP);
    3821         247 :         return 1;
    3822             :     }
    3823        1073 :     return compiler_nameop(c, asname, Store);
    3824             : }
    3825             : 
    3826             : static int
    3827       36461 : compiler_import(struct compiler *c, stmt_ty s)
    3828             : {
    3829             :     /* The Import node stores a module name like a.b.c as a single
    3830             :        string.  This is convenient for all cases except
    3831             :          import a.b.c as d
    3832             :        where we need to parse that string to extract the individual
    3833             :        module names.
    3834             :        XXX Perhaps change the representation to make this case simpler?
    3835             :      */
    3836       36461 :     Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
    3837             : 
    3838       36461 :     PyObject *zero = _PyLong_GetZero();  // borrowed reference
    3839       73986 :     for (i = 0; i < n; i++) {
    3840       37525 :         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
    3841             :         int r;
    3842             : 
    3843       37525 :         ADDOP_LOAD_CONST(c, zero);
    3844       37525 :         ADDOP_LOAD_CONST(c, Py_None);
    3845       37525 :         ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
    3846             : 
    3847       37525 :         if (alias->asname) {
    3848        1320 :             r = compiler_import_as(c, alias->name, alias->asname);
    3849        1320 :             if (!r)
    3850           0 :                 return r;
    3851             :         }
    3852             :         else {
    3853       36205 :             identifier tmp = alias->name;
    3854       36205 :             Py_ssize_t dot = PyUnicode_FindChar(
    3855       36205 :                 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
    3856       36205 :             if (dot != -1) {
    3857        2257 :                 tmp = PyUnicode_Substring(alias->name, 0, dot);
    3858        2257 :                 if (tmp == NULL)
    3859           0 :                     return 0;
    3860             :             }
    3861       36205 :             r = compiler_nameop(c, tmp, Store);
    3862       36205 :             if (dot != -1) {
    3863        2257 :                 Py_DECREF(tmp);
    3864             :             }
    3865       36205 :             if (!r)
    3866           0 :                 return r;
    3867             :         }
    3868             :     }
    3869       36461 :     return 1;
    3870             : }
    3871             : 
    3872             : static int
    3873       41616 : compiler_from_import(struct compiler *c, stmt_ty s)
    3874             : {
    3875       41616 :     Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
    3876             :     PyObject *names;
    3877             : 
    3878       41616 :     ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
    3879             : 
    3880       41616 :     names = PyTuple_New(n);
    3881       41616 :     if (!names)
    3882           0 :         return 0;
    3883             : 
    3884             :     /* build up the names */
    3885      113313 :     for (i = 0; i < n; i++) {
    3886       71697 :         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
    3887       71697 :         Py_INCREF(alias->name);
    3888       71697 :         PyTuple_SET_ITEM(names, i, alias->name);
    3889             :     }
    3890             : 
    3891       80964 :     if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
    3892       39348 :         _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
    3893           9 :         Py_DECREF(names);
    3894           9 :         return compiler_error(c, "from __future__ imports must occur "
    3895             :                               "at the beginning of the file");
    3896             :     }
    3897       41607 :     ADDOP_LOAD_CONST_NEW(c, names);
    3898             : 
    3899       41607 :     if (s->v.ImportFrom.module) {
    3900       40361 :         ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
    3901             :     }
    3902             :     else {
    3903             :         _Py_DECLARE_STR(empty, "");
    3904        1246 :         ADDOP_NAME(c, IMPORT_NAME, &_Py_STR(empty), names);
    3905             :     }
    3906      112276 :     for (i = 0; i < n; i++) {
    3907       71688 :         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
    3908             :         identifier store_name;
    3909             : 
    3910       71688 :         if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
    3911        1019 :             assert(n == 1);
    3912        1019 :             ADDOP(c, IMPORT_STAR);
    3913        1019 :             return 1;
    3914             :         }
    3915             : 
    3916       70669 :         ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
    3917       70669 :         store_name = alias->name;
    3918       70669 :         if (alias->asname)
    3919        2790 :             store_name = alias->asname;
    3920             : 
    3921       70669 :         if (!compiler_nameop(c, store_name, Store)) {
    3922           0 :             return 0;
    3923             :         }
    3924             :     }
    3925             :     /* remove imported module */
    3926       40588 :     ADDOP(c, POP_TOP);
    3927       40588 :     return 1;
    3928             : }
    3929             : 
    3930             : static int
    3931        8453 : compiler_assert(struct compiler *c, stmt_ty s)
    3932             : {
    3933             :     basicblock *end;
    3934             : 
    3935             :     /* Always emit a warning if the test is a non-zero length tuple */
    3936        8453 :     if ((s->v.Assert.test->kind == Tuple_kind &&
    3937           3 :         asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
    3938        8705 :         (s->v.Assert.test->kind == Constant_kind &&
    3939         261 :          PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
    3940           6 :          PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
    3941             :     {
    3942           9 :         if (!compiler_warn(c, "assertion is always true, "
    3943             :                               "perhaps remove parentheses?"))
    3944             :         {
    3945           6 :             return 0;
    3946             :         }
    3947             :     }
    3948        8447 :     if (c->c_optimize)
    3949         469 :         return 1;
    3950        7978 :     end = compiler_new_block(c);
    3951        7978 :     if (end == NULL)
    3952           0 :         return 0;
    3953        7978 :     if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
    3954           0 :         return 0;
    3955        7978 :     ADDOP(c, LOAD_ASSERTION_ERROR);
    3956        7978 :     if (s->v.Assert.msg) {
    3957        1833 :         VISIT(c, expr, s->v.Assert.msg);
    3958        1833 :         ADDOP_I(c, CALL, 0);
    3959             :     }
    3960        7978 :     ADDOP_I(c, RAISE_VARARGS, 1);
    3961        7978 :     compiler_use_next_block(c, end);
    3962        7978 :     return 1;
    3963             : }
    3964             : 
    3965             : static int
    3966      558991 : compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
    3967             : {
    3968      558991 :     if (c->c_interactive && c->c_nestlevel <= 1) {
    3969        2630 :         VISIT(c, expr, value);
    3970        2624 :         ADDOP(c, PRINT_EXPR);
    3971        2624 :         return 1;
    3972             :     }
    3973             : 
    3974      556361 :     if (value->kind == Constant_kind) {
    3975             :         /* ignore constant statement */
    3976        6808 :         ADDOP(c, NOP);
    3977        6808 :         return 1;
    3978             :     }
    3979             : 
    3980      549553 :     VISIT(c, expr, value);
    3981             :     /* Mark POP_TOP as artificial */
    3982      549426 :     UNSET_LOC(c);
    3983      549426 :     ADDOP(c, POP_TOP);
    3984      549426 :     return 1;
    3985             : }
    3986             : 
    3987             : static int
    3988     2524490 : compiler_visit_stmt(struct compiler *c, stmt_ty s)
    3989             : {
    3990             :     Py_ssize_t i, n;
    3991             : 
    3992             :     /* Always assign a lineno to the next instruction for a stmt. */
    3993     2524490 :     SET_LOC(c, s);
    3994             : 
    3995     2524490 :     switch (s->kind) {
    3996      258334 :     case FunctionDef_kind:
    3997      258334 :         return compiler_function(c, s, 0);
    3998       37741 :     case ClassDef_kind:
    3999       37741 :         return compiler_class(c, s);
    4000      212398 :     case Return_kind:
    4001      212398 :         return compiler_return(c, s);
    4002        6669 :     case Delete_kind:
    4003       14228 :         VISIT_SEQ(c, expr, s->v.Delete.targets)
    4004        6668 :         break;
    4005      637789 :     case Assign_kind:
    4006      637789 :         n = asdl_seq_LEN(s->v.Assign.targets);
    4007      637789 :         VISIT(c, expr, s->v.Assign.value);
    4008     1281970 :         for (i = 0; i < n; i++) {
    4009      644207 :             if (i < n - 1) {
    4010        6436 :                 ADDOP_I(c, COPY, 1);
    4011             :             }
    4012      644207 :             VISIT(c, expr,
    4013             :                   (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
    4014             :         }
    4015      637760 :         break;
    4016       21588 :     case AugAssign_kind:
    4017       21588 :         return compiler_augassign(c, s);
    4018        9866 :     case AnnAssign_kind:
    4019        9866 :         return compiler_annassign(c, s);
    4020       46446 :     case For_kind:
    4021       46446 :         return compiler_for(c, s);
    4022        9960 :     case While_kind:
    4023        9960 :         return compiler_while(c, s);
    4024      486138 :     case If_kind:
    4025      486138 :         return compiler_if(c, s);
    4026         679 :     case Match_kind:
    4027         679 :         return compiler_match(c, s);
    4028       52661 :     case Raise_kind:
    4029       52661 :         n = 0;
    4030       52661 :         if (s->v.Raise.exc) {
    4031       48653 :             VISIT(c, expr, s->v.Raise.exc);
    4032       48653 :             n++;
    4033       48653 :             if (s->v.Raise.cause) {
    4034        2503 :                 VISIT(c, expr, s->v.Raise.cause);
    4035        2503 :                 n++;
    4036             :             }
    4037             :         }
    4038       52661 :         ADDOP_I(c, RAISE_VARARGS, (int)n);
    4039       52661 :         break;
    4040       40246 :     case Try_kind:
    4041       40246 :         return compiler_try(c, s);
    4042         108 :     case TryStar_kind:
    4043         108 :         return compiler_try_star(c, s);
    4044        8453 :     case Assert_kind:
    4045        8453 :         return compiler_assert(c, s);
    4046       36461 :     case Import_kind:
    4047       36461 :         return compiler_import(c, s);
    4048       41616 :     case ImportFrom_kind:
    4049       41616 :         return compiler_from_import(c, s);
    4050        2066 :     case Global_kind:
    4051             :     case Nonlocal_kind:
    4052        2066 :         break;
    4053      558991 :     case Expr_kind:
    4054      558991 :         return compiler_visit_stmt_expr(c, s->v.Expr.value);
    4055       16873 :     case Pass_kind:
    4056       16873 :         ADDOP(c, NOP);
    4057       16873 :         break;
    4058        8573 :     case Break_kind:
    4059        8573 :         return compiler_break(c);
    4060        7537 :     case Continue_kind:
    4061        7537 :         return compiler_continue(c);
    4062       20653 :     case With_kind:
    4063       20653 :         return compiler_with(c, s, 0);
    4064        2334 :     case AsyncFunctionDef_kind:
    4065        2334 :         return compiler_function(c, s, 1);
    4066         227 :     case AsyncWith_kind:
    4067         227 :         return compiler_async_with(c, s, 0);
    4068          81 :     case AsyncFor_kind:
    4069          81 :         return compiler_async_for(c, s);
    4070             :     }
    4071             : 
    4072      716028 :     return 1;
    4073             : }
    4074             : 
    4075             : static int
    4076        7500 : unaryop(unaryop_ty op)
    4077             : {
    4078        7500 :     switch (op) {
    4079         745 :     case Invert:
    4080         745 :         return UNARY_INVERT;
    4081        2968 :     case Not:
    4082        2968 :         return UNARY_NOT;
    4083          88 :     case UAdd:
    4084          88 :         return UNARY_POSITIVE;
    4085        3699 :     case USub:
    4086        3699 :         return UNARY_NEGATIVE;
    4087           0 :     default:
    4088           0 :         PyErr_Format(PyExc_SystemError,
    4089             :             "unary op %d should not be possible", op);
    4090           0 :         return 0;
    4091             :     }
    4092             : }
    4093             : 
    4094             : static int
    4095      217650 : addop_binary(struct compiler *c, operator_ty binop, bool inplace)
    4096             : {
    4097             :     int oparg;
    4098      217650 :     switch (binop) {
    4099       88488 :         case Add:
    4100       88488 :             oparg = inplace ? NB_INPLACE_ADD : NB_ADD;
    4101       88488 :             break;
    4102       45446 :         case Sub:
    4103       45446 :             oparg = inplace ? NB_INPLACE_SUBTRACT : NB_SUBTRACT;
    4104       45446 :             break;
    4105       17859 :         case Mult:
    4106       17859 :             oparg = inplace ? NB_INPLACE_MULTIPLY : NB_MULTIPLY;
    4107       17859 :             break;
    4108          67 :         case MatMult:
    4109          67 :             oparg = inplace ? NB_INPLACE_MATRIX_MULTIPLY : NB_MATRIX_MULTIPLY;
    4110          67 :             break;
    4111        3681 :         case Div:
    4112        3681 :             oparg = inplace ? NB_INPLACE_TRUE_DIVIDE : NB_TRUE_DIVIDE;
    4113        3681 :             break;
    4114       42121 :         case Mod:
    4115       42121 :             oparg = inplace ? NB_INPLACE_REMAINDER : NB_REMAINDER;
    4116       42121 :             break;
    4117        1967 :         case Pow:
    4118        1967 :             oparg = inplace ? NB_INPLACE_POWER : NB_POWER;
    4119        1967 :             break;
    4120        1467 :         case LShift:
    4121        1467 :             oparg = inplace ? NB_INPLACE_LSHIFT : NB_LSHIFT;
    4122        1467 :             break;
    4123        1065 :         case RShift:
    4124        1065 :             oparg = inplace ? NB_INPLACE_RSHIFT : NB_RSHIFT;
    4125        1065 :             break;
    4126        6298 :         case BitOr:
    4127        6298 :             oparg = inplace ? NB_INPLACE_OR : NB_OR;
    4128        6298 :             break;
    4129         820 :         case BitXor:
    4130         820 :             oparg = inplace ? NB_INPLACE_XOR : NB_XOR;
    4131         820 :             break;
    4132        5592 :         case BitAnd:
    4133        5592 :             oparg = inplace ? NB_INPLACE_AND : NB_AND;
    4134        5592 :             break;
    4135        2779 :         case FloorDiv:
    4136        2779 :             oparg = inplace ? NB_INPLACE_FLOOR_DIVIDE : NB_FLOOR_DIVIDE;
    4137        2779 :             break;
    4138           0 :         default:
    4139           0 :             PyErr_Format(PyExc_SystemError, "%s op %d should not be possible",
    4140             :                          inplace ? "inplace" : "binary", binop);
    4141           0 :             return 0;
    4142             :     }
    4143      217650 :     ADDOP_I(c, BINARY_OP, oparg);
    4144      217650 :     return 1;
    4145             : }
    4146             : 
    4147             : 
    4148             : static int
    4149       19270 : addop_yield(struct compiler *c) {
    4150       19270 :     if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) {
    4151         223 :         ADDOP(c, ASYNC_GEN_WRAP);
    4152             :     }
    4153       19270 :     ADDOP_I(c, YIELD_VALUE, 0);
    4154       19270 :     ADDOP_I(c, RESUME, 1);
    4155       19270 :     return 1;
    4156             : }
    4157             : 
    4158             : static int
    4159     5280440 : compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
    4160             : {
    4161             :     int op, scope;
    4162             :     Py_ssize_t arg;
    4163             :     enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
    4164             : 
    4165     5280440 :     PyObject *dict = c->u->u_names;
    4166             :     PyObject *mangled;
    4167             : 
    4168     5280440 :     assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
    4169             :            !_PyUnicode_EqualToASCIIString(name, "True") &&
    4170             :            !_PyUnicode_EqualToASCIIString(name, "False"));
    4171             : 
    4172     5280440 :     if (forbidden_name(c, name, ctx))
    4173           7 :         return 0;
    4174             : 
    4175     5280440 :     mangled = _Py_Mangle(c->u->u_private, name);
    4176     5280440 :     if (!mangled)
    4177           0 :         return 0;
    4178             : 
    4179     5280440 :     op = 0;
    4180     5280440 :     optype = OP_NAME;
    4181     5280440 :     scope = _PyST_GetScope(c->u->u_ste, mangled);
    4182     5280440 :     switch (scope) {
    4183       37157 :     case FREE:
    4184       37157 :         dict = c->u->u_freevars;
    4185       37157 :         optype = OP_DEREF;
    4186       37157 :         break;
    4187       53442 :     case CELL:
    4188       53442 :         dict = c->u->u_cellvars;
    4189       53442 :         optype = OP_DEREF;
    4190       53442 :         break;
    4191     3602550 :     case LOCAL:
    4192     3602550 :         if (c->u->u_ste->ste_type == FunctionBlock)
    4193     2918020 :             optype = OP_FAST;
    4194     3602550 :         break;
    4195     1441200 :     case GLOBAL_IMPLICIT:
    4196     1441200 :         if (c->u->u_ste->ste_type == FunctionBlock)
    4197      758653 :             optype = OP_GLOBAL;
    4198     1441200 :         break;
    4199        6768 :     case GLOBAL_EXPLICIT:
    4200        6768 :         optype = OP_GLOBAL;
    4201        6768 :         break;
    4202      139325 :     default:
    4203             :         /* scope can be 0 */
    4204      139325 :         break;
    4205             :     }
    4206             : 
    4207             :     /* XXX Leave assert here, but handle __doc__ and the like better */
    4208     5280440 :     assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
    4209             : 
    4210     5280440 :     switch (optype) {
    4211       90599 :     case OP_DEREF:
    4212             :         switch (ctx) {
    4213       75138 :         case Load:
    4214       75138 :             op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
    4215       75138 :             break;
    4216       15451 :         case Store: op = STORE_DEREF; break;
    4217          10 :         case Del: op = DELETE_DEREF; break;
    4218             :         }
    4219       90599 :         break;
    4220     2918020 :     case OP_FAST:
    4221             :         switch (ctx) {
    4222     2326570 :         case Load: op = LOAD_FAST; break;
    4223      574899 :         case Store: op = STORE_FAST; break;
    4224       16546 :         case Del: op = DELETE_FAST; break;
    4225             :         }
    4226     2918020 :         ADDOP_N(c, op, mangled, varnames);
    4227     2918020 :         return 1;
    4228      765421 :     case OP_GLOBAL:
    4229             :         switch (ctx) {
    4230      761796 :         case Load: op = LOAD_GLOBAL; break;
    4231        3611 :         case Store: op = STORE_GLOBAL; break;
    4232          14 :         case Del: op = DELETE_GLOBAL; break;
    4233             :         }
    4234      765421 :         break;
    4235     1506400 :     case OP_NAME:
    4236             :         switch (ctx) {
    4237      911190 :         case Load: op = LOAD_NAME; break;
    4238      592648 :         case Store: op = STORE_NAME; break;
    4239        2564 :         case Del: op = DELETE_NAME; break;
    4240             :         }
    4241     1506400 :         break;
    4242             :     }
    4243             : 
    4244     2362420 :     assert(op);
    4245     2362420 :     arg = compiler_add_o(dict, mangled);
    4246     2362420 :     Py_DECREF(mangled);
    4247     2362420 :     if (arg < 0) {
    4248           0 :         return 0;
    4249             :     }
    4250     2362420 :     if (op == LOAD_GLOBAL) {
    4251      761796 :         arg <<= 1;
    4252             :     }
    4253     2362420 :     return compiler_addop_i(c, op, arg, true);
    4254             : }
    4255             : 
    4256             : static int
    4257       17368 : compiler_boolop(struct compiler *c, expr_ty e)
    4258             : {
    4259             :     basicblock *end;
    4260             :     int jumpi;
    4261             :     Py_ssize_t i, n;
    4262             :     asdl_expr_seq *s;
    4263             : 
    4264       17368 :     assert(e->kind == BoolOp_kind);
    4265       17368 :     if (e->v.BoolOp.op == And)
    4266        6027 :         jumpi = JUMP_IF_FALSE_OR_POP;
    4267             :     else
    4268       11341 :         jumpi = JUMP_IF_TRUE_OR_POP;
    4269       17368 :     end = compiler_new_block(c);
    4270       17368 :     if (end == NULL)
    4271           0 :         return 0;
    4272       17368 :     s = e->v.BoolOp.values;
    4273       17368 :     n = asdl_seq_LEN(s) - 1;
    4274       17368 :     assert(n >= 0);
    4275       37309 :     for (i = 0; i < n; ++i) {
    4276       19941 :         VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
    4277       19941 :         ADDOP_JUMP(c, jumpi, end);
    4278       19941 :         basicblock *next = compiler_new_block(c);
    4279       19941 :         if (next == NULL) {
    4280           0 :             return 0;
    4281             :         }
    4282       19941 :         compiler_use_next_block(c, next);
    4283             :     }
    4284       17368 :     VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
    4285       17368 :     compiler_use_next_block(c, end);
    4286       17368 :     return 1;
    4287             : }
    4288             : 
    4289             : static int
    4290      196746 : starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
    4291             :                   int build, int add, int extend, int tuple)
    4292             : {
    4293      196746 :     Py_ssize_t n = asdl_seq_LEN(elts);
    4294      196746 :     if (n > 2 && are_all_items_const(elts, 0, n)) {
    4295       10279 :         PyObject *folded = PyTuple_New(n);
    4296       10279 :         if (folded == NULL) {
    4297           0 :             return 0;
    4298             :         }
    4299             :         PyObject *val;
    4300      466984 :         for (Py_ssize_t i = 0; i < n; i++) {
    4301      456705 :             val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
    4302      456705 :             Py_INCREF(val);
    4303      456705 :             PyTuple_SET_ITEM(folded, i, val);
    4304             :         }
    4305       10279 :         if (tuple && !pushed) {
    4306          20 :             ADDOP_LOAD_CONST_NEW(c, folded);
    4307             :         } else {
    4308       10259 :             if (add == SET_ADD) {
    4309         434 :                 Py_SETREF(folded, PyFrozenSet_New(folded));
    4310         434 :                 if (folded == NULL) {
    4311           0 :                     return 0;
    4312             :                 }
    4313             :             }
    4314       10259 :             ADDOP_I(c, build, pushed);
    4315       10259 :             ADDOP_LOAD_CONST_NEW(c, folded);
    4316       10259 :             ADDOP_I(c, extend, 1);
    4317       10259 :             if (tuple) {
    4318           1 :                 ADDOP(c, LIST_TO_TUPLE);
    4319             :             }
    4320             :         }
    4321       10279 :         return 1;
    4322             :     }
    4323             : 
    4324      186467 :     int big = n+pushed > STACK_USE_GUIDELINE;
    4325      186467 :     int seen_star = 0;
    4326      608954 :     for (Py_ssize_t i = 0; i < n; i++) {
    4327      426520 :         expr_ty elt = asdl_seq_GET(elts, i);
    4328      426520 :         if (elt->kind == Starred_kind) {
    4329        4033 :             seen_star = 1;
    4330        4033 :             break;
    4331             :         }
    4332             :     }
    4333      186467 :     if (!seen_star && !big) {
    4334      573090 :         for (Py_ssize_t i = 0; i < n; i++) {
    4335      390982 :             expr_ty elt = asdl_seq_GET(elts, i);
    4336      390982 :             VISIT(c, expr, elt);
    4337             :         }
    4338      182108 :         if (tuple) {
    4339      127648 :             ADDOP_I(c, BUILD_TUPLE, n+pushed);
    4340             :         } else {
    4341       54460 :             ADDOP_I(c, build, n+pushed);
    4342             :         }
    4343      182108 :         return 1;
    4344             :     }
    4345        4304 :     int sequence_built = 0;
    4346        4304 :     if (big) {
    4347         271 :         ADDOP_I(c, build, pushed);
    4348         271 :         sequence_built = 1;
    4349             :     }
    4350       40716 :     for (Py_ssize_t i = 0; i < n; i++) {
    4351       36412 :         expr_ty elt = asdl_seq_GET(elts, i);
    4352       36412 :         if (elt->kind == Starred_kind) {
    4353        4317 :             if (sequence_built == 0) {
    4354        4033 :                 ADDOP_I(c, build, i+pushed);
    4355        4033 :                 sequence_built = 1;
    4356             :             }
    4357        4317 :             VISIT(c, expr, elt->v.Starred.value);
    4358        4317 :             ADDOP_I(c, extend, 1);
    4359             :         }
    4360             :         else {
    4361       32095 :             VISIT(c, expr, elt);
    4362       32095 :             if (sequence_built) {
    4363       27406 :                 ADDOP_I(c, add, 1);
    4364             :             }
    4365             :         }
    4366             :     }
    4367        4304 :     assert(sequence_built);
    4368        4304 :     if (tuple) {
    4369        3957 :         ADDOP(c, LIST_TO_TUPLE);
    4370             :     }
    4371        4304 :     return 1;
    4372             : }
    4373             : 
    4374             : static int
    4375       42322 : unpack_helper(struct compiler *c, asdl_expr_seq *elts)
    4376             : {
    4377       42322 :     Py_ssize_t n = asdl_seq_LEN(elts);
    4378       42322 :     int seen_star = 0;
    4379      146728 :     for (Py_ssize_t i = 0; i < n; i++) {
    4380      104411 :         expr_ty elt = asdl_seq_GET(elts, i);
    4381      104411 :         if (elt->kind == Starred_kind && !seen_star) {
    4382         203 :             if ((i >= (1 << 8)) ||
    4383         201 :                 (n-i-1 >= (INT_MAX >> 8)))
    4384           2 :                 return compiler_error(c,
    4385             :                     "too many expressions in "
    4386             :                     "star-unpacking assignment");
    4387         201 :             ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
    4388         201 :             seen_star = 1;
    4389             :         }
    4390      104208 :         else if (elt->kind == Starred_kind) {
    4391           3 :             return compiler_error(c,
    4392             :                 "multiple starred expressions in assignment");
    4393             :         }
    4394             :     }
    4395       42317 :     if (!seen_star) {
    4396       42119 :         ADDOP_I(c, UNPACK_SEQUENCE, n);
    4397             :     }
    4398       42317 :     return 1;
    4399             : }
    4400             : 
    4401             : static int
    4402       42322 : assignment_helper(struct compiler *c, asdl_expr_seq *elts)
    4403             : {
    4404       42322 :     Py_ssize_t n = asdl_seq_LEN(elts);
    4405       42322 :     RETURN_IF_FALSE(unpack_helper(c, elts));
    4406      145945 :     for (Py_ssize_t i = 0; i < n; i++) {
    4407      103630 :         expr_ty elt = asdl_seq_GET(elts, i);
    4408      103630 :         VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
    4409             :     }
    4410       42315 :     return 1;
    4411             : }
    4412             : 
    4413             : static int
    4414       63421 : compiler_list(struct compiler *c, expr_ty e)
    4415             : {
    4416       63421 :     asdl_expr_seq *elts = e->v.List.elts;
    4417       63421 :     if (e->v.List.ctx == Store) {
    4418         104 :         return assignment_helper(c, elts);
    4419             :     }
    4420       63317 :     else if (e->v.List.ctx == Load) {
    4421       63312 :         return starunpack_helper(c, elts, 0, BUILD_LIST,
    4422             :                                  LIST_APPEND, LIST_EXTEND, 0);
    4423             :     }
    4424             :     else
    4425          14 :         VISIT_SEQ(c, expr, elts);
    4426           5 :     return 1;
    4427             : }
    4428             : 
    4429             : static int
    4430      166950 : compiler_tuple(struct compiler *c, expr_ty e)
    4431             : {
    4432      166950 :     asdl_expr_seq *elts = e->v.Tuple.elts;
    4433      166950 :     if (e->v.Tuple.ctx == Store) {
    4434       42218 :         return assignment_helper(c, elts);
    4435             :     }
    4436      124732 :     else if (e->v.Tuple.ctx == Load) {
    4437      124706 :         return starunpack_helper(c, elts, 0, BUILD_LIST,
    4438             :                                  LIST_APPEND, LIST_EXTEND, 1);
    4439             :     }
    4440             :     else
    4441         512 :         VISIT_SEQ(c, expr, elts);
    4442          26 :     return 1;
    4443             : }
    4444             : 
    4445             : static int
    4446        1807 : compiler_set(struct compiler *c, expr_ty e)
    4447             : {
    4448        1807 :     return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
    4449             :                              SET_ADD, SET_UPDATE, 0);
    4450             : }
    4451             : 
    4452             : static int
    4453       77993 : are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
    4454             : {
    4455             :     Py_ssize_t i;
    4456      636464 :     for (i = begin; i < end; i++) {
    4457      611487 :         expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
    4458      611487 :         if (key == NULL || key->kind != Constant_kind)
    4459       53016 :             return 0;
    4460             :     }
    4461       24977 :     return 1;
    4462             : }
    4463             : 
    4464             : static int
    4465       50961 : compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
    4466             : {
    4467       50961 :     Py_ssize_t i, n = end - begin;
    4468             :     PyObject *keys, *key;
    4469       50961 :     int big = n*2 > STACK_USE_GUIDELINE;
    4470       50961 :     if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
    4471      106048 :         for (i = begin; i < end; i++) {
    4472       91350 :             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
    4473             :         }
    4474       14698 :         keys = PyTuple_New(n);
    4475       14698 :         if (keys == NULL) {
    4476           0 :             return 0;
    4477             :         }
    4478      106048 :         for (i = begin; i < end; i++) {
    4479       91350 :             key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
    4480       91350 :             Py_INCREF(key);
    4481       91350 :             PyTuple_SET_ITEM(keys, i - begin, key);
    4482             :         }
    4483       14698 :         ADDOP_LOAD_CONST_NEW(c, keys);
    4484       14698 :         ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
    4485       14698 :         return 1;
    4486             :     }
    4487       36263 :     if (big) {
    4488       30442 :         ADDOP_I(c, BUILD_MAP, 0);
    4489             :     }
    4490      561304 :     for (i = begin; i < end; i++) {
    4491      525041 :         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
    4492      525041 :         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
    4493      525041 :         if (big) {
    4494      517397 :             ADDOP_I(c, MAP_ADD, 1);
    4495             :         }
    4496             :     }
    4497       36263 :     if (!big) {
    4498        5821 :         ADDOP_I(c, BUILD_MAP, n);
    4499             :     }
    4500       36263 :     return 1;
    4501             : }
    4502             : 
    4503             : static int
    4504       32250 : compiler_dict(struct compiler *c, expr_ty e)
    4505             : {
    4506             :     Py_ssize_t i, n, elements;
    4507             :     int have_dict;
    4508       32250 :     int is_unpacking = 0;
    4509       32250 :     n = asdl_seq_LEN(e->v.Dict.values);
    4510       32250 :     have_dict = 0;
    4511       32250 :     elements = 0;
    4512      649924 :     for (i = 0; i < n; i++) {
    4513      617674 :         is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
    4514      617674 :         if (is_unpacking) {
    4515        1283 :             if (elements) {
    4516           7 :                 if (!compiler_subdict(c, e, i - elements, i)) {
    4517           0 :                     return 0;
    4518             :                 }
    4519           7 :                 if (have_dict) {
    4520           2 :                     ADDOP_I(c, DICT_UPDATE, 1);
    4521             :                 }
    4522           7 :                 have_dict = 1;
    4523           7 :                 elements = 0;
    4524             :             }
    4525        1283 :             if (have_dict == 0) {
    4526         148 :                 ADDOP_I(c, BUILD_MAP, 0);
    4527         148 :                 have_dict = 1;
    4528             :             }
    4529        1283 :             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
    4530        1283 :             ADDOP_I(c, DICT_UPDATE, 1);
    4531             :         }
    4532             :         else {
    4533      616391 :             if (elements*2 > STACK_USE_GUIDELINE) {
    4534       30325 :                 if (!compiler_subdict(c, e, i - elements, i + 1)) {
    4535           0 :                     return 0;
    4536             :                 }
    4537       30325 :                 if (have_dict) {
    4538       24644 :                     ADDOP_I(c, DICT_UPDATE, 1);
    4539             :                 }
    4540       30325 :                 have_dict = 1;
    4541       30325 :                 elements = 0;
    4542             :             }
    4543             :             else {
    4544      586066 :                 elements++;
    4545             :             }
    4546             :         }
    4547             :     }
    4548       32250 :     if (elements) {
    4549       20629 :         if (!compiler_subdict(c, e, n - elements, n)) {
    4550           0 :             return 0;
    4551             :         }
    4552       20629 :         if (have_dict) {
    4553        5651 :             ADDOP_I(c, DICT_UPDATE, 1);
    4554             :         }
    4555       20629 :         have_dict = 1;
    4556             :     }
    4557       32250 :     if (!have_dict) {
    4558       11438 :         ADDOP_I(c, BUILD_MAP, 0);
    4559             :     }
    4560       32250 :     return 1;
    4561             : }
    4562             : 
    4563             : static int
    4564      210496 : compiler_compare(struct compiler *c, expr_ty e)
    4565             : {
    4566             :     Py_ssize_t i, n;
    4567             : 
    4568      210496 :     if (!check_compare(c, e)) {
    4569           7 :         return 0;
    4570             :     }
    4571      210489 :     VISIT(c, expr, e->v.Compare.left);
    4572      210489 :     assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
    4573      210489 :     n = asdl_seq_LEN(e->v.Compare.ops) - 1;
    4574      210489 :     if (n == 0) {
    4575      210211 :         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
    4576      210209 :         ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
    4577             :     }
    4578             :     else {
    4579         278 :         basicblock *cleanup = compiler_new_block(c);
    4580         278 :         if (cleanup == NULL)
    4581           0 :             return 0;
    4582         675 :         for (i = 0; i < n; i++) {
    4583         397 :             VISIT(c, expr,
    4584             :                 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
    4585         397 :             ADDOP_I(c, SWAP, 2);
    4586         397 :             ADDOP_I(c, COPY, 2);
    4587         397 :             ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
    4588         397 :             ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
    4589             :         }
    4590         278 :         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
    4591         278 :         ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
    4592         278 :         basicblock *end = compiler_new_block(c);
    4593         278 :         if (end == NULL)
    4594           0 :             return 0;
    4595         278 :         ADDOP_JUMP_NOLINE(c, JUMP, end);
    4596         278 :         compiler_use_next_block(c, cleanup);
    4597         278 :         ADDOP_I(c, SWAP, 2);
    4598         278 :         ADDOP(c, POP_TOP);
    4599         278 :         compiler_use_next_block(c, end);
    4600             :     }
    4601      210487 :     return 1;
    4602             : }
    4603             : 
    4604             : static PyTypeObject *
    4605      152320 : infer_type(expr_ty e)
    4606             : {
    4607      152320 :     switch (e->kind) {
    4608       12365 :     case Tuple_kind:
    4609       12365 :         return &PyTuple_Type;
    4610          54 :     case List_kind:
    4611             :     case ListComp_kind:
    4612          54 :         return &PyList_Type;
    4613           9 :     case Dict_kind:
    4614             :     case DictComp_kind:
    4615           9 :         return &PyDict_Type;
    4616          12 :     case Set_kind:
    4617             :     case SetComp_kind:
    4618          12 :         return &PySet_Type;
    4619           6 :     case GeneratorExp_kind:
    4620           6 :         return &PyGen_Type;
    4621           4 :     case Lambda_kind:
    4622           4 :         return &PyFunction_Type;
    4623          14 :     case JoinedStr_kind:
    4624             :     case FormattedValue_kind:
    4625          14 :         return &PyUnicode_Type;
    4626       64779 :     case Constant_kind:
    4627       64779 :         return Py_TYPE(e->v.Constant.value);
    4628       75077 :     default:
    4629       75077 :         return NULL;
    4630             :     }
    4631             : }
    4632             : 
    4633             : static int
    4634      594412 : check_caller(struct compiler *c, expr_ty e)
    4635             : {
    4636      594412 :     switch (e->kind) {
    4637          38 :     case Constant_kind:
    4638             :     case Tuple_kind:
    4639             :     case List_kind:
    4640             :     case ListComp_kind:
    4641             :     case Dict_kind:
    4642             :     case DictComp_kind:
    4643             :     case Set_kind:
    4644             :     case SetComp_kind:
    4645             :     case GeneratorExp_kind:
    4646             :     case JoinedStr_kind:
    4647             :     case FormattedValue_kind:
    4648          38 :         return compiler_warn(c, "'%.200s' object is not callable; "
    4649             :                                 "perhaps you missed a comma?",
    4650          38 :                                 infer_type(e)->tp_name);
    4651      594374 :     default:
    4652      594374 :         return 1;
    4653             :     }
    4654             : }
    4655             : 
    4656             : static int
    4657      152222 : check_subscripter(struct compiler *c, expr_ty e)
    4658             : {
    4659             :     PyObject *v;
    4660             : 
    4661      152222 :     switch (e->kind) {
    4662         101 :     case Constant_kind:
    4663         101 :         v = e->v.Constant.value;
    4664         287 :         if (!(v == Py_None || v == Py_Ellipsis ||
    4665         190 :               PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
    4666         178 :               PyAnySet_Check(v)))
    4667             :         {
    4668          89 :             return 1;
    4669             :         }
    4670             :         /* fall through */
    4671             :     case Set_kind:
    4672             :     case SetComp_kind:
    4673             :     case GeneratorExp_kind:
    4674             :     case Lambda_kind:
    4675          20 :         return compiler_warn(c, "'%.200s' object is not subscriptable; "
    4676             :                                 "perhaps you missed a comma?",
    4677          20 :                                 infer_type(e)->tp_name);
    4678      152113 :     default:
    4679      152113 :         return 1;
    4680             :     }
    4681             : }
    4682             : 
    4683             : static int
    4684      152212 : check_index(struct compiler *c, expr_ty e, expr_ty s)
    4685             : {
    4686             :     PyObject *v;
    4687             : 
    4688      152212 :     PyTypeObject *index_type = infer_type(s);
    4689      152212 :     if (index_type == NULL
    4690       77135 :         || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
    4691       33876 :         || index_type == &PySlice_Type) {
    4692      118336 :         return 1;
    4693             :     }
    4694             : 
    4695       33876 :     switch (e->kind) {
    4696          12 :     case Constant_kind:
    4697          12 :         v = e->v.Constant.value;
    4698          12 :         if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
    4699           6 :             return 1;
    4700             :         }
    4701             :         /* fall through */
    4702             :     case Tuple_kind:
    4703             :     case List_kind:
    4704             :     case ListComp_kind:
    4705             :     case JoinedStr_kind:
    4706             :     case FormattedValue_kind:
    4707          50 :         return compiler_warn(c, "%.200s indices must be integers or slices, "
    4708             :                                 "not %.200s; "
    4709             :                                 "perhaps you missed a comma?",
    4710          50 :                                 infer_type(e)->tp_name,
    4711             :                                 index_type->tp_name);
    4712       33820 :     default:
    4713       33820 :         return 1;
    4714             :     }
    4715             : }
    4716             : 
    4717             : static int
    4718      633656 : is_import_originated(struct compiler *c, expr_ty e)
    4719             : {
    4720             :     /* Check whether the global scope has an import named
    4721             :      e, if it is a Name object. For not traversing all the
    4722             :      scope stack every time this function is called, it will
    4723             :      only check the global scope to determine whether something
    4724             :      is imported or not. */
    4725             : 
    4726      633656 :     if (e->kind != Name_kind) {
    4727      143614 :         return 0;
    4728             :     }
    4729             : 
    4730      490042 :     long flags = _PyST_GetSymbol(c->c_st->st_top, e->v.Name.id);
    4731      490042 :     return flags & DEF_IMPORT;
    4732             : }
    4733             : 
    4734             : static void
    4735     1067320 : update_location_to_match_attr(struct compiler *c, expr_ty meth)
    4736             : {
    4737     1067320 :     if (meth->lineno != meth->end_lineno) {
    4738             :         // Make start location match attribute
    4739        6266 :         c->u->u_loc.lineno = meth->end_lineno;
    4740        6266 :         c->u->u_loc.col_offset = meth->end_col_offset - (int)PyUnicode_GetLength(meth->v.Attribute.attr)-1;
    4741             :     }
    4742     1067320 : }
    4743             : 
    4744             : // Return 1 if the method call was optimized, -1 if not, and 0 on error.
    4745             : static int
    4746     1128070 : maybe_optimize_method_call(struct compiler *c, expr_ty e)
    4747             : {
    4748             :     Py_ssize_t argsl, i, kwdsl;
    4749     1128070 :     expr_ty meth = e->v.Call.func;
    4750     1128070 :     asdl_expr_seq *args = e->v.Call.args;
    4751     1128070 :     asdl_keyword_seq *kwds = e->v.Call.keywords;
    4752             : 
    4753             :     /* Check that the call node is an attribute access */
    4754     1128070 :     if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load) {
    4755      494414 :         return -1;
    4756             :     }
    4757             : 
    4758             :     /* Check that the base object is not something that is imported */
    4759      633656 :     if (is_import_originated(c, meth->v.Attribute.value)) {
    4760       94425 :         return -1;
    4761             :     }
    4762             : 
    4763             :     /* Check that there aren't too many arguments */
    4764      539231 :     argsl = asdl_seq_LEN(args);
    4765      539231 :     kwdsl = asdl_seq_LEN(kwds);
    4766      539231 :     if (argsl + kwdsl + (kwdsl != 0) >= STACK_USE_GUIDELINE) {
    4767           3 :         return -1;
    4768             :     }
    4769             :     /* Check that there are no *varargs types of arguments. */
    4770     1183720 :     for (i = 0; i < argsl; i++) {
    4771      648231 :         expr_ty elt = asdl_seq_GET(args, i);
    4772      648231 :         if (elt->kind == Starred_kind) {
    4773        3736 :             return -1;
    4774             :         }
    4775             :     }
    4776             : 
    4777      578722 :     for (i = 0; i < kwdsl; i++) {
    4778       45064 :         keyword_ty kw = asdl_seq_GET(kwds, i);
    4779       45064 :         if (kw->arg == NULL) {
    4780        1834 :             return -1;
    4781             :         }
    4782             :     }
    4783             :     /* Alright, we can optimize the code. */
    4784      533658 :     VISIT(c, expr, meth->v.Attribute.value);
    4785      533658 :     SET_LOC(c, meth);
    4786      533658 :     update_location_to_match_attr(c, meth);
    4787      533658 :     ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
    4788     1173430 :     VISIT_SEQ(c, expr, e->v.Call.args);
    4789             : 
    4790      533658 :     if (kwdsl) {
    4791       67662 :         VISIT_SEQ(c, keyword, kwds);
    4792       25335 :         if (!compiler_call_simple_kw_helper(c, kwds, kwdsl)) {
    4793           0 :             return 0;
    4794             :         };
    4795             :     }
    4796      533658 :     SET_LOC(c, e);
    4797      533658 :     update_location_to_match_attr(c, meth);
    4798      533658 :     ADDOP_I(c, CALL, argsl + kwdsl);
    4799      533658 :     return 1;
    4800             : }
    4801             : 
    4802             : static int
    4803     1760190 : validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
    4804             : {
    4805     1760190 :     Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
    4806     1986550 :     for (Py_ssize_t i = 0; i < nkeywords; i++) {
    4807      226364 :         keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
    4808      226364 :         if (key->arg == NULL) {
    4809       14170 :             continue;
    4810             :         }
    4811      212194 :         if (forbidden_name(c, key->arg, Store)) {
    4812           1 :             return -1;
    4813             :         }
    4814      574980 :         for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
    4815      362792 :             keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
    4816      362792 :             if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
    4817           5 :                 SET_LOC(c, other);
    4818           5 :                 compiler_error(c, "keyword argument repeated: %U", key->arg);
    4819           5 :                 return -1;
    4820             :             }
    4821             :         }
    4822             :     }
    4823     1760190 :     return 0;
    4824             : }
    4825             : 
    4826             : static int
    4827     1128080 : compiler_call(struct compiler *c, expr_ty e)
    4828             : {
    4829     1128080 :     if (validate_keywords(c, e->v.Call.keywords) == -1) {
    4830           5 :         return 0;
    4831             :     }
    4832     1128070 :     int ret = maybe_optimize_method_call(c, e);
    4833     1128070 :     if (ret >= 0) {
    4834      533658 :         return ret;
    4835             :     }
    4836      594412 :     if (!check_caller(c, e->v.Call.func)) {
    4837          19 :         return 0;
    4838             :     }
    4839      594393 :     SET_LOC(c, e->v.Call.func);
    4840      594393 :     ADDOP(c, PUSH_NULL);
    4841      594393 :     SET_LOC(c, e);
    4842      594393 :     VISIT(c, expr, e->v.Call.func);
    4843      594393 :     return compiler_call_helper(c, 0,
    4844             :                                 e->v.Call.args,
    4845             :                                 e->v.Call.keywords);
    4846             : }
    4847             : 
    4848             : static int
    4849       30390 : compiler_joined_str(struct compiler *c, expr_ty e)
    4850             : {
    4851             : 
    4852       30390 :     Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
    4853       30390 :     if (value_count > STACK_USE_GUIDELINE) {
    4854             :         _Py_DECLARE_STR(empty, "");
    4855          24 :         ADDOP_LOAD_CONST_NEW(c, &_Py_STR(empty));
    4856          24 :         ADDOP_NAME(c, LOAD_METHOD, &_Py_ID(join), names);
    4857          24 :         ADDOP_I(c, BUILD_LIST, 0);
    4858      139097 :         for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
    4859      139073 :             VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
    4860      139073 :             ADDOP_I(c, LIST_APPEND, 1);
    4861             :         }
    4862          24 :         ADDOP_I(c, CALL, 1);
    4863             :     }
    4864             :     else {
    4865      126303 :         VISIT_SEQ(c, expr, e->v.JoinedStr.values);
    4866       30366 :         if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
    4867       24949 :             ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
    4868             :         }
    4869             :     }
    4870       30390 :     return 1;
    4871             : }
    4872             : 
    4873             : /* Used to implement f-strings. Format a single value. */
    4874             : static int
    4875      118027 : compiler_formatted_value(struct compiler *c, expr_ty e)
    4876             : {
    4877             :     /* Our oparg encodes 2 pieces of information: the conversion
    4878             :        character, and whether or not a format_spec was provided.
    4879             : 
    4880             :        Convert the conversion char to 3 bits:
    4881             :            : 000  0x0  FVC_NONE   The default if nothing specified.
    4882             :        !s  : 001  0x1  FVC_STR
    4883             :        !r  : 010  0x2  FVC_REPR
    4884             :        !a  : 011  0x3  FVC_ASCII
    4885             : 
    4886             :        next bit is whether or not we have a format spec:
    4887             :        yes : 100  0x4
    4888             :        no  : 000  0x0
    4889             :     */
    4890             : 
    4891      118027 :     int conversion = e->v.FormattedValue.conversion;
    4892             :     int oparg;
    4893             : 
    4894             :     /* The expression to be formatted. */
    4895      118027 :     VISIT(c, expr, e->v.FormattedValue.value);
    4896             : 
    4897      118027 :     switch (conversion) {
    4898       18515 :     case 's': oparg = FVC_STR;   break;
    4899       12655 :     case 'r': oparg = FVC_REPR;  break;
    4900        1404 :     case 'a': oparg = FVC_ASCII; break;
    4901       85453 :     case -1:  oparg = FVC_NONE;  break;
    4902           0 :     default:
    4903           0 :         PyErr_Format(PyExc_SystemError,
    4904             :                      "Unrecognized conversion character %d", conversion);
    4905           0 :         return 0;
    4906             :     }
    4907      118027 :     if (e->v.FormattedValue.format_spec) {
    4908             :         /* Evaluate the format spec, and update our opcode arg. */
    4909        4420 :         VISIT(c, expr, e->v.FormattedValue.format_spec);
    4910        4420 :         oparg |= FVS_HAVE_SPEC;
    4911             :     }
    4912             : 
    4913             :     /* And push our opcode and oparg */
    4914      118027 :     ADDOP_I(c, FORMAT_VALUE, oparg);
    4915             : 
    4916      118027 :     return 1;
    4917             : }
    4918             : 
    4919             : static int
    4920        1378 : compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
    4921             : {
    4922        1378 :     Py_ssize_t i, n = end - begin;
    4923             :     keyword_ty kw;
    4924             :     PyObject *keys, *key;
    4925        1378 :     assert(n > 0);
    4926        1378 :     int big = n*2 > STACK_USE_GUIDELINE;
    4927        1378 :     if (n > 1 && !big) {
    4928        2830 :         for (i = begin; i < end; i++) {
    4929        2216 :             kw = asdl_seq_GET(keywords, i);
    4930        2216 :             VISIT(c, expr, kw->value);
    4931             :         }
    4932         614 :         keys = PyTuple_New(n);
    4933         614 :         if (keys == NULL) {
    4934           0 :             return 0;
    4935             :         }
    4936        2830 :         for (i = begin; i < end; i++) {
    4937        2216 :             key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
    4938        2216 :             Py_INCREF(key);
    4939        2216 :             PyTuple_SET_ITEM(keys, i - begin, key);
    4940             :         }
    4941         614 :         ADDOP_LOAD_CONST_NEW(c, keys);
    4942         614 :         ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
    4943         614 :         return 1;
    4944             :     }
    4945         764 :     if (big) {
    4946          16 :         ADDOP_I_NOLINE(c, BUILD_MAP, 0);
    4947             :     }
    4948        2274 :     for (i = begin; i < end; i++) {
    4949        1510 :         kw = asdl_seq_GET(keywords, i);
    4950        1510 :         ADDOP_LOAD_CONST(c, kw->arg);
    4951        1510 :         VISIT(c, expr, kw->value);
    4952        1510 :         if (big) {
    4953         762 :             ADDOP_I_NOLINE(c, MAP_ADD, 1);
    4954             :         }
    4955             :     }
    4956         764 :     if (!big) {
    4957         748 :         ADDOP_I(c, BUILD_MAP, n);
    4958             :     }
    4959         764 :     return 1;
    4960             : }
    4961             : 
    4962             : /* Used by compiler_call_helper and maybe_optimize_method_call to emit
    4963             :  * KW_NAMES before CALL.
    4964             :  * Returns 1 on success, 0 on error.
    4965             :  */
    4966             : static int
    4967       68012 : compiler_call_simple_kw_helper(struct compiler *c,
    4968             :                                asdl_keyword_seq *keywords,
    4969             :                                Py_ssize_t nkwelts)
    4970             : {
    4971             :     PyObject *names;
    4972       68012 :     names = PyTuple_New(nkwelts);
    4973       68012 :     if (names == NULL) {
    4974           0 :         return 0;
    4975             :     }
    4976      192110 :     for (int i = 0; i < nkwelts; i++) {
    4977      124098 :         keyword_ty kw = asdl_seq_GET(keywords, i);
    4978      124098 :         Py_INCREF(kw->arg);
    4979      124098 :         PyTuple_SET_ITEM(names, i, kw->arg);
    4980             :     }
    4981       68012 :     Py_ssize_t arg = compiler_add_const(c, names);
    4982       68012 :     if (arg < 0) {
    4983           0 :         return 0;
    4984             :     }
    4985       68012 :     Py_DECREF(names);
    4986       68012 :     ADDOP_I(c, KW_NAMES, arg);
    4987       68012 :     return 1;
    4988             : }
    4989             : 
    4990             : 
    4991             : /* shared code between compiler_call and compiler_class */
    4992             : static int
    4993      632117 : compiler_call_helper(struct compiler *c,
    4994             :                      int n, /* Args already pushed */
    4995             :                      asdl_expr_seq *args,
    4996             :                      asdl_keyword_seq *keywords)
    4997             : {
    4998             :     Py_ssize_t i, nseen, nelts, nkwelts;
    4999             : 
    5000      632117 :     if (validate_keywords(c, keywords) == -1) {
    5001           1 :         return 0;
    5002             :     }
    5003             : 
    5004      632116 :     nelts = asdl_seq_LEN(args);
    5005      632116 :     nkwelts = asdl_seq_LEN(keywords);
    5006             : 
    5007      632116 :     if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
    5008          33 :          goto ex_call;
    5009             :     }
    5010     1440070 :     for (i = 0; i < nelts; i++) {
    5011      816352 :         expr_ty elt = asdl_seq_GET(args, i);
    5012      816352 :         if (elt->kind == Starred_kind) {
    5013        8365 :             goto ex_call;
    5014             :         }
    5015             :     }
    5016      707549 :     for (i = 0; i < nkwelts; i++) {
    5017       87854 :         keyword_ty kw = asdl_seq_GET(keywords, i);
    5018       87854 :         if (kw->arg == NULL) {
    5019        4023 :             goto ex_call;
    5020             :         }
    5021             :     }
    5022             : 
    5023             :     /* No * or ** args, so can use faster calling sequence */
    5024     1418900 :     for (i = 0; i < nelts; i++) {
    5025      799203 :         expr_ty elt = asdl_seq_GET(args, i);
    5026      799203 :         assert(elt->kind != Starred_kind);
    5027      799203 :         VISIT(c, expr, elt);
    5028             :     }
    5029      619695 :     if (nkwelts) {
    5030      124448 :         VISIT_SEQ(c, keyword, keywords);
    5031       42677 :         if (!compiler_call_simple_kw_helper(c, keywords, nkwelts)) {
    5032           0 :             return 0;
    5033             :         };
    5034             :     }
    5035      619695 :     ADDOP_I(c, CALL, n + nelts + nkwelts);
    5036      619695 :     return 1;
    5037             : 
    5038       12421 : ex_call:
    5039             : 
    5040             :     /* Do positional arguments. */
    5041       12421 :     if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
    5042        5500 :         VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
    5043             :     }
    5044        6921 :     else if (starunpack_helper(c, args, n, BUILD_LIST,
    5045             :                                  LIST_APPEND, LIST_EXTEND, 1) == 0) {
    5046           0 :         return 0;
    5047             :     }
    5048             :     /* Then keyword arguments */
    5049       12421 :     if (nkwelts) {
    5050             :         /* Has a new dict been pushed */
    5051        7512 :         int have_dict = 0;
    5052             : 
    5053        7512 :         nseen = 0;  /* the number of keyword arguments on the stack following */
    5054       18327 :         for (i = 0; i < nkwelts; i++) {
    5055       10815 :             keyword_ty kw = asdl_seq_GET(keywords, i);
    5056       10815 :             if (kw->arg == NULL) {
    5057             :                 /* A keyword argument unpacking. */
    5058        7089 :                 if (nseen) {
    5059         929 :                     if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
    5060           0 :                         return 0;
    5061             :                     }
    5062         929 :                     if (have_dict) {
    5063           2 :                         ADDOP_I(c, DICT_MERGE, 1);
    5064             :                     }
    5065         929 :                     have_dict = 1;
    5066         929 :                     nseen = 0;
    5067             :                 }
    5068        7089 :                 if (!have_dict) {
    5069        6147 :                     ADDOP_I(c, BUILD_MAP, 0);
    5070        6147 :                     have_dict = 1;
    5071             :                 }
    5072        7089 :                 VISIT(c, expr, kw->value);
    5073        7089 :                 ADDOP_I(c, DICT_MERGE, 1);
    5074             :             }
    5075             :             else {
    5076        3726 :                 nseen++;
    5077             :             }
    5078             :         }
    5079        7512 :         if (nseen) {
    5080             :             /* Pack up any trailing keyword arguments. */
    5081         449 :             if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
    5082           0 :                 return 0;
    5083             :             }
    5084         449 :             if (have_dict) {
    5085          11 :                 ADDOP_I(c, DICT_MERGE, 1);
    5086             :             }
    5087         449 :             have_dict = 1;
    5088             :         }
    5089        7512 :         assert(have_dict);
    5090             :     }
    5091       12421 :     ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
    5092       12421 :     return 1;
    5093             : }
    5094             : 
    5095             : 
    5096             : /* List and set comprehensions and generator expressions work by creating a
    5097             :   nested function to perform the actual iteration. This means that the
    5098             :   iteration variables don't leak into the current scope.
    5099             :   The defined function is called immediately following its definition, with the
    5100             :   result of that call being the result of the expression.
    5101             :   The LC/SC version returns the populated container, while the GE version is
    5102             :   flagged in symtable.c as a generator, so it returns the generator object
    5103             :   when the function is called.
    5104             : 
    5105             :   Possible cleanups:
    5106             :     - iterate over the generator sequence instead of using recursion
    5107             : */
    5108             : 
    5109             : 
    5110             : static int
    5111       18389 : compiler_comprehension_generator(struct compiler *c,
    5112             :                                  asdl_comprehension_seq *generators, int gen_index,
    5113             :                                  int depth,
    5114             :                                  expr_ty elt, expr_ty val, int type)
    5115             : {
    5116             :     comprehension_ty gen;
    5117       18389 :     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
    5118       18389 :     if (gen->is_async) {
    5119          63 :         return compiler_async_comprehension_generator(
    5120             :             c, generators, gen_index, depth, elt, val, type);
    5121             :     } else {
    5122       18326 :         return compiler_sync_comprehension_generator(
    5123             :             c, generators, gen_index, depth, elt, val, type);
    5124             :     }
    5125             : }
    5126             : 
    5127             : static int
    5128       18326 : compiler_sync_comprehension_generator(struct compiler *c,
    5129             :                                       asdl_comprehension_seq *generators, int gen_index,
    5130             :                                       int depth,
    5131             :                                       expr_ty elt, expr_ty val, int type)
    5132             : {
    5133             :     /* generate code for the iterator, then each of the ifs,
    5134             :        and then write to the element */
    5135             : 
    5136             :     comprehension_ty gen;
    5137             :     basicblock *start, *anchor, *if_cleanup;
    5138             :     Py_ssize_t i, n;
    5139             : 
    5140       18326 :     start = compiler_new_block(c);
    5141       18326 :     if_cleanup = compiler_new_block(c);
    5142       18326 :     anchor = compiler_new_block(c);
    5143             : 
    5144       18326 :     if (start == NULL || if_cleanup == NULL || anchor == NULL) {
    5145           0 :         return 0;
    5146             :     }
    5147             : 
    5148       18326 :     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
    5149             : 
    5150       18326 :     if (gen_index == 0) {
    5151             :         /* Receive outermost iter as an implicit argument */
    5152       17747 :         c->u->u_argcount = 1;
    5153       17747 :         ADDOP_I(c, LOAD_FAST, 0);
    5154             :     }
    5155             :     else {
    5156             :         /* Sub-iter - calculate on the fly */
    5157             :         /* Fast path for the temporary variable assignment idiom:
    5158             :              for y in [f(x)]
    5159             :          */
    5160             :         asdl_expr_seq *elts;
    5161         579 :         switch (gen->iter->kind) {
    5162           0 :             case List_kind:
    5163           0 :                 elts = gen->iter->v.List.elts;
    5164           0 :                 break;
    5165          22 :             case Tuple_kind:
    5166          22 :                 elts = gen->iter->v.Tuple.elts;
    5167          22 :                 break;
    5168         557 :             default:
    5169         557 :                 elts = NULL;
    5170             :         }
    5171         579 :         if (asdl_seq_LEN(elts) == 1) {
    5172          21 :             expr_ty elt = asdl_seq_GET(elts, 0);
    5173          21 :             if (elt->kind != Starred_kind) {
    5174          21 :                 VISIT(c, expr, elt);
    5175          21 :                 start = NULL;
    5176             :             }
    5177             :         }
    5178         579 :         if (start) {
    5179         558 :             VISIT(c, expr, gen->iter);
    5180         558 :             ADDOP(c, GET_ITER);
    5181             :         }
    5182             :     }
    5183       18326 :     if (start) {
    5184       18305 :         depth++;
    5185       18305 :         compiler_use_next_block(c, start);
    5186       18305 :         ADDOP_JUMP(c, FOR_ITER, anchor);
    5187             :     }
    5188       18326 :     VISIT(c, expr, gen->target);
    5189             : 
    5190             :     /* XXX this needs to be cleaned up...a lot! */
    5191       18326 :     n = asdl_seq_LEN(gen->ifs);
    5192       22570 :     for (i = 0; i < n; i++) {
    5193        4244 :         expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
    5194        4244 :         if (!compiler_jump_if(c, e, if_cleanup, 0))
    5195           0 :             return 0;
    5196             :     }
    5197             : 
    5198       18326 :     if (++gen_index < asdl_seq_LEN(generators))
    5199         581 :         if (!compiler_comprehension_generator(c,
    5200             :                                               generators, gen_index, depth,
    5201             :                                               elt, val, type))
    5202           0 :         return 0;
    5203             : 
    5204             :     /* only append after the last for generator */
    5205       18326 :     if (gen_index >= asdl_seq_LEN(generators)) {
    5206             :         /* comprehension specific code */
    5207       17745 :         switch (type) {
    5208        7738 :         case COMP_GENEXP:
    5209        7738 :             VISIT(c, expr, elt);
    5210        7738 :             ADDOP_YIELD(c);
    5211        7738 :             ADDOP(c, POP_TOP);
    5212        7738 :             break;
    5213        8453 :         case COMP_LISTCOMP:
    5214        8453 :             VISIT(c, expr, elt);
    5215        8453 :             ADDOP_I(c, LIST_APPEND, depth + 1);
    5216        8453 :             break;
    5217         394 :         case COMP_SETCOMP:
    5218         394 :             VISIT(c, expr, elt);
    5219         394 :             ADDOP_I(c, SET_ADD, depth + 1);
    5220         394 :             break;
    5221        1160 :         case COMP_DICTCOMP:
    5222             :             /* With '{k: v}', k is evaluated before v, so we do
    5223             :                the same. */
    5224        1160 :             VISIT(c, expr, elt);
    5225        1160 :             VISIT(c, expr, val);
    5226        1160 :             ADDOP_I(c, MAP_ADD, depth + 1);
    5227        1160 :             break;
    5228           0 :         default:
    5229           0 :             return 0;
    5230             :         }
    5231         581 :     }
    5232       18326 :     compiler_use_next_block(c, if_cleanup);
    5233       18326 :     if (start) {
    5234       18305 :         ADDOP_JUMP(c, JUMP, start);
    5235       18305 :         compiler_use_next_block(c, anchor);
    5236             :     }
    5237             : 
    5238       18326 :     return 1;
    5239             : }
    5240             : 
    5241             : static int
    5242          63 : compiler_async_comprehension_generator(struct compiler *c,
    5243             :                                       asdl_comprehension_seq *generators, int gen_index,
    5244             :                                       int depth,
    5245             :                                       expr_ty elt, expr_ty val, int type)
    5246             : {
    5247             :     comprehension_ty gen;
    5248             :     basicblock *start, *if_cleanup, *except;
    5249             :     Py_ssize_t i, n;
    5250          63 :     start = compiler_new_block(c);
    5251          63 :     except = compiler_new_block(c);
    5252          63 :     if_cleanup = compiler_new_block(c);
    5253             : 
    5254          63 :     if (start == NULL || if_cleanup == NULL || except == NULL) {
    5255           0 :         return 0;
    5256             :     }
    5257             : 
    5258          63 :     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
    5259             : 
    5260          63 :     if (gen_index == 0) {
    5261             :         /* Receive outermost iter as an implicit argument */
    5262          58 :         c->u->u_argcount = 1;
    5263          58 :         ADDOP_I(c, LOAD_FAST, 0);
    5264             :     }
    5265             :     else {
    5266             :         /* Sub-iter - calculate on the fly */
    5267           5 :         VISIT(c, expr, gen->iter);
    5268           5 :         ADDOP(c, GET_AITER);
    5269             :     }
    5270             : 
    5271          63 :     compiler_use_next_block(c, start);
    5272             :     /* Runtime will push a block here, so we need to account for that */
    5273          63 :     if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
    5274             :                               NULL, NULL)) {
    5275           0 :         return 0;
    5276             :     }
    5277             : 
    5278          63 :     ADDOP_JUMP(c, SETUP_FINALLY, except);
    5279          63 :     ADDOP(c, GET_ANEXT);
    5280          63 :     ADDOP_LOAD_CONST(c, Py_None);
    5281          63 :     ADD_YIELD_FROM(c, 1);
    5282          63 :     ADDOP(c, POP_BLOCK);
    5283          63 :     VISIT(c, expr, gen->target);
    5284             : 
    5285          63 :     n = asdl_seq_LEN(gen->ifs);
    5286          72 :     for (i = 0; i < n; i++) {
    5287           9 :         expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
    5288           9 :         if (!compiler_jump_if(c, e, if_cleanup, 0))
    5289           0 :             return 0;
    5290             :     }
    5291             : 
    5292          63 :     depth++;
    5293          63 :     if (++gen_index < asdl_seq_LEN(generators))
    5294           3 :         if (!compiler_comprehension_generator(c,
    5295             :                                               generators, gen_index, depth,
    5296             :                                               elt, val, type))
    5297           0 :         return 0;
    5298             : 
    5299             :     /* only append after the last for generator */
    5300          63 :     if (gen_index >= asdl_seq_LEN(generators)) {
    5301             :         /* comprehension specific code */
    5302          60 :         switch (type) {
    5303          10 :         case COMP_GENEXP:
    5304          10 :             VISIT(c, expr, elt);
    5305          10 :             ADDOP_YIELD(c);
    5306          10 :             ADDOP(c, POP_TOP);
    5307          10 :             break;
    5308          36 :         case COMP_LISTCOMP:
    5309          36 :             VISIT(c, expr, elt);
    5310          36 :             ADDOP_I(c, LIST_APPEND, depth + 1);
    5311          36 :             break;
    5312           8 :         case COMP_SETCOMP:
    5313           8 :             VISIT(c, expr, elt);
    5314           8 :             ADDOP_I(c, SET_ADD, depth + 1);
    5315           8 :             break;
    5316           6 :         case COMP_DICTCOMP:
    5317             :             /* With '{k: v}', k is evaluated before v, so we do
    5318             :                the same. */
    5319           6 :             VISIT(c, expr, elt);
    5320           6 :             VISIT(c, expr, val);
    5321           6 :             ADDOP_I(c, MAP_ADD, depth + 1);
    5322           6 :             break;
    5323           0 :         default:
    5324           0 :             return 0;
    5325             :         }
    5326           3 :     }
    5327          63 :     compiler_use_next_block(c, if_cleanup);
    5328          63 :     ADDOP_JUMP(c, JUMP, start);
    5329             : 
    5330          63 :     compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
    5331             : 
    5332          63 :     compiler_use_next_block(c, except);
    5333             :     //UNSET_LOC(c);
    5334             : 
    5335          63 :     ADDOP(c, END_ASYNC_FOR);
    5336             : 
    5337          63 :     return 1;
    5338             : }
    5339             : 
    5340             : static int
    5341       17847 : compiler_comprehension(struct compiler *c, expr_ty e, int type,
    5342             :                        identifier name, asdl_comprehension_seq *generators, expr_ty elt,
    5343             :                        expr_ty val)
    5344             : {
    5345       17847 :     PyCodeObject *co = NULL;
    5346             :     comprehension_ty outermost;
    5347       17847 :     PyObject *qualname = NULL;
    5348       17847 :     int scope_type = c->u->u_scope_type;
    5349       17847 :     int is_async_generator = 0;
    5350       17847 :     int is_top_level_await = IS_TOP_LEVEL_AWAIT(c);
    5351             : 
    5352       17847 :     outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
    5353       17847 :     if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
    5354             :                               (void *)e, e->lineno))
    5355             :     {
    5356           0 :         goto error;
    5357             :     }
    5358       17847 :     SET_LOC(c, e);
    5359             : 
    5360       17847 :     is_async_generator = c->u->u_ste->ste_coroutine;
    5361             : 
    5362       17847 :     if (is_async_generator && type != COMP_GENEXP &&
    5363          63 :         scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
    5364          56 :         scope_type != COMPILER_SCOPE_COMPREHENSION &&
    5365             :         !is_top_level_await)
    5366             :     {
    5367          42 :         compiler_error(c, "asynchronous comprehension outside of "
    5368             :                           "an asynchronous function");
    5369          42 :         goto error_in_scope;
    5370             :     }
    5371             : 
    5372       17805 :     if (type != COMP_GENEXP) {
    5373             :         int op;
    5374       10057 :         switch (type) {
    5375        8489 :         case COMP_LISTCOMP:
    5376        8489 :             op = BUILD_LIST;
    5377        8489 :             break;
    5378         402 :         case COMP_SETCOMP:
    5379         402 :             op = BUILD_SET;
    5380         402 :             break;
    5381        1166 :         case COMP_DICTCOMP:
    5382        1166 :             op = BUILD_MAP;
    5383        1166 :             break;
    5384           0 :         default:
    5385           0 :             PyErr_Format(PyExc_SystemError,
    5386             :                          "unknown comprehension type %d", type);
    5387           0 :             goto error_in_scope;
    5388             :         }
    5389             : 
    5390       10057 :         ADDOP_I(c, op, 0);
    5391             :     }
    5392             : 
    5393       17805 :     if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
    5394             :                                           val, type))
    5395           0 :         goto error_in_scope;
    5396             : 
    5397       17805 :     if (type != COMP_GENEXP) {
    5398       10057 :         ADDOP(c, RETURN_VALUE);
    5399             :     }
    5400             : 
    5401       17805 :     co = assemble(c, 1);
    5402       17805 :     qualname = c->u->u_qualname;
    5403       17805 :     Py_INCREF(qualname);
    5404       17805 :     compiler_exit_scope(c);
    5405       17805 :     if (is_top_level_await && is_async_generator){
    5406          16 :         c->u->u_ste->ste_coroutine = 1;
    5407             :     }
    5408       17805 :     if (co == NULL)
    5409           0 :         goto error;
    5410             : 
    5411       17805 :     if (!compiler_make_closure(c, co, 0, qualname)) {
    5412           0 :         goto error;
    5413             :     }
    5414       17805 :     Py_DECREF(qualname);
    5415       17805 :     Py_DECREF(co);
    5416             : 
    5417       17805 :     VISIT(c, expr, outermost->iter);
    5418             : 
    5419       17801 :     if (outermost->is_async) {
    5420          58 :         ADDOP(c, GET_AITER);
    5421             :     } else {
    5422       17743 :         ADDOP(c, GET_ITER);
    5423             :     }
    5424             : 
    5425       17801 :     ADDOP_I(c, CALL, 0);
    5426             : 
    5427       17801 :     if (is_async_generator && type != COMP_GENEXP) {
    5428          67 :         ADDOP_I(c, GET_AWAITABLE, 0);
    5429          67 :         ADDOP_LOAD_CONST(c, Py_None);
    5430          67 :         ADD_YIELD_FROM(c, 1);
    5431             :     }
    5432             : 
    5433       17801 :     return 1;
    5434          42 : error_in_scope:
    5435          42 :     compiler_exit_scope(c);
    5436          42 : error:
    5437          42 :     Py_XDECREF(qualname);
    5438          42 :     Py_XDECREF(co);
    5439          42 :     return 0;
    5440             : }
    5441             : 
    5442             : static int
    5443        7748 : compiler_genexp(struct compiler *c, expr_ty e)
    5444             : {
    5445        7748 :     assert(e->kind == GeneratorExp_kind);
    5446             :     _Py_DECLARE_STR(anon_genexpr, "<genexpr>");
    5447        7748 :     return compiler_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
    5448             :                                   e->v.GeneratorExp.generators,
    5449             :                                   e->v.GeneratorExp.elt, NULL);
    5450             : }
    5451             : 
    5452             : static int
    5453        8523 : compiler_listcomp(struct compiler *c, expr_ty e)
    5454             : {
    5455        8523 :     assert(e->kind == ListComp_kind);
    5456             :     _Py_DECLARE_STR(anon_listcomp, "<listcomp>");
    5457        8523 :     return compiler_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
    5458             :                                   e->v.ListComp.generators,
    5459             :                                   e->v.ListComp.elt, NULL);
    5460             : }
    5461             : 
    5462             : static int
    5463         407 : compiler_setcomp(struct compiler *c, expr_ty e)
    5464             : {
    5465         407 :     assert(e->kind == SetComp_kind);
    5466             :     _Py_DECLARE_STR(anon_setcomp, "<setcomp>");
    5467         407 :     return compiler_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
    5468             :                                   e->v.SetComp.generators,
    5469             :                                   e->v.SetComp.elt, NULL);
    5470             : }
    5471             : 
    5472             : 
    5473             : static int
    5474        1169 : compiler_dictcomp(struct compiler *c, expr_ty e)
    5475             : {
    5476        1169 :     assert(e->kind == DictComp_kind);
    5477             :     _Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
    5478        1169 :     return compiler_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
    5479             :                                   e->v.DictComp.generators,
    5480             :                                   e->v.DictComp.key, e->v.DictComp.value);
    5481             : }
    5482             : 
    5483             : 
    5484             : static int
    5485      124098 : compiler_visit_keyword(struct compiler *c, keyword_ty k)
    5486             : {
    5487      124098 :     VISIT(c, expr, k->value);
    5488      124098 :     return 1;
    5489             : }
    5490             : 
    5491             : 
    5492             : static int
    5493       21258 : compiler_with_except_finish(struct compiler *c, basicblock * cleanup) {
    5494       21258 :     UNSET_LOC(c);
    5495       21258 :     basicblock *suppress = compiler_new_block(c);
    5496       21258 :     if (suppress == NULL) {
    5497           0 :         return 0;
    5498             :     }
    5499       21258 :     ADDOP_JUMP(c, POP_JUMP_IF_TRUE, suppress);
    5500       21258 :     ADDOP_I(c, RERAISE, 2);
    5501       21258 :     compiler_use_next_block(c, suppress);
    5502       21258 :     ADDOP(c, POP_TOP); /* exc_value */
    5503       21258 :     ADDOP(c, POP_BLOCK);
    5504       21258 :     ADDOP(c, POP_EXCEPT);
    5505       21258 :     ADDOP(c, POP_TOP);
    5506       21258 :     ADDOP(c, POP_TOP);
    5507       21258 :     basicblock *exit = compiler_new_block(c);
    5508       21258 :     if (exit == NULL) {
    5509           0 :         return 0;
    5510             :     }
    5511       21258 :     ADDOP_JUMP(c, JUMP, exit);
    5512       21258 :     compiler_use_next_block(c, cleanup);
    5513       21258 :     POP_EXCEPT_AND_RERAISE(c);
    5514       21258 :     compiler_use_next_block(c, exit);
    5515       21258 :     return 1;
    5516             : }
    5517             : 
    5518             : /*
    5519             :    Implements the async with statement.
    5520             : 
    5521             :    The semantics outlined in that PEP are as follows:
    5522             : 
    5523             :    async with EXPR as VAR:
    5524             :        BLOCK
    5525             : 
    5526             :    It is implemented roughly as:
    5527             : 
    5528             :    context = EXPR
    5529             :    exit = context.__aexit__  # not calling it
    5530             :    value = await context.__aenter__()
    5531             :    try:
    5532             :        VAR = value  # if VAR present in the syntax
    5533             :        BLOCK
    5534             :    finally:
    5535             :        if an exception was raised:
    5536             :            exc = copy of (exception, instance, traceback)
    5537             :        else:
    5538             :            exc = (None, None, None)
    5539             :        if not (await exit(*exc)):
    5540             :            raise
    5541             :  */
    5542             : static int
    5543         235 : compiler_async_with(struct compiler *c, stmt_ty s, int pos)
    5544             : {
    5545             :     basicblock *block, *final, *exit, *cleanup;
    5546         235 :     withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
    5547             : 
    5548         235 :     assert(s->kind == AsyncWith_kind);
    5549         235 :     if (IS_TOP_LEVEL_AWAIT(c)){
    5550           2 :         c->u->u_ste->ste_coroutine = 1;
    5551         233 :     } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
    5552           8 :         return compiler_error(c, "'async with' outside async function");
    5553             :     }
    5554             : 
    5555         227 :     block = compiler_new_block(c);
    5556         227 :     final = compiler_new_block(c);
    5557         227 :     exit = compiler_new_block(c);
    5558         227 :     cleanup = compiler_new_block(c);
    5559         227 :     if (!block || !final || !exit || !cleanup)
    5560           0 :         return 0;
    5561             : 
    5562             :     /* Evaluate EXPR */
    5563         227 :     VISIT(c, expr, item->context_expr);
    5564             : 
    5565         227 :     ADDOP(c, BEFORE_ASYNC_WITH);
    5566         227 :     ADDOP_I(c, GET_AWAITABLE, 1);
    5567         227 :     ADDOP_LOAD_CONST(c, Py_None);
    5568         227 :     ADD_YIELD_FROM(c, 1);
    5569             : 
    5570         227 :     ADDOP_JUMP(c, SETUP_WITH, final);
    5571             : 
    5572             :     /* SETUP_WITH pushes a finally block. */
    5573         227 :     compiler_use_next_block(c, block);
    5574         227 :     if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
    5575           0 :         return 0;
    5576             :     }
    5577             : 
    5578         227 :     if (item->optional_vars) {
    5579         109 :         VISIT(c, expr, item->optional_vars);
    5580             :     }
    5581             :     else {
    5582             :     /* Discard result from context.__aenter__() */
    5583         118 :         ADDOP(c, POP_TOP);
    5584             :     }
    5585             : 
    5586         227 :     pos++;
    5587         227 :     if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
    5588             :         /* BLOCK code */
    5589         586 :         VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
    5590           8 :     else if (!compiler_async_with(c, s, pos))
    5591           0 :             return 0;
    5592             : 
    5593         227 :     compiler_pop_fblock(c, ASYNC_WITH, block);
    5594         227 :     ADDOP(c, POP_BLOCK);
    5595             :     /* End of body; start the cleanup */
    5596             : 
    5597             :     /* For successful outcome:
    5598             :      * call __exit__(None, None, None)
    5599             :      */
    5600         227 :     SET_LOC(c, s);
    5601         227 :     if(!compiler_call_exit_with_nones(c))
    5602           0 :         return 0;
    5603         227 :     ADDOP_I(c, GET_AWAITABLE, 2);
    5604         227 :     ADDOP_LOAD_CONST(c, Py_None);
    5605         227 :     ADD_YIELD_FROM(c, 1);
    5606             : 
    5607         227 :     ADDOP(c, POP_TOP);
    5608             : 
    5609         227 :     ADDOP_JUMP(c, JUMP, exit);
    5610             : 
    5611             :     /* For exceptional outcome: */
    5612         227 :     compiler_use_next_block(c, final);
    5613             : 
    5614         227 :     ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
    5615         227 :     ADDOP(c, PUSH_EXC_INFO);
    5616         227 :     ADDOP(c, WITH_EXCEPT_START);
    5617         227 :     ADDOP_I(c, GET_AWAITABLE, 2);
    5618         227 :     ADDOP_LOAD_CONST(c, Py_None);
    5619         227 :     ADD_YIELD_FROM(c, 1);
    5620         227 :     compiler_with_except_finish(c, cleanup);
    5621             : 
    5622         227 :     compiler_use_next_block(c, exit);
    5623         227 :     return 1;
    5624             : }
    5625             : 
    5626             : 
    5627             : /*
    5628             :    Implements the with statement from PEP 343.
    5629             :    with EXPR as VAR:
    5630             :        BLOCK
    5631             :    is implemented as:
    5632             :         <code for EXPR>
    5633             :         SETUP_WITH  E
    5634             :         <code to store to VAR> or POP_TOP
    5635             :         <code for BLOCK>
    5636             :         LOAD_CONST (None, None, None)
    5637             :         CALL_FUNCTION_EX 0
    5638             :         JUMP  EXIT
    5639             :     E:  WITH_EXCEPT_START (calls EXPR.__exit__)
    5640             :         POP_JUMP_IF_TRUE T:
    5641             :         RERAISE
    5642             :     T:  POP_TOP (remove exception from stack)
    5643             :         POP_EXCEPT
    5644             :         POP_TOP
    5645             :     EXIT:
    5646             :  */
    5647             : 
    5648             : static int
    5649       21031 : compiler_with(struct compiler *c, stmt_ty s, int pos)
    5650             : {
    5651             :     basicblock *block, *final, *exit, *cleanup;
    5652       21031 :     withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
    5653             : 
    5654       21031 :     assert(s->kind == With_kind);
    5655             : 
    5656       21031 :     block = compiler_new_block(c);
    5657       21031 :     final = compiler_new_block(c);
    5658       21031 :     exit = compiler_new_block(c);
    5659       21031 :     cleanup = compiler_new_block(c);
    5660       21031 :     if (!block || !final || !exit || !cleanup)
    5661           0 :         return 0;
    5662             : 
    5663             :     /* Evaluate EXPR */
    5664       21031 :     VISIT(c, expr, item->context_expr);
    5665             :     /* Will push bound __exit__ */
    5666       21031 :     ADDOP(c, BEFORE_WITH);
    5667       21031 :     ADDOP_JUMP(c, SETUP_WITH, final);
    5668             : 
    5669             :     /* SETUP_WITH pushes a finally block. */
    5670       21031 :     compiler_use_next_block(c, block);
    5671       21031 :     if (!compiler_push_fblock(c, WITH, block, final, s)) {
    5672           0 :         return 0;
    5673             :     }
    5674             : 
    5675       21031 :     if (item->optional_vars) {
    5676        9095 :         VISIT(c, expr, item->optional_vars);
    5677             :     }
    5678             :     else {
    5679             :     /* Discard result from context.__enter__() */
    5680       11936 :         ADDOP(c, POP_TOP);
    5681             :     }
    5682             : 
    5683       21031 :     pos++;
    5684       21031 :     if (pos == asdl_seq_LEN(s->v.With.items))
    5685             :         /* BLOCK code */
    5686       60215 :         VISIT_SEQ(c, stmt, s->v.With.body)
    5687         378 :     else if (!compiler_with(c, s, pos))
    5688           0 :             return 0;
    5689             : 
    5690             : 
    5691             :     /* Mark all following code as artificial */
    5692       21031 :     UNSET_LOC(c);
    5693       21031 :     ADDOP(c, POP_BLOCK);
    5694       21031 :     compiler_pop_fblock(c, WITH, block);
    5695             : 
    5696             :     /* End of body; start the cleanup. */
    5697             : 
    5698             :     /* For successful outcome:
    5699             :      * call __exit__(None, None, None)
    5700             :      */
    5701       21031 :     SET_LOC(c, s);
    5702       21031 :     if (!compiler_call_exit_with_nones(c))
    5703           0 :         return 0;
    5704       21031 :     ADDOP(c, POP_TOP);
    5705       21031 :     ADDOP_JUMP(c, JUMP, exit);
    5706             : 
    5707             :     /* For exceptional outcome: */
    5708       21031 :     compiler_use_next_block(c, final);
    5709             : 
    5710       21031 :     ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
    5711       21031 :     ADDOP(c, PUSH_EXC_INFO);
    5712       21031 :     ADDOP(c, WITH_EXCEPT_START);
    5713       21031 :     compiler_with_except_finish(c, cleanup);
    5714             : 
    5715       21031 :     compiler_use_next_block(c, exit);
    5716       21031 :     return 1;
    5717             : }
    5718             : 
    5719             : static int
    5720    10033600 : compiler_visit_expr1(struct compiler *c, expr_ty e)
    5721             : {
    5722    10033600 :     switch (e->kind) {
    5723         779 :     case NamedExpr_kind:
    5724         779 :         VISIT(c, expr, e->v.NamedExpr.value);
    5725         779 :         ADDOP_I(c, COPY, 1);
    5726         779 :         VISIT(c, expr, e->v.NamedExpr.target);
    5727         778 :         break;
    5728       17368 :     case BoolOp_kind:
    5729       17368 :         return compiler_boolop(c, e);
    5730      196048 :     case BinOp_kind:
    5731      196048 :         VISIT(c, expr, e->v.BinOp.left);
    5732      196048 :         VISIT(c, expr, e->v.BinOp.right);
    5733      196048 :         ADDOP_BINARY(c, e->v.BinOp.op);
    5734      196048 :         break;
    5735        7500 :     case UnaryOp_kind:
    5736        7500 :         VISIT(c, expr, e->v.UnaryOp.operand);
    5737        7500 :         ADDOP(c, unaryop(e->v.UnaryOp.op));
    5738        7500 :         break;
    5739       34765 :     case Lambda_kind:
    5740       34765 :         return compiler_lambda(c, e);
    5741        9762 :     case IfExp_kind:
    5742        9762 :         return compiler_ifexp(c, e);
    5743       32250 :     case Dict_kind:
    5744       32250 :         return compiler_dict(c, e);
    5745        1807 :     case Set_kind:
    5746        1807 :         return compiler_set(c, e);
    5747        7748 :     case GeneratorExp_kind:
    5748        7748 :         return compiler_genexp(c, e);
    5749        8523 :     case ListComp_kind:
    5750        8523 :         return compiler_listcomp(c, e);
    5751         407 :     case SetComp_kind:
    5752         407 :         return compiler_setcomp(c, e);
    5753        1169 :     case DictComp_kind:
    5754        1169 :         return compiler_dictcomp(c, e);
    5755       11537 :     case Yield_kind:
    5756       11537 :         if (c->u->u_ste->ste_type != FunctionBlock)
    5757          15 :             return compiler_error(c, "'yield' outside function");
    5758       11522 :         if (e->v.Yield.value) {
    5759       10762 :             VISIT(c, expr, e->v.Yield.value);
    5760             :         }
    5761             :         else {
    5762         760 :             ADDOP_LOAD_CONST(c, Py_None);
    5763             :         }
    5764       11522 :         ADDOP_YIELD(c);
    5765       11522 :         break;
    5766        1915 :     case YieldFrom_kind:
    5767        1915 :         if (c->u->u_ste->ste_type != FunctionBlock)
    5768           3 :             return compiler_error(c, "'yield' outside function");
    5769             : 
    5770        1912 :         if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
    5771           3 :             return compiler_error(c, "'yield from' inside async function");
    5772             : 
    5773        1909 :         VISIT(c, expr, e->v.YieldFrom.value);
    5774        1909 :         ADDOP(c, GET_YIELD_FROM_ITER);
    5775        1909 :         ADDOP_LOAD_CONST(c, Py_None);
    5776        1909 :         ADD_YIELD_FROM(c, 0);
    5777        1909 :         break;
    5778        1756 :     case Await_kind:
    5779        1756 :         if (!IS_TOP_LEVEL_AWAIT(c)){
    5780        1754 :             if (c->u->u_ste->ste_type != FunctionBlock){
    5781          11 :                 return compiler_error(c, "'await' outside function");
    5782             :             }
    5783             : 
    5784        1743 :             if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
    5785          29 :                     c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
    5786          14 :                 return compiler_error(c, "'await' outside async function");
    5787             :             }
    5788             :         }
    5789             : 
    5790        1731 :         VISIT(c, expr, e->v.Await.value);
    5791        1731 :         ADDOP_I(c, GET_AWAITABLE, 0);
    5792        1731 :         ADDOP_LOAD_CONST(c, Py_None);
    5793        1731 :         ADD_YIELD_FROM(c, 1);
    5794        1731 :         break;
    5795      210496 :     case Compare_kind:
    5796      210496 :         return compiler_compare(c, e);
    5797     1128080 :     case Call_kind:
    5798     1128080 :         return compiler_call(c, e);
    5799     2338910 :     case Constant_kind:
    5800     2338910 :         ADDOP_LOAD_CONST(c, e->v.Constant.value);
    5801     2338910 :         break;
    5802       30390 :     case JoinedStr_kind:
    5803       30390 :         return compiler_joined_str(c, e);
    5804      118027 :     case FormattedValue_kind:
    5805      118027 :         return compiler_formatted_value(c, e);
    5806             :     /* The following exprs can be assignment targets. */
    5807      796079 :     case Attribute_kind:
    5808      796079 :         VISIT(c, expr, e->v.Attribute.value);
    5809      796079 :         switch (e->v.Attribute.ctx) {
    5810      673985 :         case Load:
    5811             :         {
    5812      673985 :             int old_lineno = c->u->u_loc.lineno;
    5813      673985 :             c->u->u_loc.lineno = e->end_lineno;
    5814      673985 :             ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
    5815      673985 :             c->u->u_loc.lineno = old_lineno;
    5816      673985 :             break;
    5817             :         }
    5818      121042 :         case Store:
    5819      121042 :             if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
    5820           1 :                 return 0;
    5821             :             }
    5822      121041 :             int old_lineno = c->u->u_loc.lineno;
    5823      121041 :             c->u->u_loc.lineno = e->end_lineno;
    5824      121041 :             ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
    5825      121041 :             c->u->u_loc.lineno = old_lineno;
    5826      121041 :             break;
    5827        1052 :         case Del:
    5828        1052 :             ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
    5829        1052 :             break;
    5830             :         }
    5831      796078 :         break;
    5832      184581 :     case Subscript_kind:
    5833      184581 :         return compiler_subscript(c, e);
    5834           4 :     case Starred_kind:
    5835           4 :         switch (e->v.Starred.ctx) {
    5836           1 :         case Store:
    5837             :             /* In all legitimate cases, the Starred node was already replaced
    5838             :              * by compiler_list/compiler_tuple. XXX: is that okay? */
    5839           1 :             return compiler_error(c,
    5840             :                 "starred assignment target must be in a list or tuple");
    5841           3 :         default:
    5842           3 :             return compiler_error(c,
    5843             :                 "can't use starred expression here");
    5844             :         }
    5845             :         break;
    5846        1742 :     case Slice_kind:
    5847             :     {
    5848        1742 :         int n = compiler_slice(c, e);
    5849        1742 :         if (n == 0) {
    5850           0 :             return 0;
    5851             :         }
    5852        1742 :         ADDOP_I(c, BUILD_SLICE, n);
    5853        1742 :         break;
    5854             :     }
    5855     4661620 :     case Name_kind:
    5856     4661620 :         return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
    5857             :     /* child nodes of List and Tuple will have expr_context set */
    5858       63421 :     case List_kind:
    5859       63421 :         return compiler_list(c, e);
    5860      166950 :     case Tuple_kind:
    5861      166950 :         return compiler_tuple(c, e);
    5862             :     }
    5863     3356220 :     return 1;
    5864             : }
    5865             : 
    5866             : static int
    5867    10033600 : compiler_visit_expr(struct compiler *c, expr_ty e)
    5868             : {
    5869    10033600 :     struct location old_loc = c->u->u_loc;
    5870    10033600 :     SET_LOC(c, e);
    5871    10033600 :     int res = compiler_visit_expr1(c, e);
    5872    10033600 :     c->u->u_loc = old_loc;
    5873    10033600 :     return res;
    5874             : }
    5875             : 
    5876             : static bool
    5877      186996 : is_two_element_slice(expr_ty s)
    5878             : {
    5879      217989 :     return s->kind == Slice_kind &&
    5880       30993 :            s->v.Slice.step == NULL;
    5881             : }
    5882             : 
    5883             : static int
    5884       21588 : compiler_augassign(struct compiler *c, stmt_ty s)
    5885             : {
    5886       21588 :     assert(s->kind == AugAssign_kind);
    5887       21588 :     expr_ty e = s->v.AugAssign.target;
    5888             : 
    5889       21588 :     struct location old_loc = c->u->u_loc;
    5890       21588 :     SET_LOC(c, e);
    5891             : 
    5892       21588 :     switch (e->kind) {
    5893        3947 :     case Attribute_kind:
    5894        3947 :         VISIT(c, expr, e->v.Attribute.value);
    5895        3947 :         ADDOP_I(c, COPY, 1);
    5896        3947 :         int old_lineno = c->u->u_loc.lineno;
    5897        3947 :         c->u->u_loc.lineno = e->end_lineno;
    5898        3947 :         ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
    5899        3947 :         c->u->u_loc.lineno = old_lineno;
    5900        3947 :         break;
    5901        1230 :     case Subscript_kind:
    5902        1230 :         VISIT(c, expr, e->v.Subscript.value);
    5903        1230 :         if (is_two_element_slice(e->v.Subscript.slice)) {
    5904           8 :             if (!compiler_slice(c, e->v.Subscript.slice)) {
    5905           0 :                 return 0;
    5906             :             }
    5907           8 :             ADDOP_I(c, COPY, 3);
    5908           8 :             ADDOP_I(c, COPY, 3);
    5909           8 :             ADDOP_I(c, COPY, 3);
    5910           8 :             ADDOP(c, BINARY_SLICE);
    5911             :         }
    5912             :         else {
    5913        1222 :             VISIT(c, expr, e->v.Subscript.slice);
    5914        1222 :             ADDOP_I(c, COPY, 2);
    5915        1222 :             ADDOP_I(c, COPY, 2);
    5916        1222 :             ADDOP(c, BINARY_SUBSCR);
    5917             :         }
    5918        1230 :         break;
    5919       16411 :     case Name_kind:
    5920       16411 :         if (!compiler_nameop(c, e->v.Name.id, Load))
    5921           0 :             return 0;
    5922       16411 :         break;
    5923           0 :     default:
    5924           0 :         PyErr_Format(PyExc_SystemError,
    5925             :             "invalid node type (%d) for augmented assignment",
    5926           0 :             e->kind);
    5927           0 :         return 0;
    5928             :     }
    5929             : 
    5930       21588 :     c->u->u_loc = old_loc;
    5931             : 
    5932       21588 :     VISIT(c, expr, s->v.AugAssign.value);
    5933       21588 :     ADDOP_INPLACE(c, s->v.AugAssign.op);
    5934             : 
    5935       21588 :     SET_LOC(c, e);
    5936             : 
    5937       21588 :     switch (e->kind) {
    5938        3947 :     case Attribute_kind:
    5939        3947 :         c->u->u_loc.lineno = e->end_lineno;
    5940        3947 :         ADDOP_I(c, SWAP, 2);
    5941        3947 :         ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
    5942        3947 :         break;
    5943        1230 :     case Subscript_kind:
    5944        1230 :         if (is_two_element_slice(e->v.Subscript.slice)) {
    5945           8 :             ADDOP_I(c, SWAP, 4);
    5946           8 :             ADDOP_I(c, SWAP, 3);
    5947           8 :             ADDOP_I(c, SWAP, 2);
    5948           8 :             ADDOP(c, STORE_SLICE);
    5949             :         }
    5950             :         else {
    5951        1222 :             ADDOP_I(c, SWAP, 3);
    5952        1222 :             ADDOP_I(c, SWAP, 2);
    5953        1222 :             ADDOP(c, STORE_SUBSCR);
    5954             :         }
    5955        1230 :         break;
    5956       16411 :     case Name_kind:
    5957       16411 :         return compiler_nameop(c, e->v.Name.id, Store);
    5958           0 :     default:
    5959           0 :         Py_UNREACHABLE();
    5960             :     }
    5961        5177 :     return 1;
    5962             : }
    5963             : 
    5964             : static int
    5965         385 : check_ann_expr(struct compiler *c, expr_ty e)
    5966             : {
    5967         385 :     VISIT(c, expr, e);
    5968         385 :     ADDOP(c, POP_TOP);
    5969         385 :     return 1;
    5970             : }
    5971             : 
    5972             : static int
    5973        1932 : check_annotation(struct compiler *c, stmt_ty s)
    5974             : {
    5975             :     /* Annotations of complex targets does not produce anything
    5976             :        under annotations future */
    5977        1932 :     if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
    5978         349 :         return 1;
    5979             :     }
    5980             : 
    5981             :     /* Annotations are only evaluated in a module or class. */
    5982        1583 :     if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
    5983        1576 :         c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
    5984          11 :         return check_ann_expr(c, s->v.AnnAssign.annotation);
    5985             :     }
    5986        1572 :     return 1;
    5987             : }
    5988             : 
    5989             : static int
    5990           2 : check_ann_subscr(struct compiler *c, expr_ty e)
    5991             : {
    5992             :     /* We check that everything in a subscript is defined at runtime. */
    5993           2 :     switch (e->kind) {
    5994           0 :     case Slice_kind:
    5995           0 :         if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
    5996           0 :             return 0;
    5997             :         }
    5998           0 :         if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
    5999           0 :             return 0;
    6000             :         }
    6001           0 :         if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
    6002           0 :             return 0;
    6003             :         }
    6004           0 :         return 1;
    6005           0 :     case Tuple_kind: {
    6006             :         /* extended slice */
    6007           0 :         asdl_expr_seq *elts = e->v.Tuple.elts;
    6008           0 :         Py_ssize_t i, n = asdl_seq_LEN(elts);
    6009           0 :         for (i = 0; i < n; i++) {
    6010           0 :             if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
    6011           0 :                 return 0;
    6012             :             }
    6013             :         }
    6014           0 :         return 1;
    6015             :     }
    6016           2 :     default:
    6017           2 :         return check_ann_expr(c, e);
    6018             :     }
    6019             : }
    6020             : 
    6021             : static int
    6022        9866 : compiler_annassign(struct compiler *c, stmt_ty s)
    6023             : {
    6024        9866 :     expr_ty targ = s->v.AnnAssign.target;
    6025             :     PyObject* mangled;
    6026             : 
    6027        9866 :     assert(s->kind == AnnAssign_kind);
    6028             : 
    6029             :     /* We perform the actual assignment first. */
    6030        9866 :     if (s->v.AnnAssign.value) {
    6031        7098 :         VISIT(c, expr, s->v.AnnAssign.value);
    6032        7098 :         VISIT(c, expr, targ);
    6033             :     }
    6034        9866 :     switch (targ->kind) {
    6035        7938 :     case Name_kind:
    6036        7938 :         if (forbidden_name(c, targ->v.Name.id, Store))
    6037           1 :             return 0;
    6038             :         /* If we have a simple name in a module or class, store annotation. */
    6039        7937 :         if (s->v.AnnAssign.simple &&
    6040        7932 :             (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
    6041        6380 :              c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
    6042        5268 :             if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
    6043         557 :                 VISIT(c, annexpr, s->v.AnnAssign.annotation)
    6044             :             }
    6045             :             else {
    6046        4711 :                 VISIT(c, expr, s->v.AnnAssign.annotation);
    6047             :             }
    6048        5268 :             ADDOP_NAME(c, LOAD_NAME, &_Py_ID(__annotations__), names);
    6049        5268 :             mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
    6050        5268 :             ADDOP_LOAD_CONST_NEW(c, mangled);
    6051        5268 :             ADDOP(c, STORE_SUBSCR);
    6052             :         }
    6053        7937 :         break;
    6054        1918 :     case Attribute_kind:
    6055        1918 :         if (forbidden_name(c, targ->v.Attribute.attr, Store))
    6056           1 :             return 0;
    6057        2287 :         if (!s->v.AnnAssign.value &&
    6058         370 :             !check_ann_expr(c, targ->v.Attribute.value)) {
    6059           0 :             return 0;
    6060             :         }
    6061        1917 :         break;
    6062          10 :     case Subscript_kind:
    6063          12 :         if (!s->v.AnnAssign.value &&
    6064           4 :             (!check_ann_expr(c, targ->v.Subscript.value) ||
    6065           2 :              !check_ann_subscr(c, targ->v.Subscript.slice))) {
    6066           0 :                 return 0;
    6067             :         }
    6068          10 :         break;
    6069           0 :     default:
    6070           0 :         PyErr_Format(PyExc_SystemError,
    6071             :                      "invalid node type (%d) for annotated assignment",
    6072           0 :                      targ->kind);
    6073           0 :             return 0;
    6074             :     }
    6075             :     /* Annotation is evaluated last. */
    6076        9864 :     if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
    6077           0 :         return 0;
    6078             :     }
    6079        9864 :     return 1;
    6080             : }
    6081             : 
    6082             : /* Raises a SyntaxError and returns 0.
    6083             :    If something goes wrong, a different exception may be raised.
    6084             : */
    6085             : 
    6086             : static int
    6087         289 : compiler_error(struct compiler *c, const char *format, ...)
    6088             : {
    6089             :     va_list vargs;
    6090         289 :     va_start(vargs, format);
    6091         289 :     PyObject *msg = PyUnicode_FromFormatV(format, vargs);
    6092         289 :     va_end(vargs);
    6093         289 :     if (msg == NULL) {
    6094           0 :         return 0;
    6095             :     }
    6096         289 :     PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_loc.lineno);
    6097         289 :     if (loc == NULL) {
    6098         281 :         Py_INCREF(Py_None);
    6099         281 :         loc = Py_None;
    6100             :     }
    6101         289 :     struct location u_loc = c->u->u_loc;
    6102         289 :     PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
    6103         289 :                                    u_loc.lineno, u_loc.col_offset + 1, loc,
    6104         289 :                                    u_loc.end_lineno, u_loc.end_col_offset + 1);
    6105         289 :     Py_DECREF(msg);
    6106         289 :     if (args == NULL) {
    6107           0 :         goto exit;
    6108             :     }
    6109         289 :     PyErr_SetObject(PyExc_SyntaxError, args);
    6110         289 :  exit:
    6111         289 :     Py_DECREF(loc);
    6112         289 :     Py_XDECREF(args);
    6113         289 :     return 0;
    6114             : }
    6115             : 
    6116             : /* Emits a SyntaxWarning and returns 1 on success.
    6117             :    If a SyntaxWarning raised as error, replaces it with a SyntaxError
    6118             :    and returns 0.
    6119             : */
    6120             : static int
    6121         138 : compiler_warn(struct compiler *c, const char *format, ...)
    6122             : {
    6123             :     va_list vargs;
    6124         138 :     va_start(vargs, format);
    6125         138 :     PyObject *msg = PyUnicode_FromFormatV(format, vargs);
    6126         138 :     va_end(vargs);
    6127         138 :     if (msg == NULL) {
    6128           0 :         return 0;
    6129             :     }
    6130         138 :     if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
    6131         138 :                                  c->u->u_loc.lineno, NULL, NULL) < 0)
    6132             :     {
    6133          67 :         if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
    6134             :             /* Replace the SyntaxWarning exception with a SyntaxError
    6135             :                to get a more accurate error report */
    6136          67 :             PyErr_Clear();
    6137          67 :             assert(PyUnicode_AsUTF8(msg) != NULL);
    6138          67 :             compiler_error(c, PyUnicode_AsUTF8(msg));
    6139             :         }
    6140          67 :         Py_DECREF(msg);
    6141          67 :         return 0;
    6142             :     }
    6143          71 :     Py_DECREF(msg);
    6144          71 :     return 1;
    6145             : }
    6146             : 
    6147             : static int
    6148      184581 : compiler_subscript(struct compiler *c, expr_ty e)
    6149             : {
    6150      184581 :     expr_context_ty ctx = e->v.Subscript.ctx;
    6151      184581 :     int op = 0;
    6152             : 
    6153      184581 :     if (ctx == Load) {
    6154      152222 :         if (!check_subscripter(c, e->v.Subscript.value)) {
    6155          10 :             return 0;
    6156             :         }
    6157      152212 :         if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
    6158          25 :             return 0;
    6159             :         }
    6160             :     }
    6161             : 
    6162      184546 :     VISIT(c, expr, e->v.Subscript.value);
    6163      184536 :     if (is_two_element_slice(e->v.Subscript.slice) && ctx != Del) {
    6164       29382 :         if (!compiler_slice(c, e->v.Subscript.slice)) {
    6165           0 :             return 0;
    6166             :         }
    6167       29382 :         if (ctx == Load) {
    6168       27903 :             ADDOP(c, BINARY_SLICE);
    6169             :         }
    6170             :         else {
    6171        1479 :             assert(ctx == Store);
    6172        1479 :             ADDOP(c, STORE_SLICE);
    6173             :         }
    6174             :     }
    6175             :     else {
    6176      155154 :         VISIT(c, expr, e->v.Subscript.slice);
    6177      155154 :         switch (ctx) {
    6178      124274 :             case Load:    op = BINARY_SUBSCR; break;
    6179       26938 :             case Store:   op = STORE_SUBSCR; break;
    6180        3942 :             case Del:     op = DELETE_SUBSCR; break;
    6181             :         }
    6182      155154 :         assert(op);
    6183      155154 :         ADDOP(c, op);
    6184             :     }
    6185      184536 :     return 1;
    6186             : }
    6187             : 
    6188             : /* Returns the number of the values emitted,
    6189             :  * thus are needed to build the slice, or 0 if there is an error. */
    6190             : static int
    6191       31132 : compiler_slice(struct compiler *c, expr_ty s)
    6192             : {
    6193       31132 :     int n = 2;
    6194       31132 :     assert(s->kind == Slice_kind);
    6195             : 
    6196             :     /* only handles the cases where BUILD_SLICE is emitted */
    6197       31132 :     if (s->v.Slice.lower) {
    6198       17292 :         VISIT(c, expr, s->v.Slice.lower);
    6199             :     }
    6200             :     else {
    6201       13840 :         ADDOP_LOAD_CONST(c, Py_None);
    6202             :     }
    6203             : 
    6204       31132 :     if (s->v.Slice.upper) {
    6205       17560 :         VISIT(c, expr, s->v.Slice.upper);
    6206             :     }
    6207             :     else {
    6208       13572 :         ADDOP_LOAD_CONST(c, Py_None);
    6209             :     }
    6210             : 
    6211       31132 :     if (s->v.Slice.step) {
    6212         993 :         n++;
    6213         993 :         VISIT(c, expr, s->v.Slice.step);
    6214             :     }
    6215       31132 :     return n;
    6216             : }
    6217             : 
    6218             : 
    6219             : // PEP 634: Structural Pattern Matching
    6220             : 
    6221             : // To keep things simple, all compiler_pattern_* and pattern_helper_* routines
    6222             : // follow the convention of consuming TOS (the subject for the given pattern)
    6223             : // and calling jump_to_fail_pop on failure (no match).
    6224             : 
    6225             : // When calling into these routines, it's important that pc->on_top be kept
    6226             : // updated to reflect the current number of items that we are using on the top
    6227             : // of the stack: they will be popped on failure, and any name captures will be
    6228             : // stored *underneath* them on success. This lets us defer all names stores
    6229             : // until the *entire* pattern matches.
    6230             : 
    6231             : #define WILDCARD_CHECK(N) \
    6232             :     ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
    6233             : 
    6234             : #define WILDCARD_STAR_CHECK(N) \
    6235             :     ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
    6236             : 
    6237             : // Limit permitted subexpressions, even if the parser & AST validator let them through
    6238             : #define MATCH_VALUE_EXPR(N) \
    6239             :     ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
    6240             : 
    6241             : // Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
    6242             : static int
    6243        2769 : ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
    6244             : {
    6245        2769 :     Py_ssize_t size = n + 1;
    6246        2769 :     if (size <= pc->fail_pop_size) {
    6247        1273 :         return 1;
    6248             :     }
    6249        1496 :     Py_ssize_t needed = sizeof(basicblock*) * size;
    6250        1496 :     basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
    6251        1496 :     if (resized == NULL) {
    6252           0 :         PyErr_NoMemory();
    6253           0 :         return 0;
    6254             :     }
    6255        1496 :     pc->fail_pop = resized;
    6256        3992 :     while (pc->fail_pop_size < size) {
    6257             :         basicblock *new_block;
    6258        2496 :         RETURN_IF_FALSE(new_block = compiler_new_block(c));
    6259        2496 :         pc->fail_pop[pc->fail_pop_size++] = new_block;
    6260             :     }
    6261        1496 :     return 1;
    6262             : }
    6263             : 
    6264             : // Use op to jump to the correct fail_pop block.
    6265             : static int
    6266        2664 : jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
    6267             : {
    6268             :     // Pop any items on the top of the stack, plus any objects we were going to
    6269             :     // capture on success:
    6270        2664 :     Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
    6271        2664 :     RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
    6272        2664 :     ADDOP_JUMP(c, op, pc->fail_pop[pops]);
    6273        2664 :     return 1;
    6274             : }
    6275             : 
    6276             : // Build all of the fail_pop blocks and reset fail_pop.
    6277             : static int
    6278        1192 : emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
    6279             : {
    6280        1192 :     if (!pc->fail_pop_size) {
    6281          30 :         assert(pc->fail_pop == NULL);
    6282          30 :         return 1;
    6283             :     }
    6284        2468 :     while (--pc->fail_pop_size) {
    6285        1306 :         compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
    6286        1306 :         if (!compiler_addop(c, POP_TOP, true)) {
    6287           0 :             pc->fail_pop_size = 0;
    6288           0 :             PyObject_Free(pc->fail_pop);
    6289           0 :             pc->fail_pop = NULL;
    6290           0 :             return 0;
    6291             :         }
    6292             :     }
    6293        1162 :     compiler_use_next_block(c, pc->fail_pop[0]);
    6294        1162 :     PyObject_Free(pc->fail_pop);
    6295        1162 :     pc->fail_pop = NULL;
    6296        1162 :     return 1;
    6297             : }
    6298             : 
    6299             : static int
    6300           6 : compiler_error_duplicate_store(struct compiler *c, identifier n)
    6301             : {
    6302           6 :     return compiler_error(c, "multiple assignments to name %R in pattern", n);
    6303             : }
    6304             : 
    6305             : // Duplicate the effect of 3.10's ROT_* instructions using SWAPs.
    6306             : static int
    6307         971 : pattern_helper_rotate(struct compiler *c, Py_ssize_t count)
    6308             : {
    6309        4586 :     while (1 < count) {
    6310        3615 :         ADDOP_I(c, SWAP, count--);
    6311             :     }
    6312         971 :     return 1;
    6313             : }
    6314             : 
    6315             : static int
    6316         788 : pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
    6317             : {
    6318         788 :     if (n == NULL) {
    6319          63 :         ADDOP(c, POP_TOP);
    6320          63 :         return 1;
    6321             :     }
    6322         725 :     if (forbidden_name(c, n, Store)) {
    6323           0 :         return 0;
    6324             :     }
    6325             :     // Can't assign to the same name twice:
    6326         725 :     int duplicate = PySequence_Contains(pc->stores, n);
    6327         725 :     if (duplicate < 0) {
    6328           0 :         return 0;
    6329             :     }
    6330         725 :     if (duplicate) {
    6331           6 :         return compiler_error_duplicate_store(c, n);
    6332             :     }
    6333             :     // Rotate this object underneath any items we need to preserve:
    6334         719 :     Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1;
    6335         719 :     RETURN_IF_FALSE(pattern_helper_rotate(c, rotations));
    6336         719 :     return !PyList_Append(pc->stores, n);
    6337             : }
    6338             : 
    6339             : 
    6340             : static int
    6341         350 : pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
    6342             : {
    6343         350 :     Py_ssize_t n = asdl_seq_LEN(elts);
    6344         350 :     int seen_star = 0;
    6345        1114 :     for (Py_ssize_t i = 0; i < n; i++) {
    6346         764 :         pattern_ty elt = asdl_seq_GET(elts, i);
    6347         764 :         if (elt->kind == MatchStar_kind && !seen_star) {
    6348          63 :             if ((i >= (1 << 8)) ||
    6349          63 :                 (n-i-1 >= (INT_MAX >> 8)))
    6350           0 :                 return compiler_error(c,
    6351             :                     "too many expressions in "
    6352             :                     "star-unpacking sequence pattern");
    6353          63 :             ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
    6354          63 :             seen_star = 1;
    6355             :         }
    6356         701 :         else if (elt->kind == MatchStar_kind) {
    6357           0 :             return compiler_error(c,
    6358             :                 "multiple starred expressions in sequence pattern");
    6359             :         }
    6360             :     }
    6361         350 :     if (!seen_star) {
    6362         287 :         ADDOP_I(c, UNPACK_SEQUENCE, n);
    6363             :     }
    6364         350 :     return 1;
    6365             : }
    6366             : 
    6367             : static int
    6368         350 : pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
    6369             :                                Py_ssize_t star, pattern_context *pc)
    6370             : {
    6371         350 :     RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
    6372         350 :     Py_ssize_t size = asdl_seq_LEN(patterns);
    6373             :     // We've now got a bunch of new subjects on the stack. They need to remain
    6374             :     // there after each subpattern match:
    6375         350 :     pc->on_top += size;
    6376        1112 :     for (Py_ssize_t i = 0; i < size; i++) {
    6377             :         // One less item to keep track of each time we loop through:
    6378         764 :         pc->on_top--;
    6379         764 :         pattern_ty pattern = asdl_seq_GET(patterns, i);
    6380         764 :         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
    6381             :     }
    6382         348 :     return 1;
    6383             : }
    6384             : 
    6385             : // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
    6386             : // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
    6387             : // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
    6388             : static int
    6389          23 : pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
    6390             :                                Py_ssize_t star, pattern_context *pc)
    6391             : {
    6392             :     // We need to keep the subject around for extracting elements:
    6393          23 :     pc->on_top++;
    6394          23 :     Py_ssize_t size = asdl_seq_LEN(patterns);
    6395          90 :     for (Py_ssize_t i = 0; i < size; i++) {
    6396          67 :         pattern_ty pattern = asdl_seq_GET(patterns, i);
    6397          67 :         if (WILDCARD_CHECK(pattern)) {
    6398           8 :             continue;
    6399             :         }
    6400          59 :         if (i == star) {
    6401          23 :             assert(WILDCARD_STAR_CHECK(pattern));
    6402          23 :             continue;
    6403             :         }
    6404          36 :         ADDOP_I(c, COPY, 1);
    6405          36 :         if (i < star) {
    6406          22 :             ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
    6407             :         }
    6408             :         else {
    6409             :             // The subject may not support negative indexing! Compute a
    6410             :             // nonnegative index:
    6411          14 :             ADDOP(c, GET_LEN);
    6412          14 :             ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
    6413          14 :             ADDOP_BINARY(c, Sub);
    6414             :         }
    6415          36 :         ADDOP(c, BINARY_SUBSCR);
    6416          36 :         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
    6417             :     }
    6418             :     // Pop the subject, we're done with it:
    6419          23 :     pc->on_top--;
    6420          23 :     ADDOP(c, POP_TOP);
    6421          23 :     return 1;
    6422             : }
    6423             : 
    6424             : // Like compiler_pattern, but turn off checks for irrefutability.
    6425             : static int
    6426        1276 : compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
    6427             : {
    6428        1276 :     int allow_irrefutable = pc->allow_irrefutable;
    6429        1276 :     pc->allow_irrefutable = 1;
    6430        1276 :     RETURN_IF_FALSE(compiler_pattern(c, p, pc));
    6431        1270 :     pc->allow_irrefutable = allow_irrefutable;
    6432        1270 :     return 1;
    6433             : }
    6434             : 
    6435             : static int
    6436         721 : compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
    6437             : {
    6438         721 :     assert(p->kind == MatchAs_kind);
    6439         721 :     if (p->v.MatchAs.pattern == NULL) {
    6440             :         // An irrefutable match:
    6441         629 :         if (!pc->allow_irrefutable) {
    6442          11 :             if (p->v.MatchAs.name) {
    6443           5 :                 const char *e = "name capture %R makes remaining patterns unreachable";
    6444           5 :                 return compiler_error(c, e, p->v.MatchAs.name);
    6445             :             }
    6446           6 :             const char *e = "wildcard makes remaining patterns unreachable";
    6447           6 :             return compiler_error(c, e);
    6448             :         }
    6449         618 :         return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
    6450             :     }
    6451             :     // Need to make a copy for (possibly) storing later:
    6452          92 :     pc->on_top++;
    6453          92 :     ADDOP_I(c, COPY, 1);
    6454          92 :     RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
    6455             :     // Success! Store it:
    6456          91 :     pc->on_top--;
    6457          91 :     RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
    6458          90 :     return 1;
    6459             : }
    6460             : 
    6461             : static int
    6462          63 : compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
    6463             : {
    6464          63 :     assert(p->kind == MatchStar_kind);
    6465          63 :     RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
    6466          63 :     return 1;
    6467             : }
    6468             : 
    6469             : static int
    6470          45 : validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
    6471             : {
    6472             :     // Any errors will point to the pattern rather than the arg name as the
    6473             :     // parser is only supplying identifiers rather than Name or keyword nodes
    6474          45 :     Py_ssize_t nattrs = asdl_seq_LEN(attrs);
    6475         120 :     for (Py_ssize_t i = 0; i < nattrs; i++) {
    6476          76 :         identifier attr = ((identifier)asdl_seq_GET(attrs, i));
    6477          76 :         SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
    6478          76 :         if (forbidden_name(c, attr, Store)) {
    6479           0 :             return -1;
    6480             :         }
    6481         115 :         for (Py_ssize_t j = i + 1; j < nattrs; j++) {
    6482          40 :             identifier other = ((identifier)asdl_seq_GET(attrs, j));
    6483          40 :             if (!PyUnicode_Compare(attr, other)) {
    6484           1 :                 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
    6485           1 :                 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
    6486           1 :                 return -1;
    6487             :             }
    6488             :         }
    6489             :     }
    6490          44 :     return 0;
    6491             : }
    6492             : 
    6493             : static int
    6494         184 : compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
    6495             : {
    6496         184 :     assert(p->kind == MatchClass_kind);
    6497         184 :     asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
    6498         184 :     asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
    6499         184 :     asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
    6500         184 :     Py_ssize_t nargs = asdl_seq_LEN(patterns);
    6501         184 :     Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
    6502         184 :     Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
    6503         184 :     if (nattrs != nkwd_patterns) {
    6504             :         // AST validator shouldn't let this happen, but if it does,
    6505             :         // just fail, don't crash out of the interpreter
    6506           0 :         const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
    6507           0 :         return compiler_error(c, e, nattrs, nkwd_patterns);
    6508             :     }
    6509         184 :     if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
    6510           0 :         const char *e = "too many sub-patterns in class pattern %R";
    6511           0 :         return compiler_error(c, e, p->v.MatchClass.cls);
    6512             :     }
    6513         184 :     if (nattrs) {
    6514          45 :         RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
    6515          44 :         SET_LOC(c, p);
    6516             :     }
    6517         183 :     VISIT(c, expr, p->v.MatchClass.cls);
    6518             :     PyObject *attr_names;
    6519         183 :     RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
    6520             :     Py_ssize_t i;
    6521         258 :     for (i = 0; i < nattrs; i++) {
    6522          75 :         PyObject *name = asdl_seq_GET(kwd_attrs, i);
    6523          75 :         Py_INCREF(name);
    6524          75 :         PyTuple_SET_ITEM(attr_names, i, name);
    6525             :     }
    6526         183 :     ADDOP_LOAD_CONST_NEW(c, attr_names);
    6527         183 :     ADDOP_I(c, MATCH_CLASS, nargs);
    6528         183 :     ADDOP_I(c, COPY, 1);
    6529         183 :     ADDOP_LOAD_CONST(c, Py_None);
    6530         183 :     ADDOP_I(c, IS_OP, 1);
    6531             :     // TOS is now a tuple of (nargs + nattrs) attributes (or None):
    6532         183 :     pc->on_top++;
    6533         183 :     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6534         183 :     ADDOP_I(c, UNPACK_SEQUENCE, nargs + nattrs);
    6535         183 :     pc->on_top += nargs + nattrs - 1;
    6536         450 :     for (i = 0; i < nargs + nattrs; i++) {
    6537         270 :         pc->on_top--;
    6538             :         pattern_ty pattern;
    6539         270 :         if (i < nargs) {
    6540             :             // Positional:
    6541         195 :             pattern = asdl_seq_GET(patterns, i);
    6542             :         }
    6543             :         else {
    6544             :             // Keyword:
    6545          75 :             pattern = asdl_seq_GET(kwd_patterns, i - nargs);
    6546             :         }
    6547         270 :         if (WILDCARD_CHECK(pattern)) {
    6548          48 :             ADDOP(c, POP_TOP);
    6549          48 :             continue;
    6550             :         }
    6551         222 :         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
    6552             :     }
    6553             :     // Success! Pop the tuple of attributes:
    6554         180 :     return 1;
    6555             : }
    6556             : 
    6557             : static int
    6558         271 : compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
    6559             : {
    6560         271 :     assert(p->kind == MatchMapping_kind);
    6561         271 :     asdl_expr_seq *keys = p->v.MatchMapping.keys;
    6562         271 :     asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
    6563         271 :     Py_ssize_t size = asdl_seq_LEN(keys);
    6564         271 :     Py_ssize_t npatterns = asdl_seq_LEN(patterns);
    6565         271 :     if (size != npatterns) {
    6566             :         // AST validator shouldn't let this happen, but if it does,
    6567             :         // just fail, don't crash out of the interpreter
    6568           0 :         const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
    6569           0 :         return compiler_error(c, e, size, npatterns);
    6570             :     }
    6571             :     // We have a double-star target if "rest" is set
    6572         271 :     PyObject *star_target = p->v.MatchMapping.rest;
    6573             :     // We need to keep the subject on top during the mapping and length checks:
    6574         271 :     pc->on_top++;
    6575         271 :     ADDOP(c, MATCH_MAPPING);
    6576         271 :     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6577         271 :     if (!size && !star_target) {
    6578             :         // If the pattern is just "{}", we're done! Pop the subject:
    6579          48 :         pc->on_top--;
    6580          48 :         ADDOP(c, POP_TOP);
    6581          48 :         return 1;
    6582             :     }
    6583         223 :     if (size) {
    6584             :         // If the pattern has any keys in it, perform a length check:
    6585         217 :         ADDOP(c, GET_LEN);
    6586         217 :         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
    6587         217 :         ADDOP_COMPARE(c, GtE);
    6588         217 :         RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6589             :     }
    6590         223 :     if (INT_MAX < size - 1) {
    6591           0 :         return compiler_error(c, "too many sub-patterns in mapping pattern");
    6592             :     }
    6593             :     // Collect all of the keys into a tuple for MATCH_KEYS and
    6594             :     // **rest. They can either be dotted names or literals:
    6595             : 
    6596             :     // Maintaining a set of Constant_kind kind keys allows us to raise a
    6597             :     // SyntaxError in the case of duplicates.
    6598         223 :     PyObject *seen = PySet_New(NULL);
    6599         223 :     if (seen == NULL) {
    6600           0 :         return 0;
    6601             :     }
    6602             : 
    6603             :     // NOTE: goto error on failure in the loop below to avoid leaking `seen`
    6604         482 :     for (Py_ssize_t i = 0; i < size; i++) {
    6605         265 :         expr_ty key = asdl_seq_GET(keys, i);
    6606         265 :         if (key == NULL) {
    6607           0 :             const char *e = "can't use NULL keys in MatchMapping "
    6608             :                             "(set 'rest' parameter instead)";
    6609           0 :             SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
    6610           0 :             compiler_error(c, e);
    6611           0 :             goto error;
    6612             :         }
    6613             : 
    6614         265 :         if (key->kind == Constant_kind) {
    6615         262 :             int in_seen = PySet_Contains(seen, key->v.Constant.value);
    6616         262 :             if (in_seen < 0) {
    6617           0 :                 goto error;
    6618             :             }
    6619         262 :             if (in_seen) {
    6620           5 :                 const char *e = "mapping pattern checks duplicate key (%R)";
    6621           5 :                 compiler_error(c, e, key->v.Constant.value);
    6622           5 :                 goto error;
    6623             :             }
    6624         257 :             if (PySet_Add(seen, key->v.Constant.value)) {
    6625           0 :                 goto error;
    6626             :             }
    6627             :         }
    6628             : 
    6629           3 :         else if (key->kind != Attribute_kind) {
    6630           1 :             const char *e = "mapping pattern keys may only match literals and attribute lookups";
    6631           1 :             compiler_error(c, e);
    6632           1 :             goto error;
    6633             :         }
    6634         259 :         if (!compiler_visit_expr(c, key)) {
    6635           0 :             goto error;
    6636             :         }
    6637             :     }
    6638             : 
    6639             :     // all keys have been checked; there are no duplicates
    6640         217 :     Py_DECREF(seen);
    6641             : 
    6642         217 :     ADDOP_I(c, BUILD_TUPLE, size);
    6643         217 :     ADDOP(c, MATCH_KEYS);
    6644             :     // There's now a tuple of keys and a tuple of values on top of the subject:
    6645         217 :     pc->on_top += 2;
    6646         217 :     ADDOP_I(c, COPY, 1);
    6647         217 :     ADDOP_LOAD_CONST(c, Py_None);
    6648         217 :     ADDOP_I(c, IS_OP, 1);
    6649         217 :     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6650             :     // So far so good. Use that tuple of values on the stack to match
    6651             :     // sub-patterns against:
    6652         217 :     ADDOP_I(c, UNPACK_SEQUENCE, size);
    6653         217 :     pc->on_top += size - 1;
    6654         470 :     for (Py_ssize_t i = 0; i < size; i++) {
    6655         254 :         pc->on_top--;
    6656         254 :         pattern_ty pattern = asdl_seq_GET(patterns, i);
    6657         254 :         RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
    6658             :     }
    6659             :     // If we get this far, it's a match! Whatever happens next should consume
    6660             :     // the tuple of keys and the subject:
    6661         216 :     pc->on_top -= 2;
    6662         216 :     if (star_target) {
    6663             :         // If we have a starred name, bind a dict of remaining items to it (this may
    6664             :         // seem a bit inefficient, but keys is rarely big enough to actually impact
    6665             :         // runtime):
    6666             :         // rest = dict(TOS1)
    6667             :         // for key in TOS:
    6668             :         //     del rest[key]
    6669          16 :         ADDOP_I(c, BUILD_MAP, 0);           // [subject, keys, empty]
    6670          16 :         ADDOP_I(c, SWAP, 3);                // [empty, keys, subject]
    6671          16 :         ADDOP_I(c, DICT_UPDATE, 2);         // [copy, keys]
    6672          16 :         ADDOP_I(c, UNPACK_SEQUENCE, size);  // [copy, keys...]
    6673          30 :         while (size) {
    6674          14 :             ADDOP_I(c, COPY, 1 + size--);   // [copy, keys..., copy]
    6675          14 :             ADDOP_I(c, SWAP, 2);            // [copy, keys..., copy, key]
    6676          14 :             ADDOP(c, DELETE_SUBSCR);        // [copy, keys...]
    6677             :         }
    6678          16 :         RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
    6679             :     }
    6680             :     else {
    6681         200 :         ADDOP(c, POP_TOP);  // Tuple of keys.
    6682         200 :         ADDOP(c, POP_TOP);  // Subject.
    6683             :     }
    6684         216 :     return 1;
    6685             : 
    6686           6 : error:
    6687           6 :     Py_DECREF(seen);
    6688           6 :     return 0;
    6689             : }
    6690             : 
    6691             : static int
    6692         110 : compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
    6693             : {
    6694         110 :     assert(p->kind == MatchOr_kind);
    6695             :     basicblock *end;
    6696         110 :     RETURN_IF_FALSE(end = compiler_new_block(c));
    6697         110 :     Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
    6698         110 :     assert(size > 1);
    6699             :     // We're going to be messing with pc. Keep the original info handy:
    6700         110 :     pattern_context old_pc = *pc;
    6701         110 :     Py_INCREF(pc->stores);
    6702             :     // control is the list of names bound by the first alternative. It is used
    6703             :     // for checking different name bindings in alternatives, and for correcting
    6704             :     // the order in which extracted elements are placed on the stack.
    6705         110 :     PyObject *control = NULL;
    6706             :     // NOTE: We can't use returning macros anymore! goto error on error.
    6707         389 :     for (Py_ssize_t i = 0; i < size; i++) {
    6708         289 :         pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
    6709         289 :         SET_LOC(c, alt);
    6710         289 :         PyObject *pc_stores = PyList_New(0);
    6711         289 :         if (pc_stores == NULL) {
    6712           0 :             goto error;
    6713             :         }
    6714         289 :         Py_SETREF(pc->stores, pc_stores);
    6715             :         // An irrefutable sub-pattern must be last, if it is allowed at all:
    6716         289 :         pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
    6717         289 :         pc->fail_pop = NULL;
    6718         289 :         pc->fail_pop_size = 0;
    6719         289 :         pc->on_top = 0;
    6720         289 :         if (!compiler_addop_i(c, COPY, 1, true) || !compiler_pattern(c, alt, pc)) {
    6721           8 :             goto error;
    6722             :         }
    6723             :         // Success!
    6724         281 :         Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
    6725         281 :         if (!i) {
    6726             :             // This is the first alternative, so save its stores as a "control"
    6727             :             // for the others (they can't bind a different set of names, and
    6728             :             // might need to be reordered):
    6729         103 :             assert(control == NULL);
    6730         103 :             control = pc->stores;
    6731         103 :             Py_INCREF(control);
    6732             :         }
    6733         178 :         else if (nstores != PyList_GET_SIZE(control)) {
    6734           1 :             goto diff;
    6735             :         }
    6736         177 :         else if (nstores) {
    6737             :             // There were captures. Check to see if we differ from control:
    6738          35 :             Py_ssize_t icontrol = nstores;
    6739         165 :             while (icontrol--) {
    6740         131 :                 PyObject *name = PyList_GET_ITEM(control, icontrol);
    6741         131 :                 Py_ssize_t istores = PySequence_Index(pc->stores, name);
    6742         131 :                 if (istores < 0) {
    6743           1 :                     PyErr_Clear();
    6744           1 :                     goto diff;
    6745             :                 }
    6746         130 :                 if (icontrol != istores) {
    6747             :                     // Reorder the names on the stack to match the order of the
    6748             :                     // names in control. There's probably a better way of doing
    6749             :                     // this; the current solution is potentially very
    6750             :                     // inefficient when each alternative subpattern binds lots
    6751             :                     // of names in different orders. It's fine for reasonable
    6752             :                     // cases, though, and the peephole optimizer will ensure
    6753             :                     // that the final code is as efficient as possible.
    6754          60 :                     assert(istores < icontrol);
    6755          60 :                     Py_ssize_t rotations = istores + 1;
    6756             :                     // Perform the same rotation on pc->stores:
    6757          60 :                     PyObject *rotated = PyList_GetSlice(pc->stores, 0,
    6758             :                                                         rotations);
    6759         120 :                     if (rotated == NULL ||
    6760         120 :                         PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
    6761          60 :                         PyList_SetSlice(pc->stores, icontrol - istores,
    6762             :                                         icontrol - istores, rotated))
    6763             :                     {
    6764           0 :                         Py_XDECREF(rotated);
    6765           0 :                         goto error;
    6766             :                     }
    6767          60 :                     Py_DECREF(rotated);
    6768             :                     // That just did:
    6769             :                     // rotated = pc_stores[:rotations]
    6770             :                     // del pc_stores[:rotations]
    6771             :                     // pc_stores[icontrol-istores:icontrol-istores] = rotated
    6772             :                     // Do the same thing to the stack, using several
    6773             :                     // rotations:
    6774         264 :                     while (rotations--) {
    6775         204 :                         if (!pattern_helper_rotate(c, icontrol + 1)){
    6776           0 :                             goto error;
    6777             :                         }
    6778             :                     }
    6779             :                 }
    6780             :             }
    6781             :         }
    6782         279 :         assert(control);
    6783         558 :         if (!compiler_addop_j(c, JUMP, end, true) ||
    6784         279 :             !emit_and_reset_fail_pop(c, pc))
    6785             :         {
    6786           0 :             goto error;
    6787             :         }
    6788             :     }
    6789         100 :     Py_DECREF(pc->stores);
    6790         100 :     *pc = old_pc;
    6791         100 :     Py_INCREF(pc->stores);
    6792             :     // Need to NULL this for the PyObject_Free call in the error block.
    6793         100 :     old_pc.fail_pop = NULL;
    6794             :     // No match. Pop the remaining copy of the subject and fail:
    6795         100 :     if (!compiler_addop(c, POP_TOP, true) || !jump_to_fail_pop(c, pc, JUMP)) {
    6796           0 :         goto error;
    6797             :     }
    6798         100 :     compiler_use_next_block(c, end);
    6799         100 :     Py_ssize_t nstores = PyList_GET_SIZE(control);
    6800             :     // There's a bunch of stuff on the stack between where the new stores
    6801             :     // are and where they need to be:
    6802             :     // - The other stores.
    6803             :     // - A copy of the subject.
    6804             :     // - Anything else that may be on top of the stack.
    6805             :     // - Any previous stores we've already stashed away on the stack.
    6806         100 :     Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
    6807         148 :     for (Py_ssize_t i = 0; i < nstores; i++) {
    6808             :         // Rotate this capture to its proper place on the stack:
    6809          48 :         if (!pattern_helper_rotate(c, nrots)) {
    6810           0 :             goto error;
    6811             :         }
    6812             :         // Update the list of previous stores with this new name, checking for
    6813             :         // duplicates:
    6814          48 :         PyObject *name = PyList_GET_ITEM(control, i);
    6815          48 :         int dupe = PySequence_Contains(pc->stores, name);
    6816          48 :         if (dupe < 0) {
    6817           0 :             goto error;
    6818             :         }
    6819          48 :         if (dupe) {
    6820           0 :             compiler_error_duplicate_store(c, name);
    6821           0 :             goto error;
    6822             :         }
    6823          48 :         if (PyList_Append(pc->stores, name)) {
    6824           0 :             goto error;
    6825             :         }
    6826             :     }
    6827         100 :     Py_DECREF(old_pc.stores);
    6828         100 :     Py_DECREF(control);
    6829             :     // NOTE: Returning macros are safe again.
    6830             :     // Pop the copy of the subject:
    6831         100 :     ADDOP(c, POP_TOP);
    6832         100 :     return 1;
    6833           2 : diff:
    6834           2 :     compiler_error(c, "alternative patterns bind different names");
    6835          10 : error:
    6836          10 :     PyObject_Free(old_pc.fail_pop);
    6837          10 :     Py_DECREF(old_pc.stores);
    6838          10 :     Py_XDECREF(control);
    6839          10 :     return 0;
    6840             : }
    6841             : 
    6842             : 
    6843             : static int
    6844         446 : compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
    6845             : {
    6846         446 :     assert(p->kind == MatchSequence_kind);
    6847         446 :     asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
    6848         446 :     Py_ssize_t size = asdl_seq_LEN(patterns);
    6849         446 :     Py_ssize_t star = -1;
    6850         446 :     int only_wildcard = 1;
    6851         446 :     int star_wildcard = 0;
    6852             :     // Find a starred name, if it exists. There may be at most one:
    6853        1304 :     for (Py_ssize_t i = 0; i < size; i++) {
    6854         860 :         pattern_ty pattern = asdl_seq_GET(patterns, i);
    6855         860 :         if (pattern->kind == MatchStar_kind) {
    6856         101 :             if (star >= 0) {
    6857           2 :                 const char *e = "multiple starred names in sequence pattern";
    6858           2 :                 return compiler_error(c, e);
    6859             :             }
    6860          99 :             star_wildcard = WILDCARD_STAR_CHECK(pattern);
    6861          99 :             only_wildcard &= star_wildcard;
    6862          99 :             star = i;
    6863          99 :             continue;
    6864             :         }
    6865         759 :         only_wildcard &= WILDCARD_CHECK(pattern);
    6866             :     }
    6867             :     // We need to keep the subject on top during the sequence and length checks:
    6868         444 :     pc->on_top++;
    6869         444 :     ADDOP(c, MATCH_SEQUENCE);
    6870         444 :     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6871         444 :     if (star < 0) {
    6872             :         // No star: len(subject) == size
    6873         347 :         ADDOP(c, GET_LEN);
    6874         347 :         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
    6875         347 :         ADDOP_COMPARE(c, Eq);
    6876         347 :         RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6877             :     }
    6878          97 :     else if (size > 1) {
    6879             :         // Star: len(subject) >= size - 1
    6880          82 :         ADDOP(c, GET_LEN);
    6881          82 :         ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
    6882          82 :         ADDOP_COMPARE(c, GtE);
    6883          82 :         RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6884             :     }
    6885             :     // Whatever comes next should consume the subject:
    6886         444 :     pc->on_top--;
    6887         444 :     if (only_wildcard) {
    6888             :         // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
    6889          71 :         ADDOP(c, POP_TOP);
    6890             :     }
    6891         373 :     else if (star_wildcard) {
    6892          23 :         RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
    6893             :     }
    6894             :     else {
    6895         350 :         RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
    6896             :     }
    6897         442 :     return 1;
    6898             : }
    6899             : 
    6900             : static int
    6901         769 : compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
    6902             : {
    6903         769 :     assert(p->kind == MatchValue_kind);
    6904         769 :     expr_ty value = p->v.MatchValue.value;
    6905         769 :     if (!MATCH_VALUE_EXPR(value)) {
    6906           2 :         const char *e = "patterns may only match literals and attribute lookups";
    6907           2 :         return compiler_error(c, e);
    6908             :     }
    6909         767 :     VISIT(c, expr, value);
    6910         767 :     ADDOP_COMPARE(c, Eq);
    6911         767 :     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6912         767 :     return 1;
    6913             : }
    6914             : 
    6915             : static int
    6916          36 : compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
    6917             : {
    6918          36 :     assert(p->kind == MatchSingleton_kind);
    6919          36 :     ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
    6920          36 :     ADDOP_COMPARE(c, Is);
    6921          36 :     RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
    6922          36 :     return 1;
    6923             : }
    6924             : 
    6925             : static int
    6926        2600 : compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
    6927             : {
    6928        2600 :     SET_LOC(c, p);
    6929        2600 :     switch (p->kind) {
    6930         769 :         case MatchValue_kind:
    6931         769 :             return compiler_pattern_value(c, p, pc);
    6932          36 :         case MatchSingleton_kind:
    6933          36 :             return compiler_pattern_singleton(c, p, pc);
    6934         446 :         case MatchSequence_kind:
    6935         446 :             return compiler_pattern_sequence(c, p, pc);
    6936         271 :         case MatchMapping_kind:
    6937         271 :             return compiler_pattern_mapping(c, p, pc);
    6938         184 :         case MatchClass_kind:
    6939         184 :             return compiler_pattern_class(c, p, pc);
    6940          63 :         case MatchStar_kind:
    6941          63 :             return compiler_pattern_star(c, p, pc);
    6942         721 :         case MatchAs_kind:
    6943         721 :             return compiler_pattern_as(c, p, pc);
    6944         110 :         case MatchOr_kind:
    6945         110 :             return compiler_pattern_or(c, p, pc);
    6946             :     }
    6947             :     // AST validator shouldn't let this happen, but if it does,
    6948             :     // just fail, don't crash out of the interpreter
    6949           0 :     const char *e = "invalid match pattern node in AST (kind=%d)";
    6950           0 :     return compiler_error(c, e, p->kind);
    6951             : }
    6952             : 
    6953             : static int
    6954         679 : compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
    6955             : {
    6956         679 :     VISIT(c, expr, s->v.Match.subject);
    6957             :     basicblock *end;
    6958         679 :     RETURN_IF_FALSE(end = compiler_new_block(c));
    6959         679 :     Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
    6960         679 :     assert(cases > 0);
    6961         679 :     match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
    6962         679 :     int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
    6963        1592 :     for (Py_ssize_t i = 0; i < cases - has_default; i++) {
    6964         943 :         m = asdl_seq_GET(s->v.Match.cases, i);
    6965         943 :         SET_LOC(c, m->pattern);
    6966             :         // Only copy the subject if we're *not* on the last case:
    6967         943 :         if (i != cases - has_default - 1) {
    6968         267 :             ADDOP_I(c, COPY, 1);
    6969             :         }
    6970         943 :         RETURN_IF_FALSE(pc->stores = PyList_New(0));
    6971             :         // Irrefutable cases must be either guarded, last, or both:
    6972         943 :         pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
    6973         943 :         pc->fail_pop = NULL;
    6974         943 :         pc->fail_pop_size = 0;
    6975         943 :         pc->on_top = 0;
    6976             :         // NOTE: Can't use returning macros here (they'll leak pc->stores)!
    6977         943 :         if (!compiler_pattern(c, m->pattern, pc)) {
    6978          30 :             Py_DECREF(pc->stores);
    6979          30 :             return 0;
    6980             :         }
    6981         913 :         assert(!pc->on_top);
    6982             :         // It's a match! Store all of the captured names (they're on the stack).
    6983         913 :         Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
    6984        1492 :         for (Py_ssize_t n = 0; n < nstores; n++) {
    6985         579 :             PyObject *name = PyList_GET_ITEM(pc->stores, n);
    6986         579 :             if (!compiler_nameop(c, name, Store)) {
    6987           0 :                 Py_DECREF(pc->stores);
    6988           0 :                 return 0;
    6989             :             }
    6990             :         }
    6991         913 :         Py_DECREF(pc->stores);
    6992             :         // NOTE: Returning macros are safe again.
    6993         913 :         if (m->guard) {
    6994         105 :             RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
    6995         105 :             RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
    6996             :         }
    6997             :         // Success! Pop the subject off, we're done with it:
    6998         913 :         if (i != cases - has_default - 1) {
    6999         264 :             ADDOP(c, POP_TOP);
    7000             :         }
    7001        1936 :         VISIT_SEQ(c, stmt, m->body);
    7002         913 :         ADDOP_JUMP(c, JUMP, end);
    7003             :         // If the pattern fails to match, we want the line number of the
    7004             :         // cleanup to be associated with the failed pattern, not the last line
    7005             :         // of the body
    7006         913 :         SET_LOC(c, m->pattern);
    7007         913 :         RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
    7008             :     }
    7009         649 :     if (has_default) {
    7010             :         // A trailing "case _" is common, and lets us save a bit of redundant
    7011             :         // pushing and popping in the loop above:
    7012          37 :         m = asdl_seq_GET(s->v.Match.cases, cases - 1);
    7013          37 :         SET_LOC(c, m->pattern);
    7014          37 :         if (cases == 1) {
    7015             :             // No matches. Done with the subject:
    7016           0 :             ADDOP(c, POP_TOP);
    7017             :         }
    7018             :         else {
    7019             :             // Show line coverage for default case (it doesn't create bytecode)
    7020          37 :             ADDOP(c, NOP);
    7021             :         }
    7022          37 :         if (m->guard) {
    7023           2 :             RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
    7024             :         }
    7025          74 :         VISIT_SEQ(c, stmt, m->body);
    7026             :     }
    7027         649 :     compiler_use_next_block(c, end);
    7028         649 :     return 1;
    7029             : }
    7030             : 
    7031             : static int
    7032         679 : compiler_match(struct compiler *c, stmt_ty s)
    7033             : {
    7034             :     pattern_context pc;
    7035         679 :     pc.fail_pop = NULL;
    7036         679 :     int result = compiler_match_inner(c, s, &pc);
    7037         679 :     PyObject_Free(pc.fail_pop);
    7038         679 :     return result;
    7039             : }
    7040             : 
    7041             : #undef WILDCARD_CHECK
    7042             : #undef WILDCARD_STAR_CHECK
    7043             : 
    7044             : 
    7045             : /* End of the compiler section, beginning of the assembler section */
    7046             : 
    7047             : 
    7048             : struct assembler {
    7049             :     PyObject *a_bytecode;  /* bytes containing bytecode */
    7050             :     int a_offset;              /* offset into bytecode */
    7051             :     PyObject *a_except_table;  /* bytes containing exception table */
    7052             :     int a_except_table_off;    /* offset into exception table */
    7053             :     /* Location Info */
    7054             :     int a_lineno;          /* lineno of last emitted instruction */
    7055             :     PyObject* a_linetable; /* bytes containing location info */
    7056             :     int a_location_off;    /* offset of last written location info frame */
    7057             : };
    7058             : 
    7059             : static basicblock**
    7060     2158050 : make_cfg_traversal_stack(basicblock *entryblock) {
    7061     2158050 :     int nblocks = 0;
    7062    15196500 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7063    13038400 :         b->b_visited = 0;
    7064    13038400 :         nblocks++;
    7065             :     }
    7066     2158050 :     basicblock **stack = (basicblock **)PyMem_Malloc(sizeof(basicblock *) * nblocks);
    7067     2158050 :     if (!stack) {
    7068           0 :         PyErr_NoMemory();
    7069             :     }
    7070     2158050 :     return stack;
    7071             : }
    7072             : 
    7073             : Py_LOCAL_INLINE(void)
    7074     2806670 : stackdepth_push(basicblock ***sp, basicblock *b, int depth)
    7075             : {
    7076     2806670 :     assert(b->b_startdepth < 0 || b->b_startdepth == depth);
    7077     2806670 :     if (b->b_startdepth < depth && b->b_startdepth < 100) {
    7078     2241920 :         assert(b->b_startdepth < 0);
    7079     2241920 :         b->b_startdepth = depth;
    7080     2241920 :         *(*sp)++ = b;
    7081             :     }
    7082     2806670 : }
    7083             : 
    7084             : /* Find the flow path that needs the largest stack.  We assume that
    7085             :  * cycles in the flow graph have no net effect on the stack depth.
    7086             :  */
    7087             : static int
    7088      465113 : stackdepth(basicblock *entryblock, int code_flags)
    7089             : {
    7090     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7091     2241920 :         b->b_startdepth = INT_MIN;
    7092             :     }
    7093      465113 :     basicblock **stack = make_cfg_traversal_stack(entryblock);
    7094      465113 :     if (!stack) {
    7095           0 :         return -1;
    7096             :     }
    7097             : 
    7098      465113 :     int maxdepth = 0;
    7099      465113 :     basicblock **sp = stack;
    7100      465113 :     if (code_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
    7101       16608 :         stackdepth_push(&sp, entryblock, 1);
    7102             :     } else {
    7103      448505 :         stackdepth_push(&sp, entryblock, 0);
    7104             :     }
    7105             : 
    7106     2707030 :     while (sp != stack) {
    7107     2241920 :         basicblock *b = *--sp;
    7108     2241920 :         int depth = b->b_startdepth;
    7109     2241920 :         assert(depth >= 0);
    7110     2241920 :         basicblock *next = b->b_next;
    7111    18884100 :         for (int i = 0; i < b->b_iused; i++) {
    7112    17612200 :             struct instr *instr = &b->b_instr[i];
    7113    17612200 :             int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
    7114    17612200 :             if (effect == PY_INVALID_STACK_EFFECT) {
    7115           0 :                 PyErr_Format(PyExc_SystemError,
    7116             :                              "compiler stack_effect(opcode=%d, arg=%i) failed",
    7117             :                              instr->i_opcode, instr->i_oparg);
    7118           0 :                 return -1;
    7119             :             }
    7120    17612200 :             int new_depth = depth + effect;
    7121    17612200 :             if (new_depth > maxdepth) {
    7122     1693190 :                 maxdepth = new_depth;
    7123             :             }
    7124    17612200 :             assert(depth >= 0); /* invalid code or bug in stackdepth() */
    7125    17612200 :             if (is_jump(instr) || is_block_push(instr)) {
    7126     1069650 :                 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
    7127     1069650 :                 assert(effect != PY_INVALID_STACK_EFFECT);
    7128     1069650 :                 int target_depth = depth + effect;
    7129     1069650 :                 if (target_depth > maxdepth) {
    7130       15708 :                     maxdepth = target_depth;
    7131             :                 }
    7132     1069650 :                 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
    7133     1069650 :                 stackdepth_push(&sp, instr->i_target, target_depth);
    7134             :             }
    7135    17612200 :             depth = new_depth;
    7136    17612200 :             assert(!IS_ASSEMBLER_OPCODE(instr->i_opcode));
    7137    17612200 :             if (IS_UNCONDITIONAL_JUMP_OPCODE(instr->i_opcode) ||
    7138    17392600 :                 IS_SCOPE_EXIT_OPCODE(instr->i_opcode))
    7139             :             {
    7140             :                 /* remaining code is dead */
    7141      970021 :                 next = NULL;
    7142      970021 :                 break;
    7143             :             }
    7144    16642200 :             if (instr->i_opcode == YIELD_VALUE) {
    7145       23715 :                 instr->i_oparg = depth;
    7146             :             }
    7147             :         }
    7148     2241920 :         if (next != NULL) {
    7149     1271900 :             assert(BB_HAS_FALLTHROUGH(b));
    7150     1271900 :             stackdepth_push(&sp, next, depth);
    7151             :         }
    7152             :     }
    7153      465113 :     PyMem_Free(stack);
    7154      465113 :     return maxdepth;
    7155             : }
    7156             : 
    7157             : static int
    7158      465113 : assemble_init(struct assembler *a, int firstlineno)
    7159             : {
    7160      465113 :     memset(a, 0, sizeof(struct assembler));
    7161      465113 :     a->a_lineno = firstlineno;
    7162      465113 :     a->a_linetable = NULL;
    7163      465113 :     a->a_location_off = 0;
    7164      465113 :     a->a_except_table = NULL;
    7165      465113 :     a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
    7166      465113 :     if (a->a_bytecode == NULL) {
    7167           0 :         goto error;
    7168             :     }
    7169      465113 :     a->a_linetable = PyBytes_FromStringAndSize(NULL, DEFAULT_CNOTAB_SIZE);
    7170      465113 :     if (a->a_linetable == NULL) {
    7171           0 :         goto error;
    7172             :     }
    7173      465113 :     a->a_except_table = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
    7174      465113 :     if (a->a_except_table == NULL) {
    7175           0 :         goto error;
    7176             :     }
    7177      465113 :     return 1;
    7178           0 : error:
    7179           0 :     Py_XDECREF(a->a_bytecode);
    7180           0 :     Py_XDECREF(a->a_linetable);
    7181           0 :     Py_XDECREF(a->a_except_table);
    7182           0 :     return 0;
    7183             : }
    7184             : 
    7185             : static void
    7186      465113 : assemble_free(struct assembler *a)
    7187             : {
    7188      465113 :     Py_XDECREF(a->a_bytecode);
    7189      465113 :     Py_XDECREF(a->a_linetable);
    7190      465113 :     Py_XDECREF(a->a_except_table);
    7191      465113 : }
    7192             : 
    7193             : static int
    7194     2544110 : blocksize(basicblock *b)
    7195             : {
    7196             :     int i;
    7197     2544110 :     int size = 0;
    7198             : 
    7199    22289300 :     for (i = 0; i < b->b_iused; i++) {
    7200    19745200 :         size += instr_size(&b->b_instr[i]);
    7201             :     }
    7202     2544110 :     return size;
    7203             : }
    7204             : 
    7205             : static basicblock *
    7206      131559 : push_except_block(ExceptStack *stack, struct instr *setup) {
    7207      131559 :     assert(is_block_push(setup));
    7208      131559 :     int opcode = setup->i_opcode;
    7209      131559 :     basicblock * target = setup->i_target;
    7210      131559 :     if (opcode == SETUP_WITH || opcode == SETUP_CLEANUP) {
    7211       90637 :         target->b_preserve_lasti = 1;
    7212             :     }
    7213      131559 :     stack->handlers[++stack->depth] = target;
    7214      131559 :     return target;
    7215             : }
    7216             : 
    7217             : static basicblock *
    7218      121220 : pop_except_block(ExceptStack *stack) {
    7219      121220 :     assert(stack->depth > 0);
    7220      121220 :     return stack->handlers[--stack->depth];
    7221             : }
    7222             : 
    7223             : static basicblock *
    7224     2241920 : except_stack_top(ExceptStack *stack) {
    7225     2241920 :     return stack->handlers[stack->depth];
    7226             : }
    7227             : 
    7228             : static ExceptStack *
    7229      465113 : make_except_stack(void) {
    7230      465113 :     ExceptStack *new = PyMem_Malloc(sizeof(ExceptStack));
    7231      465113 :     if (new == NULL) {
    7232           0 :         PyErr_NoMemory();
    7233           0 :         return NULL;
    7234             :     }
    7235      465113 :     new->depth = 0;
    7236      465113 :     new->handlers[0] = NULL;
    7237      465113 :     return new;
    7238             : }
    7239             : 
    7240             : static ExceptStack *
    7241      767937 : copy_except_stack(ExceptStack *stack) {
    7242      767937 :     ExceptStack *copy = PyMem_Malloc(sizeof(ExceptStack));
    7243      767937 :     if (copy == NULL) {
    7244           0 :         PyErr_NoMemory();
    7245           0 :         return NULL;
    7246             :     }
    7247      767937 :     memcpy(copy, stack, sizeof(ExceptStack));
    7248      767937 :     return copy;
    7249             : }
    7250             : 
    7251             : static int
    7252      465113 : label_exception_targets(basicblock *entryblock) {
    7253      465113 :     basicblock **todo_stack = make_cfg_traversal_stack(entryblock);
    7254      465113 :     if (todo_stack == NULL) {
    7255           0 :         return -1;
    7256             :     }
    7257      465113 :     ExceptStack *except_stack = make_except_stack();
    7258      465113 :     if (except_stack == NULL) {
    7259           0 :         PyMem_Free(todo_stack);
    7260           0 :         PyErr_NoMemory();
    7261           0 :         return -1;
    7262             :     }
    7263      465113 :     except_stack->depth = 0;
    7264      465113 :     todo_stack[0] = entryblock;
    7265      465113 :     entryblock->b_visited = 1;
    7266      465113 :     entryblock->b_exceptstack = except_stack;
    7267      465113 :     basicblock **todo = &todo_stack[1];
    7268      465113 :     basicblock *handler = NULL;
    7269     2707030 :     while (todo > todo_stack) {
    7270     2241920 :         todo--;
    7271     2241920 :         basicblock *b = todo[0];
    7272     2241920 :         assert(b->b_visited == 1);
    7273     2241920 :         except_stack = b->b_exceptstack;
    7274     2241920 :         assert(except_stack != NULL);
    7275     2241920 :         b->b_exceptstack = NULL;
    7276     2241920 :         handler = except_stack_top(except_stack);
    7277    19854100 :         for (int i = 0; i < b->b_iused; i++) {
    7278    17612200 :             struct instr *instr = &b->b_instr[i];
    7279    17612200 :             if (is_block_push(instr)) {
    7280      131559 :                 if (!instr->i_target->b_visited) {
    7281      131559 :                     ExceptStack *copy = copy_except_stack(except_stack);
    7282      131559 :                     if (copy == NULL) {
    7283           0 :                         goto error;
    7284             :                     }
    7285      131559 :                     instr->i_target->b_exceptstack = copy;
    7286      131559 :                     todo[0] = instr->i_target;
    7287      131559 :                     instr->i_target->b_visited = 1;
    7288      131559 :                     todo++;
    7289             :                 }
    7290      131559 :                 handler = push_except_block(except_stack, instr);
    7291             :             }
    7292    17480600 :             else if (instr->i_opcode == POP_BLOCK) {
    7293      121220 :                 handler = pop_except_block(except_stack);
    7294             :             }
    7295    17359400 :             else if (is_jump(instr)) {
    7296      938095 :                 instr->i_except = handler;
    7297      938095 :                 assert(i == b->b_iused -1);
    7298      938095 :                 if (!instr->i_target->b_visited) {
    7299      713396 :                     if (BB_HAS_FALLTHROUGH(b)) {
    7300      636378 :                         ExceptStack *copy = copy_except_stack(except_stack);
    7301      636378 :                         if (copy == NULL) {
    7302           0 :                             goto error;
    7303             :                         }
    7304      636378 :                         instr->i_target->b_exceptstack = copy;
    7305             :                     }
    7306             :                     else {
    7307       77018 :                         instr->i_target->b_exceptstack = except_stack;
    7308       77018 :                         except_stack = NULL;
    7309             :                     }
    7310      713396 :                     todo[0] = instr->i_target;
    7311      713396 :                     instr->i_target->b_visited = 1;
    7312      713396 :                     todo++;
    7313             :                 }
    7314             :             }
    7315             :             else {
    7316    16421300 :                 instr->i_except = handler;
    7317             :             }
    7318             :         }
    7319     2241920 :         if (BB_HAS_FALLTHROUGH(b) && !b->b_next->b_visited) {
    7320      931853 :             assert(except_stack != NULL);
    7321      931853 :             b->b_next->b_exceptstack = except_stack;
    7322      931853 :             todo[0] = b->b_next;
    7323      931853 :             b->b_next->b_visited = 1;
    7324      931853 :             todo++;
    7325             :         }
    7326     1310070 :         else if (except_stack != NULL) {
    7327     1233050 :            PyMem_Free(except_stack);
    7328             :         }
    7329             :     }
    7330             : #ifdef Py_DEBUG
    7331     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7332     2241920 :         assert(b->b_exceptstack == NULL);
    7333             :     }
    7334             : #endif
    7335      465113 :     PyMem_Free(todo_stack);
    7336      465113 :     return 0;
    7337           0 : error:
    7338           0 :     PyMem_Free(todo_stack);
    7339           0 :     PyMem_Free(except_stack);
    7340           0 :     return -1;
    7341             : }
    7342             : 
    7343             : static int
    7344      148799 : mark_warm(basicblock *entryblock) {
    7345      148799 :     basicblock **stack = make_cfg_traversal_stack(entryblock);
    7346      148799 :     if (stack == NULL) {
    7347           0 :         return -1;
    7348             :     }
    7349      148799 :     basicblock **sp = stack;
    7350             : 
    7351      148799 :     *sp++ = entryblock;
    7352      148799 :     entryblock->b_visited = 1;
    7353     1764800 :     while (sp > stack) {
    7354     1616010 :         basicblock *b = *(--sp);
    7355     1616010 :         assert(!b->b_except_predecessors);
    7356     1616010 :         b->b_warm = 1;
    7357     1616010 :         basicblock *next = b->b_next;
    7358     1616010 :         if (next && BB_HAS_FALLTHROUGH(b) && !next->b_visited) {
    7359      858653 :             *sp++ = next;
    7360      858653 :             next->b_visited = 1;
    7361             :         }
    7362    11375400 :         for (int i=0; i < b->b_iused; i++) {
    7363     9759360 :             struct instr *instr = &b->b_instr[i];
    7364     9759360 :             if (is_jump(instr) && !instr->i_target->b_visited) {
    7365      608554 :                 *sp++ = instr->i_target;
    7366      608554 :                 instr->i_target->b_visited = 1;
    7367             :             }
    7368             :         }
    7369             :     }
    7370      148799 :     PyMem_Free(stack);
    7371      148799 :     return 0;
    7372             : }
    7373             : 
    7374             : static int
    7375      148799 : mark_cold(basicblock *entryblock) {
    7376     2074410 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7377     1925610 :         assert(!b->b_cold && !b->b_warm);
    7378             :     }
    7379      148799 :     if (mark_warm(entryblock) < 0) {
    7380           0 :         return -1;
    7381             :     }
    7382             : 
    7383      148799 :     basicblock **stack = make_cfg_traversal_stack(entryblock);
    7384      148799 :     if (stack == NULL) {
    7385           0 :         return -1;
    7386             :     }
    7387             : 
    7388      148799 :     basicblock **sp = stack;
    7389     2074410 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7390     1925610 :         if (b->b_except_predecessors) {
    7391      131559 :             assert(b->b_except_predecessors == b->b_predecessors);
    7392      131559 :             assert(!b->b_warm);
    7393      131559 :             *sp++ = b;
    7394      131559 :             b->b_visited = 1;
    7395             :         }
    7396             :     }
    7397             : 
    7398      458400 :     while (sp > stack) {
    7399      309601 :         basicblock *b = *(--sp);
    7400      309601 :         b->b_cold = 1;
    7401      309601 :         basicblock *next = b->b_next;
    7402      309601 :         if (next && BB_HAS_FALLTHROUGH(b)) {
    7403      111183 :             if (!next->b_warm && !next->b_visited) {
    7404      108632 :                 *sp++ = next;
    7405      108632 :                 next->b_visited = 1;
    7406             :             }
    7407             :         }
    7408     1478600 :         for (int i = 0; i < b->b_iused; i++) {
    7409     1169000 :             struct instr *instr = &b->b_instr[i];
    7410     1169000 :             if (is_jump(instr)) {
    7411      103397 :                 assert(i == b->b_iused-1);
    7412      103397 :                 basicblock *target = b->b_instr[i].i_target;
    7413      103397 :                 if (!target->b_warm && !target->b_visited) {
    7414       69410 :                     *sp++ = target;
    7415       69410 :                     target->b_visited = 1;
    7416             :                 }
    7417             :             }
    7418             :         }
    7419             :     }
    7420      148799 :     PyMem_Free(stack);
    7421      148799 :     return 0;
    7422             : }
    7423             : 
    7424             : static int
    7425      465113 : push_cold_blocks_to_end(basicblock *entryblock, int code_flags) {
    7426      465113 :     if (entryblock->b_next == NULL) {
    7427             :         /* single basicblock, no need to reorder */
    7428      316314 :         return 0;
    7429             :     }
    7430      148799 :     if (mark_cold(entryblock) < 0) {
    7431           0 :         return -1;
    7432             :     }
    7433             : 
    7434             :     /* If we have a cold block with fallthrough to a warm block, add */
    7435             :     /* an explicit jump instead of fallthrough */
    7436     2074510 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7437     1925710 :         if (b->b_cold && BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_next->b_warm) {
    7438         101 :             basicblock *explicit_jump = basicblock_new_b_list_successor(b);
    7439         101 :             if (explicit_jump == NULL) {
    7440           0 :                 return -1;
    7441             :             }
    7442         101 :             basicblock_addop(explicit_jump, JUMP, 0, b->b_next, &NO_LOCATION);
    7443             : 
    7444         101 :             explicit_jump->b_cold = 1;
    7445         101 :             explicit_jump->b_next = b->b_next;
    7446         101 :             b->b_next = explicit_jump;
    7447             :         }
    7448             :     }
    7449             : 
    7450      148799 :     assert(!entryblock->b_cold);  /* First block can't be cold */
    7451      148799 :     basicblock *cold_blocks = NULL;
    7452      148799 :     basicblock *cold_blocks_tail = NULL;
    7453             : 
    7454      148799 :     basicblock *b = entryblock;
    7455      208309 :     while(b->b_next) {
    7456      189184 :         assert(!b->b_cold);
    7457     1656390 :         while (b->b_next && !b->b_next->b_cold) {
    7458     1467210 :             b = b->b_next;
    7459             :         }
    7460      189184 :         if (b->b_next == NULL) {
    7461             :             /* no more cold blocks */
    7462      129674 :             break;
    7463             :         }
    7464             : 
    7465             :         /* b->b_next is the beginning of a cold streak */
    7466       59510 :         assert(!b->b_cold && b->b_next->b_cold);
    7467             : 
    7468       59510 :         basicblock *b_end = b->b_next;
    7469      309702 :         while (b_end->b_next && b_end->b_next->b_cold) {
    7470      250192 :             b_end = b_end->b_next;
    7471             :         }
    7472             : 
    7473             :         /* b_end is the end of the cold streak */
    7474       59510 :         assert(b_end && b_end->b_cold);
    7475       59510 :         assert(b_end->b_next == NULL || !b_end->b_next->b_cold);
    7476             : 
    7477       59510 :         if (cold_blocks == NULL) {
    7478       41106 :             cold_blocks = b->b_next;
    7479             :         }
    7480             :         else {
    7481       18404 :             cold_blocks_tail->b_next = b->b_next;
    7482             :         }
    7483       59510 :         cold_blocks_tail = b_end;
    7484       59510 :         b->b_next = b_end->b_next;
    7485       59510 :         b_end->b_next = NULL;
    7486             :     }
    7487      148799 :     assert(b != NULL && b->b_next == NULL);
    7488      148799 :     b->b_next = cold_blocks;
    7489      148799 :     return 0;
    7490             : }
    7491             : 
    7492             : static void
    7493      465113 : convert_exception_handlers_to_nops(basicblock *entryblock) {
    7494     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7495    19854100 :         for (int i = 0; i < b->b_iused; i++) {
    7496    17612200 :             struct instr *instr = &b->b_instr[i];
    7497    17612200 :             if (is_block_push(instr) || instr->i_opcode == POP_BLOCK) {
    7498      252779 :                 instr->i_opcode = NOP;
    7499             :             }
    7500             :         }
    7501             :     }
    7502      465113 : }
    7503             : 
    7504             : static inline void
    7505     1047840 : write_except_byte(struct assembler *a, int byte) {
    7506     1047840 :     unsigned char *p = (unsigned char *) PyBytes_AS_STRING(a->a_except_table);
    7507     1047840 :     p[a->a_except_table_off++] = byte;
    7508     1047840 : }
    7509             : 
    7510             : #define CONTINUATION_BIT 64
    7511             : 
    7512             : static void
    7513      733448 : assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
    7514             : {
    7515      733448 :     assert ((msb | 128) == 128);
    7516      733448 :     assert(value >= 0 && value < (1 << 30));
    7517      733448 :     if (value >= 1 << 24) {
    7518           0 :         write_except_byte(a, (value >> 24) | CONTINUATION_BIT | msb);
    7519           0 :         msb = 0;
    7520             :     }
    7521      733448 :     if (value >= 1 << 18) {
    7522           0 :         write_except_byte(a, ((value >> 18)&0x3f) | CONTINUATION_BIT | msb);
    7523           0 :         msb = 0;
    7524             :     }
    7525      733448 :     if (value >= 1 << 12) {
    7526         385 :         write_except_byte(a, ((value >> 12)&0x3f) | CONTINUATION_BIT | msb);
    7527         385 :         msb = 0;
    7528             :     }
    7529      733448 :     if (value >= 1 << 6) {
    7530      314007 :         write_except_byte(a, ((value >> 6)&0x3f) | CONTINUATION_BIT | msb);
    7531      314007 :         msb = 0;
    7532             :     }
    7533      733448 :     write_except_byte(a, (value&0x3f) | msb);
    7534      733448 : }
    7535             : 
    7536             : /* See Objects/exception_handling_notes.txt for details of layout */
    7537             : #define MAX_SIZE_OF_ENTRY 20
    7538             : 
    7539             : static int
    7540      183362 : assemble_emit_exception_table_entry(struct assembler *a, int start, int end, basicblock *handler)
    7541             : {
    7542      183362 :     Py_ssize_t len = PyBytes_GET_SIZE(a->a_except_table);
    7543      183362 :     if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) {
    7544       65134 :         if (_PyBytes_Resize(&a->a_except_table, len * 2) < 0)
    7545           0 :             return 0;
    7546             :     }
    7547      183362 :     int size = end-start;
    7548      183362 :     assert(end > start);
    7549      183362 :     int target = handler->b_offset;
    7550      183362 :     int depth = handler->b_startdepth - 1;
    7551      183362 :     if (handler->b_preserve_lasti) {
    7552      134256 :         depth -= 1;
    7553             :     }
    7554      183362 :     assert(depth >= 0);
    7555      183362 :     int depth_lasti = (depth<<1) | handler->b_preserve_lasti;
    7556      183362 :     assemble_emit_exception_table_item(a, start, (1<<7));
    7557      183362 :     assemble_emit_exception_table_item(a, size, 0);
    7558      183362 :     assemble_emit_exception_table_item(a, target, 0);
    7559      183362 :     assemble_emit_exception_table_item(a, depth_lasti, 0);
    7560      183362 :     return 1;
    7561             : }
    7562             : 
    7563             : static int
    7564      465113 : assemble_exception_table(struct assembler *a, basicblock *entryblock)
    7565             : {
    7566             :     basicblock *b;
    7567      465113 :     int ioffset = 0;
    7568      465113 :     basicblock *handler = NULL;
    7569      465113 :     int start = -1;
    7570     2707140 :     for (b = entryblock; b != NULL; b = b->b_next) {
    7571     2242020 :         ioffset = b->b_offset;
    7572    19610900 :         for (int i = 0; i < b->b_iused; i++) {
    7573    17368800 :             struct instr *instr = &b->b_instr[i];
    7574    17368800 :             if (instr->i_except != handler) {
    7575      321789 :                 if (handler != NULL) {
    7576      183362 :                     RETURN_IF_FALSE(assemble_emit_exception_table_entry(a, start, ioffset, handler));
    7577             :                 }
    7578      321789 :                 start = ioffset;
    7579      321789 :                 handler = instr->i_except;
    7580             :             }
    7581    17368800 :             ioffset += instr_size(instr);
    7582             :         }
    7583             :     }
    7584      465113 :     if (handler != NULL) {
    7585           0 :         RETURN_IF_FALSE(assemble_emit_exception_table_entry(a, start, ioffset, handler));
    7586             :     }
    7587      465113 :     return 1;
    7588             : }
    7589             : 
    7590             : /* Code location emitting code. See locations.md for a description of the format. */
    7591             : 
    7592             : #define MSB 0x80
    7593             : 
    7594             : static void
    7595    19095100 : write_location_byte(struct assembler* a, int val)
    7596             : {
    7597    19095100 :     PyBytes_AS_STRING(a->a_linetable)[a->a_location_off] = val&255;
    7598    19095100 :     a->a_location_off++;
    7599    19095100 : }
    7600             : 
    7601             : 
    7602             : static uint8_t *
    7603    38907700 : location_pointer(struct assembler* a)
    7604             : {
    7605    38907700 :     return (uint8_t *)PyBytes_AS_STRING(a->a_linetable) +
    7606    38907700 :         a->a_location_off;
    7607             : }
    7608             : 
    7609             : static void
    7610    18579100 : write_location_first_byte(struct assembler* a, int code, int length)
    7611             : {
    7612    18579100 :     a->a_location_off += write_location_entry_start(
    7613             :         location_pointer(a), code, length);
    7614    18579100 : }
    7615             : 
    7616             : static void
    7617    15221500 : write_location_varint(struct assembler* a, unsigned int val)
    7618             : {
    7619    15221500 :     uint8_t *ptr = location_pointer(a);
    7620    15221500 :     a->a_location_off += write_varint(ptr, val);
    7621    15221500 : }
    7622             : 
    7623             : 
    7624             : static void
    7625     5107120 : write_location_signed_varint(struct assembler* a, int val)
    7626             : {
    7627     5107120 :     uint8_t *ptr = location_pointer(a);
    7628     5107120 :     a->a_location_off += write_signed_varint(ptr, val);
    7629     5107120 : }
    7630             : 
    7631             : static void
    7632     7232700 : write_location_info_short_form(struct assembler* a, int length, int column, int end_column)
    7633             : {
    7634     7232700 :     assert(length > 0 &&  length <= 8);
    7635     7232700 :     int column_low_bits = column & 7;
    7636     7232700 :     int column_group = column >> 3;
    7637     7232700 :     assert(column < 80);
    7638     7232700 :     assert(end_column >= column);
    7639     7232700 :     assert(end_column - column < 16);
    7640     7232700 :     write_location_first_byte(a, PY_CODE_LOCATION_INFO_SHORT0 + column_group, length);
    7641     7232700 :     write_location_byte(a, (column_low_bits << 4) | (end_column - column));
    7642     7232700 : }
    7643             : 
    7644             : static void
    7645     5931190 : write_location_info_oneline_form(struct assembler* a, int length, int line_delta, int column, int end_column)
    7646             : {
    7647     5931190 :     assert(length > 0 &&  length <= 8);
    7648     5931190 :     assert(line_delta >= 0 && line_delta < 3);
    7649     5931190 :     assert(column < 128);
    7650     5931190 :     assert(end_column < 128);
    7651     5931190 :     write_location_first_byte(a, PY_CODE_LOCATION_INFO_ONE_LINE0 + line_delta, length);
    7652     5931190 :     write_location_byte(a, column);
    7653     5931190 :     write_location_byte(a, end_column);
    7654     5931190 : }
    7655             : 
    7656             : static void
    7657     5073840 : write_location_info_long_form(struct assembler* a, struct instr* i, int length)
    7658             : {
    7659     5073840 :     assert(length > 0 &&  length <= 8);
    7660     5073840 :     write_location_first_byte(a, PY_CODE_LOCATION_INFO_LONG, length);
    7661     5073840 :     write_location_signed_varint(a, i->i_loc.lineno - a->a_lineno);
    7662     5073840 :     assert(i->i_loc.end_lineno >= i->i_loc.lineno);
    7663     5073840 :     write_location_varint(a, i->i_loc.end_lineno - i->i_loc.lineno);
    7664     5073840 :     write_location_varint(a, i->i_loc.col_offset + 1);
    7665     5073840 :     write_location_varint(a, i->i_loc.end_col_offset + 1);
    7666     5073840 : }
    7667             : 
    7668             : static void
    7669      308073 : write_location_info_none(struct assembler* a, int length)
    7670             : {
    7671      308073 :     write_location_first_byte(a, PY_CODE_LOCATION_INFO_NONE, length);
    7672      308073 : }
    7673             : 
    7674             : static void
    7675       33286 : write_location_info_no_column(struct assembler* a, int length, int line_delta)
    7676             : {
    7677       33286 :     write_location_first_byte(a, PY_CODE_LOCATION_INFO_NO_COLUMNS, length);
    7678       33286 :     write_location_signed_varint(a, line_delta);
    7679       33286 : }
    7680             : 
    7681             : #define THEORETICAL_MAX_ENTRY_SIZE 25 /* 1 + 6 + 6 + 6 + 6 */
    7682             : 
    7683             : static int
    7684    18579100 : write_location_info_entry(struct assembler* a, struct instr* i, int isize)
    7685             : {
    7686    18579100 :     Py_ssize_t len = PyBytes_GET_SIZE(a->a_linetable);
    7687    18579100 :     if (a->a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len) {
    7688      844851 :         assert(len > THEORETICAL_MAX_ENTRY_SIZE);
    7689      844851 :         if (_PyBytes_Resize(&a->a_linetable, len*2) < 0) {
    7690           0 :             return 0;
    7691             :         }
    7692             :     }
    7693    18579100 :     if (i->i_loc.lineno < 0) {
    7694      308073 :         write_location_info_none(a, isize);
    7695      308073 :         return 1;
    7696             :     }
    7697    18271000 :     int line_delta = i->i_loc.lineno - a->a_lineno;
    7698    18271000 :     int column = i->i_loc.col_offset;
    7699    18271000 :     int end_column = i->i_loc.end_col_offset;
    7700    18271000 :     assert(column >= -1);
    7701    18271000 :     assert(end_column >= -1);
    7702    18271000 :     if (column < 0 || end_column < 0) {
    7703       33286 :         if (i->i_loc.end_lineno == i->i_loc.lineno || i->i_loc.end_lineno == -1) {
    7704       33286 :             write_location_info_no_column(a, isize, line_delta);
    7705       33286 :             a->a_lineno = i->i_loc.lineno;
    7706       33286 :             return 1;
    7707             :         }
    7708             :     }
    7709    18237700 :     else if (i->i_loc.end_lineno == i->i_loc.lineno) {
    7710    14800800 :         if (line_delta == 0 && column < 80 && end_column - column < 16 && end_column >= column) {
    7711     7232700 :             write_location_info_short_form(a, isize, column, end_column);
    7712     7232700 :             return 1;
    7713             :         }
    7714     7568150 :         if (line_delta >= 0 && line_delta < 3 && column < 128 && end_column < 128) {
    7715     5931190 :             write_location_info_oneline_form(a, isize, line_delta, column, end_column);
    7716     5931190 :             a->a_lineno = i->i_loc.lineno;
    7717     5931190 :             return 1;
    7718             :         }
    7719             :     }
    7720     5073840 :     write_location_info_long_form(a, i, isize);
    7721     5073840 :     a->a_lineno = i->i_loc.lineno;
    7722     5073840 :     return 1;
    7723             : }
    7724             : 
    7725             : static int
    7726    17368800 : assemble_emit_location(struct assembler* a, struct instr* i)
    7727             : {
    7728    17368800 :     int isize = instr_size(i);
    7729    18579100 :     while (isize > 8) {
    7730     1210250 :         if (!write_location_info_entry(a, i, 8)) {
    7731           0 :             return 0;
    7732             :         }
    7733     1210250 :         isize -= 8;
    7734             :     }
    7735    17368800 :     return write_location_info_entry(a, i, isize);
    7736             : }
    7737             : 
    7738             : /* assemble_emit()
    7739             :    Extend the bytecode with a new instruction.
    7740             :    Update lnotab if necessary.
    7741             : */
    7742             : 
    7743             : static int
    7744    17368800 : assemble_emit(struct assembler *a, struct instr *i)
    7745             : {
    7746    17368800 :     Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
    7747             :     _Py_CODEUNIT *code;
    7748             : 
    7749    17368800 :     int size = instr_size(i);
    7750    17368800 :     if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
    7751      254794 :         if (len > PY_SSIZE_T_MAX / 2)
    7752           0 :             return 0;
    7753      254794 :         if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
    7754           0 :             return 0;
    7755             :     }
    7756    17368800 :     code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
    7757    17368800 :     a->a_offset += size;
    7758    17368800 :     write_instr(code, i, size);
    7759    17368800 :     return 1;
    7760             : }
    7761             : 
    7762             : static void
    7763      465113 : normalize_jumps(basicblock *entryblock)
    7764             : {
    7765     2707140 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7766     2242020 :         b->b_visited = 0;
    7767             :     }
    7768     2707140 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7769     2242020 :         b->b_visited = 1;
    7770     2242020 :         if (b->b_iused == 0) {
    7771        1882 :             continue;
    7772             :         }
    7773     2240140 :         struct instr *last = &b->b_instr[b->b_iused-1];
    7774     2240140 :         assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
    7775     2240140 :         if (is_jump(last)) {
    7776      903310 :             bool is_forward = last->i_target->b_visited == 0;
    7777      903310 :             switch(last->i_opcode) {
    7778      180260 :                 case JUMP:
    7779      180260 :                     last->i_opcode = is_forward ? JUMP_FORWARD : JUMP_BACKWARD;
    7780      180260 :                     break;
    7781        4531 :                 case JUMP_NO_INTERRUPT:
    7782        4531 :                     last->i_opcode = is_forward ?
    7783        4531 :                         JUMP_FORWARD : JUMP_BACKWARD_NO_INTERRUPT;
    7784        4531 :                     break;
    7785       24651 :                 case POP_JUMP_IF_NOT_NONE:
    7786       24651 :                     last->i_opcode = is_forward ?
    7787       24651 :                         POP_JUMP_FORWARD_IF_NOT_NONE : POP_JUMP_BACKWARD_IF_NOT_NONE;
    7788       24651 :                     break;
    7789       22204 :                 case POP_JUMP_IF_NONE:
    7790       22204 :                     last->i_opcode = is_forward ?
    7791       22204 :                         POP_JUMP_FORWARD_IF_NONE : POP_JUMP_BACKWARD_IF_NONE;
    7792       22204 :                     break;
    7793      497509 :                 case POP_JUMP_IF_FALSE:
    7794      497509 :                     last->i_opcode = is_forward ?
    7795      497509 :                         POP_JUMP_FORWARD_IF_FALSE : POP_JUMP_BACKWARD_IF_FALSE;
    7796      497509 :                     break;
    7797       85743 :                 case POP_JUMP_IF_TRUE:
    7798       85743 :                     last->i_opcode = is_forward ?
    7799       85743 :                         POP_JUMP_FORWARD_IF_TRUE : POP_JUMP_BACKWARD_IF_TRUE;
    7800       85743 :                     break;
    7801       19159 :                 case JUMP_IF_TRUE_OR_POP:
    7802             :                 case JUMP_IF_FALSE_OR_POP:
    7803       19159 :                     if (!is_forward) {
    7804             :                         /* As far as we can tell, the compiler never emits
    7805             :                          * these jumps with a backwards target. If/when this
    7806             :                          * exception is raised, we have found a use case for
    7807             :                          * a backwards version of this jump (or to replace
    7808             :                          * it with the sequence (COPY 1, POP_JUMP_IF_T/F, POP)
    7809             :                          */
    7810           0 :                         PyErr_Format(PyExc_SystemError,
    7811             :                             "unexpected %s jumping backwards",
    7812           0 :                             last->i_opcode == JUMP_IF_TRUE_OR_POP ?
    7813             :                                 "JUMP_IF_TRUE_OR_POP" : "JUMP_IF_FALSE_OR_POP");
    7814             :                     }
    7815       19159 :                     break;
    7816             :             }
    7817     2242020 :         }
    7818             :     }
    7819      465113 : }
    7820             : 
    7821             : static void
    7822      473351 : assemble_jump_offsets(basicblock *entryblock)
    7823             : {
    7824             :     int bsize, totsize, extended_arg_recompile;
    7825             : 
    7826             :     /* Compute the size of each block and fixup jump args.
    7827             :        Replace block pointer with position in bytecode. */
    7828             :     do {
    7829      473351 :         totsize = 0;
    7830     3017460 :         for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7831     2544110 :             bsize = blocksize(b);
    7832     2544110 :             b->b_offset = totsize;
    7833     2544110 :             totsize += bsize;
    7834             :         }
    7835      473351 :         extended_arg_recompile = 0;
    7836     3017460 :         for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7837     2544110 :             bsize = b->b_offset;
    7838    22289300 :             for (int i = 0; i < b->b_iused; i++) {
    7839    19745200 :                 struct instr *instr = &b->b_instr[i];
    7840    19745200 :                 int isize = instr_size(instr);
    7841             :                 /* Relative jumps are computed relative to
    7842             :                    the instruction pointer after fetching
    7843             :                    the jump instruction.
    7844             :                 */
    7845    19745200 :                 bsize += isize;
    7846    19745200 :                 if (is_jump(instr)) {
    7847     1069810 :                     instr->i_oparg = instr->i_target->b_offset;
    7848     1069810 :                     if (is_relative_jump(instr)) {
    7849     1069810 :                         if (instr->i_oparg < bsize) {
    7850      174139 :                             assert(IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
    7851      174139 :                             instr->i_oparg = bsize - instr->i_oparg;
    7852             :                         }
    7853             :                         else {
    7854      895675 :                             assert(!IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
    7855      895675 :                             instr->i_oparg -= bsize;
    7856             :                         }
    7857             :                     }
    7858             :                     else {
    7859           0 :                         assert(!IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
    7860             :                     }
    7861     1069810 :                     if (instr_size(instr) != isize) {
    7862       24834 :                         extended_arg_recompile = 1;
    7863             :                     }
    7864             :                 }
    7865             :             }
    7866             :         }
    7867             : 
    7868             :     /* XXX: This is an awful hack that could hurt performance, but
    7869             :         on the bright side it should work until we come up
    7870             :         with a better solution.
    7871             : 
    7872             :         The issue is that in the first loop blocksize() is called
    7873             :         which calls instr_size() which requires i_oparg be set
    7874             :         appropriately. There is a bootstrap problem because
    7875             :         i_oparg is calculated in the second loop above.
    7876             : 
    7877             :         So we loop until we stop seeing new EXTENDED_ARGs.
    7878             :         The only EXTENDED_ARGs that could be popping up are
    7879             :         ones in jump instructions.  So this should converge
    7880             :         fairly quickly.
    7881             :     */
    7882      473351 :     } while (extended_arg_recompile);
    7883      465113 : }
    7884             : 
    7885             : 
    7886             : // Ensure each basicblock is only put onto the stack once.
    7887             : #define MAYBE_PUSH(B) do {                          \
    7888             :         if ((B)->b_visited == 0) {                  \
    7889             :             *(*stack_top)++ = (B);                  \
    7890             :             (B)->b_visited = 1;                     \
    7891             :         }                                           \
    7892             :     } while (0)
    7893             : 
    7894             : static void
    7895    15150100 : scan_block_for_local(int target, basicblock *b, bool unsafe_to_start,
    7896             :                      basicblock ***stack_top)
    7897             : {
    7898    15150100 :     bool unsafe = unsafe_to_start;
    7899   172659000 :     for (int i = 0; i < b->b_iused; i++) {
    7900   157509000 :         struct instr *instr = &b->b_instr[i];
    7901   157509000 :         assert(instr->i_opcode != EXTENDED_ARG);
    7902   157509000 :         assert(instr->i_opcode != EXTENDED_ARG_QUICK);
    7903   157509000 :         assert(instr->i_opcode != LOAD_FAST__LOAD_FAST);
    7904   157509000 :         assert(instr->i_opcode != STORE_FAST__LOAD_FAST);
    7905   157509000 :         assert(instr->i_opcode != LOAD_CONST__LOAD_FAST);
    7906   157509000 :         assert(instr->i_opcode != STORE_FAST__STORE_FAST);
    7907   157509000 :         assert(instr->i_opcode != LOAD_FAST__LOAD_CONST);
    7908   157509000 :         if (unsafe && instr->i_except != NULL) {
    7909     3774640 :             MAYBE_PUSH(instr->i_except);
    7910             :         }
    7911   157509000 :         if (instr->i_oparg != target) {
    7912   147110000 :             continue;
    7913             :         }
    7914    10399600 :         switch (instr->i_opcode) {
    7915           1 :             case LOAD_FAST_CHECK:
    7916             :                 // if this doesn't raise, then var is defined
    7917           1 :                 unsafe = false;
    7918           1 :                 break;
    7919     2711520 :             case LOAD_FAST:
    7920     2711520 :                 if (unsafe) {
    7921        7488 :                     instr->i_opcode = LOAD_FAST_CHECK;
    7922             :                 }
    7923     2711520 :                 unsafe = false;
    7924     2711520 :                 break;
    7925     1018040 :             case STORE_FAST:
    7926     1018040 :                 unsafe = false;
    7927     1018040 :                 break;
    7928       13222 :             case DELETE_FAST:
    7929       13222 :                 unsafe = true;
    7930       13222 :                 break;
    7931             :         }
    7932   157509000 :     }
    7933    15150100 :     if (unsafe) {
    7934             :         // unsafe at end of this block,
    7935             :         // so unsafe at start of next blocks
    7936     3548310 :         if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
    7937     2336490 :             MAYBE_PUSH(b->b_next);
    7938             :         }
    7939     3548310 :         if (b->b_iused > 0) {
    7940     3543200 :             struct instr *last = &b->b_instr[b->b_iused-1];
    7941     3543200 :             if (is_jump(last)) {
    7942     1839280 :                 assert(last->i_target != NULL);
    7943     1839280 :                 MAYBE_PUSH(last->i_target);
    7944             :             }
    7945             :         }
    7946             :     }
    7947    15150100 : }
    7948             : #undef MAYBE_PUSH
    7949             : 
    7950             : static int
    7951      465113 : add_checks_for_loads_of_unknown_variables(basicblock *entryblock,
    7952             :                                           struct compiler *c)
    7953             : {
    7954      465113 :     basicblock **stack = make_cfg_traversal_stack(entryblock);
    7955      465113 :     if (stack == NULL) {
    7956           0 :         return -1;
    7957             :     }
    7958      465113 :     Py_ssize_t nparams = PyList_GET_SIZE(c->u->u_ste->ste_varnames);
    7959      465113 :     int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
    7960     1551910 :     for (int target = 0; target < nlocals; target++) {
    7961    12260100 :         for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7962    11173300 :             b->b_visited = 0;
    7963             :         }
    7964     1086800 :         basicblock **stack_top = stack;
    7965             : 
    7966             :         // First pass: find the relevant DFS starting points:
    7967             :         // the places where "being uninitialized" originates,
    7968             :         // which are the entry block and any DELETE_FAST statements.
    7969     1086800 :         if (target >= nparams) {
    7970             :             // only non-parameter locals start out uninitialized.
    7971      386657 :             *(stack_top++) = entryblock;
    7972      386657 :             entryblock->b_visited = 1;
    7973             :         }
    7974    12260100 :         for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    7975    11173300 :             scan_block_for_local(target, b, false, &stack_top);
    7976             :         }
    7977             : 
    7978             :         // Second pass: Depth-first search to propagate uncertainty
    7979     5063610 :         while (stack_top > stack) {
    7980     3976810 :             basicblock *b = *--stack_top;
    7981     3976810 :             scan_block_for_local(target, b, true, &stack_top);
    7982             :         }
    7983             :     }
    7984      465113 :     PyMem_Free(stack);
    7985      465113 :     return 0;
    7986             : }
    7987             : 
    7988             : static PyObject *
    7989      465113 : dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
    7990             : {
    7991             :     PyObject *tuple, *k, *v;
    7992      465113 :     Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
    7993             : 
    7994      465113 :     tuple = PyTuple_New(size);
    7995      465113 :     if (tuple == NULL)
    7996           0 :         return NULL;
    7997     2634660 :     while (PyDict_Next(dict, &pos, &k, &v)) {
    7998     2169550 :         i = PyLong_AS_LONG(v);
    7999     2169550 :         Py_INCREF(k);
    8000     2169550 :         assert((i - offset) < size);
    8001     2169550 :         assert((i - offset) >= 0);
    8002     2169550 :         PyTuple_SET_ITEM(tuple, i - offset, k);
    8003             :     }
    8004      465113 :     return tuple;
    8005             : }
    8006             : 
    8007             : static PyObject *
    8008      465113 : consts_dict_keys_inorder(PyObject *dict)
    8009             : {
    8010             :     PyObject *consts, *k, *v;
    8011      465113 :     Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
    8012             : 
    8013      465113 :     consts = PyList_New(size);   /* PyCode_Optimize() requires a list */
    8014      465113 :     if (consts == NULL)
    8015           0 :         return NULL;
    8016     2625770 :     while (PyDict_Next(dict, &pos, &k, &v)) {
    8017     2160660 :         i = PyLong_AS_LONG(v);
    8018             :         /* The keys of the dictionary can be tuples wrapping a constant.
    8019             :          * (see compiler_add_o and _PyCode_ConstantKey). In that case
    8020             :          * the object we want is always second. */
    8021     2160660 :         if (PyTuple_CheckExact(k)) {
    8022      236036 :             k = PyTuple_GET_ITEM(k, 1);
    8023             :         }
    8024     2160660 :         Py_INCREF(k);
    8025     2160660 :         assert(i < size);
    8026     2160660 :         assert(i >= 0);
    8027     2160660 :         PyList_SET_ITEM(consts, i, k);
    8028             :     }
    8029      465113 :     return consts;
    8030             : }
    8031             : 
    8032             : static int
    8033      465113 : compute_code_flags(struct compiler *c)
    8034             : {
    8035      465113 :     PySTEntryObject *ste = c->u->u_ste;
    8036      465113 :     int flags = 0;
    8037      465113 :     if (ste->ste_type == FunctionBlock) {
    8038      313153 :         flags |= CO_NEWLOCALS | CO_OPTIMIZED;
    8039      313153 :         if (ste->ste_nested)
    8040       41986 :             flags |= CO_NESTED;
    8041      313153 :         if (ste->ste_generator && !ste->ste_coroutine)
    8042       14195 :             flags |= CO_GENERATOR;
    8043      313153 :         if (!ste->ste_generator && ste->ste_coroutine)
    8044        2235 :             flags |= CO_COROUTINE;
    8045      313153 :         if (ste->ste_generator && ste->ste_coroutine)
    8046         158 :             flags |= CO_ASYNC_GENERATOR;
    8047      313153 :         if (ste->ste_varargs)
    8048       24272 :             flags |= CO_VARARGS;
    8049      313153 :         if (ste->ste_varkeywords)
    8050       23166 :             flags |= CO_VARKEYWORDS;
    8051             :     }
    8052             : 
    8053             :     /* (Only) inherit compilerflags in PyCF_MASK */
    8054      465113 :     flags |= (c->c_flags->cf_flags & PyCF_MASK);
    8055             : 
    8056      465113 :     if ((IS_TOP_LEVEL_AWAIT(c)) &&
    8057          20 :          ste->ste_coroutine &&
    8058          20 :          !ste->ste_generator) {
    8059          20 :         flags |= CO_COROUTINE;
    8060             :     }
    8061             : 
    8062      465113 :     return flags;
    8063             : }
    8064             : 
    8065             : // Merge *obj* with constant cache.
    8066             : // Unlike merge_consts_recursive(), this function doesn't work recursively.
    8067             : static int
    8068     2827030 : merge_const_one(PyObject *const_cache, PyObject **obj)
    8069             : {
    8070     2827030 :     PyDict_CheckExact(const_cache);
    8071     2827030 :     PyObject *key = _PyCode_ConstantKey(*obj);
    8072     2827030 :     if (key == NULL) {
    8073           0 :         return 0;
    8074             :     }
    8075             : 
    8076             :     // t is borrowed reference
    8077     2827030 :     PyObject *t = PyDict_SetDefault(const_cache, key, key);
    8078     2827030 :     Py_DECREF(key);
    8079     2827030 :     if (t == NULL) {
    8080           0 :         return 0;
    8081             :     }
    8082     2827030 :     if (t == key) {  // obj is new constant.
    8083     2079260 :         return 1;
    8084             :     }
    8085             : 
    8086      747765 :     if (PyTuple_CheckExact(t)) {
    8087             :         // t is still borrowed reference
    8088      747765 :         t = PyTuple_GET_ITEM(t, 1);
    8089             :     }
    8090             : 
    8091      747765 :     Py_INCREF(t);
    8092      747765 :     Py_DECREF(*obj);
    8093      747765 :     *obj = t;
    8094      747765 :     return 1;
    8095             : }
    8096             : 
    8097             : // This is in codeobject.c.
    8098             : extern void _Py_set_localsplus_info(int, PyObject *, unsigned char,
    8099             :                                    PyObject *, PyObject *);
    8100             : 
    8101             : static void
    8102      465113 : compute_localsplus_info(struct compiler *c, int nlocalsplus,
    8103             :                         PyObject *names, PyObject *kinds)
    8104             : {
    8105             :     PyObject *k, *v;
    8106      465113 :     Py_ssize_t pos = 0;
    8107     1551910 :     while (PyDict_Next(c->u->u_varnames, &pos, &k, &v)) {
    8108     1086800 :         int offset = (int)PyLong_AS_LONG(v);
    8109     1086800 :         assert(offset >= 0);
    8110     1086800 :         assert(offset < nlocalsplus);
    8111             :         // For now we do not distinguish arg kinds.
    8112     1086800 :         _PyLocals_Kind kind = CO_FAST_LOCAL;
    8113     1086800 :         if (PyDict_GetItem(c->u->u_cellvars, k) != NULL) {
    8114        9952 :             kind |= CO_FAST_CELL;
    8115             :         }
    8116     1086800 :         _Py_set_localsplus_info(offset, k, kind, names, kinds);
    8117             :     }
    8118      465113 :     int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
    8119             : 
    8120             :     // This counter mirrors the fix done in fix_cell_offsets().
    8121      465113 :     int numdropped = 0;
    8122      465113 :     pos = 0;
    8123      491509 :     while (PyDict_Next(c->u->u_cellvars, &pos, &k, &v)) {
    8124       26396 :         if (PyDict_GetItem(c->u->u_varnames, k) != NULL) {
    8125             :             // Skip cells that are already covered by locals.
    8126        9952 :             numdropped += 1;
    8127        9952 :             continue;
    8128             :         }
    8129       16444 :         int offset = (int)PyLong_AS_LONG(v);
    8130       16444 :         assert(offset >= 0);
    8131       16444 :         offset += nlocals - numdropped;
    8132       16444 :         assert(offset < nlocalsplus);
    8133       16444 :         _Py_set_localsplus_info(offset, k, CO_FAST_CELL, names, kinds);
    8134             :     }
    8135             : 
    8136      465113 :     pos = 0;
    8137      500149 :     while (PyDict_Next(c->u->u_freevars, &pos, &k, &v)) {
    8138       35036 :         int offset = (int)PyLong_AS_LONG(v);
    8139       35036 :         assert(offset >= 0);
    8140       35036 :         offset += nlocals - numdropped;
    8141       35036 :         assert(offset < nlocalsplus);
    8142       35036 :         _Py_set_localsplus_info(offset, k, CO_FAST_FREE, names, kinds);
    8143             :     }
    8144      465113 : }
    8145             : 
    8146             : static PyCodeObject *
    8147      465113 : makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
    8148             :          int maxdepth, int nlocalsplus, int code_flags)
    8149             : {
    8150      465113 :     PyCodeObject *co = NULL;
    8151      465113 :     PyObject *names = NULL;
    8152      465113 :     PyObject *consts = NULL;
    8153      465113 :     PyObject *localsplusnames = NULL;
    8154      465113 :     PyObject *localspluskinds = NULL;
    8155             : 
    8156      465113 :     names = dict_keys_inorder(c->u->u_names, 0);
    8157      465113 :     if (!names) {
    8158           0 :         goto error;
    8159             :     }
    8160      465113 :     if (!merge_const_one(c->c_const_cache, &names)) {
    8161           0 :         goto error;
    8162             :     }
    8163             : 
    8164      465113 :     consts = PyList_AsTuple(constslist); /* PyCode_New requires a tuple */
    8165      465113 :     if (consts == NULL) {
    8166           0 :         goto error;
    8167             :     }
    8168      465113 :     if (!merge_const_one(c->c_const_cache, &consts)) {
    8169           0 :         goto error;
    8170             :     }
    8171             : 
    8172      465113 :     assert(c->u->u_posonlyargcount < INT_MAX);
    8173      465113 :     assert(c->u->u_argcount < INT_MAX);
    8174      465113 :     assert(c->u->u_kwonlyargcount < INT_MAX);
    8175      465113 :     int posonlyargcount = (int)c->u->u_posonlyargcount;
    8176      465113 :     int posorkwargcount = (int)c->u->u_argcount;
    8177      465113 :     assert(INT_MAX - posonlyargcount - posorkwargcount > 0);
    8178      465113 :     int kwonlyargcount = (int)c->u->u_kwonlyargcount;
    8179             : 
    8180      465113 :     localsplusnames = PyTuple_New(nlocalsplus);
    8181      465113 :     if (localsplusnames == NULL) {
    8182           0 :         goto error;
    8183             :     }
    8184      465113 :     localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
    8185      465113 :     if (localspluskinds == NULL) {
    8186           0 :         goto error;
    8187             :     }
    8188      465113 :     compute_localsplus_info(c, nlocalsplus, localsplusnames, localspluskinds);
    8189             : 
    8190      930226 :     struct _PyCodeConstructor con = {
    8191      465113 :         .filename = c->c_filename,
    8192      465113 :         .name = c->u->u_name,
    8193      465113 :         .qualname = c->u->u_qualname ? c->u->u_qualname : c->u->u_name,
    8194             :         .flags = code_flags,
    8195             : 
    8196      465113 :         .code = a->a_bytecode,
    8197      465113 :         .firstlineno = c->u->u_firstlineno,
    8198      465113 :         .linetable = a->a_linetable,
    8199             : 
    8200             :         .consts = consts,
    8201             :         .names = names,
    8202             : 
    8203             :         .localsplusnames = localsplusnames,
    8204             :         .localspluskinds = localspluskinds,
    8205             : 
    8206      465113 :         .argcount = posonlyargcount + posorkwargcount,
    8207             :         .posonlyargcount = posonlyargcount,
    8208             :         .kwonlyargcount = kwonlyargcount,
    8209             : 
    8210             :         .stacksize = maxdepth,
    8211             : 
    8212      465113 :         .exceptiontable = a->a_except_table,
    8213             :     };
    8214             : 
    8215      465113 :     if (_PyCode_Validate(&con) < 0) {
    8216           0 :         goto error;
    8217             :     }
    8218             : 
    8219      465113 :     if (!merge_const_one(c->c_const_cache, &localsplusnames)) {
    8220           0 :         goto error;
    8221             :     }
    8222      465113 :     con.localsplusnames = localsplusnames;
    8223             : 
    8224      465113 :     co = _PyCode_New(&con);
    8225      465113 :     if (co == NULL) {
    8226           0 :         goto error;
    8227             :     }
    8228             : 
    8229      465113 :  error:
    8230      465113 :     Py_XDECREF(names);
    8231      465113 :     Py_XDECREF(consts);
    8232      465113 :     Py_XDECREF(localsplusnames);
    8233      465113 :     Py_XDECREF(localspluskinds);
    8234      465113 :     return co;
    8235             : }
    8236             : 
    8237             : 
    8238             : /* For debugging purposes only */
    8239             : #if 0
    8240             : static void
    8241             : dump_instr(struct instr *i)
    8242             : {
    8243             :     const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
    8244             :     const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
    8245             : 
    8246             :     char arg[128];
    8247             : 
    8248             :     *arg = '\0';
    8249             :     if (HAS_ARG(i->i_opcode)) {
    8250             :         sprintf(arg, "arg: %d ", i->i_oparg);
    8251             :     }
    8252             :     if (is_jump(i)) {
    8253             :         sprintf(arg, "target: %p ", i->i_target);
    8254             :     }
    8255             :     if (is_block_push(i)) {
    8256             :         sprintf(arg, "except_target: %p ", i->i_target);
    8257             :     }
    8258             :     fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
    8259             :                     i->i_loc.lineno, i->i_opcode, arg, jabs, jrel);
    8260             : }
    8261             : 
    8262             : static void
    8263             : dump_basicblock(const basicblock *b)
    8264             : {
    8265             :     const char *b_return = basicblock_returns(b) ? "return " : "";
    8266             :     fprintf(stderr, "[%d %d %d %p] used: %d, depth: %d, offset: %d %s\n",
    8267             :         b->b_cold, b->b_warm, BB_NO_FALLTHROUGH(b), b, b->b_iused,
    8268             :         b->b_startdepth, b->b_offset, b_return);
    8269             :     if (b->b_instr) {
    8270             :         int i;
    8271             :         for (i = 0; i < b->b_iused; i++) {
    8272             :             fprintf(stderr, "  [%02d] ", i);
    8273             :             dump_instr(b->b_instr + i);
    8274             :         }
    8275             :     }
    8276             : }
    8277             : #endif
    8278             : 
    8279             : 
    8280             : static int
    8281             : normalize_basic_block(basicblock *bb);
    8282             : 
    8283             : static int
    8284             : optimize_cfg(basicblock *entryblock, PyObject *consts, PyObject *const_cache);
    8285             : 
    8286             : static int
    8287             : trim_unused_consts(basicblock *entryblock, PyObject *consts);
    8288             : 
    8289             : /* Duplicates exit BBs, so that line numbers can be propagated to them */
    8290             : static int
    8291             : duplicate_exits_without_lineno(basicblock *entryblock);
    8292             : 
    8293             : static int
    8294             : extend_block(basicblock *bb);
    8295             : 
    8296             : static int *
    8297      465113 : build_cellfixedoffsets(struct compiler *c)
    8298             : {
    8299      465113 :     int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
    8300      465113 :     int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
    8301      465113 :     int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
    8302             : 
    8303      465113 :     int noffsets = ncellvars + nfreevars;
    8304      465113 :     int *fixed = PyMem_New(int, noffsets);
    8305      465113 :     if (fixed == NULL) {
    8306           0 :         PyErr_NoMemory();
    8307           0 :         return NULL;
    8308             :     }
    8309      526545 :     for (int i = 0; i < noffsets; i++) {
    8310       61432 :         fixed[i] = nlocals + i;
    8311             :     }
    8312             : 
    8313             :     PyObject *varname, *cellindex;
    8314      465113 :     Py_ssize_t pos = 0;
    8315      491509 :     while (PyDict_Next(c->u->u_cellvars, &pos, &varname, &cellindex)) {
    8316       26396 :         PyObject *varindex = PyDict_GetItem(c->u->u_varnames, varname);
    8317       26396 :         if (varindex != NULL) {
    8318        9952 :             assert(PyLong_AS_LONG(cellindex) < INT_MAX);
    8319        9952 :             assert(PyLong_AS_LONG(varindex) < INT_MAX);
    8320        9952 :             int oldindex = (int)PyLong_AS_LONG(cellindex);
    8321        9952 :             int argoffset = (int)PyLong_AS_LONG(varindex);
    8322        9952 :             fixed[oldindex] = argoffset;
    8323             :         }
    8324             :     }
    8325             : 
    8326      465113 :     return fixed;
    8327             : }
    8328             : 
    8329             : static inline int
    8330       84073 : insert_instruction(basicblock *block, int pos, struct instr *instr) {
    8331       84073 :     if (basicblock_next_instr(block) < 0) {
    8332           0 :         return -1;
    8333             :     }
    8334     1371470 :     for (int i = block->b_iused-1; i > pos; i--) {
    8335     1287400 :         block->b_instr[i] = block->b_instr[i-1];
    8336             :     }
    8337       84073 :     block->b_instr[pos] = *instr;
    8338       84073 :     return 0;
    8339             : }
    8340             : 
    8341             : static int
    8342      465113 : insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
    8343             :                            int *fixed, int nfreevars, int code_flags)
    8344             : {
    8345      465113 :     assert(c->u->u_firstlineno > 0);
    8346             : 
    8347             :     /* Add the generator prefix instructions. */
    8348      465113 :     if (code_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
    8349       16608 :         struct instr make_gen = {
    8350             :             .i_opcode = RETURN_GENERATOR,
    8351             :             .i_oparg = 0,
    8352       16608 :             .i_loc = LOCATION(c->u->u_firstlineno, c->u->u_firstlineno, -1, -1),
    8353             :             .i_target = NULL,
    8354             :         };
    8355       16608 :         if (insert_instruction(entryblock, 0, &make_gen) < 0) {
    8356           0 :             return -1;
    8357             :         }
    8358       16608 :         struct instr pop_top = {
    8359             :             .i_opcode = POP_TOP,
    8360             :             .i_oparg = 0,
    8361             :             .i_loc = NO_LOCATION,
    8362             :             .i_target = NULL,
    8363             :         };
    8364       16608 :         if (insert_instruction(entryblock, 1, &pop_top) < 0) {
    8365           0 :             return -1;
    8366             :         }
    8367             :     }
    8368             : 
    8369             :     /* Set up cells for any variable that escapes, to be put in a closure. */
    8370      465113 :     const int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
    8371      465113 :     if (ncellvars) {
    8372             :         // c->u->u_cellvars has the cells out of order so we sort them
    8373             :         // before adding the MAKE_CELL instructions.  Note that we
    8374             :         // adjust for arg cells, which come first.
    8375       16474 :         const int nvars = ncellvars + (int)PyDict_GET_SIZE(c->u->u_varnames);
    8376       16474 :         int *sorted = PyMem_RawCalloc(nvars, sizeof(int));
    8377       16474 :         if (sorted == NULL) {
    8378           0 :             PyErr_NoMemory();
    8379           0 :             return -1;
    8380             :         }
    8381       42870 :         for (int i = 0; i < ncellvars; i++) {
    8382       26396 :             sorted[fixed[i]] = i + 1;
    8383             :         }
    8384       87546 :         for (int i = 0, ncellsused = 0; ncellsused < ncellvars; i++) {
    8385       71072 :             int oldindex = sorted[i] - 1;
    8386       71072 :             if (oldindex == -1) {
    8387       44676 :                 continue;
    8388             :             }
    8389       26396 :             struct instr make_cell = {
    8390             :                 .i_opcode = MAKE_CELL,
    8391             :                 // This will get fixed in offset_derefs().
    8392             :                 .i_oparg = oldindex,
    8393             :                 .i_loc = NO_LOCATION,
    8394             :                 .i_target = NULL,
    8395             :             };
    8396       26396 :             if (insert_instruction(entryblock, ncellsused, &make_cell) < 0) {
    8397           0 :                 return -1;
    8398             :             }
    8399       26396 :             ncellsused += 1;
    8400             :         }
    8401       16474 :         PyMem_RawFree(sorted);
    8402             :     }
    8403             : 
    8404      465113 :     if (nfreevars) {
    8405       24461 :         struct instr copy_frees = {
    8406             :             .i_opcode = COPY_FREE_VARS,
    8407             :             .i_oparg = nfreevars,
    8408             :             .i_loc = NO_LOCATION,
    8409             :             .i_target = NULL,
    8410             :         };
    8411       24461 :         if (insert_instruction(entryblock, 0, &copy_frees) < 0) {
    8412           0 :             return -1;
    8413             :         }
    8414             : 
    8415             :     }
    8416             : 
    8417      465113 :     return 0;
    8418             : }
    8419             : 
    8420             : /* Make sure that all returns have a line number, even if early passes
    8421             :  * have failed to propagate a correct line number.
    8422             :  * The resulting line number may not be correct according to PEP 626,
    8423             :  * but should be "good enough", and no worse than in older versions. */
    8424             : static void
    8425      465113 : guarantee_lineno_for_exits(basicblock *entryblock, int firstlineno) {
    8426      465113 :     int lineno = firstlineno;
    8427      465113 :     assert(lineno > 0);
    8428     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    8429     2241920 :         if (b->b_iused == 0) {
    8430           0 :             continue;
    8431             :         }
    8432     2241920 :         struct instr *last = &b->b_instr[b->b_iused-1];
    8433     2241920 :         if (last->i_loc.lineno < 0) {
    8434       71197 :             if (last->i_opcode == RETURN_VALUE) {
    8435         107 :                 for (int i = 0; i < b->b_iused; i++) {
    8436          88 :                     assert(b->b_instr[i].i_loc.lineno < 0);
    8437             : 
    8438          88 :                     b->b_instr[i].i_loc.lineno = lineno;
    8439             :                 }
    8440             :             }
    8441             :         }
    8442             :         else {
    8443     2170730 :             lineno = last->i_loc.lineno;
    8444             :         }
    8445             :     }
    8446      465113 : }
    8447             : 
    8448             : static int
    8449      465113 : fix_cell_offsets(struct compiler *c, basicblock *entryblock, int *fixedmap)
    8450             : {
    8451      465113 :     int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
    8452      465113 :     int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
    8453      465113 :     int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
    8454      465113 :     int noffsets = ncellvars + nfreevars;
    8455             : 
    8456             :     // First deal with duplicates (arg cells).
    8457      465113 :     int numdropped = 0;
    8458      526545 :     for (int i = 0; i < noffsets ; i++) {
    8459       61432 :         if (fixedmap[i] == i + nlocals) {
    8460       51480 :             fixedmap[i] -= numdropped;
    8461             :         }
    8462             :         else {
    8463             :             // It was a duplicate (cell/arg).
    8464        9952 :             numdropped += 1;
    8465             :         }
    8466             :     }
    8467             : 
    8468             :     // Then update offsets, either relative to locals or by cell2arg.
    8469     2926470 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    8470    21014000 :         for (int i = 0; i < b->b_iused; i++) {
    8471    18552700 :             struct instr *inst = &b->b_instr[i];
    8472             :             // This is called before extended args are generated.
    8473    18552700 :             assert(inst->i_opcode != EXTENDED_ARG);
    8474    18552700 :             assert(inst->i_opcode != EXTENDED_ARG_QUICK);
    8475    18552700 :             int oldoffset = inst->i_oparg;
    8476    18552700 :             switch(inst->i_opcode) {
    8477      156828 :                 case MAKE_CELL:
    8478             :                 case LOAD_CLOSURE:
    8479             :                 case LOAD_DEREF:
    8480             :                 case STORE_DEREF:
    8481             :                 case DELETE_DEREF:
    8482             :                 case LOAD_CLASSDEREF:
    8483      156828 :                     assert(oldoffset >= 0);
    8484      156828 :                     assert(oldoffset < noffsets);
    8485      156828 :                     assert(fixedmap[oldoffset] >= 0);
    8486      156828 :                     inst->i_oparg = fixedmap[oldoffset];
    8487             :             }
    8488             :         }
    8489             :     }
    8490             : 
    8491      465113 :     return numdropped;
    8492             : }
    8493             : 
    8494             : static void
    8495             : propagate_line_numbers(basicblock *entryblock);
    8496             : 
    8497             : static void
    8498             : eliminate_empty_basic_blocks(basicblock *entryblock);
    8499             : 
    8500             : 
    8501             : static void
    8502      465113 : remove_redundant_jumps(basicblock *entryblock) {
    8503             :     /* If a non-empty block ends with a jump instruction, check if the next
    8504             :      * non-empty block reached through normal flow control is the target
    8505             :      * of that jump. If it is, then the jump instruction is redundant and
    8506             :      * can be deleted.
    8507             :      */
    8508      465113 :     int removed = 0;
    8509     2707140 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    8510     2242020 :         if (b->b_iused > 0) {
    8511     2242020 :             struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
    8512     2242020 :             assert(!IS_ASSEMBLER_OPCODE(b_last_instr->i_opcode));
    8513     2242020 :             if (b_last_instr->i_opcode == JUMP ||
    8514     2026880 :                 b_last_instr->i_opcode == JUMP_NO_INTERRUPT) {
    8515      219677 :                 if (b_last_instr->i_target == b->b_next) {
    8516       34886 :                     assert(b->b_next->b_iused);
    8517       34886 :                     b_last_instr->i_opcode = NOP;
    8518       34886 :                     removed++;
    8519             :                 }
    8520             :             }
    8521             :         }
    8522             :     }
    8523      465113 :     if (removed) {
    8524       23285 :         eliminate_empty_basic_blocks(entryblock);
    8525             :     }
    8526      465113 : }
    8527             : 
    8528             : static PyCodeObject *
    8529      465113 : assemble(struct compiler *c, int addNone)
    8530             : {
    8531      465113 :     PyCodeObject *co = NULL;
    8532      465113 :     PyObject *consts = NULL;
    8533             :     struct assembler a;
    8534      465113 :     memset(&a, 0, sizeof(struct assembler));
    8535             : 
    8536      465113 :     int code_flags = compute_code_flags(c);
    8537      465113 :     if (code_flags < 0) {
    8538           0 :         return NULL;
    8539             :     }
    8540             : 
    8541             :     /* Make sure every block that falls off the end returns None. */
    8542      465113 :     if (!basicblock_returns(c->u->u_curblock)) {
    8543      249539 :         UNSET_LOC(c);
    8544      249539 :         if (addNone)
    8545      177088 :             ADDOP_LOAD_CONST(c, Py_None);
    8546      249539 :         ADDOP(c, RETURN_VALUE);
    8547             :     }
    8548             : 
    8549     2926510 :     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
    8550     2461400 :         if (normalize_basic_block(b)) {
    8551           0 :             return NULL;
    8552             :         }
    8553             :     }
    8554             : 
    8555     2926510 :     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
    8556     2461400 :         if (extend_block(b)) {
    8557           0 :             return NULL;
    8558             :         }
    8559             :     }
    8560             : 
    8561      465113 :     assert(PyDict_GET_SIZE(c->u->u_varnames) < INT_MAX);
    8562      465113 :     assert(PyDict_GET_SIZE(c->u->u_cellvars) < INT_MAX);
    8563      465113 :     assert(PyDict_GET_SIZE(c->u->u_freevars) < INT_MAX);
    8564      465113 :     int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
    8565      465113 :     int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
    8566      465113 :     int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
    8567      465113 :     assert(INT_MAX - nlocals - ncellvars > 0);
    8568      465113 :     assert(INT_MAX - nlocals - ncellvars - nfreevars > 0);
    8569      465113 :     int nlocalsplus = nlocals + ncellvars + nfreevars;
    8570      465113 :     int *cellfixedoffsets = build_cellfixedoffsets(c);
    8571      465113 :     if (cellfixedoffsets == NULL) {
    8572           0 :         goto error;
    8573             :     }
    8574             : 
    8575      465113 :     int nblocks = 0;
    8576      465113 :     basicblock *entryblock = NULL;
    8577     2926510 :     for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
    8578     2461400 :         nblocks++;
    8579     2461400 :         entryblock = b;
    8580             :     }
    8581      465113 :     assert(entryblock != NULL);
    8582      465113 :     if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
    8583           0 :         PyErr_NoMemory();
    8584           0 :         goto error;
    8585             :     }
    8586             : 
    8587             :     /* Set firstlineno if it wasn't explicitly set. */
    8588      465113 :     if (!c->u->u_firstlineno) {
    8589           0 :         if (entryblock->b_instr && entryblock->b_instr->i_loc.lineno) {
    8590           0 :             c->u->u_firstlineno = entryblock->b_instr->i_loc.lineno;
    8591             :         }
    8592             :         else {
    8593           0 :             c->u->u_firstlineno = 1;
    8594             :         }
    8595             :     }
    8596             : 
    8597             :     // This must be called before fix_cell_offsets().
    8598      465113 :     if (insert_prefix_instructions(c, entryblock, cellfixedoffsets, nfreevars, code_flags)) {
    8599           0 :         goto error;
    8600             :     }
    8601             : 
    8602      465113 :     int numdropped = fix_cell_offsets(c, entryblock, cellfixedoffsets);
    8603      465113 :     PyMem_Free(cellfixedoffsets);  // At this point we're done with it.
    8604      465113 :     cellfixedoffsets = NULL;
    8605      465113 :     if (numdropped < 0) {
    8606           0 :         goto error;
    8607             :     }
    8608      465113 :     nlocalsplus -= numdropped;
    8609             : 
    8610      465113 :     consts = consts_dict_keys_inorder(c->u->u_consts);
    8611      465113 :     if (consts == NULL) {
    8612           0 :         goto error;
    8613             :     }
    8614             : 
    8615      465113 :     if (optimize_cfg(entryblock, consts, c->c_const_cache)) {
    8616           0 :         goto error;
    8617             :     }
    8618      465113 :     if (duplicate_exits_without_lineno(entryblock)) {
    8619           0 :         return NULL;
    8620             :     }
    8621      465113 :     if (trim_unused_consts(entryblock, consts)) {
    8622           0 :         goto error;
    8623             :     }
    8624      465113 :     propagate_line_numbers(entryblock);
    8625      465113 :     guarantee_lineno_for_exits(entryblock, c->u->u_firstlineno);
    8626             : 
    8627      465113 :     int maxdepth = stackdepth(entryblock, code_flags);
    8628      465113 :     if (maxdepth < 0) {
    8629           0 :         goto error;
    8630             :     }
    8631             :     /* TO DO -- For 3.12, make sure that `maxdepth <= MAX_ALLOWED_STACK_USE` */
    8632             : 
    8633      465113 :     if (label_exception_targets(entryblock)) {
    8634           0 :         goto error;
    8635             :     }
    8636      465113 :     convert_exception_handlers_to_nops(entryblock);
    8637             : 
    8638      465113 :     if (push_cold_blocks_to_end(entryblock, code_flags) < 0) {
    8639           0 :         goto error;
    8640             :     }
    8641             : 
    8642      465113 :     remove_redundant_jumps(entryblock);
    8643     2707140 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    8644     2242020 :         clean_basic_block(b);
    8645             :     }
    8646             : 
    8647             :     /* Order of basic blocks must have been determined by now */
    8648      465113 :     normalize_jumps(entryblock);
    8649             : 
    8650      465113 :     if (add_checks_for_loads_of_unknown_variables(entryblock, c) < 0) {
    8651           0 :         goto error;
    8652             :     }
    8653             : 
    8654             :     /* Can't modify the bytecode after computing jump offsets. */
    8655      465113 :     assemble_jump_offsets(entryblock);
    8656             : 
    8657             : 
    8658             :     /* Create assembler */
    8659      465113 :     if (!assemble_init(&a, c->u->u_firstlineno))
    8660           0 :         goto error;
    8661             : 
    8662             :     /* Emit code. */
    8663     2707140 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    8664    19610900 :         for (int j = 0; j < b->b_iused; j++)
    8665    17368800 :             if (!assemble_emit(&a, &b->b_instr[j]))
    8666           0 :                 goto error;
    8667             :     }
    8668             : 
    8669             :     /* Emit location info */
    8670      465113 :     a.a_lineno = c->u->u_firstlineno;
    8671     2707140 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    8672    19610900 :         for (int j = 0; j < b->b_iused; j++)
    8673    17368800 :             if (!assemble_emit_location(&a, &b->b_instr[j]))
    8674           0 :                 goto error;
    8675             :     }
    8676             : 
    8677      465113 :     if (!assemble_exception_table(&a, entryblock)) {
    8678           0 :         goto error;
    8679             :     }
    8680      465113 :     if (_PyBytes_Resize(&a.a_except_table, a.a_except_table_off) < 0) {
    8681           0 :         goto error;
    8682             :     }
    8683      465113 :     if (!merge_const_one(c->c_const_cache, &a.a_except_table)) {
    8684           0 :         goto error;
    8685             :     }
    8686             : 
    8687      465113 :     if (_PyBytes_Resize(&a.a_linetable, a.a_location_off) < 0) {
    8688           0 :         goto error;
    8689             :     }
    8690      465113 :     if (!merge_const_one(c->c_const_cache, &a.a_linetable)) {
    8691           0 :         goto error;
    8692             :     }
    8693             : 
    8694      465113 :     if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
    8695           0 :         goto error;
    8696             :     }
    8697      465113 :     if (!merge_const_one(c->c_const_cache, &a.a_bytecode)) {
    8698           0 :         goto error;
    8699             :     }
    8700             : 
    8701      465113 :     co = makecode(c, &a, consts, maxdepth, nlocalsplus, code_flags);
    8702      465113 :  error:
    8703      465113 :     Py_XDECREF(consts);
    8704      465113 :     assemble_free(&a);
    8705      465113 :     if (cellfixedoffsets != NULL) {
    8706           0 :         PyMem_Free(cellfixedoffsets);
    8707             :     }
    8708      465113 :     return co;
    8709             : }
    8710             : 
    8711             : static PyObject*
    8712      125059 : get_const_value(int opcode, int oparg, PyObject *co_consts)
    8713             : {
    8714      125059 :     PyObject *constant = NULL;
    8715      125059 :     assert(HAS_CONST(opcode));
    8716      125059 :     if (opcode == LOAD_CONST) {
    8717      125059 :         constant = PyList_GET_ITEM(co_consts, oparg);
    8718             :     }
    8719             : 
    8720      125059 :     if (constant == NULL) {
    8721           0 :         PyErr_SetString(PyExc_SystemError,
    8722             :                         "Internal error: failed to get value of a constant");
    8723           0 :         return NULL;
    8724             :     }
    8725      125059 :     Py_INCREF(constant);
    8726      125059 :     return constant;
    8727             : }
    8728             : 
    8729             : /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
    8730             :    with    LOAD_CONST (c1, c2, ... cn).
    8731             :    The consts table must still be in list form so that the
    8732             :    new constant (c1, c2, ... cn) can be appended.
    8733             :    Called with codestr pointing to the first LOAD_CONST.
    8734             : */
    8735             : static int
    8736      217862 : fold_tuple_on_constants(PyObject *const_cache,
    8737             :                         struct instr *inst,
    8738             :                         int n, PyObject *consts)
    8739             : {
    8740             :     /* Pre-conditions */
    8741      217862 :     assert(PyDict_CheckExact(const_cache));
    8742      217862 :     assert(PyList_CheckExact(consts));
    8743      217862 :     assert(inst[n].i_opcode == BUILD_TUPLE);
    8744      217862 :     assert(inst[n].i_oparg == n);
    8745             : 
    8746      326665 :     for (int i = 0; i < n; i++) {
    8747      290317 :         if (!HAS_CONST(inst[i].i_opcode)) {
    8748      181514 :             return 0;
    8749             :         }
    8750             :     }
    8751             : 
    8752             :     /* Buildup new tuple of constants */
    8753       36348 :     PyObject *newconst = PyTuple_New(n);
    8754       36348 :     if (newconst == NULL) {
    8755           0 :         return -1;
    8756             :     }
    8757      103748 :     for (int i = 0; i < n; i++) {
    8758       67400 :         int op = inst[i].i_opcode;
    8759       67400 :         int arg = inst[i].i_oparg;
    8760       67400 :         PyObject *constant = get_const_value(op, arg, consts);
    8761       67400 :         if (constant == NULL) {
    8762           0 :             return -1;
    8763             :         }
    8764       67400 :         PyTuple_SET_ITEM(newconst, i, constant);
    8765             :     }
    8766       36348 :     if (merge_const_one(const_cache, &newconst) == 0) {
    8767           0 :         Py_DECREF(newconst);
    8768           0 :         return -1;
    8769             :     }
    8770             : 
    8771             :     Py_ssize_t index;
    8772     1760030 :     for (index = 0; index < PyList_GET_SIZE(consts); index++) {
    8773     1734040 :         if (PyList_GET_ITEM(consts, index) == newconst) {
    8774       10361 :             break;
    8775             :         }
    8776             :     }
    8777       36348 :     if (index == PyList_GET_SIZE(consts)) {
    8778       25987 :         if ((size_t)index >= (size_t)INT_MAX - 1) {
    8779           0 :             Py_DECREF(newconst);
    8780           0 :             PyErr_SetString(PyExc_OverflowError, "too many constants");
    8781           0 :             return -1;
    8782             :         }
    8783       25987 :         if (PyList_Append(consts, newconst)) {
    8784           0 :             Py_DECREF(newconst);
    8785           0 :             return -1;
    8786             :         }
    8787             :     }
    8788       36348 :     Py_DECREF(newconst);
    8789      103748 :     for (int i = 0; i < n; i++) {
    8790       67400 :         inst[i].i_opcode = NOP;
    8791             :     }
    8792       36348 :     inst[n].i_opcode = LOAD_CONST;
    8793       36348 :     inst[n].i_oparg = (int)index;
    8794       36348 :     return 0;
    8795             : }
    8796             : 
    8797             : #define VISITED (-1)
    8798             : 
    8799             : // Replace an arbitrary run of SWAPs and NOPs with an optimal one that has the
    8800             : // same effect.
    8801             : static int
    8802       19164 : swaptimize(basicblock *block, int *ix)
    8803             : {
    8804             :     // NOTE: "./python -m test test_patma" serves as a good, quick stress test
    8805             :     // for this function. Make sure to blow away cached *.pyc files first!
    8806       19164 :     assert(*ix < block->b_iused);
    8807       19164 :     struct instr *instructions = &block->b_instr[*ix];
    8808             :     // Find the length of the current sequence of SWAPs and NOPs, and record the
    8809             :     // maximum depth of the stack manipulations:
    8810       19164 :     assert(instructions[0].i_opcode == SWAP);
    8811       19164 :     int depth = instructions[0].i_oparg;
    8812       19164 :     int len = 0;
    8813       19164 :     int more = false;
    8814       19164 :     int limit = block->b_iused - *ix;
    8815       23746 :     while (++len < limit) {
    8816       23746 :         int opcode = instructions[len].i_opcode;
    8817       23746 :         if (opcode == SWAP) {
    8818        4582 :             depth = Py_MAX(depth, instructions[len].i_oparg);
    8819        4582 :             more = true;
    8820             :         }
    8821       19164 :         else if (opcode != NOP) {
    8822       19164 :             break;
    8823             :         }
    8824             :     }
    8825             :     // It's already optimal if there's only one SWAP:
    8826       19164 :     if (!more) {
    8827       17742 :         return 0;
    8828             :     }
    8829             :     // Create an array with elements {0, 1, 2, ..., depth - 1}:
    8830        1422 :     int *stack = PyMem_Malloc(depth * sizeof(int));
    8831        1422 :     if (stack == NULL) {
    8832           0 :         PyErr_NoMemory();
    8833           0 :         return -1;
    8834             :     }
    8835        5924 :     for (int i = 0; i < depth; i++) {
    8836        4502 :         stack[i] = i;
    8837             :     }
    8838             :     // Simulate the combined effect of these instructions by "running" them on
    8839             :     // our "stack":
    8840        7426 :     for (int i = 0; i < len; i++) {
    8841        6004 :         if (instructions[i].i_opcode == SWAP) {
    8842        6004 :             int oparg = instructions[i].i_oparg;
    8843        6004 :             int top = stack[0];
    8844             :             // SWAPs are 1-indexed:
    8845        6004 :             stack[0] = stack[oparg - 1];
    8846        6004 :             stack[oparg - 1] = top;
    8847             :         }
    8848             :     }
    8849             :     // Now we can begin! Our approach here is based on a solution to a closely
    8850             :     // related problem (https://cs.stackexchange.com/a/13938). It's easiest to
    8851             :     // think of this algorithm as determining the steps needed to efficiently
    8852             :     // "un-shuffle" our stack. By performing the moves in *reverse* order,
    8853             :     // though, we can efficiently *shuffle* it! For this reason, we will be
    8854             :     // replacing instructions starting from the *end* of the run. Since the
    8855             :     // solution is optimal, we don't need to worry about running out of space:
    8856        1422 :     int current = len - 1;
    8857        5924 :     for (int i = 0; i < depth; i++) {
    8858             :         // Skip items that have already been visited, or just happen to be in
    8859             :         // the correct location:
    8860        4502 :         if (stack[i] == VISITED || stack[i] == i) {
    8861        3096 :             continue;
    8862             :         }
    8863             :         // Okay, we've found an item that hasn't been visited. It forms a cycle
    8864             :         // with other items; traversing the cycle and swapping each item with
    8865             :         // the next will put them all in the correct place. The weird
    8866             :         // loop-and-a-half is necessary to insert 0 into every cycle, since we
    8867             :         // can only swap from that position:
    8868        1406 :         int j = i;
    8869        4350 :         while (true) {
    8870             :             // Skip the actual swap if our item is zero, since swapping the top
    8871             :             // item with itself is pointless:
    8872        5756 :             if (j) {
    8873        3038 :                 assert(0 <= current);
    8874             :                 // SWAPs are 1-indexed:
    8875        3038 :                 instructions[current].i_opcode = SWAP;
    8876        3038 :                 instructions[current--].i_oparg = j + 1;
    8877             :             }
    8878        5756 :             if (stack[j] == VISITED) {
    8879             :                 // Completed the cycle:
    8880        1406 :                 assert(j == i);
    8881        1406 :                 break;
    8882             :             }
    8883        4350 :             int next_j = stack[j];
    8884        4350 :             stack[j] = VISITED;
    8885        4350 :             j = next_j;
    8886             :         }
    8887             :     }
    8888             :     // NOP out any unused instructions:
    8889        4388 :     while (0 <= current) {
    8890        2966 :         instructions[current--].i_opcode = NOP;
    8891             :     }
    8892        1422 :     PyMem_Free(stack);
    8893        1422 :     *ix += len - 1;
    8894        1422 :     return 0;
    8895             : }
    8896             : 
    8897             : // This list is pretty small, since it's only okay to reorder opcodes that:
    8898             : // - can't affect control flow (like jumping or raising exceptions)
    8899             : // - can't invoke arbitrary code (besides finalizers)
    8900             : // - only touch the TOS (and pop it when finished)
    8901             : #define SWAPPABLE(opcode) \
    8902             :     ((opcode) == STORE_FAST || (opcode) == POP_TOP)
    8903             : 
    8904             : static int
    8905       26144 : next_swappable_instruction(basicblock *block, int i, int lineno)
    8906             : {
    8907       26996 :     while (++i < block->b_iused) {
    8908       26718 :         struct instr *instruction = &block->b_instr[i];
    8909       26718 :         if (0 <= lineno && instruction->i_loc.lineno != lineno) {
    8910             :             // Optimizing across this instruction could cause user-visible
    8911             :             // changes in the names bound between line tracing events!
    8912          43 :             return -1;
    8913             :         }
    8914       26675 :         if (instruction->i_opcode == NOP) {
    8915         852 :             continue;
    8916             :         }
    8917       25823 :         if (SWAPPABLE(instruction->i_opcode)) {
    8918        9878 :             return i;
    8919             :         }
    8920       15945 :         return -1;
    8921             :     }
    8922         278 :     return -1;
    8923             : }
    8924             : 
    8925             : // Attempt to apply SWAPs statically by swapping *instructions* rather than
    8926             : // stack items. For example, we can replace SWAP(2), POP_TOP, STORE_FAST(42)
    8927             : // with the more efficient NOP, STORE_FAST(42), POP_TOP.
    8928             : static void
    8929       19164 : apply_static_swaps(basicblock *block, int i)
    8930             : {
    8931             :     // SWAPs are to our left, and potential swaperands are to our right:
    8932       25614 :     for (; 0 <= i; i--) {
    8933       25577 :         assert(i < block->b_iused);
    8934       25577 :         struct instr *swap = &block->b_instr[i];
    8935       25577 :         if (swap->i_opcode != SWAP) {
    8936        6283 :             if (swap->i_opcode == NOP || SWAPPABLE(swap->i_opcode)) {
    8937             :                 // Nope, but we know how to handle these. Keep looking:
    8938        3422 :                 continue;
    8939             :             }
    8940             :             // We can't reason about what this instruction does. Bail:
    8941       19127 :             return;
    8942             :         }
    8943       19294 :         int j = next_swappable_instruction(block, i, -1);
    8944       19294 :         if (j < 0) {
    8945       13258 :             return;
    8946             :         }
    8947        6036 :         int k = j;
    8948        6036 :         int lineno = block->b_instr[j].i_loc.lineno;
    8949        9878 :         for (int count = swap->i_oparg - 1; 0 < count; count--) {
    8950        6850 :             k = next_swappable_instruction(block, k, lineno);
    8951        6850 :             if (k < 0) {
    8952        3008 :                 return;
    8953             :             }
    8954             :         }
    8955             :         // Success!
    8956        3028 :         swap->i_opcode = NOP;
    8957        3028 :         struct instr temp = block->b_instr[j];
    8958        3028 :         block->b_instr[j] = block->b_instr[k];
    8959        3028 :         block->b_instr[k] = temp;
    8960             :     }
    8961             : }
    8962             : 
    8963             : // Attempt to eliminate jumps to jumps by updating inst to jump to
    8964             : // target->i_target using the provided opcode. Return whether or not the
    8965             : // optimization was successful.
    8966             : static bool
    8967       55796 : jump_thread(struct instr *inst, struct instr *target, int opcode)
    8968             : {
    8969       55796 :     assert(is_jump(inst));
    8970       55796 :     assert(is_jump(target));
    8971             :     // bpo-45773: If inst->i_target == target->i_target, then nothing actually
    8972             :     // changes (and we fall into an infinite loop):
    8973       55796 :     if ((inst->i_loc.lineno == target->i_loc.lineno || target->i_loc.lineno == -1) &&
    8974       55742 :         inst->i_target != target->i_target)
    8975             :     {
    8976       55740 :         inst->i_target = target->i_target;
    8977       55740 :         inst->i_opcode = opcode;
    8978       55740 :         return true;
    8979             :     }
    8980          56 :     return false;
    8981             : }
    8982             : 
    8983             : /* Maximum size of basic block that should be copied in optimizer */
    8984             : #define MAX_COPY_SIZE 4
    8985             : 
    8986             : /* Optimization */
    8987             : static int
    8988     2461350 : optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
    8989             : {
    8990     2461350 :     assert(PyDict_CheckExact(const_cache));
    8991     2461350 :     assert(PyList_CheckExact(consts));
    8992             :     struct instr nop;
    8993     2461350 :     nop.i_opcode = NOP;
    8994             :     struct instr *target;
    8995    21066300 :     for (int i = 0; i < bb->b_iused; i++) {
    8996    18604900 :         struct instr *inst = &bb->b_instr[i];
    8997    18604900 :         int oparg = inst->i_oparg;
    8998    18604900 :         int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
    8999    18604900 :         if (is_jump(inst) || is_block_push(inst)) {
    9000             :             /* Skip over empty basic blocks. */
    9001     1164440 :             while (inst->i_target->b_iused == 0) {
    9002           0 :                 inst->i_target = inst->i_target->b_next;
    9003             :             }
    9004     1164440 :             target = &inst->i_target->b_instr[0];
    9005     1164440 :             assert(!IS_ASSEMBLER_OPCODE(target->i_opcode));
    9006             :         }
    9007             :         else {
    9008    17440500 :             target = &nop;
    9009             :         }
    9010    18604900 :         assert(!IS_ASSEMBLER_OPCODE(inst->i_opcode));
    9011    18604900 :         switch (inst->i_opcode) {
    9012             :             /* Remove LOAD_CONST const; conditional jump */
    9013     3467380 :             case LOAD_CONST:
    9014             :             {
    9015             :                 PyObject* cnt;
    9016             :                 int is_true;
    9017             :                 int jump_if_true;
    9018             :                 switch(nextop) {
    9019        8478 :                     case POP_JUMP_IF_FALSE:
    9020             :                     case POP_JUMP_IF_TRUE:
    9021        8478 :                         cnt = get_const_value(inst->i_opcode, oparg, consts);
    9022        8478 :                         if (cnt == NULL) {
    9023           0 :                             goto error;
    9024             :                         }
    9025        8478 :                         is_true = PyObject_IsTrue(cnt);
    9026        8478 :                         Py_DECREF(cnt);
    9027        8478 :                         if (is_true == -1) {
    9028           0 :                             goto error;
    9029             :                         }
    9030        8478 :                         inst->i_opcode = NOP;
    9031        8478 :                         jump_if_true = nextop == POP_JUMP_IF_TRUE;
    9032        8478 :                         if (is_true == jump_if_true) {
    9033        3902 :                             bb->b_instr[i+1].i_opcode = JUMP;
    9034             :                         }
    9035             :                         else {
    9036        4576 :                             bb->b_instr[i+1].i_opcode = NOP;
    9037             :                         }
    9038        8478 :                         break;
    9039          79 :                     case JUMP_IF_FALSE_OR_POP:
    9040             :                     case JUMP_IF_TRUE_OR_POP:
    9041          79 :                         cnt = get_const_value(inst->i_opcode, oparg, consts);
    9042          79 :                         if (cnt == NULL) {
    9043           0 :                             goto error;
    9044             :                         }
    9045          79 :                         is_true = PyObject_IsTrue(cnt);
    9046          79 :                         Py_DECREF(cnt);
    9047          79 :                         if (is_true == -1) {
    9048           0 :                             goto error;
    9049             :                         }
    9050          79 :                         jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
    9051          79 :                         if (is_true == jump_if_true) {
    9052          43 :                             bb->b_instr[i+1].i_opcode = JUMP;
    9053             :                         }
    9054             :                         else {
    9055          36 :                             inst->i_opcode = NOP;
    9056          36 :                             bb->b_instr[i+1].i_opcode = NOP;
    9057             :                         }
    9058          79 :                         break;
    9059       49102 :                     case IS_OP:
    9060       49102 :                         cnt = get_const_value(inst->i_opcode, oparg, consts);
    9061       49102 :                         if (cnt == NULL) {
    9062           0 :                             goto error;
    9063             :                         }
    9064       49102 :                         int jump_op = i+2 < bb->b_iused ? bb->b_instr[i+2].i_opcode : 0;
    9065       49102 :                         if (Py_IsNone(cnt) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE)) {
    9066       46655 :                             unsigned char nextarg = bb->b_instr[i+1].i_oparg;
    9067       46655 :                             inst->i_opcode = NOP;
    9068       46655 :                             bb->b_instr[i+1].i_opcode = NOP;
    9069       46655 :                             bb->b_instr[i+2].i_opcode = nextarg ^ (jump_op == POP_JUMP_IF_FALSE) ?
    9070       46655 :                                     POP_JUMP_IF_NOT_NONE : POP_JUMP_IF_NONE;
    9071             :                         }
    9072       49102 :                         Py_DECREF(cnt);
    9073       49102 :                         break;
    9074             :                 }
    9075     3467380 :                 break;
    9076             :             }
    9077             : 
    9078             :                 /* Try to fold tuples of constants.
    9079             :                    Skip over BUILD_TUPLE(1) UNPACK_SEQUENCE(1).
    9080             :                    Replace BUILD_TUPLE(2) UNPACK_SEQUENCE(2) with SWAP(2).
    9081             :                    Replace BUILD_TUPLE(3) UNPACK_SEQUENCE(3) with SWAP(3). */
    9082      221679 :             case BUILD_TUPLE:
    9083      221679 :                 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
    9084             :                     switch(oparg) {
    9085           1 :                         case 1:
    9086           1 :                             inst->i_opcode = NOP;
    9087           1 :                             bb->b_instr[i+1].i_opcode = NOP;
    9088           1 :                             continue;
    9089        3215 :                         case 2:
    9090             :                         case 3:
    9091        3215 :                             inst->i_opcode = NOP;
    9092        3215 :                             bb->b_instr[i+1].i_opcode = SWAP;
    9093        3215 :                             continue;
    9094             :                     }
    9095             :                 }
    9096      218463 :                 if (i >= oparg) {
    9097      217862 :                     if (fold_tuple_on_constants(const_cache, inst-oparg, oparg, consts)) {
    9098           0 :                         goto error;
    9099             :                     }
    9100             :                 }
    9101      218463 :                 break;
    9102             : 
    9103             :                 /* Simplify conditional jump to conditional jump where the
    9104             :                    result of the first test implies the success of a similar
    9105             :                    test or the failure of the opposite test.
    9106             :                    Arises in code like:
    9107             :                    "a and b or c"
    9108             :                    "(a and b) and c"
    9109             :                    "(a or b) or c"
    9110             :                    "(a or b) and c"
    9111             :                    x:JUMP_IF_FALSE_OR_POP y   y:JUMP_IF_FALSE_OR_POP z
    9112             :                       -->  x:JUMP_IF_FALSE_OR_POP z
    9113             :                    x:JUMP_IF_FALSE_OR_POP y   y:JUMP_IF_TRUE_OR_POP z
    9114             :                       -->  x:POP_JUMP_IF_FALSE y+1
    9115             :                    where y+1 is the instruction following the second test.
    9116             :                 */
    9117        8021 :             case JUMP_IF_FALSE_OR_POP:
    9118        8021 :                 switch (target->i_opcode) {
    9119           0 :                     case POP_JUMP_IF_FALSE:
    9120           0 :                         i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
    9121           0 :                         break;
    9122          38 :                     case JUMP:
    9123             :                     case JUMP_IF_FALSE_OR_POP:
    9124          38 :                         i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
    9125          38 :                         break;
    9126        1103 :                     case JUMP_IF_TRUE_OR_POP:
    9127             :                     case POP_JUMP_IF_TRUE:
    9128        1103 :                         if (inst->i_loc.lineno == target->i_loc.lineno) {
    9129             :                             // We don't need to bother checking for loops here,
    9130             :                             // since a block's b_next cannot point to itself:
    9131        1068 :                             assert(inst->i_target != inst->i_target->b_next);
    9132        1068 :                             inst->i_opcode = POP_JUMP_IF_FALSE;
    9133        1068 :                             inst->i_target = inst->i_target->b_next;
    9134        1068 :                             --i;
    9135             :                         }
    9136        1103 :                         break;
    9137             :                 }
    9138        8021 :                 break;
    9139       12305 :             case JUMP_IF_TRUE_OR_POP:
    9140       12305 :                 switch (target->i_opcode) {
    9141           0 :                     case POP_JUMP_IF_TRUE:
    9142           0 :                         i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
    9143           0 :                         break;
    9144          29 :                     case JUMP:
    9145             :                     case JUMP_IF_TRUE_OR_POP:
    9146          29 :                         i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
    9147          29 :                         break;
    9148         109 :                     case JUMP_IF_FALSE_OR_POP:
    9149             :                     case POP_JUMP_IF_FALSE:
    9150         109 :                         if (inst->i_loc.lineno == target->i_loc.lineno) {
    9151             :                             // We don't need to bother checking for loops here,
    9152             :                             // since a block's b_next cannot point to itself:
    9153          31 :                             assert(inst->i_target != inst->i_target->b_next);
    9154          31 :                             inst->i_opcode = POP_JUMP_IF_TRUE;
    9155          31 :                             inst->i_target = inst->i_target->b_next;
    9156          31 :                             --i;
    9157             :                         }
    9158         109 :                         break;
    9159             :                 }
    9160       12305 :                 break;
    9161       48976 :             case POP_JUMP_IF_NOT_NONE:
    9162             :             case POP_JUMP_IF_NONE:
    9163       48976 :                 switch (target->i_opcode) {
    9164        2101 :                     case JUMP:
    9165        2101 :                         i -= jump_thread(inst, target, inst->i_opcode);
    9166             :                 }
    9167       48976 :                 break;
    9168      521123 :             case POP_JUMP_IF_FALSE:
    9169      521123 :                 switch (target->i_opcode) {
    9170       23395 :                     case JUMP:
    9171       23395 :                         i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
    9172             :                 }
    9173      521123 :                 break;
    9174       88821 :             case POP_JUMP_IF_TRUE:
    9175       88821 :                 switch (target->i_opcode) {
    9176        2990 :                     case JUMP:
    9177        2990 :                         i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
    9178             :                 }
    9179       88821 :                 break;
    9180      279659 :             case JUMP:
    9181      279659 :                 switch (target->i_opcode) {
    9182       27243 :                     case JUMP:
    9183       27243 :                         i -= jump_thread(inst, target, JUMP);
    9184             :                 }
    9185      279659 :                 break;
    9186       64746 :             case FOR_ITER:
    9187       64746 :                 if (target->i_opcode == JUMP) {
    9188             :                     /* This will not work now because the jump (at target) could
    9189             :                      * be forward or backward and FOR_ITER only jumps forward. We
    9190             :                      * can re-enable this if ever we implement a backward version
    9191             :                      * of FOR_ITER.
    9192             :                      */
    9193             :                     /*
    9194             :                     i -= jump_thread(inst, target, FOR_ITER);
    9195             :                     */
    9196             :                 }
    9197       64746 :                 break;
    9198       19164 :             case SWAP:
    9199       19164 :                 if (oparg == 1) {
    9200           0 :                     inst->i_opcode = NOP;
    9201           0 :                     break;
    9202             :                 }
    9203       19164 :                 if (swaptimize(bb, &i)) {
    9204           0 :                     goto error;
    9205             :                 }
    9206       19164 :                 apply_static_swaps(bb, i);
    9207       19164 :                 break;
    9208       68012 :             case KW_NAMES:
    9209       68012 :                 break;
    9210      632103 :             case PUSH_NULL:
    9211      632103 :                 if (nextop == LOAD_GLOBAL && (inst[1].i_opcode & 1) == 0) {
    9212      465611 :                     inst->i_opcode = NOP;
    9213      465611 :                     inst->i_oparg = 0;
    9214      465611 :                     inst[1].i_oparg |= 1;
    9215             :                 }
    9216      632103 :                 break;
    9217    13173000 :             default:
    9218             :                 /* All HAS_CONST opcodes should be handled with LOAD_CONST */
    9219    13173000 :                 assert (!HAS_CONST(inst->i_opcode));
    9220             :         }
    9221             :     }
    9222     2461350 :     return 0;
    9223           0 : error:
    9224           0 :     return -1;
    9225             : }
    9226             : 
    9227             : static bool
    9228       91057 : basicblock_has_lineno(const basicblock *bb) {
    9229      204105 :     for (int i = 0; i < bb->b_iused; i++) {
    9230      148406 :         if (bb->b_instr[i].i_loc.lineno > 0) {
    9231       35358 :             return true;
    9232             :         }
    9233             :     }
    9234       55699 :     return false;
    9235             : }
    9236             : 
    9237             : /* If this block ends with an unconditional jump to an exit block,
    9238             :  * then remove the jump and extend this block with the target.
    9239             :  */
    9240             : static int
    9241     4922750 : extend_block(basicblock *bb) {
    9242     4922750 :     if (bb->b_iused == 0) {
    9243      285325 :         return 0;
    9244             :     }
    9245     4637420 :     struct instr *last = &bb->b_instr[bb->b_iused-1];
    9246     4637420 :     if (last->i_opcode != JUMP &&
    9247     4084110 :         last->i_opcode != JUMP_FORWARD &&
    9248     4084110 :         last->i_opcode != JUMP_BACKWARD) {
    9249     4084110 :         return 0;
    9250             :     }
    9251      553315 :     if (basicblock_exits_scope(last->i_target) && last->i_target->b_iused <= MAX_COPY_SIZE) {
    9252       91057 :         basicblock *to_copy = last->i_target;
    9253       91057 :         if (basicblock_has_lineno(to_copy)) {
    9254             :             /* copy only blocks without line number (like implicit 'return None's) */
    9255       35358 :             return 0;
    9256             :         }
    9257       55699 :         last->i_opcode = NOP;
    9258      168732 :         for (int i = 0; i < to_copy->b_iused; i++) {
    9259      113033 :             int index = basicblock_next_instr(bb);
    9260      113033 :             if (index < 0) {
    9261           0 :                 return -1;
    9262             :             }
    9263      113033 :             bb->b_instr[index] = to_copy->b_instr[i];
    9264             :         }
    9265             :     }
    9266      517957 :     return 0;
    9267             : }
    9268             : 
    9269             : static void
    9270     6961710 : clean_basic_block(basicblock *bb) {
    9271             :     /* Remove NOPs when legal to do so. */
    9272     6961710 :     int dest = 0;
    9273     6961710 :     int prev_lineno = -1;
    9274    60708700 :     for (int src = 0; src < bb->b_iused; src++) {
    9275    53747000 :         int lineno = bb->b_instr[src].i_loc.lineno;
    9276    53747000 :         if (bb->b_instr[src].i_opcode == NOP) {
    9277             :             /* Eliminate no-op if it doesn't have a line number */
    9278     1120820 :             if (lineno < 0) {
    9279       95646 :                 continue;
    9280             :             }
    9281             :             /* or, if the previous instruction had the same line number. */
    9282     1025180 :             if (prev_lineno == lineno) {
    9283      385808 :                 continue;
    9284             :             }
    9285             :             /* or, if the next instruction has same line number or no line number */
    9286      639370 :             if (src < bb->b_iused - 1) {
    9287      583221 :                 int next_lineno = bb->b_instr[src+1].i_loc.lineno;
    9288      583221 :                 if (next_lineno < 0 || next_lineno == lineno) {
    9289      514966 :                     bb->b_instr[src+1].i_loc = bb->b_instr[src].i_loc;
    9290      514966 :                     continue;
    9291             :                 }
    9292             :             }
    9293             :             else {
    9294       56149 :                 basicblock* next = bb->b_next;
    9295       56391 :                 while (next && next->b_iused == 0) {
    9296         242 :                     next = next->b_next;
    9297             :                 }
    9298             :                 /* or if last instruction in BB and next BB has same line number */
    9299       56149 :                 if (next) {
    9300       56149 :                     if (lineno == next->b_instr[0].i_loc.lineno) {
    9301        1372 :                         continue;
    9302             :                     }
    9303             :                 }
    9304             :             }
    9305             : 
    9306             :         }
    9307    52749200 :         if (dest != src) {
    9308     5499630 :             bb->b_instr[dest] = bb->b_instr[src];
    9309             :         }
    9310    52749200 :         dest++;
    9311    52749200 :         prev_lineno = lineno;
    9312             :     }
    9313     6961710 :     assert(dest <= bb->b_iused);
    9314     6961710 :     bb->b_iused = dest;
    9315     6961710 : }
    9316             : 
    9317             : static int
    9318     2461400 : normalize_basic_block(basicblock *bb) {
    9319             :     /* Mark blocks as exit and/or nofallthrough.
    9320             :      Raise SystemError if CFG is malformed. */
    9321    20825100 :     for (int i = 0; i < bb->b_iused; i++) {
    9322    18363700 :         int opcode = bb->b_instr[i].i_opcode;
    9323    18363700 :         assert(!IS_ASSEMBLER_OPCODE(opcode));
    9324    18363700 :         int is_jump = IS_JUMP_OPCODE(opcode);
    9325    18363700 :         int is_exit = IS_SCOPE_EXIT_OPCODE(opcode);
    9326    18363700 :         if (is_exit || is_jump) {
    9327     1769340 :             if (i != bb->b_iused-1) {
    9328           0 :                 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
    9329           0 :                 return -1;
    9330             :             }
    9331             :         }
    9332    18363700 :         if (is_jump) {
    9333             :             /* Skip over empty basic blocks. */
    9334     1240470 :             while (bb->b_instr[i].i_target->b_iused == 0) {
    9335      207569 :                 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
    9336             :             }
    9337             :         }
    9338             :     }
    9339     2461400 :     return 0;
    9340             : }
    9341             : 
    9342             : static int
    9343      465113 : mark_reachable(basicblock *entryblock) {
    9344      465113 :     basicblock **stack = make_cfg_traversal_stack(entryblock);
    9345      465113 :     if (stack == NULL) {
    9346           0 :         return -1;
    9347             :     }
    9348      465113 :     basicblock **sp = stack;
    9349      465113 :     entryblock->b_predecessors = 1;
    9350      465113 :     *sp++ = entryblock;
    9351     2730450 :     while (sp > stack) {
    9352     2265340 :         basicblock *b = *(--sp);
    9353     2265340 :         b->b_visited = 1;
    9354     2265340 :         if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
    9355     1311190 :             if (!b->b_next->b_visited) {
    9356      994942 :                 assert(b->b_next->b_predecessors == 0);
    9357      994942 :                 *sp++ = b->b_next;
    9358             :             }
    9359     1311190 :             b->b_next->b_predecessors++;
    9360             :         }
    9361    19847400 :         for (int i = 0; i < b->b_iused; i++) {
    9362             :             basicblock *target;
    9363    17582000 :             struct instr *instr = &b->b_instr[i];
    9364    17582000 :             if (is_jump(instr) || is_block_push(instr)) {
    9365     1069660 :                 target = instr->i_target;
    9366     1069660 :                 if (!target->b_visited) {
    9367      805283 :                     assert(target->b_predecessors == 0 || target == b->b_next);
    9368      805283 :                     *sp++ = target;
    9369             :                 }
    9370     1069660 :                 target->b_predecessors++;
    9371     1069660 :                 if (is_block_push(instr)) {
    9372      131559 :                     target->b_except_predecessors++;
    9373             :                 }
    9374     1069660 :                 assert(target->b_except_predecessors == 0 ||
    9375             :                        target->b_except_predecessors == target->b_predecessors);
    9376             :             }
    9377             :         }
    9378             :     }
    9379      465113 :     PyMem_Free(stack);
    9380      465113 :     return 0;
    9381             : }
    9382             : 
    9383             : static void
    9384      488398 : eliminate_empty_basic_blocks(basicblock *entryblock) {
    9385             :     /* Eliminate empty blocks */
    9386     3261780 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9387     2773380 :         basicblock *next = b->b_next;
    9388     2773380 :         if (next) {
    9389     2488000 :             while (next->b_iused == 0 && next->b_next) {
    9390      203021 :                 next = next->b_next;
    9391             :             }
    9392     2284980 :             b->b_next = next;
    9393             :         }
    9394             :     }
    9395     3261780 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9396     2773380 :         if (b->b_iused == 0) {
    9397       32287 :             continue;
    9398             :         }
    9399    23698300 :         for (int i = 0; i < b->b_iused; i++) {
    9400    20957200 :             struct instr *instr = &b->b_instr[i];
    9401    20957200 :             if (is_jump(instr) || is_block_push(instr)) {
    9402     1282060 :                 basicblock *target = instr->i_target;
    9403     1282260 :                 while (target->b_iused == 0) {
    9404         196 :                     target = target->b_next;
    9405             :                 }
    9406     1282060 :                 instr->i_target = target;
    9407             :             }
    9408             :         }
    9409             :     }
    9410      488398 : }
    9411             : 
    9412             : 
    9413             : /* If an instruction has no line number, but it's predecessor in the BB does,
    9414             :  * then copy the line number. If a successor block has no line number, and only
    9415             :  * one predecessor, then inherit the line number.
    9416             :  * This ensures that all exit blocks (with one predecessor) receive a line number.
    9417             :  * Also reduces the size of the line number table,
    9418             :  * but has no impact on the generated line number events.
    9419             :  */
    9420             : static void
    9421      465113 : propagate_line_numbers(basicblock *entryblock) {
    9422     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9423     2241920 :         if (b->b_iused == 0) {
    9424           0 :             continue;
    9425             :         }
    9426             : 
    9427     2241920 :         struct location prev_location = NO_LOCATION;
    9428    19854100 :         for (int i = 0; i < b->b_iused; i++) {
    9429    17612200 :             if (b->b_instr[i].i_loc.lineno < 0) {
    9430     1785750 :                 b->b_instr[i].i_loc = prev_location;
    9431             :             }
    9432             :             else {
    9433    15826500 :                 prev_location = b->b_instr[i].i_loc;
    9434             :             }
    9435             :         }
    9436     2241920 :         if (BB_HAS_FALLTHROUGH(b) && b->b_next->b_predecessors == 1) {
    9437      851885 :             assert(b->b_next->b_iused);
    9438      851885 :             if (b->b_next->b_instr[0].i_loc.lineno < 0) {
    9439       52318 :                 b->b_next->b_instr[0].i_loc = prev_location;
    9440             :             }
    9441             :         }
    9442     2241920 :         if (is_jump(&b->b_instr[b->b_iused-1])) {
    9443      938095 :             basicblock *target = b->b_instr[b->b_iused-1].i_target;
    9444      938095 :             if (target->b_predecessors == 1) {
    9445      321268 :                 if (target->b_instr[0].i_loc.lineno < 0) {
    9446       89541 :                     target->b_instr[0].i_loc = prev_location;
    9447             :                 }
    9448             :             }
    9449             :         }
    9450             :     }
    9451      465113 : }
    9452             : 
    9453             : /* Perform optimizations on a control flow graph.
    9454             :    The consts object should still be in list form to allow new constants
    9455             :    to be appended.
    9456             : 
    9457             :    All transformations keep the code size the same or smaller.
    9458             :    For those that reduce size, the gaps are initially filled with
    9459             :    NOPs.  Later those NOPs are removed.
    9460             : */
    9461             : 
    9462             : static int
    9463      465113 : optimize_cfg(basicblock *entryblock, PyObject *consts, PyObject *const_cache)
    9464             : {
    9465      465113 :     assert(PyDict_CheckExact(const_cache));
    9466     2926470 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9467     2461350 :         if (optimize_basic_block(const_cache, b, consts)) {
    9468           0 :             return -1;
    9469             :         }
    9470     2461350 :         clean_basic_block(b);
    9471     2461350 :         assert(b->b_predecessors == 0);
    9472             :     }
    9473     2926470 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9474     2461350 :         if (extend_block(b)) {
    9475           0 :             return -1;
    9476             :         }
    9477             :     }
    9478      465113 :     if (mark_reachable(entryblock)) {
    9479           0 :         return -1;
    9480             :     }
    9481             :     /* Delete unreachable instructions */
    9482     2926470 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9483     2461350 :        if (b->b_predecessors == 0) {
    9484      196018 :             b->b_iused = 0;
    9485             :        }
    9486             :     }
    9487      465113 :     eliminate_empty_basic_blocks(entryblock);
    9488     2723440 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9489     2258330 :         clean_basic_block(b);
    9490             :     }
    9491      465113 :     return 0;
    9492             : }
    9493             : 
    9494             : // Remove trailing unused constants.
    9495             : static int
    9496      465113 : trim_unused_consts(basicblock *entryblock, PyObject *consts)
    9497             : {
    9498      465113 :     assert(PyList_CheckExact(consts));
    9499             : 
    9500             :     // The first constant may be docstring; keep it always.
    9501      465113 :     int max_const_index = 0;
    9502     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9503    19854100 :         for (int i = 0; i < b->b_iused; i++) {
    9504    17612200 :             if ((b->b_instr[i].i_opcode == LOAD_CONST ||
    9505    14277800 :                 b->b_instr[i].i_opcode == KW_NAMES) &&
    9506     3402460 :                     b->b_instr[i].i_oparg > max_const_index) {
    9507     1482750 :                 max_const_index = b->b_instr[i].i_oparg;
    9508             :             }
    9509             :         }
    9510             :     }
    9511      465113 :     if (max_const_index+1 < PyList_GET_SIZE(consts)) {
    9512             :         //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
    9513             :         //        max_const_index, (int)PyList_GET_SIZE(consts));
    9514        6005 :         if (PyList_SetSlice(consts, max_const_index+1,
    9515             :                             PyList_GET_SIZE(consts), NULL) < 0) {
    9516           0 :             return 1;
    9517             :         }
    9518             :     }
    9519      465113 :     return 0;
    9520             : }
    9521             : 
    9522             : static inline int
    9523     2210000 : is_exit_without_lineno(basicblock *b) {
    9524     2210000 :     if (!basicblock_exits_scope(b)) {
    9525     1768510 :         return 0;
    9526             :     }
    9527      670115 :     for (int i = 0; i < b->b_iused; i++) {
    9528      552370 :         if (b->b_instr[i].i_loc.lineno >= 0) {
    9529      323736 :             return 0;
    9530             :         }
    9531             :     }
    9532      117745 :     return 1;
    9533             : }
    9534             : 
    9535             : /* PEP 626 mandates that the f_lineno of a frame is correct
    9536             :  * after a frame terminates. It would be prohibitively expensive
    9537             :  * to continuously update the f_lineno field at runtime,
    9538             :  * so we make sure that all exiting instruction (raises and returns)
    9539             :  * have a valid line number, allowing us to compute f_lineno lazily.
    9540             :  * We can do this by duplicating the exit blocks without line number
    9541             :  * so that none have more than one predecessor. We can then safely
    9542             :  * copy the line number from the sole predecessor block.
    9543             :  */
    9544             : static int
    9545      465113 : duplicate_exits_without_lineno(basicblock *entryblock)
    9546             : {
    9547             :     /* Copy all exit blocks without line number that are targets of a jump.
    9548             :      */
    9549     2739320 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9550     2274210 :         if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
    9551      938095 :             basicblock *target = b->b_instr[b->b_iused-1].i_target;
    9552      938095 :             if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
    9553       15879 :                 basicblock *new_target = copy_basicblock(target);
    9554       15879 :                 if (new_target == NULL) {
    9555           0 :                     return -1;
    9556             :                 }
    9557       15879 :                 new_target->b_instr[0].i_loc = b->b_instr[b->b_iused-1].i_loc;
    9558       15879 :                 b->b_instr[b->b_iused-1].i_target = new_target;
    9559       15879 :                 target->b_predecessors--;
    9560       15879 :                 new_target->b_predecessors = 1;
    9561       15879 :                 new_target->b_next = target->b_next;
    9562       15879 :                 target->b_next = new_target;
    9563             :             }
    9564             :         }
    9565             :     }
    9566             :     /* Eliminate empty blocks */
    9567     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9568     2274210 :         while (b->b_next && b->b_next->b_iused == 0) {
    9569       32287 :             b->b_next = b->b_next->b_next;
    9570             :         }
    9571             :     }
    9572             :     /* Any remaining reachable exit blocks without line number can only be reached by
    9573             :      * fall through, and thus can only have a single predecessor */
    9574     2707040 :     for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
    9575     2241920 :         if (BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_iused > 0) {
    9576     1271900 :             if (is_exit_without_lineno(b->b_next)) {
    9577       37280 :                 assert(b->b_next->b_iused > 0);
    9578       37280 :                 b->b_next->b_instr[0].i_loc = b->b_instr[b->b_iused-1].i_loc;
    9579             :             }
    9580             :         }
    9581             :     }
    9582      465113 :     return 0;
    9583             : }
    9584             : 
    9585             : 
    9586             : /* Retained for API compatibility.
    9587             :  * Optimization is now done in optimize_cfg */
    9588             : 
    9589             : PyObject *
    9590           0 : PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
    9591             :                 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
    9592             : {
    9593           0 :     Py_INCREF(code);
    9594           0 :     return code;
    9595             : }

Generated by: LCOV version 1.14