Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
| 3 | * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 4 | * The primary entry point is _PyAST_Compile(), which returns a |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5 | * PyCodeObject. The compiler makes several passes to build the code |
| 6 | * object: |
| 7 | * 1. Checks for future statements. See future.c |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | * 2. Builds a symbol table. See symtable.c. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | * 3. Generate code for basic blocks. See compiler_mod() in this file. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | * 4. Assemble the basic blocks into final code. See assemble() in |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | * this file. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 12 | * 5. Optimize the byte code (peephole optimizations). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | * |
| 14 | * Note that compiler_mod() suggests module, but the module ast type |
| 15 | * (mod_ty) has cases for expressions and interactive statements. |
Nick Coghlan | 944d3eb | 2005-11-16 12:46:55 +0000 | [diff] [blame] | 16 | * |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | * structure takes care of releasing those. Use the arena to manage |
| 21 | * objects. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 24 | #include "Python.h" |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 25 | #include "pycore_ast.h" // _PyAST_GetDocString() |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 26 | #include "pycore_compile.h" // _PyFuture_FromAST() |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 27 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 28 | #include "pycore_long.h" // _PyLong_GetZero() |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 29 | #include "pycore_symtable.h" // PySTEntryObject |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 31 | #define NEED_OPCODE_JUMP_TABLES |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 32 | #include "opcode.h" // EXTENDED_ARG |
| 33 | #include "wordcode_helpers.h" // instrsize() |
| 34 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 35 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 36 | #define DEFAULT_BLOCK_SIZE 16 |
| 37 | #define DEFAULT_BLOCKS 8 |
| 38 | #define DEFAULT_CODE_SIZE 128 |
| 39 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 40 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 41 | #define COMP_GENEXP 0 |
| 42 | #define COMP_LISTCOMP 1 |
| 43 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 44 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 45 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 46 | /* A soft limit for stack use, to avoid excessive |
| 47 | * memory use for large constants, etc. |
| 48 | * |
| 49 | * The value 30 is plucked out of thin air. |
| 50 | * Code that could use more stack than this is |
| 51 | * rare, so the exact value is unimportant. |
| 52 | */ |
| 53 | #define STACK_USE_GUIDELINE 30 |
| 54 | |
| 55 | /* If we exceed this limit, it should |
| 56 | * be considered a compiler bug. |
| 57 | * Currently it should be impossible |
| 58 | * to exceed STACK_USE_GUIDELINE * 100, |
| 59 | * as 100 is the maximum parse depth. |
| 60 | * For performance reasons we will |
| 61 | * want to reduce this to a |
| 62 | * few hundred in the future. |
| 63 | * |
| 64 | * NOTE: Whatever MAX_ALLOWED_STACK_USE is |
| 65 | * set to, it should never restrict what Python |
| 66 | * we can write, just how we compile it. |
| 67 | */ |
| 68 | #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100) |
| 69 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 70 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 71 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 72 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 73 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 74 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | unsigned char i_opcode; |
| 76 | int i_oparg; |
| 77 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 78 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 81 | #define LOG_BITS_PER_INT 5 |
| 82 | #define MASK_LOW_LOG_BITS 31 |
| 83 | |
| 84 | static inline int |
| 85 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 86 | /* Is the relevant bit set in the relevant word? */ |
| 87 | /* 256 bits fit into 8 32-bits words. |
| 88 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 89 | * Bit within word is the low bits of bitindex. |
| 90 | */ |
| 91 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 92 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 93 | } |
| 94 | |
| 95 | static inline int |
| 96 | is_relative_jump(struct instr *i) |
| 97 | { |
| 98 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 99 | } |
| 100 | |
| 101 | static inline int |
| 102 | is_jump(struct instr *i) |
| 103 | { |
| 104 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 105 | } |
| 106 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 107 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 109 | reverse order that the block are allocated. b_list points to the next |
| 110 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | struct basicblock_ *b_list; |
| 112 | /* number of instructions used */ |
| 113 | int b_iused; |
| 114 | /* length of instruction array (b_instr) */ |
| 115 | int b_ialloc; |
| 116 | /* pointer to an array of instructions, initially NULL */ |
| 117 | struct instr *b_instr; |
| 118 | /* If b_next is non-NULL, it is a pointer to the next |
| 119 | block reached by normal control flow. */ |
| 120 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 122 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 123 | /* Number of predecssors that a block has. */ |
| 124 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 125 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 126 | unsigned b_nofallthrough : 1; |
| 127 | /* Basic block exits scope (it ends with a return or raise) */ |
| 128 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 130 | int b_startdepth; |
| 131 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 132 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 133 | } basicblock; |
| 134 | |
| 135 | /* fblockinfo tracks the current frame block. |
| 136 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 137 | A frame block is used to handle loops, try/except, and try/finally. |
| 138 | It's called a frame block to distinguish it from a basic block in the |
| 139 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 140 | */ |
| 141 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 142 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 143 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER, |
| 144 | ASYNC_COMPREHENSION_GENERATOR }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 145 | |
| 146 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | enum fblocktype fb_type; |
| 148 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 149 | /* (optional) type-specific exit or cleanup block */ |
| 150 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 151 | /* (optional) additional information required for unwinding */ |
| 152 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 155 | enum { |
| 156 | COMPILER_SCOPE_MODULE, |
| 157 | COMPILER_SCOPE_CLASS, |
| 158 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 159 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 160 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 161 | COMPILER_SCOPE_COMPREHENSION, |
| 162 | }; |
| 163 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | /* The following items change on entry and exit of code blocks. |
| 165 | They must be saved and restored when returning to a block. |
| 166 | */ |
| 167 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 171 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 172 | int u_scope_type; |
| 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | /* The following fields are dicts that map objects to |
| 175 | the index of them in co_XXX. The index is used as |
| 176 | the argument for opcodes that refer to those collections. |
| 177 | */ |
| 178 | PyObject *u_consts; /* all constants */ |
| 179 | PyObject *u_names; /* all names */ |
| 180 | PyObject *u_varnames; /* local variables */ |
| 181 | PyObject *u_cellvars; /* cell variables */ |
| 182 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 186 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 187 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 188 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* Pointer to the most recently allocated block. By following b_list |
| 190 | members, you can reach all early allocated blocks. */ |
| 191 | basicblock *u_blocks; |
| 192 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | int u_nfblocks; |
| 195 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | int u_firstlineno; /* the first lineno of the block */ |
| 198 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 199 | int u_col_offset; /* the offset of the current stmt */ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 200 | int u_end_lineno; /* the end line of the current stmt */ |
| 201 | int u_end_col_offset; /* the end offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 205 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 206 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | for enclosing blocks are stored in c_stack. The u and c_stack are |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 208 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 209 | |
| 210 | Note that we don't track recursion levels during compilation - the |
| 211 | task of detecting and rejecting excessive levels of nesting is |
| 212 | handled by the symbol analysis pass. |
| 213 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | */ |
| 215 | |
| 216 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 217 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | struct symtable *c_st; |
| 219 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 220 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 222 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | int c_interactive; /* true if in interactive mode */ |
| 224 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 225 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 226 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | struct compiler_unit *u; /* compiler state for current block */ |
| 228 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 229 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 232 | typedef struct { |
| 233 | PyObject *stores; |
| 234 | int allow_irrefutable; |
| 235 | } pattern_context; |
| 236 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 237 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 238 | static void compiler_free(struct compiler *); |
| 239 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 240 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 241 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 242 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 243 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 244 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 245 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 246 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 247 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 248 | |
| 249 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 250 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 251 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 252 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 253 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 254 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 255 | static int compiler_subscript(struct compiler *, expr_ty); |
| 256 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 257 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 258 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 259 | static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 260 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 261 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 262 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 263 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 264 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 265 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 266 | asdl_expr_seq *args, |
| 267 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 268 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 269 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 270 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 271 | static int compiler_sync_comprehension_generator( |
| 272 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 273 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 274 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 275 | expr_ty elt, expr_ty val, int type); |
| 276 | |
| 277 | static int compiler_async_comprehension_generator( |
| 278 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 279 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 280 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 281 | expr_ty elt, expr_ty val, int type); |
| 282 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 283 | static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 284 | static int compiler_match(struct compiler *, stmt_ty); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 285 | static int compiler_pattern_subpattern(struct compiler *, pattern_ty, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 286 | pattern_context *); |
| 287 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 288 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 289 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 290 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 291 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 292 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 293 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 294 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | /* Name mangling: __private becomes _classname__private. |
| 297 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 298 | PyObject *result; |
| 299 | size_t nlen, plen, ipriv; |
| 300 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 302 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 303 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | Py_INCREF(ident); |
| 305 | return ident; |
| 306 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 307 | nlen = PyUnicode_GET_LENGTH(ident); |
| 308 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | The only time a name with a dot can occur is when |
| 312 | we are compiling an import statement that has a |
| 313 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | TODO(jhylton): Decide whether we want to support |
| 316 | mangling of the module name, e.g. __M.X. |
| 317 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 318 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 319 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 320 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | Py_INCREF(ident); |
| 322 | return ident; /* Don't mangle __whatever__ */ |
| 323 | } |
| 324 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 325 | ipriv = 0; |
| 326 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 327 | ipriv++; |
| 328 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | Py_INCREF(ident); |
| 330 | return ident; /* Don't mangle if class is just underscores */ |
| 331 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 332 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 334 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 335 | PyErr_SetString(PyExc_OverflowError, |
| 336 | "private identifier too large to be mangled"); |
| 337 | return NULL; |
| 338 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 340 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 341 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 342 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 343 | |
| 344 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 345 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 347 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 348 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 349 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 350 | Py_DECREF(result); |
| 351 | return NULL; |
| 352 | } |
| 353 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 354 | Py_DECREF(result); |
| 355 | return NULL; |
| 356 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 357 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 358 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | static int |
| 362 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 365 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 366 | c->c_const_cache = PyDict_New(); |
| 367 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | c->c_stack = PyList_New(0); |
| 372 | if (!c->c_stack) { |
| 373 | Py_CLEAR(c->c_const_cache); |
| 374 | return 0; |
| 375 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | PyCodeObject * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 381 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 382 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | struct compiler c; |
| 385 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 386 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (!__doc__) { |
| 390 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 391 | if (!__doc__) |
| 392 | return NULL; |
| 393 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 394 | if (!__annotations__) { |
| 395 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 396 | if (!__annotations__) |
| 397 | return NULL; |
| 398 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | if (!compiler_init(&c)) |
| 400 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 401 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | c.c_filename = filename; |
| 403 | c.c_arena = arena; |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 404 | c.c_future = _PyFuture_FromAST(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | if (c.c_future == NULL) |
| 406 | goto finally; |
| 407 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | flags = &local_flags; |
| 409 | } |
| 410 | merged = c.c_future->ff_features | flags->cf_flags; |
| 411 | c.c_future->ff_features = merged; |
| 412 | flags->cf_flags = merged; |
| 413 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 414 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 416 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 417 | _PyASTOptimizeState state; |
| 418 | state.optimize = c.c_optimize; |
| 419 | state.ff_features = merged; |
| 420 | |
| 421 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 422 | goto finally; |
| 423 | } |
| 424 | |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 425 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (c.c_st == NULL) { |
| 427 | if (!PyErr_Occurred()) |
| 428 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 429 | goto finally; |
| 430 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 433 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 434 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | compiler_free(&c); |
| 436 | assert(co || PyErr_Occurred()); |
| 437 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 440 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 441 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | if (c->c_st) |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 444 | _PySymtable_Free(c->c_st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | if (c->c_future) |
| 446 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 447 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 448 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 452 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 453 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | Py_ssize_t i, n; |
| 456 | PyObject *v, *k; |
| 457 | PyObject *dict = PyDict_New(); |
| 458 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | n = PyList_Size(list); |
| 461 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 462 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | if (!v) { |
| 464 | Py_DECREF(dict); |
| 465 | return NULL; |
| 466 | } |
| 467 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 468 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | Py_DECREF(v); |
| 470 | Py_DECREF(dict); |
| 471 | return NULL; |
| 472 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | Py_DECREF(v); |
| 474 | } |
| 475 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* Return new dict containing names from src that match scope(s). |
| 479 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 480 | src is a symbol table dictionary. If the scope of a name matches |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | either scope_type or flag is set, insert it into the new dict. The |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 482 | values are integers, starting at offset and increasing by one for |
| 483 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 484 | */ |
| 485 | |
| 486 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 487 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 488 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 489 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 491 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | assert(offset >= 0); |
| 494 | if (dest == NULL) |
| 495 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 496 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 497 | /* Sort the keys so that we have a deterministic order on the indexes |
| 498 | saved in the returned dictionary. These indexes are used as indexes |
| 499 | into the free and cell var storage. Therefore if they aren't |
| 500 | deterministic, then the generated bytecode is not deterministic. |
| 501 | */ |
| 502 | sorted_keys = PyDict_Keys(src); |
| 503 | if (sorted_keys == NULL) |
| 504 | return NULL; |
| 505 | if (PyList_Sort(sorted_keys) != 0) { |
| 506 | Py_DECREF(sorted_keys); |
| 507 | return NULL; |
| 508 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 509 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 510 | |
| 511 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | /* XXX this should probably be a macro in symtable.h */ |
| 513 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 514 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 515 | v = PyDict_GetItemWithError(src, k); |
| 516 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | vi = PyLong_AS_LONG(v); |
| 518 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 521 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 523 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | Py_DECREF(dest); |
| 525 | return NULL; |
| 526 | } |
| 527 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 528 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 529 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | Py_DECREF(item); |
| 531 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | return NULL; |
| 533 | } |
| 534 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 537 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 541 | static void |
| 542 | compiler_unit_check(struct compiler_unit *u) |
| 543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | basicblock *block; |
| 545 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 546 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | if (block->b_instr != NULL) { |
| 548 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 549 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | assert(block->b_ialloc >= block->b_iused); |
| 551 | } |
| 552 | else { |
| 553 | assert (block->b_iused == 0); |
| 554 | assert (block->b_ialloc == 0); |
| 555 | } |
| 556 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static void |
| 560 | compiler_unit_free(struct compiler_unit *u) |
| 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | compiler_unit_check(u); |
| 565 | b = u->u_blocks; |
| 566 | while (b != NULL) { |
| 567 | if (b->b_instr) |
| 568 | PyObject_Free((void *)b->b_instr); |
| 569 | next = b->b_list; |
| 570 | PyObject_Free((void *)b); |
| 571 | b = next; |
| 572 | } |
| 573 | Py_CLEAR(u->u_ste); |
| 574 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 575 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | Py_CLEAR(u->u_consts); |
| 577 | Py_CLEAR(u->u_names); |
| 578 | Py_CLEAR(u->u_varnames); |
| 579 | Py_CLEAR(u->u_freevars); |
| 580 | Py_CLEAR(u->u_cellvars); |
| 581 | Py_CLEAR(u->u_private); |
| 582 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 586 | compiler_enter_scope(struct compiler *c, identifier name, |
| 587 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 590 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 591 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 592 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | struct compiler_unit)); |
| 594 | if (!u) { |
| 595 | PyErr_NoMemory(); |
| 596 | return 0; |
| 597 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 598 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 600 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | u->u_kwonlyargcount = 0; |
| 602 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 603 | if (!u->u_ste) { |
| 604 | compiler_unit_free(u); |
| 605 | return 0; |
| 606 | } |
| 607 | Py_INCREF(name); |
| 608 | u->u_name = name; |
| 609 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 610 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 611 | if (!u->u_varnames || !u->u_cellvars) { |
| 612 | compiler_unit_free(u); |
| 613 | return 0; |
| 614 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 615 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 616 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 617 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 618 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 619 | int res; |
| 620 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 621 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 622 | name = _PyUnicode_FromId(&PyId___class__); |
| 623 | if (!name) { |
| 624 | compiler_unit_free(u); |
| 625 | return 0; |
| 626 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 627 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 628 | if (res < 0) { |
| 629 | compiler_unit_free(u); |
| 630 | return 0; |
| 631 | } |
| 632 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 635 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (!u->u_freevars) { |
| 637 | compiler_unit_free(u); |
| 638 | return 0; |
| 639 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | u->u_blocks = NULL; |
| 642 | u->u_nfblocks = 0; |
| 643 | u->u_firstlineno = lineno; |
| 644 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 645 | u->u_col_offset = 0; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 646 | u->u_end_lineno = 0; |
| 647 | u->u_end_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | u->u_consts = PyDict_New(); |
| 649 | if (!u->u_consts) { |
| 650 | compiler_unit_free(u); |
| 651 | return 0; |
| 652 | } |
| 653 | u->u_names = PyDict_New(); |
| 654 | if (!u->u_names) { |
| 655 | compiler_unit_free(u); |
| 656 | return 0; |
| 657 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | /* Push the old compiler_unit on the stack. */ |
| 662 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 663 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 665 | Py_XDECREF(capsule); |
| 666 | compiler_unit_free(u); |
| 667 | return 0; |
| 668 | } |
| 669 | Py_DECREF(capsule); |
| 670 | u->u_private = c->u->u_private; |
| 671 | Py_XINCREF(u->u_private); |
| 672 | } |
| 673 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 676 | |
| 677 | block = compiler_new_block(c); |
| 678 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 680 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 681 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 682 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 683 | if (!compiler_set_qualname(c)) |
| 684 | return 0; |
| 685 | } |
| 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 690 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 691 | compiler_exit_scope(struct compiler *c) |
| 692 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 693 | // Don't call PySequence_DelItem() with an exception raised |
| 694 | PyObject *exc_type, *exc_val, *exc_tb; |
| 695 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | c->c_nestlevel--; |
| 698 | compiler_unit_free(c->u); |
| 699 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 700 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 702 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 703 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | assert(c->u); |
| 705 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 706 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 707 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 708 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 709 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | compiler_unit_check(c->u); |
| 711 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 712 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 714 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 715 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 716 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 719 | static int |
| 720 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 721 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 722 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 723 | _Py_static_string(dot_locals, ".<locals>"); |
| 724 | Py_ssize_t stack_size; |
| 725 | struct compiler_unit *u = c->u; |
| 726 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 727 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 728 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 729 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 730 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 731 | if (stack_size > 1) { |
| 732 | int scope, force_global = 0; |
| 733 | struct compiler_unit *parent; |
| 734 | PyObject *mangled, *capsule; |
| 735 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 736 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 737 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 738 | assert(parent); |
| 739 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 740 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 741 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 742 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 743 | assert(u->u_name); |
| 744 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 745 | if (!mangled) |
| 746 | return 0; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 747 | scope = _PyST_GetScope(parent->u_ste, mangled); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 748 | Py_DECREF(mangled); |
| 749 | assert(scope != GLOBAL_IMPLICIT); |
| 750 | if (scope == GLOBAL_EXPLICIT) |
| 751 | force_global = 1; |
| 752 | } |
| 753 | |
| 754 | if (!force_global) { |
| 755 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 756 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 757 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 758 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 759 | if (dot_locals_str == NULL) |
| 760 | return 0; |
| 761 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 762 | if (base == NULL) |
| 763 | return 0; |
| 764 | } |
| 765 | else { |
| 766 | Py_INCREF(parent->u_qualname); |
| 767 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 768 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 769 | } |
| 770 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 771 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 772 | if (base != NULL) { |
| 773 | dot_str = _PyUnicode_FromId(&dot); |
| 774 | if (dot_str == NULL) { |
| 775 | Py_DECREF(base); |
| 776 | return 0; |
| 777 | } |
| 778 | name = PyUnicode_Concat(base, dot_str); |
| 779 | Py_DECREF(base); |
| 780 | if (name == NULL) |
| 781 | return 0; |
| 782 | PyUnicode_Append(&name, u->u_name); |
| 783 | if (name == NULL) |
| 784 | return 0; |
| 785 | } |
| 786 | else { |
| 787 | Py_INCREF(u->u_name); |
| 788 | name = u->u_name; |
| 789 | } |
| 790 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 791 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 792 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 795 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | /* Allocate a new block and return a pointer to it. |
| 797 | Returns NULL on error. |
| 798 | */ |
| 799 | |
| 800 | static basicblock * |
| 801 | compiler_new_block(struct compiler *c) |
| 802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | basicblock *b; |
| 804 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 807 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | if (b == NULL) { |
| 809 | PyErr_NoMemory(); |
| 810 | return NULL; |
| 811 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | /* Extend the singly linked list of blocks with new block. */ |
| 813 | b->b_list = u->u_blocks; |
| 814 | u->u_blocks = b; |
| 815 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 818 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | compiler_next_block(struct compiler *c) |
| 820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | basicblock *block = compiler_new_block(c); |
| 822 | if (block == NULL) |
| 823 | return NULL; |
| 824 | c->u->u_curblock->b_next = block; |
| 825 | c->u->u_curblock = block; |
| 826 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static basicblock * |
| 830 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | assert(block != NULL); |
| 833 | c->u->u_curblock->b_next = block; |
| 834 | c->u->u_curblock = block; |
| 835 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 838 | static basicblock * |
| 839 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 840 | { |
| 841 | /* Cannot copy a block if it has a fallthrough, since |
| 842 | * a block can only have one fallthrough predecessor. |
| 843 | */ |
| 844 | assert(block->b_nofallthrough); |
| 845 | basicblock *result = compiler_next_block(c); |
| 846 | if (result == NULL) { |
| 847 | return NULL; |
| 848 | } |
| 849 | for (int i = 0; i < block->b_iused; i++) { |
| 850 | int n = compiler_next_instr(result); |
| 851 | if (n < 0) { |
| 852 | return NULL; |
| 853 | } |
| 854 | result->b_instr[n] = block->b_instr[i]; |
| 855 | } |
| 856 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 857 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 858 | return result; |
| 859 | } |
| 860 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | /* Returns the offset of the next instruction in the current block's |
| 862 | b_instr array. Resizes the b_instr as necessary. |
| 863 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 864 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | |
| 866 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 867 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | assert(b != NULL); |
| 870 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 871 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 872 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | if (b->b_instr == NULL) { |
| 874 | PyErr_NoMemory(); |
| 875 | return -1; |
| 876 | } |
| 877 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | } |
| 879 | else if (b->b_iused == b->b_ialloc) { |
| 880 | struct instr *tmp; |
| 881 | size_t oldsize, newsize; |
| 882 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 883 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 884 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 885 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | PyErr_NoMemory(); |
| 887 | return -1; |
| 888 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | if (newsize == 0) { |
| 891 | PyErr_NoMemory(); |
| 892 | return -1; |
| 893 | } |
| 894 | b->b_ialloc <<= 1; |
| 895 | tmp = (struct instr *)PyObject_Realloc( |
| 896 | (void *)b->b_instr, newsize); |
| 897 | if (tmp == NULL) { |
| 898 | PyErr_NoMemory(); |
| 899 | return -1; |
| 900 | } |
| 901 | b->b_instr = tmp; |
| 902 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 903 | } |
| 904 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 907 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 908 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 909 | The line number is reset in the following cases: |
| 910 | - when entering a new scope |
| 911 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 912 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 913 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 914 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 915 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 916 | #define SET_LOC(c, x) \ |
| 917 | (c)->u->u_lineno = (x)->lineno; \ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 918 | (c)->u->u_col_offset = (x)->col_offset; \ |
| 919 | (c)->u->u_end_lineno = (x)->end_lineno; \ |
| 920 | (c)->u->u_end_col_offset = (x)->end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 921 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 922 | /* Return the stack effect of opcode with argument oparg. |
| 923 | |
| 924 | Some opcodes have different stack effect when jump to the target and |
| 925 | when not jump. The 'jump' parameter specifies the case: |
| 926 | |
| 927 | * 0 -- when not jump |
| 928 | * 1 -- when jump |
| 929 | * -1 -- maximal |
| 930 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 931 | static int |
| 932 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 935 | case NOP: |
| 936 | case EXTENDED_ARG: |
| 937 | return 0; |
| 938 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 939 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | case POP_TOP: |
| 941 | return -1; |
| 942 | case ROT_TWO: |
| 943 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 944 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | return 0; |
| 946 | case DUP_TOP: |
| 947 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 948 | case DUP_TOP_TWO: |
| 949 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 950 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 951 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case UNARY_POSITIVE: |
| 953 | case UNARY_NEGATIVE: |
| 954 | case UNARY_NOT: |
| 955 | case UNARY_INVERT: |
| 956 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | case SET_ADD: |
| 959 | case LIST_APPEND: |
| 960 | return -1; |
| 961 | case MAP_ADD: |
| 962 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 963 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 964 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | case BINARY_POWER: |
| 966 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 967 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | case BINARY_MODULO: |
| 969 | case BINARY_ADD: |
| 970 | case BINARY_SUBTRACT: |
| 971 | case BINARY_SUBSCR: |
| 972 | case BINARY_FLOOR_DIVIDE: |
| 973 | case BINARY_TRUE_DIVIDE: |
| 974 | return -1; |
| 975 | case INPLACE_FLOOR_DIVIDE: |
| 976 | case INPLACE_TRUE_DIVIDE: |
| 977 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | case INPLACE_ADD: |
| 980 | case INPLACE_SUBTRACT: |
| 981 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 982 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | case INPLACE_MODULO: |
| 984 | return -1; |
| 985 | case STORE_SUBSCR: |
| 986 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | case DELETE_SUBSCR: |
| 988 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | case BINARY_LSHIFT: |
| 991 | case BINARY_RSHIFT: |
| 992 | case BINARY_AND: |
| 993 | case BINARY_XOR: |
| 994 | case BINARY_OR: |
| 995 | return -1; |
| 996 | case INPLACE_POWER: |
| 997 | return -1; |
| 998 | case GET_ITER: |
| 999 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case PRINT_EXPR: |
| 1002 | return -1; |
| 1003 | case LOAD_BUILD_CLASS: |
| 1004 | return 1; |
| 1005 | case INPLACE_LSHIFT: |
| 1006 | case INPLACE_RSHIFT: |
| 1007 | case INPLACE_AND: |
| 1008 | case INPLACE_XOR: |
| 1009 | case INPLACE_OR: |
| 1010 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1013 | /* 1 in the normal flow. |
| 1014 | * Restore the stack position and push 6 values before jumping to |
| 1015 | * the handler if an exception be raised. */ |
| 1016 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | case RETURN_VALUE: |
| 1018 | return -1; |
| 1019 | case IMPORT_STAR: |
| 1020 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1021 | case SETUP_ANNOTATIONS: |
| 1022 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | case YIELD_VALUE: |
| 1024 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1025 | case YIELD_FROM: |
| 1026 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case POP_BLOCK: |
| 1028 | return 0; |
| 1029 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1030 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | case STORE_NAME: |
| 1033 | return -1; |
| 1034 | case DELETE_NAME: |
| 1035 | return 0; |
| 1036 | case UNPACK_SEQUENCE: |
| 1037 | return oparg-1; |
| 1038 | case UNPACK_EX: |
| 1039 | return (oparg&0xFF) + (oparg>>8); |
| 1040 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1041 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1042 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | case STORE_ATTR: |
| 1045 | return -2; |
| 1046 | case DELETE_ATTR: |
| 1047 | return -1; |
| 1048 | case STORE_GLOBAL: |
| 1049 | return -1; |
| 1050 | case DELETE_GLOBAL: |
| 1051 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | case LOAD_CONST: |
| 1053 | return 1; |
| 1054 | case LOAD_NAME: |
| 1055 | return 1; |
| 1056 | case BUILD_TUPLE: |
| 1057 | case BUILD_LIST: |
| 1058 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1059 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | return 1-oparg; |
| 1061 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1062 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1063 | case BUILD_CONST_KEY_MAP: |
| 1064 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | case LOAD_ATTR: |
| 1066 | return 0; |
| 1067 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1068 | case IS_OP: |
| 1069 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1071 | case JUMP_IF_NOT_EXC_MATCH: |
| 1072 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | case IMPORT_NAME: |
| 1074 | return -1; |
| 1075 | case IMPORT_FROM: |
| 1076 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1077 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1078 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | case JUMP_ABSOLUTE: |
| 1081 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1082 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1083 | case JUMP_IF_TRUE_OR_POP: |
| 1084 | case JUMP_IF_FALSE_OR_POP: |
| 1085 | return jump ? 0 : -1; |
| 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | case POP_JUMP_IF_FALSE: |
| 1088 | case POP_JUMP_IF_TRUE: |
| 1089 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | case LOAD_GLOBAL: |
| 1092 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1093 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1094 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1096 | /* 0 in the normal flow. |
| 1097 | * Restore the stack position and push 6 values before jumping to |
| 1098 | * the handler if an exception be raised. */ |
| 1099 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1100 | case RERAISE: |
| 1101 | return -3; |
| 1102 | |
| 1103 | case WITH_EXCEPT_START: |
| 1104 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | case LOAD_FAST: |
| 1107 | return 1; |
| 1108 | case STORE_FAST: |
| 1109 | return -1; |
| 1110 | case DELETE_FAST: |
| 1111 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | case RAISE_VARARGS: |
| 1114 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1115 | |
| 1116 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1118 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1119 | case CALL_METHOD: |
| 1120 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1122 | return -oparg-1; |
| 1123 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1124 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1125 | case MAKE_FUNCTION: |
| 1126 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1127 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | case BUILD_SLICE: |
| 1129 | if (oparg == 3) |
| 1130 | return -2; |
| 1131 | else |
| 1132 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1133 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1134 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | case LOAD_CLOSURE: |
| 1136 | return 1; |
| 1137 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1138 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | return 1; |
| 1140 | case STORE_DEREF: |
| 1141 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1142 | case DELETE_DEREF: |
| 1143 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1144 | |
| 1145 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1146 | case GET_AWAITABLE: |
| 1147 | return 0; |
| 1148 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1149 | /* 0 in the normal flow. |
| 1150 | * Restore the stack position to the position before the result |
| 1151 | * of __aenter__ and push 6 values before jumping to the handler |
| 1152 | * if an exception be raised. */ |
| 1153 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1154 | case BEFORE_ASYNC_WITH: |
| 1155 | return 1; |
| 1156 | case GET_AITER: |
| 1157 | return 0; |
| 1158 | case GET_ANEXT: |
| 1159 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1160 | case GET_YIELD_FROM_ITER: |
| 1161 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1162 | case END_ASYNC_FOR: |
| 1163 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1164 | case FORMAT_VALUE: |
| 1165 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1166 | else 1->1. */ |
| 1167 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1168 | case LOAD_METHOD: |
| 1169 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1170 | case LOAD_ASSERTION_ERROR: |
| 1171 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1172 | case LIST_TO_TUPLE: |
| 1173 | return 0; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 1174 | case GEN_START: |
| 1175 | return -1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1176 | case LIST_EXTEND: |
| 1177 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1178 | case DICT_MERGE: |
| 1179 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1180 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1181 | case COPY_DICT_WITHOUT_KEYS: |
| 1182 | return 0; |
| 1183 | case MATCH_CLASS: |
| 1184 | return -1; |
| 1185 | case GET_LEN: |
| 1186 | case MATCH_MAPPING: |
| 1187 | case MATCH_SEQUENCE: |
| 1188 | return 1; |
| 1189 | case MATCH_KEYS: |
| 1190 | return 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1192 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1194 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1197 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1198 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1199 | { |
| 1200 | return stack_effect(opcode, oparg, jump); |
| 1201 | } |
| 1202 | |
| 1203 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1204 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1205 | { |
| 1206 | return stack_effect(opcode, oparg, -1); |
| 1207 | } |
| 1208 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1209 | /* Add an opcode with no argument. |
| 1210 | Returns 0 on failure, 1 on success. |
| 1211 | */ |
| 1212 | |
| 1213 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1214 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | basicblock *b; |
| 1217 | struct instr *i; |
| 1218 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1219 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1220 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | if (off < 0) |
| 1222 | return 0; |
| 1223 | b = c->u->u_curblock; |
| 1224 | i = &b->b_instr[off]; |
| 1225 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1226 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | if (opcode == RETURN_VALUE) |
| 1228 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1229 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1233 | static int |
| 1234 | compiler_addop(struct compiler *c, int opcode) |
| 1235 | { |
| 1236 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1237 | } |
| 1238 | |
| 1239 | static int |
| 1240 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1241 | { |
| 1242 | return compiler_addop_line(c, opcode, -1); |
| 1243 | } |
| 1244 | |
| 1245 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1246 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1247 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1249 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1251 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1252 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1254 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1256 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1257 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1258 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | return -1; |
| 1261 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1262 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | Py_DECREF(v); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | Py_DECREF(v); |
| 1267 | } |
| 1268 | else |
| 1269 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1270 | return arg; |
| 1271 | } |
| 1272 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1273 | // Merge const *o* recursively and return constant key object. |
| 1274 | static PyObject* |
| 1275 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1276 | { |
| 1277 | // None and Ellipsis are singleton, and key is the singleton. |
| 1278 | // No need to merge object and key. |
| 1279 | if (o == Py_None || o == Py_Ellipsis) { |
| 1280 | Py_INCREF(o); |
| 1281 | return o; |
| 1282 | } |
| 1283 | |
| 1284 | PyObject *key = _PyCode_ConstantKey(o); |
| 1285 | if (key == NULL) { |
| 1286 | return NULL; |
| 1287 | } |
| 1288 | |
| 1289 | // t is borrowed reference |
| 1290 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1291 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1292 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1293 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1294 | Py_DECREF(key); |
| 1295 | return t; |
| 1296 | } |
| 1297 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1298 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1299 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1300 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1301 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1302 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1303 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1304 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1305 | PyObject *u = merge_consts_recursive(c, item); |
| 1306 | if (u == NULL) { |
| 1307 | Py_DECREF(key); |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | // See _PyCode_ConstantKey() |
| 1312 | PyObject *v; // borrowed |
| 1313 | if (PyTuple_CheckExact(u)) { |
| 1314 | v = PyTuple_GET_ITEM(u, 1); |
| 1315 | } |
| 1316 | else { |
| 1317 | v = u; |
| 1318 | } |
| 1319 | if (v != item) { |
| 1320 | Py_INCREF(v); |
| 1321 | PyTuple_SET_ITEM(o, i, v); |
| 1322 | Py_DECREF(item); |
| 1323 | } |
| 1324 | |
| 1325 | Py_DECREF(u); |
| 1326 | } |
| 1327 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1328 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1329 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1330 | // constant keys. |
| 1331 | // See _PyCode_ConstantKey() for detail. |
| 1332 | assert(PyTuple_CheckExact(key)); |
| 1333 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1334 | |
| 1335 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1336 | if (len == 0) { // empty frozenset should not be re-created. |
| 1337 | return key; |
| 1338 | } |
| 1339 | PyObject *tuple = PyTuple_New(len); |
| 1340 | if (tuple == NULL) { |
| 1341 | Py_DECREF(key); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | Py_ssize_t i = 0, pos = 0; |
| 1345 | PyObject *item; |
| 1346 | Py_hash_t hash; |
| 1347 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1348 | PyObject *k = merge_consts_recursive(c, item); |
| 1349 | if (k == NULL) { |
| 1350 | Py_DECREF(tuple); |
| 1351 | Py_DECREF(key); |
| 1352 | return NULL; |
| 1353 | } |
| 1354 | PyObject *u; |
| 1355 | if (PyTuple_CheckExact(k)) { |
| 1356 | u = PyTuple_GET_ITEM(k, 1); |
| 1357 | Py_INCREF(u); |
| 1358 | Py_DECREF(k); |
| 1359 | } |
| 1360 | else { |
| 1361 | u = k; |
| 1362 | } |
| 1363 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1364 | i++; |
| 1365 | } |
| 1366 | |
| 1367 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1368 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1369 | PyObject *new = PyFrozenSet_New(tuple); |
| 1370 | Py_DECREF(tuple); |
| 1371 | if (new == NULL) { |
| 1372 | Py_DECREF(key); |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1376 | Py_DECREF(o); |
| 1377 | PyTuple_SET_ITEM(key, 1, new); |
| 1378 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1379 | |
| 1380 | return key; |
| 1381 | } |
| 1382 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1383 | static Py_ssize_t |
| 1384 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1385 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1386 | PyObject *key = merge_consts_recursive(c, o); |
| 1387 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1388 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1389 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1390 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1391 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1392 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1397 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1398 | { |
| 1399 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1400 | if (arg < 0) |
| 1401 | return 0; |
| 1402 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1403 | } |
| 1404 | |
| 1405 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1408 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1409 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1411 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1412 | return compiler_addop_i(c, opcode, arg); |
| 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1419 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1420 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1421 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1422 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1423 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1424 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1425 | Py_DECREF(mangled); |
| 1426 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1427 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | return compiler_addop_i(c, opcode, arg); |
| 1429 | } |
| 1430 | |
| 1431 | /* Add an opcode with an integer argument. |
| 1432 | Returns 0 on failure, 1 on success. |
| 1433 | */ |
| 1434 | |
| 1435 | static int |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1436 | compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | struct instr *i; |
| 1439 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1440 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1441 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1442 | it in the C code (like Python/ceval.c). |
| 1443 | |
| 1444 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1445 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1446 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1447 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1448 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1449 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1450 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1451 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | if (off < 0) |
| 1453 | return 0; |
| 1454 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1455 | i->i_opcode = opcode; |
| 1456 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1457 | i->i_lineno = lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1461 | static int |
| 1462 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1463 | { |
| 1464 | return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno); |
| 1465 | } |
| 1466 | |
| 1467 | static int |
| 1468 | compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1469 | { |
| 1470 | return compiler_addop_i_line(c, opcode, oparg, -1); |
| 1471 | } |
| 1472 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1473 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1474 | { |
| 1475 | assert(HAS_ARG(opcode)); |
| 1476 | assert(b != NULL); |
| 1477 | assert(target != NULL); |
| 1478 | |
| 1479 | int off = compiler_next_instr(b); |
| 1480 | struct instr *i = &b->b_instr[off]; |
| 1481 | if (off < 0) { |
| 1482 | return 0; |
| 1483 | } |
| 1484 | i->i_opcode = opcode; |
| 1485 | i->i_target = target; |
| 1486 | i->i_lineno = lineno; |
| 1487 | return 1; |
| 1488 | } |
| 1489 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1491 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1492 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1493 | return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1496 | static int |
| 1497 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1498 | { |
| 1499 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1500 | } |
| 1501 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1502 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1503 | to the new block. |
| 1504 | |
| 1505 | The returns inside this macro make it impossible to decref objects |
| 1506 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1507 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | if (compiler_next_block((C)) == NULL) \ |
| 1510 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | if (!compiler_addop((C), (OP))) \ |
| 1515 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1518 | #define ADDOP_NOLINE(C, OP) { \ |
| 1519 | if (!compiler_addop_noline((C), (OP))) \ |
| 1520 | return 0; \ |
| 1521 | } |
| 1522 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1523 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | if (!compiler_addop((C), (OP))) { \ |
| 1525 | compiler_exit_scope(c); \ |
| 1526 | return 0; \ |
| 1527 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1530 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1531 | if (!compiler_addop_load_const((C), (O))) \ |
| 1532 | return 0; \ |
| 1533 | } |
| 1534 | |
| 1535 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1536 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1537 | PyObject *__new_const = (O); \ |
| 1538 | if (__new_const == NULL) { \ |
| 1539 | return 0; \ |
| 1540 | } \ |
| 1541 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1542 | Py_DECREF(__new_const); \ |
| 1543 | return 0; \ |
| 1544 | } \ |
| 1545 | Py_DECREF(__new_const); \ |
| 1546 | } |
| 1547 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1548 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1550 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1553 | /* Same as ADDOP_O, but steals a reference. */ |
| 1554 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1555 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1556 | Py_DECREF((O)); \ |
| 1557 | return 0; \ |
| 1558 | } \ |
| 1559 | Py_DECREF((O)); \ |
| 1560 | } |
| 1561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1564 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1569 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1572 | #define ADDOP_I_NOLINE(C, OP, O) { \ |
| 1573 | if (!compiler_addop_i_noline((C), (OP), (O))) \ |
| 1574 | return 0; \ |
| 1575 | } |
| 1576 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1577 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1578 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1582 | /* Add a jump with no line number. |
| 1583 | * Used for artificial jumps that have no corresponding |
| 1584 | * token in the source code. */ |
| 1585 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1586 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1587 | return 0; \ |
| 1588 | } |
| 1589 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1590 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1591 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1592 | return 0; \ |
| 1593 | } |
| 1594 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1595 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1596 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1597 | */ |
| 1598 | |
| 1599 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1601 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1604 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1606 | compiler_exit_scope(c); \ |
| 1607 | return 0; \ |
| 1608 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1611 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1613 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1618 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1620 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1621 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1622 | return 0; \ |
| 1623 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1626 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1628 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1630 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1631 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1632 | compiler_exit_scope(c); \ |
| 1633 | return 0; \ |
| 1634 | } \ |
| 1635 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1638 | #define RETURN_IF_FALSE(X) \ |
| 1639 | if (!(X)) { \ |
| 1640 | return 0; \ |
| 1641 | } |
| 1642 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1643 | /* Search if variable annotations are present statically in a block. */ |
| 1644 | |
| 1645 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1646 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1647 | { |
| 1648 | int i, j, res = 0; |
| 1649 | stmt_ty st; |
| 1650 | |
| 1651 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1652 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1653 | switch (st->kind) { |
| 1654 | case AnnAssign_kind: |
| 1655 | return 1; |
| 1656 | case For_kind: |
| 1657 | res = find_ann(st->v.For.body) || |
| 1658 | find_ann(st->v.For.orelse); |
| 1659 | break; |
| 1660 | case AsyncFor_kind: |
| 1661 | res = find_ann(st->v.AsyncFor.body) || |
| 1662 | find_ann(st->v.AsyncFor.orelse); |
| 1663 | break; |
| 1664 | case While_kind: |
| 1665 | res = find_ann(st->v.While.body) || |
| 1666 | find_ann(st->v.While.orelse); |
| 1667 | break; |
| 1668 | case If_kind: |
| 1669 | res = find_ann(st->v.If.body) || |
| 1670 | find_ann(st->v.If.orelse); |
| 1671 | break; |
| 1672 | case With_kind: |
| 1673 | res = find_ann(st->v.With.body); |
| 1674 | break; |
| 1675 | case AsyncWith_kind: |
| 1676 | res = find_ann(st->v.AsyncWith.body); |
| 1677 | break; |
| 1678 | case Try_kind: |
| 1679 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1680 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1681 | st->v.Try.handlers, j); |
| 1682 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1683 | return 1; |
| 1684 | } |
| 1685 | } |
| 1686 | res = find_ann(st->v.Try.body) || |
| 1687 | find_ann(st->v.Try.finalbody) || |
| 1688 | find_ann(st->v.Try.orelse); |
| 1689 | break; |
| 1690 | default: |
| 1691 | res = 0; |
| 1692 | } |
| 1693 | if (res) { |
| 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | return res; |
| 1698 | } |
| 1699 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1700 | /* |
| 1701 | * Frame block handling functions |
| 1702 | */ |
| 1703 | |
| 1704 | static int |
| 1705 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1706 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1707 | { |
| 1708 | struct fblockinfo *f; |
| 1709 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1710 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1711 | } |
| 1712 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1713 | f->fb_type = t; |
| 1714 | f->fb_block = b; |
| 1715 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1716 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1717 | return 1; |
| 1718 | } |
| 1719 | |
| 1720 | static void |
| 1721 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1722 | { |
| 1723 | struct compiler_unit *u = c->u; |
| 1724 | assert(u->u_nfblocks > 0); |
| 1725 | u->u_nfblocks--; |
| 1726 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1727 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1728 | } |
| 1729 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1730 | static int |
| 1731 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1732 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1733 | ADDOP(c, DUP_TOP); |
| 1734 | ADDOP(c, DUP_TOP); |
| 1735 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1736 | return 1; |
| 1737 | } |
| 1738 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1739 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1740 | * popping the blocks will be restored afterwards, unless another |
| 1741 | * return, break or continue is found. In which case, the TOS will |
| 1742 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1743 | */ |
| 1744 | static int |
| 1745 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1746 | int preserve_tos) |
| 1747 | { |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 1748 | int loc; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1749 | switch (info->fb_type) { |
| 1750 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1751 | case EXCEPTION_HANDLER: |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 1752 | case ASYNC_COMPREHENSION_GENERATOR: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1753 | return 1; |
| 1754 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1755 | case FOR_LOOP: |
| 1756 | /* Pop the iterator */ |
| 1757 | if (preserve_tos) { |
| 1758 | ADDOP(c, ROT_TWO); |
| 1759 | } |
| 1760 | ADDOP(c, POP_TOP); |
| 1761 | return 1; |
| 1762 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1763 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1764 | ADDOP(c, POP_BLOCK); |
| 1765 | return 1; |
| 1766 | |
| 1767 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1768 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1769 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1770 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1771 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1772 | return 0; |
| 1773 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1774 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1775 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1776 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1777 | if (preserve_tos) { |
| 1778 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1779 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1780 | /* The finally block should appear to execute after the |
| 1781 | * statement causing the unwinding, so make the unwinding |
| 1782 | * instruction artificial */ |
| 1783 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1784 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1785 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1786 | case FINALLY_END: |
| 1787 | if (preserve_tos) { |
| 1788 | ADDOP(c, ROT_FOUR); |
| 1789 | } |
| 1790 | ADDOP(c, POP_TOP); |
| 1791 | ADDOP(c, POP_TOP); |
| 1792 | ADDOP(c, POP_TOP); |
| 1793 | if (preserve_tos) { |
| 1794 | ADDOP(c, ROT_FOUR); |
| 1795 | } |
| 1796 | ADDOP(c, POP_EXCEPT); |
| 1797 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1798 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1799 | case WITH: |
| 1800 | case ASYNC_WITH: |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 1801 | loc = c->u->u_lineno; |
| 1802 | SET_LOC(c, (stmt_ty)info->fb_datum); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1803 | ADDOP(c, POP_BLOCK); |
| 1804 | if (preserve_tos) { |
| 1805 | ADDOP(c, ROT_TWO); |
| 1806 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1807 | if(!compiler_call_exit_with_nones(c)) { |
| 1808 | return 0; |
| 1809 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1810 | if (info->fb_type == ASYNC_WITH) { |
| 1811 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1812 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1813 | ADDOP(c, YIELD_FROM); |
| 1814 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1815 | ADDOP(c, POP_TOP); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 1816 | c->u->u_lineno = loc; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1817 | return 1; |
| 1818 | |
| 1819 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1820 | if (info->fb_datum) { |
| 1821 | ADDOP(c, POP_BLOCK); |
| 1822 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1823 | if (preserve_tos) { |
| 1824 | ADDOP(c, ROT_FOUR); |
| 1825 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1826 | ADDOP(c, POP_EXCEPT); |
| 1827 | if (info->fb_datum) { |
| 1828 | ADDOP_LOAD_CONST(c, Py_None); |
| 1829 | compiler_nameop(c, info->fb_datum, Store); |
| 1830 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1831 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1832 | return 1; |
| 1833 | |
| 1834 | case POP_VALUE: |
| 1835 | if (preserve_tos) { |
| 1836 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1837 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1838 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1839 | return 1; |
| 1840 | } |
| 1841 | Py_UNREACHABLE(); |
| 1842 | } |
| 1843 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1844 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1845 | static int |
| 1846 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1847 | if (c->u->u_nfblocks == 0) { |
| 1848 | return 1; |
| 1849 | } |
| 1850 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1851 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1852 | *loop = top; |
| 1853 | return 1; |
| 1854 | } |
| 1855 | struct fblockinfo copy = *top; |
| 1856 | c->u->u_nfblocks--; |
| 1857 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1858 | return 0; |
| 1859 | } |
| 1860 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1861 | return 0; |
| 1862 | } |
| 1863 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1864 | c->u->u_nfblocks++; |
| 1865 | return 1; |
| 1866 | } |
| 1867 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1868 | /* Compile a sequence of statements, checking for a docstring |
| 1869 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1870 | |
| 1871 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1872 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1873 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1874 | int i = 0; |
| 1875 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1876 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1877 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1878 | /* Set current line number to the line number of first statement. |
| 1879 | This way line number for SETUP_ANNOTATIONS will always |
| 1880 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1881 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1882 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1883 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1884 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1885 | } |
| 1886 | /* Every annotated class and module should have __annotations__. */ |
| 1887 | if (find_ann(stmts)) { |
| 1888 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1889 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1890 | if (!asdl_seq_LEN(stmts)) |
| 1891 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1892 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1893 | if (c->c_optimize < 2) { |
| 1894 | docstring = _PyAST_GetDocString(stmts); |
| 1895 | if (docstring) { |
| 1896 | i = 1; |
| 1897 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1898 | assert(st->kind == Expr_kind); |
| 1899 | VISIT(c, expr, st->v.Expr.value); |
| 1900 | if (!compiler_nameop(c, __doc__, Store)) |
| 1901 | return 0; |
| 1902 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1904 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1905 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | static PyCodeObject * |
| 1910 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1911 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | PyCodeObject *co; |
| 1913 | int addNone = 1; |
| 1914 | static PyObject *module; |
| 1915 | if (!module) { |
| 1916 | module = PyUnicode_InternFromString("<module>"); |
| 1917 | if (!module) |
| 1918 | return NULL; |
| 1919 | } |
| 1920 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1921 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | return NULL; |
| 1923 | switch (mod->kind) { |
| 1924 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1925 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | compiler_exit_scope(c); |
| 1927 | return 0; |
| 1928 | } |
| 1929 | break; |
| 1930 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1931 | if (find_ann(mod->v.Interactive.body)) { |
| 1932 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1933 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1935 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | break; |
| 1937 | case Expression_kind: |
| 1938 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1939 | addNone = 0; |
| 1940 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | default: |
| 1942 | PyErr_Format(PyExc_SystemError, |
| 1943 | "module kind %d should not be possible", |
| 1944 | mod->kind); |
| 1945 | return 0; |
| 1946 | } |
| 1947 | co = assemble(c, addNone); |
| 1948 | compiler_exit_scope(c); |
| 1949 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | /* The test for LOCAL must come before the test for FREE in order to |
| 1953 | handle classes where name is both local and free. The local var is |
| 1954 | a method and the free var is a free var referenced within a method. |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1955 | */ |
| 1956 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1957 | static int |
| 1958 | get_ref_type(struct compiler *c, PyObject *name) |
| 1959 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1960 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1961 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1962 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1963 | return CELL; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1964 | scope = _PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1966 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1967 | "_PyST_GetScope(name=%R) failed: " |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1968 | "unknown scope in unit %S (%R); " |
| 1969 | "symbols: %R; locals: %R; globals: %R", |
| 1970 | name, |
| 1971 | c->u->u_name, c->u->u_ste->ste_id, |
| 1972 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1973 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | static int |
| 1979 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1980 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1981 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1982 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1983 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1984 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1985 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1989 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1990 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1991 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1992 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1993 | if (qualname == NULL) |
| 1994 | qualname = co->co_name; |
| 1995 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1996 | if (free) { |
| 1997 | for (i = 0; i < free; ++i) { |
| 1998 | /* Bypass com_addop_varname because it will generate |
| 1999 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2000 | */ |
| 2001 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2002 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2003 | /* Special case: If a class contains a method with a |
| 2004 | free variable that has the same name as a method, |
| 2005 | the name will be considered free *and* local in the |
| 2006 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 2007 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2008 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2009 | int reftype = get_ref_type(c, name); |
| 2010 | if (reftype == -1) { |
| 2011 | return 0; |
| 2012 | } |
| 2013 | int arg; |
| 2014 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2015 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2016 | } |
| 2017 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2018 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2019 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2020 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2021 | PyErr_Format(PyExc_SystemError, |
| 2022 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 2023 | "freevars of code %S: %R", |
| 2024 | name, |
| 2025 | reftype, |
| 2026 | c->u->u_name, |
| 2027 | co->co_name, |
| 2028 | co->co_freevars); |
| 2029 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2030 | } |
| 2031 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2033 | flags |= 0x08; |
| 2034 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2036 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 2037 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2038 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2043 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | if (!decos) |
| 2048 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2051 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2052 | } |
| 2053 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2057 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2058 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2059 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2060 | /* Push a dict of keyword-only default values. |
| 2061 | |
| 2062 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2063 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2064 | int i; |
| 2065 | PyObject *keys = NULL; |
| 2066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2068 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2069 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2070 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2071 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2072 | if (!mangled) { |
| 2073 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2075 | if (keys == NULL) { |
| 2076 | keys = PyList_New(1); |
| 2077 | if (keys == NULL) { |
| 2078 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2079 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2080 | } |
| 2081 | PyList_SET_ITEM(keys, 0, mangled); |
| 2082 | } |
| 2083 | else { |
| 2084 | int res = PyList_Append(keys, mangled); |
| 2085 | Py_DECREF(mangled); |
| 2086 | if (res == -1) { |
| 2087 | goto error; |
| 2088 | } |
| 2089 | } |
| 2090 | if (!compiler_visit_expr(c, default_)) { |
| 2091 | goto error; |
| 2092 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2095 | if (keys != NULL) { |
| 2096 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2097 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2098 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2099 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2100 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2101 | assert(default_count > 0); |
| 2102 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2103 | } |
| 2104 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2105 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | error: |
| 2109 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2110 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2114 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2115 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2116 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2117 | return 1; |
| 2118 | } |
| 2119 | |
| 2120 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2121 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2122 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2123 | { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2124 | if (!annotation) { |
| 2125 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | } |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2127 | |
| 2128 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
| 2129 | if (!mangled) { |
| 2130 | return 0; |
| 2131 | } |
| 2132 | ADDOP_LOAD_CONST(c, mangled); |
| 2133 | Py_DECREF(mangled); |
| 2134 | |
| 2135 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2136 | VISIT(c, annexpr, annotation) |
| 2137 | } |
| 2138 | else { |
| 2139 | VISIT(c, expr, annotation); |
| 2140 | } |
| 2141 | *annotations_len += 2; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2142 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2146 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2147 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2148 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2149 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2151 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2152 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | c, |
| 2154 | arg->arg, |
| 2155 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2156 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2157 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2158 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2159 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | static int |
| 2163 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2164 | expr_ty returns) |
| 2165 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2166 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2167 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2168 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2169 | Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | */ |
| 2171 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2172 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2173 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2174 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2175 | return 0; |
| 2176 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2177 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2178 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2179 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2180 | args->vararg->annotation, &annotations_len)) |
| 2181 | return 0; |
| 2182 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2183 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2184 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2185 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2186 | args->kwarg->annotation, &annotations_len)) |
| 2187 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | if (!return_str) { |
| 2190 | return_str = PyUnicode_InternFromString("return"); |
| 2191 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2192 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2193 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2194 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2195 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2198 | if (annotations_len) { |
| 2199 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2200 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2202 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2203 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2207 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2208 | { |
| 2209 | VISIT_SEQ(c, expr, args->defaults); |
| 2210 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2211 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2214 | static Py_ssize_t |
| 2215 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2216 | { |
| 2217 | Py_ssize_t funcflags = 0; |
| 2218 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2219 | if (!compiler_visit_defaults(c, args)) |
| 2220 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2221 | funcflags |= 0x01; |
| 2222 | } |
| 2223 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2224 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2225 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2226 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2227 | return -1; |
| 2228 | } |
| 2229 | else if (res > 0) { |
| 2230 | funcflags |= 0x02; |
| 2231 | } |
| 2232 | } |
| 2233 | return funcflags; |
| 2234 | } |
| 2235 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2236 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2237 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2238 | { |
| 2239 | |
| 2240 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2241 | compiler_error(c, "cannot assign to __debug__"); |
| 2242 | return 1; |
| 2243 | } |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | static int |
| 2248 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2249 | { |
| 2250 | if (arg != NULL) { |
| 2251 | if (forbidden_name(c, arg->arg, Store)) |
| 2252 | return 0; |
| 2253 | } |
| 2254 | return 1; |
| 2255 | } |
| 2256 | |
| 2257 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2258 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2259 | { |
| 2260 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2261 | for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2262 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2263 | return 0; |
| 2264 | } |
| 2265 | } |
| 2266 | return 1; |
| 2267 | } |
| 2268 | |
| 2269 | static int |
| 2270 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2271 | { |
| 2272 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2273 | return 0; |
| 2274 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2275 | return 0; |
| 2276 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2277 | return 0; |
| 2278 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2279 | return 0; |
| 2280 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2281 | return 0; |
| 2282 | return 1; |
| 2283 | } |
| 2284 | |
| 2285 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2286 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2289 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2290 | arguments_ty args; |
| 2291 | expr_ty returns; |
| 2292 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2293 | asdl_expr_seq* decos; |
| 2294 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2295 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2296 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2297 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2298 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2300 | if (is_async) { |
| 2301 | assert(s->kind == AsyncFunctionDef_kind); |
| 2302 | |
| 2303 | args = s->v.AsyncFunctionDef.args; |
| 2304 | returns = s->v.AsyncFunctionDef.returns; |
| 2305 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2306 | name = s->v.AsyncFunctionDef.name; |
| 2307 | body = s->v.AsyncFunctionDef.body; |
| 2308 | |
| 2309 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2310 | } else { |
| 2311 | assert(s->kind == FunctionDef_kind); |
| 2312 | |
| 2313 | args = s->v.FunctionDef.args; |
| 2314 | returns = s->v.FunctionDef.returns; |
| 2315 | decos = s->v.FunctionDef.decorator_list; |
| 2316 | name = s->v.FunctionDef.name; |
| 2317 | body = s->v.FunctionDef.body; |
| 2318 | |
| 2319 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2320 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2321 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2322 | if (!compiler_check_debug_args(c, args)) |
| 2323 | return 0; |
| 2324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | if (!compiler_decorators(c, decos)) |
| 2326 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2327 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2328 | firstlineno = s->lineno; |
| 2329 | if (asdl_seq_LEN(decos)) { |
| 2330 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2331 | } |
| 2332 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2333 | funcflags = compiler_default_arguments(c, args); |
| 2334 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2336 | } |
| 2337 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2338 | annotations = compiler_visit_annotations(c, args, returns); |
| 2339 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2340 | return 0; |
| 2341 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2342 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2343 | funcflags |= 0x04; |
| 2344 | } |
| 2345 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2346 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2349 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2350 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2351 | if (c->c_optimize < 2) { |
| 2352 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2353 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2354 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | compiler_exit_scope(c); |
| 2356 | return 0; |
| 2357 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2360 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2362 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2363 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2364 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2366 | qualname = c->u->u_qualname; |
| 2367 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2369 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2370 | Py_XDECREF(qualname); |
| 2371 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2373 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2374 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2375 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2376 | Py_DECREF(qualname); |
| 2377 | Py_DECREF(co); |
| 2378 | return 0; |
| 2379 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2380 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2383 | /* decorators */ |
| 2384 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2385 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2386 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2387 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2388 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | static int |
| 2392 | compiler_class(struct compiler *c, stmt_ty s) |
| 2393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | PyCodeObject *co; |
| 2395 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2396 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2397 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | if (!compiler_decorators(c, decos)) |
| 2400 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2401 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2402 | firstlineno = s->lineno; |
| 2403 | if (asdl_seq_LEN(decos)) { |
| 2404 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2405 | } |
| 2406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | /* ultimately generate code for: |
| 2408 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2409 | where: |
| 2410 | <func> is a function/closure created from the class body; |
| 2411 | it has a single argument (__locals__) where the dict |
| 2412 | (or MutableSequence) representing the locals is passed |
| 2413 | <name> is the class name |
| 2414 | <bases> is the positional arguments and *varargs argument |
| 2415 | <keywords> is the keyword arguments and **kwds argument |
| 2416 | This borrows from compiler_call. |
| 2417 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2420 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2421 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | return 0; |
| 2423 | /* this block represents what we do in the new scope */ |
| 2424 | { |
| 2425 | /* use the class name for name mangling */ |
| 2426 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2427 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2428 | /* load (global) __name__ ... */ |
| 2429 | str = PyUnicode_InternFromString("__name__"); |
| 2430 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2431 | Py_XDECREF(str); |
| 2432 | compiler_exit_scope(c); |
| 2433 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2434 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | Py_DECREF(str); |
| 2436 | /* ... and store it as __module__ */ |
| 2437 | str = PyUnicode_InternFromString("__module__"); |
| 2438 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2439 | Py_XDECREF(str); |
| 2440 | compiler_exit_scope(c); |
| 2441 | return 0; |
| 2442 | } |
| 2443 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2444 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2445 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2446 | str = PyUnicode_InternFromString("__qualname__"); |
| 2447 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2448 | Py_XDECREF(str); |
| 2449 | compiler_exit_scope(c); |
| 2450 | return 0; |
| 2451 | } |
| 2452 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2454 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 | compiler_exit_scope(c); |
| 2456 | return 0; |
| 2457 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2458 | /* The following code is artificial */ |
| 2459 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2460 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2461 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2462 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2463 | str = PyUnicode_InternFromString("__class__"); |
| 2464 | if (str == NULL) { |
| 2465 | compiler_exit_scope(c); |
| 2466 | return 0; |
| 2467 | } |
| 2468 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2469 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2470 | if (i < 0) { |
| 2471 | compiler_exit_scope(c); |
| 2472 | return 0; |
| 2473 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2474 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2477 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2478 | str = PyUnicode_InternFromString("__classcell__"); |
| 2479 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2480 | Py_XDECREF(str); |
| 2481 | compiler_exit_scope(c); |
| 2482 | return 0; |
| 2483 | } |
| 2484 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2485 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2486 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2487 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2488 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2489 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2490 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2491 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2492 | /* create the code object */ |
| 2493 | co = assemble(c, 1); |
| 2494 | } |
| 2495 | /* leave the new scope */ |
| 2496 | compiler_exit_scope(c); |
| 2497 | if (co == NULL) |
| 2498 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2500 | /* 2. load the 'build_class' function */ |
| 2501 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2502 | |
| 2503 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2504 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2505 | Py_DECREF(co); |
| 2506 | return 0; |
| 2507 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2508 | Py_DECREF(co); |
| 2509 | |
| 2510 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2511 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | |
| 2513 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2514 | if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | return 0; |
| 2516 | |
| 2517 | /* 6. apply decorators */ |
| 2518 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2519 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2520 | } |
| 2521 | |
| 2522 | /* 7. store into <name> */ |
| 2523 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2524 | return 0; |
| 2525 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2528 | /* Return 0 if the expression is a constant value except named singletons. |
| 2529 | Return 1 otherwise. */ |
| 2530 | static int |
| 2531 | check_is_arg(expr_ty e) |
| 2532 | { |
| 2533 | if (e->kind != Constant_kind) { |
| 2534 | return 1; |
| 2535 | } |
| 2536 | PyObject *value = e->v.Constant.value; |
| 2537 | return (value == Py_None |
| 2538 | || value == Py_False |
| 2539 | || value == Py_True |
| 2540 | || value == Py_Ellipsis); |
| 2541 | } |
| 2542 | |
| 2543 | /* Check operands of identity chacks ("is" and "is not"). |
| 2544 | Emit a warning if any operand is a constant except named singletons. |
| 2545 | Return 0 on error. |
| 2546 | */ |
| 2547 | static int |
| 2548 | check_compare(struct compiler *c, expr_ty e) |
| 2549 | { |
| 2550 | Py_ssize_t i, n; |
| 2551 | int left = check_is_arg(e->v.Compare.left); |
| 2552 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2553 | for (i = 0; i < n; i++) { |
| 2554 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2555 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2556 | if (op == Is || op == IsNot) { |
| 2557 | if (!right || !left) { |
| 2558 | const char *msg = (op == Is) |
| 2559 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2560 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2561 | return compiler_warn(c, msg); |
| 2562 | } |
| 2563 | } |
| 2564 | left = right; |
| 2565 | } |
| 2566 | return 1; |
| 2567 | } |
| 2568 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2569 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2570 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2571 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2572 | switch (op) { |
| 2573 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2574 | cmp = Py_EQ; |
| 2575 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2576 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2577 | cmp = Py_NE; |
| 2578 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2579 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2580 | cmp = Py_LT; |
| 2581 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2582 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2583 | cmp = Py_LE; |
| 2584 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2585 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2586 | cmp = Py_GT; |
| 2587 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2588 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2589 | cmp = Py_GE; |
| 2590 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2591 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2592 | ADDOP_I(c, IS_OP, 0); |
| 2593 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2594 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2595 | ADDOP_I(c, IS_OP, 1); |
| 2596 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2597 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2598 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2599 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2600 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2601 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2602 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2603 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2604 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2605 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2606 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2607 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2608 | } |
| 2609 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2610 | |
| 2611 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2612 | static int |
| 2613 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2614 | { |
| 2615 | switch (e->kind) { |
| 2616 | case UnaryOp_kind: |
| 2617 | if (e->v.UnaryOp.op == Not) |
| 2618 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2619 | /* fallback to general implementation */ |
| 2620 | break; |
| 2621 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2622 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2623 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2624 | assert(n >= 0); |
| 2625 | int cond2 = e->v.BoolOp.op == Or; |
| 2626 | basicblock *next2 = next; |
| 2627 | if (!cond2 != !cond) { |
| 2628 | next2 = compiler_new_block(c); |
| 2629 | if (next2 == NULL) |
| 2630 | return 0; |
| 2631 | } |
| 2632 | for (i = 0; i < n; ++i) { |
| 2633 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2634 | return 0; |
| 2635 | } |
| 2636 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2637 | return 0; |
| 2638 | if (next2 != next) |
| 2639 | compiler_use_next_block(c, next2); |
| 2640 | return 1; |
| 2641 | } |
| 2642 | case IfExp_kind: { |
| 2643 | basicblock *end, *next2; |
| 2644 | end = compiler_new_block(c); |
| 2645 | if (end == NULL) |
| 2646 | return 0; |
| 2647 | next2 = compiler_new_block(c); |
| 2648 | if (next2 == NULL) |
| 2649 | return 0; |
| 2650 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2651 | return 0; |
| 2652 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2653 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2654 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2655 | compiler_use_next_block(c, next2); |
| 2656 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2657 | return 0; |
| 2658 | compiler_use_next_block(c, end); |
| 2659 | return 1; |
| 2660 | } |
| 2661 | case Compare_kind: { |
| 2662 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2663 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2664 | if (!check_compare(c, e)) { |
| 2665 | return 0; |
| 2666 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2667 | basicblock *cleanup = compiler_new_block(c); |
| 2668 | if (cleanup == NULL) |
| 2669 | return 0; |
| 2670 | VISIT(c, expr, e->v.Compare.left); |
| 2671 | for (i = 0; i < n; i++) { |
| 2672 | VISIT(c, expr, |
| 2673 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2674 | ADDOP(c, DUP_TOP); |
| 2675 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2676 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2677 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2678 | NEXT_BLOCK(c); |
| 2679 | } |
| 2680 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2681 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2682 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2683 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2684 | basicblock *end = compiler_new_block(c); |
| 2685 | if (end == NULL) |
| 2686 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2687 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2688 | compiler_use_next_block(c, cleanup); |
| 2689 | ADDOP(c, POP_TOP); |
| 2690 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2691 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2692 | } |
| 2693 | compiler_use_next_block(c, end); |
| 2694 | return 1; |
| 2695 | } |
| 2696 | /* fallback to general implementation */ |
| 2697 | break; |
| 2698 | } |
| 2699 | default: |
| 2700 | /* fallback to general implementation */ |
| 2701 | break; |
| 2702 | } |
| 2703 | |
| 2704 | /* general implementation */ |
| 2705 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2706 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2707 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2708 | return 1; |
| 2709 | } |
| 2710 | |
| 2711 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2712 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 | basicblock *end, *next; |
| 2715 | |
| 2716 | assert(e->kind == IfExp_kind); |
| 2717 | end = compiler_new_block(c); |
| 2718 | if (end == NULL) |
| 2719 | return 0; |
| 2720 | next = compiler_new_block(c); |
| 2721 | if (next == NULL) |
| 2722 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2723 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2724 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2726 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | compiler_use_next_block(c, next); |
| 2728 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2729 | compiler_use_next_block(c, end); |
| 2730 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2734 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2737 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2739 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2740 | arguments_ty args = e->v.Lambda.args; |
| 2741 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2742 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2743 | if (!compiler_check_debug_args(c, args)) |
| 2744 | return 0; |
| 2745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | if (!name) { |
| 2747 | name = PyUnicode_InternFromString("<lambda>"); |
| 2748 | if (!name) |
| 2749 | return 0; |
| 2750 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2751 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2752 | funcflags = compiler_default_arguments(c, args); |
| 2753 | if (funcflags == -1) { |
| 2754 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2756 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2757 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2758 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2759 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | /* Make None the first constant, so the lambda can't have a |
| 2762 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2763 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2767 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2768 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2769 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2770 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2771 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | } |
| 2773 | else { |
| 2774 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2775 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2777 | qualname = c->u->u_qualname; |
| 2778 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2780 | if (co == NULL) { |
| 2781 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2783 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2784 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2785 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2786 | Py_DECREF(qualname); |
| 2787 | Py_DECREF(co); |
| 2788 | return 0; |
| 2789 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2790 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | Py_DECREF(co); |
| 2792 | |
| 2793 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2797 | compiler_if(struct compiler *c, stmt_ty s) |
| 2798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | assert(s->kind == If_kind); |
| 2801 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2802 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2804 | } |
| 2805 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2806 | next = compiler_new_block(c); |
| 2807 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2808 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2809 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2810 | } |
| 2811 | else { |
| 2812 | next = end; |
| 2813 | } |
| 2814 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2815 | return 0; |
| 2816 | } |
| 2817 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2818 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2819 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2820 | compiler_use_next_block(c, next); |
| 2821 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | } |
| 2823 | compiler_use_next_block(c, end); |
| 2824 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | static int |
| 2828 | compiler_for(struct compiler *c, stmt_ty s) |
| 2829 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2830 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2833 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | cleanup = compiler_new_block(c); |
| 2835 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2836 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2838 | } |
| 2839 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2841 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | VISIT(c, expr, s->v.For.iter); |
| 2843 | ADDOP(c, GET_ITER); |
| 2844 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2845 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2846 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | VISIT(c, expr, s->v.For.target); |
| 2848 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2849 | /* Mark jump as artificial */ |
| 2850 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2851 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2853 | |
| 2854 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2857 | compiler_use_next_block(c, end); |
| 2858 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2861 | |
| 2862 | static int |
| 2863 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2864 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2865 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2866 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2867 | c->u->u_ste->ste_coroutine = 1; |
| 2868 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2869 | return compiler_error(c, "'async for' outside async function"); |
| 2870 | } |
| 2871 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2872 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2873 | except = compiler_new_block(c); |
| 2874 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2875 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2876 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2877 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2878 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2879 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2880 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2881 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2882 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2883 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2884 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2885 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2886 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2887 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2888 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2889 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2890 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2891 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2892 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2893 | /* Success block for __anext__ */ |
| 2894 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2895 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2896 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2897 | |
| 2898 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2899 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2900 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2901 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2902 | |
| 2903 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2904 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2905 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2906 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2907 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2908 | |
| 2909 | compiler_use_next_block(c, end); |
| 2910 | |
| 2911 | return 1; |
| 2912 | } |
| 2913 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2914 | static int |
| 2915 | compiler_while(struct compiler *c, stmt_ty s) |
| 2916 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2917 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2919 | body = compiler_new_block(c); |
| 2920 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2922 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2926 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2927 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2929 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2930 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2935 | SET_LOC(c, s); |
| 2936 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2937 | return 0; |
| 2938 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2939 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2940 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2941 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2942 | compiler_use_next_block(c, anchor); |
| 2943 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2945 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2948 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2952 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2953 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2954 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2955 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2956 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2957 | return compiler_error(c, "'return' outside function"); |
| 2958 | if (s->v.Return.value != NULL && |
| 2959 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2960 | { |
| 2961 | return compiler_error( |
| 2962 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2964 | if (preserve_tos) { |
| 2965 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2966 | } else { |
| 2967 | /* Emit instruction with line number for expression */ |
| 2968 | if (s->v.Return.value != NULL) { |
| 2969 | SET_LOC(c, s->v.Return.value); |
| 2970 | ADDOP(c, NOP); |
| 2971 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2972 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2973 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2974 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2975 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2976 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2977 | } |
| 2978 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2979 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2980 | } |
| 2981 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2982 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2987 | static int |
| 2988 | compiler_break(struct compiler *c) |
| 2989 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2990 | struct fblockinfo *loop = NULL; |
| 2991 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2992 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2993 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2994 | if (loop == NULL) { |
| 2995 | return compiler_error(c, "'break' outside loop"); |
| 2996 | } |
| 2997 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2998 | return 0; |
| 2999 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3000 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3001 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3002 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | static int |
| 3006 | compiler_continue(struct compiler *c) |
| 3007 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3008 | struct fblockinfo *loop = NULL; |
| 3009 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 3010 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3011 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3012 | if (loop == NULL) { |
| 3013 | return compiler_error(c, "'continue' not properly in loop"); |
| 3014 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3015 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3016 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3017 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3021 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | |
| 3023 | SETUP_FINALLY L |
| 3024 | <code for body> |
| 3025 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3026 | <code for finalbody> |
| 3027 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3028 | L: |
| 3029 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3030 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3032 | The special instructions use the block stack. Each block |
| 3033 | stack entry contains the instruction that created it (here |
| 3034 | SETUP_FINALLY), the level of the value stack at the time the |
| 3035 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3037 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | Pushes the current value stack level and the label |
| 3039 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3040 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3041 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3043 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3044 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 3045 | exceptions are pushed onto the value stack (and the exception |
| 3046 | condition is cleared), and the interpreter jumps to the label |
| 3047 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3048 | */ |
| 3049 | |
| 3050 | static int |
| 3051 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 3052 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3053 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 | body = compiler_new_block(c); |
| 3056 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3057 | exit = compiler_new_block(c); |
| 3058 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3060 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3061 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3062 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3064 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3066 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3067 | if (!compiler_try_except(c, s)) |
| 3068 | return 0; |
| 3069 | } |
| 3070 | else { |
| 3071 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3072 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3073 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3074 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3075 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3076 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3077 | /* `finally` block */ |
| 3078 | compiler_use_next_block(c, end); |
| 3079 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3080 | return 0; |
| 3081 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3082 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3083 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3084 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3089 | Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...": |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3090 | (The contents of the value stack is shown in [], with the top |
| 3091 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3092 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | |
| 3094 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3095 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | [] <code for S> |
| 3097 | [] POP_BLOCK |
| 3098 | [] JUMP_FORWARD L0 |
| 3099 | |
| 3100 | [tb, val, exc] L1: DUP ) |
| 3101 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3102 | [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | [tb, val, exc] POP |
| 3104 | [tb, val] <assign to V1> (or POP if no V1) |
| 3105 | [tb] POP |
| 3106 | [] <code for S1> |
| 3107 | JUMP_FORWARD L0 |
| 3108 | |
| 3109 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3110 | .............................etc....................... |
| 3111 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3112 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | |
| 3114 | [] L0: <next statement> |
| 3115 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3116 | Of course, parts are not generated if Vi or Ei is not present. |
| 3117 | */ |
| 3118 | static int |
| 3119 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3122 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | body = compiler_new_block(c); |
| 3125 | except = compiler_new_block(c); |
| 3126 | orelse = compiler_new_block(c); |
| 3127 | end = compiler_new_block(c); |
| 3128 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3129 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3130 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3132 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3134 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3135 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3136 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3137 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3138 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3140 | /* Runtime will push a block here, so we need to account for that */ |
| 3141 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3142 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | for (i = 0; i < n; i++) { |
| 3144 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3145 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3147 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3148 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | except = compiler_new_block(c); |
| 3150 | if (except == NULL) |
| 3151 | return 0; |
| 3152 | if (handler->v.ExceptHandler.type) { |
| 3153 | ADDOP(c, DUP_TOP); |
| 3154 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3155 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3156 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | } |
| 3158 | ADDOP(c, POP_TOP); |
| 3159 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3160 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3161 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3162 | cleanup_end = compiler_new_block(c); |
| 3163 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3164 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3165 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3166 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3167 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3168 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3169 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3171 | /* |
| 3172 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3173 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3174 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3175 | try: |
| 3176 | # body |
| 3177 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3178 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3179 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3180 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3182 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3183 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3184 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3185 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3186 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3188 | /* second # body */ |
| 3189 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3190 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3191 | ADDOP(c, POP_BLOCK); |
| 3192 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3193 | /* name = None; del name; # Mark as artificial */ |
| 3194 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3195 | ADDOP_LOAD_CONST(c, Py_None); |
| 3196 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3197 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3198 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3200 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3201 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3203 | /* name = None; del name; # Mark as artificial */ |
| 3204 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3205 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3206 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3207 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3209 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | } |
| 3211 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3212 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3214 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3215 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3216 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3218 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3219 | ADDOP(c, POP_TOP); |
| 3220 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3221 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3222 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3224 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3225 | /* name = None; del name; # Mark as artificial */ |
| 3226 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3227 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3228 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | compiler_use_next_block(c, except); |
| 3231 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3232 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3233 | /* Mark as artificial */ |
| 3234 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3235 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3237 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | compiler_use_next_block(c, end); |
| 3239 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3243 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3244 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3245 | return compiler_try_finally(c, s); |
| 3246 | else |
| 3247 | return compiler_try_except(c, s); |
| 3248 | } |
| 3249 | |
| 3250 | |
| 3251 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3252 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | /* The IMPORT_NAME opcode was already generated. This function |
| 3255 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | If there is a dot in name, we need to split it and emit a |
Serhiy Storchaka | f93234b | 2017-05-09 22:31:05 +0300 | [diff] [blame] | 3258 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3260 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3261 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3262 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3263 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3264 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3266 | while (1) { |
| 3267 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3269 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3270 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3271 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3272 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3273 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3274 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3275 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3276 | if (dot == -1) { |
| 3277 | break; |
| 3278 | } |
| 3279 | ADDOP(c, ROT_TWO); |
| 3280 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3282 | if (!compiler_nameop(c, asname, Store)) { |
| 3283 | return 0; |
| 3284 | } |
| 3285 | ADDOP(c, POP_TOP); |
| 3286 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | } |
| 3288 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | static int |
| 3292 | compiler_import(struct compiler *c, stmt_ty s) |
| 3293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | /* The Import node stores a module name like a.b.c as a single |
| 3295 | string. This is convenient for all cases except |
| 3296 | import a.b.c as d |
| 3297 | where we need to parse that string to extract the individual |
| 3298 | module names. |
| 3299 | XXX Perhaps change the representation to make this case simpler? |
| 3300 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3301 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3302 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3303 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | for (i = 0; i < n; i++) { |
| 3305 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3306 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3308 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3309 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3312 | if (alias->asname) { |
| 3313 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3314 | if (!r) |
| 3315 | return r; |
| 3316 | } |
| 3317 | else { |
| 3318 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3319 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3320 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3321 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3322 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3323 | if (tmp == NULL) |
| 3324 | return 0; |
| 3325 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3327 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | Py_DECREF(tmp); |
| 3329 | } |
| 3330 | if (!r) |
| 3331 | return r; |
| 3332 | } |
| 3333 | } |
| 3334 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | static int |
| 3338 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3339 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3340 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3341 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | if (!empty_string) { |
| 3345 | empty_string = PyUnicode_FromString(""); |
| 3346 | if (!empty_string) |
| 3347 | return 0; |
| 3348 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3349 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3350 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3351 | |
| 3352 | names = PyTuple_New(n); |
| 3353 | if (!names) |
| 3354 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | /* build up the names */ |
| 3357 | for (i = 0; i < n; i++) { |
| 3358 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3359 | Py_INCREF(alias->name); |
| 3360 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3361 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3364 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | Py_DECREF(names); |
| 3366 | return compiler_error(c, "from __future__ imports must occur " |
| 3367 | "at the beginning of the file"); |
| 3368 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3369 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | if (s->v.ImportFrom.module) { |
| 3372 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3373 | } |
| 3374 | else { |
| 3375 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3376 | } |
| 3377 | for (i = 0; i < n; i++) { |
| 3378 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3379 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3380 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3381 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3382 | assert(n == 1); |
| 3383 | ADDOP(c, IMPORT_STAR); |
| 3384 | return 1; |
| 3385 | } |
| 3386 | |
| 3387 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3388 | store_name = alias->name; |
| 3389 | if (alias->asname) |
| 3390 | store_name = alias->asname; |
| 3391 | |
| 3392 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | return 0; |
| 3394 | } |
| 3395 | } |
| 3396 | /* remove imported module */ |
| 3397 | ADDOP(c, POP_TOP); |
| 3398 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | static int |
| 3402 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3405 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3406 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3407 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3408 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3409 | (s->v.Assert.test->kind == Constant_kind && |
| 3410 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3411 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3412 | { |
| 3413 | if (!compiler_warn(c, "assertion is always true, " |
| 3414 | "perhaps remove parentheses?")) |
| 3415 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3416 | return 0; |
| 3417 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3419 | if (c->c_optimize) |
| 3420 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3421 | end = compiler_new_block(c); |
| 3422 | if (end == NULL) |
| 3423 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3424 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3425 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3426 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | if (s->v.Assert.msg) { |
| 3428 | VISIT(c, expr, s->v.Assert.msg); |
| 3429 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3430 | } |
| 3431 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3432 | compiler_use_next_block(c, end); |
| 3433 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3437 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3438 | { |
| 3439 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3440 | VISIT(c, expr, value); |
| 3441 | ADDOP(c, PRINT_EXPR); |
| 3442 | return 1; |
| 3443 | } |
| 3444 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3445 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3446 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3447 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3448 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3452 | /* Mark POP_TOP as artificial */ |
| 3453 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3454 | ADDOP(c, POP_TOP); |
| 3455 | return 1; |
| 3456 | } |
| 3457 | |
| 3458 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3459 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3460 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3461 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3464 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | switch (s->kind) { |
| 3467 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3468 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | case ClassDef_kind: |
| 3470 | return compiler_class(c, s); |
| 3471 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3472 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3473 | case Delete_kind: |
| 3474 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3475 | break; |
| 3476 | case Assign_kind: |
| 3477 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3478 | VISIT(c, expr, s->v.Assign.value); |
| 3479 | for (i = 0; i < n; i++) { |
| 3480 | if (i < n - 1) |
| 3481 | ADDOP(c, DUP_TOP); |
| 3482 | VISIT(c, expr, |
| 3483 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3484 | } |
| 3485 | break; |
| 3486 | case AugAssign_kind: |
| 3487 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3488 | case AnnAssign_kind: |
| 3489 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3490 | case For_kind: |
| 3491 | return compiler_for(c, s); |
| 3492 | case While_kind: |
| 3493 | return compiler_while(c, s); |
| 3494 | case If_kind: |
| 3495 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3496 | case Match_kind: |
| 3497 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | case Raise_kind: |
| 3499 | n = 0; |
| 3500 | if (s->v.Raise.exc) { |
| 3501 | VISIT(c, expr, s->v.Raise.exc); |
| 3502 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3503 | if (s->v.Raise.cause) { |
| 3504 | VISIT(c, expr, s->v.Raise.cause); |
| 3505 | n++; |
| 3506 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3508 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3509 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3511 | case Try_kind: |
| 3512 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | case Assert_kind: |
| 3514 | return compiler_assert(c, s); |
| 3515 | case Import_kind: |
| 3516 | return compiler_import(c, s); |
| 3517 | case ImportFrom_kind: |
| 3518 | return compiler_from_import(c, s); |
| 3519 | case Global_kind: |
| 3520 | case Nonlocal_kind: |
| 3521 | break; |
| 3522 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3523 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3525 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3526 | break; |
| 3527 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3528 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3529 | case Continue_kind: |
| 3530 | return compiler_continue(c); |
| 3531 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3532 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3533 | case AsyncFunctionDef_kind: |
| 3534 | return compiler_function(c, s, 1); |
| 3535 | case AsyncWith_kind: |
| 3536 | return compiler_async_with(c, s, 0); |
| 3537 | case AsyncFor_kind: |
| 3538 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3539 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | static int |
| 3545 | unaryop(unaryop_ty op) |
| 3546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | switch (op) { |
| 3548 | case Invert: |
| 3549 | return UNARY_INVERT; |
| 3550 | case Not: |
| 3551 | return UNARY_NOT; |
| 3552 | case UAdd: |
| 3553 | return UNARY_POSITIVE; |
| 3554 | case USub: |
| 3555 | return UNARY_NEGATIVE; |
| 3556 | default: |
| 3557 | PyErr_Format(PyExc_SystemError, |
| 3558 | "unary op %d should not be possible", op); |
| 3559 | return 0; |
| 3560 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3564 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | switch (op) { |
| 3567 | case Add: |
| 3568 | return BINARY_ADD; |
| 3569 | case Sub: |
| 3570 | return BINARY_SUBTRACT; |
| 3571 | case Mult: |
| 3572 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3573 | case MatMult: |
| 3574 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | case Div: |
| 3576 | return BINARY_TRUE_DIVIDE; |
| 3577 | case Mod: |
| 3578 | return BINARY_MODULO; |
| 3579 | case Pow: |
| 3580 | return BINARY_POWER; |
| 3581 | case LShift: |
| 3582 | return BINARY_LSHIFT; |
| 3583 | case RShift: |
| 3584 | return BINARY_RSHIFT; |
| 3585 | case BitOr: |
| 3586 | return BINARY_OR; |
| 3587 | case BitXor: |
| 3588 | return BINARY_XOR; |
| 3589 | case BitAnd: |
| 3590 | return BINARY_AND; |
| 3591 | case FloorDiv: |
| 3592 | return BINARY_FLOOR_DIVIDE; |
| 3593 | default: |
| 3594 | PyErr_Format(PyExc_SystemError, |
| 3595 | "binary op %d should not be possible", op); |
| 3596 | return 0; |
| 3597 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3601 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | switch (op) { |
| 3604 | case Add: |
| 3605 | return INPLACE_ADD; |
| 3606 | case Sub: |
| 3607 | return INPLACE_SUBTRACT; |
| 3608 | case Mult: |
| 3609 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3610 | case MatMult: |
| 3611 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3612 | case Div: |
| 3613 | return INPLACE_TRUE_DIVIDE; |
| 3614 | case Mod: |
| 3615 | return INPLACE_MODULO; |
| 3616 | case Pow: |
| 3617 | return INPLACE_POWER; |
| 3618 | case LShift: |
| 3619 | return INPLACE_LSHIFT; |
| 3620 | case RShift: |
| 3621 | return INPLACE_RSHIFT; |
| 3622 | case BitOr: |
| 3623 | return INPLACE_OR; |
| 3624 | case BitXor: |
| 3625 | return INPLACE_XOR; |
| 3626 | case BitAnd: |
| 3627 | return INPLACE_AND; |
| 3628 | case FloorDiv: |
| 3629 | return INPLACE_FLOOR_DIVIDE; |
| 3630 | default: |
| 3631 | PyErr_Format(PyExc_SystemError, |
| 3632 | "inplace binary op %d should not be possible", op); |
| 3633 | return 0; |
| 3634 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | static int |
| 3638 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3639 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3640 | int op, scope; |
| 3641 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | PyObject *dict = c->u->u_names; |
| 3645 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3646 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3647 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3648 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3649 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3650 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3651 | if (forbidden_name(c, name, ctx)) |
| 3652 | return 0; |
| 3653 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3654 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3655 | if (!mangled) |
| 3656 | return 0; |
| 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | op = 0; |
| 3659 | optype = OP_NAME; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 3660 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3661 | switch (scope) { |
| 3662 | case FREE: |
| 3663 | dict = c->u->u_freevars; |
| 3664 | optype = OP_DEREF; |
| 3665 | break; |
| 3666 | case CELL: |
| 3667 | dict = c->u->u_cellvars; |
| 3668 | optype = OP_DEREF; |
| 3669 | break; |
| 3670 | case LOCAL: |
| 3671 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3672 | optype = OP_FAST; |
| 3673 | break; |
| 3674 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3675 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | optype = OP_GLOBAL; |
| 3677 | break; |
| 3678 | case GLOBAL_EXPLICIT: |
| 3679 | optype = OP_GLOBAL; |
| 3680 | break; |
| 3681 | default: |
| 3682 | /* scope can be 0 */ |
| 3683 | break; |
| 3684 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3686 | /* XXX Leave assert here, but handle __doc__ and the like better */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3687 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3689 | switch (optype) { |
| 3690 | case OP_DEREF: |
| 3691 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3692 | case Load: |
| 3693 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3694 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3695 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3696 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3697 | } |
| 3698 | break; |
| 3699 | case OP_FAST: |
| 3700 | switch (ctx) { |
| 3701 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3702 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3703 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3704 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3705 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | return 1; |
| 3707 | case OP_GLOBAL: |
| 3708 | switch (ctx) { |
| 3709 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3710 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3711 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3712 | } |
| 3713 | break; |
| 3714 | case OP_NAME: |
| 3715 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3716 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3717 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3719 | } |
| 3720 | break; |
| 3721 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3724 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3725 | Py_DECREF(mangled); |
| 3726 | if (arg < 0) |
| 3727 | return 0; |
| 3728 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | static int |
| 3732 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3733 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3735 | int jumpi; |
| 3736 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3737 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | assert(e->kind == BoolOp_kind); |
| 3740 | if (e->v.BoolOp.op == And) |
| 3741 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3742 | else |
| 3743 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3744 | end = compiler_new_block(c); |
| 3745 | if (end == NULL) |
| 3746 | return 0; |
| 3747 | s = e->v.BoolOp.values; |
| 3748 | n = asdl_seq_LEN(s) - 1; |
| 3749 | assert(n >= 0); |
| 3750 | for (i = 0; i < n; ++i) { |
| 3751 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3752 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3753 | basicblock *next = compiler_new_block(c); |
| 3754 | if (next == NULL) { |
| 3755 | return 0; |
| 3756 | } |
| 3757 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3758 | } |
| 3759 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3760 | compiler_use_next_block(c, end); |
| 3761 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3765 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3766 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3767 | { |
| 3768 | Py_ssize_t n = asdl_seq_LEN(elts); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3769 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3770 | PyObject *folded = PyTuple_New(n); |
| 3771 | if (folded == NULL) { |
| 3772 | return 0; |
| 3773 | } |
| 3774 | PyObject *val; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3775 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3776 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3777 | Py_INCREF(val); |
| 3778 | PyTuple_SET_ITEM(folded, i, val); |
| 3779 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3780 | if (tuple) { |
| 3781 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3782 | } else { |
| 3783 | if (add == SET_ADD) { |
| 3784 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3785 | if (folded == NULL) { |
| 3786 | return 0; |
| 3787 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3788 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3789 | ADDOP_I(c, build, pushed); |
| 3790 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3791 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3792 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3793 | return 1; |
| 3794 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3795 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3796 | int big = n+pushed > STACK_USE_GUIDELINE; |
| 3797 | int seen_star = 0; |
| 3798 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3799 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3800 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3801 | seen_star = 1; |
| 3802 | } |
| 3803 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3804 | if (!seen_star && !big) { |
| 3805 | for (Py_ssize_t i = 0; i < n; i++) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3806 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3807 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3808 | } |
| 3809 | if (tuple) { |
| 3810 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3811 | } else { |
| 3812 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3813 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3814 | return 1; |
| 3815 | } |
| 3816 | int sequence_built = 0; |
| 3817 | if (big) { |
| 3818 | ADDOP_I(c, build, pushed); |
| 3819 | sequence_built = 1; |
| 3820 | } |
| 3821 | for (Py_ssize_t i = 0; i < n; i++) { |
| 3822 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3823 | if (elt->kind == Starred_kind) { |
| 3824 | if (sequence_built == 0) { |
| 3825 | ADDOP_I(c, build, i+pushed); |
| 3826 | sequence_built = 1; |
| 3827 | } |
| 3828 | VISIT(c, expr, elt->v.Starred.value); |
| 3829 | ADDOP_I(c, extend, 1); |
| 3830 | } |
| 3831 | else { |
| 3832 | VISIT(c, expr, elt); |
| 3833 | if (sequence_built) { |
| 3834 | ADDOP_I(c, add, 1); |
| 3835 | } |
| 3836 | } |
| 3837 | } |
| 3838 | assert(sequence_built); |
| 3839 | if (tuple) { |
| 3840 | ADDOP(c, LIST_TO_TUPLE); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3841 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3842 | return 1; |
| 3843 | } |
| 3844 | |
| 3845 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3846 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3847 | { |
| 3848 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3849 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3850 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3851 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3852 | if (elt->kind == Starred_kind && !seen_star) { |
| 3853 | if ((i >= (1 << 8)) || |
| 3854 | (n-i-1 >= (INT_MAX >> 8))) |
| 3855 | return compiler_error(c, |
| 3856 | "too many expressions in " |
| 3857 | "star-unpacking assignment"); |
| 3858 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3859 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3860 | } |
| 3861 | else if (elt->kind == Starred_kind) { |
| 3862 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3863 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3864 | } |
| 3865 | } |
| 3866 | if (!seen_star) { |
| 3867 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3868 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3869 | return 1; |
| 3870 | } |
| 3871 | |
| 3872 | static int |
| 3873 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3874 | { |
| 3875 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3876 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3877 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3878 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3879 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3880 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3881 | return 1; |
| 3882 | } |
| 3883 | |
| 3884 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3885 | compiler_list(struct compiler *c, expr_ty e) |
| 3886 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3887 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3888 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3889 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3890 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3891 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3892 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3893 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3894 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3895 | else |
| 3896 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3897 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | static int |
| 3901 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3902 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3903 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3904 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3905 | return assignment_helper(c, elts); |
| 3906 | } |
| 3907 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3908 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3909 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3910 | } |
| 3911 | else |
| 3912 | VISIT_SEQ(c, expr, elts); |
| 3913 | return 1; |
| 3914 | } |
| 3915 | |
| 3916 | static int |
| 3917 | compiler_set(struct compiler *c, expr_ty e) |
| 3918 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3919 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3920 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3924 | are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3925 | { |
| 3926 | Py_ssize_t i; |
| 3927 | for (i = begin; i < end; i++) { |
| 3928 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3929 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3930 | return 0; |
| 3931 | } |
| 3932 | return 1; |
| 3933 | } |
| 3934 | |
| 3935 | static int |
| 3936 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3937 | { |
| 3938 | Py_ssize_t i, n = end - begin; |
| 3939 | PyObject *keys, *key; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3940 | int big = n*2 > STACK_USE_GUIDELINE; |
| 3941 | if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3942 | for (i = begin; i < end; i++) { |
| 3943 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3944 | } |
| 3945 | keys = PyTuple_New(n); |
| 3946 | if (keys == NULL) { |
| 3947 | return 0; |
| 3948 | } |
| 3949 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3950 | key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3951 | Py_INCREF(key); |
| 3952 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3953 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3954 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3955 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3956 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3957 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3958 | if (big) { |
| 3959 | ADDOP_I(c, BUILD_MAP, 0); |
| 3960 | } |
| 3961 | for (i = begin; i < end; i++) { |
| 3962 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3963 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3964 | if (big) { |
| 3965 | ADDOP_I(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3966 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3967 | } |
| 3968 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3969 | ADDOP_I(c, BUILD_MAP, n); |
| 3970 | } |
| 3971 | return 1; |
| 3972 | } |
| 3973 | |
| 3974 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3975 | compiler_dict(struct compiler *c, expr_ty e) |
| 3976 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3977 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3978 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3979 | int is_unpacking = 0; |
| 3980 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3981 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3982 | elements = 0; |
| 3983 | for (i = 0; i < n; i++) { |
| 3984 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3985 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3986 | if (elements) { |
| 3987 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3988 | return 0; |
| 3989 | } |
| 3990 | if (have_dict) { |
| 3991 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3992 | } |
| 3993 | have_dict = 1; |
| 3994 | elements = 0; |
| 3995 | } |
| 3996 | if (have_dict == 0) { |
| 3997 | ADDOP_I(c, BUILD_MAP, 0); |
| 3998 | have_dict = 1; |
| 3999 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4000 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4001 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4002 | } |
| 4003 | else { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4004 | if (elements*2 > STACK_USE_GUIDELINE) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 4005 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4006 | return 0; |
| 4007 | } |
| 4008 | if (have_dict) { |
| 4009 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4010 | } |
| 4011 | have_dict = 1; |
| 4012 | elements = 0; |
| 4013 | } |
| 4014 | else { |
| 4015 | elements++; |
| 4016 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4017 | } |
| 4018 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4019 | if (elements) { |
| 4020 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4021 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4022 | } |
| 4023 | if (have_dict) { |
| 4024 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4025 | } |
| 4026 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4027 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4028 | if (!have_dict) { |
| 4029 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4030 | } |
| 4031 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | static int |
| 4035 | compiler_compare(struct compiler *c, expr_ty e) |
| 4036 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4037 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4038 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 4039 | if (!check_compare(c, e)) { |
| 4040 | return 0; |
| 4041 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4042 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4043 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 4044 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 4045 | if (n == 0) { |
| 4046 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4047 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4048 | } |
| 4049 | else { |
| 4050 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4051 | if (cleanup == NULL) |
| 4052 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4053 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | VISIT(c, expr, |
| 4055 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4056 | ADDOP(c, DUP_TOP); |
| 4057 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4058 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4059 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4060 | NEXT_BLOCK(c); |
| 4061 | } |
| 4062 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4063 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | basicblock *end = compiler_new_block(c); |
| 4065 | if (end == NULL) |
| 4066 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 4067 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | compiler_use_next_block(c, cleanup); |
| 4069 | ADDOP(c, ROT_TWO); |
| 4070 | ADDOP(c, POP_TOP); |
| 4071 | compiler_use_next_block(c, end); |
| 4072 | } |
| 4073 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4076 | static PyTypeObject * |
| 4077 | infer_type(expr_ty e) |
| 4078 | { |
| 4079 | switch (e->kind) { |
| 4080 | case Tuple_kind: |
| 4081 | return &PyTuple_Type; |
| 4082 | case List_kind: |
| 4083 | case ListComp_kind: |
| 4084 | return &PyList_Type; |
| 4085 | case Dict_kind: |
| 4086 | case DictComp_kind: |
| 4087 | return &PyDict_Type; |
| 4088 | case Set_kind: |
| 4089 | case SetComp_kind: |
| 4090 | return &PySet_Type; |
| 4091 | case GeneratorExp_kind: |
| 4092 | return &PyGen_Type; |
| 4093 | case Lambda_kind: |
| 4094 | return &PyFunction_Type; |
| 4095 | case JoinedStr_kind: |
| 4096 | case FormattedValue_kind: |
| 4097 | return &PyUnicode_Type; |
| 4098 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4099 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4100 | default: |
| 4101 | return NULL; |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | static int |
| 4106 | check_caller(struct compiler *c, expr_ty e) |
| 4107 | { |
| 4108 | switch (e->kind) { |
| 4109 | case Constant_kind: |
| 4110 | case Tuple_kind: |
| 4111 | case List_kind: |
| 4112 | case ListComp_kind: |
| 4113 | case Dict_kind: |
| 4114 | case DictComp_kind: |
| 4115 | case Set_kind: |
| 4116 | case SetComp_kind: |
| 4117 | case GeneratorExp_kind: |
| 4118 | case JoinedStr_kind: |
| 4119 | case FormattedValue_kind: |
| 4120 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4121 | "perhaps you missed a comma?", |
| 4122 | infer_type(e)->tp_name); |
| 4123 | default: |
| 4124 | return 1; |
| 4125 | } |
| 4126 | } |
| 4127 | |
| 4128 | static int |
| 4129 | check_subscripter(struct compiler *c, expr_ty e) |
| 4130 | { |
| 4131 | PyObject *v; |
| 4132 | |
| 4133 | switch (e->kind) { |
| 4134 | case Constant_kind: |
| 4135 | v = e->v.Constant.value; |
| 4136 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4137 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4138 | PyAnySet_Check(v))) |
| 4139 | { |
| 4140 | return 1; |
| 4141 | } |
| 4142 | /* fall through */ |
| 4143 | case Set_kind: |
| 4144 | case SetComp_kind: |
| 4145 | case GeneratorExp_kind: |
| 4146 | case Lambda_kind: |
| 4147 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4148 | "perhaps you missed a comma?", |
| 4149 | infer_type(e)->tp_name); |
| 4150 | default: |
| 4151 | return 1; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4156 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4157 | { |
| 4158 | PyObject *v; |
| 4159 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4160 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4161 | if (index_type == NULL |
| 4162 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4163 | || index_type == &PySlice_Type) { |
| 4164 | return 1; |
| 4165 | } |
| 4166 | |
| 4167 | switch (e->kind) { |
| 4168 | case Constant_kind: |
| 4169 | v = e->v.Constant.value; |
| 4170 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4171 | return 1; |
| 4172 | } |
| 4173 | /* fall through */ |
| 4174 | case Tuple_kind: |
| 4175 | case List_kind: |
| 4176 | case ListComp_kind: |
| 4177 | case JoinedStr_kind: |
| 4178 | case FormattedValue_kind: |
| 4179 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4180 | "not %.200s; " |
| 4181 | "perhaps you missed a comma?", |
| 4182 | infer_type(e)->tp_name, |
| 4183 | index_type->tp_name); |
| 4184 | default: |
| 4185 | return 1; |
| 4186 | } |
| 4187 | } |
| 4188 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4189 | // Return 1 if the method call was optimized, -1 if not, and 0 on error. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4190 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4191 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4192 | { |
| 4193 | Py_ssize_t argsl, i; |
| 4194 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4195 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4196 | |
| 4197 | /* Check that the call node is an attribute access, and that |
| 4198 | the call doesn't have keyword parameters. */ |
| 4199 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4200 | asdl_seq_LEN(e->v.Call.keywords)) { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4201 | return -1; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4202 | } |
| 4203 | /* Check that there aren't too many arguments */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4204 | argsl = asdl_seq_LEN(args); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4205 | if (argsl >= STACK_USE_GUIDELINE) { |
| 4206 | return -1; |
| 4207 | } |
| 4208 | /* Check that there are no *varargs types of arguments. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4209 | for (i = 0; i < argsl; i++) { |
| 4210 | expr_ty elt = asdl_seq_GET(args, i); |
| 4211 | if (elt->kind == Starred_kind) { |
| 4212 | return -1; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | /* Alright, we can optimize the code. */ |
| 4217 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4218 | int old_lineno = c->u->u_lineno; |
| 4219 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4220 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4221 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4222 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4223 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4224 | return 1; |
| 4225 | } |
| 4226 | |
| 4227 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4228 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4229 | { |
| 4230 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4231 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4232 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4233 | if (key->arg == NULL) { |
| 4234 | continue; |
| 4235 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4236 | if (forbidden_name(c, key->arg, Store)) { |
| 4237 | return -1; |
| 4238 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4239 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4240 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4241 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4242 | c->u->u_col_offset = other->col_offset; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4243 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4244 | return -1; |
| 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | return 0; |
| 4249 | } |
| 4250 | |
| 4251 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4252 | compiler_call(struct compiler *c, expr_ty e) |
| 4253 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4254 | int ret = maybe_optimize_method_call(c, e); |
| 4255 | if (ret >= 0) { |
| 4256 | return ret; |
| 4257 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4258 | if (!check_caller(c, e->v.Call.func)) { |
| 4259 | return 0; |
| 4260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4261 | VISIT(c, expr, e->v.Call.func); |
| 4262 | return compiler_call_helper(c, 0, |
| 4263 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4264 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4267 | static int |
| 4268 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4269 | { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4270 | |
| 4271 | Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); |
| 4272 | if (value_count > STACK_USE_GUIDELINE) { |
| 4273 | ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0)); |
| 4274 | PyObject *join = _PyUnicode_FromASCII("join", 4); |
| 4275 | if (join == NULL) { |
| 4276 | return 0; |
| 4277 | } |
| 4278 | ADDOP_NAME(c, LOAD_METHOD, join, names); |
| 4279 | Py_DECREF(join); |
| 4280 | ADDOP_I(c, BUILD_LIST, 0); |
| 4281 | for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { |
| 4282 | VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); |
| 4283 | ADDOP_I(c, LIST_APPEND, 1); |
| 4284 | } |
| 4285 | ADDOP_I(c, CALL_METHOD, 1); |
| 4286 | } |
| 4287 | else { |
| 4288 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
| 4289 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { |
| 4290 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
| 4291 | } |
| 4292 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4293 | return 1; |
| 4294 | } |
| 4295 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4296 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4297 | static int |
| 4298 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4299 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4300 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4301 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4302 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4303 | Convert the conversion char to 3 bits: |
| 4304 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4305 | !s : 001 0x1 FVC_STR |
| 4306 | !r : 010 0x2 FVC_REPR |
| 4307 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4308 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4309 | next bit is whether or not we have a format spec: |
| 4310 | yes : 100 0x4 |
| 4311 | no : 000 0x0 |
| 4312 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4313 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4314 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4315 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4316 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4317 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4318 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4319 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4320 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4321 | case 's': oparg = FVC_STR; break; |
| 4322 | case 'r': oparg = FVC_REPR; break; |
| 4323 | case 'a': oparg = FVC_ASCII; break; |
| 4324 | case -1: oparg = FVC_NONE; break; |
| 4325 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4326 | PyErr_Format(PyExc_SystemError, |
| 4327 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4328 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4329 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4330 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4331 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4332 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4333 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4334 | } |
| 4335 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4336 | /* And push our opcode and oparg */ |
| 4337 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4338 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4339 | return 1; |
| 4340 | } |
| 4341 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4342 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4343 | compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4344 | { |
| 4345 | Py_ssize_t i, n = end - begin; |
| 4346 | keyword_ty kw; |
| 4347 | PyObject *keys, *key; |
| 4348 | assert(n > 0); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4349 | int big = n*2 > STACK_USE_GUIDELINE; |
| 4350 | if (n > 1 && !big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4351 | for (i = begin; i < end; i++) { |
| 4352 | kw = asdl_seq_GET(keywords, i); |
| 4353 | VISIT(c, expr, kw->value); |
| 4354 | } |
| 4355 | keys = PyTuple_New(n); |
| 4356 | if (keys == NULL) { |
| 4357 | return 0; |
| 4358 | } |
| 4359 | for (i = begin; i < end; i++) { |
| 4360 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4361 | Py_INCREF(key); |
| 4362 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4363 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4364 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4365 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4366 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4367 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4368 | if (big) { |
| 4369 | ADDOP_I_NOLINE(c, BUILD_MAP, 0); |
| 4370 | } |
| 4371 | for (i = begin; i < end; i++) { |
| 4372 | kw = asdl_seq_GET(keywords, i); |
| 4373 | ADDOP_LOAD_CONST(c, kw->arg); |
| 4374 | VISIT(c, expr, kw->value); |
| 4375 | if (big) { |
| 4376 | ADDOP_I_NOLINE(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4377 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4378 | } |
| 4379 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4380 | ADDOP_I(c, BUILD_MAP, n); |
| 4381 | } |
| 4382 | return 1; |
| 4383 | } |
| 4384 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4385 | /* shared code between compiler_call and compiler_class */ |
| 4386 | static int |
| 4387 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4388 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4389 | asdl_expr_seq *args, |
| 4390 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4391 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4392 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4393 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4394 | if (validate_keywords(c, keywords) == -1) { |
| 4395 | return 0; |
| 4396 | } |
| 4397 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4398 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4399 | nkwelts = asdl_seq_LEN(keywords); |
| 4400 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4401 | if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) { |
| 4402 | goto ex_call; |
| 4403 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4404 | for (i = 0; i < nelts; i++) { |
| 4405 | expr_ty elt = asdl_seq_GET(args, i); |
| 4406 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4407 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4408 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4409 | } |
| 4410 | for (i = 0; i < nkwelts; i++) { |
| 4411 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4412 | if (kw->arg == NULL) { |
| 4413 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4414 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4416 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4417 | /* No * or ** args, so can use faster calling sequence */ |
| 4418 | for (i = 0; i < nelts; i++) { |
| 4419 | expr_ty elt = asdl_seq_GET(args, i); |
| 4420 | assert(elt->kind != Starred_kind); |
| 4421 | VISIT(c, expr, elt); |
| 4422 | } |
| 4423 | if (nkwelts) { |
| 4424 | PyObject *names; |
| 4425 | VISIT_SEQ(c, keyword, keywords); |
| 4426 | names = PyTuple_New(nkwelts); |
| 4427 | if (names == NULL) { |
| 4428 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4429 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4430 | for (i = 0; i < nkwelts; i++) { |
| 4431 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4432 | Py_INCREF(kw->arg); |
| 4433 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4434 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4435 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4436 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | else { |
| 4440 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4441 | return 1; |
| 4442 | } |
| 4443 | |
| 4444 | ex_call: |
| 4445 | |
| 4446 | /* Do positional arguments. */ |
| 4447 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4448 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4449 | } |
| 4450 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4451 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4452 | return 0; |
| 4453 | } |
| 4454 | /* Then keyword arguments */ |
| 4455 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4456 | /* Has a new dict been pushed */ |
| 4457 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4458 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4459 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4460 | for (i = 0; i < nkwelts; i++) { |
| 4461 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4462 | if (kw->arg == NULL) { |
| 4463 | /* A keyword argument unpacking. */ |
| 4464 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4465 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4466 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4467 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4468 | if (have_dict) { |
| 4469 | ADDOP_I(c, DICT_MERGE, 1); |
| 4470 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4471 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4472 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4473 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4474 | if (!have_dict) { |
| 4475 | ADDOP_I(c, BUILD_MAP, 0); |
| 4476 | have_dict = 1; |
| 4477 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4478 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4479 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4480 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4481 | else { |
| 4482 | nseen++; |
| 4483 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4484 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4485 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4486 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4487 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4488 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4489 | } |
| 4490 | if (have_dict) { |
| 4491 | ADDOP_I(c, DICT_MERGE, 1); |
| 4492 | } |
| 4493 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4494 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4495 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4497 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4498 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4501 | |
| 4502 | /* List and set comprehensions and generator expressions work by creating a |
| 4503 | nested function to perform the actual iteration. This means that the |
| 4504 | iteration variables don't leak into the current scope. |
| 4505 | The defined function is called immediately following its definition, with the |
| 4506 | result of that call being the result of the expression. |
| 4507 | The LC/SC version returns the populated container, while the GE version is |
| 4508 | flagged in symtable.c as a generator, so it returns the generator object |
| 4509 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4510 | |
| 4511 | Possible cleanups: |
| 4512 | - iterate over the generator sequence instead of using recursion |
| 4513 | */ |
| 4514 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4515 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4516 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4518 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4519 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4521 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4522 | comprehension_ty gen; |
| 4523 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4524 | if (gen->is_async) { |
| 4525 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4526 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4527 | } else { |
| 4528 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4529 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | static int |
| 4534 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4535 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4536 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4537 | expr_ty elt, expr_ty val, int type) |
| 4538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4539 | /* generate code for the iterator, then each of the ifs, |
| 4540 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | comprehension_ty gen; |
| 4543 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4544 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | start = compiler_new_block(c); |
| 4547 | skip = compiler_new_block(c); |
| 4548 | if_cleanup = compiler_new_block(c); |
| 4549 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4552 | anchor == NULL) |
| 4553 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4555 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | if (gen_index == 0) { |
| 4558 | /* Receive outermost iter as an implicit argument */ |
| 4559 | c->u->u_argcount = 1; |
| 4560 | ADDOP_I(c, LOAD_FAST, 0); |
| 4561 | } |
| 4562 | else { |
| 4563 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4564 | /* Fast path for the temporary variable assignment idiom: |
| 4565 | for y in [f(x)] |
| 4566 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4567 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4568 | switch (gen->iter->kind) { |
| 4569 | case List_kind: |
| 4570 | elts = gen->iter->v.List.elts; |
| 4571 | break; |
| 4572 | case Tuple_kind: |
| 4573 | elts = gen->iter->v.Tuple.elts; |
| 4574 | break; |
| 4575 | default: |
| 4576 | elts = NULL; |
| 4577 | } |
| 4578 | if (asdl_seq_LEN(elts) == 1) { |
| 4579 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4580 | if (elt->kind != Starred_kind) { |
| 4581 | VISIT(c, expr, elt); |
| 4582 | start = NULL; |
| 4583 | } |
| 4584 | } |
| 4585 | if (start) { |
| 4586 | VISIT(c, expr, gen->iter); |
| 4587 | ADDOP(c, GET_ITER); |
| 4588 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4590 | if (start) { |
| 4591 | depth++; |
| 4592 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4593 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4594 | NEXT_BLOCK(c); |
| 4595 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | /* XXX this needs to be cleaned up...a lot! */ |
| 4599 | n = asdl_seq_LEN(gen->ifs); |
| 4600 | for (i = 0; i < n; i++) { |
| 4601 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4602 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4603 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | NEXT_BLOCK(c); |
| 4605 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4607 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4608 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4609 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4610 | elt, val, type)) |
| 4611 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4613 | /* only append after the last for generator */ |
| 4614 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4615 | /* comprehension specific code */ |
| 4616 | switch (type) { |
| 4617 | case COMP_GENEXP: |
| 4618 | VISIT(c, expr, elt); |
| 4619 | ADDOP(c, YIELD_VALUE); |
| 4620 | ADDOP(c, POP_TOP); |
| 4621 | break; |
| 4622 | case COMP_LISTCOMP: |
| 4623 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4624 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4625 | break; |
| 4626 | case COMP_SETCOMP: |
| 4627 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4628 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | break; |
| 4630 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4631 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4634 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4635 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | break; |
| 4637 | default: |
| 4638 | return 0; |
| 4639 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | compiler_use_next_block(c, skip); |
| 4642 | } |
| 4643 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4644 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4645 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4646 | compiler_use_next_block(c, anchor); |
| 4647 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | |
| 4649 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4653 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4654 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4655 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4656 | expr_ty elt, expr_ty val, int type) |
| 4657 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4658 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4659 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4660 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4661 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4662 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4663 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4664 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4665 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4666 | return 0; |
| 4667 | } |
| 4668 | |
| 4669 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4670 | |
| 4671 | if (gen_index == 0) { |
| 4672 | /* Receive outermost iter as an implicit argument */ |
| 4673 | c->u->u_argcount = 1; |
| 4674 | ADDOP_I(c, LOAD_FAST, 0); |
| 4675 | } |
| 4676 | else { |
| 4677 | /* Sub-iter - calculate on the fly */ |
| 4678 | VISIT(c, expr, gen->iter); |
| 4679 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4682 | compiler_use_next_block(c, start); |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4683 | /* Runtime will push a block here, so we need to account for that */ |
| 4684 | if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start, |
| 4685 | NULL, NULL)) { |
| 4686 | return 0; |
| 4687 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4688 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4689 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4690 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4691 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4692 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4693 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4694 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4695 | |
| 4696 | n = asdl_seq_LEN(gen->ifs); |
| 4697 | for (i = 0; i < n; i++) { |
| 4698 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4699 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4700 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4701 | NEXT_BLOCK(c); |
| 4702 | } |
| 4703 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4704 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4705 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4706 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4707 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4708 | elt, val, type)) |
| 4709 | return 0; |
| 4710 | |
| 4711 | /* only append after the last for generator */ |
| 4712 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4713 | /* comprehension specific code */ |
| 4714 | switch (type) { |
| 4715 | case COMP_GENEXP: |
| 4716 | VISIT(c, expr, elt); |
| 4717 | ADDOP(c, YIELD_VALUE); |
| 4718 | ADDOP(c, POP_TOP); |
| 4719 | break; |
| 4720 | case COMP_LISTCOMP: |
| 4721 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4722 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4723 | break; |
| 4724 | case COMP_SETCOMP: |
| 4725 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4726 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4727 | break; |
| 4728 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4729 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4730 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4731 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4732 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4733 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4734 | break; |
| 4735 | default: |
| 4736 | return 0; |
| 4737 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4738 | } |
| 4739 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4740 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4741 | |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4742 | compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); |
| 4743 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4744 | compiler_use_next_block(c, except); |
| 4745 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4746 | |
| 4747 | return 1; |
| 4748 | } |
| 4749 | |
| 4750 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4751 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4752 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4753 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4755 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4756 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4757 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4758 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4759 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4760 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4761 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4762 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4763 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4764 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4765 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4766 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4768 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4772 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4773 | if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4774 | compiler_error(c, "asynchronous comprehension outside of " |
| 4775 | "an asynchronous function"); |
| 4776 | goto error_in_scope; |
| 4777 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | if (type != COMP_GENEXP) { |
| 4780 | int op; |
| 4781 | switch (type) { |
| 4782 | case COMP_LISTCOMP: |
| 4783 | op = BUILD_LIST; |
| 4784 | break; |
| 4785 | case COMP_SETCOMP: |
| 4786 | op = BUILD_SET; |
| 4787 | break; |
| 4788 | case COMP_DICTCOMP: |
| 4789 | op = BUILD_MAP; |
| 4790 | break; |
| 4791 | default: |
| 4792 | PyErr_Format(PyExc_SystemError, |
| 4793 | "unknown comprehension type %d", type); |
| 4794 | goto error_in_scope; |
| 4795 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4797 | ADDOP_I(c, op, 0); |
| 4798 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4799 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4800 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4801 | val, type)) |
| 4802 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | if (type != COMP_GENEXP) { |
| 4805 | ADDOP(c, RETURN_VALUE); |
| 4806 | } |
| 4807 | |
| 4808 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4809 | qualname = c->u->u_qualname; |
| 4810 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4811 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4812 | if (top_level_await && is_async_generator){ |
| 4813 | c->u->u_ste->ste_coroutine = 1; |
| 4814 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4815 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4816 | goto error; |
| 4817 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4818 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4819 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4820 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4821 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4822 | Py_DECREF(co); |
| 4823 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4824 | VISIT(c, expr, outermost->iter); |
| 4825 | |
| 4826 | if (outermost->is_async) { |
| 4827 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4828 | } else { |
| 4829 | ADDOP(c, GET_ITER); |
| 4830 | } |
| 4831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4832 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4833 | |
| 4834 | if (is_async_generator && type != COMP_GENEXP) { |
| 4835 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4836 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4837 | ADDOP(c, YIELD_FROM); |
| 4838 | } |
| 4839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4841 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4842 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4843 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4844 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4845 | Py_XDECREF(co); |
| 4846 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4850 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4852 | static identifier name; |
| 4853 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4854 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4855 | if (!name) |
| 4856 | return 0; |
| 4857 | } |
| 4858 | assert(e->kind == GeneratorExp_kind); |
| 4859 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4860 | e->v.GeneratorExp.generators, |
| 4861 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4865 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4867 | static identifier name; |
| 4868 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4869 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | if (!name) |
| 4871 | return 0; |
| 4872 | } |
| 4873 | assert(e->kind == ListComp_kind); |
| 4874 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4875 | e->v.ListComp.generators, |
| 4876 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static int |
| 4880 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 | static identifier name; |
| 4883 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4884 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4885 | if (!name) |
| 4886 | return 0; |
| 4887 | } |
| 4888 | assert(e->kind == SetComp_kind); |
| 4889 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4890 | e->v.SetComp.generators, |
| 4891 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | |
| 4895 | static int |
| 4896 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4897 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4898 | static identifier name; |
| 4899 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4900 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | if (!name) |
| 4902 | return 0; |
| 4903 | } |
| 4904 | assert(e->kind == DictComp_kind); |
| 4905 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4906 | e->v.DictComp.generators, |
| 4907 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | |
| 4911 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4912 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4914 | VISIT(c, expr, k->value); |
| 4915 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4919 | whether they are true or false. |
| 4920 | |
| 4921 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4922 | */ |
| 4923 | |
| 4924 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4925 | compiler_with_except_finish(struct compiler *c) { |
| 4926 | basicblock *exit; |
| 4927 | exit = compiler_new_block(c); |
| 4928 | if (exit == NULL) |
| 4929 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4930 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4931 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4932 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4933 | compiler_use_next_block(c, exit); |
| 4934 | ADDOP(c, POP_TOP); |
| 4935 | ADDOP(c, POP_TOP); |
| 4936 | ADDOP(c, POP_TOP); |
| 4937 | ADDOP(c, POP_EXCEPT); |
| 4938 | ADDOP(c, POP_TOP); |
| 4939 | return 1; |
| 4940 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4941 | |
| 4942 | /* |
| 4943 | Implements the async with statement. |
| 4944 | |
| 4945 | The semantics outlined in that PEP are as follows: |
| 4946 | |
| 4947 | async with EXPR as VAR: |
| 4948 | BLOCK |
| 4949 | |
| 4950 | It is implemented roughly as: |
| 4951 | |
| 4952 | context = EXPR |
| 4953 | exit = context.__aexit__ # not calling it |
| 4954 | value = await context.__aenter__() |
| 4955 | try: |
| 4956 | VAR = value # if VAR present in the syntax |
| 4957 | BLOCK |
| 4958 | finally: |
| 4959 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4960 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4961 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4962 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4963 | if not (await exit(*exc)): |
| 4964 | raise |
| 4965 | */ |
| 4966 | static int |
| 4967 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4968 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4969 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4970 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4971 | |
| 4972 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4973 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4974 | c->u->u_ste->ste_coroutine = 1; |
| 4975 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4976 | return compiler_error(c, "'async with' outside async function"); |
| 4977 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4978 | |
| 4979 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4980 | final = compiler_new_block(c); |
| 4981 | exit = compiler_new_block(c); |
| 4982 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4983 | return 0; |
| 4984 | |
| 4985 | /* Evaluate EXPR */ |
| 4986 | VISIT(c, expr, item->context_expr); |
| 4987 | |
| 4988 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4989 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4990 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4991 | ADDOP(c, YIELD_FROM); |
| 4992 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4993 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4994 | |
| 4995 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4996 | compiler_use_next_block(c, block); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 4997 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4998 | return 0; |
| 4999 | } |
| 5000 | |
| 5001 | if (item->optional_vars) { |
| 5002 | VISIT(c, expr, item->optional_vars); |
| 5003 | } |
| 5004 | else { |
| 5005 | /* Discard result from context.__aenter__() */ |
| 5006 | ADDOP(c, POP_TOP); |
| 5007 | } |
| 5008 | |
| 5009 | pos++; |
| 5010 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 5011 | /* BLOCK code */ |
| 5012 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 5013 | else if (!compiler_async_with(c, s, pos)) |
| 5014 | return 0; |
| 5015 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5016 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5017 | ADDOP(c, POP_BLOCK); |
| 5018 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5019 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5020 | /* For successful outcome: |
| 5021 | * call __exit__(None, None, None) |
| 5022 | */ |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 5023 | SET_LOC(c, s); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5024 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5025 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5026 | ADDOP(c, GET_AWAITABLE); |
| 5027 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 5028 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5029 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5030 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5031 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5032 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5033 | |
| 5034 | /* For exceptional outcome: */ |
| 5035 | compiler_use_next_block(c, final); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5036 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5037 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5038 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5039 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5040 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5041 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5042 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5043 | return 1; |
| 5044 | } |
| 5045 | |
| 5046 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5047 | /* |
| 5048 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5049 | with EXPR as VAR: |
| 5050 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5051 | is implemented as: |
| 5052 | <code for EXPR> |
| 5053 | SETUP_WITH E |
| 5054 | <code to store to VAR> or POP_TOP |
| 5055 | <code for BLOCK> |
| 5056 | LOAD_CONST (None, None, None) |
| 5057 | CALL_FUNCTION_EX 0 |
| 5058 | JUMP_FORWARD EXIT |
| 5059 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 5060 | POP_JUMP_IF_TRUE T: |
| 5061 | RERAISE |
| 5062 | T: POP_TOP * 3 (remove exception from stack) |
| 5063 | POP_EXCEPT |
| 5064 | POP_TOP |
| 5065 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5066 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5067 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5068 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5069 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5070 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5071 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5072 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5073 | |
| 5074 | assert(s->kind == With_kind); |
| 5075 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5076 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5077 | final = compiler_new_block(c); |
| 5078 | exit = compiler_new_block(c); |
| 5079 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5080 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5081 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5082 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5083 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5084 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5085 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5086 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5087 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5088 | compiler_use_next_block(c, block); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 5089 | if (!compiler_push_fblock(c, WITH, block, final, s)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5090 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5093 | if (item->optional_vars) { |
| 5094 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5095 | } |
| 5096 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5097 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5098 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5101 | pos++; |
| 5102 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 5103 | /* BLOCK code */ |
| 5104 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 5105 | else if (!compiler_with(c, s, pos)) |
| 5106 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5107 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 5108 | |
| 5109 | /* Mark all following code as artificial */ |
| 5110 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5111 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5112 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5113 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5114 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5115 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5116 | /* For successful outcome: |
| 5117 | * call __exit__(None, None, None) |
| 5118 | */ |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame^] | 5119 | SET_LOC(c, s); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5120 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5121 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5122 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5123 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5124 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5125 | /* For exceptional outcome: */ |
| 5126 | compiler_use_next_block(c, final); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5127 | ADDOP(c, WITH_EXCEPT_START); |
| 5128 | compiler_with_except_finish(c); |
| 5129 | |
| 5130 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5131 | return 1; |
| 5132 | } |
| 5133 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5134 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5135 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5138 | case NamedExpr_kind: |
| 5139 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5140 | ADDOP(c, DUP_TOP); |
| 5141 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5142 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | case BoolOp_kind: |
| 5144 | return compiler_boolop(c, e); |
| 5145 | case BinOp_kind: |
| 5146 | VISIT(c, expr, e->v.BinOp.left); |
| 5147 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5148 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 | break; |
| 5150 | case UnaryOp_kind: |
| 5151 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5152 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5153 | break; |
| 5154 | case Lambda_kind: |
| 5155 | return compiler_lambda(c, e); |
| 5156 | case IfExp_kind: |
| 5157 | return compiler_ifexp(c, e); |
| 5158 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5159 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5161 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | case GeneratorExp_kind: |
| 5163 | return compiler_genexp(c, e); |
| 5164 | case ListComp_kind: |
| 5165 | return compiler_listcomp(c, e); |
| 5166 | case SetComp_kind: |
| 5167 | return compiler_setcomp(c, e); |
| 5168 | case DictComp_kind: |
| 5169 | return compiler_dictcomp(c, e); |
| 5170 | case Yield_kind: |
| 5171 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5172 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5173 | if (e->v.Yield.value) { |
| 5174 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | } |
| 5176 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5177 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5178 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5179 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5180 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5181 | case YieldFrom_kind: |
| 5182 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5183 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5184 | |
| 5185 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5186 | return compiler_error(c, "'yield from' inside async function"); |
| 5187 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5188 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5189 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5190 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5191 | ADDOP(c, YIELD_FROM); |
| 5192 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5193 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5194 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5195 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5196 | return compiler_error(c, "'await' outside function"); |
| 5197 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5198 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5199 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5200 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5201 | return compiler_error(c, "'await' outside async function"); |
| 5202 | } |
| 5203 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5204 | |
| 5205 | VISIT(c, expr, e->v.Await.value); |
| 5206 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5207 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5208 | ADDOP(c, YIELD_FROM); |
| 5209 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5210 | case Compare_kind: |
| 5211 | return compiler_compare(c, e); |
| 5212 | case Call_kind: |
| 5213 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5214 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5215 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5216 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5217 | case JoinedStr_kind: |
| 5218 | return compiler_joined_str(c, e); |
| 5219 | case FormattedValue_kind: |
| 5220 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | /* The following exprs can be assignment targets. */ |
| 5222 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5223 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5224 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5225 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5226 | { |
| 5227 | int old_lineno = c->u->u_lineno; |
| 5228 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5230 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5231 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5232 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5233 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5234 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5235 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5236 | } |
| 5237 | int old_lineno = c->u->u_lineno; |
| 5238 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5239 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5240 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5241 | break; |
| 5242 | case Del: |
| 5243 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5244 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5245 | } |
| 5246 | break; |
| 5247 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5248 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5249 | case Starred_kind: |
| 5250 | switch (e->v.Starred.ctx) { |
| 5251 | case Store: |
| 5252 | /* In all legitimate cases, the Starred node was already replaced |
| 5253 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5254 | return compiler_error(c, |
| 5255 | "starred assignment target must be in a list or tuple"); |
| 5256 | default: |
| 5257 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5258 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5260 | break; |
| 5261 | case Slice_kind: |
| 5262 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5263 | case Name_kind: |
| 5264 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5265 | /* child nodes of List and Tuple will have expr_context set */ |
| 5266 | case List_kind: |
| 5267 | return compiler_list(c, e); |
| 5268 | case Tuple_kind: |
| 5269 | return compiler_tuple(c, e); |
| 5270 | } |
| 5271 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5275 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5276 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5277 | int old_lineno = c->u->u_lineno; |
| 5278 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5279 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5280 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5281 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5282 | c->u->u_col_offset = old_col_offset; |
| 5283 | return res; |
| 5284 | } |
| 5285 | |
| 5286 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5287 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5288 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5290 | expr_ty e = s->v.AugAssign.target; |
| 5291 | |
| 5292 | int old_lineno = c->u->u_lineno; |
| 5293 | int old_col_offset = c->u->u_col_offset; |
| 5294 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5296 | switch (e->kind) { |
| 5297 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5298 | VISIT(c, expr, e->v.Attribute.value); |
| 5299 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5300 | int old_lineno = c->u->u_lineno; |
| 5301 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5302 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5303 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | break; |
| 5305 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5306 | VISIT(c, expr, e->v.Subscript.value); |
| 5307 | VISIT(c, expr, e->v.Subscript.slice); |
| 5308 | ADDOP(c, DUP_TOP_TWO); |
| 5309 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | break; |
| 5311 | case Name_kind: |
| 5312 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5313 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5314 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5315 | default: |
| 5316 | PyErr_Format(PyExc_SystemError, |
| 5317 | "invalid node type (%d) for augmented assignment", |
| 5318 | e->kind); |
| 5319 | return 0; |
| 5320 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5321 | |
| 5322 | c->u->u_lineno = old_lineno; |
| 5323 | c->u->u_col_offset = old_col_offset; |
| 5324 | |
| 5325 | VISIT(c, expr, s->v.AugAssign.value); |
| 5326 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5327 | |
| 5328 | SET_LOC(c, e); |
| 5329 | |
| 5330 | switch (e->kind) { |
| 5331 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5332 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5333 | ADDOP(c, ROT_TWO); |
| 5334 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5335 | break; |
| 5336 | case Subscript_kind: |
| 5337 | ADDOP(c, ROT_THREE); |
| 5338 | ADDOP(c, STORE_SUBSCR); |
| 5339 | break; |
| 5340 | case Name_kind: |
| 5341 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5342 | default: |
| 5343 | Py_UNREACHABLE(); |
| 5344 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5345 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5349 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5350 | { |
| 5351 | VISIT(c, expr, e); |
| 5352 | ADDOP(c, POP_TOP); |
| 5353 | return 1; |
| 5354 | } |
| 5355 | |
| 5356 | static int |
| 5357 | check_annotation(struct compiler *c, stmt_ty s) |
| 5358 | { |
Batuhan Taskaya | 8cc3cfa | 2021-04-25 05:31:20 +0300 | [diff] [blame] | 5359 | /* Annotations of complex targets does not produce anything |
| 5360 | under annotations future */ |
| 5361 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5362 | return 1; |
| 5363 | } |
| 5364 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5365 | /* Annotations are only evaluated in a module or class. */ |
| 5366 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5367 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5368 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5369 | } |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
| 5373 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5374 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5375 | { |
| 5376 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5377 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5378 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5379 | if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5380 | return 0; |
| 5381 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5382 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5383 | return 0; |
| 5384 | } |
| 5385 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5386 | return 0; |
| 5387 | } |
| 5388 | return 1; |
| 5389 | case Tuple_kind: { |
| 5390 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5391 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5392 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5393 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5394 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5395 | return 0; |
| 5396 | } |
| 5397 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5398 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5399 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5400 | default: |
| 5401 | return check_ann_expr(c, e); |
| 5402 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | static int |
| 5406 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5407 | { |
| 5408 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5409 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5410 | |
| 5411 | assert(s->kind == AnnAssign_kind); |
| 5412 | |
| 5413 | /* We perform the actual assignment first. */ |
| 5414 | if (s->v.AnnAssign.value) { |
| 5415 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5416 | VISIT(c, expr, targ); |
| 5417 | } |
| 5418 | switch (targ->kind) { |
| 5419 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5420 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5421 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5422 | /* If we have a simple name in a module or class, store annotation. */ |
| 5423 | if (s->v.AnnAssign.simple && |
| 5424 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5425 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 5426 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5427 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5428 | } |
| 5429 | else { |
| 5430 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5431 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5432 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5433 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5434 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5435 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5436 | } |
| 5437 | break; |
| 5438 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5439 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5440 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5441 | if (!s->v.AnnAssign.value && |
| 5442 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5443 | return 0; |
| 5444 | } |
| 5445 | break; |
| 5446 | case Subscript_kind: |
| 5447 | if (!s->v.AnnAssign.value && |
| 5448 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5449 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5450 | return 0; |
| 5451 | } |
| 5452 | break; |
| 5453 | default: |
| 5454 | PyErr_Format(PyExc_SystemError, |
| 5455 | "invalid node type (%d) for annotated assignment", |
| 5456 | targ->kind); |
| 5457 | return 0; |
| 5458 | } |
| 5459 | /* Annotation is evaluated last. */ |
| 5460 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5461 | return 0; |
| 5462 | } |
| 5463 | return 1; |
| 5464 | } |
| 5465 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5466 | /* Raises a SyntaxError and returns 0. |
| 5467 | If something goes wrong, a different exception may be raised. |
| 5468 | */ |
| 5469 | |
| 5470 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5471 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5472 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5473 | va_list vargs; |
| 5474 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5475 | va_start(vargs, format); |
| 5476 | #else |
| 5477 | va_start(vargs); |
| 5478 | #endif |
| 5479 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5480 | va_end(vargs); |
| 5481 | if (msg == NULL) { |
| 5482 | return 0; |
| 5483 | } |
| 5484 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5485 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5486 | Py_INCREF(Py_None); |
| 5487 | loc = Py_None; |
| 5488 | } |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 5489 | PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename, |
| 5490 | c->u->u_lineno, c->u->u_col_offset + 1, loc, |
| 5491 | c->u->u_end_lineno, c->u->u_end_col_offset + 1); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5492 | Py_DECREF(msg); |
| 5493 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5495 | } |
| 5496 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5497 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5498 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5499 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5500 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5503 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5504 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5505 | and returns 0. |
| 5506 | */ |
| 5507 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5508 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5509 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5510 | va_list vargs; |
| 5511 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5512 | va_start(vargs, format); |
| 5513 | #else |
| 5514 | va_start(vargs); |
| 5515 | #endif |
| 5516 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5517 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5518 | if (msg == NULL) { |
| 5519 | return 0; |
| 5520 | } |
| 5521 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5522 | c->u->u_lineno, NULL, NULL) < 0) |
| 5523 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5524 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5525 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5526 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5527 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5528 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5529 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5530 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5531 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5532 | return 0; |
| 5533 | } |
| 5534 | Py_DECREF(msg); |
| 5535 | return 1; |
| 5536 | } |
| 5537 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5538 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5539 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5540 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5541 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5542 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5543 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5544 | if (ctx == Load) { |
| 5545 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5546 | return 0; |
| 5547 | } |
| 5548 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5549 | return 0; |
| 5550 | } |
| 5551 | } |
| 5552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5554 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | case Store: op = STORE_SUBSCR; break; |
| 5556 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5557 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5558 | assert(op); |
| 5559 | VISIT(c, expr, e->v.Subscript.value); |
| 5560 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5561 | ADDOP(c, op); |
| 5562 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5566 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5568 | int n = 2; |
| 5569 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5571 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5572 | if (s->v.Slice.lower) { |
| 5573 | VISIT(c, expr, s->v.Slice.lower); |
| 5574 | } |
| 5575 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5576 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5579 | if (s->v.Slice.upper) { |
| 5580 | VISIT(c, expr, s->v.Slice.upper); |
| 5581 | } |
| 5582 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5583 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | if (s->v.Slice.step) { |
| 5587 | n++; |
| 5588 | VISIT(c, expr, s->v.Slice.step); |
| 5589 | } |
| 5590 | ADDOP_I(c, BUILD_SLICE, n); |
| 5591 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5594 | |
| 5595 | // PEP 634: Structural Pattern Matching |
| 5596 | |
| 5597 | // To keep things simple, all compiler_pattern_* routines follow the convention |
| 5598 | // of replacing TOS (the subject for the given pattern) with either True (match) |
| 5599 | // or False (no match). We do this even for irrefutable patterns; the idea is |
| 5600 | // that it's much easier to smooth out any redundant pushing, popping, and |
| 5601 | // jumping in the peephole optimizer than to detect or predict it here. |
| 5602 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5603 | #define WILDCARD_CHECK(N) \ |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5604 | ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5605 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5606 | #define WILDCARD_STAR_CHECK(N) \ |
| 5607 | ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name) |
| 5608 | |
| 5609 | // Limit permitted subexpressions, even if the parser & AST validator let them through |
| 5610 | #define MATCH_VALUE_EXPR(N) \ |
| 5611 | ((N)->kind == Constant_kind || (N)->kind == Attribute_kind) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5612 | |
| 5613 | static int |
| 5614 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5615 | { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5616 | if (n == NULL) { |
| 5617 | ADDOP(c, POP_TOP); |
| 5618 | return 1; |
| 5619 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5620 | if (forbidden_name(c, n, Store)) { |
| 5621 | return 0; |
| 5622 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5623 | // Can't assign to the same name twice: |
| 5624 | if (pc->stores == NULL) { |
| 5625 | RETURN_IF_FALSE(pc->stores = PySet_New(NULL)); |
| 5626 | } |
| 5627 | else { |
| 5628 | int duplicate = PySet_Contains(pc->stores, n); |
| 5629 | if (duplicate < 0) { |
| 5630 | return 0; |
| 5631 | } |
| 5632 | if (duplicate) { |
| 5633 | const char *e = "multiple assignments to name %R in pattern"; |
| 5634 | return compiler_error(c, e, n); |
| 5635 | } |
| 5636 | } |
| 5637 | RETURN_IF_FALSE(!PySet_Add(pc->stores, n)); |
| 5638 | RETURN_IF_FALSE(compiler_nameop(c, n, Store)); |
| 5639 | return 1; |
| 5640 | } |
| 5641 | |
| 5642 | |
| 5643 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5644 | pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts) |
| 5645 | { |
| 5646 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 5647 | int seen_star = 0; |
| 5648 | for (Py_ssize_t i = 0; i < n; i++) { |
| 5649 | pattern_ty elt = asdl_seq_GET(elts, i); |
| 5650 | if (elt->kind == MatchStar_kind && !seen_star) { |
| 5651 | if ((i >= (1 << 8)) || |
| 5652 | (n-i-1 >= (INT_MAX >> 8))) |
| 5653 | return compiler_error(c, |
| 5654 | "too many expressions in " |
| 5655 | "star-unpacking sequence pattern"); |
| 5656 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 5657 | seen_star = 1; |
| 5658 | } |
| 5659 | else if (elt->kind == MatchStar_kind) { |
| 5660 | return compiler_error(c, |
| 5661 | "multiple starred expressions in sequence pattern"); |
| 5662 | } |
| 5663 | } |
| 5664 | if (!seen_star) { |
| 5665 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 5666 | } |
| 5667 | return 1; |
| 5668 | } |
| 5669 | |
| 5670 | static int |
| 5671 | pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5672 | Py_ssize_t star, pattern_context *pc) |
| 5673 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5674 | RETURN_IF_FALSE(pattern_unpack_helper(c, patterns)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5675 | // We've now got a bunch of new subjects on the stack. If any of them fail |
| 5676 | // to match, we need to pop everything else off, then finally push False. |
| 5677 | // fails is an array of blocks that correspond to the necessary amount of |
| 5678 | // popping for each element: |
| 5679 | basicblock **fails; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5680 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5681 | fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size); |
| 5682 | if (fails == NULL) { |
| 5683 | PyErr_NoMemory(); |
| 5684 | return 0; |
| 5685 | } |
| 5686 | // NOTE: Can't use our nice returning macros anymore: they'll leak memory! |
| 5687 | // goto error on error. |
| 5688 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5689 | fails[i] = compiler_new_block(c); |
| 5690 | if (fails[i] == NULL) { |
| 5691 | goto error; |
| 5692 | } |
| 5693 | } |
| 5694 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5695 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 5696 | assert((i == star) == (pattern->kind == MatchStar_kind)); |
| 5697 | if (!compiler_pattern_subpattern(c, pattern, pc) || |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5698 | !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) || |
| 5699 | compiler_next_block(c) == NULL) |
| 5700 | { |
| 5701 | goto error; |
| 5702 | } |
| 5703 | } |
| 5704 | // Success! |
| 5705 | basicblock *end = compiler_new_block(c); |
| 5706 | if (end == NULL || |
| 5707 | !compiler_addop_load_const(c, Py_True) || |
| 5708 | !compiler_addop_j(c, JUMP_FORWARD, end)) |
| 5709 | { |
| 5710 | goto error; |
| 5711 | } |
| 5712 | // This is where we handle failed sub-patterns. For a sequence pattern like |
| 5713 | // [a, b, c, d], this will look like: |
| 5714 | // fails[0]: POP_TOP |
| 5715 | // fails[1]: POP_TOP |
| 5716 | // fails[2]: POP_TOP |
| 5717 | // fails[3]: LOAD_CONST False |
| 5718 | for (Py_ssize_t i = 0; i < size - 1; i++) { |
| 5719 | compiler_use_next_block(c, fails[i]); |
| 5720 | if (!compiler_addop(c, POP_TOP)) { |
| 5721 | goto error; |
| 5722 | } |
| 5723 | } |
| 5724 | compiler_use_next_block(c, fails[size - 1]); |
| 5725 | if (!compiler_addop_load_const(c, Py_False)) { |
| 5726 | goto error; |
| 5727 | } |
| 5728 | compiler_use_next_block(c, end); |
| 5729 | PyObject_Free(fails); |
| 5730 | return 1; |
| 5731 | error: |
| 5732 | PyObject_Free(fails); |
| 5733 | return 0; |
| 5734 | } |
| 5735 | |
| 5736 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5737 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5738 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5739 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5740 | pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5741 | Py_ssize_t star, pattern_context *pc) |
| 5742 | { |
| 5743 | basicblock *end, *fail_pop_1; |
| 5744 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5745 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5746 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5747 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5748 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 5749 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5750 | continue; |
| 5751 | } |
| 5752 | if (i == star) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5753 | assert(WILDCARD_STAR_CHECK(pattern)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5754 | continue; |
| 5755 | } |
| 5756 | ADDOP(c, DUP_TOP); |
| 5757 | if (i < star) { |
| 5758 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5759 | } |
| 5760 | else { |
| 5761 | // The subject may not support negative indexing! Compute a |
| 5762 | // nonnegative index: |
| 5763 | ADDOP(c, GET_LEN); |
| 5764 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5765 | ADDOP(c, BINARY_SUBTRACT); |
| 5766 | } |
| 5767 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5768 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5769 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5770 | NEXT_BLOCK(c); |
| 5771 | } |
| 5772 | ADDOP(c, POP_TOP); |
| 5773 | ADDOP_LOAD_CONST(c, Py_True); |
| 5774 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5775 | compiler_use_next_block(c, fail_pop_1); |
| 5776 | ADDOP(c, POP_TOP); |
| 5777 | ADDOP_LOAD_CONST(c, Py_False); |
| 5778 | compiler_use_next_block(c, end); |
| 5779 | return 1; |
| 5780 | } |
| 5781 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5782 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5783 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5784 | compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5785 | { |
| 5786 | int allow_irrefutable = pc->allow_irrefutable; |
| 5787 | pc->allow_irrefutable = 1; |
| 5788 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5789 | pc->allow_irrefutable = allow_irrefutable; |
| 5790 | return 1; |
| 5791 | } |
| 5792 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5793 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5794 | compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) |
| 5795 | { |
| 5796 | assert(p->kind == MatchAs_kind); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5797 | if (p->v.MatchAs.pattern == NULL) { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5798 | // An irrefutable match: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5799 | if (!pc->allow_irrefutable) { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5800 | if (p->v.MatchAs.name) { |
| 5801 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5802 | return compiler_error(c, e, p->v.MatchAs.name); |
| 5803 | } |
| 5804 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 5805 | return compiler_error(c, e); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5806 | } |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5807 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5808 | ADDOP_LOAD_CONST(c, Py_True); |
| 5809 | return 1; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5810 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5811 | basicblock *end, *fail_pop_1; |
| 5812 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5813 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5814 | // Need to make a copy for (possibly) storing later: |
| 5815 | ADDOP(c, DUP_TOP); |
| 5816 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
| 5817 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5818 | NEXT_BLOCK(c); |
| 5819 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5820 | ADDOP_LOAD_CONST(c, Py_True); |
| 5821 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5822 | compiler_use_next_block(c, fail_pop_1); |
| 5823 | // Need to pop that unused copy from before: |
| 5824 | ADDOP(c, POP_TOP); |
| 5825 | ADDOP_LOAD_CONST(c, Py_False); |
| 5826 | compiler_use_next_block(c, end); |
| 5827 | return 1; |
| 5828 | } |
| 5829 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5830 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5831 | compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5832 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5833 | assert(p->kind == MatchStar_kind); |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5834 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc)); |
| 5835 | ADDOP_LOAD_CONST(c, Py_True); |
| 5836 | return 1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5837 | } |
| 5838 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5839 | static int |
| 5840 | validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns) |
| 5841 | { |
| 5842 | // Any errors will point to the pattern rather than the arg name as the |
| 5843 | // parser is only supplying identifiers rather than Name or keyword nodes |
| 5844 | Py_ssize_t nattrs = asdl_seq_LEN(attrs); |
| 5845 | for (Py_ssize_t i = 0; i < nattrs; i++) { |
| 5846 | identifier attr = ((identifier)asdl_seq_GET(attrs, i)); |
| 5847 | c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset; |
| 5848 | if (forbidden_name(c, attr, Store)) { |
| 5849 | return -1; |
| 5850 | } |
| 5851 | for (Py_ssize_t j = i + 1; j < nattrs; j++) { |
| 5852 | identifier other = ((identifier)asdl_seq_GET(attrs, j)); |
| 5853 | if (!PyUnicode_Compare(attr, other)) { |
| 5854 | c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, j))->col_offset; |
| 5855 | compiler_error(c, "attribute name repeated in class pattern: %U", attr); |
| 5856 | return -1; |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | return 0; |
| 5861 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5862 | |
| 5863 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5864 | compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5865 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5866 | assert(p->kind == MatchClass_kind); |
| 5867 | asdl_pattern_seq *patterns = p->v.MatchClass.patterns; |
| 5868 | asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs; |
| 5869 | asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns; |
| 5870 | Py_ssize_t nargs = asdl_seq_LEN(patterns); |
| 5871 | Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs); |
| 5872 | Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns); |
| 5873 | if (nattrs != nkwd_patterns) { |
| 5874 | // AST validator shouldn't let this happen, but if it does, |
| 5875 | // just fail, don't crash out of the interpreter |
| 5876 | const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern"; |
| 5877 | return compiler_error(c, e, nattrs, nkwd_patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5878 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5879 | if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) { |
| 5880 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5881 | return compiler_error(c, e, p->v.MatchClass.cls); |
| 5882 | } |
| 5883 | if (nattrs) { |
| 5884 | RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); |
| 5885 | c->u->u_col_offset = p->col_offset; // validate_kwd_attrs moves this |
| 5886 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5887 | basicblock *end, *fail_pop_1; |
| 5888 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5889 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5890 | VISIT(c, expr, p->v.MatchClass.cls); |
| 5891 | PyObject *attr_names; |
| 5892 | RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5893 | Py_ssize_t i; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5894 | for (i = 0; i < nattrs; i++) { |
| 5895 | PyObject *name = asdl_seq_GET(kwd_attrs, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5896 | Py_INCREF(name); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5897 | PyTuple_SET_ITEM(attr_names, i, name); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5898 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5899 | ADDOP_LOAD_CONST_NEW(c, attr_names); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5900 | ADDOP_I(c, MATCH_CLASS, nargs); |
| 5901 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5902 | NEXT_BLOCK(c); |
| 5903 | // TOS is now a tuple of (nargs + nkwargs) attributes. |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5904 | for (i = 0; i < nargs + nattrs; i++) { |
| 5905 | pattern_ty pattern; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5906 | if (i < nargs) { |
| 5907 | // Positional: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5908 | pattern = asdl_seq_GET(patterns, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5909 | } |
| 5910 | else { |
| 5911 | // Keyword: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5912 | pattern = asdl_seq_GET(kwd_patterns, i - nargs); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5913 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5914 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5915 | continue; |
| 5916 | } |
| 5917 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5918 | ADDOP(c, DUP_TOP); |
| 5919 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5920 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5921 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5922 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5923 | NEXT_BLOCK(c); |
| 5924 | } |
| 5925 | // Success! Pop the tuple of attributes: |
| 5926 | ADDOP(c, POP_TOP); |
| 5927 | ADDOP_LOAD_CONST(c, Py_True); |
| 5928 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5929 | compiler_use_next_block(c, fail_pop_1); |
| 5930 | ADDOP(c, POP_TOP); |
| 5931 | ADDOP_LOAD_CONST(c, Py_False); |
| 5932 | compiler_use_next_block(c, end); |
| 5933 | return 1; |
| 5934 | } |
| 5935 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5936 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5937 | compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5938 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5939 | assert(p->kind == MatchMapping_kind); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5940 | basicblock *end, *fail_pop_1, *fail_pop_3; |
| 5941 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5942 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5943 | RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5944 | asdl_expr_seq *keys = p->v.MatchMapping.keys; |
| 5945 | asdl_pattern_seq *patterns = p->v.MatchMapping.patterns; |
| 5946 | Py_ssize_t size = asdl_seq_LEN(keys); |
| 5947 | Py_ssize_t npatterns = asdl_seq_LEN(patterns); |
| 5948 | if (size != npatterns) { |
| 5949 | // AST validator shouldn't let this happen, but if it does, |
| 5950 | // just fail, don't crash out of the interpreter |
| 5951 | const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern"; |
| 5952 | return compiler_error(c, e, size, npatterns); |
| 5953 | } |
| 5954 | // We have a double-star target if "rest" is set |
| 5955 | PyObject *star_target = p->v.MatchMapping.rest; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5956 | ADDOP(c, MATCH_MAPPING); |
| 5957 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5958 | NEXT_BLOCK(c); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5959 | if (!size && !star_target) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5960 | // If the pattern is just "{}", we're done! |
| 5961 | ADDOP(c, POP_TOP); |
| 5962 | ADDOP_LOAD_CONST(c, Py_True); |
| 5963 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5964 | compiler_use_next_block(c, fail_pop_1); |
| 5965 | ADDOP(c, POP_TOP); |
| 5966 | ADDOP_LOAD_CONST(c, Py_False); |
| 5967 | compiler_use_next_block(c, end); |
| 5968 | return 1; |
| 5969 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5970 | if (size) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5971 | // If the pattern has any keys in it, perform a length check: |
| 5972 | ADDOP(c, GET_LEN); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5973 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5974 | ADDOP_COMPARE(c, GtE); |
| 5975 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5976 | NEXT_BLOCK(c); |
| 5977 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5978 | if (INT_MAX < size - 1) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5979 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 5980 | } |
| 5981 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 5982 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5983 | for (Py_ssize_t i = 0; i < size; i++) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5984 | expr_ty key = asdl_seq_GET(keys, i); |
| 5985 | if (key == NULL) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5986 | const char *e = "can't use NULL keys in MatchMapping " |
| 5987 | "(set 'rest' parameter instead)"; |
| 5988 | c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset; |
| 5989 | return compiler_error(c, e); |
| 5990 | } |
| 5991 | if (!MATCH_VALUE_EXPR(key)) { |
| 5992 | const char *e = "mapping pattern keys may only match literals and attribute lookups"; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5993 | return compiler_error(c, e); |
| 5994 | } |
| 5995 | VISIT(c, expr, key); |
| 5996 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5997 | ADDOP_I(c, BUILD_TUPLE, size); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5998 | ADDOP(c, MATCH_KEYS); |
| 5999 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 6000 | NEXT_BLOCK(c); |
| 6001 | // So far so good. There's now a tuple of values on the stack to match |
| 6002 | // sub-patterns against: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6003 | for (Py_ssize_t i = 0; i < size; i++) { |
| 6004 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 6005 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6006 | continue; |
| 6007 | } |
| 6008 | ADDOP(c, DUP_TOP); |
| 6009 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 6010 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6011 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6012 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 6013 | NEXT_BLOCK(c); |
| 6014 | } |
| 6015 | // If we get this far, it's a match! We're done with that tuple of values. |
| 6016 | ADDOP(c, POP_TOP); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6017 | if (star_target) { |
| 6018 | // If we have a starred name, bind a dict of remaining items to it: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6019 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6020 | RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6021 | } |
| 6022 | else { |
| 6023 | // Otherwise, we don't care about this tuple of keys anymore: |
| 6024 | ADDOP(c, POP_TOP); |
| 6025 | } |
| 6026 | // Pop the subject: |
| 6027 | ADDOP(c, POP_TOP); |
| 6028 | ADDOP_LOAD_CONST(c, Py_True); |
| 6029 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6030 | // The top two items are a tuple of values or None, followed by a tuple of |
| 6031 | // keys. Pop them both: |
| 6032 | compiler_use_next_block(c, fail_pop_3); |
| 6033 | ADDOP(c, POP_TOP); |
| 6034 | ADDOP(c, POP_TOP); |
| 6035 | compiler_use_next_block(c, fail_pop_1); |
| 6036 | // Pop the subject: |
| 6037 | ADDOP(c, POP_TOP); |
| 6038 | ADDOP_LOAD_CONST(c, Py_False); |
| 6039 | compiler_use_next_block(c, end); |
| 6040 | return 1; |
| 6041 | } |
| 6042 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6043 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6044 | compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6045 | { |
| 6046 | assert(p->kind == MatchOr_kind); |
| 6047 | // control is the set of names bound by the first alternative. If all of the |
| 6048 | // others bind the same names (they should), then this becomes pc->stores. |
| 6049 | PyObject *control = NULL; |
| 6050 | basicblock *end, *pass_pop_1; |
| 6051 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6052 | RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c)); |
| 6053 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 6054 | assert(size > 1); |
| 6055 | // We're going to be messing with pc. Keep the original info handy: |
| 6056 | PyObject *stores_init = pc->stores; |
| 6057 | int allow_irrefutable = pc->allow_irrefutable; |
| 6058 | for (Py_ssize_t i = 0; i < size; i++) { |
| 6059 | // NOTE: Can't use our nice returning macros in here: they'll leak sets! |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6060 | pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6061 | pc->stores = PySet_New(stores_init); |
| 6062 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 6063 | int is_last = i == size - 1; |
| 6064 | pc->allow_irrefutable = allow_irrefutable && is_last; |
| 6065 | SET_LOC(c, alt); |
| 6066 | if (pc->stores == NULL || |
| 6067 | // Only copy the subject if we're *not* on the last alternative: |
| 6068 | (!is_last && !compiler_addop(c, DUP_TOP)) || |
| 6069 | !compiler_pattern(c, alt, pc) || |
| 6070 | // Only jump if we're *not* on the last alternative: |
| 6071 | (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) || |
| 6072 | !compiler_next_block(c)) |
| 6073 | { |
| 6074 | goto fail; |
| 6075 | } |
| 6076 | if (!i) { |
| 6077 | // If this is the first alternative, save its stores as a "control" |
| 6078 | // for the others (they can't bind a different set of names): |
| 6079 | control = pc->stores; |
| 6080 | continue; |
| 6081 | } |
| 6082 | if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) { |
| 6083 | // Otherwise, check to see if we differ from the control set: |
| 6084 | PyObject *diff = PyNumber_InPlaceXor(pc->stores, control); |
| 6085 | if (diff == NULL) { |
| 6086 | goto fail; |
| 6087 | } |
| 6088 | if (PySet_GET_SIZE(diff)) { |
| 6089 | // The names differ! Raise. |
| 6090 | Py_DECREF(diff); |
| 6091 | compiler_error(c, "alternative patterns bind different names"); |
| 6092 | goto fail; |
| 6093 | } |
| 6094 | Py_DECREF(diff); |
| 6095 | } |
| 6096 | Py_DECREF(pc->stores); |
| 6097 | } |
| 6098 | Py_XDECREF(stores_init); |
| 6099 | // Update pc->stores and restore pc->allow_irrefutable: |
| 6100 | pc->stores = control; |
| 6101 | pc->allow_irrefutable = allow_irrefutable; |
| 6102 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6103 | compiler_use_next_block(c, pass_pop_1); |
| 6104 | ADDOP(c, POP_TOP); |
| 6105 | ADDOP_LOAD_CONST(c, Py_True); |
| 6106 | compiler_use_next_block(c, end); |
| 6107 | return 1; |
| 6108 | fail: |
| 6109 | Py_XDECREF(stores_init); |
| 6110 | Py_XDECREF(control); |
| 6111 | return 0; |
| 6112 | } |
| 6113 | |
| 6114 | |
| 6115 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6116 | compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6117 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6118 | assert(p->kind == MatchSequence_kind); |
| 6119 | asdl_pattern_seq *patterns = p->v.MatchSequence.patterns; |
| 6120 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6121 | Py_ssize_t star = -1; |
| 6122 | int only_wildcard = 1; |
| 6123 | int star_wildcard = 0; |
| 6124 | // Find a starred name, if it exists. There may be at most one: |
| 6125 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6126 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 6127 | if (pattern->kind == MatchStar_kind) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6128 | if (star >= 0) { |
| 6129 | const char *e = "multiple starred names in sequence pattern"; |
| 6130 | return compiler_error(c, e); |
| 6131 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6132 | star_wildcard = WILDCARD_STAR_CHECK(pattern); |
| 6133 | only_wildcard &= star_wildcard; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6134 | star = i; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6135 | continue; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6136 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6137 | only_wildcard &= WILDCARD_CHECK(pattern); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6138 | } |
| 6139 | basicblock *end, *fail_pop_1; |
| 6140 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6141 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 6142 | ADDOP(c, MATCH_SEQUENCE); |
| 6143 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 6144 | NEXT_BLOCK(c); |
| 6145 | if (star < 0) { |
| 6146 | // No star: len(subject) == size |
| 6147 | ADDOP(c, GET_LEN); |
| 6148 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 6149 | ADDOP_COMPARE(c, Eq); |
| 6150 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 6151 | NEXT_BLOCK(c); |
| 6152 | } |
| 6153 | else if (size > 1) { |
| 6154 | // Star: len(subject) >= size - 1 |
| 6155 | ADDOP(c, GET_LEN); |
| 6156 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 6157 | ADDOP_COMPARE(c, GtE); |
| 6158 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 6159 | NEXT_BLOCK(c); |
| 6160 | } |
| 6161 | if (only_wildcard) { |
| 6162 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 6163 | ADDOP(c, POP_TOP); |
| 6164 | ADDOP_LOAD_CONST(c, Py_True); |
| 6165 | } |
| 6166 | else if (star_wildcard) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6167 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6168 | } |
| 6169 | else { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6170 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6171 | } |
| 6172 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6173 | compiler_use_next_block(c, fail_pop_1); |
| 6174 | ADDOP(c, POP_TOP) |
| 6175 | ADDOP_LOAD_CONST(c, Py_False); |
| 6176 | compiler_use_next_block(c, end); |
| 6177 | return 1; |
| 6178 | } |
| 6179 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6180 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6181 | compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6182 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6183 | assert(p->kind == MatchValue_kind); |
| 6184 | expr_ty value = p->v.MatchValue.value; |
| 6185 | if (!MATCH_VALUE_EXPR(value)) { |
| 6186 | const char *e = "patterns may only match literals and attribute lookups"; |
| 6187 | return compiler_error(c, e); |
| 6188 | } |
| 6189 | VISIT(c, expr, value); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6190 | ADDOP_COMPARE(c, Eq); |
| 6191 | return 1; |
| 6192 | } |
| 6193 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6194 | static int |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 6195 | compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6196 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6197 | assert(p->kind == MatchSingleton_kind); |
| 6198 | ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value); |
| 6199 | ADDOP_COMPARE(c, Is); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6200 | return 1; |
| 6201 | } |
| 6202 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6203 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6204 | compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6205 | { |
| 6206 | SET_LOC(c, p); |
| 6207 | switch (p->kind) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6208 | case MatchValue_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6209 | return compiler_pattern_value(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6210 | case MatchSingleton_kind: |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 6211 | return compiler_pattern_singleton(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6212 | case MatchSequence_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6213 | return compiler_pattern_sequence(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6214 | case MatchMapping_kind: |
| 6215 | return compiler_pattern_mapping(c, p, pc); |
| 6216 | case MatchClass_kind: |
| 6217 | return compiler_pattern_class(c, p, pc); |
| 6218 | case MatchStar_kind: |
| 6219 | return compiler_pattern_star(c, p, pc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6220 | case MatchAs_kind: |
| 6221 | return compiler_pattern_as(c, p, pc); |
| 6222 | case MatchOr_kind: |
| 6223 | return compiler_pattern_or(c, p, pc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6224 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6225 | // AST validator shouldn't let this happen, but if it does, |
| 6226 | // just fail, don't crash out of the interpreter |
| 6227 | const char *e = "invalid match pattern node in AST (kind=%d)"; |
| 6228 | return compiler_error(c, e, p->kind); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6229 | } |
| 6230 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6231 | static int |
| 6232 | compiler_match(struct compiler *c, stmt_ty s) |
| 6233 | { |
| 6234 | VISIT(c, expr, s->v.Match.subject); |
| 6235 | basicblock *next, *end; |
| 6236 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6237 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6238 | assert(cases > 0); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6239 | pattern_context pc; |
| 6240 | // We use pc.stores to track: |
| 6241 | // - Repeated name assignments in the same pattern. |
| 6242 | // - Different name assignments in alternatives. |
| 6243 | // It's a set of names, but we don't create it until it's needed: |
| 6244 | pc.stores = NULL; |
| 6245 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6246 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6247 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6248 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6249 | SET_LOC(c, m->pattern); |
| 6250 | RETURN_IF_FALSE(next = compiler_new_block(c)); |
| 6251 | // If pc.allow_irrefutable is 0, any name captures against our subject |
| 6252 | // will raise. Irrefutable cases must be either guarded, last, or both: |
| 6253 | pc.allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6254 | // Only copy the subject if we're *not* on the last case: |
| 6255 | if (i != cases - has_default - 1) { |
| 6256 | ADDOP(c, DUP_TOP); |
| 6257 | } |
| 6258 | int result = compiler_pattern(c, m->pattern, &pc); |
| 6259 | Py_CLEAR(pc.stores); |
| 6260 | RETURN_IF_FALSE(result); |
| 6261 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next); |
| 6262 | NEXT_BLOCK(c); |
| 6263 | if (m->guard) { |
| 6264 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0)); |
| 6265 | } |
| 6266 | // Success! Pop the subject off, we're done with it: |
| 6267 | if (i != cases - has_default - 1) { |
| 6268 | ADDOP(c, POP_TOP); |
| 6269 | } |
| 6270 | VISIT_SEQ(c, stmt, m->body); |
| 6271 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6272 | compiler_use_next_block(c, next); |
| 6273 | } |
| 6274 | if (has_default) { |
| 6275 | if (cases == 1) { |
| 6276 | // No matches. Done with the subject: |
| 6277 | ADDOP(c, POP_TOP); |
| 6278 | } |
| 6279 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6280 | // pushing and popping in the loop above: |
| 6281 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6282 | SET_LOC(c, m->pattern); |
| 6283 | if (m->guard) { |
| 6284 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6285 | } |
| 6286 | VISIT_SEQ(c, stmt, m->body); |
| 6287 | } |
| 6288 | compiler_use_next_block(c, end); |
| 6289 | return 1; |
| 6290 | } |
| 6291 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6292 | #undef WILDCARD_CHECK |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6293 | #undef WILDCARD_STAR_CHECK |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6294 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6295 | /* End of the compiler section, beginning of the assembler section */ |
| 6296 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6297 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6298 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6299 | |
| 6300 | XXX must handle implicit jumps from one block to next |
| 6301 | */ |
| 6302 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6303 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6304 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6305 | int a_offset; /* offset into bytecode */ |
| 6306 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6307 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6308 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6309 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6310 | int a_lineno; /* lineno of last emitted instruction */ |
| 6311 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6312 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6313 | }; |
| 6314 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6315 | Py_LOCAL_INLINE(void) |
| 6316 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6317 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6318 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6319 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6320 | assert(b->b_startdepth < 0); |
| 6321 | b->b_startdepth = depth; |
| 6322 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6323 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | /* Find the flow path that needs the largest stack. We assume that |
| 6327 | * cycles in the flow graph have no net effect on the stack depth. |
| 6328 | */ |
| 6329 | static int |
| 6330 | stackdepth(struct compiler *c) |
| 6331 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6332 | basicblock *b, *entryblock = NULL; |
| 6333 | basicblock **stack, **sp; |
| 6334 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6335 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6336 | b->b_startdepth = INT_MIN; |
| 6337 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6338 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6339 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6340 | assert(entryblock!= NULL); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6341 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6342 | if (!stack) { |
| 6343 | PyErr_NoMemory(); |
| 6344 | return -1; |
| 6345 | } |
| 6346 | |
| 6347 | sp = stack; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6348 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
| 6349 | stackdepth_push(&sp, entryblock, 1); |
| 6350 | } else { |
| 6351 | stackdepth_push(&sp, entryblock, 0); |
| 6352 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6353 | while (sp != stack) { |
| 6354 | b = *--sp; |
| 6355 | int depth = b->b_startdepth; |
| 6356 | assert(depth >= 0); |
| 6357 | basicblock *next = b->b_next; |
| 6358 | for (int i = 0; i < b->b_iused; i++) { |
| 6359 | struct instr *instr = &b->b_instr[i]; |
| 6360 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6361 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6362 | PyErr_Format(PyExc_SystemError, |
| 6363 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6364 | instr->i_opcode, instr->i_oparg); |
| 6365 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6366 | } |
| 6367 | int new_depth = depth + effect; |
| 6368 | if (new_depth > maxdepth) { |
| 6369 | maxdepth = new_depth; |
| 6370 | } |
| 6371 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6372 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6373 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6374 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6375 | int target_depth = depth + effect; |
| 6376 | if (target_depth > maxdepth) { |
| 6377 | maxdepth = target_depth; |
| 6378 | } |
| 6379 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6380 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6381 | } |
| 6382 | depth = new_depth; |
| 6383 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6384 | instr->i_opcode == JUMP_FORWARD || |
| 6385 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6386 | instr->i_opcode == RAISE_VARARGS || |
| 6387 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6388 | { |
| 6389 | /* remaining code is dead */ |
| 6390 | next = NULL; |
| 6391 | break; |
| 6392 | } |
| 6393 | } |
| 6394 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6395 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6396 | stackdepth_push(&sp, next, depth); |
| 6397 | } |
| 6398 | } |
| 6399 | PyObject_Free(stack); |
| 6400 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
| 6403 | static int |
| 6404 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6406 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6407 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6408 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6409 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6410 | if (a->a_bytecode == NULL) { |
| 6411 | goto error; |
| 6412 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6413 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6414 | if (a->a_lnotab == NULL) { |
| 6415 | goto error; |
| 6416 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6417 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6419 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6420 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6421 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6422 | error: |
| 6423 | Py_XDECREF(a->a_bytecode); |
| 6424 | Py_XDECREF(a->a_lnotab); |
| 6425 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
| 6428 | static void |
| 6429 | assemble_free(struct assembler *a) |
| 6430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6431 | Py_XDECREF(a->a_bytecode); |
| 6432 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6435 | static int |
| 6436 | blocksize(basicblock *b) |
| 6437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6438 | int i; |
| 6439 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6441 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6442 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6443 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6444 | } |
| 6445 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6446 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6447 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6448 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6449 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6450 | if (a->a_lnotab_off + 2 >= len) { |
| 6451 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6452 | return 0; |
| 6453 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6454 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6455 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6456 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6457 | *lnotab++ = bdelta; |
| 6458 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6459 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6460 | } |
| 6461 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6462 | /* Appends a range to the end of the line number table. See |
| 6463 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6464 | |
| 6465 | static int |
| 6466 | assemble_line_range(struct assembler *a) |
| 6467 | { |
| 6468 | int ldelta, bdelta; |
| 6469 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6470 | if (bdelta == 0) { |
| 6471 | return 1; |
| 6472 | } |
| 6473 | if (a->a_lineno < 0) { |
| 6474 | ldelta = -128; |
| 6475 | } |
| 6476 | else { |
| 6477 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6478 | a->a_prevlineno = a->a_lineno; |
| 6479 | while (ldelta > 127) { |
| 6480 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6481 | return 0; |
| 6482 | } |
| 6483 | ldelta -= 127; |
| 6484 | } |
| 6485 | while (ldelta < -127) { |
| 6486 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6487 | return 0; |
| 6488 | } |
| 6489 | ldelta += 127; |
| 6490 | } |
| 6491 | } |
| 6492 | assert(-128 <= ldelta && ldelta < 128); |
| 6493 | while (bdelta > 254) { |
| 6494 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6495 | return 0; |
| 6496 | } |
| 6497 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6498 | bdelta -= 254; |
| 6499 | } |
| 6500 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6501 | return 0; |
| 6502 | } |
| 6503 | a->a_lineno_start = a->a_offset; |
| 6504 | return 1; |
| 6505 | } |
| 6506 | |
| 6507 | static int |
| 6508 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6509 | { |
| 6510 | if (i->i_lineno == a->a_lineno) { |
| 6511 | return 1; |
| 6512 | } |
| 6513 | if (!assemble_line_range(a)) { |
| 6514 | return 0; |
| 6515 | } |
| 6516 | a->a_lineno = i->i_lineno; |
| 6517 | return 1; |
| 6518 | } |
| 6519 | |
| 6520 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6521 | /* assemble_emit() |
| 6522 | Extend the bytecode with a new instruction. |
| 6523 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6524 | */ |
| 6525 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6526 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6527 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6528 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6529 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6530 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6531 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6532 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6533 | arg = i->i_oparg; |
| 6534 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6535 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6536 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6537 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6538 | if (len > PY_SSIZE_T_MAX / 2) |
| 6539 | return 0; |
| 6540 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6541 | return 0; |
| 6542 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6543 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6544 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6545 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6549 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6550 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6551 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6553 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6554 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6556 | /* Compute the size of each block and fixup jump args. |
| 6557 | Replace block pointer with position in bytecode. */ |
| 6558 | do { |
| 6559 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6560 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | bsize = blocksize(b); |
| 6562 | b->b_offset = totsize; |
| 6563 | totsize += bsize; |
| 6564 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6565 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6566 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6567 | bsize = b->b_offset; |
| 6568 | for (i = 0; i < b->b_iused; i++) { |
| 6569 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6570 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | /* Relative jumps are computed relative to |
| 6572 | the instruction pointer after fetching |
| 6573 | the jump instruction. |
| 6574 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6575 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6576 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6577 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6578 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6579 | instr->i_oparg -= bsize; |
| 6580 | } |
| 6581 | if (instrsize(instr->i_oparg) != isize) { |
| 6582 | extended_arg_recompile = 1; |
| 6583 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6584 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6585 | } |
| 6586 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6588 | /* XXX: This is an awful hack that could hurt performance, but |
| 6589 | on the bright side it should work until we come up |
| 6590 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | The issue is that in the first loop blocksize() is called |
| 6593 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6594 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6597 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6598 | The only EXTENDED_ARGs that could be popping up are |
| 6599 | ones in jump instructions. So this should converge |
| 6600 | fairly quickly. |
| 6601 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6602 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6603 | } |
| 6604 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6605 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6606 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6608 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6609 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | tuple = PyTuple_New(size); |
| 6612 | if (tuple == NULL) |
| 6613 | return NULL; |
| 6614 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6615 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6616 | Py_INCREF(k); |
| 6617 | assert((i - offset) < size); |
| 6618 | assert((i - offset) >= 0); |
| 6619 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6620 | } |
| 6621 | return tuple; |
| 6622 | } |
| 6623 | |
| 6624 | static PyObject * |
| 6625 | consts_dict_keys_inorder(PyObject *dict) |
| 6626 | { |
| 6627 | PyObject *consts, *k, *v; |
| 6628 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6629 | |
| 6630 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6631 | if (consts == NULL) |
| 6632 | return NULL; |
| 6633 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6634 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6635 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6636 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6637 | * the object we want is always second. */ |
| 6638 | if (PyTuple_CheckExact(k)) { |
| 6639 | k = PyTuple_GET_ITEM(k, 1); |
| 6640 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6641 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6642 | assert(i < size); |
| 6643 | assert(i >= 0); |
| 6644 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6645 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6646 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6649 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6650 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6652 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6653 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6654 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6655 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6656 | if (ste->ste_nested) |
| 6657 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6658 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6659 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6660 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6661 | flags |= CO_COROUTINE; |
| 6662 | if (ste->ste_generator && ste->ste_coroutine) |
| 6663 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6664 | if (ste->ste_varargs) |
| 6665 | flags |= CO_VARARGS; |
| 6666 | if (ste->ste_varkeywords) |
| 6667 | flags |= CO_VARKEYWORDS; |
| 6668 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6670 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6671 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6672 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6673 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6674 | ste->ste_coroutine && |
| 6675 | !ste->ste_generator) { |
| 6676 | flags |= CO_COROUTINE; |
| 6677 | } |
| 6678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6679 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6680 | } |
| 6681 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6682 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6683 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6684 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6685 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6686 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6687 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6688 | if (key == NULL) { |
| 6689 | return 0; |
| 6690 | } |
| 6691 | |
| 6692 | // t is borrowed reference |
| 6693 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6694 | Py_DECREF(key); |
| 6695 | if (t == NULL) { |
| 6696 | return 0; |
| 6697 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6698 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6699 | return 1; |
| 6700 | } |
| 6701 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6702 | if (PyTuple_CheckExact(t)) { |
| 6703 | // t is still borrowed reference |
| 6704 | t = PyTuple_GET_ITEM(t, 1); |
| 6705 | } |
| 6706 | |
| 6707 | Py_INCREF(t); |
| 6708 | Py_DECREF(*obj); |
| 6709 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6710 | return 1; |
| 6711 | } |
| 6712 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6713 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6714 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6716 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6717 | PyObject *names = NULL; |
| 6718 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6719 | PyObject *name = NULL; |
| 6720 | PyObject *freevars = NULL; |
| 6721 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6722 | Py_ssize_t nlocals; |
| 6723 | int nlocals_int; |
| 6724 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6725 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6727 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6728 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6729 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6730 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6731 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6732 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6733 | if (!cellvars) |
| 6734 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6735 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6736 | if (!freevars) |
| 6737 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6738 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6739 | if (!merge_const_one(c, &names) || |
| 6740 | !merge_const_one(c, &varnames) || |
| 6741 | !merge_const_one(c, &cellvars) || |
| 6742 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6743 | { |
| 6744 | goto error; |
| 6745 | } |
| 6746 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6747 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6748 | assert(nlocals < INT_MAX); |
| 6749 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6751 | flags = compute_code_flags(c); |
| 6752 | if (flags < 0) |
| 6753 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6754 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6755 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6756 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6757 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6758 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6759 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6760 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6761 | goto error; |
| 6762 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6763 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6764 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6765 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6766 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6767 | maxdepth = stackdepth(c); |
| 6768 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6769 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6770 | goto error; |
| 6771 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 6772 | if (maxdepth > MAX_ALLOWED_STACK_USE) { |
| 6773 | PyErr_Format(PyExc_SystemError, |
| 6774 | "excessive stack use: stack is %d deep", |
| 6775 | maxdepth); |
| 6776 | Py_DECREF(consts); |
| 6777 | goto error; |
| 6778 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6779 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6780 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6781 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6782 | varnames, freevars, cellvars, c->c_filename, |
| 6783 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6784 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6785 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6786 | Py_XDECREF(names); |
| 6787 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6788 | Py_XDECREF(name); |
| 6789 | Py_XDECREF(freevars); |
| 6790 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6791 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6794 | |
| 6795 | /* For debugging purposes only */ |
| 6796 | #if 0 |
| 6797 | static void |
| 6798 | dump_instr(const struct instr *i) |
| 6799 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6800 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 6801 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6802 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6804 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6805 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6806 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6807 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6808 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6809 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | static void |
| 6813 | dump_basicblock(const basicblock *b) |
| 6814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6815 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6816 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6817 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6818 | if (b->b_instr) { |
| 6819 | int i; |
| 6820 | for (i = 0; i < b->b_iused; i++) { |
| 6821 | fprintf(stderr, " [%02d] ", i); |
| 6822 | dump_instr(b->b_instr + i); |
| 6823 | } |
| 6824 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6825 | } |
| 6826 | #endif |
| 6827 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6828 | |
| 6829 | static int |
| 6830 | normalize_basic_block(basicblock *bb); |
| 6831 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6832 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6833 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6834 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6835 | static int |
| 6836 | ensure_exits_have_lineno(struct compiler *c); |
| 6837 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6838 | static int |
| 6839 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
| 6840 | |
| 6841 | int flags = compute_code_flags(c); |
| 6842 | if (flags < 0) { |
| 6843 | return -1; |
| 6844 | } |
| 6845 | int kind; |
| 6846 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 6847 | if (flags & CO_COROUTINE) { |
| 6848 | kind = 1; |
| 6849 | } |
| 6850 | else if (flags & CO_ASYNC_GENERATOR) { |
| 6851 | kind = 2; |
| 6852 | } |
| 6853 | else { |
| 6854 | kind = 0; |
| 6855 | } |
| 6856 | } |
| 6857 | else { |
| 6858 | return 0; |
| 6859 | } |
| 6860 | if (compiler_next_instr(entryblock) < 0) { |
| 6861 | return -1; |
| 6862 | } |
| 6863 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
| 6864 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
| 6865 | } |
| 6866 | entryblock->b_instr[0].i_opcode = GEN_START; |
| 6867 | entryblock->b_instr[0].i_oparg = kind; |
| 6868 | entryblock->b_instr[0].i_lineno = -1; |
| 6869 | entryblock->b_instr[0].i_target = NULL; |
| 6870 | return 0; |
| 6871 | } |
| 6872 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6873 | static PyCodeObject * |
| 6874 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6876 | basicblock *b, *entryblock; |
| 6877 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6878 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6879 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6880 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6882 | /* Make sure every block that falls off the end returns None. |
| 6883 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6884 | block ends with a jump or return b_next shouldn't set. |
| 6885 | */ |
| 6886 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6887 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6888 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6889 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6890 | ADDOP(c, RETURN_VALUE); |
| 6891 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6892 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6893 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6894 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6895 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6896 | } |
| 6897 | } |
| 6898 | |
| 6899 | if (ensure_exits_have_lineno(c)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6900 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6901 | } |
| 6902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6903 | nblocks = 0; |
| 6904 | entryblock = NULL; |
| 6905 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6906 | nblocks++; |
| 6907 | entryblock = b; |
| 6908 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6909 | assert(entryblock != NULL); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6910 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6911 | if (insert_generator_prefix(c, entryblock)) { |
| 6912 | goto error; |
| 6913 | } |
| 6914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6915 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6916 | if (!c->u->u_firstlineno) { |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6917 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6918 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6919 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6920 | c->u->u_firstlineno = 1; |
| 6921 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6923 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6924 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6925 | a.a_entry = entryblock; |
| 6926 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6927 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6928 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6929 | if (consts == NULL) { |
| 6930 | goto error; |
| 6931 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6932 | if (optimize_cfg(c, &a, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6933 | goto error; |
| 6934 | } |
| 6935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6936 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6937 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6938 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6939 | /* Emit code. */ |
| 6940 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6941 | for (j = 0; j < b->b_iused; j++) |
| 6942 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6943 | goto error; |
| 6944 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6945 | if (!assemble_line_range(&a)) { |
| 6946 | return 0; |
| 6947 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6948 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6949 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6950 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6951 | } |
| 6952 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6953 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6954 | } |
| 6955 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 6956 | goto error; |
| 6957 | } |
| 6958 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 6959 | goto error; |
| 6960 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6961 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6962 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6963 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6964 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6965 | assemble_free(&a); |
| 6966 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6967 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6968 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6969 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6970 | with LOAD_CONST (c1, c2, ... cn). |
| 6971 | The consts table must still be in list form so that the |
| 6972 | new constant (c1, c2, ... cn) can be appended. |
| 6973 | Called with codestr pointing to the first LOAD_CONST. |
| 6974 | */ |
| 6975 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6976 | fold_tuple_on_constants(struct compiler *c, |
| 6977 | struct instr *inst, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6978 | int n, PyObject *consts) |
| 6979 | { |
| 6980 | /* Pre-conditions */ |
| 6981 | assert(PyList_CheckExact(consts)); |
| 6982 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6983 | assert(inst[n].i_oparg == n); |
| 6984 | |
| 6985 | for (int i = 0; i < n; i++) { |
| 6986 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6987 | return 0; |
| 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | /* Buildup new tuple of constants */ |
| 6992 | PyObject *newconst = PyTuple_New(n); |
| 6993 | if (newconst == NULL) { |
| 6994 | return -1; |
| 6995 | } |
| 6996 | for (int i = 0; i < n; i++) { |
| 6997 | int arg = inst[i].i_oparg; |
| 6998 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6999 | Py_INCREF(constant); |
| 7000 | PyTuple_SET_ITEM(newconst, i, constant); |
| 7001 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7002 | if (merge_const_one(c, &newconst) == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7003 | Py_DECREF(newconst); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7004 | return -1; |
| 7005 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7006 | |
| 7007 | Py_ssize_t index; |
| 7008 | for (index = 0; index < PyList_GET_SIZE(consts); index++) { |
| 7009 | if (PyList_GET_ITEM(consts, index) == newconst) { |
| 7010 | break; |
| 7011 | } |
| 7012 | } |
| 7013 | if (index == PyList_GET_SIZE(consts)) { |
| 7014 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
| 7015 | Py_DECREF(newconst); |
| 7016 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 7017 | return -1; |
| 7018 | } |
| 7019 | if (PyList_Append(consts, newconst)) { |
| 7020 | Py_DECREF(newconst); |
| 7021 | return -1; |
| 7022 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7023 | } |
| 7024 | Py_DECREF(newconst); |
| 7025 | for (int i = 0; i < n; i++) { |
| 7026 | inst[i].i_opcode = NOP; |
| 7027 | } |
| 7028 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 7029 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7030 | return 0; |
| 7031 | } |
| 7032 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7033 | |
| 7034 | static int |
| 7035 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 7036 | assert (bb->b_iused > 0); |
| 7037 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 7038 | assert (is_jump(inst)); |
| 7039 | assert (inst->i_target->b_iused > 0); |
| 7040 | struct instr *target = &inst->i_target->b_instr[0]; |
| 7041 | if (inst->i_target == target->i_target) { |
| 7042 | /* Nothing to do */ |
| 7043 | return 0; |
| 7044 | } |
| 7045 | int lineno = target->i_lineno; |
| 7046 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 7047 | return -1; |
| 7048 | } |
| 7049 | assert (bb->b_iused >= 2); |
| 7050 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 7051 | return 0; |
| 7052 | } |
| 7053 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7054 | /* Maximum size of basic block that should be copied in optimizer */ |
| 7055 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7056 | |
| 7057 | /* Optimization */ |
| 7058 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7059 | optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7060 | { |
| 7061 | assert(PyList_CheckExact(consts)); |
| 7062 | struct instr nop; |
| 7063 | nop.i_opcode = NOP; |
| 7064 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7065 | for (int i = 0; i < bb->b_iused; i++) { |
| 7066 | struct instr *inst = &bb->b_instr[i]; |
| 7067 | int oparg = inst->i_oparg; |
| 7068 | int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7069 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7070 | /* Skip over empty basic blocks. */ |
| 7071 | while (inst->i_target->b_iused == 0) { |
| 7072 | inst->i_target = inst->i_target->b_next; |
| 7073 | } |
| 7074 | target = &inst->i_target->b_instr[0]; |
| 7075 | } |
| 7076 | else { |
| 7077 | target = &nop; |
| 7078 | } |
| 7079 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7080 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7081 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7082 | { |
| 7083 | PyObject* cnt; |
| 7084 | int is_true; |
| 7085 | int jump_if_true; |
| 7086 | switch(nextop) { |
| 7087 | case POP_JUMP_IF_FALSE: |
| 7088 | case POP_JUMP_IF_TRUE: |
| 7089 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7090 | is_true = PyObject_IsTrue(cnt); |
| 7091 | if (is_true == -1) { |
| 7092 | goto error; |
| 7093 | } |
| 7094 | inst->i_opcode = NOP; |
| 7095 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 7096 | if (is_true == jump_if_true) { |
| 7097 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7098 | bb->b_nofallthrough = 1; |
| 7099 | } |
| 7100 | else { |
| 7101 | bb->b_instr[i+1].i_opcode = NOP; |
| 7102 | } |
| 7103 | break; |
| 7104 | case JUMP_IF_FALSE_OR_POP: |
| 7105 | case JUMP_IF_TRUE_OR_POP: |
| 7106 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7107 | is_true = PyObject_IsTrue(cnt); |
| 7108 | if (is_true == -1) { |
| 7109 | goto error; |
| 7110 | } |
| 7111 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 7112 | if (is_true == jump_if_true) { |
| 7113 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7114 | bb->b_nofallthrough = 1; |
| 7115 | } |
| 7116 | else { |
| 7117 | inst->i_opcode = NOP; |
| 7118 | bb->b_instr[i+1].i_opcode = NOP; |
| 7119 | } |
| 7120 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7121 | } |
| 7122 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7123 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7124 | |
| 7125 | /* Try to fold tuples of constants. |
| 7126 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 7127 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 7128 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 7129 | case BUILD_TUPLE: |
| 7130 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 7131 | switch(oparg) { |
| 7132 | case 1: |
| 7133 | inst->i_opcode = NOP; |
| 7134 | bb->b_instr[i+1].i_opcode = NOP; |
| 7135 | break; |
| 7136 | case 2: |
| 7137 | inst->i_opcode = ROT_TWO; |
| 7138 | bb->b_instr[i+1].i_opcode = NOP; |
| 7139 | break; |
| 7140 | case 3: |
| 7141 | inst->i_opcode = ROT_THREE; |
| 7142 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 7143 | } |
| 7144 | break; |
| 7145 | } |
| 7146 | if (i >= oparg) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7147 | if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7148 | goto error; |
| 7149 | } |
| 7150 | } |
| 7151 | break; |
| 7152 | |
| 7153 | /* Simplify conditional jump to conditional jump where the |
| 7154 | result of the first test implies the success of a similar |
| 7155 | test or the failure of the opposite test. |
| 7156 | Arises in code like: |
| 7157 | "a and b or c" |
| 7158 | "(a and b) and c" |
| 7159 | "(a or b) or c" |
| 7160 | "(a or b) and c" |
| 7161 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 7162 | --> x:JUMP_IF_FALSE_OR_POP z |
| 7163 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 7164 | --> x:POP_JUMP_IF_FALSE y+1 |
| 7165 | where y+1 is the instruction following the second test. |
| 7166 | */ |
| 7167 | case JUMP_IF_FALSE_OR_POP: |
| 7168 | switch(target->i_opcode) { |
| 7169 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7170 | if (inst->i_lineno == target->i_lineno) { |
| 7171 | *inst = *target; |
| 7172 | i--; |
| 7173 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7174 | break; |
| 7175 | case JUMP_ABSOLUTE: |
| 7176 | case JUMP_FORWARD: |
| 7177 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7178 | if (inst->i_lineno == target->i_lineno && |
| 7179 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7180 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7181 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7182 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7183 | break; |
| 7184 | case JUMP_IF_TRUE_OR_POP: |
| 7185 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7186 | if (inst->i_lineno == target->i_lineno) { |
| 7187 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 7188 | inst->i_target = inst->i_target->b_next; |
| 7189 | --i; |
| 7190 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7191 | break; |
| 7192 | } |
| 7193 | break; |
| 7194 | |
| 7195 | case JUMP_IF_TRUE_OR_POP: |
| 7196 | switch(target->i_opcode) { |
| 7197 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7198 | if (inst->i_lineno == target->i_lineno) { |
| 7199 | *inst = *target; |
| 7200 | i--; |
| 7201 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7202 | break; |
| 7203 | case JUMP_ABSOLUTE: |
| 7204 | case JUMP_FORWARD: |
| 7205 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7206 | if (inst->i_lineno == target->i_lineno && |
| 7207 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7208 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7209 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7210 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7211 | break; |
| 7212 | case JUMP_IF_FALSE_OR_POP: |
| 7213 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7214 | if (inst->i_lineno == target->i_lineno) { |
| 7215 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 7216 | inst->i_target = inst->i_target->b_next; |
| 7217 | --i; |
| 7218 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7219 | break; |
| 7220 | } |
| 7221 | break; |
| 7222 | |
| 7223 | case POP_JUMP_IF_FALSE: |
| 7224 | switch(target->i_opcode) { |
| 7225 | case JUMP_ABSOLUTE: |
| 7226 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7227 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7228 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7229 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7230 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7231 | break; |
| 7232 | } |
| 7233 | break; |
| 7234 | |
| 7235 | case POP_JUMP_IF_TRUE: |
| 7236 | switch(target->i_opcode) { |
| 7237 | case JUMP_ABSOLUTE: |
| 7238 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7239 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7240 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7241 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7242 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7243 | break; |
| 7244 | } |
| 7245 | break; |
| 7246 | |
| 7247 | case JUMP_ABSOLUTE: |
| 7248 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7249 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7250 | switch(target->i_opcode) { |
| 7251 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7252 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7253 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7254 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7255 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7256 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7257 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7258 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7259 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7260 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7261 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7262 | default: |
| 7263 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7264 | basicblock *to_copy = inst->i_target; |
| 7265 | inst->i_opcode = NOP; |
| 7266 | for (i = 0; i < to_copy->b_iused; i++) { |
| 7267 | int index = compiler_next_instr(bb); |
| 7268 | if (index < 0) { |
| 7269 | return -1; |
| 7270 | } |
| 7271 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7272 | } |
| 7273 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7274 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7275 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7276 | } |
| 7277 | } |
| 7278 | return 0; |
| 7279 | error: |
| 7280 | return -1; |
| 7281 | } |
| 7282 | |
| 7283 | |
| 7284 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7285 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7286 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7287 | int dest = 0; |
| 7288 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7289 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7290 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7291 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7292 | if (lineno < 0) { |
| 7293 | continue; |
| 7294 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7295 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7296 | if (prev_lineno == lineno) { |
| 7297 | continue; |
| 7298 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7299 | /* or, if the next instruction has same line number or no line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7300 | if (src < bb->b_iused - 1) { |
| 7301 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7302 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7303 | bb->b_instr[src+1].i_lineno = lineno; |
| 7304 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7305 | } |
| 7306 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7307 | else { |
| 7308 | basicblock* next = bb->b_next; |
| 7309 | while (next && next->b_iused == 0) { |
| 7310 | next = next->b_next; |
| 7311 | } |
| 7312 | /* or if last instruction in BB and next BB has same line number */ |
| 7313 | if (next) { |
| 7314 | if (lineno == next->b_instr[0].i_lineno) { |
| 7315 | continue; |
| 7316 | } |
| 7317 | } |
| 7318 | } |
| 7319 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7320 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7321 | if (dest != src) { |
| 7322 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7323 | } |
| 7324 | dest++; |
| 7325 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7326 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7327 | assert(dest <= bb->b_iused); |
| 7328 | bb->b_iused = dest; |
| 7329 | } |
| 7330 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7331 | static int |
| 7332 | normalize_basic_block(basicblock *bb) { |
| 7333 | /* Mark blocks as exit and/or nofallthrough. |
| 7334 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7335 | for (int i = 0; i < bb->b_iused; i++) { |
| 7336 | switch(bb->b_instr[i].i_opcode) { |
| 7337 | case RETURN_VALUE: |
| 7338 | case RAISE_VARARGS: |
| 7339 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7340 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7341 | bb->b_nofallthrough = 1; |
| 7342 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7343 | case JUMP_ABSOLUTE: |
| 7344 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7345 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7346 | /* fall through */ |
| 7347 | case POP_JUMP_IF_FALSE: |
| 7348 | case POP_JUMP_IF_TRUE: |
| 7349 | case JUMP_IF_FALSE_OR_POP: |
| 7350 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7351 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7352 | if (i != bb->b_iused-1) { |
| 7353 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7354 | return -1; |
| 7355 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7356 | /* Skip over empty basic blocks. */ |
| 7357 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7358 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7359 | } |
| 7360 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7361 | } |
| 7362 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7363 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7364 | } |
| 7365 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7366 | static int |
| 7367 | mark_reachable(struct assembler *a) { |
| 7368 | basicblock **stack, **sp; |
| 7369 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7370 | if (stack == NULL) { |
| 7371 | return -1; |
| 7372 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7373 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7374 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7375 | while (sp > stack) { |
| 7376 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7377 | if (b->b_next && !b->b_nofallthrough) { |
| 7378 | if (b->b_next->b_predecessors == 0) { |
| 7379 | *sp++ = b->b_next; |
| 7380 | } |
| 7381 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7382 | } |
| 7383 | for (int i = 0; i < b->b_iused; i++) { |
| 7384 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7385 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7386 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7387 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7388 | *sp++ = target; |
| 7389 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7390 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7391 | } |
| 7392 | } |
| 7393 | } |
| 7394 | PyObject_Free(stack); |
| 7395 | return 0; |
| 7396 | } |
| 7397 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7398 | static void |
| 7399 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7400 | /* Eliminate empty blocks */ |
| 7401 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7402 | basicblock *next = b->b_next; |
| 7403 | if (next) { |
| 7404 | while (next->b_iused == 0 && next->b_next) { |
| 7405 | next = next->b_next; |
| 7406 | } |
| 7407 | b->b_next = next; |
| 7408 | } |
| 7409 | } |
| 7410 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7411 | if (b->b_iused == 0) { |
| 7412 | continue; |
| 7413 | } |
| 7414 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7415 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7416 | while (target->b_iused == 0) { |
| 7417 | target = target->b_next; |
| 7418 | } |
| 7419 | b->b_instr[b->b_iused-1].i_target = target; |
| 7420 | } |
| 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7425 | /* If an instruction has no line number, but it's predecessor in the BB does, |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7426 | * then copy the line number. If a successor block has no line number, and only |
| 7427 | * one predecessor, then inherit the line number. |
| 7428 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7429 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7430 | * but has no impact on the generated line number events. |
| 7431 | */ |
| 7432 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7433 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7434 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7435 | if (b->b_iused == 0) { |
| 7436 | continue; |
| 7437 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7438 | int prev_lineno = -1; |
| 7439 | for (int i = 0; i < b->b_iused; i++) { |
| 7440 | if (b->b_instr[i].i_lineno < 0) { |
| 7441 | b->b_instr[i].i_lineno = prev_lineno; |
| 7442 | } |
| 7443 | else { |
| 7444 | prev_lineno = b->b_instr[i].i_lineno; |
| 7445 | } |
| 7446 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7447 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7448 | assert(b->b_next->b_iused); |
| 7449 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7450 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7451 | } |
| 7452 | } |
| 7453 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7454 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7455 | /* Note: Only actual jumps, not exception handlers */ |
| 7456 | case SETUP_ASYNC_WITH: |
| 7457 | case SETUP_WITH: |
| 7458 | case SETUP_FINALLY: |
| 7459 | continue; |
| 7460 | } |
| 7461 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7462 | if (target->b_predecessors == 1) { |
| 7463 | if (target->b_instr[0].i_lineno < 0) { |
| 7464 | target->b_instr[0].i_lineno = prev_lineno; |
| 7465 | } |
| 7466 | } |
| 7467 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7468 | } |
| 7469 | } |
| 7470 | |
| 7471 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7472 | The consts object should still be in list form to allow new constants |
| 7473 | to be appended. |
| 7474 | |
| 7475 | All transformations keep the code size the same or smaller. |
| 7476 | For those that reduce size, the gaps are initially filled with |
| 7477 | NOPs. Later those NOPs are removed. |
| 7478 | */ |
| 7479 | |
| 7480 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7481 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7482 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7483 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7484 | if (optimize_basic_block(c, b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7485 | return -1; |
| 7486 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7487 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7488 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7489 | } |
| 7490 | if (mark_reachable(a)) { |
| 7491 | return -1; |
| 7492 | } |
| 7493 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7494 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7495 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7496 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7497 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7498 | } |
| 7499 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7500 | basicblock *pred = NULL; |
| 7501 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7502 | int prev_lineno = -1; |
| 7503 | if (pred && pred->b_iused) { |
| 7504 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7505 | } |
| 7506 | clean_basic_block(b, prev_lineno); |
| 7507 | pred = b->b_nofallthrough ? NULL : b; |
| 7508 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7509 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7510 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7511 | block ends with a jump instruction, check if the next non-empty block |
| 7512 | reached through normal flow control is the target of that jump. If it |
| 7513 | is, then the jump instruction is redundant and can be deleted. |
| 7514 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7515 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7516 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7517 | if (b->b_iused > 0) { |
| 7518 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7519 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7520 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7521 | if (b_last_instr->i_target == b->b_next) { |
| 7522 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7523 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7524 | b_last_instr->i_opcode = NOP; |
| 7525 | clean_basic_block(b, -1); |
| 7526 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7527 | } |
| 7528 | } |
| 7529 | } |
| 7530 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7531 | if (maybe_empty_blocks) { |
| 7532 | eliminate_empty_basic_blocks(a->a_entry); |
| 7533 | } |
| 7534 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7535 | return 0; |
| 7536 | } |
| 7537 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7538 | static inline int |
| 7539 | is_exit_without_lineno(basicblock *b) { |
| 7540 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7541 | } |
| 7542 | |
| 7543 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7544 | * after a frame terminates. It would be prohibitively expensive |
| 7545 | * to continuously update the f_lineno field at runtime, |
| 7546 | * so we make sure that all exiting instruction (raises and returns) |
| 7547 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7548 | * We can do this by duplicating the exit blocks without line number |
| 7549 | * so that none have more than one predecessor. We can then safely |
| 7550 | * copy the line number from the sole predecessor block. |
| 7551 | */ |
| 7552 | static int |
| 7553 | ensure_exits_have_lineno(struct compiler *c) |
| 7554 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7555 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7556 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7557 | */ |
| 7558 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7559 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7560 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7561 | /* Note: Only actual jumps, not exception handlers */ |
| 7562 | case SETUP_ASYNC_WITH: |
| 7563 | case SETUP_WITH: |
| 7564 | case SETUP_FINALLY: |
| 7565 | continue; |
| 7566 | } |
| 7567 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7568 | if (is_exit_without_lineno(target)) { |
| 7569 | basicblock *new_target = compiler_copy_block(c, target); |
| 7570 | if (new_target == NULL) { |
| 7571 | return -1; |
| 7572 | } |
| 7573 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7574 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 7575 | } |
| 7576 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7577 | entry = b; |
| 7578 | } |
| 7579 | assert(entry != NULL); |
| 7580 | if (is_exit_without_lineno(entry)) { |
| 7581 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7582 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7583 | /* Eliminate empty blocks */ |
| 7584 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7585 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7586 | b->b_next = b->b_next->b_next; |
| 7587 | } |
| 7588 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7589 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7590 | * fall through, and thus can only have a single predecessor */ |
| 7591 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7592 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7593 | if (is_exit_without_lineno(b->b_next)) { |
| 7594 | assert(b->b_next->b_iused > 0); |
| 7595 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7596 | } |
| 7597 | } |
| 7598 | } |
| 7599 | return 0; |
| 7600 | } |
| 7601 | |
| 7602 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7603 | /* Retained for API compatibility. |
| 7604 | * Optimization is now done in optimize_cfg */ |
| 7605 | |
| 7606 | PyObject * |
| 7607 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7608 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7609 | { |
| 7610 | Py_INCREF(code); |
| 7611 | return code; |
| 7612 | } |