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 | |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 24 | #include <stdbool.h> |
| 25 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 26 | #include "Python.h" |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 27 | #include "pycore_ast.h" // _PyAST_GetDocString() |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 28 | #include "pycore_compile.h" // _PyFuture_FromAST() |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 29 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 30 | #include "pycore_long.h" // _PyLong_GetZero() |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 31 | #include "pycore_symtable.h" // PySTEntryObject |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 33 | #define NEED_OPCODE_JUMP_TABLES |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 34 | #include "opcode.h" // EXTENDED_ARG |
| 35 | #include "wordcode_helpers.h" // instrsize() |
| 36 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 37 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 38 | #define DEFAULT_BLOCK_SIZE 16 |
| 39 | #define DEFAULT_BLOCKS 8 |
| 40 | #define DEFAULT_CODE_SIZE 128 |
| 41 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 42 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 43 | #define COMP_GENEXP 0 |
| 44 | #define COMP_LISTCOMP 1 |
| 45 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 46 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 47 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 48 | /* A soft limit for stack use, to avoid excessive |
| 49 | * memory use for large constants, etc. |
| 50 | * |
| 51 | * The value 30 is plucked out of thin air. |
| 52 | * Code that could use more stack than this is |
| 53 | * rare, so the exact value is unimportant. |
| 54 | */ |
| 55 | #define STACK_USE_GUIDELINE 30 |
| 56 | |
| 57 | /* If we exceed this limit, it should |
| 58 | * be considered a compiler bug. |
| 59 | * Currently it should be impossible |
| 60 | * to exceed STACK_USE_GUIDELINE * 100, |
| 61 | * as 100 is the maximum parse depth. |
| 62 | * For performance reasons we will |
| 63 | * want to reduce this to a |
| 64 | * few hundred in the future. |
| 65 | * |
| 66 | * NOTE: Whatever MAX_ALLOWED_STACK_USE is |
| 67 | * set to, it should never restrict what Python |
| 68 | * we can write, just how we compile it. |
| 69 | */ |
| 70 | #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100) |
| 71 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 72 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 73 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 74 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 75 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 76 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | unsigned char i_opcode; |
| 78 | int i_oparg; |
| 79 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 80 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 83 | #define LOG_BITS_PER_INT 5 |
| 84 | #define MASK_LOW_LOG_BITS 31 |
| 85 | |
| 86 | static inline int |
| 87 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 88 | /* Is the relevant bit set in the relevant word? */ |
| 89 | /* 256 bits fit into 8 32-bits words. |
| 90 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 91 | * Bit within word is the low bits of bitindex. |
| 92 | */ |
| 93 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 94 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 95 | } |
| 96 | |
| 97 | static inline int |
| 98 | is_relative_jump(struct instr *i) |
| 99 | { |
| 100 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 101 | } |
| 102 | |
| 103 | static inline int |
| 104 | is_jump(struct instr *i) |
| 105 | { |
| 106 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 107 | } |
| 108 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 110 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 111 | reverse order that the block are allocated. b_list points to the next |
| 112 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | struct basicblock_ *b_list; |
| 114 | /* number of instructions used */ |
| 115 | int b_iused; |
| 116 | /* length of instruction array (b_instr) */ |
| 117 | int b_ialloc; |
| 118 | /* pointer to an array of instructions, initially NULL */ |
| 119 | struct instr *b_instr; |
| 120 | /* If b_next is non-NULL, it is a pointer to the next |
| 121 | block reached by normal control flow. */ |
| 122 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 124 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 125 | /* Number of predecssors that a block has. */ |
| 126 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 127 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 128 | unsigned b_nofallthrough : 1; |
| 129 | /* Basic block exits scope (it ends with a return or raise) */ |
| 130 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 132 | int b_startdepth; |
| 133 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 134 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 135 | } basicblock; |
| 136 | |
| 137 | /* fblockinfo tracks the current frame block. |
| 138 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 139 | A frame block is used to handle loops, try/except, and try/finally. |
| 140 | It's called a frame block to distinguish it from a basic block in the |
| 141 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 142 | */ |
| 143 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 144 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 145 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER, |
| 146 | ASYNC_COMPREHENSION_GENERATOR }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 147 | |
| 148 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | enum fblocktype fb_type; |
| 150 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 151 | /* (optional) type-specific exit or cleanup block */ |
| 152 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 153 | /* (optional) additional information required for unwinding */ |
| 154 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 157 | enum { |
| 158 | COMPILER_SCOPE_MODULE, |
| 159 | COMPILER_SCOPE_CLASS, |
| 160 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 161 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 162 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 163 | COMPILER_SCOPE_COMPREHENSION, |
| 164 | }; |
| 165 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 166 | /* The following items change on entry and exit of code blocks. |
| 167 | They must be saved and restored when returning to a block. |
| 168 | */ |
| 169 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 173 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 174 | int u_scope_type; |
| 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | /* The following fields are dicts that map objects to |
| 177 | the index of them in co_XXX. The index is used as |
| 178 | the argument for opcodes that refer to those collections. |
| 179 | */ |
| 180 | PyObject *u_consts; /* all constants */ |
| 181 | PyObject *u_names; /* all names */ |
| 182 | PyObject *u_varnames; /* local variables */ |
| 183 | PyObject *u_cellvars; /* cell variables */ |
| 184 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 188 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 189 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 190 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | /* Pointer to the most recently allocated block. By following b_list |
| 192 | members, you can reach all early allocated blocks. */ |
| 193 | basicblock *u_blocks; |
| 194 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | int u_nfblocks; |
| 197 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | int u_firstlineno; /* the first lineno of the block */ |
| 200 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 201 | int u_col_offset; /* the offset of the current stmt */ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 202 | int u_end_lineno; /* the end line of the current stmt */ |
| 203 | int u_end_col_offset; /* the end offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 207 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 208 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | 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] | 210 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 211 | |
| 212 | Note that we don't track recursion levels during compilation - the |
| 213 | task of detecting and rejecting excessive levels of nesting is |
| 214 | handled by the symbol analysis pass. |
| 215 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 216 | */ |
| 217 | |
| 218 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 219 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | struct symtable *c_st; |
| 221 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 222 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 224 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | int c_interactive; /* true if in interactive mode */ |
| 226 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 227 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 228 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | struct compiler_unit *u; /* compiler state for current block */ |
| 230 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 231 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 234 | typedef struct { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 235 | // A list of strings corresponding to name captures. It is used to track: |
| 236 | // - Repeated name assignments in the same pattern. |
| 237 | // - Different name assignments in alternatives. |
| 238 | // - The order of name assignments in alternatives. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 239 | PyObject *stores; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 240 | // If 0, any name captures against our subject will raise. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 241 | int allow_irrefutable; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 242 | // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop |
| 243 | // i items off of the stack. The end result looks like this (with each block |
| 244 | // falling through to the next): |
| 245 | // fail_pop[4]: POP_TOP |
| 246 | // fail_pop[3]: POP_TOP |
| 247 | // fail_pop[2]: POP_TOP |
| 248 | // fail_pop[1]: POP_TOP |
| 249 | // fail_pop[0]: NOP |
| 250 | basicblock **fail_pop; |
| 251 | // The current length of fail_pop. |
| 252 | Py_ssize_t fail_pop_size; |
| 253 | // The number of items on top of the stack that need to *stay* on top of the |
| 254 | // stack. Variable captures go beneath these. All of them will be popped on |
| 255 | // failure. |
| 256 | Py_ssize_t on_top; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 257 | } pattern_context; |
| 258 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 259 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 260 | static void compiler_free(struct compiler *); |
| 261 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 262 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 263 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 264 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 265 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 266 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 267 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 268 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 269 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 270 | |
| 271 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 272 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 273 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 274 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 275 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 276 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 277 | static int compiler_subscript(struct compiler *, expr_ty); |
| 278 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 279 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 280 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 281 | 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] | 282 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 283 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 284 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 285 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 286 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 287 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 288 | asdl_expr_seq *args, |
| 289 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 290 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 291 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 292 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 293 | static int compiler_sync_comprehension_generator( |
| 294 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 295 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 296 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 297 | expr_ty elt, expr_ty val, int type); |
| 298 | |
| 299 | static int compiler_async_comprehension_generator( |
| 300 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 301 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 302 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 303 | expr_ty elt, expr_ty val, int type); |
| 304 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 305 | static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 306 | static int compiler_match(struct compiler *, stmt_ty); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 307 | static int compiler_pattern_subpattern(struct compiler *, pattern_ty, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 308 | pattern_context *); |
| 309 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 310 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 311 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 312 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 313 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 314 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 315 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 316 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | /* Name mangling: __private becomes _classname__private. |
| 319 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 320 | PyObject *result; |
| 321 | size_t nlen, plen, ipriv; |
| 322 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 324 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 325 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | Py_INCREF(ident); |
| 327 | return ident; |
| 328 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 329 | nlen = PyUnicode_GET_LENGTH(ident); |
| 330 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | The only time a name with a dot can occur is when |
| 334 | we are compiling an import statement that has a |
| 335 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | TODO(jhylton): Decide whether we want to support |
| 338 | mangling of the module name, e.g. __M.X. |
| 339 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 340 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 341 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 342 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | Py_INCREF(ident); |
| 344 | return ident; /* Don't mangle __whatever__ */ |
| 345 | } |
| 346 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 347 | ipriv = 0; |
| 348 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 349 | ipriv++; |
| 350 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | Py_INCREF(ident); |
| 352 | return ident; /* Don't mangle if class is just underscores */ |
| 353 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 354 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 356 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 357 | PyErr_SetString(PyExc_OverflowError, |
| 358 | "private identifier too large to be mangled"); |
| 359 | return NULL; |
| 360 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 361 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 362 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 363 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 364 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 365 | |
| 366 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 367 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 369 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 370 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 371 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 372 | Py_DECREF(result); |
| 373 | return NULL; |
| 374 | } |
| 375 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 376 | Py_DECREF(result); |
| 377 | return NULL; |
| 378 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 379 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 380 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 383 | static int |
| 384 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 387 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 388 | c->c_const_cache = PyDict_New(); |
| 389 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | c->c_stack = PyList_New(0); |
| 394 | if (!c->c_stack) { |
| 395 | Py_CLEAR(c->c_const_cache); |
| 396 | return 0; |
| 397 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | PyCodeObject * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 403 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 404 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 405 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | struct compiler c; |
| 407 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 408 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | if (!__doc__) { |
| 412 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 413 | if (!__doc__) |
| 414 | return NULL; |
| 415 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 416 | if (!__annotations__) { |
| 417 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 418 | if (!__annotations__) |
| 419 | return NULL; |
| 420 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | if (!compiler_init(&c)) |
| 422 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 423 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | c.c_filename = filename; |
| 425 | c.c_arena = arena; |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 426 | c.c_future = _PyFuture_FromAST(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | if (c.c_future == NULL) |
| 428 | goto finally; |
| 429 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | flags = &local_flags; |
| 431 | } |
| 432 | merged = c.c_future->ff_features | flags->cf_flags; |
| 433 | c.c_future->ff_features = merged; |
| 434 | flags->cf_flags = merged; |
| 435 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 436 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 438 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 439 | _PyASTOptimizeState state; |
| 440 | state.optimize = c.c_optimize; |
| 441 | state.ff_features = merged; |
| 442 | |
| 443 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 444 | goto finally; |
| 445 | } |
| 446 | |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 447 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | if (c.c_st == NULL) { |
| 449 | if (!PyErr_Occurred()) |
| 450 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 451 | goto finally; |
| 452 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 455 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 456 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | compiler_free(&c); |
| 458 | assert(co || PyErr_Occurred()); |
| 459 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 462 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 463 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | if (c->c_st) |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 466 | _PySymtable_Free(c->c_st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | if (c->c_future) |
| 468 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 469 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 470 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 474 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 475 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | Py_ssize_t i, n; |
| 478 | PyObject *v, *k; |
| 479 | PyObject *dict = PyDict_New(); |
| 480 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | n = PyList_Size(list); |
| 483 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 484 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | if (!v) { |
| 486 | Py_DECREF(dict); |
| 487 | return NULL; |
| 488 | } |
| 489 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 490 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | Py_DECREF(v); |
| 492 | Py_DECREF(dict); |
| 493 | return NULL; |
| 494 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | Py_DECREF(v); |
| 496 | } |
| 497 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /* Return new dict containing names from src that match scope(s). |
| 501 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 502 | 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] | 503 | 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] | 504 | values are integers, starting at offset and increasing by one for |
| 505 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 506 | */ |
| 507 | |
| 508 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 509 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 510 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 511 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 513 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | assert(offset >= 0); |
| 516 | if (dest == NULL) |
| 517 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 518 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 519 | /* Sort the keys so that we have a deterministic order on the indexes |
| 520 | saved in the returned dictionary. These indexes are used as indexes |
| 521 | into the free and cell var storage. Therefore if they aren't |
| 522 | deterministic, then the generated bytecode is not deterministic. |
| 523 | */ |
| 524 | sorted_keys = PyDict_Keys(src); |
| 525 | if (sorted_keys == NULL) |
| 526 | return NULL; |
| 527 | if (PyList_Sort(sorted_keys) != 0) { |
| 528 | Py_DECREF(sorted_keys); |
| 529 | return NULL; |
| 530 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 531 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 532 | |
| 533 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | /* XXX this should probably be a macro in symtable.h */ |
| 535 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 536 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 537 | v = PyDict_GetItemWithError(src, k); |
| 538 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | vi = PyLong_AS_LONG(v); |
| 540 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 543 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 545 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | Py_DECREF(dest); |
| 547 | return NULL; |
| 548 | } |
| 549 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 550 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 551 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | Py_DECREF(item); |
| 553 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | return NULL; |
| 555 | } |
| 556 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 559 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | static void |
| 564 | compiler_unit_check(struct compiler_unit *u) |
| 565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | basicblock *block; |
| 567 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 568 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | if (block->b_instr != NULL) { |
| 570 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 571 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | assert(block->b_ialloc >= block->b_iused); |
| 573 | } |
| 574 | else { |
| 575 | assert (block->b_iused == 0); |
| 576 | assert (block->b_ialloc == 0); |
| 577 | } |
| 578 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static void |
| 582 | compiler_unit_free(struct compiler_unit *u) |
| 583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | compiler_unit_check(u); |
| 587 | b = u->u_blocks; |
| 588 | while (b != NULL) { |
| 589 | if (b->b_instr) |
| 590 | PyObject_Free((void *)b->b_instr); |
| 591 | next = b->b_list; |
| 592 | PyObject_Free((void *)b); |
| 593 | b = next; |
| 594 | } |
| 595 | Py_CLEAR(u->u_ste); |
| 596 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 597 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | Py_CLEAR(u->u_consts); |
| 599 | Py_CLEAR(u->u_names); |
| 600 | Py_CLEAR(u->u_varnames); |
| 601 | Py_CLEAR(u->u_freevars); |
| 602 | Py_CLEAR(u->u_cellvars); |
| 603 | Py_CLEAR(u->u_private); |
| 604 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 608 | compiler_enter_scope(struct compiler *c, identifier name, |
| 609 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 612 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 613 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 614 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | struct compiler_unit)); |
| 616 | if (!u) { |
| 617 | PyErr_NoMemory(); |
| 618 | return 0; |
| 619 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 620 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 622 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | u->u_kwonlyargcount = 0; |
| 624 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 625 | if (!u->u_ste) { |
| 626 | compiler_unit_free(u); |
| 627 | return 0; |
| 628 | } |
| 629 | Py_INCREF(name); |
| 630 | u->u_name = name; |
| 631 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 632 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 633 | if (!u->u_varnames || !u->u_cellvars) { |
| 634 | compiler_unit_free(u); |
| 635 | return 0; |
| 636 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 637 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 638 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 639 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 640 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 641 | int res; |
| 642 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 643 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 644 | name = _PyUnicode_FromId(&PyId___class__); |
| 645 | if (!name) { |
| 646 | compiler_unit_free(u); |
| 647 | return 0; |
| 648 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 649 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 650 | if (res < 0) { |
| 651 | compiler_unit_free(u); |
| 652 | return 0; |
| 653 | } |
| 654 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | 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] | 657 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | if (!u->u_freevars) { |
| 659 | compiler_unit_free(u); |
| 660 | return 0; |
| 661 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | u->u_blocks = NULL; |
| 664 | u->u_nfblocks = 0; |
| 665 | u->u_firstlineno = lineno; |
| 666 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 667 | u->u_col_offset = 0; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 668 | u->u_end_lineno = 0; |
| 669 | u->u_end_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | u->u_consts = PyDict_New(); |
| 671 | if (!u->u_consts) { |
| 672 | compiler_unit_free(u); |
| 673 | return 0; |
| 674 | } |
| 675 | u->u_names = PyDict_New(); |
| 676 | if (!u->u_names) { |
| 677 | compiler_unit_free(u); |
| 678 | return 0; |
| 679 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | /* Push the old compiler_unit on the stack. */ |
| 684 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 685 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 687 | Py_XDECREF(capsule); |
| 688 | compiler_unit_free(u); |
| 689 | return 0; |
| 690 | } |
| 691 | Py_DECREF(capsule); |
| 692 | u->u_private = c->u->u_private; |
| 693 | Py_XINCREF(u->u_private); |
| 694 | } |
| 695 | c->u = u; |
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++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 698 | |
| 699 | block = compiler_new_block(c); |
| 700 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 702 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 703 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 704 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 705 | if (!compiler_set_qualname(c)) |
| 706 | return 0; |
| 707 | } |
| 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 712 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 713 | compiler_exit_scope(struct compiler *c) |
| 714 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 715 | // Don't call PySequence_DelItem() with an exception raised |
| 716 | PyObject *exc_type, *exc_val, *exc_tb; |
| 717 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | c->c_nestlevel--; |
| 720 | compiler_unit_free(c->u); |
| 721 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 722 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 724 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 725 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | assert(c->u); |
| 727 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 728 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 729 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 730 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 731 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | compiler_unit_check(c->u); |
| 733 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 734 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 736 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 737 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 738 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 741 | static int |
| 742 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 743 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 744 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 745 | _Py_static_string(dot_locals, ".<locals>"); |
| 746 | Py_ssize_t stack_size; |
| 747 | struct compiler_unit *u = c->u; |
| 748 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 749 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 750 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 751 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 752 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 753 | if (stack_size > 1) { |
| 754 | int scope, force_global = 0; |
| 755 | struct compiler_unit *parent; |
| 756 | PyObject *mangled, *capsule; |
| 757 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 758 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 759 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 760 | assert(parent); |
| 761 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 762 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 763 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 764 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 765 | assert(u->u_name); |
| 766 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 767 | if (!mangled) |
| 768 | return 0; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 769 | scope = _PyST_GetScope(parent->u_ste, mangled); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 770 | Py_DECREF(mangled); |
| 771 | assert(scope != GLOBAL_IMPLICIT); |
| 772 | if (scope == GLOBAL_EXPLICIT) |
| 773 | force_global = 1; |
| 774 | } |
| 775 | |
| 776 | if (!force_global) { |
| 777 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 778 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 779 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 780 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 781 | if (dot_locals_str == NULL) |
| 782 | return 0; |
| 783 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 784 | if (base == NULL) |
| 785 | return 0; |
| 786 | } |
| 787 | else { |
| 788 | Py_INCREF(parent->u_qualname); |
| 789 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 790 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 791 | } |
| 792 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 793 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 794 | if (base != NULL) { |
| 795 | dot_str = _PyUnicode_FromId(&dot); |
| 796 | if (dot_str == NULL) { |
| 797 | Py_DECREF(base); |
| 798 | return 0; |
| 799 | } |
| 800 | name = PyUnicode_Concat(base, dot_str); |
| 801 | Py_DECREF(base); |
| 802 | if (name == NULL) |
| 803 | return 0; |
| 804 | PyUnicode_Append(&name, u->u_name); |
| 805 | if (name == NULL) |
| 806 | return 0; |
| 807 | } |
| 808 | else { |
| 809 | Py_INCREF(u->u_name); |
| 810 | name = u->u_name; |
| 811 | } |
| 812 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 813 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 814 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 817 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 818 | /* Allocate a new block and return a pointer to it. |
| 819 | Returns NULL on error. |
| 820 | */ |
| 821 | |
| 822 | static basicblock * |
| 823 | compiler_new_block(struct compiler *c) |
| 824 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | basicblock *b; |
| 826 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 829 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | if (b == NULL) { |
| 831 | PyErr_NoMemory(); |
| 832 | return NULL; |
| 833 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | /* Extend the singly linked list of blocks with new block. */ |
| 835 | b->b_list = u->u_blocks; |
| 836 | u->u_blocks = b; |
| 837 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 840 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 841 | compiler_next_block(struct compiler *c) |
| 842 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | basicblock *block = compiler_new_block(c); |
| 844 | if (block == NULL) |
| 845 | return NULL; |
| 846 | c->u->u_curblock->b_next = block; |
| 847 | c->u->u_curblock = block; |
| 848 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static basicblock * |
| 852 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | assert(block != NULL); |
| 855 | c->u->u_curblock->b_next = block; |
| 856 | c->u->u_curblock = block; |
| 857 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 860 | static basicblock * |
| 861 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 862 | { |
| 863 | /* Cannot copy a block if it has a fallthrough, since |
| 864 | * a block can only have one fallthrough predecessor. |
| 865 | */ |
| 866 | assert(block->b_nofallthrough); |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 867 | basicblock *result = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 868 | if (result == NULL) { |
| 869 | return NULL; |
| 870 | } |
| 871 | for (int i = 0; i < block->b_iused; i++) { |
| 872 | int n = compiler_next_instr(result); |
| 873 | if (n < 0) { |
| 874 | return NULL; |
| 875 | } |
| 876 | result->b_instr[n] = block->b_instr[i]; |
| 877 | } |
| 878 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 879 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 880 | return result; |
| 881 | } |
| 882 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 883 | /* Returns the offset of the next instruction in the current block's |
| 884 | b_instr array. Resizes the b_instr as necessary. |
| 885 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 886 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 887 | |
| 888 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 889 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | assert(b != NULL); |
| 892 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 893 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 894 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | if (b->b_instr == NULL) { |
| 896 | PyErr_NoMemory(); |
| 897 | return -1; |
| 898 | } |
| 899 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | } |
| 901 | else if (b->b_iused == b->b_ialloc) { |
| 902 | struct instr *tmp; |
| 903 | size_t oldsize, newsize; |
| 904 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 905 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 906 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 907 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | PyErr_NoMemory(); |
| 909 | return -1; |
| 910 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | if (newsize == 0) { |
| 913 | PyErr_NoMemory(); |
| 914 | return -1; |
| 915 | } |
| 916 | b->b_ialloc <<= 1; |
| 917 | tmp = (struct instr *)PyObject_Realloc( |
| 918 | (void *)b->b_instr, newsize); |
| 919 | if (tmp == NULL) { |
| 920 | PyErr_NoMemory(); |
| 921 | return -1; |
| 922 | } |
| 923 | b->b_instr = tmp; |
| 924 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 925 | } |
| 926 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 929 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 930 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 931 | The line number is reset in the following cases: |
| 932 | - when entering a new scope |
| 933 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 934 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 935 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 936 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 937 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 938 | #define SET_LOC(c, x) \ |
| 939 | (c)->u->u_lineno = (x)->lineno; \ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 940 | (c)->u->u_col_offset = (x)->col_offset; \ |
| 941 | (c)->u->u_end_lineno = (x)->end_lineno; \ |
| 942 | (c)->u->u_end_col_offset = (x)->end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 943 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 944 | /* Return the stack effect of opcode with argument oparg. |
| 945 | |
| 946 | Some opcodes have different stack effect when jump to the target and |
| 947 | when not jump. The 'jump' parameter specifies the case: |
| 948 | |
| 949 | * 0 -- when not jump |
| 950 | * 1 -- when jump |
| 951 | * -1 -- maximal |
| 952 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 953 | static int |
| 954 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 957 | case NOP: |
| 958 | case EXTENDED_ARG: |
| 959 | return 0; |
| 960 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 961 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | case POP_TOP: |
| 963 | return -1; |
| 964 | case ROT_TWO: |
| 965 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 966 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | case DUP_TOP: |
| 969 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 970 | case DUP_TOP_TWO: |
| 971 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 972 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 973 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | case UNARY_POSITIVE: |
| 975 | case UNARY_NEGATIVE: |
| 976 | case UNARY_NOT: |
| 977 | case UNARY_INVERT: |
| 978 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | case SET_ADD: |
| 981 | case LIST_APPEND: |
| 982 | return -1; |
| 983 | case MAP_ADD: |
| 984 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 985 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 986 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | case BINARY_POWER: |
| 988 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 989 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | case BINARY_MODULO: |
| 991 | case BINARY_ADD: |
| 992 | case BINARY_SUBTRACT: |
| 993 | case BINARY_SUBSCR: |
| 994 | case BINARY_FLOOR_DIVIDE: |
| 995 | case BINARY_TRUE_DIVIDE: |
| 996 | return -1; |
| 997 | case INPLACE_FLOOR_DIVIDE: |
| 998 | case INPLACE_TRUE_DIVIDE: |
| 999 | return -1; |
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 INPLACE_ADD: |
| 1002 | case INPLACE_SUBTRACT: |
| 1003 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1004 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | case INPLACE_MODULO: |
| 1006 | return -1; |
| 1007 | case STORE_SUBSCR: |
| 1008 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | case DELETE_SUBSCR: |
| 1010 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | case BINARY_LSHIFT: |
| 1013 | case BINARY_RSHIFT: |
| 1014 | case BINARY_AND: |
| 1015 | case BINARY_XOR: |
| 1016 | case BINARY_OR: |
| 1017 | return -1; |
| 1018 | case INPLACE_POWER: |
| 1019 | return -1; |
| 1020 | case GET_ITER: |
| 1021 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | case PRINT_EXPR: |
| 1024 | return -1; |
| 1025 | case LOAD_BUILD_CLASS: |
| 1026 | return 1; |
| 1027 | case INPLACE_LSHIFT: |
| 1028 | case INPLACE_RSHIFT: |
| 1029 | case INPLACE_AND: |
| 1030 | case INPLACE_XOR: |
| 1031 | case INPLACE_OR: |
| 1032 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1035 | /* 1 in the normal flow. |
| 1036 | * Restore the stack position and push 6 values before jumping to |
| 1037 | * the handler if an exception be raised. */ |
| 1038 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | case RETURN_VALUE: |
| 1040 | return -1; |
| 1041 | case IMPORT_STAR: |
| 1042 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1043 | case SETUP_ANNOTATIONS: |
| 1044 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | case YIELD_VALUE: |
| 1046 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1047 | case YIELD_FROM: |
| 1048 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case POP_BLOCK: |
| 1050 | return 0; |
| 1051 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1052 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | case STORE_NAME: |
| 1055 | return -1; |
| 1056 | case DELETE_NAME: |
| 1057 | return 0; |
| 1058 | case UNPACK_SEQUENCE: |
| 1059 | return oparg-1; |
| 1060 | case UNPACK_EX: |
| 1061 | return (oparg&0xFF) + (oparg>>8); |
| 1062 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1063 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1064 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | case STORE_ATTR: |
| 1067 | return -2; |
| 1068 | case DELETE_ATTR: |
| 1069 | return -1; |
| 1070 | case STORE_GLOBAL: |
| 1071 | return -1; |
| 1072 | case DELETE_GLOBAL: |
| 1073 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | case LOAD_CONST: |
| 1075 | return 1; |
| 1076 | case LOAD_NAME: |
| 1077 | return 1; |
| 1078 | case BUILD_TUPLE: |
| 1079 | case BUILD_LIST: |
| 1080 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1081 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | return 1-oparg; |
| 1083 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1084 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1085 | case BUILD_CONST_KEY_MAP: |
| 1086 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | case LOAD_ATTR: |
| 1088 | return 0; |
| 1089 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1090 | case IS_OP: |
| 1091 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1093 | case JUMP_IF_NOT_EXC_MATCH: |
| 1094 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | case IMPORT_NAME: |
| 1096 | return -1; |
| 1097 | case IMPORT_FROM: |
| 1098 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1099 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1100 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | case JUMP_ABSOLUTE: |
| 1103 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1104 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1105 | case JUMP_IF_TRUE_OR_POP: |
| 1106 | case JUMP_IF_FALSE_OR_POP: |
| 1107 | return jump ? 0 : -1; |
| 1108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | case POP_JUMP_IF_FALSE: |
| 1110 | case POP_JUMP_IF_TRUE: |
| 1111 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | case LOAD_GLOBAL: |
| 1114 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1115 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1116 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1118 | /* 0 in the normal flow. |
| 1119 | * Restore the stack position and push 6 values before jumping to |
| 1120 | * the handler if an exception be raised. */ |
| 1121 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1122 | case RERAISE: |
| 1123 | return -3; |
| 1124 | |
| 1125 | case WITH_EXCEPT_START: |
| 1126 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | case LOAD_FAST: |
| 1129 | return 1; |
| 1130 | case STORE_FAST: |
| 1131 | return -1; |
| 1132 | case DELETE_FAST: |
| 1133 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | case RAISE_VARARGS: |
| 1136 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1140 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1141 | case CALL_METHOD: |
| 1142 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1144 | return -oparg-1; |
| 1145 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1146 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1147 | case MAKE_FUNCTION: |
| 1148 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1149 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | case BUILD_SLICE: |
| 1151 | if (oparg == 3) |
| 1152 | return -2; |
| 1153 | else |
| 1154 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1155 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1156 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | case LOAD_CLOSURE: |
| 1158 | return 1; |
| 1159 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1160 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | return 1; |
| 1162 | case STORE_DEREF: |
| 1163 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1164 | case DELETE_DEREF: |
| 1165 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1166 | |
| 1167 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1168 | case GET_AWAITABLE: |
| 1169 | return 0; |
| 1170 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1171 | /* 0 in the normal flow. |
| 1172 | * Restore the stack position to the position before the result |
| 1173 | * of __aenter__ and push 6 values before jumping to the handler |
| 1174 | * if an exception be raised. */ |
| 1175 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1176 | case BEFORE_ASYNC_WITH: |
| 1177 | return 1; |
| 1178 | case GET_AITER: |
| 1179 | return 0; |
| 1180 | case GET_ANEXT: |
| 1181 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1182 | case GET_YIELD_FROM_ITER: |
| 1183 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1184 | case END_ASYNC_FOR: |
| 1185 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1186 | case FORMAT_VALUE: |
| 1187 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1188 | else 1->1. */ |
| 1189 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1190 | case LOAD_METHOD: |
| 1191 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1192 | case LOAD_ASSERTION_ERROR: |
| 1193 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1194 | case LIST_TO_TUPLE: |
| 1195 | return 0; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 1196 | case GEN_START: |
| 1197 | return -1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1198 | case LIST_EXTEND: |
| 1199 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1200 | case DICT_MERGE: |
| 1201 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1202 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1203 | case COPY_DICT_WITHOUT_KEYS: |
| 1204 | return 0; |
| 1205 | case MATCH_CLASS: |
| 1206 | return -1; |
| 1207 | case GET_LEN: |
| 1208 | case MATCH_MAPPING: |
| 1209 | case MATCH_SEQUENCE: |
| 1210 | return 1; |
| 1211 | case MATCH_KEYS: |
| 1212 | return 2; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 1213 | case ROT_N: |
| 1214 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1216 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1218 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1221 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1222 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1223 | { |
| 1224 | return stack_effect(opcode, oparg, jump); |
| 1225 | } |
| 1226 | |
| 1227 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1228 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1229 | { |
| 1230 | return stack_effect(opcode, oparg, -1); |
| 1231 | } |
| 1232 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1233 | /* Add an opcode with no argument. |
| 1234 | Returns 0 on failure, 1 on success. |
| 1235 | */ |
| 1236 | |
| 1237 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1238 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | basicblock *b; |
| 1241 | struct instr *i; |
| 1242 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1243 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1244 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | if (off < 0) |
| 1246 | return 0; |
| 1247 | b = c->u->u_curblock; |
| 1248 | i = &b->b_instr[off]; |
| 1249 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1250 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | if (opcode == RETURN_VALUE) |
| 1252 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1253 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1257 | static int |
| 1258 | compiler_addop(struct compiler *c, int opcode) |
| 1259 | { |
| 1260 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1261 | } |
| 1262 | |
| 1263 | static int |
| 1264 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1265 | { |
| 1266 | return compiler_addop_line(c, opcode, -1); |
| 1267 | } |
| 1268 | |
| 1269 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1270 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1271 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1272 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1273 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1275 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1276 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1278 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1280 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1281 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1282 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | return -1; |
| 1285 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1286 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | Py_DECREF(v); |
| 1288 | return -1; |
| 1289 | } |
| 1290 | Py_DECREF(v); |
| 1291 | } |
| 1292 | else |
| 1293 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1294 | return arg; |
| 1295 | } |
| 1296 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1297 | // Merge const *o* recursively and return constant key object. |
| 1298 | static PyObject* |
| 1299 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1300 | { |
| 1301 | // None and Ellipsis are singleton, and key is the singleton. |
| 1302 | // No need to merge object and key. |
| 1303 | if (o == Py_None || o == Py_Ellipsis) { |
| 1304 | Py_INCREF(o); |
| 1305 | return o; |
| 1306 | } |
| 1307 | |
| 1308 | PyObject *key = _PyCode_ConstantKey(o); |
| 1309 | if (key == NULL) { |
| 1310 | return NULL; |
| 1311 | } |
| 1312 | |
| 1313 | // t is borrowed reference |
| 1314 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1315 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1316 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1317 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1318 | Py_DECREF(key); |
| 1319 | return t; |
| 1320 | } |
| 1321 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1322 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1323 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1324 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1325 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1326 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1327 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1328 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1329 | PyObject *u = merge_consts_recursive(c, item); |
| 1330 | if (u == NULL) { |
| 1331 | Py_DECREF(key); |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | |
| 1335 | // See _PyCode_ConstantKey() |
| 1336 | PyObject *v; // borrowed |
| 1337 | if (PyTuple_CheckExact(u)) { |
| 1338 | v = PyTuple_GET_ITEM(u, 1); |
| 1339 | } |
| 1340 | else { |
| 1341 | v = u; |
| 1342 | } |
| 1343 | if (v != item) { |
| 1344 | Py_INCREF(v); |
| 1345 | PyTuple_SET_ITEM(o, i, v); |
| 1346 | Py_DECREF(item); |
| 1347 | } |
| 1348 | |
| 1349 | Py_DECREF(u); |
| 1350 | } |
| 1351 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1352 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1353 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1354 | // constant keys. |
| 1355 | // See _PyCode_ConstantKey() for detail. |
| 1356 | assert(PyTuple_CheckExact(key)); |
| 1357 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1358 | |
| 1359 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1360 | if (len == 0) { // empty frozenset should not be re-created. |
| 1361 | return key; |
| 1362 | } |
| 1363 | PyObject *tuple = PyTuple_New(len); |
| 1364 | if (tuple == NULL) { |
| 1365 | Py_DECREF(key); |
| 1366 | return NULL; |
| 1367 | } |
| 1368 | Py_ssize_t i = 0, pos = 0; |
| 1369 | PyObject *item; |
| 1370 | Py_hash_t hash; |
| 1371 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1372 | PyObject *k = merge_consts_recursive(c, item); |
| 1373 | if (k == NULL) { |
| 1374 | Py_DECREF(tuple); |
| 1375 | Py_DECREF(key); |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | PyObject *u; |
| 1379 | if (PyTuple_CheckExact(k)) { |
| 1380 | u = PyTuple_GET_ITEM(k, 1); |
| 1381 | Py_INCREF(u); |
| 1382 | Py_DECREF(k); |
| 1383 | } |
| 1384 | else { |
| 1385 | u = k; |
| 1386 | } |
| 1387 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1388 | i++; |
| 1389 | } |
| 1390 | |
| 1391 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1392 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1393 | PyObject *new = PyFrozenSet_New(tuple); |
| 1394 | Py_DECREF(tuple); |
| 1395 | if (new == NULL) { |
| 1396 | Py_DECREF(key); |
| 1397 | return NULL; |
| 1398 | } |
| 1399 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1400 | Py_DECREF(o); |
| 1401 | PyTuple_SET_ITEM(key, 1, new); |
| 1402 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1403 | |
| 1404 | return key; |
| 1405 | } |
| 1406 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1407 | static Py_ssize_t |
| 1408 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1409 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1410 | PyObject *key = merge_consts_recursive(c, o); |
| 1411 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1412 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1413 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1414 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1415 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1416 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1421 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1422 | { |
| 1423 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1424 | if (arg < 0) |
| 1425 | return 0; |
| 1426 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1427 | } |
| 1428 | |
| 1429 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1430 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1431 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1432 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1433 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1434 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1435 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1436 | return compiler_addop_i(c, opcode, arg); |
| 1437 | } |
| 1438 | |
| 1439 | static int |
| 1440 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1442 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1443 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1444 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1445 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1446 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1447 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1448 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1449 | Py_DECREF(mangled); |
| 1450 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1451 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1452 | return compiler_addop_i(c, opcode, arg); |
| 1453 | } |
| 1454 | |
| 1455 | /* Add an opcode with an integer argument. |
| 1456 | Returns 0 on failure, 1 on success. |
| 1457 | */ |
| 1458 | |
| 1459 | static int |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1460 | 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] | 1461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | struct instr *i; |
| 1463 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1464 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1465 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1466 | it in the C code (like Python/ceval.c). |
| 1467 | |
| 1468 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1469 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1470 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1471 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1472 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1473 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1474 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1475 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | if (off < 0) |
| 1477 | return 0; |
| 1478 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1479 | i->i_opcode = opcode; |
| 1480 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1481 | i->i_lineno = lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1485 | static int |
| 1486 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1487 | { |
| 1488 | return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno); |
| 1489 | } |
| 1490 | |
| 1491 | static int |
| 1492 | compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1493 | { |
| 1494 | return compiler_addop_i_line(c, opcode, oparg, -1); |
| 1495 | } |
| 1496 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1497 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1498 | { |
| 1499 | assert(HAS_ARG(opcode)); |
| 1500 | assert(b != NULL); |
| 1501 | assert(target != NULL); |
| 1502 | |
| 1503 | int off = compiler_next_instr(b); |
| 1504 | struct instr *i = &b->b_instr[off]; |
| 1505 | if (off < 0) { |
| 1506 | return 0; |
| 1507 | } |
| 1508 | i->i_opcode = opcode; |
| 1509 | i->i_target = target; |
| 1510 | i->i_lineno = lineno; |
| 1511 | return 1; |
| 1512 | } |
| 1513 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1514 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1515 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1517 | 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] | 1518 | } |
| 1519 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1520 | static int |
| 1521 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1522 | { |
| 1523 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1524 | } |
| 1525 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1526 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1527 | to the new block. |
| 1528 | |
| 1529 | The returns inside this macro make it impossible to decref objects |
| 1530 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1531 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | if (compiler_next_block((C)) == NULL) \ |
| 1534 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | if (!compiler_addop((C), (OP))) \ |
| 1539 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1542 | #define ADDOP_NOLINE(C, OP) { \ |
| 1543 | if (!compiler_addop_noline((C), (OP))) \ |
| 1544 | return 0; \ |
| 1545 | } |
| 1546 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1547 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | if (!compiler_addop((C), (OP))) { \ |
| 1549 | compiler_exit_scope(c); \ |
| 1550 | return 0; \ |
| 1551 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1554 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1555 | if (!compiler_addop_load_const((C), (O))) \ |
| 1556 | return 0; \ |
| 1557 | } |
| 1558 | |
| 1559 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1560 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1561 | PyObject *__new_const = (O); \ |
| 1562 | if (__new_const == NULL) { \ |
| 1563 | return 0; \ |
| 1564 | } \ |
| 1565 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1566 | Py_DECREF(__new_const); \ |
| 1567 | return 0; \ |
| 1568 | } \ |
| 1569 | Py_DECREF(__new_const); \ |
| 1570 | } |
| 1571 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1572 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Miss Islington (bot) | ebbd0ac | 2021-08-31 11:08:32 -0700 | [diff] [blame] | 1573 | assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1575 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1578 | /* Same as ADDOP_O, but steals a reference. */ |
| 1579 | #define ADDOP_N(C, OP, O, TYPE) { \ |
Miss Islington (bot) | ebbd0ac | 2021-08-31 11:08:32 -0700 | [diff] [blame] | 1580 | assert((OP) != LOAD_CONST); /* use ADDOP_LOAD_CONST_NEW */ \ |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1581 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1582 | Py_DECREF((O)); \ |
| 1583 | return 0; \ |
| 1584 | } \ |
| 1585 | Py_DECREF((O)); \ |
| 1586 | } |
| 1587 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1588 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1590 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1595 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1598 | #define ADDOP_I_NOLINE(C, OP, O) { \ |
| 1599 | if (!compiler_addop_i_noline((C), (OP), (O))) \ |
| 1600 | return 0; \ |
| 1601 | } |
| 1602 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1603 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1604 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1608 | /* Add a jump with no line number. |
| 1609 | * Used for artificial jumps that have no corresponding |
| 1610 | * token in the source code. */ |
| 1611 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1612 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1613 | return 0; \ |
| 1614 | } |
| 1615 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1616 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1617 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1618 | return 0; \ |
| 1619 | } |
| 1620 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1621 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1622 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1623 | */ |
| 1624 | |
| 1625 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1627 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1630 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1632 | compiler_exit_scope(c); \ |
| 1633 | return 0; \ |
| 1634 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1637 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1639 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1644 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1646 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1647 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1648 | return 0; \ |
| 1649 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1652 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1654 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1656 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1657 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1658 | compiler_exit_scope(c); \ |
| 1659 | return 0; \ |
| 1660 | } \ |
| 1661 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1664 | #define RETURN_IF_FALSE(X) \ |
| 1665 | if (!(X)) { \ |
| 1666 | return 0; \ |
| 1667 | } |
| 1668 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1669 | /* Search if variable annotations are present statically in a block. */ |
| 1670 | |
| 1671 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1672 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1673 | { |
| 1674 | int i, j, res = 0; |
| 1675 | stmt_ty st; |
| 1676 | |
| 1677 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1678 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1679 | switch (st->kind) { |
| 1680 | case AnnAssign_kind: |
| 1681 | return 1; |
| 1682 | case For_kind: |
| 1683 | res = find_ann(st->v.For.body) || |
| 1684 | find_ann(st->v.For.orelse); |
| 1685 | break; |
| 1686 | case AsyncFor_kind: |
| 1687 | res = find_ann(st->v.AsyncFor.body) || |
| 1688 | find_ann(st->v.AsyncFor.orelse); |
| 1689 | break; |
| 1690 | case While_kind: |
| 1691 | res = find_ann(st->v.While.body) || |
| 1692 | find_ann(st->v.While.orelse); |
| 1693 | break; |
| 1694 | case If_kind: |
| 1695 | res = find_ann(st->v.If.body) || |
| 1696 | find_ann(st->v.If.orelse); |
| 1697 | break; |
| 1698 | case With_kind: |
| 1699 | res = find_ann(st->v.With.body); |
| 1700 | break; |
| 1701 | case AsyncWith_kind: |
| 1702 | res = find_ann(st->v.AsyncWith.body); |
| 1703 | break; |
| 1704 | case Try_kind: |
| 1705 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1706 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1707 | st->v.Try.handlers, j); |
| 1708 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1709 | return 1; |
| 1710 | } |
| 1711 | } |
| 1712 | res = find_ann(st->v.Try.body) || |
| 1713 | find_ann(st->v.Try.finalbody) || |
| 1714 | find_ann(st->v.Try.orelse); |
| 1715 | break; |
| 1716 | default: |
| 1717 | res = 0; |
| 1718 | } |
| 1719 | if (res) { |
| 1720 | break; |
| 1721 | } |
| 1722 | } |
| 1723 | return res; |
| 1724 | } |
| 1725 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1726 | /* |
| 1727 | * Frame block handling functions |
| 1728 | */ |
| 1729 | |
| 1730 | static int |
| 1731 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1732 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1733 | { |
| 1734 | struct fblockinfo *f; |
| 1735 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1736 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1737 | } |
| 1738 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1739 | f->fb_type = t; |
| 1740 | f->fb_block = b; |
| 1741 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1742 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1743 | return 1; |
| 1744 | } |
| 1745 | |
| 1746 | static void |
| 1747 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1748 | { |
| 1749 | struct compiler_unit *u = c->u; |
| 1750 | assert(u->u_nfblocks > 0); |
| 1751 | u->u_nfblocks--; |
| 1752 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1753 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1754 | } |
| 1755 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1756 | static int |
| 1757 | compiler_call_exit_with_nones(struct compiler *c) { |
Miss Islington (bot) | ebbd0ac | 2021-08-31 11:08:32 -0700 | [diff] [blame] | 1758 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1759 | ADDOP(c, DUP_TOP); |
| 1760 | ADDOP(c, DUP_TOP); |
| 1761 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1762 | return 1; |
| 1763 | } |
| 1764 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1765 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1766 | * popping the blocks will be restored afterwards, unless another |
| 1767 | * return, break or continue is found. In which case, the TOS will |
| 1768 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1769 | */ |
| 1770 | static int |
| 1771 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1772 | int preserve_tos) |
| 1773 | { |
| 1774 | switch (info->fb_type) { |
| 1775 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1776 | case EXCEPTION_HANDLER: |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 1777 | case ASYNC_COMPREHENSION_GENERATOR: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1778 | return 1; |
| 1779 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1780 | case FOR_LOOP: |
| 1781 | /* Pop the iterator */ |
| 1782 | if (preserve_tos) { |
| 1783 | ADDOP(c, ROT_TWO); |
| 1784 | } |
| 1785 | ADDOP(c, POP_TOP); |
| 1786 | return 1; |
| 1787 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1788 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1789 | ADDOP(c, POP_BLOCK); |
| 1790 | return 1; |
| 1791 | |
| 1792 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1793 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1794 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1795 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1796 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1797 | return 0; |
| 1798 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1799 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1800 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1801 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1802 | if (preserve_tos) { |
| 1803 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1804 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1805 | /* The finally block should appear to execute after the |
| 1806 | * statement causing the unwinding, so make the unwinding |
| 1807 | * instruction artificial */ |
| 1808 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1809 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1810 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1811 | case FINALLY_END: |
| 1812 | if (preserve_tos) { |
| 1813 | ADDOP(c, ROT_FOUR); |
| 1814 | } |
| 1815 | ADDOP(c, POP_TOP); |
| 1816 | ADDOP(c, POP_TOP); |
| 1817 | ADDOP(c, POP_TOP); |
| 1818 | if (preserve_tos) { |
| 1819 | ADDOP(c, ROT_FOUR); |
| 1820 | } |
| 1821 | ADDOP(c, POP_EXCEPT); |
| 1822 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1823 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1824 | case WITH: |
| 1825 | case ASYNC_WITH: |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 1826 | SET_LOC(c, (stmt_ty)info->fb_datum); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1827 | ADDOP(c, POP_BLOCK); |
| 1828 | if (preserve_tos) { |
| 1829 | ADDOP(c, ROT_TWO); |
| 1830 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1831 | if(!compiler_call_exit_with_nones(c)) { |
| 1832 | return 0; |
| 1833 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1834 | if (info->fb_type == ASYNC_WITH) { |
| 1835 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1836 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1837 | ADDOP(c, YIELD_FROM); |
| 1838 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1839 | ADDOP(c, POP_TOP); |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 1840 | /* The exit block should appear to execute after the |
| 1841 | * statement causing the unwinding, so make the unwinding |
| 1842 | * instruction artificial */ |
| 1843 | c->u->u_lineno = -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1844 | return 1; |
| 1845 | |
| 1846 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1847 | if (info->fb_datum) { |
| 1848 | ADDOP(c, POP_BLOCK); |
| 1849 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1850 | if (preserve_tos) { |
| 1851 | ADDOP(c, ROT_FOUR); |
| 1852 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1853 | ADDOP(c, POP_EXCEPT); |
| 1854 | if (info->fb_datum) { |
| 1855 | ADDOP_LOAD_CONST(c, Py_None); |
| 1856 | compiler_nameop(c, info->fb_datum, Store); |
| 1857 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1858 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1859 | return 1; |
| 1860 | |
| 1861 | case POP_VALUE: |
| 1862 | if (preserve_tos) { |
| 1863 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1864 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1865 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1866 | return 1; |
| 1867 | } |
| 1868 | Py_UNREACHABLE(); |
| 1869 | } |
| 1870 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1871 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1872 | static int |
| 1873 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1874 | if (c->u->u_nfblocks == 0) { |
| 1875 | return 1; |
| 1876 | } |
| 1877 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1878 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1879 | *loop = top; |
| 1880 | return 1; |
| 1881 | } |
| 1882 | struct fblockinfo copy = *top; |
| 1883 | c->u->u_nfblocks--; |
| 1884 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1885 | return 0; |
| 1886 | } |
| 1887 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1888 | return 0; |
| 1889 | } |
| 1890 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1891 | c->u->u_nfblocks++; |
| 1892 | return 1; |
| 1893 | } |
| 1894 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1895 | /* Compile a sequence of statements, checking for a docstring |
| 1896 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1897 | |
| 1898 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1899 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1900 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1901 | int i = 0; |
| 1902 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1903 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1904 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1905 | /* Set current line number to the line number of first statement. |
| 1906 | This way line number for SETUP_ANNOTATIONS will always |
| 1907 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1908 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1909 | 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] | 1910 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1911 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1912 | } |
| 1913 | /* Every annotated class and module should have __annotations__. */ |
| 1914 | if (find_ann(stmts)) { |
| 1915 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1916 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1917 | if (!asdl_seq_LEN(stmts)) |
| 1918 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1919 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1920 | if (c->c_optimize < 2) { |
| 1921 | docstring = _PyAST_GetDocString(stmts); |
| 1922 | if (docstring) { |
| 1923 | i = 1; |
| 1924 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1925 | assert(st->kind == Expr_kind); |
| 1926 | VISIT(c, expr, st->v.Expr.value); |
| 1927 | if (!compiler_nameop(c, __doc__, Store)) |
| 1928 | return 0; |
| 1929 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1931 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1932 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1933 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | static PyCodeObject * |
| 1937 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1938 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | PyCodeObject *co; |
| 1940 | int addNone = 1; |
| 1941 | static PyObject *module; |
| 1942 | if (!module) { |
| 1943 | module = PyUnicode_InternFromString("<module>"); |
| 1944 | if (!module) |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1948 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | return NULL; |
| 1950 | switch (mod->kind) { |
| 1951 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1952 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | compiler_exit_scope(c); |
| 1954 | return 0; |
| 1955 | } |
| 1956 | break; |
| 1957 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1958 | if (find_ann(mod->v.Interactive.body)) { |
| 1959 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1960 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1962 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | break; |
| 1964 | case Expression_kind: |
| 1965 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1966 | addNone = 0; |
| 1967 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1968 | default: |
| 1969 | PyErr_Format(PyExc_SystemError, |
| 1970 | "module kind %d should not be possible", |
| 1971 | mod->kind); |
| 1972 | return 0; |
| 1973 | } |
| 1974 | co = assemble(c, addNone); |
| 1975 | compiler_exit_scope(c); |
| 1976 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1979 | /* The test for LOCAL must come before the test for FREE in order to |
| 1980 | handle classes where name is both local and free. The local var is |
| 1981 | a method and the free var is a free var referenced within a method. |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1982 | */ |
| 1983 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1984 | static int |
| 1985 | get_ref_type(struct compiler *c, PyObject *name) |
| 1986 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1987 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1988 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1989 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1990 | return CELL; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1991 | scope = _PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1993 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1994 | "_PyST_GetScope(name=%R) failed: " |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1995 | "unknown scope in unit %S (%R); " |
| 1996 | "symbols: %R; locals: %R; globals: %R", |
| 1997 | name, |
| 1998 | c->u->u_name, c->u->u_ste->ste_id, |
| 1999 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 2000 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | static int |
| 2006 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 2007 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2008 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 2009 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2010 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 2011 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2012 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2016 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 2017 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2018 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2019 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2020 | if (qualname == NULL) |
| 2021 | qualname = co->co_name; |
| 2022 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2023 | if (free) { |
| 2024 | for (i = 0; i < free; ++i) { |
| 2025 | /* Bypass com_addop_varname because it will generate |
| 2026 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2027 | */ |
| 2028 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2029 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2030 | /* Special case: If a class contains a method with a |
| 2031 | free variable that has the same name as a method, |
| 2032 | the name will be considered free *and* local in the |
| 2033 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 2034 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2035 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2036 | int reftype = get_ref_type(c, name); |
| 2037 | if (reftype == -1) { |
| 2038 | return 0; |
| 2039 | } |
| 2040 | int arg; |
| 2041 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2042 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2043 | } |
| 2044 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2045 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2046 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2047 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2048 | PyErr_Format(PyExc_SystemError, |
| 2049 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 2050 | "freevars of code %S: %R", |
| 2051 | name, |
| 2052 | reftype, |
| 2053 | c->u->u_name, |
| 2054 | co->co_name, |
| 2055 | co->co_freevars); |
| 2056 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2057 | } |
| 2058 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2060 | flags |= 0x08; |
| 2061 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2063 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 2064 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2065 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2070 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | if (!decos) |
| 2075 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2077 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2078 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2079 | } |
| 2080 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2084 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2085 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2086 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2087 | /* Push a dict of keyword-only default values. |
| 2088 | |
| 2089 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2090 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2091 | int i; |
| 2092 | PyObject *keys = NULL; |
| 2093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2095 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2096 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2097 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2098 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2099 | if (!mangled) { |
| 2100 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2102 | if (keys == NULL) { |
| 2103 | keys = PyList_New(1); |
| 2104 | if (keys == NULL) { |
| 2105 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2106 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2107 | } |
| 2108 | PyList_SET_ITEM(keys, 0, mangled); |
| 2109 | } |
| 2110 | else { |
| 2111 | int res = PyList_Append(keys, mangled); |
| 2112 | Py_DECREF(mangled); |
| 2113 | if (res == -1) { |
| 2114 | goto error; |
| 2115 | } |
| 2116 | } |
| 2117 | if (!compiler_visit_expr(c, default_)) { |
| 2118 | goto error; |
| 2119 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | } |
| 2121 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2122 | if (keys != NULL) { |
| 2123 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2124 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2125 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2126 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2127 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2128 | assert(default_count > 0); |
| 2129 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2130 | } |
| 2131 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2132 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | error: |
| 2136 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2137 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2141 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2142 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2143 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2144 | return 1; |
| 2145 | } |
| 2146 | |
| 2147 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2148 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2149 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2150 | { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2151 | if (!annotation) { |
| 2152 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | } |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2154 | |
| 2155 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
| 2156 | if (!mangled) { |
| 2157 | return 0; |
| 2158 | } |
| 2159 | ADDOP_LOAD_CONST(c, mangled); |
| 2160 | Py_DECREF(mangled); |
| 2161 | |
| 2162 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2163 | VISIT(c, annexpr, annotation) |
| 2164 | } |
| 2165 | else { |
| 2166 | VISIT(c, expr, annotation); |
| 2167 | } |
| 2168 | *annotations_len += 2; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2169 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2173 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2174 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2175 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2176 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2178 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2179 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | c, |
| 2181 | arg->arg, |
| 2182 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2183 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2184 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2186 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | static int |
| 2190 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2191 | expr_ty returns) |
| 2192 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2193 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2194 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2195 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2196 | 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] | 2197 | */ |
| 2198 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2199 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2200 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2201 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2202 | return 0; |
| 2203 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2204 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2205 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2206 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2207 | args->vararg->annotation, &annotations_len)) |
| 2208 | return 0; |
| 2209 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2210 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2211 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2212 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2213 | args->kwarg->annotation, &annotations_len)) |
| 2214 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | if (!return_str) { |
| 2217 | return_str = PyUnicode_InternFromString("return"); |
| 2218 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2219 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2220 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2221 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2222 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2225 | if (annotations_len) { |
| 2226 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2227 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2229 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2230 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2234 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2235 | { |
| 2236 | VISIT_SEQ(c, expr, args->defaults); |
| 2237 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2238 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2241 | static Py_ssize_t |
| 2242 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2243 | { |
| 2244 | Py_ssize_t funcflags = 0; |
| 2245 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2246 | if (!compiler_visit_defaults(c, args)) |
| 2247 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2248 | funcflags |= 0x01; |
| 2249 | } |
| 2250 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2251 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2252 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2253 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2254 | return -1; |
| 2255 | } |
| 2256 | else if (res > 0) { |
| 2257 | funcflags |= 0x02; |
| 2258 | } |
| 2259 | } |
| 2260 | return funcflags; |
| 2261 | } |
| 2262 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2263 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2264 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2265 | { |
| 2266 | |
| 2267 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2268 | compiler_error(c, "cannot assign to __debug__"); |
| 2269 | return 1; |
| 2270 | } |
Dong-hee Na | 32c1caa | 2021-08-26 09:52:21 +0000 | [diff] [blame] | 2271 | if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2272 | compiler_error(c, "cannot delete __debug__"); |
| 2273 | return 1; |
| 2274 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2275 | return 0; |
| 2276 | } |
| 2277 | |
| 2278 | static int |
| 2279 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2280 | { |
| 2281 | if (arg != NULL) { |
| 2282 | if (forbidden_name(c, arg->arg, Store)) |
| 2283 | return 0; |
| 2284 | } |
| 2285 | return 1; |
| 2286 | } |
| 2287 | |
| 2288 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2289 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2290 | { |
| 2291 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2292 | 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] | 2293 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2294 | return 0; |
| 2295 | } |
| 2296 | } |
| 2297 | return 1; |
| 2298 | } |
| 2299 | |
| 2300 | static int |
| 2301 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2302 | { |
| 2303 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2304 | return 0; |
| 2305 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2306 | return 0; |
| 2307 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2308 | return 0; |
| 2309 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2310 | return 0; |
| 2311 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2312 | return 0; |
| 2313 | return 1; |
| 2314 | } |
| 2315 | |
| 2316 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2317 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2320 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2321 | arguments_ty args; |
| 2322 | expr_ty returns; |
| 2323 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2324 | asdl_expr_seq* decos; |
| 2325 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2326 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2327 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2328 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2329 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2330 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2331 | if (is_async) { |
| 2332 | assert(s->kind == AsyncFunctionDef_kind); |
| 2333 | |
| 2334 | args = s->v.AsyncFunctionDef.args; |
| 2335 | returns = s->v.AsyncFunctionDef.returns; |
| 2336 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2337 | name = s->v.AsyncFunctionDef.name; |
| 2338 | body = s->v.AsyncFunctionDef.body; |
| 2339 | |
| 2340 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2341 | } else { |
| 2342 | assert(s->kind == FunctionDef_kind); |
| 2343 | |
| 2344 | args = s->v.FunctionDef.args; |
| 2345 | returns = s->v.FunctionDef.returns; |
| 2346 | decos = s->v.FunctionDef.decorator_list; |
| 2347 | name = s->v.FunctionDef.name; |
| 2348 | body = s->v.FunctionDef.body; |
| 2349 | |
| 2350 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2351 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2352 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2353 | if (!compiler_check_debug_args(c, args)) |
| 2354 | return 0; |
| 2355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | if (!compiler_decorators(c, decos)) |
| 2357 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2358 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2359 | firstlineno = s->lineno; |
| 2360 | if (asdl_seq_LEN(decos)) { |
| 2361 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2362 | } |
| 2363 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2364 | funcflags = compiler_default_arguments(c, args); |
| 2365 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2367 | } |
| 2368 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2369 | annotations = compiler_visit_annotations(c, args, returns); |
| 2370 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2371 | return 0; |
| 2372 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2373 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2374 | funcflags |= 0x04; |
| 2375 | } |
| 2376 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2377 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2378 | return 0; |
| 2379 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2380 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2381 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2382 | if (c->c_optimize < 2) { |
| 2383 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2384 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2385 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | compiler_exit_scope(c); |
| 2387 | return 0; |
| 2388 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2391 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2393 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2394 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2395 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2397 | qualname = c->u->u_qualname; |
| 2398 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2400 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2401 | Py_XDECREF(qualname); |
| 2402 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2404 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2405 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2406 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2407 | Py_DECREF(qualname); |
| 2408 | Py_DECREF(co); |
| 2409 | return 0; |
| 2410 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2411 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | /* decorators */ |
| 2415 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2416 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2417 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2418 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2419 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | static int |
| 2423 | compiler_class(struct compiler *c, stmt_ty s) |
| 2424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | PyCodeObject *co; |
| 2426 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2427 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2428 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2430 | if (!compiler_decorators(c, decos)) |
| 2431 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2432 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2433 | firstlineno = s->lineno; |
| 2434 | if (asdl_seq_LEN(decos)) { |
| 2435 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2436 | } |
| 2437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2438 | /* ultimately generate code for: |
| 2439 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2440 | where: |
| 2441 | <func> is a function/closure created from the class body; |
| 2442 | it has a single argument (__locals__) where the dict |
| 2443 | (or MutableSequence) representing the locals is passed |
| 2444 | <name> is the class name |
| 2445 | <bases> is the positional arguments and *varargs argument |
| 2446 | <keywords> is the keyword arguments and **kwds argument |
| 2447 | This borrows from compiler_call. |
| 2448 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2450 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2451 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2452 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | return 0; |
| 2454 | /* this block represents what we do in the new scope */ |
| 2455 | { |
| 2456 | /* use the class name for name mangling */ |
| 2457 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2458 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | /* load (global) __name__ ... */ |
| 2460 | str = PyUnicode_InternFromString("__name__"); |
| 2461 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2462 | Py_XDECREF(str); |
| 2463 | compiler_exit_scope(c); |
| 2464 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2465 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | Py_DECREF(str); |
| 2467 | /* ... and store it as __module__ */ |
| 2468 | str = PyUnicode_InternFromString("__module__"); |
| 2469 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2470 | Py_XDECREF(str); |
| 2471 | compiler_exit_scope(c); |
| 2472 | return 0; |
| 2473 | } |
| 2474 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2475 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2476 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2477 | str = PyUnicode_InternFromString("__qualname__"); |
| 2478 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2479 | Py_XDECREF(str); |
| 2480 | compiler_exit_scope(c); |
| 2481 | return 0; |
| 2482 | } |
| 2483 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2485 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | compiler_exit_scope(c); |
| 2487 | return 0; |
| 2488 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2489 | /* The following code is artificial */ |
| 2490 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2491 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2492 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2493 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2494 | str = PyUnicode_InternFromString("__class__"); |
| 2495 | if (str == NULL) { |
| 2496 | compiler_exit_scope(c); |
| 2497 | return 0; |
| 2498 | } |
| 2499 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2500 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2501 | if (i < 0) { |
| 2502 | compiler_exit_scope(c); |
| 2503 | return 0; |
| 2504 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2505 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2508 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2509 | str = PyUnicode_InternFromString("__classcell__"); |
| 2510 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2511 | Py_XDECREF(str); |
| 2512 | compiler_exit_scope(c); |
| 2513 | return 0; |
| 2514 | } |
| 2515 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2517 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2518 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2519 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2520 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2521 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2522 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | /* create the code object */ |
| 2524 | co = assemble(c, 1); |
| 2525 | } |
| 2526 | /* leave the new scope */ |
| 2527 | compiler_exit_scope(c); |
| 2528 | if (co == NULL) |
| 2529 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | /* 2. load the 'build_class' function */ |
| 2532 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2533 | |
| 2534 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2535 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2536 | Py_DECREF(co); |
| 2537 | return 0; |
| 2538 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | Py_DECREF(co); |
| 2540 | |
| 2541 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2542 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | |
| 2544 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2545 | 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] | 2546 | return 0; |
| 2547 | |
| 2548 | /* 6. apply decorators */ |
| 2549 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2550 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2551 | } |
| 2552 | |
| 2553 | /* 7. store into <name> */ |
| 2554 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2555 | return 0; |
| 2556 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2559 | /* Return 0 if the expression is a constant value except named singletons. |
| 2560 | Return 1 otherwise. */ |
| 2561 | static int |
| 2562 | check_is_arg(expr_ty e) |
| 2563 | { |
| 2564 | if (e->kind != Constant_kind) { |
| 2565 | return 1; |
| 2566 | } |
| 2567 | PyObject *value = e->v.Constant.value; |
| 2568 | return (value == Py_None |
| 2569 | || value == Py_False |
| 2570 | || value == Py_True |
| 2571 | || value == Py_Ellipsis); |
| 2572 | } |
| 2573 | |
| 2574 | /* Check operands of identity chacks ("is" and "is not"). |
| 2575 | Emit a warning if any operand is a constant except named singletons. |
| 2576 | Return 0 on error. |
| 2577 | */ |
| 2578 | static int |
| 2579 | check_compare(struct compiler *c, expr_ty e) |
| 2580 | { |
| 2581 | Py_ssize_t i, n; |
| 2582 | int left = check_is_arg(e->v.Compare.left); |
| 2583 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2584 | for (i = 0; i < n; i++) { |
| 2585 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2586 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2587 | if (op == Is || op == IsNot) { |
| 2588 | if (!right || !left) { |
| 2589 | const char *msg = (op == Is) |
| 2590 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2591 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2592 | return compiler_warn(c, msg); |
| 2593 | } |
| 2594 | } |
| 2595 | left = right; |
| 2596 | } |
| 2597 | return 1; |
| 2598 | } |
| 2599 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2600 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2601 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2602 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2603 | switch (op) { |
| 2604 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2605 | cmp = Py_EQ; |
| 2606 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2607 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2608 | cmp = Py_NE; |
| 2609 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2610 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2611 | cmp = Py_LT; |
| 2612 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2613 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2614 | cmp = Py_LE; |
| 2615 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2616 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2617 | cmp = Py_GT; |
| 2618 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2619 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2620 | cmp = Py_GE; |
| 2621 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2622 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2623 | ADDOP_I(c, IS_OP, 0); |
| 2624 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2625 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2626 | ADDOP_I(c, IS_OP, 1); |
| 2627 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2628 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2629 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2630 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2631 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2632 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2633 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2634 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2635 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2636 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2637 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2638 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2639 | } |
| 2640 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2641 | |
| 2642 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2643 | static int |
| 2644 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2645 | { |
| 2646 | switch (e->kind) { |
| 2647 | case UnaryOp_kind: |
| 2648 | if (e->v.UnaryOp.op == Not) |
| 2649 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2650 | /* fallback to general implementation */ |
| 2651 | break; |
| 2652 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2653 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2654 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2655 | assert(n >= 0); |
| 2656 | int cond2 = e->v.BoolOp.op == Or; |
| 2657 | basicblock *next2 = next; |
| 2658 | if (!cond2 != !cond) { |
| 2659 | next2 = compiler_new_block(c); |
| 2660 | if (next2 == NULL) |
| 2661 | return 0; |
| 2662 | } |
| 2663 | for (i = 0; i < n; ++i) { |
| 2664 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2665 | return 0; |
| 2666 | } |
| 2667 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2668 | return 0; |
| 2669 | if (next2 != next) |
| 2670 | compiler_use_next_block(c, next2); |
| 2671 | return 1; |
| 2672 | } |
| 2673 | case IfExp_kind: { |
| 2674 | basicblock *end, *next2; |
| 2675 | end = compiler_new_block(c); |
| 2676 | if (end == NULL) |
| 2677 | return 0; |
| 2678 | next2 = compiler_new_block(c); |
| 2679 | if (next2 == NULL) |
| 2680 | return 0; |
| 2681 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2682 | return 0; |
| 2683 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2684 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2685 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2686 | compiler_use_next_block(c, next2); |
| 2687 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2688 | return 0; |
| 2689 | compiler_use_next_block(c, end); |
| 2690 | return 1; |
| 2691 | } |
| 2692 | case Compare_kind: { |
| 2693 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2694 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2695 | if (!check_compare(c, e)) { |
| 2696 | return 0; |
| 2697 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2698 | basicblock *cleanup = compiler_new_block(c); |
| 2699 | if (cleanup == NULL) |
| 2700 | return 0; |
| 2701 | VISIT(c, expr, e->v.Compare.left); |
| 2702 | for (i = 0; i < n; i++) { |
| 2703 | VISIT(c, expr, |
| 2704 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2705 | ADDOP(c, DUP_TOP); |
| 2706 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2707 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2708 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2709 | NEXT_BLOCK(c); |
| 2710 | } |
| 2711 | 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] | 2712 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2713 | 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] | 2714 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2715 | basicblock *end = compiler_new_block(c); |
| 2716 | if (end == NULL) |
| 2717 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2718 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2719 | compiler_use_next_block(c, cleanup); |
| 2720 | ADDOP(c, POP_TOP); |
| 2721 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2722 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2723 | } |
| 2724 | compiler_use_next_block(c, end); |
| 2725 | return 1; |
| 2726 | } |
| 2727 | /* fallback to general implementation */ |
| 2728 | break; |
| 2729 | } |
| 2730 | default: |
| 2731 | /* fallback to general implementation */ |
| 2732 | break; |
| 2733 | } |
| 2734 | |
| 2735 | /* general implementation */ |
| 2736 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2737 | 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] | 2738 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2739 | return 1; |
| 2740 | } |
| 2741 | |
| 2742 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2743 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | basicblock *end, *next; |
| 2746 | |
| 2747 | assert(e->kind == IfExp_kind); |
| 2748 | end = compiler_new_block(c); |
| 2749 | if (end == NULL) |
| 2750 | return 0; |
| 2751 | next = compiler_new_block(c); |
| 2752 | if (next == NULL) |
| 2753 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2754 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2755 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2757 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | compiler_use_next_block(c, next); |
| 2759 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2760 | compiler_use_next_block(c, end); |
| 2761 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2765 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2768 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2770 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | arguments_ty args = e->v.Lambda.args; |
| 2772 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2773 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2774 | if (!compiler_check_debug_args(c, args)) |
| 2775 | return 0; |
| 2776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | if (!name) { |
| 2778 | name = PyUnicode_InternFromString("<lambda>"); |
| 2779 | if (!name) |
| 2780 | return 0; |
| 2781 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2782 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2783 | funcflags = compiler_default_arguments(c, args); |
| 2784 | if (funcflags == -1) { |
| 2785 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2787 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2788 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2789 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | /* Make None the first constant, so the lambda can't have a |
| 2793 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2794 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2797 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2798 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2800 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2801 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2802 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | } |
| 2804 | else { |
| 2805 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2806 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2808 | qualname = c->u->u_qualname; |
| 2809 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2811 | if (co == NULL) { |
| 2812 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2814 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2815 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2816 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2817 | Py_DECREF(qualname); |
| 2818 | Py_DECREF(co); |
| 2819 | return 0; |
| 2820 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2821 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | Py_DECREF(co); |
| 2823 | |
| 2824 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2828 | compiler_if(struct compiler *c, stmt_ty s) |
| 2829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | assert(s->kind == If_kind); |
| 2832 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2833 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2835 | } |
| 2836 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2837 | next = compiler_new_block(c); |
| 2838 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2839 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2840 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2841 | } |
| 2842 | else { |
| 2843 | next = end; |
| 2844 | } |
| 2845 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2846 | return 0; |
| 2847 | } |
| 2848 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2849 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2850 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2851 | compiler_use_next_block(c, next); |
| 2852 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | } |
| 2854 | compiler_use_next_block(c, end); |
| 2855 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | static int |
| 2859 | compiler_for(struct compiler *c, stmt_ty s) |
| 2860 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2861 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2864 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | cleanup = compiler_new_block(c); |
| 2866 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2867 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2869 | } |
| 2870 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2872 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | VISIT(c, expr, s->v.For.iter); |
| 2874 | ADDOP(c, GET_ITER); |
| 2875 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2876 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2877 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | VISIT(c, expr, s->v.For.target); |
| 2879 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2880 | /* Mark jump as artificial */ |
| 2881 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2882 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2884 | |
| 2885 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2888 | compiler_use_next_block(c, end); |
| 2889 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2892 | |
| 2893 | static int |
| 2894 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2895 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2896 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2897 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2898 | c->u->u_ste->ste_coroutine = 1; |
| 2899 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2900 | return compiler_error(c, "'async for' outside async function"); |
| 2901 | } |
| 2902 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2903 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2904 | except = compiler_new_block(c); |
| 2905 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2906 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2907 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2908 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2909 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2910 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2911 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2912 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2913 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2914 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2915 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2916 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2917 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2918 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2919 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2920 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2921 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2922 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2923 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2924 | /* Success block for __anext__ */ |
| 2925 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2926 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2927 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2928 | |
| 2929 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2930 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2931 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2932 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2933 | |
Mark Shannon | 47695e3 | 2021-07-15 15:54:38 +0100 | [diff] [blame] | 2934 | /* Use same line number as the iterator, |
| 2935 | * as the END_ASYNC_FOR succeeds the `for`, not the body. */ |
| 2936 | SET_LOC(c, s->v.AsyncFor.iter); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2937 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2938 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2939 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2940 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2941 | |
| 2942 | compiler_use_next_block(c, end); |
| 2943 | |
| 2944 | return 1; |
| 2945 | } |
| 2946 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | static int |
| 2948 | compiler_while(struct compiler *c, stmt_ty s) |
| 2949 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2950 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2952 | body = compiler_new_block(c); |
| 2953 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2955 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2959 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2960 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2962 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2963 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2968 | SET_LOC(c, s); |
| 2969 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2970 | return 0; |
| 2971 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2973 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2974 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2975 | compiler_use_next_block(c, anchor); |
| 2976 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2978 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2981 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2985 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2986 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2987 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2988 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2989 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2990 | return compiler_error(c, "'return' outside function"); |
| 2991 | if (s->v.Return.value != NULL && |
| 2992 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2993 | { |
| 2994 | return compiler_error( |
| 2995 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2997 | if (preserve_tos) { |
| 2998 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2999 | } else { |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3000 | /* Emit instruction with line number for return value */ |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 3001 | if (s->v.Return.value != NULL) { |
| 3002 | SET_LOC(c, s->v.Return.value); |
| 3003 | ADDOP(c, NOP); |
| 3004 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3005 | } |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3006 | if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) { |
| 3007 | SET_LOC(c, s); |
| 3008 | ADDOP(c, NOP); |
| 3009 | } |
| 3010 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3011 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 3012 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3013 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3014 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3015 | } |
| 3016 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 3017 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3018 | } |
| 3019 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3020 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3025 | static int |
| 3026 | compiler_break(struct compiler *c) |
| 3027 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3028 | struct fblockinfo *loop = NULL; |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3029 | /* Emit instruction with line number */ |
| 3030 | ADDOP(c, NOP); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3031 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 3032 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3033 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3034 | if (loop == NULL) { |
| 3035 | return compiler_error(c, "'break' outside loop"); |
| 3036 | } |
| 3037 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 3038 | return 0; |
| 3039 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3040 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3041 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3042 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | static int |
| 3046 | compiler_continue(struct compiler *c) |
| 3047 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3048 | struct fblockinfo *loop = NULL; |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3049 | /* Emit instruction with line number */ |
| 3050 | ADDOP(c, NOP); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3051 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 3052 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3053 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3054 | if (loop == NULL) { |
| 3055 | return compiler_error(c, "'continue' not properly in loop"); |
| 3056 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3057 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3058 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3059 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3063 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | |
| 3065 | SETUP_FINALLY L |
| 3066 | <code for body> |
| 3067 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3068 | <code for finalbody> |
| 3069 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3070 | L: |
| 3071 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3072 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3074 | The special instructions use the block stack. Each block |
| 3075 | stack entry contains the instruction that created it (here |
| 3076 | SETUP_FINALLY), the level of the value stack at the time the |
| 3077 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3079 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | Pushes the current value stack level and the label |
| 3081 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3082 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3083 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3085 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3086 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 3087 | exceptions are pushed onto the value stack (and the exception |
| 3088 | condition is cleared), and the interpreter jumps to the label |
| 3089 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3090 | */ |
| 3091 | |
| 3092 | static int |
| 3093 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 3094 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3095 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | body = compiler_new_block(c); |
| 3098 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3099 | exit = compiler_new_block(c); |
| 3100 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3102 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3103 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3104 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3106 | 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] | 3107 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3108 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3109 | if (!compiler_try_except(c, s)) |
| 3110 | return 0; |
| 3111 | } |
| 3112 | else { |
| 3113 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3114 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3115 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3116 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3117 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3118 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3119 | /* `finally` block */ |
| 3120 | compiler_use_next_block(c, end); |
| 3121 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3122 | return 0; |
| 3123 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3124 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3125 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3126 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3131 | 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] | 3132 | (The contents of the value stack is shown in [], with the top |
| 3133 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3134 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | |
| 3136 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3137 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | [] <code for S> |
| 3139 | [] POP_BLOCK |
| 3140 | [] JUMP_FORWARD L0 |
| 3141 | |
| 3142 | [tb, val, exc] L1: DUP ) |
| 3143 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3144 | [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] | 3145 | [tb, val, exc] POP |
| 3146 | [tb, val] <assign to V1> (or POP if no V1) |
| 3147 | [tb] POP |
| 3148 | [] <code for S1> |
| 3149 | JUMP_FORWARD L0 |
| 3150 | |
| 3151 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3152 | .............................etc....................... |
| 3153 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3154 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | |
| 3156 | [] L0: <next statement> |
| 3157 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3158 | Of course, parts are not generated if Vi or Ei is not present. |
| 3159 | */ |
| 3160 | static int |
| 3161 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3164 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3166 | body = compiler_new_block(c); |
| 3167 | except = compiler_new_block(c); |
| 3168 | orelse = compiler_new_block(c); |
| 3169 | end = compiler_new_block(c); |
| 3170 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3171 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3172 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3174 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3176 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3177 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3178 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3179 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3180 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3182 | /* Runtime will push a block here, so we need to account for that */ |
| 3183 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3184 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | for (i = 0; i < n; i++) { |
| 3186 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3187 | s->v.Try.handlers, i); |
Mark Shannon | 8d4b184 | 2021-05-06 13:38:50 +0100 | [diff] [blame] | 3188 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3190 | return compiler_error(c, "default 'except:' must be last"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 | except = compiler_new_block(c); |
| 3192 | if (except == NULL) |
| 3193 | return 0; |
| 3194 | if (handler->v.ExceptHandler.type) { |
| 3195 | ADDOP(c, DUP_TOP); |
| 3196 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3197 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3198 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | } |
| 3200 | ADDOP(c, POP_TOP); |
| 3201 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3202 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3203 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3204 | cleanup_end = compiler_new_block(c); |
| 3205 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3206 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3207 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3208 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3209 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3210 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3211 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3213 | /* |
| 3214 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3215 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3216 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3217 | try: |
| 3218 | # body |
| 3219 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3220 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3221 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3222 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3224 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3225 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3226 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3227 | 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] | 3228 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3230 | /* second # body */ |
| 3231 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3232 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3233 | /* name = None; del name; # Mark as artificial */ |
| 3234 | c->u->u_lineno = -1; |
Mark Shannon | 794ff7d | 2021-07-14 11:43:56 +0100 | [diff] [blame] | 3235 | ADDOP(c, POP_BLOCK); |
| 3236 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3237 | ADDOP_LOAD_CONST(c, Py_None); |
| 3238 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3239 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3240 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3241 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3242 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3243 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3245 | /* name = None; del name; # Mark as artificial */ |
| 3246 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3247 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3248 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3249 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3251 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3252 | } |
| 3253 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3254 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3256 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3257 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3258 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3260 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3261 | ADDOP(c, POP_TOP); |
| 3262 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3263 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3264 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3266 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3267 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3268 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3269 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3270 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | compiler_use_next_block(c, except); |
| 3272 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3273 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3274 | /* Mark as artificial */ |
| 3275 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3276 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3278 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3279 | compiler_use_next_block(c, end); |
| 3280 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3284 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3285 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3286 | return compiler_try_finally(c, s); |
| 3287 | else |
| 3288 | return compiler_try_except(c, s); |
| 3289 | } |
| 3290 | |
| 3291 | |
| 3292 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | /* The IMPORT_NAME opcode was already generated. This function |
| 3296 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | 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] | 3299 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3301 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3302 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3303 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3304 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3305 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3307 | while (1) { |
| 3308 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3310 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3311 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3312 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3313 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3315 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3316 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3317 | if (dot == -1) { |
| 3318 | break; |
| 3319 | } |
| 3320 | ADDOP(c, ROT_TWO); |
| 3321 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3323 | if (!compiler_nameop(c, asname, Store)) { |
| 3324 | return 0; |
| 3325 | } |
| 3326 | ADDOP(c, POP_TOP); |
| 3327 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | } |
| 3329 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | static int |
| 3333 | compiler_import(struct compiler *c, stmt_ty s) |
| 3334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | /* The Import node stores a module name like a.b.c as a single |
| 3336 | string. This is convenient for all cases except |
| 3337 | import a.b.c as d |
| 3338 | where we need to parse that string to extract the individual |
| 3339 | module names. |
| 3340 | XXX Perhaps change the representation to make this case simpler? |
| 3341 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3342 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3343 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3344 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | for (i = 0; i < n; i++) { |
| 3346 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3347 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3348 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3349 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3350 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3353 | if (alias->asname) { |
| 3354 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3355 | if (!r) |
| 3356 | return r; |
| 3357 | } |
| 3358 | else { |
| 3359 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3360 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3361 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3362 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3363 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3364 | if (tmp == NULL) |
| 3365 | return 0; |
| 3366 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3368 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | Py_DECREF(tmp); |
| 3370 | } |
| 3371 | if (!r) |
| 3372 | return r; |
| 3373 | } |
| 3374 | } |
| 3375 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | static int |
| 3379 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3380 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3381 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3382 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3383 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3385 | if (!empty_string) { |
| 3386 | empty_string = PyUnicode_FromString(""); |
| 3387 | if (!empty_string) |
| 3388 | return 0; |
| 3389 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3390 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3391 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3392 | |
| 3393 | names = PyTuple_New(n); |
| 3394 | if (!names) |
| 3395 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | /* build up the names */ |
| 3398 | for (i = 0; i < n; i++) { |
| 3399 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3400 | Py_INCREF(alias->name); |
| 3401 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3402 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3405 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | Py_DECREF(names); |
| 3407 | return compiler_error(c, "from __future__ imports must occur " |
| 3408 | "at the beginning of the file"); |
| 3409 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3410 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3412 | if (s->v.ImportFrom.module) { |
| 3413 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3414 | } |
| 3415 | else { |
| 3416 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3417 | } |
| 3418 | for (i = 0; i < n; i++) { |
| 3419 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3420 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3421 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3422 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | assert(n == 1); |
| 3424 | ADDOP(c, IMPORT_STAR); |
| 3425 | return 1; |
| 3426 | } |
| 3427 | |
| 3428 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3429 | store_name = alias->name; |
| 3430 | if (alias->asname) |
| 3431 | store_name = alias->asname; |
| 3432 | |
| 3433 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | return 0; |
| 3435 | } |
| 3436 | } |
| 3437 | /* remove imported module */ |
| 3438 | ADDOP(c, POP_TOP); |
| 3439 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | static int |
| 3443 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3446 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3447 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3448 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3449 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3450 | (s->v.Assert.test->kind == Constant_kind && |
| 3451 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3452 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3453 | { |
| 3454 | if (!compiler_warn(c, "assertion is always true, " |
| 3455 | "perhaps remove parentheses?")) |
| 3456 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3457 | return 0; |
| 3458 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3460 | if (c->c_optimize) |
| 3461 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | end = compiler_new_block(c); |
| 3463 | if (end == NULL) |
| 3464 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3465 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3466 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3467 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | if (s->v.Assert.msg) { |
| 3469 | VISIT(c, expr, s->v.Assert.msg); |
| 3470 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3471 | } |
| 3472 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3473 | compiler_use_next_block(c, end); |
| 3474 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3478 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3479 | { |
| 3480 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3481 | VISIT(c, expr, value); |
| 3482 | ADDOP(c, PRINT_EXPR); |
| 3483 | return 1; |
| 3484 | } |
| 3485 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3486 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3487 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3488 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3489 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3493 | /* Mark POP_TOP as artificial */ |
| 3494 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3495 | ADDOP(c, POP_TOP); |
| 3496 | return 1; |
| 3497 | } |
| 3498 | |
| 3499 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3500 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3501 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3502 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3504 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3505 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | switch (s->kind) { |
| 3508 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3509 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | case ClassDef_kind: |
| 3511 | return compiler_class(c, s); |
| 3512 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3513 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | case Delete_kind: |
| 3515 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3516 | break; |
| 3517 | case Assign_kind: |
| 3518 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3519 | VISIT(c, expr, s->v.Assign.value); |
| 3520 | for (i = 0; i < n; i++) { |
| 3521 | if (i < n - 1) |
| 3522 | ADDOP(c, DUP_TOP); |
| 3523 | VISIT(c, expr, |
| 3524 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3525 | } |
| 3526 | break; |
| 3527 | case AugAssign_kind: |
| 3528 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3529 | case AnnAssign_kind: |
| 3530 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | case For_kind: |
| 3532 | return compiler_for(c, s); |
| 3533 | case While_kind: |
| 3534 | return compiler_while(c, s); |
| 3535 | case If_kind: |
| 3536 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3537 | case Match_kind: |
| 3538 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3539 | case Raise_kind: |
| 3540 | n = 0; |
| 3541 | if (s->v.Raise.exc) { |
| 3542 | VISIT(c, expr, s->v.Raise.exc); |
| 3543 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3544 | if (s->v.Raise.cause) { |
| 3545 | VISIT(c, expr, s->v.Raise.cause); |
| 3546 | n++; |
| 3547 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3549 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3550 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3552 | case Try_kind: |
| 3553 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | case Assert_kind: |
| 3555 | return compiler_assert(c, s); |
| 3556 | case Import_kind: |
| 3557 | return compiler_import(c, s); |
| 3558 | case ImportFrom_kind: |
| 3559 | return compiler_from_import(c, s); |
| 3560 | case Global_kind: |
| 3561 | case Nonlocal_kind: |
| 3562 | break; |
| 3563 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3564 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3565 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3566 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | break; |
| 3568 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3569 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | case Continue_kind: |
| 3571 | return compiler_continue(c); |
| 3572 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3573 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3574 | case AsyncFunctionDef_kind: |
| 3575 | return compiler_function(c, s, 1); |
| 3576 | case AsyncWith_kind: |
| 3577 | return compiler_async_with(c, s, 0); |
| 3578 | case AsyncFor_kind: |
| 3579 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | static int |
| 3586 | unaryop(unaryop_ty op) |
| 3587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3588 | switch (op) { |
| 3589 | case Invert: |
| 3590 | return UNARY_INVERT; |
| 3591 | case Not: |
| 3592 | return UNARY_NOT; |
| 3593 | case UAdd: |
| 3594 | return UNARY_POSITIVE; |
| 3595 | case USub: |
| 3596 | return UNARY_NEGATIVE; |
| 3597 | default: |
| 3598 | PyErr_Format(PyExc_SystemError, |
| 3599 | "unary op %d should not be possible", op); |
| 3600 | return 0; |
| 3601 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3605 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3607 | switch (op) { |
| 3608 | case Add: |
| 3609 | return BINARY_ADD; |
| 3610 | case Sub: |
| 3611 | return BINARY_SUBTRACT; |
| 3612 | case Mult: |
| 3613 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3614 | case MatMult: |
| 3615 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | case Div: |
| 3617 | return BINARY_TRUE_DIVIDE; |
| 3618 | case Mod: |
| 3619 | return BINARY_MODULO; |
| 3620 | case Pow: |
| 3621 | return BINARY_POWER; |
| 3622 | case LShift: |
| 3623 | return BINARY_LSHIFT; |
| 3624 | case RShift: |
| 3625 | return BINARY_RSHIFT; |
| 3626 | case BitOr: |
| 3627 | return BINARY_OR; |
| 3628 | case BitXor: |
| 3629 | return BINARY_XOR; |
| 3630 | case BitAnd: |
| 3631 | return BINARY_AND; |
| 3632 | case FloorDiv: |
| 3633 | return BINARY_FLOOR_DIVIDE; |
| 3634 | default: |
| 3635 | PyErr_Format(PyExc_SystemError, |
| 3636 | "binary op %d should not be possible", op); |
| 3637 | return 0; |
| 3638 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3642 | inplace_binop(operator_ty op) |
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 | switch (op) { |
| 3645 | case Add: |
| 3646 | return INPLACE_ADD; |
| 3647 | case Sub: |
| 3648 | return INPLACE_SUBTRACT; |
| 3649 | case Mult: |
| 3650 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3651 | case MatMult: |
| 3652 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | case Div: |
| 3654 | return INPLACE_TRUE_DIVIDE; |
| 3655 | case Mod: |
| 3656 | return INPLACE_MODULO; |
| 3657 | case Pow: |
| 3658 | return INPLACE_POWER; |
| 3659 | case LShift: |
| 3660 | return INPLACE_LSHIFT; |
| 3661 | case RShift: |
| 3662 | return INPLACE_RSHIFT; |
| 3663 | case BitOr: |
| 3664 | return INPLACE_OR; |
| 3665 | case BitXor: |
| 3666 | return INPLACE_XOR; |
| 3667 | case BitAnd: |
| 3668 | return INPLACE_AND; |
| 3669 | case FloorDiv: |
| 3670 | return INPLACE_FLOOR_DIVIDE; |
| 3671 | default: |
| 3672 | PyErr_Format(PyExc_SystemError, |
| 3673 | "inplace binary op %d should not be possible", op); |
| 3674 | return 0; |
| 3675 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | static int |
| 3679 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3680 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3681 | int op, scope; |
| 3682 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3685 | PyObject *dict = c->u->u_names; |
| 3686 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3688 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3689 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3690 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3691 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3692 | if (forbidden_name(c, name, ctx)) |
| 3693 | return 0; |
| 3694 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3695 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3696 | if (!mangled) |
| 3697 | return 0; |
| 3698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3699 | op = 0; |
| 3700 | optype = OP_NAME; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 3701 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3702 | switch (scope) { |
| 3703 | case FREE: |
| 3704 | dict = c->u->u_freevars; |
| 3705 | optype = OP_DEREF; |
| 3706 | break; |
| 3707 | case CELL: |
| 3708 | dict = c->u->u_cellvars; |
| 3709 | optype = OP_DEREF; |
| 3710 | break; |
| 3711 | case LOCAL: |
| 3712 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3713 | optype = OP_FAST; |
| 3714 | break; |
| 3715 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3716 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3717 | optype = OP_GLOBAL; |
| 3718 | break; |
| 3719 | case GLOBAL_EXPLICIT: |
| 3720 | optype = OP_GLOBAL; |
| 3721 | break; |
| 3722 | default: |
| 3723 | /* scope can be 0 */ |
| 3724 | break; |
| 3725 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3727 | /* 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] | 3728 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3730 | switch (optype) { |
| 3731 | case OP_DEREF: |
| 3732 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3733 | case Load: |
| 3734 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3735 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3736 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3737 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | } |
| 3739 | break; |
| 3740 | case OP_FAST: |
| 3741 | switch (ctx) { |
| 3742 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3743 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3746 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3747 | return 1; |
| 3748 | case OP_GLOBAL: |
| 3749 | switch (ctx) { |
| 3750 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3751 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | } |
| 3754 | break; |
| 3755 | case OP_NAME: |
| 3756 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3757 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3758 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | } |
| 3761 | break; |
| 3762 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3764 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3765 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | Py_DECREF(mangled); |
| 3767 | if (arg < 0) |
| 3768 | return 0; |
| 3769 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | static int |
| 3773 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3776 | int jumpi; |
| 3777 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3778 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3780 | assert(e->kind == BoolOp_kind); |
| 3781 | if (e->v.BoolOp.op == And) |
| 3782 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3783 | else |
| 3784 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3785 | end = compiler_new_block(c); |
| 3786 | if (end == NULL) |
| 3787 | return 0; |
| 3788 | s = e->v.BoolOp.values; |
| 3789 | n = asdl_seq_LEN(s) - 1; |
| 3790 | assert(n >= 0); |
| 3791 | for (i = 0; i < n; ++i) { |
| 3792 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3793 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3794 | basicblock *next = compiler_new_block(c); |
| 3795 | if (next == NULL) { |
| 3796 | return 0; |
| 3797 | } |
| 3798 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3799 | } |
| 3800 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3801 | compiler_use_next_block(c, end); |
| 3802 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3806 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3807 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3808 | { |
| 3809 | Py_ssize_t n = asdl_seq_LEN(elts); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3810 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3811 | PyObject *folded = PyTuple_New(n); |
| 3812 | if (folded == NULL) { |
| 3813 | return 0; |
| 3814 | } |
| 3815 | PyObject *val; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3816 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3817 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3818 | Py_INCREF(val); |
| 3819 | PyTuple_SET_ITEM(folded, i, val); |
| 3820 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3821 | if (tuple) { |
| 3822 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3823 | } else { |
| 3824 | if (add == SET_ADD) { |
| 3825 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3826 | if (folded == NULL) { |
| 3827 | return 0; |
| 3828 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3829 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3830 | ADDOP_I(c, build, pushed); |
| 3831 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3832 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3833 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3834 | return 1; |
| 3835 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3836 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3837 | int big = n+pushed > STACK_USE_GUIDELINE; |
| 3838 | int seen_star = 0; |
| 3839 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3840 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3841 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3842 | seen_star = 1; |
| 3843 | } |
| 3844 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3845 | if (!seen_star && !big) { |
| 3846 | for (Py_ssize_t i = 0; i < n; i++) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3847 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3848 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3849 | } |
| 3850 | if (tuple) { |
| 3851 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3852 | } else { |
| 3853 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3854 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3855 | return 1; |
| 3856 | } |
| 3857 | int sequence_built = 0; |
| 3858 | if (big) { |
| 3859 | ADDOP_I(c, build, pushed); |
| 3860 | sequence_built = 1; |
| 3861 | } |
| 3862 | for (Py_ssize_t i = 0; i < n; i++) { |
| 3863 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3864 | if (elt->kind == Starred_kind) { |
| 3865 | if (sequence_built == 0) { |
| 3866 | ADDOP_I(c, build, i+pushed); |
| 3867 | sequence_built = 1; |
| 3868 | } |
| 3869 | VISIT(c, expr, elt->v.Starred.value); |
| 3870 | ADDOP_I(c, extend, 1); |
| 3871 | } |
| 3872 | else { |
| 3873 | VISIT(c, expr, elt); |
| 3874 | if (sequence_built) { |
| 3875 | ADDOP_I(c, add, 1); |
| 3876 | } |
| 3877 | } |
| 3878 | } |
| 3879 | assert(sequence_built); |
| 3880 | if (tuple) { |
| 3881 | ADDOP(c, LIST_TO_TUPLE); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3882 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3883 | return 1; |
| 3884 | } |
| 3885 | |
| 3886 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3887 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3888 | { |
| 3889 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3890 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3891 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3892 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3893 | if (elt->kind == Starred_kind && !seen_star) { |
| 3894 | if ((i >= (1 << 8)) || |
| 3895 | (n-i-1 >= (INT_MAX >> 8))) |
| 3896 | return compiler_error(c, |
| 3897 | "too many expressions in " |
| 3898 | "star-unpacking assignment"); |
| 3899 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3900 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3901 | } |
| 3902 | else if (elt->kind == Starred_kind) { |
| 3903 | return compiler_error(c, |
Furkan Ănder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3904 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3905 | } |
| 3906 | } |
| 3907 | if (!seen_star) { |
| 3908 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3909 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3910 | return 1; |
| 3911 | } |
| 3912 | |
| 3913 | static int |
| 3914 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3915 | { |
| 3916 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3917 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3918 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3919 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3920 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3921 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3922 | return 1; |
| 3923 | } |
| 3924 | |
| 3925 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3926 | compiler_list(struct compiler *c, expr_ty e) |
| 3927 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3928 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3929 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3930 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3931 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3932 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3933 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3934 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3936 | else |
| 3937 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | static int |
| 3942 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3943 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3944 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3945 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3946 | return assignment_helper(c, elts); |
| 3947 | } |
| 3948 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3949 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3950 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3951 | } |
| 3952 | else |
| 3953 | VISIT_SEQ(c, expr, elts); |
| 3954 | return 1; |
| 3955 | } |
| 3956 | |
| 3957 | static int |
| 3958 | compiler_set(struct compiler *c, expr_ty e) |
| 3959 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3960 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3961 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3965 | 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] | 3966 | { |
| 3967 | Py_ssize_t i; |
| 3968 | for (i = begin; i < end; i++) { |
| 3969 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3970 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3971 | return 0; |
| 3972 | } |
| 3973 | return 1; |
| 3974 | } |
| 3975 | |
| 3976 | static int |
| 3977 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3978 | { |
| 3979 | Py_ssize_t i, n = end - begin; |
| 3980 | PyObject *keys, *key; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3981 | int big = n*2 > STACK_USE_GUIDELINE; |
| 3982 | 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] | 3983 | for (i = begin; i < end; i++) { |
| 3984 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3985 | } |
| 3986 | keys = PyTuple_New(n); |
| 3987 | if (keys == NULL) { |
| 3988 | return 0; |
| 3989 | } |
| 3990 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3991 | 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] | 3992 | Py_INCREF(key); |
| 3993 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3994 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3995 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3996 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3997 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3998 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3999 | if (big) { |
| 4000 | ADDOP_I(c, BUILD_MAP, 0); |
| 4001 | } |
| 4002 | for (i = begin; i < end; i++) { |
| 4003 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 4004 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 4005 | if (big) { |
| 4006 | ADDOP_I(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4007 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4008 | } |
| 4009 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4010 | ADDOP_I(c, BUILD_MAP, n); |
| 4011 | } |
| 4012 | return 1; |
| 4013 | } |
| 4014 | |
| 4015 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4016 | compiler_dict(struct compiler *c, expr_ty e) |
| 4017 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4018 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4019 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4020 | int is_unpacking = 0; |
| 4021 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4022 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4023 | elements = 0; |
| 4024 | for (i = 0; i < n; i++) { |
| 4025 | 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] | 4026 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4027 | if (elements) { |
| 4028 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 4029 | return 0; |
| 4030 | } |
| 4031 | if (have_dict) { |
| 4032 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4033 | } |
| 4034 | have_dict = 1; |
| 4035 | elements = 0; |
| 4036 | } |
| 4037 | if (have_dict == 0) { |
| 4038 | ADDOP_I(c, BUILD_MAP, 0); |
| 4039 | have_dict = 1; |
| 4040 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4041 | 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] | 4042 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4043 | } |
| 4044 | else { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4045 | if (elements*2 > STACK_USE_GUIDELINE) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 4046 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4047 | return 0; |
| 4048 | } |
| 4049 | if (have_dict) { |
| 4050 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4051 | } |
| 4052 | have_dict = 1; |
| 4053 | elements = 0; |
| 4054 | } |
| 4055 | else { |
| 4056 | elements++; |
| 4057 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4058 | } |
| 4059 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4060 | if (elements) { |
| 4061 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4062 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4063 | } |
| 4064 | if (have_dict) { |
| 4065 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4066 | } |
| 4067 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4068 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4069 | if (!have_dict) { |
| 4070 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4071 | } |
| 4072 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | static int |
| 4076 | compiler_compare(struct compiler *c, expr_ty e) |
| 4077 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4078 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4079 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 4080 | if (!check_compare(c, e)) { |
| 4081 | return 0; |
| 4082 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4083 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4084 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 4085 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 4086 | if (n == 0) { |
| 4087 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4088 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4089 | } |
| 4090 | else { |
| 4091 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | if (cleanup == NULL) |
| 4093 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4094 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4095 | VISIT(c, expr, |
| 4096 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4097 | ADDOP(c, DUP_TOP); |
| 4098 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4099 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4100 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4101 | NEXT_BLOCK(c); |
| 4102 | } |
| 4103 | 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] | 4104 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | basicblock *end = compiler_new_block(c); |
| 4106 | if (end == NULL) |
| 4107 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 4108 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4109 | compiler_use_next_block(c, cleanup); |
| 4110 | ADDOP(c, ROT_TWO); |
| 4111 | ADDOP(c, POP_TOP); |
| 4112 | compiler_use_next_block(c, end); |
| 4113 | } |
| 4114 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4117 | static PyTypeObject * |
| 4118 | infer_type(expr_ty e) |
| 4119 | { |
| 4120 | switch (e->kind) { |
| 4121 | case Tuple_kind: |
| 4122 | return &PyTuple_Type; |
| 4123 | case List_kind: |
| 4124 | case ListComp_kind: |
| 4125 | return &PyList_Type; |
| 4126 | case Dict_kind: |
| 4127 | case DictComp_kind: |
| 4128 | return &PyDict_Type; |
| 4129 | case Set_kind: |
| 4130 | case SetComp_kind: |
| 4131 | return &PySet_Type; |
| 4132 | case GeneratorExp_kind: |
| 4133 | return &PyGen_Type; |
| 4134 | case Lambda_kind: |
| 4135 | return &PyFunction_Type; |
| 4136 | case JoinedStr_kind: |
| 4137 | case FormattedValue_kind: |
| 4138 | return &PyUnicode_Type; |
| 4139 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4140 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4141 | default: |
| 4142 | return NULL; |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | static int |
| 4147 | check_caller(struct compiler *c, expr_ty e) |
| 4148 | { |
| 4149 | switch (e->kind) { |
| 4150 | case Constant_kind: |
| 4151 | case Tuple_kind: |
| 4152 | case List_kind: |
| 4153 | case ListComp_kind: |
| 4154 | case Dict_kind: |
| 4155 | case DictComp_kind: |
| 4156 | case Set_kind: |
| 4157 | case SetComp_kind: |
| 4158 | case GeneratorExp_kind: |
| 4159 | case JoinedStr_kind: |
| 4160 | case FormattedValue_kind: |
| 4161 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4162 | "perhaps you missed a comma?", |
| 4163 | infer_type(e)->tp_name); |
| 4164 | default: |
| 4165 | return 1; |
| 4166 | } |
| 4167 | } |
| 4168 | |
| 4169 | static int |
| 4170 | check_subscripter(struct compiler *c, expr_ty e) |
| 4171 | { |
| 4172 | PyObject *v; |
| 4173 | |
| 4174 | switch (e->kind) { |
| 4175 | case Constant_kind: |
| 4176 | v = e->v.Constant.value; |
| 4177 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4178 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4179 | PyAnySet_Check(v))) |
| 4180 | { |
| 4181 | return 1; |
| 4182 | } |
| 4183 | /* fall through */ |
| 4184 | case Set_kind: |
| 4185 | case SetComp_kind: |
| 4186 | case GeneratorExp_kind: |
| 4187 | case Lambda_kind: |
| 4188 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4189 | "perhaps you missed a comma?", |
| 4190 | infer_type(e)->tp_name); |
| 4191 | default: |
| 4192 | return 1; |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4197 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4198 | { |
| 4199 | PyObject *v; |
| 4200 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4201 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4202 | if (index_type == NULL |
| 4203 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4204 | || index_type == &PySlice_Type) { |
| 4205 | return 1; |
| 4206 | } |
| 4207 | |
| 4208 | switch (e->kind) { |
| 4209 | case Constant_kind: |
| 4210 | v = e->v.Constant.value; |
| 4211 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4212 | return 1; |
| 4213 | } |
| 4214 | /* fall through */ |
| 4215 | case Tuple_kind: |
| 4216 | case List_kind: |
| 4217 | case ListComp_kind: |
| 4218 | case JoinedStr_kind: |
| 4219 | case FormattedValue_kind: |
| 4220 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4221 | "not %.200s; " |
| 4222 | "perhaps you missed a comma?", |
| 4223 | infer_type(e)->tp_name, |
| 4224 | index_type->tp_name); |
| 4225 | default: |
| 4226 | return 1; |
| 4227 | } |
| 4228 | } |
| 4229 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4230 | // 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] | 4231 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4232 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4233 | { |
| 4234 | Py_ssize_t argsl, i; |
| 4235 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4236 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4237 | |
| 4238 | /* Check that the call node is an attribute access, and that |
| 4239 | the call doesn't have keyword parameters. */ |
| 4240 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4241 | asdl_seq_LEN(e->v.Call.keywords)) { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4242 | return -1; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4243 | } |
| 4244 | /* Check that there aren't too many arguments */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4245 | argsl = asdl_seq_LEN(args); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4246 | if (argsl >= STACK_USE_GUIDELINE) { |
| 4247 | return -1; |
| 4248 | } |
| 4249 | /* Check that there are no *varargs types of arguments. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4250 | for (i = 0; i < argsl; i++) { |
| 4251 | expr_ty elt = asdl_seq_GET(args, i); |
| 4252 | if (elt->kind == Starred_kind) { |
| 4253 | return -1; |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | /* Alright, we can optimize the code. */ |
| 4258 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4259 | int old_lineno = c->u->u_lineno; |
| 4260 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4261 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4262 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4263 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4264 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4265 | return 1; |
| 4266 | } |
| 4267 | |
| 4268 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4269 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4270 | { |
| 4271 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4272 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4273 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4274 | if (key->arg == NULL) { |
| 4275 | continue; |
| 4276 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4277 | if (forbidden_name(c, key->arg, Store)) { |
| 4278 | return -1; |
| 4279 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4280 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4281 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4282 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 4283 | SET_LOC(c, other); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4284 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4285 | return -1; |
| 4286 | } |
| 4287 | } |
| 4288 | } |
| 4289 | return 0; |
| 4290 | } |
| 4291 | |
| 4292 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4293 | compiler_call(struct compiler *c, expr_ty e) |
| 4294 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4295 | int ret = maybe_optimize_method_call(c, e); |
| 4296 | if (ret >= 0) { |
| 4297 | return ret; |
| 4298 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4299 | if (!check_caller(c, e->v.Call.func)) { |
| 4300 | return 0; |
| 4301 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | VISIT(c, expr, e->v.Call.func); |
| 4303 | return compiler_call_helper(c, 0, |
| 4304 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4305 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4308 | static int |
| 4309 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4310 | { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4311 | |
| 4312 | Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); |
| 4313 | if (value_count > STACK_USE_GUIDELINE) { |
| 4314 | ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0)); |
| 4315 | PyObject *join = _PyUnicode_FromASCII("join", 4); |
| 4316 | if (join == NULL) { |
| 4317 | return 0; |
| 4318 | } |
| 4319 | ADDOP_NAME(c, LOAD_METHOD, join, names); |
| 4320 | Py_DECREF(join); |
| 4321 | ADDOP_I(c, BUILD_LIST, 0); |
| 4322 | for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { |
| 4323 | VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); |
| 4324 | ADDOP_I(c, LIST_APPEND, 1); |
| 4325 | } |
| 4326 | ADDOP_I(c, CALL_METHOD, 1); |
| 4327 | } |
| 4328 | else { |
| 4329 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
| 4330 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { |
| 4331 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
| 4332 | } |
| 4333 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4334 | return 1; |
| 4335 | } |
| 4336 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4337 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4338 | static int |
| 4339 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4340 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4341 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4342 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4343 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4344 | Convert the conversion char to 3 bits: |
| 4345 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4346 | !s : 001 0x1 FVC_STR |
| 4347 | !r : 010 0x2 FVC_REPR |
| 4348 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4349 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4350 | next bit is whether or not we have a format spec: |
| 4351 | yes : 100 0x4 |
| 4352 | no : 000 0x0 |
| 4353 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4354 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4355 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4356 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4357 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4358 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4359 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4360 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4361 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4362 | case 's': oparg = FVC_STR; break; |
| 4363 | case 'r': oparg = FVC_REPR; break; |
| 4364 | case 'a': oparg = FVC_ASCII; break; |
| 4365 | case -1: oparg = FVC_NONE; break; |
| 4366 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4367 | PyErr_Format(PyExc_SystemError, |
| 4368 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4369 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4370 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4371 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4372 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4373 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4374 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4375 | } |
| 4376 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4377 | /* And push our opcode and oparg */ |
| 4378 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4379 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4380 | return 1; |
| 4381 | } |
| 4382 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4383 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4384 | 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] | 4385 | { |
| 4386 | Py_ssize_t i, n = end - begin; |
| 4387 | keyword_ty kw; |
| 4388 | PyObject *keys, *key; |
| 4389 | assert(n > 0); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4390 | int big = n*2 > STACK_USE_GUIDELINE; |
| 4391 | if (n > 1 && !big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4392 | for (i = begin; i < end; i++) { |
| 4393 | kw = asdl_seq_GET(keywords, i); |
| 4394 | VISIT(c, expr, kw->value); |
| 4395 | } |
| 4396 | keys = PyTuple_New(n); |
| 4397 | if (keys == NULL) { |
| 4398 | return 0; |
| 4399 | } |
| 4400 | for (i = begin; i < end; i++) { |
| 4401 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4402 | Py_INCREF(key); |
| 4403 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4404 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4405 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4406 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4407 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4408 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4409 | if (big) { |
| 4410 | ADDOP_I_NOLINE(c, BUILD_MAP, 0); |
| 4411 | } |
| 4412 | for (i = begin; i < end; i++) { |
| 4413 | kw = asdl_seq_GET(keywords, i); |
| 4414 | ADDOP_LOAD_CONST(c, kw->arg); |
| 4415 | VISIT(c, expr, kw->value); |
| 4416 | if (big) { |
| 4417 | ADDOP_I_NOLINE(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4418 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4419 | } |
| 4420 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4421 | ADDOP_I(c, BUILD_MAP, n); |
| 4422 | } |
| 4423 | return 1; |
| 4424 | } |
| 4425 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4426 | /* shared code between compiler_call and compiler_class */ |
| 4427 | static int |
| 4428 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4429 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4430 | asdl_expr_seq *args, |
| 4431 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4432 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4433 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4434 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4435 | if (validate_keywords(c, keywords) == -1) { |
| 4436 | return 0; |
| 4437 | } |
| 4438 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4439 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4440 | nkwelts = asdl_seq_LEN(keywords); |
| 4441 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4442 | if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) { |
| 4443 | goto ex_call; |
| 4444 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4445 | for (i = 0; i < nelts; i++) { |
| 4446 | expr_ty elt = asdl_seq_GET(args, i); |
| 4447 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4448 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4449 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4450 | } |
| 4451 | for (i = 0; i < nkwelts; i++) { |
| 4452 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4453 | if (kw->arg == NULL) { |
| 4454 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4455 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4456 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4457 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4458 | /* No * or ** args, so can use faster calling sequence */ |
| 4459 | for (i = 0; i < nelts; i++) { |
| 4460 | expr_ty elt = asdl_seq_GET(args, i); |
| 4461 | assert(elt->kind != Starred_kind); |
| 4462 | VISIT(c, expr, elt); |
| 4463 | } |
| 4464 | if (nkwelts) { |
| 4465 | PyObject *names; |
| 4466 | VISIT_SEQ(c, keyword, keywords); |
| 4467 | names = PyTuple_New(nkwelts); |
| 4468 | if (names == NULL) { |
| 4469 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4470 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4471 | for (i = 0; i < nkwelts; i++) { |
| 4472 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4473 | Py_INCREF(kw->arg); |
| 4474 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4475 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4476 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4477 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4478 | return 1; |
| 4479 | } |
| 4480 | else { |
| 4481 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4482 | return 1; |
| 4483 | } |
| 4484 | |
| 4485 | ex_call: |
| 4486 | |
| 4487 | /* Do positional arguments. */ |
| 4488 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4489 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4490 | } |
| 4491 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4492 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4493 | return 0; |
| 4494 | } |
| 4495 | /* Then keyword arguments */ |
| 4496 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4497 | /* Has a new dict been pushed */ |
| 4498 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4499 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4500 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4501 | for (i = 0; i < nkwelts; i++) { |
| 4502 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4503 | if (kw->arg == NULL) { |
| 4504 | /* A keyword argument unpacking. */ |
| 4505 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4506 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4507 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4508 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4509 | if (have_dict) { |
| 4510 | ADDOP_I(c, DICT_MERGE, 1); |
| 4511 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4512 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4513 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4514 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4515 | if (!have_dict) { |
| 4516 | ADDOP_I(c, BUILD_MAP, 0); |
| 4517 | have_dict = 1; |
| 4518 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4519 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4520 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4521 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4522 | else { |
| 4523 | nseen++; |
| 4524 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4525 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4526 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4527 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4528 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4529 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4530 | } |
| 4531 | if (have_dict) { |
| 4532 | ADDOP_I(c, DICT_MERGE, 1); |
| 4533 | } |
| 4534 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4535 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4536 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4537 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4538 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4539 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4542 | |
| 4543 | /* List and set comprehensions and generator expressions work by creating a |
| 4544 | nested function to perform the actual iteration. This means that the |
| 4545 | iteration variables don't leak into the current scope. |
| 4546 | The defined function is called immediately following its definition, with the |
| 4547 | result of that call being the result of the expression. |
| 4548 | The LC/SC version returns the populated container, while the GE version is |
| 4549 | flagged in symtable.c as a generator, so it returns the generator object |
| 4550 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4551 | |
| 4552 | Possible cleanups: |
| 4553 | - iterate over the generator sequence instead of using recursion |
| 4554 | */ |
| 4555 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4556 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4557 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4558 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4559 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4560 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4562 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4563 | comprehension_ty gen; |
| 4564 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4565 | if (gen->is_async) { |
| 4566 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4567 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4568 | } else { |
| 4569 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4570 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4571 | } |
| 4572 | } |
| 4573 | |
| 4574 | static int |
| 4575 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4576 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4577 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4578 | expr_ty elt, expr_ty val, int type) |
| 4579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | /* generate code for the iterator, then each of the ifs, |
| 4581 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4583 | comprehension_ty gen; |
| 4584 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4585 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | start = compiler_new_block(c); |
| 4588 | skip = compiler_new_block(c); |
| 4589 | if_cleanup = compiler_new_block(c); |
| 4590 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4593 | anchor == NULL) |
| 4594 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4596 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
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 | if (gen_index == 0) { |
| 4599 | /* Receive outermost iter as an implicit argument */ |
| 4600 | c->u->u_argcount = 1; |
| 4601 | ADDOP_I(c, LOAD_FAST, 0); |
| 4602 | } |
| 4603 | else { |
| 4604 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4605 | /* Fast path for the temporary variable assignment idiom: |
| 4606 | for y in [f(x)] |
| 4607 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4608 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4609 | switch (gen->iter->kind) { |
| 4610 | case List_kind: |
| 4611 | elts = gen->iter->v.List.elts; |
| 4612 | break; |
| 4613 | case Tuple_kind: |
| 4614 | elts = gen->iter->v.Tuple.elts; |
| 4615 | break; |
| 4616 | default: |
| 4617 | elts = NULL; |
| 4618 | } |
| 4619 | if (asdl_seq_LEN(elts) == 1) { |
| 4620 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4621 | if (elt->kind != Starred_kind) { |
| 4622 | VISIT(c, expr, elt); |
| 4623 | start = NULL; |
| 4624 | } |
| 4625 | } |
| 4626 | if (start) { |
| 4627 | VISIT(c, expr, gen->iter); |
| 4628 | ADDOP(c, GET_ITER); |
| 4629 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4631 | if (start) { |
| 4632 | depth++; |
| 4633 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4634 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4635 | NEXT_BLOCK(c); |
| 4636 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4639 | /* XXX this needs to be cleaned up...a lot! */ |
| 4640 | n = asdl_seq_LEN(gen->ifs); |
| 4641 | for (i = 0; i < n; i++) { |
| 4642 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4643 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4644 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | NEXT_BLOCK(c); |
| 4646 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4648 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4649 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4650 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4651 | elt, val, type)) |
| 4652 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | /* only append after the last for generator */ |
| 4655 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4656 | /* comprehension specific code */ |
| 4657 | switch (type) { |
| 4658 | case COMP_GENEXP: |
| 4659 | VISIT(c, expr, elt); |
| 4660 | ADDOP(c, YIELD_VALUE); |
| 4661 | ADDOP(c, POP_TOP); |
| 4662 | break; |
| 4663 | case COMP_LISTCOMP: |
| 4664 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4665 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | break; |
| 4667 | case COMP_SETCOMP: |
| 4668 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4669 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | break; |
| 4671 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4672 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4675 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4676 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | break; |
| 4678 | default: |
| 4679 | return 0; |
| 4680 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | compiler_use_next_block(c, skip); |
| 4683 | } |
| 4684 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4685 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4686 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4687 | compiler_use_next_block(c, anchor); |
| 4688 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4689 | |
| 4690 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4694 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4695 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4696 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4697 | expr_ty elt, expr_ty val, int type) |
| 4698 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4699 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4700 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4701 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4702 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4703 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4704 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4705 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4706 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4707 | return 0; |
| 4708 | } |
| 4709 | |
| 4710 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4711 | |
| 4712 | if (gen_index == 0) { |
| 4713 | /* Receive outermost iter as an implicit argument */ |
| 4714 | c->u->u_argcount = 1; |
| 4715 | ADDOP_I(c, LOAD_FAST, 0); |
| 4716 | } |
| 4717 | else { |
| 4718 | /* Sub-iter - calculate on the fly */ |
| 4719 | VISIT(c, expr, gen->iter); |
| 4720 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4721 | } |
| 4722 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4723 | compiler_use_next_block(c, start); |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4724 | /* Runtime will push a block here, so we need to account for that */ |
| 4725 | if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start, |
| 4726 | NULL, NULL)) { |
| 4727 | return 0; |
| 4728 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4729 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4730 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4731 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4732 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4733 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4734 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4735 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4736 | |
| 4737 | n = asdl_seq_LEN(gen->ifs); |
| 4738 | for (i = 0; i < n; i++) { |
| 4739 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4740 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4741 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4742 | NEXT_BLOCK(c); |
| 4743 | } |
| 4744 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4745 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4746 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4747 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4748 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4749 | elt, val, type)) |
| 4750 | return 0; |
| 4751 | |
| 4752 | /* only append after the last for generator */ |
| 4753 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4754 | /* comprehension specific code */ |
| 4755 | switch (type) { |
| 4756 | case COMP_GENEXP: |
| 4757 | VISIT(c, expr, elt); |
| 4758 | ADDOP(c, YIELD_VALUE); |
| 4759 | ADDOP(c, POP_TOP); |
| 4760 | break; |
| 4761 | case COMP_LISTCOMP: |
| 4762 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4763 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4764 | break; |
| 4765 | case COMP_SETCOMP: |
| 4766 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4767 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4768 | break; |
| 4769 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4770 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4771 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4772 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4773 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4774 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4775 | break; |
| 4776 | default: |
| 4777 | return 0; |
| 4778 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4779 | } |
| 4780 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4781 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4782 | |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4783 | compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); |
| 4784 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4785 | compiler_use_next_block(c, except); |
| 4786 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4787 | |
| 4788 | return 1; |
| 4789 | } |
| 4790 | |
| 4791 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4792 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4793 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4794 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4797 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4798 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4799 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4800 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4801 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4802 | |
Batuhan TaĆkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4803 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4804 | |
Batuhan TaĆkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4805 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4806 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4807 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4810 | } |
Mark Shannon | 7674c83 | 2021-06-21 11:47:16 +0100 | [diff] [blame] | 4811 | SET_LOC(c, e); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4812 | |
| 4813 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4814 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4815 | 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] | 4816 | compiler_error(c, "asynchronous comprehension outside of " |
| 4817 | "an asynchronous function"); |
| 4818 | goto error_in_scope; |
| 4819 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4821 | if (type != COMP_GENEXP) { |
| 4822 | int op; |
| 4823 | switch (type) { |
| 4824 | case COMP_LISTCOMP: |
| 4825 | op = BUILD_LIST; |
| 4826 | break; |
| 4827 | case COMP_SETCOMP: |
| 4828 | op = BUILD_SET; |
| 4829 | break; |
| 4830 | case COMP_DICTCOMP: |
| 4831 | op = BUILD_MAP; |
| 4832 | break; |
| 4833 | default: |
| 4834 | PyErr_Format(PyExc_SystemError, |
| 4835 | "unknown comprehension type %d", type); |
| 4836 | goto error_in_scope; |
| 4837 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | ADDOP_I(c, op, 0); |
| 4840 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4841 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4842 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | val, type)) |
| 4844 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4846 | if (type != COMP_GENEXP) { |
| 4847 | ADDOP(c, RETURN_VALUE); |
| 4848 | } |
| 4849 | |
| 4850 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4851 | qualname = c->u->u_qualname; |
| 4852 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4853 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4854 | if (top_level_await && is_async_generator){ |
| 4855 | c->u->u_ste->ste_coroutine = 1; |
| 4856 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4857 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4858 | goto error; |
| 4859 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4860 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4862 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4863 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4864 | Py_DECREF(co); |
| 4865 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4866 | VISIT(c, expr, outermost->iter); |
| 4867 | |
| 4868 | if (outermost->is_async) { |
| 4869 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4870 | } else { |
| 4871 | ADDOP(c, GET_ITER); |
| 4872 | } |
| 4873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4875 | |
| 4876 | if (is_async_generator && type != COMP_GENEXP) { |
| 4877 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4878 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4879 | ADDOP(c, YIELD_FROM); |
| 4880 | } |
| 4881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4882 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4883 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4884 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4885 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4886 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4887 | Py_XDECREF(co); |
| 4888 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4892 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | static identifier name; |
| 4895 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4896 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4897 | if (!name) |
| 4898 | return 0; |
| 4899 | } |
| 4900 | assert(e->kind == GeneratorExp_kind); |
| 4901 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4902 | e->v.GeneratorExp.generators, |
| 4903 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4907 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4909 | static identifier name; |
| 4910 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4911 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4912 | if (!name) |
| 4913 | return 0; |
| 4914 | } |
| 4915 | assert(e->kind == ListComp_kind); |
| 4916 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4917 | e->v.ListComp.generators, |
| 4918 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | static int |
| 4922 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | static identifier name; |
| 4925 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4926 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4927 | if (!name) |
| 4928 | return 0; |
| 4929 | } |
| 4930 | assert(e->kind == SetComp_kind); |
| 4931 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4932 | e->v.SetComp.generators, |
| 4933 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | |
| 4937 | static int |
| 4938 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4940 | static identifier name; |
| 4941 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4942 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | if (!name) |
| 4944 | return 0; |
| 4945 | } |
| 4946 | assert(e->kind == DictComp_kind); |
| 4947 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4948 | e->v.DictComp.generators, |
| 4949 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | |
| 4953 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4954 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | VISIT(c, expr, k->value); |
| 4957 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4960 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4961 | whether they are true or false. |
| 4962 | |
| 4963 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4964 | */ |
| 4965 | |
| 4966 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4967 | compiler_with_except_finish(struct compiler *c) { |
| 4968 | basicblock *exit; |
| 4969 | exit = compiler_new_block(c); |
| 4970 | if (exit == NULL) |
| 4971 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4972 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4973 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4974 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4975 | compiler_use_next_block(c, exit); |
| 4976 | ADDOP(c, POP_TOP); |
| 4977 | ADDOP(c, POP_TOP); |
| 4978 | ADDOP(c, POP_TOP); |
| 4979 | ADDOP(c, POP_EXCEPT); |
| 4980 | ADDOP(c, POP_TOP); |
| 4981 | return 1; |
| 4982 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4983 | |
| 4984 | /* |
| 4985 | Implements the async with statement. |
| 4986 | |
| 4987 | The semantics outlined in that PEP are as follows: |
| 4988 | |
| 4989 | async with EXPR as VAR: |
| 4990 | BLOCK |
| 4991 | |
| 4992 | It is implemented roughly as: |
| 4993 | |
| 4994 | context = EXPR |
| 4995 | exit = context.__aexit__ # not calling it |
| 4996 | value = await context.__aenter__() |
| 4997 | try: |
| 4998 | VAR = value # if VAR present in the syntax |
| 4999 | BLOCK |
| 5000 | finally: |
| 5001 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 5002 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5003 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 5004 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5005 | if not (await exit(*exc)): |
| 5006 | raise |
| 5007 | */ |
| 5008 | static int |
| 5009 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 5010 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5011 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5012 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 5013 | |
| 5014 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5015 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5016 | c->u->u_ste->ste_coroutine = 1; |
| 5017 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 5018 | return compiler_error(c, "'async with' outside async function"); |
| 5019 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5020 | |
| 5021 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5022 | final = compiler_new_block(c); |
| 5023 | exit = compiler_new_block(c); |
| 5024 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5025 | return 0; |
| 5026 | |
| 5027 | /* Evaluate EXPR */ |
| 5028 | VISIT(c, expr, item->context_expr); |
| 5029 | |
| 5030 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 5031 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5032 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5033 | ADDOP(c, YIELD_FROM); |
| 5034 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5035 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5036 | |
| 5037 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 5038 | compiler_use_next_block(c, block); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5039 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5040 | return 0; |
| 5041 | } |
| 5042 | |
| 5043 | if (item->optional_vars) { |
| 5044 | VISIT(c, expr, item->optional_vars); |
| 5045 | } |
| 5046 | else { |
| 5047 | /* Discard result from context.__aenter__() */ |
| 5048 | ADDOP(c, POP_TOP); |
| 5049 | } |
| 5050 | |
| 5051 | pos++; |
| 5052 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 5053 | /* BLOCK code */ |
| 5054 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 5055 | else if (!compiler_async_with(c, s, pos)) |
| 5056 | return 0; |
| 5057 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5058 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5059 | ADDOP(c, POP_BLOCK); |
| 5060 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5061 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5062 | /* For successful outcome: |
| 5063 | * call __exit__(None, None, None) |
| 5064 | */ |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5065 | SET_LOC(c, s); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5066 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5067 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5068 | ADDOP(c, GET_AWAITABLE); |
Miss Islington (bot) | ebbd0ac | 2021-08-31 11:08:32 -0700 | [diff] [blame] | 5069 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5070 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5071 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5072 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5073 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5074 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5075 | |
| 5076 | /* For exceptional outcome: */ |
| 5077 | compiler_use_next_block(c, final); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5078 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5079 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5080 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5081 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5082 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5083 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5084 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5085 | return 1; |
| 5086 | } |
| 5087 | |
| 5088 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5089 | /* |
| 5090 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5091 | with EXPR as VAR: |
| 5092 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5093 | is implemented as: |
| 5094 | <code for EXPR> |
| 5095 | SETUP_WITH E |
| 5096 | <code to store to VAR> or POP_TOP |
| 5097 | <code for BLOCK> |
| 5098 | LOAD_CONST (None, None, None) |
| 5099 | CALL_FUNCTION_EX 0 |
| 5100 | JUMP_FORWARD EXIT |
| 5101 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 5102 | POP_JUMP_IF_TRUE T: |
| 5103 | RERAISE |
| 5104 | T: POP_TOP * 3 (remove exception from stack) |
| 5105 | POP_EXCEPT |
| 5106 | POP_TOP |
| 5107 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5108 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5109 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5110 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5111 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5112 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5113 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5114 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5115 | |
| 5116 | assert(s->kind == With_kind); |
| 5117 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5118 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5119 | final = compiler_new_block(c); |
| 5120 | exit = compiler_new_block(c); |
| 5121 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5122 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5123 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5124 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5125 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5126 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5127 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5128 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5129 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5130 | compiler_use_next_block(c, block); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5131 | if (!compiler_push_fblock(c, WITH, block, final, s)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5132 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5135 | if (item->optional_vars) { |
| 5136 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5137 | } |
| 5138 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5139 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5140 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5143 | pos++; |
| 5144 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 5145 | /* BLOCK code */ |
| 5146 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 5147 | else if (!compiler_with(c, s, pos)) |
| 5148 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5149 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 5150 | |
| 5151 | /* Mark all following code as artificial */ |
| 5152 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5153 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5154 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5155 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5156 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5157 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5158 | /* For successful outcome: |
| 5159 | * call __exit__(None, None, None) |
| 5160 | */ |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5161 | SET_LOC(c, s); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5162 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5163 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5164 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5165 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5166 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5167 | /* For exceptional outcome: */ |
| 5168 | compiler_use_next_block(c, final); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5169 | ADDOP(c, WITH_EXCEPT_START); |
| 5170 | compiler_with_except_finish(c); |
| 5171 | |
| 5172 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5173 | return 1; |
| 5174 | } |
| 5175 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5176 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5177 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5180 | case NamedExpr_kind: |
| 5181 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5182 | ADDOP(c, DUP_TOP); |
| 5183 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5184 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5185 | case BoolOp_kind: |
| 5186 | return compiler_boolop(c, e); |
| 5187 | case BinOp_kind: |
| 5188 | VISIT(c, expr, e->v.BinOp.left); |
| 5189 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5190 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5191 | break; |
| 5192 | case UnaryOp_kind: |
| 5193 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5194 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5195 | break; |
| 5196 | case Lambda_kind: |
| 5197 | return compiler_lambda(c, e); |
| 5198 | case IfExp_kind: |
| 5199 | return compiler_ifexp(c, e); |
| 5200 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5201 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5202 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5203 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | case GeneratorExp_kind: |
| 5205 | return compiler_genexp(c, e); |
| 5206 | case ListComp_kind: |
| 5207 | return compiler_listcomp(c, e); |
| 5208 | case SetComp_kind: |
| 5209 | return compiler_setcomp(c, e); |
| 5210 | case DictComp_kind: |
| 5211 | return compiler_dictcomp(c, e); |
| 5212 | case Yield_kind: |
| 5213 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5214 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5215 | if (e->v.Yield.value) { |
| 5216 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | } |
| 5218 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5219 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5221 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5223 | case YieldFrom_kind: |
| 5224 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5225 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5226 | |
| 5227 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5228 | return compiler_error(c, "'yield from' inside async function"); |
| 5229 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5230 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5231 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5232 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5233 | ADDOP(c, YIELD_FROM); |
| 5234 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5235 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5236 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5237 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5238 | return compiler_error(c, "'await' outside function"); |
| 5239 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5240 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5241 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5242 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5243 | return compiler_error(c, "'await' outside async function"); |
| 5244 | } |
| 5245 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5246 | |
| 5247 | VISIT(c, expr, e->v.Await.value); |
| 5248 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5249 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5250 | ADDOP(c, YIELD_FROM); |
| 5251 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5252 | case Compare_kind: |
| 5253 | return compiler_compare(c, e); |
| 5254 | case Call_kind: |
| 5255 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5256 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5257 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5258 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5259 | case JoinedStr_kind: |
| 5260 | return compiler_joined_str(c, e); |
| 5261 | case FormattedValue_kind: |
| 5262 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5263 | /* The following exprs can be assignment targets. */ |
| 5264 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5265 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5268 | { |
| 5269 | int old_lineno = c->u->u_lineno; |
| 5270 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5272 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5274 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5275 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5276 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5277 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5278 | } |
| 5279 | int old_lineno = c->u->u_lineno; |
| 5280 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5282 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | break; |
| 5284 | case Del: |
| 5285 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5286 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | } |
| 5288 | break; |
| 5289 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5290 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5291 | case Starred_kind: |
| 5292 | switch (e->v.Starred.ctx) { |
| 5293 | case Store: |
| 5294 | /* In all legitimate cases, the Starred node was already replaced |
| 5295 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5296 | return compiler_error(c, |
| 5297 | "starred assignment target must be in a list or tuple"); |
| 5298 | default: |
| 5299 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5300 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5302 | break; |
| 5303 | case Slice_kind: |
| 5304 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5305 | case Name_kind: |
| 5306 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5307 | /* child nodes of List and Tuple will have expr_context set */ |
| 5308 | case List_kind: |
| 5309 | return compiler_list(c, e); |
| 5310 | case Tuple_kind: |
| 5311 | return compiler_tuple(c, e); |
| 5312 | } |
| 5313 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5317 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5318 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5319 | int old_lineno = c->u->u_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5320 | int old_end_lineno = c->u->u_end_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5321 | int old_col_offset = c->u->u_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5322 | int old_end_col_offset = c->u->u_end_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5323 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5324 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5325 | c->u->u_lineno = old_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5326 | c->u->u_end_lineno = old_end_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5327 | c->u->u_col_offset = old_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5328 | c->u->u_end_col_offset = old_end_col_offset; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5329 | return res; |
| 5330 | } |
| 5331 | |
| 5332 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5333 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5336 | expr_ty e = s->v.AugAssign.target; |
| 5337 | |
| 5338 | int old_lineno = c->u->u_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5339 | int old_end_lineno = c->u->u_end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5340 | int old_col_offset = c->u->u_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5341 | int old_end_col_offset = c->u->u_end_col_offset; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5342 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | switch (e->kind) { |
| 5345 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5346 | VISIT(c, expr, e->v.Attribute.value); |
| 5347 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5348 | int old_lineno = c->u->u_lineno; |
| 5349 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5350 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5351 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5352 | break; |
| 5353 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5354 | VISIT(c, expr, e->v.Subscript.value); |
| 5355 | VISIT(c, expr, e->v.Subscript.slice); |
| 5356 | ADDOP(c, DUP_TOP_TWO); |
| 5357 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5358 | break; |
| 5359 | case Name_kind: |
| 5360 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5361 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5362 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | default: |
| 5364 | PyErr_Format(PyExc_SystemError, |
| 5365 | "invalid node type (%d) for augmented assignment", |
| 5366 | e->kind); |
| 5367 | return 0; |
| 5368 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5369 | |
| 5370 | c->u->u_lineno = old_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5371 | c->u->u_end_lineno = old_end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5372 | c->u->u_col_offset = old_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5373 | c->u->u_end_col_offset = old_end_col_offset; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5374 | |
| 5375 | VISIT(c, expr, s->v.AugAssign.value); |
| 5376 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5377 | |
| 5378 | SET_LOC(c, e); |
| 5379 | |
| 5380 | switch (e->kind) { |
| 5381 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5382 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5383 | ADDOP(c, ROT_TWO); |
| 5384 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5385 | break; |
| 5386 | case Subscript_kind: |
| 5387 | ADDOP(c, ROT_THREE); |
| 5388 | ADDOP(c, STORE_SUBSCR); |
| 5389 | break; |
| 5390 | case Name_kind: |
| 5391 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5392 | default: |
| 5393 | Py_UNREACHABLE(); |
| 5394 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5395 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5399 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5400 | { |
| 5401 | VISIT(c, expr, e); |
| 5402 | ADDOP(c, POP_TOP); |
| 5403 | return 1; |
| 5404 | } |
| 5405 | |
| 5406 | static int |
| 5407 | check_annotation(struct compiler *c, stmt_ty s) |
| 5408 | { |
Batuhan Taskaya | 8cc3cfa | 2021-04-25 05:31:20 +0300 | [diff] [blame] | 5409 | /* Annotations of complex targets does not produce anything |
| 5410 | under annotations future */ |
| 5411 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5412 | return 1; |
| 5413 | } |
| 5414 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5415 | /* Annotations are only evaluated in a module or class. */ |
| 5416 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5417 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5418 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5419 | } |
| 5420 | return 1; |
| 5421 | } |
| 5422 | |
| 5423 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5424 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5425 | { |
| 5426 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5427 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5428 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5429 | 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] | 5430 | return 0; |
| 5431 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5432 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5433 | return 0; |
| 5434 | } |
| 5435 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5436 | return 0; |
| 5437 | } |
| 5438 | return 1; |
| 5439 | case Tuple_kind: { |
| 5440 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5441 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5442 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5443 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5444 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5445 | return 0; |
| 5446 | } |
| 5447 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5448 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5449 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5450 | default: |
| 5451 | return check_ann_expr(c, e); |
| 5452 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | static int |
| 5456 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5457 | { |
| 5458 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5459 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5460 | |
| 5461 | assert(s->kind == AnnAssign_kind); |
| 5462 | |
| 5463 | /* We perform the actual assignment first. */ |
| 5464 | if (s->v.AnnAssign.value) { |
| 5465 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5466 | VISIT(c, expr, targ); |
| 5467 | } |
| 5468 | switch (targ->kind) { |
| 5469 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5470 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5471 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5472 | /* If we have a simple name in a module or class, store annotation. */ |
| 5473 | if (s->v.AnnAssign.simple && |
| 5474 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5475 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 5476 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5477 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5478 | } |
| 5479 | else { |
| 5480 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5481 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5482 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5483 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5484 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5485 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5486 | } |
| 5487 | break; |
| 5488 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5489 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5490 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5491 | if (!s->v.AnnAssign.value && |
| 5492 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5493 | return 0; |
| 5494 | } |
| 5495 | break; |
| 5496 | case Subscript_kind: |
| 5497 | if (!s->v.AnnAssign.value && |
| 5498 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5499 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5500 | return 0; |
| 5501 | } |
| 5502 | break; |
| 5503 | default: |
| 5504 | PyErr_Format(PyExc_SystemError, |
| 5505 | "invalid node type (%d) for annotated assignment", |
| 5506 | targ->kind); |
| 5507 | return 0; |
| 5508 | } |
| 5509 | /* Annotation is evaluated last. */ |
| 5510 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5511 | return 0; |
| 5512 | } |
| 5513 | return 1; |
| 5514 | } |
| 5515 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5516 | /* Raises a SyntaxError and returns 0. |
| 5517 | If something goes wrong, a different exception may be raised. |
| 5518 | */ |
| 5519 | |
| 5520 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5521 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5522 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5523 | va_list vargs; |
| 5524 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5525 | va_start(vargs, format); |
| 5526 | #else |
| 5527 | va_start(vargs); |
| 5528 | #endif |
| 5529 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5530 | va_end(vargs); |
| 5531 | if (msg == NULL) { |
| 5532 | return 0; |
| 5533 | } |
| 5534 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5535 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | Py_INCREF(Py_None); |
| 5537 | loc = Py_None; |
| 5538 | } |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 5539 | PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename, |
| 5540 | c->u->u_lineno, c->u->u_col_offset + 1, loc, |
| 5541 | c->u->u_end_lineno, c->u->u_end_col_offset + 1); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5542 | Py_DECREF(msg); |
| 5543 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5545 | } |
| 5546 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5547 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5549 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5553 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5554 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5555 | and returns 0. |
| 5556 | */ |
| 5557 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5558 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5559 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5560 | va_list vargs; |
| 5561 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5562 | va_start(vargs, format); |
| 5563 | #else |
| 5564 | va_start(vargs); |
| 5565 | #endif |
| 5566 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5567 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5568 | if (msg == NULL) { |
| 5569 | return 0; |
| 5570 | } |
| 5571 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5572 | c->u->u_lineno, NULL, NULL) < 0) |
| 5573 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5574 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5575 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5576 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5577 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5578 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5579 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5580 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5581 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5582 | return 0; |
| 5583 | } |
| 5584 | Py_DECREF(msg); |
| 5585 | return 1; |
| 5586 | } |
| 5587 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5588 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5589 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5590 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5591 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5593 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5594 | if (ctx == Load) { |
| 5595 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5596 | return 0; |
| 5597 | } |
| 5598 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5599 | return 0; |
| 5600 | } |
| 5601 | } |
| 5602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5604 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5605 | case Store: op = STORE_SUBSCR; break; |
| 5606 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5607 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5608 | assert(op); |
| 5609 | VISIT(c, expr, e->v.Subscript.value); |
| 5610 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5611 | ADDOP(c, op); |
| 5612 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5616 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5617 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5618 | int n = 2; |
| 5619 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5621 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5622 | if (s->v.Slice.lower) { |
| 5623 | VISIT(c, expr, s->v.Slice.lower); |
| 5624 | } |
| 5625 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5626 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5627 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5629 | if (s->v.Slice.upper) { |
| 5630 | VISIT(c, expr, s->v.Slice.upper); |
| 5631 | } |
| 5632 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5633 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | if (s->v.Slice.step) { |
| 5637 | n++; |
| 5638 | VISIT(c, expr, s->v.Slice.step); |
| 5639 | } |
| 5640 | ADDOP_I(c, BUILD_SLICE, n); |
| 5641 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5644 | |
| 5645 | // PEP 634: Structural Pattern Matching |
| 5646 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5647 | // To keep things simple, all compiler_pattern_* and pattern_helper_* routines |
| 5648 | // follow the convention of consuming TOS (the subject for the given pattern) |
| 5649 | // and calling jump_to_fail_pop on failure (no match). |
| 5650 | |
| 5651 | // When calling into these routines, it's important that pc->on_top be kept |
| 5652 | // updated to reflect the current number of items that we are using on the top |
| 5653 | // of the stack: they will be popped on failure, and any name captures will be |
| 5654 | // stored *underneath* them on success. This lets us defer all names stores |
| 5655 | // until the *entire* pattern matches. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5656 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5657 | #define WILDCARD_CHECK(N) \ |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5658 | ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5659 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5660 | #define WILDCARD_STAR_CHECK(N) \ |
| 5661 | ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name) |
| 5662 | |
| 5663 | // Limit permitted subexpressions, even if the parser & AST validator let them through |
| 5664 | #define MATCH_VALUE_EXPR(N) \ |
| 5665 | ((N)->kind == Constant_kind || (N)->kind == Attribute_kind) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5666 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5667 | // Allocate or resize pc->fail_pop to allow for n items to be popped on failure. |
| 5668 | static int |
| 5669 | ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n) |
| 5670 | { |
| 5671 | Py_ssize_t size = n + 1; |
| 5672 | if (size <= pc->fail_pop_size) { |
| 5673 | return 1; |
| 5674 | } |
| 5675 | Py_ssize_t needed = sizeof(basicblock*) * size; |
| 5676 | basicblock **resized = PyObject_Realloc(pc->fail_pop, needed); |
| 5677 | if (resized == NULL) { |
| 5678 | PyErr_NoMemory(); |
| 5679 | return 0; |
| 5680 | } |
| 5681 | pc->fail_pop = resized; |
| 5682 | while (pc->fail_pop_size < size) { |
| 5683 | basicblock *new_block; |
| 5684 | RETURN_IF_FALSE(new_block = compiler_new_block(c)); |
| 5685 | pc->fail_pop[pc->fail_pop_size++] = new_block; |
| 5686 | } |
| 5687 | return 1; |
| 5688 | } |
| 5689 | |
| 5690 | // Use op to jump to the correct fail_pop block. |
| 5691 | static int |
| 5692 | jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op) |
| 5693 | { |
| 5694 | // Pop any items on the top of the stack, plus any objects we were going to |
| 5695 | // capture on success: |
| 5696 | Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores); |
| 5697 | RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops)); |
| 5698 | ADDOP_JUMP(c, op, pc->fail_pop[pops]); |
| 5699 | NEXT_BLOCK(c); |
| 5700 | return 1; |
| 5701 | } |
| 5702 | |
| 5703 | // Build all of the fail_pop blocks and reset fail_pop. |
| 5704 | static int |
| 5705 | emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc) |
| 5706 | { |
| 5707 | if (!pc->fail_pop_size) { |
| 5708 | assert(pc->fail_pop == NULL); |
| 5709 | NEXT_BLOCK(c); |
| 5710 | return 1; |
| 5711 | } |
| 5712 | while (--pc->fail_pop_size) { |
| 5713 | compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]); |
| 5714 | if (!compiler_addop(c, POP_TOP)) { |
| 5715 | pc->fail_pop_size = 0; |
| 5716 | PyObject_Free(pc->fail_pop); |
| 5717 | pc->fail_pop = NULL; |
| 5718 | return 0; |
| 5719 | } |
| 5720 | } |
| 5721 | compiler_use_next_block(c, pc->fail_pop[0]); |
| 5722 | PyObject_Free(pc->fail_pop); |
| 5723 | pc->fail_pop = NULL; |
| 5724 | return 1; |
| 5725 | } |
| 5726 | |
| 5727 | static int |
| 5728 | compiler_error_duplicate_store(struct compiler *c, identifier n) |
| 5729 | { |
| 5730 | return compiler_error(c, "multiple assignments to name %R in pattern", n); |
| 5731 | } |
| 5732 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5733 | static int |
| 5734 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5735 | { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5736 | if (n == NULL) { |
| 5737 | ADDOP(c, POP_TOP); |
| 5738 | return 1; |
| 5739 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5740 | if (forbidden_name(c, n, Store)) { |
| 5741 | return 0; |
| 5742 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5743 | // Can't assign to the same name twice: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5744 | int duplicate = PySequence_Contains(pc->stores, n); |
| 5745 | if (duplicate < 0) { |
| 5746 | return 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5747 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5748 | if (duplicate) { |
| 5749 | return compiler_error_duplicate_store(c, n); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5750 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5751 | // Rotate this object underneath any items we need to preserve: |
| 5752 | ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1); |
| 5753 | return !PyList_Append(pc->stores, n); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | |
| 5757 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5758 | pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts) |
| 5759 | { |
| 5760 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 5761 | int seen_star = 0; |
| 5762 | for (Py_ssize_t i = 0; i < n; i++) { |
| 5763 | pattern_ty elt = asdl_seq_GET(elts, i); |
| 5764 | if (elt->kind == MatchStar_kind && !seen_star) { |
| 5765 | if ((i >= (1 << 8)) || |
| 5766 | (n-i-1 >= (INT_MAX >> 8))) |
| 5767 | return compiler_error(c, |
| 5768 | "too many expressions in " |
| 5769 | "star-unpacking sequence pattern"); |
| 5770 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 5771 | seen_star = 1; |
| 5772 | } |
| 5773 | else if (elt->kind == MatchStar_kind) { |
| 5774 | return compiler_error(c, |
| 5775 | "multiple starred expressions in sequence pattern"); |
| 5776 | } |
| 5777 | } |
| 5778 | if (!seen_star) { |
| 5779 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 5780 | } |
| 5781 | return 1; |
| 5782 | } |
| 5783 | |
| 5784 | static int |
| 5785 | pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5786 | Py_ssize_t star, pattern_context *pc) |
| 5787 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5788 | RETURN_IF_FALSE(pattern_unpack_helper(c, patterns)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5789 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5790 | // We've now got a bunch of new subjects on the stack. They need to remain |
| 5791 | // there after each subpattern match: |
| 5792 | pc->on_top += size; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5793 | for (Py_ssize_t i = 0; i < size; i++) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5794 | // One less item to keep track of each time we loop through: |
| 5795 | pc->on_top--; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5796 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5797 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5798 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5799 | return 1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5803 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5804 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5805 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5806 | pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5807 | Py_ssize_t star, pattern_context *pc) |
| 5808 | { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5809 | // We need to keep the subject around for extracting elements: |
| 5810 | pc->on_top++; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5811 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5812 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5813 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 5814 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5815 | continue; |
| 5816 | } |
| 5817 | if (i == star) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5818 | assert(WILDCARD_STAR_CHECK(pattern)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5819 | continue; |
| 5820 | } |
| 5821 | ADDOP(c, DUP_TOP); |
| 5822 | if (i < star) { |
| 5823 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5824 | } |
| 5825 | else { |
| 5826 | // The subject may not support negative indexing! Compute a |
| 5827 | // nonnegative index: |
| 5828 | ADDOP(c, GET_LEN); |
| 5829 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5830 | ADDOP(c, BINARY_SUBTRACT); |
| 5831 | } |
| 5832 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5833 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5834 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5835 | // Pop the subject, we're done with it: |
| 5836 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5837 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5838 | return 1; |
| 5839 | } |
| 5840 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5841 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5842 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5843 | compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5844 | { |
| 5845 | int allow_irrefutable = pc->allow_irrefutable; |
| 5846 | pc->allow_irrefutable = 1; |
| 5847 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5848 | pc->allow_irrefutable = allow_irrefutable; |
| 5849 | return 1; |
| 5850 | } |
| 5851 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5852 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5853 | compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) |
| 5854 | { |
| 5855 | assert(p->kind == MatchAs_kind); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5856 | if (p->v.MatchAs.pattern == NULL) { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5857 | // An irrefutable match: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5858 | if (!pc->allow_irrefutable) { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5859 | if (p->v.MatchAs.name) { |
| 5860 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5861 | return compiler_error(c, e, p->v.MatchAs.name); |
| 5862 | } |
| 5863 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 5864 | return compiler_error(c, e); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5865 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5866 | return pattern_helper_store_name(c, p->v.MatchAs.name, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5867 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5868 | // Need to make a copy for (possibly) storing later: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5869 | pc->on_top++; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5870 | ADDOP(c, DUP_TOP); |
| 5871 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5872 | // Success! Store it: |
| 5873 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5874 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5875 | return 1; |
| 5876 | } |
| 5877 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5878 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5879 | compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5880 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5881 | assert(p->kind == MatchStar_kind); |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5882 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc)); |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5883 | return 1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5884 | } |
| 5885 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5886 | static int |
| 5887 | validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns) |
| 5888 | { |
| 5889 | // Any errors will point to the pattern rather than the arg name as the |
| 5890 | // parser is only supplying identifiers rather than Name or keyword nodes |
| 5891 | Py_ssize_t nattrs = asdl_seq_LEN(attrs); |
| 5892 | for (Py_ssize_t i = 0; i < nattrs; i++) { |
| 5893 | identifier attr = ((identifier)asdl_seq_GET(attrs, i)); |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5894 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i))); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5895 | if (forbidden_name(c, attr, Store)) { |
| 5896 | return -1; |
| 5897 | } |
| 5898 | for (Py_ssize_t j = i + 1; j < nattrs; j++) { |
| 5899 | identifier other = ((identifier)asdl_seq_GET(attrs, j)); |
| 5900 | if (!PyUnicode_Compare(attr, other)) { |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5901 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j))); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5902 | compiler_error(c, "attribute name repeated in class pattern: %U", attr); |
| 5903 | return -1; |
| 5904 | } |
| 5905 | } |
| 5906 | } |
| 5907 | return 0; |
| 5908 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5909 | |
| 5910 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5911 | compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5912 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5913 | assert(p->kind == MatchClass_kind); |
| 5914 | asdl_pattern_seq *patterns = p->v.MatchClass.patterns; |
| 5915 | asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs; |
| 5916 | asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns; |
| 5917 | Py_ssize_t nargs = asdl_seq_LEN(patterns); |
| 5918 | Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs); |
| 5919 | Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns); |
| 5920 | if (nattrs != nkwd_patterns) { |
| 5921 | // AST validator shouldn't let this happen, but if it does, |
| 5922 | // just fail, don't crash out of the interpreter |
| 5923 | const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern"; |
| 5924 | return compiler_error(c, e, nattrs, nkwd_patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5925 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5926 | if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) { |
| 5927 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5928 | return compiler_error(c, e, p->v.MatchClass.cls); |
| 5929 | } |
| 5930 | if (nattrs) { |
| 5931 | RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5932 | SET_LOC(c, p); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5933 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5934 | VISIT(c, expr, p->v.MatchClass.cls); |
| 5935 | PyObject *attr_names; |
| 5936 | RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5937 | Py_ssize_t i; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5938 | for (i = 0; i < nattrs; i++) { |
| 5939 | PyObject *name = asdl_seq_GET(kwd_attrs, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5940 | Py_INCREF(name); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5941 | PyTuple_SET_ITEM(attr_names, i, name); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5942 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5943 | ADDOP_LOAD_CONST_NEW(c, attr_names); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5944 | ADDOP_I(c, MATCH_CLASS, nargs); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5945 | // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it: |
| 5946 | pc->on_top++; |
| 5947 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5948 | for (i = 0; i < nargs + nattrs; i++) { |
| 5949 | pattern_ty pattern; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5950 | if (i < nargs) { |
| 5951 | // Positional: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5952 | pattern = asdl_seq_GET(patterns, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5953 | } |
| 5954 | else { |
| 5955 | // Keyword: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5956 | pattern = asdl_seq_GET(kwd_patterns, i - nargs); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5957 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5958 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5959 | continue; |
| 5960 | } |
| 5961 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5962 | ADDOP(c, DUP_TOP); |
| 5963 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5964 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5965 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5966 | } |
| 5967 | // Success! Pop the tuple of attributes: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5968 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5969 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5970 | return 1; |
| 5971 | } |
| 5972 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5973 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5974 | compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5975 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5976 | assert(p->kind == MatchMapping_kind); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5977 | asdl_expr_seq *keys = p->v.MatchMapping.keys; |
| 5978 | asdl_pattern_seq *patterns = p->v.MatchMapping.patterns; |
| 5979 | Py_ssize_t size = asdl_seq_LEN(keys); |
| 5980 | Py_ssize_t npatterns = asdl_seq_LEN(patterns); |
| 5981 | if (size != npatterns) { |
| 5982 | // AST validator shouldn't let this happen, but if it does, |
| 5983 | // just fail, don't crash out of the interpreter |
| 5984 | const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern"; |
| 5985 | return compiler_error(c, e, size, npatterns); |
| 5986 | } |
| 5987 | // We have a double-star target if "rest" is set |
| 5988 | PyObject *star_target = p->v.MatchMapping.rest; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5989 | // We need to keep the subject on top during the mapping and length checks: |
| 5990 | pc->on_top++; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5991 | ADDOP(c, MATCH_MAPPING); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5992 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5993 | if (!size && !star_target) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5994 | // If the pattern is just "{}", we're done! Pop the subject: |
| 5995 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5996 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5997 | return 1; |
| 5998 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5999 | if (size) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6000 | // If the pattern has any keys in it, perform a length check: |
| 6001 | ADDOP(c, GET_LEN); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6002 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6003 | ADDOP_COMPARE(c, GtE); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6004 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6005 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6006 | if (INT_MAX < size - 1) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6007 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 6008 | } |
| 6009 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 6010 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6011 | |
| 6012 | // Maintaining a set of Constant_kind kind keys allows us to raise a |
| 6013 | // SyntaxError in the case of duplicates. |
| 6014 | PyObject *seen = PySet_New(NULL); |
| 6015 | if (seen == NULL) { |
| 6016 | return 0; |
| 6017 | } |
| 6018 | |
| 6019 | // NOTE: goto error on failure in the loop below to avoid leaking `seen` |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6020 | for (Py_ssize_t i = 0; i < size; i++) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6021 | expr_ty key = asdl_seq_GET(keys, i); |
| 6022 | if (key == NULL) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6023 | const char *e = "can't use NULL keys in MatchMapping " |
| 6024 | "(set 'rest' parameter instead)"; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 6025 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i))); |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6026 | compiler_error(c, e); |
| 6027 | goto error; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6028 | } |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6029 | |
| 6030 | if (key->kind == Constant_kind) { |
| 6031 | int in_seen = PySet_Contains(seen, key->v.Constant.value); |
| 6032 | if (in_seen < 0) { |
| 6033 | goto error; |
| 6034 | } |
| 6035 | if (in_seen) { |
| 6036 | const char *e = "mapping pattern checks duplicate key (%R)"; |
| 6037 | compiler_error(c, e, key->v.Constant.value); |
| 6038 | goto error; |
| 6039 | } |
| 6040 | if (PySet_Add(seen, key->v.Constant.value)) { |
| 6041 | goto error; |
| 6042 | } |
| 6043 | } |
| 6044 | |
| 6045 | else if (key->kind != Attribute_kind) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6046 | const char *e = "mapping pattern keys may only match literals and attribute lookups"; |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6047 | compiler_error(c, e); |
| 6048 | goto error; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6049 | } |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6050 | if (!compiler_visit_expr(c, key)) { |
| 6051 | goto error; |
| 6052 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6053 | } |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6054 | |
| 6055 | // all keys have been checked; there are no duplicates |
| 6056 | Py_DECREF(seen); |
| 6057 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6058 | ADDOP_I(c, BUILD_TUPLE, size); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6059 | ADDOP(c, MATCH_KEYS); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6060 | // There's now a tuple of keys and a tuple of values on top of the subject: |
| 6061 | pc->on_top += 2; |
| 6062 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
| 6063 | // So far so good. Use that tuple of values on the stack to match |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6064 | // sub-patterns against: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6065 | for (Py_ssize_t i = 0; i < size; i++) { |
| 6066 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 6067 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6068 | continue; |
| 6069 | } |
| 6070 | ADDOP(c, DUP_TOP); |
| 6071 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 6072 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6073 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6074 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6075 | // If we get this far, it's a match! We're done with the tuple of values, |
| 6076 | // and whatever happens next should consume the tuple of keys underneath it: |
| 6077 | pc->on_top -= 2; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6078 | ADDOP(c, POP_TOP); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6079 | if (star_target) { |
| 6080 | // 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] | 6081 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6082 | RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6083 | } |
| 6084 | else { |
| 6085 | // Otherwise, we don't care about this tuple of keys anymore: |
| 6086 | ADDOP(c, POP_TOP); |
| 6087 | } |
| 6088 | // Pop the subject: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6089 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6090 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6091 | return 1; |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6092 | |
| 6093 | error: |
| 6094 | Py_DECREF(seen); |
| 6095 | return 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6096 | } |
| 6097 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6098 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6099 | compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6100 | { |
| 6101 | assert(p->kind == MatchOr_kind); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6102 | basicblock *end; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6103 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6104 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 6105 | assert(size > 1); |
| 6106 | // We're going to be messing with pc. Keep the original info handy: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6107 | pattern_context old_pc = *pc; |
| 6108 | Py_INCREF(pc->stores); |
| 6109 | // control is the list of names bound by the first alternative. It is used |
| 6110 | // for checking different name bindings in alternatives, and for correcting |
| 6111 | // the order in which extracted elements are placed on the stack. |
| 6112 | PyObject *control = NULL; |
| 6113 | // NOTE: We can't use returning macros anymore! goto error on error. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6114 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6115 | pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6116 | SET_LOC(c, alt); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6117 | PyObject *pc_stores = PyList_New(0); |
| 6118 | if (pc_stores == NULL) { |
| 6119 | goto error; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6120 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6121 | Py_SETREF(pc->stores, pc_stores); |
| 6122 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 6123 | pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable; |
| 6124 | pc->fail_pop = NULL; |
| 6125 | pc->fail_pop_size = 0; |
| 6126 | pc->on_top = 0; |
| 6127 | if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) { |
| 6128 | goto error; |
| 6129 | } |
| 6130 | // Success! |
| 6131 | Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6132 | if (!i) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6133 | // This is the first alternative, so save its stores as a "control" |
| 6134 | // for the others (they can't bind a different set of names, and |
| 6135 | // might need to be reordered): |
| 6136 | assert(control == NULL); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6137 | control = pc->stores; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6138 | Py_INCREF(control); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6139 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6140 | else if (nstores != PyList_GET_SIZE(control)) { |
| 6141 | goto diff; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6142 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6143 | else if (nstores) { |
| 6144 | // There were captures. Check to see if we differ from control: |
| 6145 | Py_ssize_t icontrol = nstores; |
| 6146 | while (icontrol--) { |
| 6147 | PyObject *name = PyList_GET_ITEM(control, icontrol); |
| 6148 | Py_ssize_t istores = PySequence_Index(pc->stores, name); |
| 6149 | if (istores < 0) { |
| 6150 | PyErr_Clear(); |
| 6151 | goto diff; |
| 6152 | } |
| 6153 | if (icontrol != istores) { |
| 6154 | // Reorder the names on the stack to match the order of the |
| 6155 | // names in control. There's probably a better way of doing |
| 6156 | // this; the current solution is potentially very |
| 6157 | // inefficient when each alternative subpattern binds lots |
| 6158 | // of names in different orders. It's fine for reasonable |
| 6159 | // cases, though. |
| 6160 | assert(istores < icontrol); |
| 6161 | Py_ssize_t rotations = istores + 1; |
Christian Clauss | ccd82a0 | 2021-10-07 17:30:08 +0200 | [diff] [blame] | 6162 | // Perform the same rotation on pc->stores: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6163 | PyObject *rotated = PyList_GetSlice(pc->stores, 0, |
| 6164 | rotations); |
| 6165 | if (rotated == NULL || |
| 6166 | PyList_SetSlice(pc->stores, 0, rotations, NULL) || |
| 6167 | PyList_SetSlice(pc->stores, icontrol - istores, |
| 6168 | icontrol - istores, rotated)) |
| 6169 | { |
| 6170 | Py_XDECREF(rotated); |
| 6171 | goto error; |
| 6172 | } |
| 6173 | Py_DECREF(rotated); |
| 6174 | // That just did: |
| 6175 | // rotated = pc_stores[:rotations] |
| 6176 | // del pc_stores[:rotations] |
| 6177 | // pc_stores[icontrol-istores:icontrol-istores] = rotated |
| 6178 | // Do the same thing to the stack, using several ROT_Ns: |
| 6179 | while (rotations--) { |
| 6180 | if (!compiler_addop_i(c, ROT_N, icontrol + 1)) { |
| 6181 | goto error; |
| 6182 | } |
| 6183 | } |
| 6184 | } |
| 6185 | } |
| 6186 | } |
| 6187 | assert(control); |
| 6188 | if (!compiler_addop_j(c, JUMP_FORWARD, end) || |
| 6189 | !compiler_next_block(c) || |
| 6190 | !emit_and_reset_fail_pop(c, pc)) |
| 6191 | { |
| 6192 | goto error; |
| 6193 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6194 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6195 | Py_DECREF(pc->stores); |
| 6196 | *pc = old_pc; |
| 6197 | Py_INCREF(pc->stores); |
| 6198 | // Need to NULL this for the PyObject_Free call in the error block. |
| 6199 | old_pc.fail_pop = NULL; |
| 6200 | // No match. Pop the remaining copy of the subject and fail: |
| 6201 | if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) { |
| 6202 | goto error; |
| 6203 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6204 | compiler_use_next_block(c, end); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6205 | Py_ssize_t nstores = PyList_GET_SIZE(control); |
| 6206 | // There's a bunch of stuff on the stack between any where the new stores |
| 6207 | // are and where they need to be: |
| 6208 | // - The other stores. |
| 6209 | // - A copy of the subject. |
| 6210 | // - Anything else that may be on top of the stack. |
| 6211 | // - Any previous stores we've already stashed away on the stack. |
Pablo Galindo | 3949428 | 2021-05-03 16:20:46 +0100 | [diff] [blame] | 6212 | Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6213 | for (Py_ssize_t i = 0; i < nstores; i++) { |
| 6214 | // Rotate this capture to its proper place on the stack: |
| 6215 | if (!compiler_addop_i(c, ROT_N, nrots)) { |
| 6216 | goto error; |
| 6217 | } |
| 6218 | // Update the list of previous stores with this new name, checking for |
| 6219 | // duplicates: |
| 6220 | PyObject *name = PyList_GET_ITEM(control, i); |
| 6221 | int dupe = PySequence_Contains(pc->stores, name); |
| 6222 | if (dupe < 0) { |
| 6223 | goto error; |
| 6224 | } |
| 6225 | if (dupe) { |
| 6226 | compiler_error_duplicate_store(c, name); |
| 6227 | goto error; |
| 6228 | } |
| 6229 | if (PyList_Append(pc->stores, name)) { |
| 6230 | goto error; |
| 6231 | } |
| 6232 | } |
| 6233 | Py_DECREF(old_pc.stores); |
| 6234 | Py_DECREF(control); |
| 6235 | // NOTE: Returning macros are safe again. |
| 6236 | // Pop the copy of the subject: |
| 6237 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6238 | return 1; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6239 | diff: |
| 6240 | compiler_error(c, "alternative patterns bind different names"); |
| 6241 | error: |
| 6242 | PyObject_Free(old_pc.fail_pop); |
| 6243 | Py_DECREF(old_pc.stores); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6244 | Py_XDECREF(control); |
| 6245 | return 0; |
| 6246 | } |
| 6247 | |
| 6248 | |
| 6249 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6250 | compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6251 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6252 | assert(p->kind == MatchSequence_kind); |
| 6253 | asdl_pattern_seq *patterns = p->v.MatchSequence.patterns; |
| 6254 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6255 | Py_ssize_t star = -1; |
| 6256 | int only_wildcard = 1; |
| 6257 | int star_wildcard = 0; |
| 6258 | // Find a starred name, if it exists. There may be at most one: |
| 6259 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6260 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 6261 | if (pattern->kind == MatchStar_kind) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6262 | if (star >= 0) { |
| 6263 | const char *e = "multiple starred names in sequence pattern"; |
| 6264 | return compiler_error(c, e); |
| 6265 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6266 | star_wildcard = WILDCARD_STAR_CHECK(pattern); |
| 6267 | only_wildcard &= star_wildcard; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6268 | star = i; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6269 | continue; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6270 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6271 | only_wildcard &= WILDCARD_CHECK(pattern); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6272 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6273 | // We need to keep the subject on top during the sequence and length checks: |
| 6274 | pc->on_top++; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6275 | ADDOP(c, MATCH_SEQUENCE); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6276 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6277 | if (star < 0) { |
| 6278 | // No star: len(subject) == size |
| 6279 | ADDOP(c, GET_LEN); |
| 6280 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 6281 | ADDOP_COMPARE(c, Eq); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6282 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6283 | } |
| 6284 | else if (size > 1) { |
| 6285 | // Star: len(subject) >= size - 1 |
| 6286 | ADDOP(c, GET_LEN); |
| 6287 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 6288 | ADDOP_COMPARE(c, GtE); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6289 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6290 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6291 | // Whatever comes next should consume the subject: |
| 6292 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6293 | if (only_wildcard) { |
| 6294 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 6295 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6296 | } |
| 6297 | else if (star_wildcard) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6298 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6299 | } |
| 6300 | else { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6301 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6302 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6303 | return 1; |
| 6304 | } |
| 6305 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6306 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6307 | compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6308 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6309 | assert(p->kind == MatchValue_kind); |
| 6310 | expr_ty value = p->v.MatchValue.value; |
| 6311 | if (!MATCH_VALUE_EXPR(value)) { |
| 6312 | const char *e = "patterns may only match literals and attribute lookups"; |
| 6313 | return compiler_error(c, e); |
| 6314 | } |
| 6315 | VISIT(c, expr, value); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6316 | ADDOP_COMPARE(c, Eq); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6317 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6318 | return 1; |
| 6319 | } |
| 6320 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6321 | static int |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 6322 | compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6323 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6324 | assert(p->kind == MatchSingleton_kind); |
| 6325 | ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value); |
| 6326 | ADDOP_COMPARE(c, Is); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6327 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6328 | return 1; |
| 6329 | } |
| 6330 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6331 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6332 | compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6333 | { |
| 6334 | SET_LOC(c, p); |
| 6335 | switch (p->kind) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6336 | case MatchValue_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6337 | return compiler_pattern_value(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6338 | case MatchSingleton_kind: |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 6339 | return compiler_pattern_singleton(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6340 | case MatchSequence_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6341 | return compiler_pattern_sequence(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6342 | case MatchMapping_kind: |
| 6343 | return compiler_pattern_mapping(c, p, pc); |
| 6344 | case MatchClass_kind: |
| 6345 | return compiler_pattern_class(c, p, pc); |
| 6346 | case MatchStar_kind: |
| 6347 | return compiler_pattern_star(c, p, pc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6348 | case MatchAs_kind: |
| 6349 | return compiler_pattern_as(c, p, pc); |
| 6350 | case MatchOr_kind: |
| 6351 | return compiler_pattern_or(c, p, pc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6352 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6353 | // AST validator shouldn't let this happen, but if it does, |
| 6354 | // just fail, don't crash out of the interpreter |
| 6355 | const char *e = "invalid match pattern node in AST (kind=%d)"; |
| 6356 | return compiler_error(c, e, p->kind); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6357 | } |
| 6358 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6359 | static int |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6360 | compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6361 | { |
| 6362 | VISIT(c, expr, s->v.Match.subject); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6363 | basicblock *end; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6364 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6365 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6366 | assert(cases > 0); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6367 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6368 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6369 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6370 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6371 | SET_LOC(c, m->pattern); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6372 | // Only copy the subject if we're *not* on the last case: |
| 6373 | if (i != cases - has_default - 1) { |
| 6374 | ADDOP(c, DUP_TOP); |
| 6375 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6376 | RETURN_IF_FALSE(pc->stores = PyList_New(0)); |
| 6377 | // Irrefutable cases must be either guarded, last, or both: |
| 6378 | pc->allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6379 | pc->fail_pop = NULL; |
| 6380 | pc->fail_pop_size = 0; |
| 6381 | pc->on_top = 0; |
| 6382 | // NOTE: Can't use returning macros here (they'll leak pc->stores)! |
| 6383 | if (!compiler_pattern(c, m->pattern, pc)) { |
| 6384 | Py_DECREF(pc->stores); |
| 6385 | return 0; |
| 6386 | } |
| 6387 | assert(!pc->on_top); |
| 6388 | // It's a match! Store all of the captured names (they're on the stack). |
| 6389 | Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); |
| 6390 | for (Py_ssize_t n = 0; n < nstores; n++) { |
| 6391 | PyObject *name = PyList_GET_ITEM(pc->stores, n); |
| 6392 | if (!compiler_nameop(c, name, Store)) { |
| 6393 | Py_DECREF(pc->stores); |
| 6394 | return 0; |
| 6395 | } |
| 6396 | } |
| 6397 | Py_DECREF(pc->stores); |
| 6398 | // NOTE: Returning macros are safe again. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6399 | if (m->guard) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6400 | RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0)); |
| 6401 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6402 | } |
| 6403 | // Success! Pop the subject off, we're done with it: |
| 6404 | if (i != cases - has_default - 1) { |
| 6405 | ADDOP(c, POP_TOP); |
| 6406 | } |
| 6407 | VISIT_SEQ(c, stmt, m->body); |
| 6408 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Miss Islington (bot) | 01601aa | 2021-07-25 17:04:06 -0700 | [diff] [blame] | 6409 | // If the pattern fails to match, we want the line number of the |
| 6410 | // cleanup to be associated with the failed pattern, not the last line |
| 6411 | // of the body |
| 6412 | SET_LOC(c, m->pattern); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6413 | RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6414 | } |
| 6415 | if (has_default) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6416 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6417 | // pushing and popping in the loop above: |
| 6418 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6419 | SET_LOC(c, m->pattern); |
Miss Islington (bot) | 01601aa | 2021-07-25 17:04:06 -0700 | [diff] [blame] | 6420 | if (cases == 1) { |
| 6421 | // No matches. Done with the subject: |
| 6422 | ADDOP(c, POP_TOP); |
| 6423 | } |
| 6424 | else { |
| 6425 | // Show line coverage for default case (it doesn't create bytecode) |
| 6426 | ADDOP(c, NOP); |
| 6427 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6428 | if (m->guard) { |
| 6429 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6430 | } |
| 6431 | VISIT_SEQ(c, stmt, m->body); |
| 6432 | } |
| 6433 | compiler_use_next_block(c, end); |
| 6434 | return 1; |
| 6435 | } |
| 6436 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6437 | static int |
| 6438 | compiler_match(struct compiler *c, stmt_ty s) |
| 6439 | { |
| 6440 | pattern_context pc; |
| 6441 | pc.fail_pop = NULL; |
| 6442 | int result = compiler_match_inner(c, s, &pc); |
| 6443 | PyObject_Free(pc.fail_pop); |
| 6444 | return result; |
| 6445 | } |
| 6446 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6447 | #undef WILDCARD_CHECK |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6448 | #undef WILDCARD_STAR_CHECK |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6449 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6450 | /* End of the compiler section, beginning of the assembler section */ |
| 6451 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6452 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6453 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6454 | |
| 6455 | XXX must handle implicit jumps from one block to next |
| 6456 | */ |
| 6457 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6458 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6459 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6460 | int a_offset; /* offset into bytecode */ |
| 6461 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6462 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6463 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6464 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6465 | int a_lineno; /* lineno of last emitted instruction */ |
| 6466 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6467 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6468 | }; |
| 6469 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6470 | Py_LOCAL_INLINE(void) |
| 6471 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6472 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6473 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6474 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6475 | assert(b->b_startdepth < 0); |
| 6476 | b->b_startdepth = depth; |
| 6477 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6478 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6479 | } |
| 6480 | |
| 6481 | /* Find the flow path that needs the largest stack. We assume that |
| 6482 | * cycles in the flow graph have no net effect on the stack depth. |
| 6483 | */ |
| 6484 | static int |
| 6485 | stackdepth(struct compiler *c) |
| 6486 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6487 | basicblock *b, *entryblock = NULL; |
| 6488 | basicblock **stack, **sp; |
| 6489 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6490 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6491 | b->b_startdepth = INT_MIN; |
| 6492 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6493 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6494 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6495 | assert(entryblock!= NULL); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6496 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6497 | if (!stack) { |
| 6498 | PyErr_NoMemory(); |
| 6499 | return -1; |
| 6500 | } |
| 6501 | |
| 6502 | sp = stack; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6503 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
| 6504 | stackdepth_push(&sp, entryblock, 1); |
| 6505 | } else { |
| 6506 | stackdepth_push(&sp, entryblock, 0); |
| 6507 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6508 | while (sp != stack) { |
| 6509 | b = *--sp; |
| 6510 | int depth = b->b_startdepth; |
| 6511 | assert(depth >= 0); |
| 6512 | basicblock *next = b->b_next; |
| 6513 | for (int i = 0; i < b->b_iused; i++) { |
| 6514 | struct instr *instr = &b->b_instr[i]; |
| 6515 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6516 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6517 | PyErr_Format(PyExc_SystemError, |
| 6518 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6519 | instr->i_opcode, instr->i_oparg); |
| 6520 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6521 | } |
| 6522 | int new_depth = depth + effect; |
| 6523 | if (new_depth > maxdepth) { |
| 6524 | maxdepth = new_depth; |
| 6525 | } |
| 6526 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6527 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6528 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6529 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6530 | int target_depth = depth + effect; |
| 6531 | if (target_depth > maxdepth) { |
| 6532 | maxdepth = target_depth; |
| 6533 | } |
| 6534 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6535 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6536 | } |
| 6537 | depth = new_depth; |
| 6538 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6539 | instr->i_opcode == JUMP_FORWARD || |
| 6540 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6541 | instr->i_opcode == RAISE_VARARGS || |
| 6542 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6543 | { |
| 6544 | /* remaining code is dead */ |
| 6545 | next = NULL; |
| 6546 | break; |
| 6547 | } |
| 6548 | } |
| 6549 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6550 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6551 | stackdepth_push(&sp, next, depth); |
| 6552 | } |
| 6553 | } |
| 6554 | PyObject_Free(stack); |
| 6555 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | static int |
| 6559 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6562 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6563 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6564 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6565 | if (a->a_bytecode == NULL) { |
| 6566 | goto error; |
| 6567 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6568 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6569 | if (a->a_lnotab == NULL) { |
| 6570 | goto error; |
| 6571 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6572 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6573 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6574 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6575 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6577 | error: |
| 6578 | Py_XDECREF(a->a_bytecode); |
| 6579 | Py_XDECREF(a->a_lnotab); |
| 6580 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | static void |
| 6584 | assemble_free(struct assembler *a) |
| 6585 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6586 | Py_XDECREF(a->a_bytecode); |
| 6587 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6590 | static int |
| 6591 | blocksize(basicblock *b) |
| 6592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | int i; |
| 6594 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6596 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6597 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6601 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6602 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6603 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6604 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6605 | if (a->a_lnotab_off + 2 >= len) { |
| 6606 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6607 | return 0; |
| 6608 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6609 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6610 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6612 | *lnotab++ = bdelta; |
| 6613 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6614 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6615 | } |
| 6616 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6617 | /* Appends a range to the end of the line number table. See |
| 6618 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6619 | |
| 6620 | static int |
| 6621 | assemble_line_range(struct assembler *a) |
| 6622 | { |
| 6623 | int ldelta, bdelta; |
Serhiy Storchaka | 1670d59 | 2021-10-04 17:07:21 +0300 | [diff] [blame] | 6624 | bdelta = (a->a_offset - a->a_lineno_start) * sizeof(_Py_CODEUNIT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6625 | if (bdelta == 0) { |
| 6626 | return 1; |
| 6627 | } |
| 6628 | if (a->a_lineno < 0) { |
| 6629 | ldelta = -128; |
| 6630 | } |
| 6631 | else { |
| 6632 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6633 | a->a_prevlineno = a->a_lineno; |
| 6634 | while (ldelta > 127) { |
| 6635 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6636 | return 0; |
| 6637 | } |
| 6638 | ldelta -= 127; |
| 6639 | } |
| 6640 | while (ldelta < -127) { |
| 6641 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6642 | return 0; |
| 6643 | } |
| 6644 | ldelta += 127; |
| 6645 | } |
| 6646 | } |
| 6647 | assert(-128 <= ldelta && ldelta < 128); |
| 6648 | while (bdelta > 254) { |
| 6649 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6650 | return 0; |
| 6651 | } |
| 6652 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6653 | bdelta -= 254; |
| 6654 | } |
| 6655 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6656 | return 0; |
| 6657 | } |
| 6658 | a->a_lineno_start = a->a_offset; |
| 6659 | return 1; |
| 6660 | } |
| 6661 | |
| 6662 | static int |
| 6663 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6664 | { |
| 6665 | if (i->i_lineno == a->a_lineno) { |
| 6666 | return 1; |
| 6667 | } |
| 6668 | if (!assemble_line_range(a)) { |
| 6669 | return 0; |
| 6670 | } |
| 6671 | a->a_lineno = i->i_lineno; |
| 6672 | return 1; |
| 6673 | } |
| 6674 | |
| 6675 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6676 | /* assemble_emit() |
| 6677 | Extend the bytecode with a new instruction. |
| 6678 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6679 | */ |
| 6680 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6681 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6682 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6683 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6684 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6685 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6686 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6687 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6688 | arg = i->i_oparg; |
| 6689 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6690 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6691 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6692 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6693 | if (len > PY_SSIZE_T_MAX / 2) |
| 6694 | return 0; |
| 6695 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6696 | return 0; |
| 6697 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6698 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6699 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6700 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6701 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6702 | } |
| 6703 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6704 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6705 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6707 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6708 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6709 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6711 | /* Compute the size of each block and fixup jump args. |
| 6712 | Replace block pointer with position in bytecode. */ |
| 6713 | do { |
| 6714 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6715 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6716 | bsize = blocksize(b); |
| 6717 | b->b_offset = totsize; |
| 6718 | totsize += bsize; |
| 6719 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6720 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6721 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6722 | bsize = b->b_offset; |
| 6723 | for (i = 0; i < b->b_iused; i++) { |
| 6724 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6725 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6726 | /* Relative jumps are computed relative to |
| 6727 | the instruction pointer after fetching |
| 6728 | the jump instruction. |
| 6729 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6730 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6731 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6732 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6733 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6734 | instr->i_oparg -= bsize; |
| 6735 | } |
| 6736 | if (instrsize(instr->i_oparg) != isize) { |
| 6737 | extended_arg_recompile = 1; |
| 6738 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6739 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6740 | } |
| 6741 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6743 | /* XXX: This is an awful hack that could hurt performance, but |
| 6744 | on the bright side it should work until we come up |
| 6745 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6747 | The issue is that in the first loop blocksize() is called |
| 6748 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6749 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6750 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6752 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6753 | The only EXTENDED_ARGs that could be popping up are |
| 6754 | ones in jump instructions. So this should converge |
| 6755 | fairly quickly. |
| 6756 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6757 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6758 | } |
| 6759 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6760 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6761 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6763 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6764 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6766 | tuple = PyTuple_New(size); |
| 6767 | if (tuple == NULL) |
| 6768 | return NULL; |
| 6769 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6770 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6771 | Py_INCREF(k); |
| 6772 | assert((i - offset) < size); |
| 6773 | assert((i - offset) >= 0); |
| 6774 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6775 | } |
| 6776 | return tuple; |
| 6777 | } |
| 6778 | |
| 6779 | static PyObject * |
| 6780 | consts_dict_keys_inorder(PyObject *dict) |
| 6781 | { |
| 6782 | PyObject *consts, *k, *v; |
| 6783 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6784 | |
| 6785 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6786 | if (consts == NULL) |
| 6787 | return NULL; |
| 6788 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6789 | i = PyLong_AS_LONG(v); |
Christian Clauss | ccd82a0 | 2021-10-07 17:30:08 +0200 | [diff] [blame] | 6790 | /* The keys of the dictionary can be tuples wrapping a constant. |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6791 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6792 | * the object we want is always second. */ |
| 6793 | if (PyTuple_CheckExact(k)) { |
| 6794 | k = PyTuple_GET_ITEM(k, 1); |
| 6795 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6796 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6797 | assert(i < size); |
| 6798 | assert(i >= 0); |
| 6799 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6800 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6801 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6804 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6805 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6807 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6808 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6809 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6810 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6811 | if (ste->ste_nested) |
| 6812 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6813 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6814 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6815 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6816 | flags |= CO_COROUTINE; |
| 6817 | if (ste->ste_generator && ste->ste_coroutine) |
| 6818 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6819 | if (ste->ste_varargs) |
| 6820 | flags |= CO_VARARGS; |
| 6821 | if (ste->ste_varkeywords) |
| 6822 | flags |= CO_VARKEYWORDS; |
| 6823 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6825 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6826 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6827 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6828 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6829 | ste->ste_coroutine && |
| 6830 | !ste->ste_generator) { |
| 6831 | flags |= CO_COROUTINE; |
| 6832 | } |
| 6833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6834 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6837 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6838 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6839 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6840 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6841 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6842 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6843 | if (key == NULL) { |
| 6844 | return 0; |
| 6845 | } |
| 6846 | |
| 6847 | // t is borrowed reference |
| 6848 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6849 | Py_DECREF(key); |
| 6850 | if (t == NULL) { |
| 6851 | return 0; |
| 6852 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6853 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6854 | return 1; |
| 6855 | } |
| 6856 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6857 | if (PyTuple_CheckExact(t)) { |
| 6858 | // t is still borrowed reference |
| 6859 | t = PyTuple_GET_ITEM(t, 1); |
| 6860 | } |
| 6861 | |
| 6862 | Py_INCREF(t); |
| 6863 | Py_DECREF(*obj); |
| 6864 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6865 | return 1; |
| 6866 | } |
| 6867 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6868 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6869 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6871 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6872 | PyObject *names = NULL; |
| 6873 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6874 | PyObject *name = NULL; |
| 6875 | PyObject *freevars = NULL; |
| 6876 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6877 | Py_ssize_t nlocals; |
| 6878 | int nlocals_int; |
| 6879 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6880 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6882 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6883 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6884 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6885 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6886 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6887 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6888 | if (!cellvars) |
| 6889 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6890 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6891 | if (!freevars) |
| 6892 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6893 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6894 | if (!merge_const_one(c, &names) || |
| 6895 | !merge_const_one(c, &varnames) || |
| 6896 | !merge_const_one(c, &cellvars) || |
| 6897 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6898 | { |
| 6899 | goto error; |
| 6900 | } |
| 6901 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6902 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6903 | assert(nlocals < INT_MAX); |
| 6904 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6906 | flags = compute_code_flags(c); |
| 6907 | if (flags < 0) |
| 6908 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6909 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6910 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6911 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6912 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6913 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6914 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6915 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6916 | goto error; |
| 6917 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6918 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6919 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6920 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6921 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6922 | maxdepth = stackdepth(c); |
| 6923 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6924 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6925 | goto error; |
| 6926 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 6927 | if (maxdepth > MAX_ALLOWED_STACK_USE) { |
| 6928 | PyErr_Format(PyExc_SystemError, |
| 6929 | "excessive stack use: stack is %d deep", |
| 6930 | maxdepth); |
| 6931 | Py_DECREF(consts); |
| 6932 | goto error; |
| 6933 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6934 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6935 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6936 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6937 | varnames, freevars, cellvars, c->c_filename, |
| 6938 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6939 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6940 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6941 | Py_XDECREF(names); |
| 6942 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6943 | Py_XDECREF(name); |
| 6944 | Py_XDECREF(freevars); |
| 6945 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6946 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6949 | |
| 6950 | /* For debugging purposes only */ |
| 6951 | #if 0 |
| 6952 | static void |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 6953 | dump_instr(struct instr *i) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6954 | { |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 6955 | const char *jrel = (is_relative_jump(i)) ? "jrel " : ""; |
| 6956 | const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : ""; |
| 6957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6958 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6960 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6961 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6962 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6963 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6964 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6965 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | static void |
| 6969 | dump_basicblock(const basicblock *b) |
| 6970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6971 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6972 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6973 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6974 | if (b->b_instr) { |
| 6975 | int i; |
| 6976 | for (i = 0; i < b->b_iused; i++) { |
| 6977 | fprintf(stderr, " [%02d] ", i); |
| 6978 | dump_instr(b->b_instr + i); |
| 6979 | } |
| 6980 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6981 | } |
| 6982 | #endif |
| 6983 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6984 | |
| 6985 | static int |
| 6986 | normalize_basic_block(basicblock *bb); |
| 6987 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6988 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6989 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6990 | |
Ćukasz Langa | d41abe8 | 2021-09-08 18:25:09 +0200 | [diff] [blame] | 6991 | static int |
| 6992 | trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts); |
| 6993 | |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 6994 | /* Duplicates exit BBs, so that line numbers can be propagated to them */ |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6995 | static int |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 6996 | duplicate_exits_without_lineno(struct compiler *c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6997 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6998 | static int |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 6999 | extend_block(basicblock *bb); |
| 7000 | |
| 7001 | static int |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 7002 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
| 7003 | |
| 7004 | int flags = compute_code_flags(c); |
| 7005 | if (flags < 0) { |
| 7006 | return -1; |
| 7007 | } |
| 7008 | int kind; |
| 7009 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 7010 | if (flags & CO_COROUTINE) { |
| 7011 | kind = 1; |
| 7012 | } |
| 7013 | else if (flags & CO_ASYNC_GENERATOR) { |
| 7014 | kind = 2; |
| 7015 | } |
| 7016 | else { |
| 7017 | kind = 0; |
| 7018 | } |
| 7019 | } |
| 7020 | else { |
| 7021 | return 0; |
| 7022 | } |
| 7023 | if (compiler_next_instr(entryblock) < 0) { |
| 7024 | return -1; |
| 7025 | } |
| 7026 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
| 7027 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
| 7028 | } |
| 7029 | entryblock->b_instr[0].i_opcode = GEN_START; |
| 7030 | entryblock->b_instr[0].i_oparg = kind; |
| 7031 | entryblock->b_instr[0].i_lineno = -1; |
| 7032 | entryblock->b_instr[0].i_target = NULL; |
| 7033 | return 0; |
| 7034 | } |
| 7035 | |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7036 | /* Make sure that all returns have a line number, even if early passes |
| 7037 | * have failed to propagate a correct line number. |
| 7038 | * The resulting line number may not be correct according to PEP 626, |
| 7039 | * but should be "good enough", and no worse than in older versions. */ |
| 7040 | static void |
| 7041 | guarantee_lineno_for_exits(struct assembler *a, int firstlineno) { |
| 7042 | int lineno = firstlineno; |
| 7043 | assert(lineno > 0); |
| 7044 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7045 | if (b->b_iused == 0) { |
| 7046 | continue; |
| 7047 | } |
| 7048 | struct instr *last = &b->b_instr[b->b_iused-1]; |
| 7049 | if (last->i_lineno < 0) { |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7050 | if (last->i_opcode == RETURN_VALUE) { |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7051 | for (int i = 0; i < b->b_iused; i++) { |
| 7052 | assert(b->b_instr[i].i_lineno < 0); |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7053 | |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7054 | b->b_instr[i].i_lineno = lineno; |
| 7055 | } |
| 7056 | } |
| 7057 | } |
| 7058 | else { |
| 7059 | lineno = last->i_lineno; |
| 7060 | } |
| 7061 | } |
| 7062 | } |
| 7063 | |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7064 | static void |
| 7065 | propagate_line_numbers(struct assembler *a); |
| 7066 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7067 | static PyCodeObject * |
| 7068 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 7069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7070 | basicblock *b, *entryblock; |
| 7071 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7072 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7073 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7074 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 7075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7076 | /* Make sure every block that falls off the end returns None. |
| 7077 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 7078 | block ends with a jump or return b_next shouldn't set. |
| 7079 | */ |
| 7080 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7081 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7082 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 7083 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7084 | ADDOP(c, RETURN_VALUE); |
| 7085 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7086 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7087 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7088 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 7089 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7090 | } |
| 7091 | } |
| 7092 | |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7093 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7094 | if (extend_block(b)) { |
| 7095 | return NULL; |
| 7096 | } |
| 7097 | } |
| 7098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7099 | nblocks = 0; |
| 7100 | entryblock = NULL; |
| 7101 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7102 | nblocks++; |
| 7103 | entryblock = b; |
| 7104 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 7105 | assert(entryblock != NULL); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7106 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 7107 | if (insert_generator_prefix(c, entryblock)) { |
| 7108 | goto error; |
| 7109 | } |
| 7110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7111 | /* Set firstlineno if it wasn't explicitly set. */ |
| 7112 | if (!c->u->u_firstlineno) { |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 7113 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7114 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7115 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7116 | c->u->u_firstlineno = 1; |
| 7117 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7119 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 7120 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7121 | a.a_entry = entryblock; |
| 7122 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7123 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7124 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 7125 | if (consts == NULL) { |
| 7126 | goto error; |
| 7127 | } |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7128 | |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7129 | if (optimize_cfg(c, &a, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7130 | goto error; |
| 7131 | } |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7132 | if (duplicate_exits_without_lineno(c)) { |
| 7133 | return NULL; |
| 7134 | } |
Ćukasz Langa | d41abe8 | 2021-09-08 18:25:09 +0200 | [diff] [blame] | 7135 | if (trim_unused_consts(c, &a, consts)) { |
| 7136 | goto error; |
| 7137 | } |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7138 | propagate_line_numbers(&a); |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7139 | guarantee_lineno_for_exits(&a, c->u->u_firstlineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7140 | /* Can't modify the bytecode after computing jump offsets. */ |
| 7141 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 7142 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7143 | /* Emit code. */ |
| 7144 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7145 | for (j = 0; j < b->b_iused; j++) |
| 7146 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 7147 | goto error; |
| 7148 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7149 | if (!assemble_line_range(&a)) { |
| 7150 | return 0; |
| 7151 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 7152 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 7153 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7154 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 7155 | } |
| 7156 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7157 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 7158 | } |
| 7159 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 7160 | goto error; |
| 7161 | } |
| 7162 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 7163 | goto error; |
| 7164 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7165 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7166 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7167 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7168 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7169 | assemble_free(&a); |
| 7170 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7171 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 7172 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7173 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 7174 | with LOAD_CONST (c1, c2, ... cn). |
| 7175 | The consts table must still be in list form so that the |
| 7176 | new constant (c1, c2, ... cn) can be appended. |
| 7177 | Called with codestr pointing to the first LOAD_CONST. |
| 7178 | */ |
| 7179 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7180 | fold_tuple_on_constants(struct compiler *c, |
| 7181 | struct instr *inst, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7182 | int n, PyObject *consts) |
| 7183 | { |
| 7184 | /* Pre-conditions */ |
| 7185 | assert(PyList_CheckExact(consts)); |
| 7186 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 7187 | assert(inst[n].i_oparg == n); |
| 7188 | |
| 7189 | for (int i = 0; i < n; i++) { |
| 7190 | if (inst[i].i_opcode != LOAD_CONST) { |
| 7191 | return 0; |
| 7192 | } |
| 7193 | } |
| 7194 | |
| 7195 | /* Buildup new tuple of constants */ |
| 7196 | PyObject *newconst = PyTuple_New(n); |
| 7197 | if (newconst == NULL) { |
| 7198 | return -1; |
| 7199 | } |
| 7200 | for (int i = 0; i < n; i++) { |
| 7201 | int arg = inst[i].i_oparg; |
| 7202 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 7203 | Py_INCREF(constant); |
| 7204 | PyTuple_SET_ITEM(newconst, i, constant); |
| 7205 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7206 | if (merge_const_one(c, &newconst) == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7207 | Py_DECREF(newconst); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7208 | return -1; |
| 7209 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7210 | |
| 7211 | Py_ssize_t index; |
| 7212 | for (index = 0; index < PyList_GET_SIZE(consts); index++) { |
| 7213 | if (PyList_GET_ITEM(consts, index) == newconst) { |
| 7214 | break; |
| 7215 | } |
| 7216 | } |
| 7217 | if (index == PyList_GET_SIZE(consts)) { |
| 7218 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
| 7219 | Py_DECREF(newconst); |
| 7220 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 7221 | return -1; |
| 7222 | } |
| 7223 | if (PyList_Append(consts, newconst)) { |
| 7224 | Py_DECREF(newconst); |
| 7225 | return -1; |
| 7226 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7227 | } |
| 7228 | Py_DECREF(newconst); |
| 7229 | for (int i = 0; i < n; i++) { |
| 7230 | inst[i].i_opcode = NOP; |
| 7231 | } |
| 7232 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 7233 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7234 | return 0; |
| 7235 | } |
| 7236 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7237 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 7238 | // Eliminate n * ROT_N(n). |
| 7239 | static void |
| 7240 | fold_rotations(struct instr *inst, int n) |
| 7241 | { |
| 7242 | for (int i = 0; i < n; i++) { |
| 7243 | int rot; |
| 7244 | switch (inst[i].i_opcode) { |
| 7245 | case ROT_N: |
| 7246 | rot = inst[i].i_oparg; |
| 7247 | break; |
| 7248 | case ROT_FOUR: |
| 7249 | rot = 4; |
| 7250 | break; |
| 7251 | case ROT_THREE: |
| 7252 | rot = 3; |
| 7253 | break; |
| 7254 | case ROT_TWO: |
| 7255 | rot = 2; |
| 7256 | break; |
| 7257 | default: |
| 7258 | return; |
| 7259 | } |
| 7260 | if (rot != n) { |
| 7261 | return; |
| 7262 | } |
| 7263 | } |
| 7264 | for (int i = 0; i < n; i++) { |
| 7265 | inst[i].i_opcode = NOP; |
| 7266 | } |
| 7267 | } |
| 7268 | |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7269 | // Attempt to eliminate jumps to jumps by updating inst to jump to |
| 7270 | // target->i_target using the provided opcode. Return whether or not the |
| 7271 | // optimization was successful. |
| 7272 | static bool |
| 7273 | jump_thread(struct instr *inst, struct instr *target, int opcode) |
| 7274 | { |
| 7275 | assert(is_jump(inst)); |
| 7276 | assert(is_jump(target)); |
| 7277 | // bpo-45773: If inst->i_target == target->i_target, then nothing actually |
| 7278 | // changes (and we fall into an infinite loop): |
| 7279 | if (inst->i_lineno == target->i_lineno && |
| 7280 | inst->i_target != target->i_target) |
| 7281 | { |
| 7282 | inst->i_target = target->i_target; |
| 7283 | inst->i_opcode = opcode; |
| 7284 | return true; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7285 | } |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7286 | return false; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7289 | /* Maximum size of basic block that should be copied in optimizer */ |
| 7290 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7291 | |
| 7292 | /* Optimization */ |
| 7293 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7294 | optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7295 | { |
| 7296 | assert(PyList_CheckExact(consts)); |
| 7297 | struct instr nop; |
| 7298 | nop.i_opcode = NOP; |
| 7299 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7300 | for (int i = 0; i < bb->b_iused; i++) { |
| 7301 | struct instr *inst = &bb->b_instr[i]; |
| 7302 | int oparg = inst->i_oparg; |
| 7303 | int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7304 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7305 | /* Skip over empty basic blocks. */ |
| 7306 | while (inst->i_target->b_iused == 0) { |
| 7307 | inst->i_target = inst->i_target->b_next; |
| 7308 | } |
| 7309 | target = &inst->i_target->b_instr[0]; |
| 7310 | } |
| 7311 | else { |
| 7312 | target = &nop; |
| 7313 | } |
| 7314 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7315 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7316 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7317 | { |
| 7318 | PyObject* cnt; |
| 7319 | int is_true; |
| 7320 | int jump_if_true; |
| 7321 | switch(nextop) { |
| 7322 | case POP_JUMP_IF_FALSE: |
| 7323 | case POP_JUMP_IF_TRUE: |
| 7324 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7325 | is_true = PyObject_IsTrue(cnt); |
| 7326 | if (is_true == -1) { |
| 7327 | goto error; |
| 7328 | } |
| 7329 | inst->i_opcode = NOP; |
| 7330 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 7331 | if (is_true == jump_if_true) { |
| 7332 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7333 | bb->b_nofallthrough = 1; |
| 7334 | } |
| 7335 | else { |
| 7336 | bb->b_instr[i+1].i_opcode = NOP; |
| 7337 | } |
| 7338 | break; |
| 7339 | case JUMP_IF_FALSE_OR_POP: |
| 7340 | case JUMP_IF_TRUE_OR_POP: |
| 7341 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7342 | is_true = PyObject_IsTrue(cnt); |
| 7343 | if (is_true == -1) { |
| 7344 | goto error; |
| 7345 | } |
| 7346 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 7347 | if (is_true == jump_if_true) { |
| 7348 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7349 | bb->b_nofallthrough = 1; |
| 7350 | } |
| 7351 | else { |
| 7352 | inst->i_opcode = NOP; |
| 7353 | bb->b_instr[i+1].i_opcode = NOP; |
| 7354 | } |
| 7355 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7356 | } |
| 7357 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7358 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7359 | |
| 7360 | /* Try to fold tuples of constants. |
| 7361 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 7362 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 7363 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 7364 | case BUILD_TUPLE: |
| 7365 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 7366 | switch(oparg) { |
| 7367 | case 1: |
| 7368 | inst->i_opcode = NOP; |
| 7369 | bb->b_instr[i+1].i_opcode = NOP; |
| 7370 | break; |
| 7371 | case 2: |
| 7372 | inst->i_opcode = ROT_TWO; |
| 7373 | bb->b_instr[i+1].i_opcode = NOP; |
| 7374 | break; |
| 7375 | case 3: |
| 7376 | inst->i_opcode = ROT_THREE; |
| 7377 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 7378 | } |
| 7379 | break; |
| 7380 | } |
| 7381 | if (i >= oparg) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7382 | if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7383 | goto error; |
| 7384 | } |
| 7385 | } |
| 7386 | break; |
| 7387 | |
| 7388 | /* Simplify conditional jump to conditional jump where the |
| 7389 | result of the first test implies the success of a similar |
| 7390 | test or the failure of the opposite test. |
| 7391 | Arises in code like: |
| 7392 | "a and b or c" |
| 7393 | "(a and b) and c" |
| 7394 | "(a or b) or c" |
| 7395 | "(a or b) and c" |
| 7396 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 7397 | --> x:JUMP_IF_FALSE_OR_POP z |
| 7398 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 7399 | --> x:POP_JUMP_IF_FALSE y+1 |
| 7400 | where y+1 is the instruction following the second test. |
| 7401 | */ |
| 7402 | case JUMP_IF_FALSE_OR_POP: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7403 | switch (target->i_opcode) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7404 | case POP_JUMP_IF_FALSE: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7405 | i -= jump_thread(inst, target, POP_JUMP_IF_FALSE); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7406 | break; |
| 7407 | case JUMP_ABSOLUTE: |
| 7408 | case JUMP_FORWARD: |
| 7409 | case JUMP_IF_FALSE_OR_POP: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7410 | i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7411 | break; |
| 7412 | case JUMP_IF_TRUE_OR_POP: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7413 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7414 | if (inst->i_lineno == target->i_lineno) { |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7415 | // We don't need to bother checking for loops here, |
| 7416 | // since a block's b_next cannot point to itself: |
| 7417 | assert(inst->i_target != inst->i_target->b_next); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7418 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 7419 | inst->i_target = inst->i_target->b_next; |
| 7420 | --i; |
| 7421 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7422 | break; |
| 7423 | } |
| 7424 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7425 | case JUMP_IF_TRUE_OR_POP: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7426 | switch (target->i_opcode) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7427 | case POP_JUMP_IF_TRUE: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7428 | i -= jump_thread(inst, target, POP_JUMP_IF_TRUE); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7429 | break; |
| 7430 | case JUMP_ABSOLUTE: |
| 7431 | case JUMP_FORWARD: |
| 7432 | case JUMP_IF_TRUE_OR_POP: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7433 | i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7434 | break; |
| 7435 | case JUMP_IF_FALSE_OR_POP: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7436 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7437 | if (inst->i_lineno == target->i_lineno) { |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7438 | // We don't need to bother checking for loops here, |
| 7439 | // since a block's b_next cannot point to itself: |
| 7440 | assert(inst->i_target != inst->i_target->b_next); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7441 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 7442 | inst->i_target = inst->i_target->b_next; |
| 7443 | --i; |
| 7444 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7445 | break; |
| 7446 | } |
| 7447 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7448 | case POP_JUMP_IF_FALSE: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7449 | switch (target->i_opcode) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7450 | case JUMP_ABSOLUTE: |
| 7451 | case JUMP_FORWARD: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7452 | case JUMP_IF_FALSE_OR_POP: |
| 7453 | i -= jump_thread(inst, target, POP_JUMP_IF_FALSE); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7454 | } |
| 7455 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7456 | case POP_JUMP_IF_TRUE: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7457 | switch (target->i_opcode) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7458 | case JUMP_ABSOLUTE: |
| 7459 | case JUMP_FORWARD: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7460 | case JUMP_IF_TRUE_OR_POP: |
| 7461 | i -= jump_thread(inst, target, POP_JUMP_IF_TRUE); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7462 | } |
| 7463 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7464 | case JUMP_ABSOLUTE: |
| 7465 | case JUMP_FORWARD: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7466 | switch (target->i_opcode) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7467 | case JUMP_ABSOLUTE: |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7468 | case JUMP_FORWARD: |
| 7469 | i -= jump_thread(inst, target, JUMP_ABSOLUTE); |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7470 | } |
| 7471 | break; |
| 7472 | case FOR_ITER: |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7473 | if (target->i_opcode == JUMP_FORWARD) { |
Brandt Bucher | a89bbde | 2021-11-11 13:52:43 -0800 | [diff] [blame^] | 7474 | i -= jump_thread(inst, target, FOR_ITER); |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7475 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 7476 | break; |
| 7477 | case ROT_N: |
| 7478 | switch (oparg) { |
| 7479 | case 0: |
| 7480 | case 1: |
| 7481 | inst->i_opcode = NOP; |
| 7482 | continue; |
| 7483 | case 2: |
| 7484 | inst->i_opcode = ROT_TWO; |
| 7485 | break; |
| 7486 | case 3: |
| 7487 | inst->i_opcode = ROT_THREE; |
| 7488 | break; |
| 7489 | case 4: |
| 7490 | inst->i_opcode = ROT_FOUR; |
| 7491 | break; |
| 7492 | } |
| 7493 | if (i >= oparg - 1) { |
| 7494 | fold_rotations(inst - oparg + 1, oparg); |
| 7495 | } |
| 7496 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7497 | } |
| 7498 | } |
| 7499 | return 0; |
| 7500 | error: |
| 7501 | return -1; |
| 7502 | } |
| 7503 | |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7504 | /* If this block ends with an unconditional jump to an exit block, |
| 7505 | * then remove the jump and extend this block with the target. |
| 7506 | */ |
| 7507 | static int |
| 7508 | extend_block(basicblock *bb) { |
| 7509 | if (bb->b_iused == 0) { |
| 7510 | return 0; |
| 7511 | } |
| 7512 | struct instr *last = &bb->b_instr[bb->b_iused-1]; |
| 7513 | if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) { |
| 7514 | return 0; |
| 7515 | } |
| 7516 | if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7517 | basicblock *to_copy = last->i_target; |
| 7518 | last->i_opcode = NOP; |
| 7519 | for (int i = 0; i < to_copy->b_iused; i++) { |
| 7520 | int index = compiler_next_instr(bb); |
| 7521 | if (index < 0) { |
| 7522 | return -1; |
| 7523 | } |
| 7524 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7525 | } |
| 7526 | bb->b_exit = 1; |
| 7527 | } |
| 7528 | return 0; |
| 7529 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7530 | |
| 7531 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7532 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7533 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7534 | int dest = 0; |
| 7535 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7536 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7537 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7538 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7539 | if (lineno < 0) { |
| 7540 | continue; |
| 7541 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7542 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7543 | if (prev_lineno == lineno) { |
| 7544 | continue; |
| 7545 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7546 | /* 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] | 7547 | if (src < bb->b_iused - 1) { |
| 7548 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7549 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7550 | bb->b_instr[src+1].i_lineno = lineno; |
| 7551 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7552 | } |
| 7553 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7554 | else { |
| 7555 | basicblock* next = bb->b_next; |
| 7556 | while (next && next->b_iused == 0) { |
| 7557 | next = next->b_next; |
| 7558 | } |
| 7559 | /* or if last instruction in BB and next BB has same line number */ |
| 7560 | if (next) { |
| 7561 | if (lineno == next->b_instr[0].i_lineno) { |
| 7562 | continue; |
| 7563 | } |
| 7564 | } |
| 7565 | } |
| 7566 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7567 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7568 | if (dest != src) { |
| 7569 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7570 | } |
| 7571 | dest++; |
| 7572 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7573 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7574 | assert(dest <= bb->b_iused); |
| 7575 | bb->b_iused = dest; |
| 7576 | } |
| 7577 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7578 | static int |
| 7579 | normalize_basic_block(basicblock *bb) { |
| 7580 | /* Mark blocks as exit and/or nofallthrough. |
| 7581 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7582 | for (int i = 0; i < bb->b_iused; i++) { |
| 7583 | switch(bb->b_instr[i].i_opcode) { |
| 7584 | case RETURN_VALUE: |
| 7585 | case RAISE_VARARGS: |
| 7586 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7587 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7588 | bb->b_nofallthrough = 1; |
| 7589 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7590 | case JUMP_ABSOLUTE: |
| 7591 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7592 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7593 | /* fall through */ |
| 7594 | case POP_JUMP_IF_FALSE: |
| 7595 | case POP_JUMP_IF_TRUE: |
| 7596 | case JUMP_IF_FALSE_OR_POP: |
| 7597 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7598 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7599 | if (i != bb->b_iused-1) { |
| 7600 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7601 | return -1; |
| 7602 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7603 | /* Skip over empty basic blocks. */ |
| 7604 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7605 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7606 | } |
| 7607 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7608 | } |
| 7609 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7610 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7611 | } |
| 7612 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7613 | static int |
| 7614 | mark_reachable(struct assembler *a) { |
| 7615 | basicblock **stack, **sp; |
| 7616 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7617 | if (stack == NULL) { |
| 7618 | return -1; |
| 7619 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7620 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7621 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7622 | while (sp > stack) { |
| 7623 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7624 | if (b->b_next && !b->b_nofallthrough) { |
| 7625 | if (b->b_next->b_predecessors == 0) { |
| 7626 | *sp++ = b->b_next; |
| 7627 | } |
| 7628 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7629 | } |
| 7630 | for (int i = 0; i < b->b_iused; i++) { |
| 7631 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7632 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7633 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7634 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7635 | *sp++ = target; |
| 7636 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7637 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7638 | } |
| 7639 | } |
| 7640 | } |
| 7641 | PyObject_Free(stack); |
| 7642 | return 0; |
| 7643 | } |
| 7644 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7645 | static void |
| 7646 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7647 | /* Eliminate empty blocks */ |
| 7648 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7649 | basicblock *next = b->b_next; |
| 7650 | if (next) { |
| 7651 | while (next->b_iused == 0 && next->b_next) { |
| 7652 | next = next->b_next; |
| 7653 | } |
| 7654 | b->b_next = next; |
| 7655 | } |
| 7656 | } |
| 7657 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7658 | if (b->b_iused == 0) { |
| 7659 | continue; |
| 7660 | } |
| 7661 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7662 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7663 | while (target->b_iused == 0) { |
| 7664 | target = target->b_next; |
| 7665 | } |
| 7666 | b->b_instr[b->b_iused-1].i_target = target; |
| 7667 | } |
| 7668 | } |
| 7669 | } |
| 7670 | |
| 7671 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7672 | /* 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] | 7673 | * then copy the line number. If a successor block has no line number, and only |
| 7674 | * one predecessor, then inherit the line number. |
| 7675 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7676 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7677 | * but has no impact on the generated line number events. |
| 7678 | */ |
| 7679 | static void |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7680 | propagate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7681 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7682 | if (b->b_iused == 0) { |
| 7683 | continue; |
| 7684 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7685 | int prev_lineno = -1; |
| 7686 | for (int i = 0; i < b->b_iused; i++) { |
| 7687 | if (b->b_instr[i].i_lineno < 0) { |
| 7688 | b->b_instr[i].i_lineno = prev_lineno; |
| 7689 | } |
| 7690 | else { |
| 7691 | prev_lineno = b->b_instr[i].i_lineno; |
| 7692 | } |
| 7693 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7694 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7695 | assert(b->b_next->b_iused); |
| 7696 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7697 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7698 | } |
| 7699 | } |
| 7700 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7701 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7702 | /* Note: Only actual jumps, not exception handlers */ |
| 7703 | case SETUP_ASYNC_WITH: |
| 7704 | case SETUP_WITH: |
| 7705 | case SETUP_FINALLY: |
| 7706 | continue; |
| 7707 | } |
| 7708 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7709 | if (target->b_predecessors == 1) { |
| 7710 | if (target->b_instr[0].i_lineno < 0) { |
| 7711 | target->b_instr[0].i_lineno = prev_lineno; |
| 7712 | } |
| 7713 | } |
| 7714 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7715 | } |
| 7716 | } |
| 7717 | |
| 7718 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7719 | The consts object should still be in list form to allow new constants |
| 7720 | to be appended. |
| 7721 | |
| 7722 | All transformations keep the code size the same or smaller. |
| 7723 | For those that reduce size, the gaps are initially filled with |
| 7724 | NOPs. Later those NOPs are removed. |
| 7725 | */ |
| 7726 | |
| 7727 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7728 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7729 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7730 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7731 | if (optimize_basic_block(c, b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7732 | return -1; |
| 7733 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7734 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7735 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7736 | } |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7737 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7738 | if (extend_block(b)) { |
| 7739 | return -1; |
| 7740 | } |
| 7741 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7742 | if (mark_reachable(a)) { |
| 7743 | return -1; |
| 7744 | } |
| 7745 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7746 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7747 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7748 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7749 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7750 | } |
| 7751 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7752 | basicblock *pred = NULL; |
| 7753 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7754 | int prev_lineno = -1; |
| 7755 | if (pred && pred->b_iused) { |
| 7756 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7757 | } |
| 7758 | clean_basic_block(b, prev_lineno); |
| 7759 | pred = b->b_nofallthrough ? NULL : b; |
| 7760 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7761 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7762 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7763 | block ends with a jump instruction, check if the next non-empty block |
| 7764 | reached through normal flow control is the target of that jump. If it |
| 7765 | is, then the jump instruction is redundant and can be deleted. |
| 7766 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7767 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7768 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7769 | if (b->b_iused > 0) { |
| 7770 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7771 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7772 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7773 | if (b_last_instr->i_target == b->b_next) { |
| 7774 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7775 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7776 | b_last_instr->i_opcode = NOP; |
| 7777 | clean_basic_block(b, -1); |
| 7778 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7779 | } |
| 7780 | } |
| 7781 | } |
| 7782 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7783 | if (maybe_empty_blocks) { |
| 7784 | eliminate_empty_basic_blocks(a->a_entry); |
| 7785 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7786 | return 0; |
| 7787 | } |
| 7788 | |
Ćukasz Langa | d41abe8 | 2021-09-08 18:25:09 +0200 | [diff] [blame] | 7789 | // Remove trailing unused constants. |
| 7790 | static int |
| 7791 | trim_unused_consts(struct compiler *c, struct assembler *a, PyObject *consts) |
| 7792 | { |
| 7793 | assert(PyList_CheckExact(consts)); |
| 7794 | |
| 7795 | // The first constant may be docstring; keep it always. |
| 7796 | int max_const_index = 0; |
| 7797 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7798 | for (int i = 0; i < b->b_iused; i++) { |
| 7799 | if (b->b_instr[i].i_opcode == LOAD_CONST && |
| 7800 | b->b_instr[i].i_oparg > max_const_index) { |
| 7801 | max_const_index = b->b_instr[i].i_oparg; |
| 7802 | } |
| 7803 | } |
| 7804 | } |
| 7805 | if (max_const_index+1 < PyList_GET_SIZE(consts)) { |
| 7806 | //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n", |
| 7807 | // max_const_index, (int)PyList_GET_SIZE(consts)); |
| 7808 | if (PyList_SetSlice(consts, max_const_index+1, |
| 7809 | PyList_GET_SIZE(consts), NULL) < 0) { |
| 7810 | return 1; |
| 7811 | } |
| 7812 | } |
| 7813 | return 0; |
| 7814 | } |
| 7815 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7816 | static inline int |
| 7817 | is_exit_without_lineno(basicblock *b) { |
| 7818 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7819 | } |
| 7820 | |
| 7821 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7822 | * after a frame terminates. It would be prohibitively expensive |
| 7823 | * to continuously update the f_lineno field at runtime, |
| 7824 | * so we make sure that all exiting instruction (raises and returns) |
| 7825 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7826 | * We can do this by duplicating the exit blocks without line number |
| 7827 | * so that none have more than one predecessor. We can then safely |
| 7828 | * copy the line number from the sole predecessor block. |
| 7829 | */ |
| 7830 | static int |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7831 | duplicate_exits_without_lineno(struct compiler *c) |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7832 | { |
| 7833 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7834 | */ |
| 7835 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7836 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7837 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7838 | /* Note: Only actual jumps, not exception handlers */ |
| 7839 | case SETUP_ASYNC_WITH: |
| 7840 | case SETUP_WITH: |
| 7841 | case SETUP_FINALLY: |
| 7842 | continue; |
| 7843 | } |
| 7844 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7845 | if (is_exit_without_lineno(target) && target->b_predecessors > 1) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7846 | basicblock *new_target = compiler_copy_block(c, target); |
| 7847 | if (new_target == NULL) { |
| 7848 | return -1; |
| 7849 | } |
| 7850 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7851 | b->b_instr[b->b_iused-1].i_target = new_target; |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7852 | target->b_predecessors--; |
| 7853 | new_target->b_predecessors = 1; |
| 7854 | new_target->b_next = target->b_next; |
| 7855 | target->b_next = new_target; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7856 | } |
| 7857 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7858 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7859 | /* Eliminate empty blocks */ |
| 7860 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7861 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7862 | b->b_next = b->b_next->b_next; |
| 7863 | } |
| 7864 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7865 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7866 | * fall through, and thus can only have a single predecessor */ |
| 7867 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7868 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7869 | if (is_exit_without_lineno(b->b_next)) { |
| 7870 | assert(b->b_next->b_iused > 0); |
| 7871 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7872 | } |
| 7873 | } |
| 7874 | } |
| 7875 | return 0; |
| 7876 | } |
| 7877 | |
| 7878 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7879 | /* Retained for API compatibility. |
| 7880 | * Optimization is now done in optimize_cfg */ |
| 7881 | |
| 7882 | PyObject * |
| 7883 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7884 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7885 | { |
| 7886 | Py_INCREF(code); |
| 7887 | return code; |
| 7888 | } |