Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
| 3 | * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 4 | * The primary entry point is _PyAST_Compile(), which returns a |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5 | * PyCodeObject. The compiler makes several passes to build the code |
| 6 | * object: |
| 7 | * 1. Checks for future statements. See future.c |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | * 2. Builds a symbol table. See symtable.c. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | * 3. Generate code for basic blocks. See compiler_mod() in this file. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | * 4. Assemble the basic blocks into final code. See assemble() in |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | * this file. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 12 | * 5. Optimize the byte code (peephole optimizations). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | * |
| 14 | * Note that compiler_mod() suggests module, but the module ast type |
| 15 | * (mod_ty) has cases for expressions and interactive statements. |
Nick Coghlan | 944d3eb | 2005-11-16 12:46:55 +0000 | [diff] [blame] | 16 | * |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 17 | * CAUTION: The VISIT_* macros abort the current function when they |
| 18 | * encounter a problem. So don't invoke them when there is memory |
| 19 | * which needs to be released. Code blocks are OK, as the compiler |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | * structure takes care of releasing those. Use the arena to manage |
| 21 | * objects. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 24 | #include "Python.h" |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 25 | #include "pycore_ast.h" // _PyAST_GetDocString() |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 26 | #include "pycore_compile.h" // _PyFuture_FromAST() |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 27 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 28 | #include "pycore_long.h" // _PyLong_GetZero() |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 29 | #include "pycore_symtable.h" // PySTEntryObject |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 31 | #define NEED_OPCODE_JUMP_TABLES |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 32 | #include "opcode.h" // EXTENDED_ARG |
| 33 | #include "wordcode_helpers.h" // instrsize() |
| 34 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 35 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 36 | #define DEFAULT_BLOCK_SIZE 16 |
| 37 | #define DEFAULT_BLOCKS 8 |
| 38 | #define DEFAULT_CODE_SIZE 128 |
| 39 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 40 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 41 | #define COMP_GENEXP 0 |
| 42 | #define COMP_LISTCOMP 1 |
| 43 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 44 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 45 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 46 | /* A soft limit for stack use, to avoid excessive |
| 47 | * memory use for large constants, etc. |
| 48 | * |
| 49 | * The value 30 is plucked out of thin air. |
| 50 | * Code that could use more stack than this is |
| 51 | * rare, so the exact value is unimportant. |
| 52 | */ |
| 53 | #define STACK_USE_GUIDELINE 30 |
| 54 | |
| 55 | /* If we exceed this limit, it should |
| 56 | * be considered a compiler bug. |
| 57 | * Currently it should be impossible |
| 58 | * to exceed STACK_USE_GUIDELINE * 100, |
| 59 | * as 100 is the maximum parse depth. |
| 60 | * For performance reasons we will |
| 61 | * want to reduce this to a |
| 62 | * few hundred in the future. |
| 63 | * |
| 64 | * NOTE: Whatever MAX_ALLOWED_STACK_USE is |
| 65 | * set to, it should never restrict what Python |
| 66 | * we can write, just how we compile it. |
| 67 | */ |
| 68 | #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100) |
| 69 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 70 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 71 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 72 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 73 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 74 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | unsigned char i_opcode; |
| 76 | int i_oparg; |
| 77 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 78 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 81 | #define LOG_BITS_PER_INT 5 |
| 82 | #define MASK_LOW_LOG_BITS 31 |
| 83 | |
| 84 | static inline int |
| 85 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 86 | /* Is the relevant bit set in the relevant word? */ |
| 87 | /* 256 bits fit into 8 32-bits words. |
| 88 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 89 | * Bit within word is the low bits of bitindex. |
| 90 | */ |
| 91 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 92 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 93 | } |
| 94 | |
| 95 | static inline int |
| 96 | is_relative_jump(struct instr *i) |
| 97 | { |
| 98 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 99 | } |
| 100 | |
| 101 | static inline int |
| 102 | is_jump(struct instr *i) |
| 103 | { |
| 104 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 105 | } |
| 106 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 107 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 109 | reverse order that the block are allocated. b_list points to the next |
| 110 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | struct basicblock_ *b_list; |
| 112 | /* number of instructions used */ |
| 113 | int b_iused; |
| 114 | /* length of instruction array (b_instr) */ |
| 115 | int b_ialloc; |
| 116 | /* pointer to an array of instructions, initially NULL */ |
| 117 | struct instr *b_instr; |
| 118 | /* If b_next is non-NULL, it is a pointer to the next |
| 119 | block reached by normal control flow. */ |
| 120 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 122 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 123 | /* Number of predecssors that a block has. */ |
| 124 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 125 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 126 | unsigned b_nofallthrough : 1; |
| 127 | /* Basic block exits scope (it ends with a return or raise) */ |
| 128 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 130 | int b_startdepth; |
| 131 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 132 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 133 | } basicblock; |
| 134 | |
| 135 | /* fblockinfo tracks the current frame block. |
| 136 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 137 | A frame block is used to handle loops, try/except, and try/finally. |
| 138 | It's called a frame block to distinguish it from a basic block in the |
| 139 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 140 | */ |
| 141 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 142 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 143 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER, |
| 144 | ASYNC_COMPREHENSION_GENERATOR }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 145 | |
| 146 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | enum fblocktype fb_type; |
| 148 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 149 | /* (optional) type-specific exit or cleanup block */ |
| 150 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 151 | /* (optional) additional information required for unwinding */ |
| 152 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 155 | enum { |
| 156 | COMPILER_SCOPE_MODULE, |
| 157 | COMPILER_SCOPE_CLASS, |
| 158 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 159 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 160 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 161 | COMPILER_SCOPE_COMPREHENSION, |
| 162 | }; |
| 163 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | /* The following items change on entry and exit of code blocks. |
| 165 | They must be saved and restored when returning to a block. |
| 166 | */ |
| 167 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 171 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 172 | int u_scope_type; |
| 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | /* The following fields are dicts that map objects to |
| 175 | the index of them in co_XXX. The index is used as |
| 176 | the argument for opcodes that refer to those collections. |
| 177 | */ |
| 178 | PyObject *u_consts; /* all constants */ |
| 179 | PyObject *u_names; /* all names */ |
| 180 | PyObject *u_varnames; /* local variables */ |
| 181 | PyObject *u_cellvars; /* cell variables */ |
| 182 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 186 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 187 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 188 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* Pointer to the most recently allocated block. By following b_list |
| 190 | members, you can reach all early allocated blocks. */ |
| 191 | basicblock *u_blocks; |
| 192 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | int u_nfblocks; |
| 195 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | int u_firstlineno; /* the first lineno of the block */ |
| 198 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 199 | int u_col_offset; /* the offset of the current stmt */ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 200 | int u_end_lineno; /* the end line of the current stmt */ |
| 201 | int u_end_col_offset; /* the end offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 205 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 206 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | for enclosing blocks are stored in c_stack. The u and c_stack are |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 208 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 209 | |
| 210 | Note that we don't track recursion levels during compilation - the |
| 211 | task of detecting and rejecting excessive levels of nesting is |
| 212 | handled by the symbol analysis pass. |
| 213 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | */ |
| 215 | |
| 216 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 217 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | struct symtable *c_st; |
| 219 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 220 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 222 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | int c_interactive; /* true if in interactive mode */ |
| 224 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 225 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 226 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | struct compiler_unit *u; /* compiler state for current block */ |
| 228 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 229 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 232 | typedef struct { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 233 | // A list of strings corresponding to name captures. It is used to track: |
| 234 | // - Repeated name assignments in the same pattern. |
| 235 | // - Different name assignments in alternatives. |
| 236 | // - The order of name assignments in alternatives. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 237 | PyObject *stores; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 238 | // If 0, any name captures against our subject will raise. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 239 | int allow_irrefutable; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 240 | // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop |
| 241 | // i items off of the stack. The end result looks like this (with each block |
| 242 | // falling through to the next): |
| 243 | // fail_pop[4]: POP_TOP |
| 244 | // fail_pop[3]: POP_TOP |
| 245 | // fail_pop[2]: POP_TOP |
| 246 | // fail_pop[1]: POP_TOP |
| 247 | // fail_pop[0]: NOP |
| 248 | basicblock **fail_pop; |
| 249 | // The current length of fail_pop. |
| 250 | Py_ssize_t fail_pop_size; |
| 251 | // The number of items on top of the stack that need to *stay* on top of the |
| 252 | // stack. Variable captures go beneath these. All of them will be popped on |
| 253 | // failure. |
| 254 | Py_ssize_t on_top; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 255 | } pattern_context; |
| 256 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 257 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 258 | static void compiler_free(struct compiler *); |
| 259 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 260 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 261 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 262 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 263 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 264 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 265 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 266 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 267 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 268 | |
| 269 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 270 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 271 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 272 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 273 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 274 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 275 | static int compiler_subscript(struct compiler *, expr_ty); |
| 276 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 277 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 278 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 279 | 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] | 280 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 281 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 282 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 283 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 284 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 285 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 286 | asdl_expr_seq *args, |
| 287 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 288 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 289 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 290 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 291 | static int compiler_sync_comprehension_generator( |
| 292 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 293 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 294 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 295 | expr_ty elt, expr_ty val, int type); |
| 296 | |
| 297 | static int compiler_async_comprehension_generator( |
| 298 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 299 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 300 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 301 | expr_ty elt, expr_ty val, int type); |
| 302 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 303 | static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 304 | static int compiler_match(struct compiler *, stmt_ty); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 305 | static int compiler_pattern_subpattern(struct compiler *, pattern_ty, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 306 | pattern_context *); |
| 307 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 308 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 309 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 310 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 311 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 312 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 313 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 314 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | /* Name mangling: __private becomes _classname__private. |
| 317 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 318 | PyObject *result; |
| 319 | size_t nlen, plen, ipriv; |
| 320 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 322 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 323 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | Py_INCREF(ident); |
| 325 | return ident; |
| 326 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 327 | nlen = PyUnicode_GET_LENGTH(ident); |
| 328 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | The only time a name with a dot can occur is when |
| 332 | we are compiling an import statement that has a |
| 333 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | TODO(jhylton): Decide whether we want to support |
| 336 | mangling of the module name, e.g. __M.X. |
| 337 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 338 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 339 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 340 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | Py_INCREF(ident); |
| 342 | return ident; /* Don't mangle __whatever__ */ |
| 343 | } |
| 344 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 345 | ipriv = 0; |
| 346 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 347 | ipriv++; |
| 348 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | Py_INCREF(ident); |
| 350 | return ident; /* Don't mangle if class is just underscores */ |
| 351 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 352 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 353 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 354 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 355 | PyErr_SetString(PyExc_OverflowError, |
| 356 | "private identifier too large to be mangled"); |
| 357 | return NULL; |
| 358 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 359 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 360 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 361 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 362 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 363 | |
| 364 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 365 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 367 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 368 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 369 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 370 | Py_DECREF(result); |
| 371 | return NULL; |
| 372 | } |
| 373 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 374 | Py_DECREF(result); |
| 375 | return NULL; |
| 376 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 377 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 378 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 381 | static int |
| 382 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 385 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 386 | c->c_const_cache = PyDict_New(); |
| 387 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | c->c_stack = PyList_New(0); |
| 392 | if (!c->c_stack) { |
| 393 | Py_CLEAR(c->c_const_cache); |
| 394 | return 0; |
| 395 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | PyCodeObject * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 401 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 402 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | struct compiler c; |
| 405 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 406 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | if (!__doc__) { |
| 410 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 411 | if (!__doc__) |
| 412 | return NULL; |
| 413 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 414 | if (!__annotations__) { |
| 415 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 416 | if (!__annotations__) |
| 417 | return NULL; |
| 418 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (!compiler_init(&c)) |
| 420 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 421 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | c.c_filename = filename; |
| 423 | c.c_arena = arena; |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 424 | c.c_future = _PyFuture_FromAST(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | if (c.c_future == NULL) |
| 426 | goto finally; |
| 427 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | flags = &local_flags; |
| 429 | } |
| 430 | merged = c.c_future->ff_features | flags->cf_flags; |
| 431 | c.c_future->ff_features = merged; |
| 432 | flags->cf_flags = merged; |
| 433 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 434 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 436 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 437 | _PyASTOptimizeState state; |
| 438 | state.optimize = c.c_optimize; |
| 439 | state.ff_features = merged; |
| 440 | |
| 441 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 442 | goto finally; |
| 443 | } |
| 444 | |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 445 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | if (c.c_st == NULL) { |
| 447 | if (!PyErr_Occurred()) |
| 448 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 449 | goto finally; |
| 450 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 453 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 454 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | compiler_free(&c); |
| 456 | assert(co || PyErr_Occurred()); |
| 457 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 460 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 461 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | if (c->c_st) |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 464 | _PySymtable_Free(c->c_st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | if (c->c_future) |
| 466 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 467 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 468 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 472 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 473 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | Py_ssize_t i, n; |
| 476 | PyObject *v, *k; |
| 477 | PyObject *dict = PyDict_New(); |
| 478 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | n = PyList_Size(list); |
| 481 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 482 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | if (!v) { |
| 484 | Py_DECREF(dict); |
| 485 | return NULL; |
| 486 | } |
| 487 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 488 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | Py_DECREF(v); |
| 490 | Py_DECREF(dict); |
| 491 | return NULL; |
| 492 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | Py_DECREF(v); |
| 494 | } |
| 495 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /* Return new dict containing names from src that match scope(s). |
| 499 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 500 | 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] | 501 | 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] | 502 | values are integers, starting at offset and increasing by one for |
| 503 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 504 | */ |
| 505 | |
| 506 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 507 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 508 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 509 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 511 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | assert(offset >= 0); |
| 514 | if (dest == NULL) |
| 515 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 516 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 517 | /* Sort the keys so that we have a deterministic order on the indexes |
| 518 | saved in the returned dictionary. These indexes are used as indexes |
| 519 | into the free and cell var storage. Therefore if they aren't |
| 520 | deterministic, then the generated bytecode is not deterministic. |
| 521 | */ |
| 522 | sorted_keys = PyDict_Keys(src); |
| 523 | if (sorted_keys == NULL) |
| 524 | return NULL; |
| 525 | if (PyList_Sort(sorted_keys) != 0) { |
| 526 | Py_DECREF(sorted_keys); |
| 527 | return NULL; |
| 528 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 529 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 530 | |
| 531 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | /* XXX this should probably be a macro in symtable.h */ |
| 533 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 534 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 535 | v = PyDict_GetItemWithError(src, k); |
| 536 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | vi = PyLong_AS_LONG(v); |
| 538 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 541 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 543 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | Py_DECREF(dest); |
| 545 | return NULL; |
| 546 | } |
| 547 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 548 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 549 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | Py_DECREF(item); |
| 551 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | return NULL; |
| 553 | } |
| 554 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 557 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 561 | static void |
| 562 | compiler_unit_check(struct compiler_unit *u) |
| 563 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | basicblock *block; |
| 565 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 566 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | if (block->b_instr != NULL) { |
| 568 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 569 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | assert(block->b_ialloc >= block->b_iused); |
| 571 | } |
| 572 | else { |
| 573 | assert (block->b_iused == 0); |
| 574 | assert (block->b_ialloc == 0); |
| 575 | } |
| 576 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static void |
| 580 | compiler_unit_free(struct compiler_unit *u) |
| 581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | compiler_unit_check(u); |
| 585 | b = u->u_blocks; |
| 586 | while (b != NULL) { |
| 587 | if (b->b_instr) |
| 588 | PyObject_Free((void *)b->b_instr); |
| 589 | next = b->b_list; |
| 590 | PyObject_Free((void *)b); |
| 591 | b = next; |
| 592 | } |
| 593 | Py_CLEAR(u->u_ste); |
| 594 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 595 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | Py_CLEAR(u->u_consts); |
| 597 | Py_CLEAR(u->u_names); |
| 598 | Py_CLEAR(u->u_varnames); |
| 599 | Py_CLEAR(u->u_freevars); |
| 600 | Py_CLEAR(u->u_cellvars); |
| 601 | Py_CLEAR(u->u_private); |
| 602 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 606 | compiler_enter_scope(struct compiler *c, identifier name, |
| 607 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 610 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 611 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 612 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | struct compiler_unit)); |
| 614 | if (!u) { |
| 615 | PyErr_NoMemory(); |
| 616 | return 0; |
| 617 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 618 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 620 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | u->u_kwonlyargcount = 0; |
| 622 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 623 | if (!u->u_ste) { |
| 624 | compiler_unit_free(u); |
| 625 | return 0; |
| 626 | } |
| 627 | Py_INCREF(name); |
| 628 | u->u_name = name; |
| 629 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 630 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 631 | if (!u->u_varnames || !u->u_cellvars) { |
| 632 | compiler_unit_free(u); |
| 633 | return 0; |
| 634 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 635 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 636 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 637 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 638 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 639 | int res; |
| 640 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 641 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 642 | name = _PyUnicode_FromId(&PyId___class__); |
| 643 | if (!name) { |
| 644 | compiler_unit_free(u); |
| 645 | return 0; |
| 646 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 647 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 648 | if (res < 0) { |
| 649 | compiler_unit_free(u); |
| 650 | return 0; |
| 651 | } |
| 652 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | 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] | 655 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | if (!u->u_freevars) { |
| 657 | compiler_unit_free(u); |
| 658 | return 0; |
| 659 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | u->u_blocks = NULL; |
| 662 | u->u_nfblocks = 0; |
| 663 | u->u_firstlineno = lineno; |
| 664 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 665 | u->u_col_offset = 0; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 666 | u->u_end_lineno = 0; |
| 667 | u->u_end_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | u->u_consts = PyDict_New(); |
| 669 | if (!u->u_consts) { |
| 670 | compiler_unit_free(u); |
| 671 | return 0; |
| 672 | } |
| 673 | u->u_names = PyDict_New(); |
| 674 | if (!u->u_names) { |
| 675 | compiler_unit_free(u); |
| 676 | return 0; |
| 677 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | u->u_private = NULL; |
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 | /* Push the old compiler_unit on the stack. */ |
| 682 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 683 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 685 | Py_XDECREF(capsule); |
| 686 | compiler_unit_free(u); |
| 687 | return 0; |
| 688 | } |
| 689 | Py_DECREF(capsule); |
| 690 | u->u_private = c->u->u_private; |
| 691 | Py_XINCREF(u->u_private); |
| 692 | } |
| 693 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 696 | |
| 697 | block = compiler_new_block(c); |
| 698 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 700 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 701 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 702 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 703 | if (!compiler_set_qualname(c)) |
| 704 | return 0; |
| 705 | } |
| 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 710 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 711 | compiler_exit_scope(struct compiler *c) |
| 712 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 713 | // Don't call PySequence_DelItem() with an exception raised |
| 714 | PyObject *exc_type, *exc_val, *exc_tb; |
| 715 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | c->c_nestlevel--; |
| 718 | compiler_unit_free(c->u); |
| 719 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 720 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 722 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 723 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | assert(c->u); |
| 725 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 726 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 727 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 728 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 729 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | compiler_unit_check(c->u); |
| 731 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 732 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 734 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 735 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 736 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 739 | static int |
| 740 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 741 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 742 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 743 | _Py_static_string(dot_locals, ".<locals>"); |
| 744 | Py_ssize_t stack_size; |
| 745 | struct compiler_unit *u = c->u; |
| 746 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 747 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 748 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 749 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 750 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 751 | if (stack_size > 1) { |
| 752 | int scope, force_global = 0; |
| 753 | struct compiler_unit *parent; |
| 754 | PyObject *mangled, *capsule; |
| 755 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 756 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 757 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 758 | assert(parent); |
| 759 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 760 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 761 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 762 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 763 | assert(u->u_name); |
| 764 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 765 | if (!mangled) |
| 766 | return 0; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 767 | scope = _PyST_GetScope(parent->u_ste, mangled); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 768 | Py_DECREF(mangled); |
| 769 | assert(scope != GLOBAL_IMPLICIT); |
| 770 | if (scope == GLOBAL_EXPLICIT) |
| 771 | force_global = 1; |
| 772 | } |
| 773 | |
| 774 | if (!force_global) { |
| 775 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 776 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 777 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 778 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 779 | if (dot_locals_str == NULL) |
| 780 | return 0; |
| 781 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 782 | if (base == NULL) |
| 783 | return 0; |
| 784 | } |
| 785 | else { |
| 786 | Py_INCREF(parent->u_qualname); |
| 787 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 788 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 789 | } |
| 790 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 791 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 792 | if (base != NULL) { |
| 793 | dot_str = _PyUnicode_FromId(&dot); |
| 794 | if (dot_str == NULL) { |
| 795 | Py_DECREF(base); |
| 796 | return 0; |
| 797 | } |
| 798 | name = PyUnicode_Concat(base, dot_str); |
| 799 | Py_DECREF(base); |
| 800 | if (name == NULL) |
| 801 | return 0; |
| 802 | PyUnicode_Append(&name, u->u_name); |
| 803 | if (name == NULL) |
| 804 | return 0; |
| 805 | } |
| 806 | else { |
| 807 | Py_INCREF(u->u_name); |
| 808 | name = u->u_name; |
| 809 | } |
| 810 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 811 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 812 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 815 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 816 | /* Allocate a new block and return a pointer to it. |
| 817 | Returns NULL on error. |
| 818 | */ |
| 819 | |
| 820 | static basicblock * |
| 821 | compiler_new_block(struct compiler *c) |
| 822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | basicblock *b; |
| 824 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 827 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | if (b == NULL) { |
| 829 | PyErr_NoMemory(); |
| 830 | return NULL; |
| 831 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | /* Extend the singly linked list of blocks with new block. */ |
| 833 | b->b_list = u->u_blocks; |
| 834 | u->u_blocks = b; |
| 835 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 838 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 | compiler_next_block(struct compiler *c) |
| 840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | basicblock *block = compiler_new_block(c); |
| 842 | if (block == NULL) |
| 843 | return NULL; |
| 844 | c->u->u_curblock->b_next = block; |
| 845 | c->u->u_curblock = block; |
| 846 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static basicblock * |
| 850 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | assert(block != NULL); |
| 853 | c->u->u_curblock->b_next = block; |
| 854 | c->u->u_curblock = block; |
| 855 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 858 | static basicblock * |
| 859 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 860 | { |
| 861 | /* Cannot copy a block if it has a fallthrough, since |
| 862 | * a block can only have one fallthrough predecessor. |
| 863 | */ |
| 864 | assert(block->b_nofallthrough); |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 865 | basicblock *result = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 866 | if (result == NULL) { |
| 867 | return NULL; |
| 868 | } |
| 869 | for (int i = 0; i < block->b_iused; i++) { |
| 870 | int n = compiler_next_instr(result); |
| 871 | if (n < 0) { |
| 872 | return NULL; |
| 873 | } |
| 874 | result->b_instr[n] = block->b_instr[i]; |
| 875 | } |
| 876 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 877 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 878 | return result; |
| 879 | } |
| 880 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 881 | /* Returns the offset of the next instruction in the current block's |
| 882 | b_instr array. Resizes the b_instr as necessary. |
| 883 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 884 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 885 | |
| 886 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 887 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | assert(b != NULL); |
| 890 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 891 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 892 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | if (b->b_instr == NULL) { |
| 894 | PyErr_NoMemory(); |
| 895 | return -1; |
| 896 | } |
| 897 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | } |
| 899 | else if (b->b_iused == b->b_ialloc) { |
| 900 | struct instr *tmp; |
| 901 | size_t oldsize, newsize; |
| 902 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 903 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 904 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 905 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | PyErr_NoMemory(); |
| 907 | return -1; |
| 908 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | if (newsize == 0) { |
| 911 | PyErr_NoMemory(); |
| 912 | return -1; |
| 913 | } |
| 914 | b->b_ialloc <<= 1; |
| 915 | tmp = (struct instr *)PyObject_Realloc( |
| 916 | (void *)b->b_instr, newsize); |
| 917 | if (tmp == NULL) { |
| 918 | PyErr_NoMemory(); |
| 919 | return -1; |
| 920 | } |
| 921 | b->b_instr = tmp; |
| 922 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 923 | } |
| 924 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 927 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 928 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 929 | The line number is reset in the following cases: |
| 930 | - when entering a new scope |
| 931 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 932 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 933 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 934 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 935 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 936 | #define SET_LOC(c, x) \ |
| 937 | (c)->u->u_lineno = (x)->lineno; \ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 938 | (c)->u->u_col_offset = (x)->col_offset; \ |
| 939 | (c)->u->u_end_lineno = (x)->end_lineno; \ |
| 940 | (c)->u->u_end_col_offset = (x)->end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 941 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 942 | /* Return the stack effect of opcode with argument oparg. |
| 943 | |
| 944 | Some opcodes have different stack effect when jump to the target and |
| 945 | when not jump. The 'jump' parameter specifies the case: |
| 946 | |
| 947 | * 0 -- when not jump |
| 948 | * 1 -- when jump |
| 949 | * -1 -- maximal |
| 950 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 951 | static int |
| 952 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 955 | case NOP: |
| 956 | case EXTENDED_ARG: |
| 957 | return 0; |
| 958 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 959 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | case POP_TOP: |
| 961 | return -1; |
| 962 | case ROT_TWO: |
| 963 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 964 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | return 0; |
| 966 | case DUP_TOP: |
| 967 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 968 | case DUP_TOP_TWO: |
| 969 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 970 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 971 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | case UNARY_POSITIVE: |
| 973 | case UNARY_NEGATIVE: |
| 974 | case UNARY_NOT: |
| 975 | case UNARY_INVERT: |
| 976 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | case SET_ADD: |
| 979 | case LIST_APPEND: |
| 980 | return -1; |
| 981 | case MAP_ADD: |
| 982 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 983 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 984 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | case BINARY_POWER: |
| 986 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 987 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | case BINARY_MODULO: |
| 989 | case BINARY_ADD: |
| 990 | case BINARY_SUBTRACT: |
| 991 | case BINARY_SUBSCR: |
| 992 | case BINARY_FLOOR_DIVIDE: |
| 993 | case BINARY_TRUE_DIVIDE: |
| 994 | return -1; |
| 995 | case INPLACE_FLOOR_DIVIDE: |
| 996 | case INPLACE_TRUE_DIVIDE: |
| 997 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | case INPLACE_ADD: |
| 1000 | case INPLACE_SUBTRACT: |
| 1001 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1002 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | case INPLACE_MODULO: |
| 1004 | return -1; |
| 1005 | case STORE_SUBSCR: |
| 1006 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | case DELETE_SUBSCR: |
| 1008 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | case BINARY_LSHIFT: |
| 1011 | case BINARY_RSHIFT: |
| 1012 | case BINARY_AND: |
| 1013 | case BINARY_XOR: |
| 1014 | case BINARY_OR: |
| 1015 | return -1; |
| 1016 | case INPLACE_POWER: |
| 1017 | return -1; |
| 1018 | case GET_ITER: |
| 1019 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | case PRINT_EXPR: |
| 1022 | return -1; |
| 1023 | case LOAD_BUILD_CLASS: |
| 1024 | return 1; |
| 1025 | case INPLACE_LSHIFT: |
| 1026 | case INPLACE_RSHIFT: |
| 1027 | case INPLACE_AND: |
| 1028 | case INPLACE_XOR: |
| 1029 | case INPLACE_OR: |
| 1030 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1033 | /* 1 in the normal flow. |
| 1034 | * Restore the stack position and push 6 values before jumping to |
| 1035 | * the handler if an exception be raised. */ |
| 1036 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | case RETURN_VALUE: |
| 1038 | return -1; |
| 1039 | case IMPORT_STAR: |
| 1040 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1041 | case SETUP_ANNOTATIONS: |
| 1042 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | case YIELD_VALUE: |
| 1044 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1045 | case YIELD_FROM: |
| 1046 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | case POP_BLOCK: |
| 1048 | return 0; |
| 1049 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1050 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | case STORE_NAME: |
| 1053 | return -1; |
| 1054 | case DELETE_NAME: |
| 1055 | return 0; |
| 1056 | case UNPACK_SEQUENCE: |
| 1057 | return oparg-1; |
| 1058 | case UNPACK_EX: |
| 1059 | return (oparg&0xFF) + (oparg>>8); |
| 1060 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1061 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1062 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | case STORE_ATTR: |
| 1065 | return -2; |
| 1066 | case DELETE_ATTR: |
| 1067 | return -1; |
| 1068 | case STORE_GLOBAL: |
| 1069 | return -1; |
| 1070 | case DELETE_GLOBAL: |
| 1071 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | case LOAD_CONST: |
| 1073 | return 1; |
| 1074 | case LOAD_NAME: |
| 1075 | return 1; |
| 1076 | case BUILD_TUPLE: |
| 1077 | case BUILD_LIST: |
| 1078 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1079 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | return 1-oparg; |
| 1081 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1082 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1083 | case BUILD_CONST_KEY_MAP: |
| 1084 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | case LOAD_ATTR: |
| 1086 | return 0; |
| 1087 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1088 | case IS_OP: |
| 1089 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1091 | case JUMP_IF_NOT_EXC_MATCH: |
| 1092 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | case IMPORT_NAME: |
| 1094 | return -1; |
| 1095 | case IMPORT_FROM: |
| 1096 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1097 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1098 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | case JUMP_ABSOLUTE: |
| 1101 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1102 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1103 | case JUMP_IF_TRUE_OR_POP: |
| 1104 | case JUMP_IF_FALSE_OR_POP: |
| 1105 | return jump ? 0 : -1; |
| 1106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | case POP_JUMP_IF_FALSE: |
| 1108 | case POP_JUMP_IF_TRUE: |
| 1109 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | case LOAD_GLOBAL: |
| 1112 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1113 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1114 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1116 | /* 0 in the normal flow. |
| 1117 | * Restore the stack position and push 6 values before jumping to |
| 1118 | * the handler if an exception be raised. */ |
| 1119 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1120 | case RERAISE: |
| 1121 | return -3; |
| 1122 | |
| 1123 | case WITH_EXCEPT_START: |
| 1124 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | case LOAD_FAST: |
| 1127 | return 1; |
| 1128 | case STORE_FAST: |
| 1129 | return -1; |
| 1130 | case DELETE_FAST: |
| 1131 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | case RAISE_VARARGS: |
| 1134 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1135 | |
| 1136 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1138 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1139 | case CALL_METHOD: |
| 1140 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1142 | return -oparg-1; |
| 1143 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1144 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1145 | case MAKE_FUNCTION: |
| 1146 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1147 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | case BUILD_SLICE: |
| 1149 | if (oparg == 3) |
| 1150 | return -2; |
| 1151 | else |
| 1152 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1153 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1154 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | case LOAD_CLOSURE: |
| 1156 | return 1; |
| 1157 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1158 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | return 1; |
| 1160 | case STORE_DEREF: |
| 1161 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1162 | case DELETE_DEREF: |
| 1163 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1164 | |
| 1165 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1166 | case GET_AWAITABLE: |
| 1167 | return 0; |
| 1168 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1169 | /* 0 in the normal flow. |
| 1170 | * Restore the stack position to the position before the result |
| 1171 | * of __aenter__ and push 6 values before jumping to the handler |
| 1172 | * if an exception be raised. */ |
| 1173 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1174 | case BEFORE_ASYNC_WITH: |
| 1175 | return 1; |
| 1176 | case GET_AITER: |
| 1177 | return 0; |
| 1178 | case GET_ANEXT: |
| 1179 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1180 | case GET_YIELD_FROM_ITER: |
| 1181 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1182 | case END_ASYNC_FOR: |
| 1183 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1184 | case FORMAT_VALUE: |
| 1185 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1186 | else 1->1. */ |
| 1187 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1188 | case LOAD_METHOD: |
| 1189 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1190 | case LOAD_ASSERTION_ERROR: |
| 1191 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1192 | case LIST_TO_TUPLE: |
| 1193 | return 0; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 1194 | case GEN_START: |
| 1195 | return -1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1196 | case LIST_EXTEND: |
| 1197 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1198 | case DICT_MERGE: |
| 1199 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1200 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1201 | case COPY_DICT_WITHOUT_KEYS: |
| 1202 | return 0; |
| 1203 | case MATCH_CLASS: |
| 1204 | return -1; |
| 1205 | case GET_LEN: |
| 1206 | case MATCH_MAPPING: |
| 1207 | case MATCH_SEQUENCE: |
| 1208 | return 1; |
| 1209 | case MATCH_KEYS: |
| 1210 | return 2; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 1211 | case ROT_N: |
| 1212 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1214 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1216 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1219 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1220 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1221 | { |
| 1222 | return stack_effect(opcode, oparg, jump); |
| 1223 | } |
| 1224 | |
| 1225 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1226 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1227 | { |
| 1228 | return stack_effect(opcode, oparg, -1); |
| 1229 | } |
| 1230 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1231 | /* Add an opcode with no argument. |
| 1232 | Returns 0 on failure, 1 on success. |
| 1233 | */ |
| 1234 | |
| 1235 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1236 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | basicblock *b; |
| 1239 | struct instr *i; |
| 1240 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1241 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1242 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | if (off < 0) |
| 1244 | return 0; |
| 1245 | b = c->u->u_curblock; |
| 1246 | i = &b->b_instr[off]; |
| 1247 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1248 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | if (opcode == RETURN_VALUE) |
| 1250 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1251 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1255 | static int |
| 1256 | compiler_addop(struct compiler *c, int opcode) |
| 1257 | { |
| 1258 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1259 | } |
| 1260 | |
| 1261 | static int |
| 1262 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1263 | { |
| 1264 | return compiler_addop_line(c, opcode, -1); |
| 1265 | } |
| 1266 | |
| 1267 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1268 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1269 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1270 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1271 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1273 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1274 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1276 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1278 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1279 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1280 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | return -1; |
| 1283 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1284 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | Py_DECREF(v); |
| 1286 | return -1; |
| 1287 | } |
| 1288 | Py_DECREF(v); |
| 1289 | } |
| 1290 | else |
| 1291 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1292 | return arg; |
| 1293 | } |
| 1294 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1295 | // Merge const *o* recursively and return constant key object. |
| 1296 | static PyObject* |
| 1297 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1298 | { |
| 1299 | // None and Ellipsis are singleton, and key is the singleton. |
| 1300 | // No need to merge object and key. |
| 1301 | if (o == Py_None || o == Py_Ellipsis) { |
| 1302 | Py_INCREF(o); |
| 1303 | return o; |
| 1304 | } |
| 1305 | |
| 1306 | PyObject *key = _PyCode_ConstantKey(o); |
| 1307 | if (key == NULL) { |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | // t is borrowed reference |
| 1312 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1313 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1314 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1315 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1316 | Py_DECREF(key); |
| 1317 | return t; |
| 1318 | } |
| 1319 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1320 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1321 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1322 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1323 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1324 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1325 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1326 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1327 | PyObject *u = merge_consts_recursive(c, item); |
| 1328 | if (u == NULL) { |
| 1329 | Py_DECREF(key); |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
| 1333 | // See _PyCode_ConstantKey() |
| 1334 | PyObject *v; // borrowed |
| 1335 | if (PyTuple_CheckExact(u)) { |
| 1336 | v = PyTuple_GET_ITEM(u, 1); |
| 1337 | } |
| 1338 | else { |
| 1339 | v = u; |
| 1340 | } |
| 1341 | if (v != item) { |
| 1342 | Py_INCREF(v); |
| 1343 | PyTuple_SET_ITEM(o, i, v); |
| 1344 | Py_DECREF(item); |
| 1345 | } |
| 1346 | |
| 1347 | Py_DECREF(u); |
| 1348 | } |
| 1349 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1350 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1351 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1352 | // constant keys. |
| 1353 | // See _PyCode_ConstantKey() for detail. |
| 1354 | assert(PyTuple_CheckExact(key)); |
| 1355 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1356 | |
| 1357 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1358 | if (len == 0) { // empty frozenset should not be re-created. |
| 1359 | return key; |
| 1360 | } |
| 1361 | PyObject *tuple = PyTuple_New(len); |
| 1362 | if (tuple == NULL) { |
| 1363 | Py_DECREF(key); |
| 1364 | return NULL; |
| 1365 | } |
| 1366 | Py_ssize_t i = 0, pos = 0; |
| 1367 | PyObject *item; |
| 1368 | Py_hash_t hash; |
| 1369 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1370 | PyObject *k = merge_consts_recursive(c, item); |
| 1371 | if (k == NULL) { |
| 1372 | Py_DECREF(tuple); |
| 1373 | Py_DECREF(key); |
| 1374 | return NULL; |
| 1375 | } |
| 1376 | PyObject *u; |
| 1377 | if (PyTuple_CheckExact(k)) { |
| 1378 | u = PyTuple_GET_ITEM(k, 1); |
| 1379 | Py_INCREF(u); |
| 1380 | Py_DECREF(k); |
| 1381 | } |
| 1382 | else { |
| 1383 | u = k; |
| 1384 | } |
| 1385 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1386 | i++; |
| 1387 | } |
| 1388 | |
| 1389 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1390 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1391 | PyObject *new = PyFrozenSet_New(tuple); |
| 1392 | Py_DECREF(tuple); |
| 1393 | if (new == NULL) { |
| 1394 | Py_DECREF(key); |
| 1395 | return NULL; |
| 1396 | } |
| 1397 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1398 | Py_DECREF(o); |
| 1399 | PyTuple_SET_ITEM(key, 1, new); |
| 1400 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1401 | |
| 1402 | return key; |
| 1403 | } |
| 1404 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1405 | static Py_ssize_t |
| 1406 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1407 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1408 | PyObject *key = merge_consts_recursive(c, o); |
| 1409 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1410 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1411 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1412 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1413 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1414 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1419 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1420 | { |
| 1421 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1422 | if (arg < 0) |
| 1423 | return 0; |
| 1424 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1425 | } |
| 1426 | |
| 1427 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1430 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1431 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1432 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1433 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1434 | return compiler_addop_i(c, opcode, arg); |
| 1435 | } |
| 1436 | |
| 1437 | static int |
| 1438 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1441 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1442 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1443 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1444 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1445 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1446 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1447 | Py_DECREF(mangled); |
| 1448 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1449 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1450 | return compiler_addop_i(c, opcode, arg); |
| 1451 | } |
| 1452 | |
| 1453 | /* Add an opcode with an integer argument. |
| 1454 | Returns 0 on failure, 1 on success. |
| 1455 | */ |
| 1456 | |
| 1457 | static int |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1458 | 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] | 1459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | struct instr *i; |
| 1461 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1462 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1463 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1464 | it in the C code (like Python/ceval.c). |
| 1465 | |
| 1466 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1467 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1468 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1469 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1470 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1471 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1472 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1473 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | if (off < 0) |
| 1475 | return 0; |
| 1476 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1477 | i->i_opcode = opcode; |
| 1478 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1479 | i->i_lineno = lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1483 | static int |
| 1484 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1485 | { |
| 1486 | return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno); |
| 1487 | } |
| 1488 | |
| 1489 | static int |
| 1490 | compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1491 | { |
| 1492 | return compiler_addop_i_line(c, opcode, oparg, -1); |
| 1493 | } |
| 1494 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1495 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1496 | { |
| 1497 | assert(HAS_ARG(opcode)); |
| 1498 | assert(b != NULL); |
| 1499 | assert(target != NULL); |
| 1500 | |
| 1501 | int off = compiler_next_instr(b); |
| 1502 | struct instr *i = &b->b_instr[off]; |
| 1503 | if (off < 0) { |
| 1504 | return 0; |
| 1505 | } |
| 1506 | i->i_opcode = opcode; |
| 1507 | i->i_target = target; |
| 1508 | i->i_lineno = lineno; |
| 1509 | return 1; |
| 1510 | } |
| 1511 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1512 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1513 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1514 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1515 | 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] | 1516 | } |
| 1517 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1518 | static int |
| 1519 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1520 | { |
| 1521 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1522 | } |
| 1523 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1524 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1525 | to the new block. |
| 1526 | |
| 1527 | The returns inside this macro make it impossible to decref objects |
| 1528 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1530 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | if (compiler_next_block((C)) == NULL) \ |
| 1532 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | if (!compiler_addop((C), (OP))) \ |
| 1537 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1540 | #define ADDOP_NOLINE(C, OP) { \ |
| 1541 | if (!compiler_addop_noline((C), (OP))) \ |
| 1542 | return 0; \ |
| 1543 | } |
| 1544 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1545 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | if (!compiler_addop((C), (OP))) { \ |
| 1547 | compiler_exit_scope(c); \ |
| 1548 | return 0; \ |
| 1549 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1552 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1553 | if (!compiler_addop_load_const((C), (O))) \ |
| 1554 | return 0; \ |
| 1555 | } |
| 1556 | |
| 1557 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1558 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1559 | PyObject *__new_const = (O); \ |
| 1560 | if (__new_const == NULL) { \ |
| 1561 | return 0; \ |
| 1562 | } \ |
| 1563 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1564 | Py_DECREF(__new_const); \ |
| 1565 | return 0; \ |
| 1566 | } \ |
| 1567 | Py_DECREF(__new_const); \ |
| 1568 | } |
| 1569 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1570 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1572 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1575 | /* Same as ADDOP_O, but steals a reference. */ |
| 1576 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1577 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1578 | Py_DECREF((O)); \ |
| 1579 | return 0; \ |
| 1580 | } \ |
| 1581 | Py_DECREF((O)); \ |
| 1582 | } |
| 1583 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1584 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1586 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1591 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1594 | #define ADDOP_I_NOLINE(C, OP, O) { \ |
| 1595 | if (!compiler_addop_i_noline((C), (OP), (O))) \ |
| 1596 | return 0; \ |
| 1597 | } |
| 1598 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1599 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1600 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1604 | /* Add a jump with no line number. |
| 1605 | * Used for artificial jumps that have no corresponding |
| 1606 | * token in the source code. */ |
| 1607 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1608 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1609 | return 0; \ |
| 1610 | } |
| 1611 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1612 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1613 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1614 | return 0; \ |
| 1615 | } |
| 1616 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1617 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1618 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1619 | */ |
| 1620 | |
| 1621 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1623 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1626 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1628 | compiler_exit_scope(c); \ |
| 1629 | return 0; \ |
| 1630 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1633 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1634 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1635 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1640 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1642 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1643 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1644 | return 0; \ |
| 1645 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1648 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1650 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1652 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1653 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1654 | compiler_exit_scope(c); \ |
| 1655 | return 0; \ |
| 1656 | } \ |
| 1657 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1660 | #define RETURN_IF_FALSE(X) \ |
| 1661 | if (!(X)) { \ |
| 1662 | return 0; \ |
| 1663 | } |
| 1664 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1665 | /* Search if variable annotations are present statically in a block. */ |
| 1666 | |
| 1667 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1668 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1669 | { |
| 1670 | int i, j, res = 0; |
| 1671 | stmt_ty st; |
| 1672 | |
| 1673 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1674 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1675 | switch (st->kind) { |
| 1676 | case AnnAssign_kind: |
| 1677 | return 1; |
| 1678 | case For_kind: |
| 1679 | res = find_ann(st->v.For.body) || |
| 1680 | find_ann(st->v.For.orelse); |
| 1681 | break; |
| 1682 | case AsyncFor_kind: |
| 1683 | res = find_ann(st->v.AsyncFor.body) || |
| 1684 | find_ann(st->v.AsyncFor.orelse); |
| 1685 | break; |
| 1686 | case While_kind: |
| 1687 | res = find_ann(st->v.While.body) || |
| 1688 | find_ann(st->v.While.orelse); |
| 1689 | break; |
| 1690 | case If_kind: |
| 1691 | res = find_ann(st->v.If.body) || |
| 1692 | find_ann(st->v.If.orelse); |
| 1693 | break; |
| 1694 | case With_kind: |
| 1695 | res = find_ann(st->v.With.body); |
| 1696 | break; |
| 1697 | case AsyncWith_kind: |
| 1698 | res = find_ann(st->v.AsyncWith.body); |
| 1699 | break; |
| 1700 | case Try_kind: |
| 1701 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1702 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1703 | st->v.Try.handlers, j); |
| 1704 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1705 | return 1; |
| 1706 | } |
| 1707 | } |
| 1708 | res = find_ann(st->v.Try.body) || |
| 1709 | find_ann(st->v.Try.finalbody) || |
| 1710 | find_ann(st->v.Try.orelse); |
| 1711 | break; |
| 1712 | default: |
| 1713 | res = 0; |
| 1714 | } |
| 1715 | if (res) { |
| 1716 | break; |
| 1717 | } |
| 1718 | } |
| 1719 | return res; |
| 1720 | } |
| 1721 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1722 | /* |
| 1723 | * Frame block handling functions |
| 1724 | */ |
| 1725 | |
| 1726 | static int |
| 1727 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1728 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1729 | { |
| 1730 | struct fblockinfo *f; |
| 1731 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1732 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1733 | } |
| 1734 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1735 | f->fb_type = t; |
| 1736 | f->fb_block = b; |
| 1737 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1738 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1739 | return 1; |
| 1740 | } |
| 1741 | |
| 1742 | static void |
| 1743 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1744 | { |
| 1745 | struct compiler_unit *u = c->u; |
| 1746 | assert(u->u_nfblocks > 0); |
| 1747 | u->u_nfblocks--; |
| 1748 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1749 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1750 | } |
| 1751 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1752 | static int |
| 1753 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1754 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1755 | ADDOP(c, DUP_TOP); |
| 1756 | ADDOP(c, DUP_TOP); |
| 1757 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1761 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1762 | * popping the blocks will be restored afterwards, unless another |
| 1763 | * return, break or continue is found. In which case, the TOS will |
| 1764 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1765 | */ |
| 1766 | static int |
| 1767 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1768 | int preserve_tos) |
| 1769 | { |
| 1770 | switch (info->fb_type) { |
| 1771 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1772 | case EXCEPTION_HANDLER: |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 1773 | case ASYNC_COMPREHENSION_GENERATOR: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1774 | return 1; |
| 1775 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1776 | case FOR_LOOP: |
| 1777 | /* Pop the iterator */ |
| 1778 | if (preserve_tos) { |
| 1779 | ADDOP(c, ROT_TWO); |
| 1780 | } |
| 1781 | ADDOP(c, POP_TOP); |
| 1782 | return 1; |
| 1783 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1784 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1785 | ADDOP(c, POP_BLOCK); |
| 1786 | return 1; |
| 1787 | |
| 1788 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1789 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1790 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1791 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1792 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1793 | return 0; |
| 1794 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1795 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1796 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1797 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1798 | if (preserve_tos) { |
| 1799 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1800 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1801 | /* The finally block should appear to execute after the |
| 1802 | * statement causing the unwinding, so make the unwinding |
| 1803 | * instruction artificial */ |
| 1804 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1805 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1806 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1807 | case FINALLY_END: |
| 1808 | if (preserve_tos) { |
| 1809 | ADDOP(c, ROT_FOUR); |
| 1810 | } |
| 1811 | ADDOP(c, POP_TOP); |
| 1812 | ADDOP(c, POP_TOP); |
| 1813 | ADDOP(c, POP_TOP); |
| 1814 | if (preserve_tos) { |
| 1815 | ADDOP(c, ROT_FOUR); |
| 1816 | } |
| 1817 | ADDOP(c, POP_EXCEPT); |
| 1818 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1819 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1820 | case WITH: |
| 1821 | case ASYNC_WITH: |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 1822 | SET_LOC(c, (stmt_ty)info->fb_datum); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1823 | ADDOP(c, POP_BLOCK); |
| 1824 | if (preserve_tos) { |
| 1825 | ADDOP(c, ROT_TWO); |
| 1826 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1827 | if(!compiler_call_exit_with_nones(c)) { |
| 1828 | return 0; |
| 1829 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1830 | if (info->fb_type == ASYNC_WITH) { |
| 1831 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1832 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1833 | ADDOP(c, YIELD_FROM); |
| 1834 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1835 | ADDOP(c, POP_TOP); |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 1836 | /* The exit block should appear to execute after the |
| 1837 | * statement causing the unwinding, so make the unwinding |
| 1838 | * instruction artificial */ |
| 1839 | c->u->u_lineno = -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1840 | return 1; |
| 1841 | |
| 1842 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1843 | if (info->fb_datum) { |
| 1844 | ADDOP(c, POP_BLOCK); |
| 1845 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1846 | if (preserve_tos) { |
| 1847 | ADDOP(c, ROT_FOUR); |
| 1848 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1849 | ADDOP(c, POP_EXCEPT); |
| 1850 | if (info->fb_datum) { |
| 1851 | ADDOP_LOAD_CONST(c, Py_None); |
| 1852 | compiler_nameop(c, info->fb_datum, Store); |
| 1853 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1854 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1855 | return 1; |
| 1856 | |
| 1857 | case POP_VALUE: |
| 1858 | if (preserve_tos) { |
| 1859 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1860 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1861 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1862 | return 1; |
| 1863 | } |
| 1864 | Py_UNREACHABLE(); |
| 1865 | } |
| 1866 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1867 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1868 | static int |
| 1869 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1870 | if (c->u->u_nfblocks == 0) { |
| 1871 | return 1; |
| 1872 | } |
| 1873 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1874 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1875 | *loop = top; |
| 1876 | return 1; |
| 1877 | } |
| 1878 | struct fblockinfo copy = *top; |
| 1879 | c->u->u_nfblocks--; |
| 1880 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1881 | return 0; |
| 1882 | } |
| 1883 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1884 | return 0; |
| 1885 | } |
| 1886 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1887 | c->u->u_nfblocks++; |
| 1888 | return 1; |
| 1889 | } |
| 1890 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1891 | /* Compile a sequence of statements, checking for a docstring |
| 1892 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1893 | |
| 1894 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1895 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1896 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1897 | int i = 0; |
| 1898 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1899 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1900 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1901 | /* Set current line number to the line number of first statement. |
| 1902 | This way line number for SETUP_ANNOTATIONS will always |
| 1903 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1904 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1905 | 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] | 1906 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1907 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1908 | } |
| 1909 | /* Every annotated class and module should have __annotations__. */ |
| 1910 | if (find_ann(stmts)) { |
| 1911 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1912 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1913 | if (!asdl_seq_LEN(stmts)) |
| 1914 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1915 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1916 | if (c->c_optimize < 2) { |
| 1917 | docstring = _PyAST_GetDocString(stmts); |
| 1918 | if (docstring) { |
| 1919 | i = 1; |
| 1920 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1921 | assert(st->kind == Expr_kind); |
| 1922 | VISIT(c, expr, st->v.Expr.value); |
| 1923 | if (!compiler_nameop(c, __doc__, Store)) |
| 1924 | return 0; |
| 1925 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1927 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1928 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | static PyCodeObject * |
| 1933 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | PyCodeObject *co; |
| 1936 | int addNone = 1; |
| 1937 | static PyObject *module; |
| 1938 | if (!module) { |
| 1939 | module = PyUnicode_InternFromString("<module>"); |
| 1940 | if (!module) |
| 1941 | return NULL; |
| 1942 | } |
| 1943 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1944 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | return NULL; |
| 1946 | switch (mod->kind) { |
| 1947 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1948 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | compiler_exit_scope(c); |
| 1950 | return 0; |
| 1951 | } |
| 1952 | break; |
| 1953 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1954 | if (find_ann(mod->v.Interactive.body)) { |
| 1955 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1956 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1958 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | case Expression_kind: |
| 1961 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1962 | addNone = 0; |
| 1963 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | default: |
| 1965 | PyErr_Format(PyExc_SystemError, |
| 1966 | "module kind %d should not be possible", |
| 1967 | mod->kind); |
| 1968 | return 0; |
| 1969 | } |
| 1970 | co = assemble(c, addNone); |
| 1971 | compiler_exit_scope(c); |
| 1972 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1975 | /* The test for LOCAL must come before the test for FREE in order to |
| 1976 | handle classes where name is both local and free. The local var is |
| 1977 | 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] | 1978 | */ |
| 1979 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1980 | static int |
| 1981 | get_ref_type(struct compiler *c, PyObject *name) |
| 1982 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1983 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1984 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1985 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1986 | return CELL; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1987 | scope = _PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1989 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1990 | "_PyST_GetScope(name=%R) failed: " |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1991 | "unknown scope in unit %S (%R); " |
| 1992 | "symbols: %R; locals: %R; globals: %R", |
| 1993 | name, |
| 1994 | c->u->u_name, c->u->u_ste->ste_id, |
| 1995 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1996 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | static int |
| 2002 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 2003 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2004 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 2005 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2006 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 2007 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2008 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2012 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 2013 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2014 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2015 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2016 | if (qualname == NULL) |
| 2017 | qualname = co->co_name; |
| 2018 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2019 | if (free) { |
| 2020 | for (i = 0; i < free; ++i) { |
| 2021 | /* Bypass com_addop_varname because it will generate |
| 2022 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2023 | */ |
| 2024 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2025 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2026 | /* Special case: If a class contains a method with a |
| 2027 | free variable that has the same name as a method, |
| 2028 | the name will be considered free *and* local in the |
| 2029 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 2030 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2031 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2032 | int reftype = get_ref_type(c, name); |
| 2033 | if (reftype == -1) { |
| 2034 | return 0; |
| 2035 | } |
| 2036 | int arg; |
| 2037 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2038 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2039 | } |
| 2040 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2041 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2042 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2043 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2044 | PyErr_Format(PyExc_SystemError, |
| 2045 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 2046 | "freevars of code %S: %R", |
| 2047 | name, |
| 2048 | reftype, |
| 2049 | c->u->u_name, |
| 2050 | co->co_name, |
| 2051 | co->co_freevars); |
| 2052 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2053 | } |
| 2054 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2056 | flags |= 0x08; |
| 2057 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2059 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 2060 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2061 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2066 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | if (!decos) |
| 2071 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2074 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2075 | } |
| 2076 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2080 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2081 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2082 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2083 | /* Push a dict of keyword-only default values. |
| 2084 | |
| 2085 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2086 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2087 | int i; |
| 2088 | PyObject *keys = NULL; |
| 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2091 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2092 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2093 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2094 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2095 | if (!mangled) { |
| 2096 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2098 | if (keys == NULL) { |
| 2099 | keys = PyList_New(1); |
| 2100 | if (keys == NULL) { |
| 2101 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2102 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2103 | } |
| 2104 | PyList_SET_ITEM(keys, 0, mangled); |
| 2105 | } |
| 2106 | else { |
| 2107 | int res = PyList_Append(keys, mangled); |
| 2108 | Py_DECREF(mangled); |
| 2109 | if (res == -1) { |
| 2110 | goto error; |
| 2111 | } |
| 2112 | } |
| 2113 | if (!compiler_visit_expr(c, default_)) { |
| 2114 | goto error; |
| 2115 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | } |
| 2117 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2118 | if (keys != NULL) { |
| 2119 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2120 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2121 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2122 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2123 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2124 | assert(default_count > 0); |
| 2125 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2126 | } |
| 2127 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2128 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | error: |
| 2132 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2133 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2137 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2138 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2139 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2140 | return 1; |
| 2141 | } |
| 2142 | |
| 2143 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2144 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2145 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2146 | { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2147 | if (!annotation) { |
| 2148 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | } |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2150 | |
| 2151 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
| 2152 | if (!mangled) { |
| 2153 | return 0; |
| 2154 | } |
| 2155 | ADDOP_LOAD_CONST(c, mangled); |
| 2156 | Py_DECREF(mangled); |
| 2157 | |
| 2158 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2159 | VISIT(c, annexpr, annotation) |
| 2160 | } |
| 2161 | else { |
| 2162 | VISIT(c, expr, annotation); |
| 2163 | } |
| 2164 | *annotations_len += 2; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2165 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2169 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2170 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2171 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2172 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2174 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2175 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | c, |
| 2177 | arg->arg, |
| 2178 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2179 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2180 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2182 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | static int |
| 2186 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2187 | expr_ty returns) |
| 2188 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2189 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2190 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2191 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2192 | 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] | 2193 | */ |
| 2194 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2195 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2196 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2197 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2198 | return 0; |
| 2199 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2200 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2201 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2202 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2203 | args->vararg->annotation, &annotations_len)) |
| 2204 | return 0; |
| 2205 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2206 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2207 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2208 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2209 | args->kwarg->annotation, &annotations_len)) |
| 2210 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | if (!return_str) { |
| 2213 | return_str = PyUnicode_InternFromString("return"); |
| 2214 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2215 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2217 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2218 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2221 | if (annotations_len) { |
| 2222 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2223 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2225 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2226 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2230 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2231 | { |
| 2232 | VISIT_SEQ(c, expr, args->defaults); |
| 2233 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2234 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2237 | static Py_ssize_t |
| 2238 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2239 | { |
| 2240 | Py_ssize_t funcflags = 0; |
| 2241 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2242 | if (!compiler_visit_defaults(c, args)) |
| 2243 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2244 | funcflags |= 0x01; |
| 2245 | } |
| 2246 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2247 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2248 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2249 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2250 | return -1; |
| 2251 | } |
| 2252 | else if (res > 0) { |
| 2253 | funcflags |= 0x02; |
| 2254 | } |
| 2255 | } |
| 2256 | return funcflags; |
| 2257 | } |
| 2258 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2259 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2260 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2261 | { |
| 2262 | |
| 2263 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2264 | compiler_error(c, "cannot assign to __debug__"); |
| 2265 | return 1; |
| 2266 | } |
Dong-hee Na | 32c1caa | 2021-08-26 09:52:21 +0000 | [diff] [blame^] | 2267 | if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2268 | compiler_error(c, "cannot delete __debug__"); |
| 2269 | return 1; |
| 2270 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2271 | return 0; |
| 2272 | } |
| 2273 | |
| 2274 | static int |
| 2275 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2276 | { |
| 2277 | if (arg != NULL) { |
| 2278 | if (forbidden_name(c, arg->arg, Store)) |
| 2279 | return 0; |
| 2280 | } |
| 2281 | return 1; |
| 2282 | } |
| 2283 | |
| 2284 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2285 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2286 | { |
| 2287 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2288 | 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] | 2289 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2290 | return 0; |
| 2291 | } |
| 2292 | } |
| 2293 | return 1; |
| 2294 | } |
| 2295 | |
| 2296 | static int |
| 2297 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2298 | { |
| 2299 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2300 | return 0; |
| 2301 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2302 | return 0; |
| 2303 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2304 | return 0; |
| 2305 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2306 | return 0; |
| 2307 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2308 | return 0; |
| 2309 | return 1; |
| 2310 | } |
| 2311 | |
| 2312 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2313 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2316 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2317 | arguments_ty args; |
| 2318 | expr_ty returns; |
| 2319 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2320 | asdl_expr_seq* decos; |
| 2321 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2322 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2323 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2324 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2325 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2326 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2327 | if (is_async) { |
| 2328 | assert(s->kind == AsyncFunctionDef_kind); |
| 2329 | |
| 2330 | args = s->v.AsyncFunctionDef.args; |
| 2331 | returns = s->v.AsyncFunctionDef.returns; |
| 2332 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2333 | name = s->v.AsyncFunctionDef.name; |
| 2334 | body = s->v.AsyncFunctionDef.body; |
| 2335 | |
| 2336 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2337 | } else { |
| 2338 | assert(s->kind == FunctionDef_kind); |
| 2339 | |
| 2340 | args = s->v.FunctionDef.args; |
| 2341 | returns = s->v.FunctionDef.returns; |
| 2342 | decos = s->v.FunctionDef.decorator_list; |
| 2343 | name = s->v.FunctionDef.name; |
| 2344 | body = s->v.FunctionDef.body; |
| 2345 | |
| 2346 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2347 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2348 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2349 | if (!compiler_check_debug_args(c, args)) |
| 2350 | return 0; |
| 2351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | if (!compiler_decorators(c, decos)) |
| 2353 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2354 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2355 | firstlineno = s->lineno; |
| 2356 | if (asdl_seq_LEN(decos)) { |
| 2357 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2358 | } |
| 2359 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2360 | funcflags = compiler_default_arguments(c, args); |
| 2361 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2363 | } |
| 2364 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2365 | annotations = compiler_visit_annotations(c, args, returns); |
| 2366 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2367 | return 0; |
| 2368 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2369 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2370 | funcflags |= 0x04; |
| 2371 | } |
| 2372 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2373 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2374 | return 0; |
| 2375 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2376 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2377 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2378 | if (c->c_optimize < 2) { |
| 2379 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2380 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2381 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | compiler_exit_scope(c); |
| 2383 | return 0; |
| 2384 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2387 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2389 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2390 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2391 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2393 | qualname = c->u->u_qualname; |
| 2394 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2396 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2397 | Py_XDECREF(qualname); |
| 2398 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2400 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2401 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2402 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2403 | Py_DECREF(qualname); |
| 2404 | Py_DECREF(co); |
| 2405 | return 0; |
| 2406 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2407 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | /* decorators */ |
| 2411 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2412 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2413 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2414 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2415 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | static int |
| 2419 | compiler_class(struct compiler *c, stmt_ty s) |
| 2420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | PyCodeObject *co; |
| 2422 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2423 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2424 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | if (!compiler_decorators(c, decos)) |
| 2427 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2428 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2429 | firstlineno = s->lineno; |
| 2430 | if (asdl_seq_LEN(decos)) { |
| 2431 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2432 | } |
| 2433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2434 | /* ultimately generate code for: |
| 2435 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2436 | where: |
| 2437 | <func> is a function/closure created from the class body; |
| 2438 | it has a single argument (__locals__) where the dict |
| 2439 | (or MutableSequence) representing the locals is passed |
| 2440 | <name> is the class name |
| 2441 | <bases> is the positional arguments and *varargs argument |
| 2442 | <keywords> is the keyword arguments and **kwds argument |
| 2443 | This borrows from compiler_call. |
| 2444 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2447 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2448 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | return 0; |
| 2450 | /* this block represents what we do in the new scope */ |
| 2451 | { |
| 2452 | /* use the class name for name mangling */ |
| 2453 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2454 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 | /* load (global) __name__ ... */ |
| 2456 | str = PyUnicode_InternFromString("__name__"); |
| 2457 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2458 | Py_XDECREF(str); |
| 2459 | compiler_exit_scope(c); |
| 2460 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2461 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | Py_DECREF(str); |
| 2463 | /* ... and store it as __module__ */ |
| 2464 | str = PyUnicode_InternFromString("__module__"); |
| 2465 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2466 | Py_XDECREF(str); |
| 2467 | compiler_exit_scope(c); |
| 2468 | return 0; |
| 2469 | } |
| 2470 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2471 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2472 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2473 | str = PyUnicode_InternFromString("__qualname__"); |
| 2474 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2475 | Py_XDECREF(str); |
| 2476 | compiler_exit_scope(c); |
| 2477 | return 0; |
| 2478 | } |
| 2479 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2481 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | compiler_exit_scope(c); |
| 2483 | return 0; |
| 2484 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2485 | /* The following code is artificial */ |
| 2486 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2487 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2488 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2489 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2490 | str = PyUnicode_InternFromString("__class__"); |
| 2491 | if (str == NULL) { |
| 2492 | compiler_exit_scope(c); |
| 2493 | return 0; |
| 2494 | } |
| 2495 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2496 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2497 | if (i < 0) { |
| 2498 | compiler_exit_scope(c); |
| 2499 | return 0; |
| 2500 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2501 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2504 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2505 | str = PyUnicode_InternFromString("__classcell__"); |
| 2506 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2507 | Py_XDECREF(str); |
| 2508 | compiler_exit_scope(c); |
| 2509 | return 0; |
| 2510 | } |
| 2511 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2513 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2514 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2515 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2516 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2517 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2518 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | /* create the code object */ |
| 2520 | co = assemble(c, 1); |
| 2521 | } |
| 2522 | /* leave the new scope */ |
| 2523 | compiler_exit_scope(c); |
| 2524 | if (co == NULL) |
| 2525 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | /* 2. load the 'build_class' function */ |
| 2528 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2529 | |
| 2530 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2531 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2532 | Py_DECREF(co); |
| 2533 | return 0; |
| 2534 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | Py_DECREF(co); |
| 2536 | |
| 2537 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2538 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | |
| 2540 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2541 | 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] | 2542 | return 0; |
| 2543 | |
| 2544 | /* 6. apply decorators */ |
| 2545 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2546 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2547 | } |
| 2548 | |
| 2549 | /* 7. store into <name> */ |
| 2550 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2551 | return 0; |
| 2552 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2555 | /* Return 0 if the expression is a constant value except named singletons. |
| 2556 | Return 1 otherwise. */ |
| 2557 | static int |
| 2558 | check_is_arg(expr_ty e) |
| 2559 | { |
| 2560 | if (e->kind != Constant_kind) { |
| 2561 | return 1; |
| 2562 | } |
| 2563 | PyObject *value = e->v.Constant.value; |
| 2564 | return (value == Py_None |
| 2565 | || value == Py_False |
| 2566 | || value == Py_True |
| 2567 | || value == Py_Ellipsis); |
| 2568 | } |
| 2569 | |
| 2570 | /* Check operands of identity chacks ("is" and "is not"). |
| 2571 | Emit a warning if any operand is a constant except named singletons. |
| 2572 | Return 0 on error. |
| 2573 | */ |
| 2574 | static int |
| 2575 | check_compare(struct compiler *c, expr_ty e) |
| 2576 | { |
| 2577 | Py_ssize_t i, n; |
| 2578 | int left = check_is_arg(e->v.Compare.left); |
| 2579 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2580 | for (i = 0; i < n; i++) { |
| 2581 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2582 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2583 | if (op == Is || op == IsNot) { |
| 2584 | if (!right || !left) { |
| 2585 | const char *msg = (op == Is) |
| 2586 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2587 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2588 | return compiler_warn(c, msg); |
| 2589 | } |
| 2590 | } |
| 2591 | left = right; |
| 2592 | } |
| 2593 | return 1; |
| 2594 | } |
| 2595 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2596 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2597 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2598 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2599 | switch (op) { |
| 2600 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2601 | cmp = Py_EQ; |
| 2602 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2603 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2604 | cmp = Py_NE; |
| 2605 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2606 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2607 | cmp = Py_LT; |
| 2608 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2609 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2610 | cmp = Py_LE; |
| 2611 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2612 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2613 | cmp = Py_GT; |
| 2614 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2615 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2616 | cmp = Py_GE; |
| 2617 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2618 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2619 | ADDOP_I(c, IS_OP, 0); |
| 2620 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2621 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2622 | ADDOP_I(c, IS_OP, 1); |
| 2623 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2624 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2625 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2626 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2627 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2628 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2629 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2630 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2631 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2632 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2633 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2634 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2635 | } |
| 2636 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2637 | |
| 2638 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2639 | static int |
| 2640 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2641 | { |
| 2642 | switch (e->kind) { |
| 2643 | case UnaryOp_kind: |
| 2644 | if (e->v.UnaryOp.op == Not) |
| 2645 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2646 | /* fallback to general implementation */ |
| 2647 | break; |
| 2648 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2649 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2650 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2651 | assert(n >= 0); |
| 2652 | int cond2 = e->v.BoolOp.op == Or; |
| 2653 | basicblock *next2 = next; |
| 2654 | if (!cond2 != !cond) { |
| 2655 | next2 = compiler_new_block(c); |
| 2656 | if (next2 == NULL) |
| 2657 | return 0; |
| 2658 | } |
| 2659 | for (i = 0; i < n; ++i) { |
| 2660 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2661 | return 0; |
| 2662 | } |
| 2663 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2664 | return 0; |
| 2665 | if (next2 != next) |
| 2666 | compiler_use_next_block(c, next2); |
| 2667 | return 1; |
| 2668 | } |
| 2669 | case IfExp_kind: { |
| 2670 | basicblock *end, *next2; |
| 2671 | end = compiler_new_block(c); |
| 2672 | if (end == NULL) |
| 2673 | return 0; |
| 2674 | next2 = compiler_new_block(c); |
| 2675 | if (next2 == NULL) |
| 2676 | return 0; |
| 2677 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2678 | return 0; |
| 2679 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2680 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2681 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2682 | compiler_use_next_block(c, next2); |
| 2683 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2684 | return 0; |
| 2685 | compiler_use_next_block(c, end); |
| 2686 | return 1; |
| 2687 | } |
| 2688 | case Compare_kind: { |
| 2689 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2690 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2691 | if (!check_compare(c, e)) { |
| 2692 | return 0; |
| 2693 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2694 | basicblock *cleanup = compiler_new_block(c); |
| 2695 | if (cleanup == NULL) |
| 2696 | return 0; |
| 2697 | VISIT(c, expr, e->v.Compare.left); |
| 2698 | for (i = 0; i < n; i++) { |
| 2699 | VISIT(c, expr, |
| 2700 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2701 | ADDOP(c, DUP_TOP); |
| 2702 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2703 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2704 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2705 | NEXT_BLOCK(c); |
| 2706 | } |
| 2707 | 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] | 2708 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2709 | 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] | 2710 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2711 | basicblock *end = compiler_new_block(c); |
| 2712 | if (end == NULL) |
| 2713 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2714 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2715 | compiler_use_next_block(c, cleanup); |
| 2716 | ADDOP(c, POP_TOP); |
| 2717 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2718 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2719 | } |
| 2720 | compiler_use_next_block(c, end); |
| 2721 | return 1; |
| 2722 | } |
| 2723 | /* fallback to general implementation */ |
| 2724 | break; |
| 2725 | } |
| 2726 | default: |
| 2727 | /* fallback to general implementation */ |
| 2728 | break; |
| 2729 | } |
| 2730 | |
| 2731 | /* general implementation */ |
| 2732 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2733 | 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] | 2734 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2735 | return 1; |
| 2736 | } |
| 2737 | |
| 2738 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2739 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | basicblock *end, *next; |
| 2742 | |
| 2743 | assert(e->kind == IfExp_kind); |
| 2744 | end = compiler_new_block(c); |
| 2745 | if (end == NULL) |
| 2746 | return 0; |
| 2747 | next = compiler_new_block(c); |
| 2748 | if (next == NULL) |
| 2749 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2750 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2751 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2753 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | compiler_use_next_block(c, next); |
| 2755 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2756 | compiler_use_next_block(c, end); |
| 2757 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2761 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2764 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2766 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | arguments_ty args = e->v.Lambda.args; |
| 2768 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2769 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2770 | if (!compiler_check_debug_args(c, args)) |
| 2771 | return 0; |
| 2772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | if (!name) { |
| 2774 | name = PyUnicode_InternFromString("<lambda>"); |
| 2775 | if (!name) |
| 2776 | return 0; |
| 2777 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2778 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2779 | funcflags = compiler_default_arguments(c, args); |
| 2780 | if (funcflags == -1) { |
| 2781 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2783 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2784 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2785 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | /* Make None the first constant, so the lambda can't have a |
| 2789 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2790 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2794 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2796 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2797 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2798 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | } |
| 2800 | else { |
| 2801 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2802 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2804 | qualname = c->u->u_qualname; |
| 2805 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2807 | if (co == NULL) { |
| 2808 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2810 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2811 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2812 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2813 | Py_DECREF(qualname); |
| 2814 | Py_DECREF(co); |
| 2815 | return 0; |
| 2816 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2817 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | Py_DECREF(co); |
| 2819 | |
| 2820 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2824 | compiler_if(struct compiler *c, stmt_ty s) |
| 2825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | assert(s->kind == If_kind); |
| 2828 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2829 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2831 | } |
| 2832 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2833 | next = compiler_new_block(c); |
| 2834 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2835 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2836 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2837 | } |
| 2838 | else { |
| 2839 | next = end; |
| 2840 | } |
| 2841 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2842 | return 0; |
| 2843 | } |
| 2844 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2845 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2846 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2847 | compiler_use_next_block(c, next); |
| 2848 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | } |
| 2850 | compiler_use_next_block(c, end); |
| 2851 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | static int |
| 2855 | compiler_for(struct compiler *c, stmt_ty s) |
| 2856 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2857 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2860 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | cleanup = compiler_new_block(c); |
| 2862 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2863 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2865 | } |
| 2866 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2868 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | VISIT(c, expr, s->v.For.iter); |
| 2870 | ADDOP(c, GET_ITER); |
| 2871 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2872 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2873 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | VISIT(c, expr, s->v.For.target); |
| 2875 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2876 | /* Mark jump as artificial */ |
| 2877 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2878 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2880 | |
| 2881 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2884 | compiler_use_next_block(c, end); |
| 2885 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2888 | |
| 2889 | static int |
| 2890 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2891 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2892 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2893 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2894 | c->u->u_ste->ste_coroutine = 1; |
| 2895 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2896 | return compiler_error(c, "'async for' outside async function"); |
| 2897 | } |
| 2898 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2899 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2900 | except = compiler_new_block(c); |
| 2901 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2902 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2903 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2904 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2905 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2906 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2907 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2908 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2909 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2910 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2911 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2912 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2913 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2914 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2915 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2916 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2917 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2918 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2919 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2920 | /* Success block for __anext__ */ |
| 2921 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2922 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2923 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2924 | |
| 2925 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2926 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2927 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2928 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2929 | |
Mark Shannon | 47695e3 | 2021-07-15 15:54:38 +0100 | [diff] [blame] | 2930 | /* Use same line number as the iterator, |
| 2931 | * as the END_ASYNC_FOR succeeds the `for`, not the body. */ |
| 2932 | SET_LOC(c, s->v.AsyncFor.iter); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2933 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2934 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2935 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2936 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2937 | |
| 2938 | compiler_use_next_block(c, end); |
| 2939 | |
| 2940 | return 1; |
| 2941 | } |
| 2942 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | static int |
| 2944 | compiler_while(struct compiler *c, stmt_ty s) |
| 2945 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2946 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2948 | body = compiler_new_block(c); |
| 2949 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2950 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2951 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2955 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, 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 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2958 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2959 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2964 | SET_LOC(c, s); |
| 2965 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2966 | return 0; |
| 2967 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2968 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2969 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2970 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2971 | compiler_use_next_block(c, anchor); |
| 2972 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2974 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2981 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2983 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2984 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2985 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2986 | return compiler_error(c, "'return' outside function"); |
| 2987 | if (s->v.Return.value != NULL && |
| 2988 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2989 | { |
| 2990 | return compiler_error( |
| 2991 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2992 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2993 | if (preserve_tos) { |
| 2994 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2995 | } else { |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 2996 | /* Emit instruction with line number for return value */ |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2997 | if (s->v.Return.value != NULL) { |
| 2998 | SET_LOC(c, s->v.Return.value); |
| 2999 | ADDOP(c, NOP); |
| 3000 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3001 | } |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3002 | if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) { |
| 3003 | SET_LOC(c, s); |
| 3004 | ADDOP(c, NOP); |
| 3005 | } |
| 3006 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3007 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 3008 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3009 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3010 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3011 | } |
| 3012 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 3013 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3014 | } |
| 3015 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3016 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3021 | static int |
| 3022 | compiler_break(struct compiler *c) |
| 3023 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3024 | struct fblockinfo *loop = NULL; |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3025 | /* Emit instruction with line number */ |
| 3026 | ADDOP(c, NOP); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3027 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 3028 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3029 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3030 | if (loop == NULL) { |
| 3031 | return compiler_error(c, "'break' outside loop"); |
| 3032 | } |
| 3033 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 3034 | return 0; |
| 3035 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3036 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3037 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3038 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | static int |
| 3042 | compiler_continue(struct compiler *c) |
| 3043 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3044 | struct fblockinfo *loop = NULL; |
Mark Shannon | cea0585 | 2021-06-03 19:57:31 +0100 | [diff] [blame] | 3045 | /* Emit instruction with line number */ |
| 3046 | ADDOP(c, NOP); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3047 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 3048 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3049 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3050 | if (loop == NULL) { |
| 3051 | return compiler_error(c, "'continue' not properly in loop"); |
| 3052 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3053 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3054 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3055 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3059 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | |
| 3061 | SETUP_FINALLY L |
| 3062 | <code for body> |
| 3063 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3064 | <code for finalbody> |
| 3065 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3066 | L: |
| 3067 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3068 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3070 | The special instructions use the block stack. Each block |
| 3071 | stack entry contains the instruction that created it (here |
| 3072 | SETUP_FINALLY), the level of the value stack at the time the |
| 3073 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3075 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | Pushes the current value stack level and the label |
| 3077 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3078 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3079 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3081 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3082 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 3083 | exceptions are pushed onto the value stack (and the exception |
| 3084 | condition is cleared), and the interpreter jumps to the label |
| 3085 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3086 | */ |
| 3087 | |
| 3088 | static int |
| 3089 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 3090 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3091 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | body = compiler_new_block(c); |
| 3094 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3095 | exit = compiler_new_block(c); |
| 3096 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3098 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3099 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3100 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3102 | 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] | 3103 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3104 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3105 | if (!compiler_try_except(c, s)) |
| 3106 | return 0; |
| 3107 | } |
| 3108 | else { |
| 3109 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3110 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3111 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3112 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3113 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3114 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3115 | /* `finally` block */ |
| 3116 | compiler_use_next_block(c, end); |
| 3117 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3118 | return 0; |
| 3119 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3120 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3121 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3122 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3123 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3127 | 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] | 3128 | (The contents of the value stack is shown in [], with the top |
| 3129 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3130 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | |
| 3132 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3133 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | [] <code for S> |
| 3135 | [] POP_BLOCK |
| 3136 | [] JUMP_FORWARD L0 |
| 3137 | |
| 3138 | [tb, val, exc] L1: DUP ) |
| 3139 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3140 | [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] | 3141 | [tb, val, exc] POP |
| 3142 | [tb, val] <assign to V1> (or POP if no V1) |
| 3143 | [tb] POP |
| 3144 | [] <code for S1> |
| 3145 | JUMP_FORWARD L0 |
| 3146 | |
| 3147 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3148 | .............................etc....................... |
| 3149 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3150 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | |
| 3152 | [] L0: <next statement> |
| 3153 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3154 | Of course, parts are not generated if Vi or Ei is not present. |
| 3155 | */ |
| 3156 | static int |
| 3157 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3159 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3160 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | body = compiler_new_block(c); |
| 3163 | except = compiler_new_block(c); |
| 3164 | orelse = compiler_new_block(c); |
| 3165 | end = compiler_new_block(c); |
| 3166 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3167 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3168 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3170 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3172 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3173 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3174 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3175 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3176 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3178 | /* Runtime will push a block here, so we need to account for that */ |
| 3179 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3180 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | for (i = 0; i < n; i++) { |
| 3182 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3183 | s->v.Try.handlers, i); |
Mark Shannon | 8d4b184 | 2021-05-06 13:38:50 +0100 | [diff] [blame] | 3184 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3186 | return compiler_error(c, "default 'except:' must be last"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | except = compiler_new_block(c); |
| 3188 | if (except == NULL) |
| 3189 | return 0; |
| 3190 | if (handler->v.ExceptHandler.type) { |
| 3191 | ADDOP(c, DUP_TOP); |
| 3192 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3193 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3194 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | } |
| 3196 | ADDOP(c, POP_TOP); |
| 3197 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3198 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3199 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3200 | cleanup_end = compiler_new_block(c); |
| 3201 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3202 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3203 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3204 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3205 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3206 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3207 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3209 | /* |
| 3210 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3211 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3212 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3213 | try: |
| 3214 | # body |
| 3215 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3216 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3217 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3218 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3220 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3221 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3222 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3223 | 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] | 3224 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3226 | /* second # body */ |
| 3227 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3228 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3229 | /* name = None; del name; # Mark as artificial */ |
| 3230 | c->u->u_lineno = -1; |
Mark Shannon | 794ff7d | 2021-07-14 11:43:56 +0100 | [diff] [blame] | 3231 | ADDOP(c, POP_BLOCK); |
| 3232 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3233 | ADDOP_LOAD_CONST(c, Py_None); |
| 3234 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3235 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3236 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3238 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3239 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3241 | /* name = None; del name; # Mark as artificial */ |
| 3242 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3243 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3244 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3245 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3247 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | } |
| 3249 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3250 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3252 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3253 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3254 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3256 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3257 | ADDOP(c, POP_TOP); |
| 3258 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3259 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3260 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3262 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3263 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3264 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3265 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | compiler_use_next_block(c, except); |
| 3268 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3269 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3270 | /* Mark as artificial */ |
| 3271 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3272 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3273 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3274 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | compiler_use_next_block(c, end); |
| 3276 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3280 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3281 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3282 | return compiler_try_finally(c, s); |
| 3283 | else |
| 3284 | return compiler_try_except(c, s); |
| 3285 | } |
| 3286 | |
| 3287 | |
| 3288 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3289 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | /* The IMPORT_NAME opcode was already generated. This function |
| 3292 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | 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] | 3295 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3297 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3298 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3299 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3300 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3301 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3302 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3303 | while (1) { |
| 3304 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3306 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3307 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3308 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3309 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3311 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3312 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3313 | if (dot == -1) { |
| 3314 | break; |
| 3315 | } |
| 3316 | ADDOP(c, ROT_TWO); |
| 3317 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3318 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3319 | if (!compiler_nameop(c, asname, Store)) { |
| 3320 | return 0; |
| 3321 | } |
| 3322 | ADDOP(c, POP_TOP); |
| 3323 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | } |
| 3325 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | static int |
| 3329 | compiler_import(struct compiler *c, stmt_ty s) |
| 3330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | /* The Import node stores a module name like a.b.c as a single |
| 3332 | string. This is convenient for all cases except |
| 3333 | import a.b.c as d |
| 3334 | where we need to parse that string to extract the individual |
| 3335 | module names. |
| 3336 | XXX Perhaps change the representation to make this case simpler? |
| 3337 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3338 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3339 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3340 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | for (i = 0; i < n; i++) { |
| 3342 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3343 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3344 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3345 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3346 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3347 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | if (alias->asname) { |
| 3350 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3351 | if (!r) |
| 3352 | return r; |
| 3353 | } |
| 3354 | else { |
| 3355 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3356 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3357 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3358 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3359 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3360 | if (tmp == NULL) |
| 3361 | return 0; |
| 3362 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3364 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | Py_DECREF(tmp); |
| 3366 | } |
| 3367 | if (!r) |
| 3368 | return r; |
| 3369 | } |
| 3370 | } |
| 3371 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | static int |
| 3375 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3376 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3377 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3378 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | if (!empty_string) { |
| 3382 | empty_string = PyUnicode_FromString(""); |
| 3383 | if (!empty_string) |
| 3384 | return 0; |
| 3385 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3386 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3387 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3388 | |
| 3389 | names = PyTuple_New(n); |
| 3390 | if (!names) |
| 3391 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | /* build up the names */ |
| 3394 | for (i = 0; i < n; i++) { |
| 3395 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3396 | Py_INCREF(alias->name); |
| 3397 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3398 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3401 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | Py_DECREF(names); |
| 3403 | return compiler_error(c, "from __future__ imports must occur " |
| 3404 | "at the beginning of the file"); |
| 3405 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3406 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | if (s->v.ImportFrom.module) { |
| 3409 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3410 | } |
| 3411 | else { |
| 3412 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3413 | } |
| 3414 | for (i = 0; i < n; i++) { |
| 3415 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3416 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3417 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3418 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | assert(n == 1); |
| 3420 | ADDOP(c, IMPORT_STAR); |
| 3421 | return 1; |
| 3422 | } |
| 3423 | |
| 3424 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3425 | store_name = alias->name; |
| 3426 | if (alias->asname) |
| 3427 | store_name = alias->asname; |
| 3428 | |
| 3429 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | return 0; |
| 3431 | } |
| 3432 | } |
| 3433 | /* remove imported module */ |
| 3434 | ADDOP(c, POP_TOP); |
| 3435 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | static int |
| 3439 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3442 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3443 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3444 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3445 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3446 | (s->v.Assert.test->kind == Constant_kind && |
| 3447 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3448 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3449 | { |
| 3450 | if (!compiler_warn(c, "assertion is always true, " |
| 3451 | "perhaps remove parentheses?")) |
| 3452 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3453 | return 0; |
| 3454 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3456 | if (c->c_optimize) |
| 3457 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | end = compiler_new_block(c); |
| 3459 | if (end == NULL) |
| 3460 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3461 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3462 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3463 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | if (s->v.Assert.msg) { |
| 3465 | VISIT(c, expr, s->v.Assert.msg); |
| 3466 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3467 | } |
| 3468 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3469 | compiler_use_next_block(c, end); |
| 3470 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3474 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3475 | { |
| 3476 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3477 | VISIT(c, expr, value); |
| 3478 | ADDOP(c, PRINT_EXPR); |
| 3479 | return 1; |
| 3480 | } |
| 3481 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3482 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3483 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3484 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3485 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3489 | /* Mark POP_TOP as artificial */ |
| 3490 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3491 | ADDOP(c, POP_TOP); |
| 3492 | return 1; |
| 3493 | } |
| 3494 | |
| 3495 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3496 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3497 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3498 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3501 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3503 | switch (s->kind) { |
| 3504 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3505 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | case ClassDef_kind: |
| 3507 | return compiler_class(c, s); |
| 3508 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3509 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | case Delete_kind: |
| 3511 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3512 | break; |
| 3513 | case Assign_kind: |
| 3514 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3515 | VISIT(c, expr, s->v.Assign.value); |
| 3516 | for (i = 0; i < n; i++) { |
| 3517 | if (i < n - 1) |
| 3518 | ADDOP(c, DUP_TOP); |
| 3519 | VISIT(c, expr, |
| 3520 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3521 | } |
| 3522 | break; |
| 3523 | case AugAssign_kind: |
| 3524 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3525 | case AnnAssign_kind: |
| 3526 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | case For_kind: |
| 3528 | return compiler_for(c, s); |
| 3529 | case While_kind: |
| 3530 | return compiler_while(c, s); |
| 3531 | case If_kind: |
| 3532 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3533 | case Match_kind: |
| 3534 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3535 | case Raise_kind: |
| 3536 | n = 0; |
| 3537 | if (s->v.Raise.exc) { |
| 3538 | VISIT(c, expr, s->v.Raise.exc); |
| 3539 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3540 | if (s->v.Raise.cause) { |
| 3541 | VISIT(c, expr, s->v.Raise.cause); |
| 3542 | n++; |
| 3543 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3545 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3546 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3547 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3548 | case Try_kind: |
| 3549 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3550 | case Assert_kind: |
| 3551 | return compiler_assert(c, s); |
| 3552 | case Import_kind: |
| 3553 | return compiler_import(c, s); |
| 3554 | case ImportFrom_kind: |
| 3555 | return compiler_from_import(c, s); |
| 3556 | case Global_kind: |
| 3557 | case Nonlocal_kind: |
| 3558 | break; |
| 3559 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3560 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3562 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | break; |
| 3564 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3565 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | case Continue_kind: |
| 3567 | return compiler_continue(c); |
| 3568 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3569 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3570 | case AsyncFunctionDef_kind: |
| 3571 | return compiler_function(c, s, 1); |
| 3572 | case AsyncWith_kind: |
| 3573 | return compiler_async_with(c, s, 0); |
| 3574 | case AsyncFor_kind: |
| 3575 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | static int |
| 3582 | unaryop(unaryop_ty op) |
| 3583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3584 | switch (op) { |
| 3585 | case Invert: |
| 3586 | return UNARY_INVERT; |
| 3587 | case Not: |
| 3588 | return UNARY_NOT; |
| 3589 | case UAdd: |
| 3590 | return UNARY_POSITIVE; |
| 3591 | case USub: |
| 3592 | return UNARY_NEGATIVE; |
| 3593 | default: |
| 3594 | PyErr_Format(PyExc_SystemError, |
| 3595 | "unary op %d should not be possible", op); |
| 3596 | return 0; |
| 3597 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3601 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | switch (op) { |
| 3604 | case Add: |
| 3605 | return BINARY_ADD; |
| 3606 | case Sub: |
| 3607 | return BINARY_SUBTRACT; |
| 3608 | case Mult: |
| 3609 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3610 | case MatMult: |
| 3611 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3612 | case Div: |
| 3613 | return BINARY_TRUE_DIVIDE; |
| 3614 | case Mod: |
| 3615 | return BINARY_MODULO; |
| 3616 | case Pow: |
| 3617 | return BINARY_POWER; |
| 3618 | case LShift: |
| 3619 | return BINARY_LSHIFT; |
| 3620 | case RShift: |
| 3621 | return BINARY_RSHIFT; |
| 3622 | case BitOr: |
| 3623 | return BINARY_OR; |
| 3624 | case BitXor: |
| 3625 | return BINARY_XOR; |
| 3626 | case BitAnd: |
| 3627 | return BINARY_AND; |
| 3628 | case FloorDiv: |
| 3629 | return BINARY_FLOOR_DIVIDE; |
| 3630 | default: |
| 3631 | PyErr_Format(PyExc_SystemError, |
| 3632 | "binary op %d should not be possible", op); |
| 3633 | return 0; |
| 3634 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3638 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | switch (op) { |
| 3641 | case Add: |
| 3642 | return INPLACE_ADD; |
| 3643 | case Sub: |
| 3644 | return INPLACE_SUBTRACT; |
| 3645 | case Mult: |
| 3646 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3647 | case MatMult: |
| 3648 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | case Div: |
| 3650 | return INPLACE_TRUE_DIVIDE; |
| 3651 | case Mod: |
| 3652 | return INPLACE_MODULO; |
| 3653 | case Pow: |
| 3654 | return INPLACE_POWER; |
| 3655 | case LShift: |
| 3656 | return INPLACE_LSHIFT; |
| 3657 | case RShift: |
| 3658 | return INPLACE_RSHIFT; |
| 3659 | case BitOr: |
| 3660 | return INPLACE_OR; |
| 3661 | case BitXor: |
| 3662 | return INPLACE_XOR; |
| 3663 | case BitAnd: |
| 3664 | return INPLACE_AND; |
| 3665 | case FloorDiv: |
| 3666 | return INPLACE_FLOOR_DIVIDE; |
| 3667 | default: |
| 3668 | PyErr_Format(PyExc_SystemError, |
| 3669 | "inplace binary op %d should not be possible", op); |
| 3670 | return 0; |
| 3671 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | static int |
| 3675 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3676 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3677 | int op, scope; |
| 3678 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3679 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | PyObject *dict = c->u->u_names; |
| 3682 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3683 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3684 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3685 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3686 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3687 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3688 | if (forbidden_name(c, name, ctx)) |
| 3689 | return 0; |
| 3690 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3691 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3692 | if (!mangled) |
| 3693 | return 0; |
| 3694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3695 | op = 0; |
| 3696 | optype = OP_NAME; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 3697 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | switch (scope) { |
| 3699 | case FREE: |
| 3700 | dict = c->u->u_freevars; |
| 3701 | optype = OP_DEREF; |
| 3702 | break; |
| 3703 | case CELL: |
| 3704 | dict = c->u->u_cellvars; |
| 3705 | optype = OP_DEREF; |
| 3706 | break; |
| 3707 | case LOCAL: |
| 3708 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3709 | optype = OP_FAST; |
| 3710 | break; |
| 3711 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3712 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3713 | optype = OP_GLOBAL; |
| 3714 | break; |
| 3715 | case GLOBAL_EXPLICIT: |
| 3716 | optype = OP_GLOBAL; |
| 3717 | break; |
| 3718 | default: |
| 3719 | /* scope can be 0 */ |
| 3720 | break; |
| 3721 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | /* 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] | 3724 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3726 | switch (optype) { |
| 3727 | case OP_DEREF: |
| 3728 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3729 | case Load: |
| 3730 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3731 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3732 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3733 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | } |
| 3735 | break; |
| 3736 | case OP_FAST: |
| 3737 | switch (ctx) { |
| 3738 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3739 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3740 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3742 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | return 1; |
| 3744 | case OP_GLOBAL: |
| 3745 | switch (ctx) { |
| 3746 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3747 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3748 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | } |
| 3750 | break; |
| 3751 | case OP_NAME: |
| 3752 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3753 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3754 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3756 | } |
| 3757 | break; |
| 3758 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3760 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3761 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3762 | Py_DECREF(mangled); |
| 3763 | if (arg < 0) |
| 3764 | return 0; |
| 3765 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | static int |
| 3769 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3771 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3772 | int jumpi; |
| 3773 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3774 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3776 | assert(e->kind == BoolOp_kind); |
| 3777 | if (e->v.BoolOp.op == And) |
| 3778 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3779 | else |
| 3780 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3781 | end = compiler_new_block(c); |
| 3782 | if (end == NULL) |
| 3783 | return 0; |
| 3784 | s = e->v.BoolOp.values; |
| 3785 | n = asdl_seq_LEN(s) - 1; |
| 3786 | assert(n >= 0); |
| 3787 | for (i = 0; i < n; ++i) { |
| 3788 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3789 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3790 | basicblock *next = compiler_new_block(c); |
| 3791 | if (next == NULL) { |
| 3792 | return 0; |
| 3793 | } |
| 3794 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3795 | } |
| 3796 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3797 | compiler_use_next_block(c, end); |
| 3798 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3802 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3803 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3804 | { |
| 3805 | Py_ssize_t n = asdl_seq_LEN(elts); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3806 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3807 | PyObject *folded = PyTuple_New(n); |
| 3808 | if (folded == NULL) { |
| 3809 | return 0; |
| 3810 | } |
| 3811 | PyObject *val; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3812 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3813 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3814 | Py_INCREF(val); |
| 3815 | PyTuple_SET_ITEM(folded, i, val); |
| 3816 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3817 | if (tuple) { |
| 3818 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3819 | } else { |
| 3820 | if (add == SET_ADD) { |
| 3821 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3822 | if (folded == NULL) { |
| 3823 | return 0; |
| 3824 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3825 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3826 | ADDOP_I(c, build, pushed); |
| 3827 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3828 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3829 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3830 | return 1; |
| 3831 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3832 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3833 | int big = n+pushed > STACK_USE_GUIDELINE; |
| 3834 | int seen_star = 0; |
| 3835 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3836 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3837 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3838 | seen_star = 1; |
| 3839 | } |
| 3840 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3841 | if (!seen_star && !big) { |
| 3842 | for (Py_ssize_t i = 0; i < n; i++) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3843 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3844 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3845 | } |
| 3846 | if (tuple) { |
| 3847 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3848 | } else { |
| 3849 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3850 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3851 | return 1; |
| 3852 | } |
| 3853 | int sequence_built = 0; |
| 3854 | if (big) { |
| 3855 | ADDOP_I(c, build, pushed); |
| 3856 | sequence_built = 1; |
| 3857 | } |
| 3858 | for (Py_ssize_t i = 0; i < n; i++) { |
| 3859 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3860 | if (elt->kind == Starred_kind) { |
| 3861 | if (sequence_built == 0) { |
| 3862 | ADDOP_I(c, build, i+pushed); |
| 3863 | sequence_built = 1; |
| 3864 | } |
| 3865 | VISIT(c, expr, elt->v.Starred.value); |
| 3866 | ADDOP_I(c, extend, 1); |
| 3867 | } |
| 3868 | else { |
| 3869 | VISIT(c, expr, elt); |
| 3870 | if (sequence_built) { |
| 3871 | ADDOP_I(c, add, 1); |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | assert(sequence_built); |
| 3876 | if (tuple) { |
| 3877 | ADDOP(c, LIST_TO_TUPLE); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3878 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3879 | return 1; |
| 3880 | } |
| 3881 | |
| 3882 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3883 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3884 | { |
| 3885 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3886 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3887 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3888 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3889 | if (elt->kind == Starred_kind && !seen_star) { |
| 3890 | if ((i >= (1 << 8)) || |
| 3891 | (n-i-1 >= (INT_MAX >> 8))) |
| 3892 | return compiler_error(c, |
| 3893 | "too many expressions in " |
| 3894 | "star-unpacking assignment"); |
| 3895 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3896 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3897 | } |
| 3898 | else if (elt->kind == Starred_kind) { |
| 3899 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3900 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | if (!seen_star) { |
| 3904 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3905 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3906 | return 1; |
| 3907 | } |
| 3908 | |
| 3909 | static int |
| 3910 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3911 | { |
| 3912 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3913 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3914 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3915 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3916 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3917 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3918 | return 1; |
| 3919 | } |
| 3920 | |
| 3921 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3922 | compiler_list(struct compiler *c, expr_ty e) |
| 3923 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3924 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3925 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3926 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3927 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3928 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3929 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3930 | LIST_APPEND, LIST_EXTEND, 0); |
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 |
| 3933 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3934 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | static int |
| 3938 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3939 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3940 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3941 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3942 | return assignment_helper(c, elts); |
| 3943 | } |
| 3944 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3945 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3946 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3947 | } |
| 3948 | else |
| 3949 | VISIT_SEQ(c, expr, elts); |
| 3950 | return 1; |
| 3951 | } |
| 3952 | |
| 3953 | static int |
| 3954 | compiler_set(struct compiler *c, expr_ty e) |
| 3955 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3956 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3957 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3961 | 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] | 3962 | { |
| 3963 | Py_ssize_t i; |
| 3964 | for (i = begin; i < end; i++) { |
| 3965 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3966 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3967 | return 0; |
| 3968 | } |
| 3969 | return 1; |
| 3970 | } |
| 3971 | |
| 3972 | static int |
| 3973 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3974 | { |
| 3975 | Py_ssize_t i, n = end - begin; |
| 3976 | PyObject *keys, *key; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3977 | int big = n*2 > STACK_USE_GUIDELINE; |
| 3978 | 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] | 3979 | for (i = begin; i < end; i++) { |
| 3980 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3981 | } |
| 3982 | keys = PyTuple_New(n); |
| 3983 | if (keys == NULL) { |
| 3984 | return 0; |
| 3985 | } |
| 3986 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3987 | 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] | 3988 | Py_INCREF(key); |
| 3989 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3990 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3991 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3992 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3993 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3994 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3995 | if (big) { |
| 3996 | ADDOP_I(c, BUILD_MAP, 0); |
| 3997 | } |
| 3998 | for (i = begin; i < end; i++) { |
| 3999 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 4000 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 4001 | if (big) { |
| 4002 | ADDOP_I(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4003 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4004 | } |
| 4005 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4006 | ADDOP_I(c, BUILD_MAP, n); |
| 4007 | } |
| 4008 | return 1; |
| 4009 | } |
| 4010 | |
| 4011 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4012 | compiler_dict(struct compiler *c, expr_ty e) |
| 4013 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4014 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4015 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4016 | int is_unpacking = 0; |
| 4017 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4018 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4019 | elements = 0; |
| 4020 | for (i = 0; i < n; i++) { |
| 4021 | 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] | 4022 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4023 | if (elements) { |
| 4024 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 4025 | return 0; |
| 4026 | } |
| 4027 | if (have_dict) { |
| 4028 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4029 | } |
| 4030 | have_dict = 1; |
| 4031 | elements = 0; |
| 4032 | } |
| 4033 | if (have_dict == 0) { |
| 4034 | ADDOP_I(c, BUILD_MAP, 0); |
| 4035 | have_dict = 1; |
| 4036 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4037 | 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] | 4038 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4039 | } |
| 4040 | else { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4041 | if (elements*2 > STACK_USE_GUIDELINE) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 4042 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4043 | return 0; |
| 4044 | } |
| 4045 | if (have_dict) { |
| 4046 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4047 | } |
| 4048 | have_dict = 1; |
| 4049 | elements = 0; |
| 4050 | } |
| 4051 | else { |
| 4052 | elements++; |
| 4053 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | } |
| 4055 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4056 | if (elements) { |
| 4057 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4058 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4059 | } |
| 4060 | if (have_dict) { |
| 4061 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4062 | } |
| 4063 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4064 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4065 | if (!have_dict) { |
| 4066 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | } |
| 4068 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | static int |
| 4072 | compiler_compare(struct compiler *c, expr_ty e) |
| 4073 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4074 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4075 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 4076 | if (!check_compare(c, e)) { |
| 4077 | return 0; |
| 4078 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4080 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 4081 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 4082 | if (n == 0) { |
| 4083 | 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] | 4084 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4085 | } |
| 4086 | else { |
| 4087 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4088 | if (cleanup == NULL) |
| 4089 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4090 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4091 | VISIT(c, expr, |
| 4092 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4093 | ADDOP(c, DUP_TOP); |
| 4094 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4095 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4096 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4097 | NEXT_BLOCK(c); |
| 4098 | } |
| 4099 | 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] | 4100 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | basicblock *end = compiler_new_block(c); |
| 4102 | if (end == NULL) |
| 4103 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 4104 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | compiler_use_next_block(c, cleanup); |
| 4106 | ADDOP(c, ROT_TWO); |
| 4107 | ADDOP(c, POP_TOP); |
| 4108 | compiler_use_next_block(c, end); |
| 4109 | } |
| 4110 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4113 | static PyTypeObject * |
| 4114 | infer_type(expr_ty e) |
| 4115 | { |
| 4116 | switch (e->kind) { |
| 4117 | case Tuple_kind: |
| 4118 | return &PyTuple_Type; |
| 4119 | case List_kind: |
| 4120 | case ListComp_kind: |
| 4121 | return &PyList_Type; |
| 4122 | case Dict_kind: |
| 4123 | case DictComp_kind: |
| 4124 | return &PyDict_Type; |
| 4125 | case Set_kind: |
| 4126 | case SetComp_kind: |
| 4127 | return &PySet_Type; |
| 4128 | case GeneratorExp_kind: |
| 4129 | return &PyGen_Type; |
| 4130 | case Lambda_kind: |
| 4131 | return &PyFunction_Type; |
| 4132 | case JoinedStr_kind: |
| 4133 | case FormattedValue_kind: |
| 4134 | return &PyUnicode_Type; |
| 4135 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4136 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4137 | default: |
| 4138 | return NULL; |
| 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | static int |
| 4143 | check_caller(struct compiler *c, expr_ty e) |
| 4144 | { |
| 4145 | switch (e->kind) { |
| 4146 | case Constant_kind: |
| 4147 | case Tuple_kind: |
| 4148 | case List_kind: |
| 4149 | case ListComp_kind: |
| 4150 | case Dict_kind: |
| 4151 | case DictComp_kind: |
| 4152 | case Set_kind: |
| 4153 | case SetComp_kind: |
| 4154 | case GeneratorExp_kind: |
| 4155 | case JoinedStr_kind: |
| 4156 | case FormattedValue_kind: |
| 4157 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4158 | "perhaps you missed a comma?", |
| 4159 | infer_type(e)->tp_name); |
| 4160 | default: |
| 4161 | return 1; |
| 4162 | } |
| 4163 | } |
| 4164 | |
| 4165 | static int |
| 4166 | check_subscripter(struct compiler *c, expr_ty e) |
| 4167 | { |
| 4168 | PyObject *v; |
| 4169 | |
| 4170 | switch (e->kind) { |
| 4171 | case Constant_kind: |
| 4172 | v = e->v.Constant.value; |
| 4173 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4174 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4175 | PyAnySet_Check(v))) |
| 4176 | { |
| 4177 | return 1; |
| 4178 | } |
| 4179 | /* fall through */ |
| 4180 | case Set_kind: |
| 4181 | case SetComp_kind: |
| 4182 | case GeneratorExp_kind: |
| 4183 | case Lambda_kind: |
| 4184 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4185 | "perhaps you missed a comma?", |
| 4186 | infer_type(e)->tp_name); |
| 4187 | default: |
| 4188 | return 1; |
| 4189 | } |
| 4190 | } |
| 4191 | |
| 4192 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4193 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4194 | { |
| 4195 | PyObject *v; |
| 4196 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4197 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4198 | if (index_type == NULL |
| 4199 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4200 | || index_type == &PySlice_Type) { |
| 4201 | return 1; |
| 4202 | } |
| 4203 | |
| 4204 | switch (e->kind) { |
| 4205 | case Constant_kind: |
| 4206 | v = e->v.Constant.value; |
| 4207 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4208 | return 1; |
| 4209 | } |
| 4210 | /* fall through */ |
| 4211 | case Tuple_kind: |
| 4212 | case List_kind: |
| 4213 | case ListComp_kind: |
| 4214 | case JoinedStr_kind: |
| 4215 | case FormattedValue_kind: |
| 4216 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4217 | "not %.200s; " |
| 4218 | "perhaps you missed a comma?", |
| 4219 | infer_type(e)->tp_name, |
| 4220 | index_type->tp_name); |
| 4221 | default: |
| 4222 | return 1; |
| 4223 | } |
| 4224 | } |
| 4225 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4226 | // 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] | 4227 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4228 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4229 | { |
| 4230 | Py_ssize_t argsl, i; |
| 4231 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4232 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4233 | |
| 4234 | /* Check that the call node is an attribute access, and that |
| 4235 | the call doesn't have keyword parameters. */ |
| 4236 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4237 | asdl_seq_LEN(e->v.Call.keywords)) { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4238 | return -1; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4239 | } |
| 4240 | /* Check that there aren't too many arguments */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4241 | argsl = asdl_seq_LEN(args); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4242 | if (argsl >= STACK_USE_GUIDELINE) { |
| 4243 | return -1; |
| 4244 | } |
| 4245 | /* Check that there are no *varargs types of arguments. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4246 | for (i = 0; i < argsl; i++) { |
| 4247 | expr_ty elt = asdl_seq_GET(args, i); |
| 4248 | if (elt->kind == Starred_kind) { |
| 4249 | return -1; |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | /* Alright, we can optimize the code. */ |
| 4254 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4255 | int old_lineno = c->u->u_lineno; |
| 4256 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4257 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4258 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4259 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4260 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4261 | return 1; |
| 4262 | } |
| 4263 | |
| 4264 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4265 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4266 | { |
| 4267 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4268 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4269 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4270 | if (key->arg == NULL) { |
| 4271 | continue; |
| 4272 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4273 | if (forbidden_name(c, key->arg, Store)) { |
| 4274 | return -1; |
| 4275 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4276 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4277 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4278 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 4279 | SET_LOC(c, other); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4280 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4281 | return -1; |
| 4282 | } |
| 4283 | } |
| 4284 | } |
| 4285 | return 0; |
| 4286 | } |
| 4287 | |
| 4288 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4289 | compiler_call(struct compiler *c, expr_ty e) |
| 4290 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4291 | int ret = maybe_optimize_method_call(c, e); |
| 4292 | if (ret >= 0) { |
| 4293 | return ret; |
| 4294 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4295 | if (!check_caller(c, e->v.Call.func)) { |
| 4296 | return 0; |
| 4297 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | VISIT(c, expr, e->v.Call.func); |
| 4299 | return compiler_call_helper(c, 0, |
| 4300 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4301 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4304 | static int |
| 4305 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4306 | { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4307 | |
| 4308 | Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); |
| 4309 | if (value_count > STACK_USE_GUIDELINE) { |
| 4310 | ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0)); |
| 4311 | PyObject *join = _PyUnicode_FromASCII("join", 4); |
| 4312 | if (join == NULL) { |
| 4313 | return 0; |
| 4314 | } |
| 4315 | ADDOP_NAME(c, LOAD_METHOD, join, names); |
| 4316 | Py_DECREF(join); |
| 4317 | ADDOP_I(c, BUILD_LIST, 0); |
| 4318 | for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { |
| 4319 | VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); |
| 4320 | ADDOP_I(c, LIST_APPEND, 1); |
| 4321 | } |
| 4322 | ADDOP_I(c, CALL_METHOD, 1); |
| 4323 | } |
| 4324 | else { |
| 4325 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
| 4326 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { |
| 4327 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
| 4328 | } |
| 4329 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4330 | return 1; |
| 4331 | } |
| 4332 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4333 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4334 | static int |
| 4335 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4336 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4337 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4338 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4339 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4340 | Convert the conversion char to 3 bits: |
| 4341 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4342 | !s : 001 0x1 FVC_STR |
| 4343 | !r : 010 0x2 FVC_REPR |
| 4344 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4345 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4346 | next bit is whether or not we have a format spec: |
| 4347 | yes : 100 0x4 |
| 4348 | no : 000 0x0 |
| 4349 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4350 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4351 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4352 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4353 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4354 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4355 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4356 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4357 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4358 | case 's': oparg = FVC_STR; break; |
| 4359 | case 'r': oparg = FVC_REPR; break; |
| 4360 | case 'a': oparg = FVC_ASCII; break; |
| 4361 | case -1: oparg = FVC_NONE; break; |
| 4362 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4363 | PyErr_Format(PyExc_SystemError, |
| 4364 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4365 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4366 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4367 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4368 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4369 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4370 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4371 | } |
| 4372 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4373 | /* And push our opcode and oparg */ |
| 4374 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4375 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4376 | return 1; |
| 4377 | } |
| 4378 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4379 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4380 | 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] | 4381 | { |
| 4382 | Py_ssize_t i, n = end - begin; |
| 4383 | keyword_ty kw; |
| 4384 | PyObject *keys, *key; |
| 4385 | assert(n > 0); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4386 | int big = n*2 > STACK_USE_GUIDELINE; |
| 4387 | if (n > 1 && !big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4388 | for (i = begin; i < end; i++) { |
| 4389 | kw = asdl_seq_GET(keywords, i); |
| 4390 | VISIT(c, expr, kw->value); |
| 4391 | } |
| 4392 | keys = PyTuple_New(n); |
| 4393 | if (keys == NULL) { |
| 4394 | return 0; |
| 4395 | } |
| 4396 | for (i = begin; i < end; i++) { |
| 4397 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4398 | Py_INCREF(key); |
| 4399 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4400 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4401 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4402 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4403 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4404 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4405 | if (big) { |
| 4406 | ADDOP_I_NOLINE(c, BUILD_MAP, 0); |
| 4407 | } |
| 4408 | for (i = begin; i < end; i++) { |
| 4409 | kw = asdl_seq_GET(keywords, i); |
| 4410 | ADDOP_LOAD_CONST(c, kw->arg); |
| 4411 | VISIT(c, expr, kw->value); |
| 4412 | if (big) { |
| 4413 | ADDOP_I_NOLINE(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4414 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4415 | } |
| 4416 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4417 | ADDOP_I(c, BUILD_MAP, n); |
| 4418 | } |
| 4419 | return 1; |
| 4420 | } |
| 4421 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4422 | /* shared code between compiler_call and compiler_class */ |
| 4423 | static int |
| 4424 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4425 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4426 | asdl_expr_seq *args, |
| 4427 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4428 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4429 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4430 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4431 | if (validate_keywords(c, keywords) == -1) { |
| 4432 | return 0; |
| 4433 | } |
| 4434 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4435 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4436 | nkwelts = asdl_seq_LEN(keywords); |
| 4437 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4438 | if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) { |
| 4439 | goto ex_call; |
| 4440 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4441 | for (i = 0; i < nelts; i++) { |
| 4442 | expr_ty elt = asdl_seq_GET(args, i); |
| 4443 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4444 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4445 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4446 | } |
| 4447 | for (i = 0; i < nkwelts; i++) { |
| 4448 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4449 | if (kw->arg == NULL) { |
| 4450 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4451 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4453 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4454 | /* No * or ** args, so can use faster calling sequence */ |
| 4455 | for (i = 0; i < nelts; i++) { |
| 4456 | expr_ty elt = asdl_seq_GET(args, i); |
| 4457 | assert(elt->kind != Starred_kind); |
| 4458 | VISIT(c, expr, elt); |
| 4459 | } |
| 4460 | if (nkwelts) { |
| 4461 | PyObject *names; |
| 4462 | VISIT_SEQ(c, keyword, keywords); |
| 4463 | names = PyTuple_New(nkwelts); |
| 4464 | if (names == NULL) { |
| 4465 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4466 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4467 | for (i = 0; i < nkwelts; i++) { |
| 4468 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4469 | Py_INCREF(kw->arg); |
| 4470 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4471 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4472 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4473 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4474 | return 1; |
| 4475 | } |
| 4476 | else { |
| 4477 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4478 | return 1; |
| 4479 | } |
| 4480 | |
| 4481 | ex_call: |
| 4482 | |
| 4483 | /* Do positional arguments. */ |
| 4484 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4485 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4486 | } |
| 4487 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4488 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4489 | return 0; |
| 4490 | } |
| 4491 | /* Then keyword arguments */ |
| 4492 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4493 | /* Has a new dict been pushed */ |
| 4494 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4495 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4496 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4497 | for (i = 0; i < nkwelts; i++) { |
| 4498 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4499 | if (kw->arg == NULL) { |
| 4500 | /* A keyword argument unpacking. */ |
| 4501 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4502 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4503 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4504 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4505 | if (have_dict) { |
| 4506 | ADDOP_I(c, DICT_MERGE, 1); |
| 4507 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4508 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4509 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4510 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4511 | if (!have_dict) { |
| 4512 | ADDOP_I(c, BUILD_MAP, 0); |
| 4513 | have_dict = 1; |
| 4514 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4515 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4516 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4517 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4518 | else { |
| 4519 | nseen++; |
| 4520 | } |
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 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4523 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4524 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4525 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4526 | } |
| 4527 | if (have_dict) { |
| 4528 | ADDOP_I(c, DICT_MERGE, 1); |
| 4529 | } |
| 4530 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4531 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4532 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4534 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4535 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4538 | |
| 4539 | /* List and set comprehensions and generator expressions work by creating a |
| 4540 | nested function to perform the actual iteration. This means that the |
| 4541 | iteration variables don't leak into the current scope. |
| 4542 | The defined function is called immediately following its definition, with the |
| 4543 | result of that call being the result of the expression. |
| 4544 | The LC/SC version returns the populated container, while the GE version is |
| 4545 | flagged in symtable.c as a generator, so it returns the generator object |
| 4546 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4547 | |
| 4548 | Possible cleanups: |
| 4549 | - iterate over the generator sequence instead of using recursion |
| 4550 | */ |
| 4551 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4552 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4553 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4554 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4555 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4556 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4558 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | comprehension_ty gen; |
| 4560 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4561 | if (gen->is_async) { |
| 4562 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4563 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4564 | } else { |
| 4565 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4566 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | static int |
| 4571 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4572 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4573 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4574 | expr_ty elt, expr_ty val, int type) |
| 4575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | /* generate code for the iterator, then each of the ifs, |
| 4577 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4579 | comprehension_ty gen; |
| 4580 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4581 | Py_ssize_t i, n; |
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 | start = compiler_new_block(c); |
| 4584 | skip = compiler_new_block(c); |
| 4585 | if_cleanup = compiler_new_block(c); |
| 4586 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4589 | anchor == NULL) |
| 4590 | return 0; |
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 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4594 | if (gen_index == 0) { |
| 4595 | /* Receive outermost iter as an implicit argument */ |
| 4596 | c->u->u_argcount = 1; |
| 4597 | ADDOP_I(c, LOAD_FAST, 0); |
| 4598 | } |
| 4599 | else { |
| 4600 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4601 | /* Fast path for the temporary variable assignment idiom: |
| 4602 | for y in [f(x)] |
| 4603 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4604 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4605 | switch (gen->iter->kind) { |
| 4606 | case List_kind: |
| 4607 | elts = gen->iter->v.List.elts; |
| 4608 | break; |
| 4609 | case Tuple_kind: |
| 4610 | elts = gen->iter->v.Tuple.elts; |
| 4611 | break; |
| 4612 | default: |
| 4613 | elts = NULL; |
| 4614 | } |
| 4615 | if (asdl_seq_LEN(elts) == 1) { |
| 4616 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4617 | if (elt->kind != Starred_kind) { |
| 4618 | VISIT(c, expr, elt); |
| 4619 | start = NULL; |
| 4620 | } |
| 4621 | } |
| 4622 | if (start) { |
| 4623 | VISIT(c, expr, gen->iter); |
| 4624 | ADDOP(c, GET_ITER); |
| 4625 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4627 | if (start) { |
| 4628 | depth++; |
| 4629 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4630 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4631 | NEXT_BLOCK(c); |
| 4632 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | /* XXX this needs to be cleaned up...a lot! */ |
| 4636 | n = asdl_seq_LEN(gen->ifs); |
| 4637 | for (i = 0; i < n; i++) { |
| 4638 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4639 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4640 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | NEXT_BLOCK(c); |
| 4642 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4644 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4645 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4646 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4647 | elt, val, type)) |
| 4648 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | /* only append after the last for generator */ |
| 4651 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4652 | /* comprehension specific code */ |
| 4653 | switch (type) { |
| 4654 | case COMP_GENEXP: |
| 4655 | VISIT(c, expr, elt); |
| 4656 | ADDOP(c, YIELD_VALUE); |
| 4657 | ADDOP(c, POP_TOP); |
| 4658 | break; |
| 4659 | case COMP_LISTCOMP: |
| 4660 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4661 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4662 | break; |
| 4663 | case COMP_SETCOMP: |
| 4664 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4665 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | break; |
| 4667 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4668 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4671 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4672 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | break; |
| 4674 | default: |
| 4675 | return 0; |
| 4676 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | compiler_use_next_block(c, skip); |
| 4679 | } |
| 4680 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4681 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4682 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4683 | compiler_use_next_block(c, anchor); |
| 4684 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4685 | |
| 4686 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4690 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4691 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4692 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4693 | expr_ty elt, expr_ty val, int type) |
| 4694 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4695 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4696 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4697 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4698 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4699 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4700 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4701 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4702 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4703 | return 0; |
| 4704 | } |
| 4705 | |
| 4706 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4707 | |
| 4708 | if (gen_index == 0) { |
| 4709 | /* Receive outermost iter as an implicit argument */ |
| 4710 | c->u->u_argcount = 1; |
| 4711 | ADDOP_I(c, LOAD_FAST, 0); |
| 4712 | } |
| 4713 | else { |
| 4714 | /* Sub-iter - calculate on the fly */ |
| 4715 | VISIT(c, expr, gen->iter); |
| 4716 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4717 | } |
| 4718 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4719 | compiler_use_next_block(c, start); |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4720 | /* Runtime will push a block here, so we need to account for that */ |
| 4721 | if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start, |
| 4722 | NULL, NULL)) { |
| 4723 | return 0; |
| 4724 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4725 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4726 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4727 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4728 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4729 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4730 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4731 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4732 | |
| 4733 | n = asdl_seq_LEN(gen->ifs); |
| 4734 | for (i = 0; i < n; i++) { |
| 4735 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4736 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4737 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4738 | NEXT_BLOCK(c); |
| 4739 | } |
| 4740 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4741 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4742 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4743 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4744 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4745 | elt, val, type)) |
| 4746 | return 0; |
| 4747 | |
| 4748 | /* only append after the last for generator */ |
| 4749 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4750 | /* comprehension specific code */ |
| 4751 | switch (type) { |
| 4752 | case COMP_GENEXP: |
| 4753 | VISIT(c, expr, elt); |
| 4754 | ADDOP(c, YIELD_VALUE); |
| 4755 | ADDOP(c, POP_TOP); |
| 4756 | break; |
| 4757 | case COMP_LISTCOMP: |
| 4758 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4759 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4760 | break; |
| 4761 | case COMP_SETCOMP: |
| 4762 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4763 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4764 | break; |
| 4765 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4766 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4767 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4768 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4769 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4770 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4771 | break; |
| 4772 | default: |
| 4773 | return 0; |
| 4774 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4775 | } |
| 4776 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4777 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4778 | |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4779 | compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); |
| 4780 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4781 | compiler_use_next_block(c, except); |
| 4782 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4783 | |
| 4784 | return 1; |
| 4785 | } |
| 4786 | |
| 4787 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4788 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4789 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4790 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4793 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4794 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4795 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4796 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4797 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4798 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4799 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4800 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4801 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4802 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4803 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4806 | } |
Mark Shannon | 7674c83 | 2021-06-21 11:47:16 +0100 | [diff] [blame] | 4807 | SET_LOC(c, e); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4808 | |
| 4809 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4810 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4811 | 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] | 4812 | compiler_error(c, "asynchronous comprehension outside of " |
| 4813 | "an asynchronous function"); |
| 4814 | goto error_in_scope; |
| 4815 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4817 | if (type != COMP_GENEXP) { |
| 4818 | int op; |
| 4819 | switch (type) { |
| 4820 | case COMP_LISTCOMP: |
| 4821 | op = BUILD_LIST; |
| 4822 | break; |
| 4823 | case COMP_SETCOMP: |
| 4824 | op = BUILD_SET; |
| 4825 | break; |
| 4826 | case COMP_DICTCOMP: |
| 4827 | op = BUILD_MAP; |
| 4828 | break; |
| 4829 | default: |
| 4830 | PyErr_Format(PyExc_SystemError, |
| 4831 | "unknown comprehension type %d", type); |
| 4832 | goto error_in_scope; |
| 4833 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4835 | ADDOP_I(c, op, 0); |
| 4836 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4837 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4838 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | val, type)) |
| 4840 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4842 | if (type != COMP_GENEXP) { |
| 4843 | ADDOP(c, RETURN_VALUE); |
| 4844 | } |
| 4845 | |
| 4846 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4847 | qualname = c->u->u_qualname; |
| 4848 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4849 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4850 | if (top_level_await && is_async_generator){ |
| 4851 | c->u->u_ste->ste_coroutine = 1; |
| 4852 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4853 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4854 | goto error; |
| 4855 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4856 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4858 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4859 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4860 | Py_DECREF(co); |
| 4861 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4862 | VISIT(c, expr, outermost->iter); |
| 4863 | |
| 4864 | if (outermost->is_async) { |
| 4865 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4866 | } else { |
| 4867 | ADDOP(c, GET_ITER); |
| 4868 | } |
| 4869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4871 | |
| 4872 | if (is_async_generator && type != COMP_GENEXP) { |
| 4873 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4874 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4875 | ADDOP(c, YIELD_FROM); |
| 4876 | } |
| 4877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4879 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4880 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4881 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4882 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4883 | Py_XDECREF(co); |
| 4884 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4888 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4890 | static identifier name; |
| 4891 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4892 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4893 | if (!name) |
| 4894 | return 0; |
| 4895 | } |
| 4896 | assert(e->kind == GeneratorExp_kind); |
| 4897 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4898 | e->v.GeneratorExp.generators, |
| 4899 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4903 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4904 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4905 | static identifier name; |
| 4906 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4907 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4908 | if (!name) |
| 4909 | return 0; |
| 4910 | } |
| 4911 | assert(e->kind == ListComp_kind); |
| 4912 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4913 | e->v.ListComp.generators, |
| 4914 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | static int |
| 4918 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4919 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4920 | static identifier name; |
| 4921 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4922 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4923 | if (!name) |
| 4924 | return 0; |
| 4925 | } |
| 4926 | assert(e->kind == SetComp_kind); |
| 4927 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4928 | e->v.SetComp.generators, |
| 4929 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | |
| 4933 | static int |
| 4934 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4935 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4936 | static identifier name; |
| 4937 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4938 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | if (!name) |
| 4940 | return 0; |
| 4941 | } |
| 4942 | assert(e->kind == DictComp_kind); |
| 4943 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4944 | e->v.DictComp.generators, |
| 4945 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | |
| 4949 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4950 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4952 | VISIT(c, expr, k->value); |
| 4953 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4956 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4957 | whether they are true or false. |
| 4958 | |
| 4959 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4960 | */ |
| 4961 | |
| 4962 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4963 | compiler_with_except_finish(struct compiler *c) { |
| 4964 | basicblock *exit; |
| 4965 | exit = compiler_new_block(c); |
| 4966 | if (exit == NULL) |
| 4967 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4968 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4969 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4970 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4971 | compiler_use_next_block(c, exit); |
| 4972 | ADDOP(c, POP_TOP); |
| 4973 | ADDOP(c, POP_TOP); |
| 4974 | ADDOP(c, POP_TOP); |
| 4975 | ADDOP(c, POP_EXCEPT); |
| 4976 | ADDOP(c, POP_TOP); |
| 4977 | return 1; |
| 4978 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4979 | |
| 4980 | /* |
| 4981 | Implements the async with statement. |
| 4982 | |
| 4983 | The semantics outlined in that PEP are as follows: |
| 4984 | |
| 4985 | async with EXPR as VAR: |
| 4986 | BLOCK |
| 4987 | |
| 4988 | It is implemented roughly as: |
| 4989 | |
| 4990 | context = EXPR |
| 4991 | exit = context.__aexit__ # not calling it |
| 4992 | value = await context.__aenter__() |
| 4993 | try: |
| 4994 | VAR = value # if VAR present in the syntax |
| 4995 | BLOCK |
| 4996 | finally: |
| 4997 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4998 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4999 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 5000 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5001 | if not (await exit(*exc)): |
| 5002 | raise |
| 5003 | */ |
| 5004 | static int |
| 5005 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 5006 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5007 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5008 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 5009 | |
| 5010 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5011 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5012 | c->u->u_ste->ste_coroutine = 1; |
| 5013 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 5014 | return compiler_error(c, "'async with' outside async function"); |
| 5015 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5016 | |
| 5017 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5018 | final = compiler_new_block(c); |
| 5019 | exit = compiler_new_block(c); |
| 5020 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5021 | return 0; |
| 5022 | |
| 5023 | /* Evaluate EXPR */ |
| 5024 | VISIT(c, expr, item->context_expr); |
| 5025 | |
| 5026 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 5027 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5028 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5029 | ADDOP(c, YIELD_FROM); |
| 5030 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5031 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5032 | |
| 5033 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 5034 | compiler_use_next_block(c, block); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5035 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5036 | return 0; |
| 5037 | } |
| 5038 | |
| 5039 | if (item->optional_vars) { |
| 5040 | VISIT(c, expr, item->optional_vars); |
| 5041 | } |
| 5042 | else { |
| 5043 | /* Discard result from context.__aenter__() */ |
| 5044 | ADDOP(c, POP_TOP); |
| 5045 | } |
| 5046 | |
| 5047 | pos++; |
| 5048 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 5049 | /* BLOCK code */ |
| 5050 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 5051 | else if (!compiler_async_with(c, s, pos)) |
| 5052 | return 0; |
| 5053 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5054 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5055 | ADDOP(c, POP_BLOCK); |
| 5056 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5057 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5058 | /* For successful outcome: |
| 5059 | * call __exit__(None, None, None) |
| 5060 | */ |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5061 | SET_LOC(c, s); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5062 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5063 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5064 | ADDOP(c, GET_AWAITABLE); |
| 5065 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 5066 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5067 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5068 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5069 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5070 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5071 | |
| 5072 | /* For exceptional outcome: */ |
| 5073 | compiler_use_next_block(c, final); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5074 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5075 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5076 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5077 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5078 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5079 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5080 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5081 | return 1; |
| 5082 | } |
| 5083 | |
| 5084 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5085 | /* |
| 5086 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5087 | with EXPR as VAR: |
| 5088 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5089 | is implemented as: |
| 5090 | <code for EXPR> |
| 5091 | SETUP_WITH E |
| 5092 | <code to store to VAR> or POP_TOP |
| 5093 | <code for BLOCK> |
| 5094 | LOAD_CONST (None, None, None) |
| 5095 | CALL_FUNCTION_EX 0 |
| 5096 | JUMP_FORWARD EXIT |
| 5097 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 5098 | POP_JUMP_IF_TRUE T: |
| 5099 | RERAISE |
| 5100 | T: POP_TOP * 3 (remove exception from stack) |
| 5101 | POP_EXCEPT |
| 5102 | POP_TOP |
| 5103 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5104 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5105 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5106 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5107 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
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 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5110 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5111 | |
| 5112 | assert(s->kind == With_kind); |
| 5113 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5114 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5115 | final = compiler_new_block(c); |
| 5116 | exit = compiler_new_block(c); |
| 5117 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5118 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5119 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5120 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5121 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5122 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5123 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5124 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5125 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5126 | compiler_use_next_block(c, block); |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5127 | if (!compiler_push_fblock(c, WITH, block, final, s)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5128 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5131 | if (item->optional_vars) { |
| 5132 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5133 | } |
| 5134 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5135 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5136 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5139 | pos++; |
| 5140 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 5141 | /* BLOCK code */ |
| 5142 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 5143 | else if (!compiler_with(c, s, pos)) |
| 5144 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5145 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 5146 | |
| 5147 | /* Mark all following code as artificial */ |
| 5148 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5149 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5150 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5151 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5152 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5153 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5154 | /* For successful outcome: |
| 5155 | * call __exit__(None, None, None) |
| 5156 | */ |
Mark Shannon | 5979e81 | 2021-04-30 14:32:47 +0100 | [diff] [blame] | 5157 | SET_LOC(c, s); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5158 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5159 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5160 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5161 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5162 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5163 | /* For exceptional outcome: */ |
| 5164 | compiler_use_next_block(c, final); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5165 | ADDOP(c, WITH_EXCEPT_START); |
| 5166 | compiler_with_except_finish(c); |
| 5167 | |
| 5168 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5169 | return 1; |
| 5170 | } |
| 5171 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5172 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5173 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5176 | case NamedExpr_kind: |
| 5177 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5178 | ADDOP(c, DUP_TOP); |
| 5179 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5180 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5181 | case BoolOp_kind: |
| 5182 | return compiler_boolop(c, e); |
| 5183 | case BinOp_kind: |
| 5184 | VISIT(c, expr, e->v.BinOp.left); |
| 5185 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5186 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5187 | break; |
| 5188 | case UnaryOp_kind: |
| 5189 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5190 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5191 | break; |
| 5192 | case Lambda_kind: |
| 5193 | return compiler_lambda(c, e); |
| 5194 | case IfExp_kind: |
| 5195 | return compiler_ifexp(c, e); |
| 5196 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5197 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5199 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5200 | case GeneratorExp_kind: |
| 5201 | return compiler_genexp(c, e); |
| 5202 | case ListComp_kind: |
| 5203 | return compiler_listcomp(c, e); |
| 5204 | case SetComp_kind: |
| 5205 | return compiler_setcomp(c, e); |
| 5206 | case DictComp_kind: |
| 5207 | return compiler_dictcomp(c, e); |
| 5208 | case Yield_kind: |
| 5209 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5210 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5211 | if (e->v.Yield.value) { |
| 5212 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5213 | } |
| 5214 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5215 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5216 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5217 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5219 | case YieldFrom_kind: |
| 5220 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5221 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5222 | |
| 5223 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5224 | return compiler_error(c, "'yield from' inside async function"); |
| 5225 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5226 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5227 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5228 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5229 | ADDOP(c, YIELD_FROM); |
| 5230 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5231 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5232 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5233 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5234 | return compiler_error(c, "'await' outside function"); |
| 5235 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5236 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5237 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5238 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5239 | return compiler_error(c, "'await' outside async function"); |
| 5240 | } |
| 5241 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5242 | |
| 5243 | VISIT(c, expr, e->v.Await.value); |
| 5244 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5245 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5246 | ADDOP(c, YIELD_FROM); |
| 5247 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5248 | case Compare_kind: |
| 5249 | return compiler_compare(c, e); |
| 5250 | case Call_kind: |
| 5251 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5252 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5253 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5254 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5255 | case JoinedStr_kind: |
| 5256 | return compiler_joined_str(c, e); |
| 5257 | case FormattedValue_kind: |
| 5258 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | /* The following exprs can be assignment targets. */ |
| 5260 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5261 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5263 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5264 | { |
| 5265 | int old_lineno = c->u->u_lineno; |
| 5266 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5268 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5269 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5270 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5272 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5273 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5274 | } |
| 5275 | int old_lineno = c->u->u_lineno; |
| 5276 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5277 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5278 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | break; |
| 5280 | case Del: |
| 5281 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5282 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | } |
| 5284 | break; |
| 5285 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5286 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | case Starred_kind: |
| 5288 | switch (e->v.Starred.ctx) { |
| 5289 | case Store: |
| 5290 | /* In all legitimate cases, the Starred node was already replaced |
| 5291 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5292 | return compiler_error(c, |
| 5293 | "starred assignment target must be in a list or tuple"); |
| 5294 | default: |
| 5295 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5296 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5297 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5298 | break; |
| 5299 | case Slice_kind: |
| 5300 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | case Name_kind: |
| 5302 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5303 | /* child nodes of List and Tuple will have expr_context set */ |
| 5304 | case List_kind: |
| 5305 | return compiler_list(c, e); |
| 5306 | case Tuple_kind: |
| 5307 | return compiler_tuple(c, e); |
| 5308 | } |
| 5309 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5313 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5314 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5315 | int old_lineno = c->u->u_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5316 | int old_end_lineno = c->u->u_end_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5317 | int old_col_offset = c->u->u_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5318 | int old_end_col_offset = c->u->u_end_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5319 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5320 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5321 | c->u->u_lineno = old_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5322 | c->u->u_end_lineno = old_end_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5323 | c->u->u_col_offset = old_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5324 | c->u->u_end_col_offset = old_end_col_offset; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5325 | return res; |
| 5326 | } |
| 5327 | |
| 5328 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5329 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5331 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5332 | expr_ty e = s->v.AugAssign.target; |
| 5333 | |
| 5334 | int old_lineno = c->u->u_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5335 | int old_end_lineno = c->u->u_end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5336 | int old_col_offset = c->u->u_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5337 | int old_end_col_offset = c->u->u_end_col_offset; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5338 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5340 | switch (e->kind) { |
| 5341 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5342 | VISIT(c, expr, e->v.Attribute.value); |
| 5343 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5344 | int old_lineno = c->u->u_lineno; |
| 5345 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5346 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5347 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5348 | break; |
| 5349 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5350 | VISIT(c, expr, e->v.Subscript.value); |
| 5351 | VISIT(c, expr, e->v.Subscript.slice); |
| 5352 | ADDOP(c, DUP_TOP_TWO); |
| 5353 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5354 | break; |
| 5355 | case Name_kind: |
| 5356 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5357 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5358 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5359 | default: |
| 5360 | PyErr_Format(PyExc_SystemError, |
| 5361 | "invalid node type (%d) for augmented assignment", |
| 5362 | e->kind); |
| 5363 | return 0; |
| 5364 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5365 | |
| 5366 | c->u->u_lineno = old_lineno; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5367 | c->u->u_end_lineno = old_end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5368 | c->u->u_col_offset = old_col_offset; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5369 | c->u->u_end_col_offset = old_end_col_offset; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5370 | |
| 5371 | VISIT(c, expr, s->v.AugAssign.value); |
| 5372 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5373 | |
| 5374 | SET_LOC(c, e); |
| 5375 | |
| 5376 | switch (e->kind) { |
| 5377 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5378 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5379 | ADDOP(c, ROT_TWO); |
| 5380 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5381 | break; |
| 5382 | case Subscript_kind: |
| 5383 | ADDOP(c, ROT_THREE); |
| 5384 | ADDOP(c, STORE_SUBSCR); |
| 5385 | break; |
| 5386 | case Name_kind: |
| 5387 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5388 | default: |
| 5389 | Py_UNREACHABLE(); |
| 5390 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5391 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5395 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5396 | { |
| 5397 | VISIT(c, expr, e); |
| 5398 | ADDOP(c, POP_TOP); |
| 5399 | return 1; |
| 5400 | } |
| 5401 | |
| 5402 | static int |
| 5403 | check_annotation(struct compiler *c, stmt_ty s) |
| 5404 | { |
Batuhan Taskaya | 8cc3cfa | 2021-04-25 05:31:20 +0300 | [diff] [blame] | 5405 | /* Annotations of complex targets does not produce anything |
| 5406 | under annotations future */ |
| 5407 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5408 | return 1; |
| 5409 | } |
| 5410 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5411 | /* Annotations are only evaluated in a module or class. */ |
| 5412 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5413 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5414 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5415 | } |
| 5416 | return 1; |
| 5417 | } |
| 5418 | |
| 5419 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5420 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5421 | { |
| 5422 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5423 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5424 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5425 | 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] | 5426 | return 0; |
| 5427 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5428 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5429 | return 0; |
| 5430 | } |
| 5431 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5432 | return 0; |
| 5433 | } |
| 5434 | return 1; |
| 5435 | case Tuple_kind: { |
| 5436 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5437 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5438 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5439 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5440 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5441 | return 0; |
| 5442 | } |
| 5443 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5444 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5445 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5446 | default: |
| 5447 | return check_ann_expr(c, e); |
| 5448 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | static int |
| 5452 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5453 | { |
| 5454 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5455 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5456 | |
| 5457 | assert(s->kind == AnnAssign_kind); |
| 5458 | |
| 5459 | /* We perform the actual assignment first. */ |
| 5460 | if (s->v.AnnAssign.value) { |
| 5461 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5462 | VISIT(c, expr, targ); |
| 5463 | } |
| 5464 | switch (targ->kind) { |
| 5465 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5466 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5467 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5468 | /* If we have a simple name in a module or class, store annotation. */ |
| 5469 | if (s->v.AnnAssign.simple && |
| 5470 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5471 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 5472 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5473 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5474 | } |
| 5475 | else { |
| 5476 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5477 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5478 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5479 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5480 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5481 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5482 | } |
| 5483 | break; |
| 5484 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5485 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5486 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5487 | if (!s->v.AnnAssign.value && |
| 5488 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5489 | return 0; |
| 5490 | } |
| 5491 | break; |
| 5492 | case Subscript_kind: |
| 5493 | if (!s->v.AnnAssign.value && |
| 5494 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5495 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5496 | return 0; |
| 5497 | } |
| 5498 | break; |
| 5499 | default: |
| 5500 | PyErr_Format(PyExc_SystemError, |
| 5501 | "invalid node type (%d) for annotated assignment", |
| 5502 | targ->kind); |
| 5503 | return 0; |
| 5504 | } |
| 5505 | /* Annotation is evaluated last. */ |
| 5506 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5507 | return 0; |
| 5508 | } |
| 5509 | return 1; |
| 5510 | } |
| 5511 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5512 | /* Raises a SyntaxError and returns 0. |
| 5513 | If something goes wrong, a different exception may be raised. |
| 5514 | */ |
| 5515 | |
| 5516 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5517 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5518 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5519 | va_list vargs; |
| 5520 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5521 | va_start(vargs, format); |
| 5522 | #else |
| 5523 | va_start(vargs); |
| 5524 | #endif |
| 5525 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5526 | va_end(vargs); |
| 5527 | if (msg == NULL) { |
| 5528 | return 0; |
| 5529 | } |
| 5530 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5531 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5532 | Py_INCREF(Py_None); |
| 5533 | loc = Py_None; |
| 5534 | } |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 5535 | PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename, |
| 5536 | c->u->u_lineno, c->u->u_col_offset + 1, loc, |
| 5537 | c->u->u_end_lineno, c->u->u_end_col_offset + 1); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5538 | Py_DECREF(msg); |
| 5539 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5540 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5541 | } |
| 5542 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5543 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5545 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5546 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5549 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5550 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5551 | and returns 0. |
| 5552 | */ |
| 5553 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5554 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5555 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5556 | va_list vargs; |
| 5557 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5558 | va_start(vargs, format); |
| 5559 | #else |
| 5560 | va_start(vargs); |
| 5561 | #endif |
| 5562 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5563 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5564 | if (msg == NULL) { |
| 5565 | return 0; |
| 5566 | } |
| 5567 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5568 | c->u->u_lineno, NULL, NULL) < 0) |
| 5569 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5570 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5571 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5572 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5573 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5574 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5575 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5576 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5577 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5578 | return 0; |
| 5579 | } |
| 5580 | Py_DECREF(msg); |
| 5581 | return 1; |
| 5582 | } |
| 5583 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5584 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5585 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5586 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5587 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5589 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5590 | if (ctx == Load) { |
| 5591 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5592 | return 0; |
| 5593 | } |
| 5594 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5595 | return 0; |
| 5596 | } |
| 5597 | } |
| 5598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5600 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5601 | case Store: op = STORE_SUBSCR; break; |
| 5602 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5604 | assert(op); |
| 5605 | VISIT(c, expr, e->v.Subscript.value); |
| 5606 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5607 | ADDOP(c, op); |
| 5608 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5612 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5614 | int n = 2; |
| 5615 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5617 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5618 | if (s->v.Slice.lower) { |
| 5619 | VISIT(c, expr, s->v.Slice.lower); |
| 5620 | } |
| 5621 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5622 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5623 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5625 | if (s->v.Slice.upper) { |
| 5626 | VISIT(c, expr, s->v.Slice.upper); |
| 5627 | } |
| 5628 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5629 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | if (s->v.Slice.step) { |
| 5633 | n++; |
| 5634 | VISIT(c, expr, s->v.Slice.step); |
| 5635 | } |
| 5636 | ADDOP_I(c, BUILD_SLICE, n); |
| 5637 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5638 | } |
| 5639 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5640 | |
| 5641 | // PEP 634: Structural Pattern Matching |
| 5642 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5643 | // To keep things simple, all compiler_pattern_* and pattern_helper_* routines |
| 5644 | // follow the convention of consuming TOS (the subject for the given pattern) |
| 5645 | // and calling jump_to_fail_pop on failure (no match). |
| 5646 | |
| 5647 | // When calling into these routines, it's important that pc->on_top be kept |
| 5648 | // updated to reflect the current number of items that we are using on the top |
| 5649 | // of the stack: they will be popped on failure, and any name captures will be |
| 5650 | // stored *underneath* them on success. This lets us defer all names stores |
| 5651 | // until the *entire* pattern matches. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5652 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5653 | #define WILDCARD_CHECK(N) \ |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5654 | ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5655 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5656 | #define WILDCARD_STAR_CHECK(N) \ |
| 5657 | ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name) |
| 5658 | |
| 5659 | // Limit permitted subexpressions, even if the parser & AST validator let them through |
| 5660 | #define MATCH_VALUE_EXPR(N) \ |
| 5661 | ((N)->kind == Constant_kind || (N)->kind == Attribute_kind) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5662 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5663 | // Allocate or resize pc->fail_pop to allow for n items to be popped on failure. |
| 5664 | static int |
| 5665 | ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n) |
| 5666 | { |
| 5667 | Py_ssize_t size = n + 1; |
| 5668 | if (size <= pc->fail_pop_size) { |
| 5669 | return 1; |
| 5670 | } |
| 5671 | Py_ssize_t needed = sizeof(basicblock*) * size; |
| 5672 | basicblock **resized = PyObject_Realloc(pc->fail_pop, needed); |
| 5673 | if (resized == NULL) { |
| 5674 | PyErr_NoMemory(); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | pc->fail_pop = resized; |
| 5678 | while (pc->fail_pop_size < size) { |
| 5679 | basicblock *new_block; |
| 5680 | RETURN_IF_FALSE(new_block = compiler_new_block(c)); |
| 5681 | pc->fail_pop[pc->fail_pop_size++] = new_block; |
| 5682 | } |
| 5683 | return 1; |
| 5684 | } |
| 5685 | |
| 5686 | // Use op to jump to the correct fail_pop block. |
| 5687 | static int |
| 5688 | jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op) |
| 5689 | { |
| 5690 | // Pop any items on the top of the stack, plus any objects we were going to |
| 5691 | // capture on success: |
| 5692 | Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores); |
| 5693 | RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops)); |
| 5694 | ADDOP_JUMP(c, op, pc->fail_pop[pops]); |
| 5695 | NEXT_BLOCK(c); |
| 5696 | return 1; |
| 5697 | } |
| 5698 | |
| 5699 | // Build all of the fail_pop blocks and reset fail_pop. |
| 5700 | static int |
| 5701 | emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc) |
| 5702 | { |
| 5703 | if (!pc->fail_pop_size) { |
| 5704 | assert(pc->fail_pop == NULL); |
| 5705 | NEXT_BLOCK(c); |
| 5706 | return 1; |
| 5707 | } |
| 5708 | while (--pc->fail_pop_size) { |
| 5709 | compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]); |
| 5710 | if (!compiler_addop(c, POP_TOP)) { |
| 5711 | pc->fail_pop_size = 0; |
| 5712 | PyObject_Free(pc->fail_pop); |
| 5713 | pc->fail_pop = NULL; |
| 5714 | return 0; |
| 5715 | } |
| 5716 | } |
| 5717 | compiler_use_next_block(c, pc->fail_pop[0]); |
| 5718 | PyObject_Free(pc->fail_pop); |
| 5719 | pc->fail_pop = NULL; |
| 5720 | return 1; |
| 5721 | } |
| 5722 | |
| 5723 | static int |
| 5724 | compiler_error_duplicate_store(struct compiler *c, identifier n) |
| 5725 | { |
| 5726 | return compiler_error(c, "multiple assignments to name %R in pattern", n); |
| 5727 | } |
| 5728 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5729 | static int |
| 5730 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5731 | { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5732 | if (n == NULL) { |
| 5733 | ADDOP(c, POP_TOP); |
| 5734 | return 1; |
| 5735 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5736 | if (forbidden_name(c, n, Store)) { |
| 5737 | return 0; |
| 5738 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5739 | // Can't assign to the same name twice: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5740 | int duplicate = PySequence_Contains(pc->stores, n); |
| 5741 | if (duplicate < 0) { |
| 5742 | return 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5743 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5744 | if (duplicate) { |
| 5745 | return compiler_error_duplicate_store(c, n); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5746 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5747 | // Rotate this object underneath any items we need to preserve: |
| 5748 | ADDOP_I(c, ROT_N, pc->on_top + PyList_GET_SIZE(pc->stores) + 1); |
| 5749 | return !PyList_Append(pc->stores, n); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | |
| 5753 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5754 | pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts) |
| 5755 | { |
| 5756 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 5757 | int seen_star = 0; |
| 5758 | for (Py_ssize_t i = 0; i < n; i++) { |
| 5759 | pattern_ty elt = asdl_seq_GET(elts, i); |
| 5760 | if (elt->kind == MatchStar_kind && !seen_star) { |
| 5761 | if ((i >= (1 << 8)) || |
| 5762 | (n-i-1 >= (INT_MAX >> 8))) |
| 5763 | return compiler_error(c, |
| 5764 | "too many expressions in " |
| 5765 | "star-unpacking sequence pattern"); |
| 5766 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 5767 | seen_star = 1; |
| 5768 | } |
| 5769 | else if (elt->kind == MatchStar_kind) { |
| 5770 | return compiler_error(c, |
| 5771 | "multiple starred expressions in sequence pattern"); |
| 5772 | } |
| 5773 | } |
| 5774 | if (!seen_star) { |
| 5775 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 5776 | } |
| 5777 | return 1; |
| 5778 | } |
| 5779 | |
| 5780 | static int |
| 5781 | pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5782 | Py_ssize_t star, pattern_context *pc) |
| 5783 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5784 | RETURN_IF_FALSE(pattern_unpack_helper(c, patterns)); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5785 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5786 | // We've now got a bunch of new subjects on the stack. They need to remain |
| 5787 | // there after each subpattern match: |
| 5788 | pc->on_top += size; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5789 | for (Py_ssize_t i = 0; i < size; i++) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5790 | // One less item to keep track of each time we loop through: |
| 5791 | pc->on_top--; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5792 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5793 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5794 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5795 | return 1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5799 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5800 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5801 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5802 | pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns, |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5803 | Py_ssize_t star, pattern_context *pc) |
| 5804 | { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5805 | // We need to keep the subject around for extracting elements: |
| 5806 | pc->on_top++; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5807 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5808 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5809 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 5810 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5811 | continue; |
| 5812 | } |
| 5813 | if (i == star) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5814 | assert(WILDCARD_STAR_CHECK(pattern)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5815 | continue; |
| 5816 | } |
| 5817 | ADDOP(c, DUP_TOP); |
| 5818 | if (i < star) { |
| 5819 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5820 | } |
| 5821 | else { |
| 5822 | // The subject may not support negative indexing! Compute a |
| 5823 | // nonnegative index: |
| 5824 | ADDOP(c, GET_LEN); |
| 5825 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5826 | ADDOP(c, BINARY_SUBTRACT); |
| 5827 | } |
| 5828 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5829 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5830 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5831 | // Pop the subject, we're done with it: |
| 5832 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5833 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5834 | return 1; |
| 5835 | } |
| 5836 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5837 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5838 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5839 | compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5840 | { |
| 5841 | int allow_irrefutable = pc->allow_irrefutable; |
| 5842 | pc->allow_irrefutable = 1; |
| 5843 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5844 | pc->allow_irrefutable = allow_irrefutable; |
| 5845 | return 1; |
| 5846 | } |
| 5847 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5848 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5849 | compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) |
| 5850 | { |
| 5851 | assert(p->kind == MatchAs_kind); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5852 | if (p->v.MatchAs.pattern == NULL) { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5853 | // An irrefutable match: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5854 | if (!pc->allow_irrefutable) { |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5855 | if (p->v.MatchAs.name) { |
| 5856 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5857 | return compiler_error(c, e, p->v.MatchAs.name); |
| 5858 | } |
| 5859 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 5860 | return compiler_error(c, e); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5861 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5862 | return pattern_helper_store_name(c, p->v.MatchAs.name, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5863 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5864 | // Need to make a copy for (possibly) storing later: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5865 | pc->on_top++; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5866 | ADDOP(c, DUP_TOP); |
| 5867 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5868 | // Success! Store it: |
| 5869 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5870 | 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] | 5871 | return 1; |
| 5872 | } |
| 5873 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5874 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5875 | compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5876 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5877 | assert(p->kind == MatchStar_kind); |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 5878 | 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] | 5879 | return 1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5880 | } |
| 5881 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5882 | static int |
| 5883 | validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns) |
| 5884 | { |
| 5885 | // Any errors will point to the pattern rather than the arg name as the |
| 5886 | // parser is only supplying identifiers rather than Name or keyword nodes |
| 5887 | Py_ssize_t nattrs = asdl_seq_LEN(attrs); |
| 5888 | for (Py_ssize_t i = 0; i < nattrs; i++) { |
| 5889 | identifier attr = ((identifier)asdl_seq_GET(attrs, i)); |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5890 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i))); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5891 | if (forbidden_name(c, attr, Store)) { |
| 5892 | return -1; |
| 5893 | } |
| 5894 | for (Py_ssize_t j = i + 1; j < nattrs; j++) { |
| 5895 | identifier other = ((identifier)asdl_seq_GET(attrs, j)); |
| 5896 | if (!PyUnicode_Compare(attr, other)) { |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5897 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j))); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5898 | compiler_error(c, "attribute name repeated in class pattern: %U", attr); |
| 5899 | return -1; |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | return 0; |
| 5904 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5905 | |
| 5906 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5907 | compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5908 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5909 | assert(p->kind == MatchClass_kind); |
| 5910 | asdl_pattern_seq *patterns = p->v.MatchClass.patterns; |
| 5911 | asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs; |
| 5912 | asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns; |
| 5913 | Py_ssize_t nargs = asdl_seq_LEN(patterns); |
| 5914 | Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs); |
| 5915 | Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns); |
| 5916 | if (nattrs != nkwd_patterns) { |
| 5917 | // AST validator shouldn't let this happen, but if it does, |
| 5918 | // just fail, don't crash out of the interpreter |
| 5919 | const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern"; |
| 5920 | return compiler_error(c, e, nattrs, nkwd_patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5921 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5922 | if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) { |
| 5923 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5924 | return compiler_error(c, e, p->v.MatchClass.cls); |
| 5925 | } |
| 5926 | if (nattrs) { |
| 5927 | RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 5928 | SET_LOC(c, p); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5929 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5930 | VISIT(c, expr, p->v.MatchClass.cls); |
| 5931 | PyObject *attr_names; |
| 5932 | RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5933 | Py_ssize_t i; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5934 | for (i = 0; i < nattrs; i++) { |
| 5935 | PyObject *name = asdl_seq_GET(kwd_attrs, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5936 | Py_INCREF(name); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5937 | PyTuple_SET_ITEM(attr_names, i, name); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5938 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5939 | ADDOP_LOAD_CONST_NEW(c, attr_names); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5940 | ADDOP_I(c, MATCH_CLASS, nargs); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5941 | // TOS is now a tuple of (nargs + nattrs) attributes. Preserve it: |
| 5942 | pc->on_top++; |
| 5943 | 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] | 5944 | for (i = 0; i < nargs + nattrs; i++) { |
| 5945 | pattern_ty pattern; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5946 | if (i < nargs) { |
| 5947 | // Positional: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5948 | pattern = asdl_seq_GET(patterns, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5949 | } |
| 5950 | else { |
| 5951 | // Keyword: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5952 | pattern = asdl_seq_GET(kwd_patterns, i - nargs); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5953 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5954 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5955 | continue; |
| 5956 | } |
| 5957 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5958 | ADDOP(c, DUP_TOP); |
| 5959 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5960 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5961 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5962 | } |
| 5963 | // Success! Pop the tuple of attributes: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5964 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5965 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5966 | return 1; |
| 5967 | } |
| 5968 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5969 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5970 | compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5971 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5972 | assert(p->kind == MatchMapping_kind); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5973 | asdl_expr_seq *keys = p->v.MatchMapping.keys; |
| 5974 | asdl_pattern_seq *patterns = p->v.MatchMapping.patterns; |
| 5975 | Py_ssize_t size = asdl_seq_LEN(keys); |
| 5976 | Py_ssize_t npatterns = asdl_seq_LEN(patterns); |
| 5977 | if (size != npatterns) { |
| 5978 | // AST validator shouldn't let this happen, but if it does, |
| 5979 | // just fail, don't crash out of the interpreter |
| 5980 | const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern"; |
| 5981 | return compiler_error(c, e, size, npatterns); |
| 5982 | } |
| 5983 | // We have a double-star target if "rest" is set |
| 5984 | PyObject *star_target = p->v.MatchMapping.rest; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5985 | // We need to keep the subject on top during the mapping and length checks: |
| 5986 | pc->on_top++; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5987 | ADDOP(c, MATCH_MAPPING); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5988 | 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] | 5989 | if (!size && !star_target) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 5990 | // If the pattern is just "{}", we're done! Pop the subject: |
| 5991 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5992 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5993 | return 1; |
| 5994 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5995 | if (size) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5996 | // If the pattern has any keys in it, perform a length check: |
| 5997 | ADDOP(c, GET_LEN); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 5998 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5999 | ADDOP_COMPARE(c, GtE); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6000 | 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] | 6001 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6002 | if (INT_MAX < size - 1) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6003 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 6004 | } |
| 6005 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 6006 | // 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] | 6007 | |
| 6008 | // Maintaining a set of Constant_kind kind keys allows us to raise a |
| 6009 | // SyntaxError in the case of duplicates. |
| 6010 | PyObject *seen = PySet_New(NULL); |
| 6011 | if (seen == NULL) { |
| 6012 | return 0; |
| 6013 | } |
| 6014 | |
| 6015 | // 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] | 6016 | for (Py_ssize_t i = 0; i < size; i++) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6017 | expr_ty key = asdl_seq_GET(keys, i); |
| 6018 | if (key == NULL) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6019 | const char *e = "can't use NULL keys in MatchMapping " |
| 6020 | "(set 'rest' parameter instead)"; |
Miss Islington (bot) | 13de28f | 2021-05-07 13:40:09 -0700 | [diff] [blame] | 6021 | SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i))); |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6022 | compiler_error(c, e); |
| 6023 | goto error; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6024 | } |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6025 | |
| 6026 | if (key->kind == Constant_kind) { |
| 6027 | int in_seen = PySet_Contains(seen, key->v.Constant.value); |
| 6028 | if (in_seen < 0) { |
| 6029 | goto error; |
| 6030 | } |
| 6031 | if (in_seen) { |
| 6032 | const char *e = "mapping pattern checks duplicate key (%R)"; |
| 6033 | compiler_error(c, e, key->v.Constant.value); |
| 6034 | goto error; |
| 6035 | } |
| 6036 | if (PySet_Add(seen, key->v.Constant.value)) { |
| 6037 | goto error; |
| 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | else if (key->kind != Attribute_kind) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6042 | 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] | 6043 | compiler_error(c, e); |
| 6044 | goto error; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6045 | } |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6046 | if (!compiler_visit_expr(c, key)) { |
| 6047 | goto error; |
| 6048 | } |
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 | |
| 6051 | // all keys have been checked; there are no duplicates |
| 6052 | Py_DECREF(seen); |
| 6053 | |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6054 | ADDOP_I(c, BUILD_TUPLE, size); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6055 | ADDOP(c, MATCH_KEYS); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6056 | // There's now a tuple of keys and a tuple of values on top of the subject: |
| 6057 | pc->on_top += 2; |
| 6058 | RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE)); |
| 6059 | // 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] | 6060 | // sub-patterns against: |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6061 | for (Py_ssize_t i = 0; i < size; i++) { |
| 6062 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 6063 | if (WILDCARD_CHECK(pattern)) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6064 | continue; |
| 6065 | } |
| 6066 | ADDOP(c, DUP_TOP); |
| 6067 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 6068 | ADDOP(c, BINARY_SUBSCR); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6069 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6070 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6071 | // If we get this far, it's a match! We're done with the tuple of values, |
| 6072 | // and whatever happens next should consume the tuple of keys underneath it: |
| 6073 | pc->on_top -= 2; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6074 | ADDOP(c, POP_TOP); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6075 | if (star_target) { |
| 6076 | // 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] | 6077 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6078 | RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6079 | } |
| 6080 | else { |
| 6081 | // Otherwise, we don't care about this tuple of keys anymore: |
| 6082 | ADDOP(c, POP_TOP); |
| 6083 | } |
| 6084 | // Pop the subject: |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6085 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6086 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6087 | return 1; |
Miss Islington (bot) | 016af14 | 2021-07-14 18:00:35 -0700 | [diff] [blame] | 6088 | |
| 6089 | error: |
| 6090 | Py_DECREF(seen); |
| 6091 | return 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6092 | } |
| 6093 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6094 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6095 | compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6096 | { |
| 6097 | assert(p->kind == MatchOr_kind); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6098 | basicblock *end; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6099 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6100 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 6101 | assert(size > 1); |
| 6102 | // 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] | 6103 | pattern_context old_pc = *pc; |
| 6104 | Py_INCREF(pc->stores); |
| 6105 | // control is the list of names bound by the first alternative. It is used |
| 6106 | // for checking different name bindings in alternatives, and for correcting |
| 6107 | // the order in which extracted elements are placed on the stack. |
| 6108 | PyObject *control = NULL; |
| 6109 | // NOTE: We can't use returning macros anymore! goto error on error. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6110 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6111 | pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6112 | SET_LOC(c, alt); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6113 | PyObject *pc_stores = PyList_New(0); |
| 6114 | if (pc_stores == NULL) { |
| 6115 | goto error; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6116 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6117 | Py_SETREF(pc->stores, pc_stores); |
| 6118 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 6119 | pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable; |
| 6120 | pc->fail_pop = NULL; |
| 6121 | pc->fail_pop_size = 0; |
| 6122 | pc->on_top = 0; |
| 6123 | if (!compiler_addop(c, DUP_TOP) || !compiler_pattern(c, alt, pc)) { |
| 6124 | goto error; |
| 6125 | } |
| 6126 | // Success! |
| 6127 | Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6128 | if (!i) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6129 | // This is the first alternative, so save its stores as a "control" |
| 6130 | // for the others (they can't bind a different set of names, and |
| 6131 | // might need to be reordered): |
| 6132 | assert(control == NULL); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6133 | control = pc->stores; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6134 | Py_INCREF(control); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6135 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6136 | else if (nstores != PyList_GET_SIZE(control)) { |
| 6137 | goto diff; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6138 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6139 | else if (nstores) { |
| 6140 | // There were captures. Check to see if we differ from control: |
| 6141 | Py_ssize_t icontrol = nstores; |
| 6142 | while (icontrol--) { |
| 6143 | PyObject *name = PyList_GET_ITEM(control, icontrol); |
| 6144 | Py_ssize_t istores = PySequence_Index(pc->stores, name); |
| 6145 | if (istores < 0) { |
| 6146 | PyErr_Clear(); |
| 6147 | goto diff; |
| 6148 | } |
| 6149 | if (icontrol != istores) { |
| 6150 | // Reorder the names on the stack to match the order of the |
| 6151 | // names in control. There's probably a better way of doing |
| 6152 | // this; the current solution is potentially very |
| 6153 | // inefficient when each alternative subpattern binds lots |
| 6154 | // of names in different orders. It's fine for reasonable |
| 6155 | // cases, though. |
| 6156 | assert(istores < icontrol); |
| 6157 | Py_ssize_t rotations = istores + 1; |
| 6158 | // Perfom the same rotation on pc->stores: |
| 6159 | PyObject *rotated = PyList_GetSlice(pc->stores, 0, |
| 6160 | rotations); |
| 6161 | if (rotated == NULL || |
| 6162 | PyList_SetSlice(pc->stores, 0, rotations, NULL) || |
| 6163 | PyList_SetSlice(pc->stores, icontrol - istores, |
| 6164 | icontrol - istores, rotated)) |
| 6165 | { |
| 6166 | Py_XDECREF(rotated); |
| 6167 | goto error; |
| 6168 | } |
| 6169 | Py_DECREF(rotated); |
| 6170 | // That just did: |
| 6171 | // rotated = pc_stores[:rotations] |
| 6172 | // del pc_stores[:rotations] |
| 6173 | // pc_stores[icontrol-istores:icontrol-istores] = rotated |
| 6174 | // Do the same thing to the stack, using several ROT_Ns: |
| 6175 | while (rotations--) { |
| 6176 | if (!compiler_addop_i(c, ROT_N, icontrol + 1)) { |
| 6177 | goto error; |
| 6178 | } |
| 6179 | } |
| 6180 | } |
| 6181 | } |
| 6182 | } |
| 6183 | assert(control); |
| 6184 | if (!compiler_addop_j(c, JUMP_FORWARD, end) || |
| 6185 | !compiler_next_block(c) || |
| 6186 | !emit_and_reset_fail_pop(c, pc)) |
| 6187 | { |
| 6188 | goto error; |
| 6189 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6190 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6191 | Py_DECREF(pc->stores); |
| 6192 | *pc = old_pc; |
| 6193 | Py_INCREF(pc->stores); |
| 6194 | // Need to NULL this for the PyObject_Free call in the error block. |
| 6195 | old_pc.fail_pop = NULL; |
| 6196 | // No match. Pop the remaining copy of the subject and fail: |
| 6197 | if (!compiler_addop(c, POP_TOP) || !jump_to_fail_pop(c, pc, JUMP_FORWARD)) { |
| 6198 | goto error; |
| 6199 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6200 | compiler_use_next_block(c, end); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6201 | Py_ssize_t nstores = PyList_GET_SIZE(control); |
| 6202 | // There's a bunch of stuff on the stack between any where the new stores |
| 6203 | // are and where they need to be: |
| 6204 | // - The other stores. |
| 6205 | // - A copy of the subject. |
| 6206 | // - Anything else that may be on top of the stack. |
| 6207 | // - Any previous stores we've already stashed away on the stack. |
Pablo Galindo | 3949428 | 2021-05-03 16:20:46 +0100 | [diff] [blame] | 6208 | 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] | 6209 | for (Py_ssize_t i = 0; i < nstores; i++) { |
| 6210 | // Rotate this capture to its proper place on the stack: |
| 6211 | if (!compiler_addop_i(c, ROT_N, nrots)) { |
| 6212 | goto error; |
| 6213 | } |
| 6214 | // Update the list of previous stores with this new name, checking for |
| 6215 | // duplicates: |
| 6216 | PyObject *name = PyList_GET_ITEM(control, i); |
| 6217 | int dupe = PySequence_Contains(pc->stores, name); |
| 6218 | if (dupe < 0) { |
| 6219 | goto error; |
| 6220 | } |
| 6221 | if (dupe) { |
| 6222 | compiler_error_duplicate_store(c, name); |
| 6223 | goto error; |
| 6224 | } |
| 6225 | if (PyList_Append(pc->stores, name)) { |
| 6226 | goto error; |
| 6227 | } |
| 6228 | } |
| 6229 | Py_DECREF(old_pc.stores); |
| 6230 | Py_DECREF(control); |
| 6231 | // NOTE: Returning macros are safe again. |
| 6232 | // Pop the copy of the subject: |
| 6233 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6234 | return 1; |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6235 | diff: |
| 6236 | compiler_error(c, "alternative patterns bind different names"); |
| 6237 | error: |
| 6238 | PyObject_Free(old_pc.fail_pop); |
| 6239 | Py_DECREF(old_pc.stores); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6240 | Py_XDECREF(control); |
| 6241 | return 0; |
| 6242 | } |
| 6243 | |
| 6244 | |
| 6245 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6246 | compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6247 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6248 | assert(p->kind == MatchSequence_kind); |
| 6249 | asdl_pattern_seq *patterns = p->v.MatchSequence.patterns; |
| 6250 | Py_ssize_t size = asdl_seq_LEN(patterns); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6251 | Py_ssize_t star = -1; |
| 6252 | int only_wildcard = 1; |
| 6253 | int star_wildcard = 0; |
| 6254 | // Find a starred name, if it exists. There may be at most one: |
| 6255 | for (Py_ssize_t i = 0; i < size; i++) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6256 | pattern_ty pattern = asdl_seq_GET(patterns, i); |
| 6257 | if (pattern->kind == MatchStar_kind) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6258 | if (star >= 0) { |
| 6259 | const char *e = "multiple starred names in sequence pattern"; |
| 6260 | return compiler_error(c, e); |
| 6261 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6262 | star_wildcard = WILDCARD_STAR_CHECK(pattern); |
| 6263 | only_wildcard &= star_wildcard; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6264 | star = i; |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6265 | continue; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6266 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6267 | only_wildcard &= WILDCARD_CHECK(pattern); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6268 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6269 | // We need to keep the subject on top during the sequence and length checks: |
| 6270 | pc->on_top++; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6271 | ADDOP(c, MATCH_SEQUENCE); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6272 | 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] | 6273 | if (star < 0) { |
| 6274 | // No star: len(subject) == size |
| 6275 | ADDOP(c, GET_LEN); |
| 6276 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 6277 | ADDOP_COMPARE(c, Eq); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6278 | 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] | 6279 | } |
| 6280 | else if (size > 1) { |
| 6281 | // Star: len(subject) >= size - 1 |
| 6282 | ADDOP(c, GET_LEN); |
| 6283 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 6284 | ADDOP_COMPARE(c, GtE); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6285 | 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] | 6286 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6287 | // Whatever comes next should consume the subject: |
| 6288 | pc->on_top--; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6289 | if (only_wildcard) { |
| 6290 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 6291 | ADDOP(c, POP_TOP); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6292 | } |
| 6293 | else if (star_wildcard) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6294 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6295 | } |
| 6296 | else { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6297 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6298 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6299 | return 1; |
| 6300 | } |
| 6301 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6302 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6303 | compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6304 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6305 | assert(p->kind == MatchValue_kind); |
| 6306 | expr_ty value = p->v.MatchValue.value; |
| 6307 | if (!MATCH_VALUE_EXPR(value)) { |
| 6308 | const char *e = "patterns may only match literals and attribute lookups"; |
| 6309 | return compiler_error(c, e); |
| 6310 | } |
| 6311 | VISIT(c, expr, value); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6312 | ADDOP_COMPARE(c, Eq); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6313 | 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] | 6314 | return 1; |
| 6315 | } |
| 6316 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6317 | static int |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 6318 | compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6319 | { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6320 | assert(p->kind == MatchSingleton_kind); |
| 6321 | ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value); |
| 6322 | ADDOP_COMPARE(c, Is); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6323 | 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] | 6324 | return 1; |
| 6325 | } |
| 6326 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6327 | static int |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6328 | compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6329 | { |
| 6330 | SET_LOC(c, p); |
| 6331 | switch (p->kind) { |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6332 | case MatchValue_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6333 | return compiler_pattern_value(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6334 | case MatchSingleton_kind: |
Brandt Bucher | dbe60ee | 2021-04-29 17:19:28 -0700 | [diff] [blame] | 6335 | return compiler_pattern_singleton(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6336 | case MatchSequence_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6337 | return compiler_pattern_sequence(c, p, pc); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6338 | case MatchMapping_kind: |
| 6339 | return compiler_pattern_mapping(c, p, pc); |
| 6340 | case MatchClass_kind: |
| 6341 | return compiler_pattern_class(c, p, pc); |
| 6342 | case MatchStar_kind: |
| 6343 | return compiler_pattern_star(c, p, pc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6344 | case MatchAs_kind: |
| 6345 | return compiler_pattern_as(c, p, pc); |
| 6346 | case MatchOr_kind: |
| 6347 | return compiler_pattern_or(c, p, pc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6348 | } |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6349 | // AST validator shouldn't let this happen, but if it does, |
| 6350 | // just fail, don't crash out of the interpreter |
| 6351 | const char *e = "invalid match pattern node in AST (kind=%d)"; |
| 6352 | return compiler_error(c, e, p->kind); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6353 | } |
| 6354 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6355 | static int |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6356 | compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6357 | { |
| 6358 | VISIT(c, expr, s->v.Match.subject); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6359 | basicblock *end; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6360 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6361 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6362 | assert(cases > 0); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6363 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6364 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6365 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6366 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6367 | SET_LOC(c, m->pattern); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6368 | // Only copy the subject if we're *not* on the last case: |
| 6369 | if (i != cases - has_default - 1) { |
| 6370 | ADDOP(c, DUP_TOP); |
| 6371 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6372 | RETURN_IF_FALSE(pc->stores = PyList_New(0)); |
| 6373 | // Irrefutable cases must be either guarded, last, or both: |
| 6374 | pc->allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6375 | pc->fail_pop = NULL; |
| 6376 | pc->fail_pop_size = 0; |
| 6377 | pc->on_top = 0; |
| 6378 | // NOTE: Can't use returning macros here (they'll leak pc->stores)! |
| 6379 | if (!compiler_pattern(c, m->pattern, pc)) { |
| 6380 | Py_DECREF(pc->stores); |
| 6381 | return 0; |
| 6382 | } |
| 6383 | assert(!pc->on_top); |
| 6384 | // It's a match! Store all of the captured names (they're on the stack). |
| 6385 | Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); |
| 6386 | for (Py_ssize_t n = 0; n < nstores; n++) { |
| 6387 | PyObject *name = PyList_GET_ITEM(pc->stores, n); |
| 6388 | if (!compiler_nameop(c, name, Store)) { |
| 6389 | Py_DECREF(pc->stores); |
| 6390 | return 0; |
| 6391 | } |
| 6392 | } |
| 6393 | Py_DECREF(pc->stores); |
| 6394 | // NOTE: Returning macros are safe again. |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6395 | if (m->guard) { |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6396 | RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0)); |
| 6397 | 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] | 6398 | } |
| 6399 | // Success! Pop the subject off, we're done with it: |
| 6400 | if (i != cases - has_default - 1) { |
| 6401 | ADDOP(c, POP_TOP); |
| 6402 | } |
| 6403 | VISIT_SEQ(c, stmt, m->body); |
| 6404 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Miss Islington (bot) | 01601aa | 2021-07-25 17:04:06 -0700 | [diff] [blame] | 6405 | // If the pattern fails to match, we want the line number of the |
| 6406 | // cleanup to be associated with the failed pattern, not the last line |
| 6407 | // of the body |
| 6408 | SET_LOC(c, m->pattern); |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6409 | RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc)); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6410 | } |
| 6411 | if (has_default) { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6412 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6413 | // pushing and popping in the loop above: |
| 6414 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6415 | SET_LOC(c, m->pattern); |
Miss Islington (bot) | 01601aa | 2021-07-25 17:04:06 -0700 | [diff] [blame] | 6416 | if (cases == 1) { |
| 6417 | // No matches. Done with the subject: |
| 6418 | ADDOP(c, POP_TOP); |
| 6419 | } |
| 6420 | else { |
| 6421 | // Show line coverage for default case (it doesn't create bytecode) |
| 6422 | ADDOP(c, NOP); |
| 6423 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6424 | if (m->guard) { |
| 6425 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6426 | } |
| 6427 | VISIT_SEQ(c, stmt, m->body); |
| 6428 | } |
| 6429 | compiler_use_next_block(c, end); |
| 6430 | return 1; |
| 6431 | } |
| 6432 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 6433 | static int |
| 6434 | compiler_match(struct compiler *c, stmt_ty s) |
| 6435 | { |
| 6436 | pattern_context pc; |
| 6437 | pc.fail_pop = NULL; |
| 6438 | int result = compiler_match_inner(c, s, &pc); |
| 6439 | PyObject_Free(pc.fail_pop); |
| 6440 | return result; |
| 6441 | } |
| 6442 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6443 | #undef WILDCARD_CHECK |
Nick Coghlan | 1e7b858 | 2021-04-29 15:58:44 +1000 | [diff] [blame] | 6444 | #undef WILDCARD_STAR_CHECK |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 6445 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6446 | /* End of the compiler section, beginning of the assembler section */ |
| 6447 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6448 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6449 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6450 | |
| 6451 | XXX must handle implicit jumps from one block to next |
| 6452 | */ |
| 6453 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6454 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6455 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6456 | int a_offset; /* offset into bytecode */ |
| 6457 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6458 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6459 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6460 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6461 | int a_lineno; /* lineno of last emitted instruction */ |
| 6462 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6463 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6464 | }; |
| 6465 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6466 | Py_LOCAL_INLINE(void) |
| 6467 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6468 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6469 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6470 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6471 | assert(b->b_startdepth < 0); |
| 6472 | b->b_startdepth = depth; |
| 6473 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6474 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6475 | } |
| 6476 | |
| 6477 | /* Find the flow path that needs the largest stack. We assume that |
| 6478 | * cycles in the flow graph have no net effect on the stack depth. |
| 6479 | */ |
| 6480 | static int |
| 6481 | stackdepth(struct compiler *c) |
| 6482 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6483 | basicblock *b, *entryblock = NULL; |
| 6484 | basicblock **stack, **sp; |
| 6485 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6486 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6487 | b->b_startdepth = INT_MIN; |
| 6488 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6489 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6490 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6491 | assert(entryblock!= NULL); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6492 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6493 | if (!stack) { |
| 6494 | PyErr_NoMemory(); |
| 6495 | return -1; |
| 6496 | } |
| 6497 | |
| 6498 | sp = stack; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6499 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
| 6500 | stackdepth_push(&sp, entryblock, 1); |
| 6501 | } else { |
| 6502 | stackdepth_push(&sp, entryblock, 0); |
| 6503 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6504 | while (sp != stack) { |
| 6505 | b = *--sp; |
| 6506 | int depth = b->b_startdepth; |
| 6507 | assert(depth >= 0); |
| 6508 | basicblock *next = b->b_next; |
| 6509 | for (int i = 0; i < b->b_iused; i++) { |
| 6510 | struct instr *instr = &b->b_instr[i]; |
| 6511 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6512 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6513 | PyErr_Format(PyExc_SystemError, |
| 6514 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6515 | instr->i_opcode, instr->i_oparg); |
| 6516 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6517 | } |
| 6518 | int new_depth = depth + effect; |
| 6519 | if (new_depth > maxdepth) { |
| 6520 | maxdepth = new_depth; |
| 6521 | } |
| 6522 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6523 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6524 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6525 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6526 | int target_depth = depth + effect; |
| 6527 | if (target_depth > maxdepth) { |
| 6528 | maxdepth = target_depth; |
| 6529 | } |
| 6530 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6531 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6532 | } |
| 6533 | depth = new_depth; |
| 6534 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6535 | instr->i_opcode == JUMP_FORWARD || |
| 6536 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6537 | instr->i_opcode == RAISE_VARARGS || |
| 6538 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6539 | { |
| 6540 | /* remaining code is dead */ |
| 6541 | next = NULL; |
| 6542 | break; |
| 6543 | } |
| 6544 | } |
| 6545 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6546 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6547 | stackdepth_push(&sp, next, depth); |
| 6548 | } |
| 6549 | } |
| 6550 | PyObject_Free(stack); |
| 6551 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | static int |
| 6555 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6557 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6558 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6559 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6560 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6561 | if (a->a_bytecode == NULL) { |
| 6562 | goto error; |
| 6563 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6564 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6565 | if (a->a_lnotab == NULL) { |
| 6566 | goto error; |
| 6567 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6568 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6569 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6570 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6572 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6573 | error: |
| 6574 | Py_XDECREF(a->a_bytecode); |
| 6575 | Py_XDECREF(a->a_lnotab); |
| 6576 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | static void |
| 6580 | assemble_free(struct assembler *a) |
| 6581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6582 | Py_XDECREF(a->a_bytecode); |
| 6583 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6586 | static int |
| 6587 | blocksize(basicblock *b) |
| 6588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6589 | int i; |
| 6590 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6593 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6594 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6597 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6598 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6599 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6600 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6601 | if (a->a_lnotab_off + 2 >= len) { |
| 6602 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6603 | return 0; |
| 6604 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6605 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6606 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6607 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6608 | *lnotab++ = bdelta; |
| 6609 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6610 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6613 | /* Appends a range to the end of the line number table. See |
| 6614 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6615 | |
| 6616 | static int |
| 6617 | assemble_line_range(struct assembler *a) |
| 6618 | { |
| 6619 | int ldelta, bdelta; |
| 6620 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6621 | if (bdelta == 0) { |
| 6622 | return 1; |
| 6623 | } |
| 6624 | if (a->a_lineno < 0) { |
| 6625 | ldelta = -128; |
| 6626 | } |
| 6627 | else { |
| 6628 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6629 | a->a_prevlineno = a->a_lineno; |
| 6630 | while (ldelta > 127) { |
| 6631 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6632 | return 0; |
| 6633 | } |
| 6634 | ldelta -= 127; |
| 6635 | } |
| 6636 | while (ldelta < -127) { |
| 6637 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6638 | return 0; |
| 6639 | } |
| 6640 | ldelta += 127; |
| 6641 | } |
| 6642 | } |
| 6643 | assert(-128 <= ldelta && ldelta < 128); |
| 6644 | while (bdelta > 254) { |
| 6645 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6646 | return 0; |
| 6647 | } |
| 6648 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6649 | bdelta -= 254; |
| 6650 | } |
| 6651 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6652 | return 0; |
| 6653 | } |
| 6654 | a->a_lineno_start = a->a_offset; |
| 6655 | return 1; |
| 6656 | } |
| 6657 | |
| 6658 | static int |
| 6659 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6660 | { |
| 6661 | if (i->i_lineno == a->a_lineno) { |
| 6662 | return 1; |
| 6663 | } |
| 6664 | if (!assemble_line_range(a)) { |
| 6665 | return 0; |
| 6666 | } |
| 6667 | a->a_lineno = i->i_lineno; |
| 6668 | return 1; |
| 6669 | } |
| 6670 | |
| 6671 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6672 | /* assemble_emit() |
| 6673 | Extend the bytecode with a new instruction. |
| 6674 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6675 | */ |
| 6676 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6677 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6678 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6679 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6680 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6681 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6682 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6683 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6684 | arg = i->i_oparg; |
| 6685 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6686 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6687 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6688 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6689 | if (len > PY_SSIZE_T_MAX / 2) |
| 6690 | return 0; |
| 6691 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6692 | return 0; |
| 6693 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6694 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6695 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6696 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6697 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6700 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6701 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6704 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6705 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6707 | /* Compute the size of each block and fixup jump args. |
| 6708 | Replace block pointer with position in bytecode. */ |
| 6709 | do { |
| 6710 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6711 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6712 | bsize = blocksize(b); |
| 6713 | b->b_offset = totsize; |
| 6714 | totsize += bsize; |
| 6715 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6716 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6717 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6718 | bsize = b->b_offset; |
| 6719 | for (i = 0; i < b->b_iused; i++) { |
| 6720 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6721 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6722 | /* Relative jumps are computed relative to |
| 6723 | the instruction pointer after fetching |
| 6724 | the jump instruction. |
| 6725 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6726 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6727 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6728 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6729 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6730 | instr->i_oparg -= bsize; |
| 6731 | } |
| 6732 | if (instrsize(instr->i_oparg) != isize) { |
| 6733 | extended_arg_recompile = 1; |
| 6734 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6735 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6736 | } |
| 6737 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6739 | /* XXX: This is an awful hack that could hurt performance, but |
| 6740 | on the bright side it should work until we come up |
| 6741 | with a better solution. |
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 | The issue is that in the first loop blocksize() is called |
| 6744 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6745 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6746 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6748 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6749 | The only EXTENDED_ARGs that could be popping up are |
| 6750 | ones in jump instructions. So this should converge |
| 6751 | fairly quickly. |
| 6752 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6753 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6754 | } |
| 6755 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6756 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6757 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6759 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6760 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6762 | tuple = PyTuple_New(size); |
| 6763 | if (tuple == NULL) |
| 6764 | return NULL; |
| 6765 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6766 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6767 | Py_INCREF(k); |
| 6768 | assert((i - offset) < size); |
| 6769 | assert((i - offset) >= 0); |
| 6770 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6771 | } |
| 6772 | return tuple; |
| 6773 | } |
| 6774 | |
| 6775 | static PyObject * |
| 6776 | consts_dict_keys_inorder(PyObject *dict) |
| 6777 | { |
| 6778 | PyObject *consts, *k, *v; |
| 6779 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6780 | |
| 6781 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6782 | if (consts == NULL) |
| 6783 | return NULL; |
| 6784 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6785 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6786 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6787 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6788 | * the object we want is always second. */ |
| 6789 | if (PyTuple_CheckExact(k)) { |
| 6790 | k = PyTuple_GET_ITEM(k, 1); |
| 6791 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6792 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6793 | assert(i < size); |
| 6794 | assert(i >= 0); |
| 6795 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6796 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6797 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6800 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6801 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6803 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6804 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6805 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6806 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6807 | if (ste->ste_nested) |
| 6808 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6809 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6810 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6811 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6812 | flags |= CO_COROUTINE; |
| 6813 | if (ste->ste_generator && ste->ste_coroutine) |
| 6814 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6815 | if (ste->ste_varargs) |
| 6816 | flags |= CO_VARARGS; |
| 6817 | if (ste->ste_varkeywords) |
| 6818 | flags |= CO_VARKEYWORDS; |
| 6819 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6821 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6822 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6823 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6824 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6825 | ste->ste_coroutine && |
| 6826 | !ste->ste_generator) { |
| 6827 | flags |= CO_COROUTINE; |
| 6828 | } |
| 6829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6830 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6833 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6834 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6835 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6836 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6837 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6838 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6839 | if (key == NULL) { |
| 6840 | return 0; |
| 6841 | } |
| 6842 | |
| 6843 | // t is borrowed reference |
| 6844 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6845 | Py_DECREF(key); |
| 6846 | if (t == NULL) { |
| 6847 | return 0; |
| 6848 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6849 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6850 | return 1; |
| 6851 | } |
| 6852 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6853 | if (PyTuple_CheckExact(t)) { |
| 6854 | // t is still borrowed reference |
| 6855 | t = PyTuple_GET_ITEM(t, 1); |
| 6856 | } |
| 6857 | |
| 6858 | Py_INCREF(t); |
| 6859 | Py_DECREF(*obj); |
| 6860 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6861 | return 1; |
| 6862 | } |
| 6863 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6864 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6865 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6867 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6868 | PyObject *names = NULL; |
| 6869 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6870 | PyObject *name = NULL; |
| 6871 | PyObject *freevars = NULL; |
| 6872 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6873 | Py_ssize_t nlocals; |
| 6874 | int nlocals_int; |
| 6875 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6876 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6878 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6879 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6880 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6881 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6882 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6883 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6884 | if (!cellvars) |
| 6885 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6886 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6887 | if (!freevars) |
| 6888 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6889 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6890 | if (!merge_const_one(c, &names) || |
| 6891 | !merge_const_one(c, &varnames) || |
| 6892 | !merge_const_one(c, &cellvars) || |
| 6893 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6894 | { |
| 6895 | goto error; |
| 6896 | } |
| 6897 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6898 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6899 | assert(nlocals < INT_MAX); |
| 6900 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6902 | flags = compute_code_flags(c); |
| 6903 | if (flags < 0) |
| 6904 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6905 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6906 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6907 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6908 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6909 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6910 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6911 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6912 | goto error; |
| 6913 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6914 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6915 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6916 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6917 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6918 | maxdepth = stackdepth(c); |
| 6919 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6920 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6921 | goto error; |
| 6922 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 6923 | if (maxdepth > MAX_ALLOWED_STACK_USE) { |
| 6924 | PyErr_Format(PyExc_SystemError, |
| 6925 | "excessive stack use: stack is %d deep", |
| 6926 | maxdepth); |
| 6927 | Py_DECREF(consts); |
| 6928 | goto error; |
| 6929 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6930 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6931 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6932 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6933 | varnames, freevars, cellvars, c->c_filename, |
| 6934 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6935 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6936 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6937 | Py_XDECREF(names); |
| 6938 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6939 | Py_XDECREF(name); |
| 6940 | Py_XDECREF(freevars); |
| 6941 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6942 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6945 | |
| 6946 | /* For debugging purposes only */ |
| 6947 | #if 0 |
| 6948 | static void |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 6949 | dump_instr(struct instr *i) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6950 | { |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 6951 | const char *jrel = (is_relative_jump(i)) ? "jrel " : ""; |
| 6952 | const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : ""; |
| 6953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6954 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6956 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6957 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6958 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6959 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6960 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6961 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | static void |
| 6965 | dump_basicblock(const basicblock *b) |
| 6966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6967 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6968 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6969 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6970 | if (b->b_instr) { |
| 6971 | int i; |
| 6972 | for (i = 0; i < b->b_iused; i++) { |
| 6973 | fprintf(stderr, " [%02d] ", i); |
| 6974 | dump_instr(b->b_instr + i); |
| 6975 | } |
| 6976 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6977 | } |
| 6978 | #endif |
| 6979 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6980 | |
| 6981 | static int |
| 6982 | normalize_basic_block(basicblock *bb); |
| 6983 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6984 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6985 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6986 | |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 6987 | /* Duplicates exit BBs, so that line numbers can be propagated to them */ |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6988 | static int |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 6989 | duplicate_exits_without_lineno(struct compiler *c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6990 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6991 | static int |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 6992 | extend_block(basicblock *bb); |
| 6993 | |
| 6994 | static int |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6995 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
| 6996 | |
| 6997 | int flags = compute_code_flags(c); |
| 6998 | if (flags < 0) { |
| 6999 | return -1; |
| 7000 | } |
| 7001 | int kind; |
| 7002 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 7003 | if (flags & CO_COROUTINE) { |
| 7004 | kind = 1; |
| 7005 | } |
| 7006 | else if (flags & CO_ASYNC_GENERATOR) { |
| 7007 | kind = 2; |
| 7008 | } |
| 7009 | else { |
| 7010 | kind = 0; |
| 7011 | } |
| 7012 | } |
| 7013 | else { |
| 7014 | return 0; |
| 7015 | } |
| 7016 | if (compiler_next_instr(entryblock) < 0) { |
| 7017 | return -1; |
| 7018 | } |
| 7019 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
| 7020 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
| 7021 | } |
| 7022 | entryblock->b_instr[0].i_opcode = GEN_START; |
| 7023 | entryblock->b_instr[0].i_oparg = kind; |
| 7024 | entryblock->b_instr[0].i_lineno = -1; |
| 7025 | entryblock->b_instr[0].i_target = NULL; |
| 7026 | return 0; |
| 7027 | } |
| 7028 | |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7029 | /* Make sure that all returns have a line number, even if early passes |
| 7030 | * have failed to propagate a correct line number. |
| 7031 | * The resulting line number may not be correct according to PEP 626, |
| 7032 | * but should be "good enough", and no worse than in older versions. */ |
| 7033 | static void |
| 7034 | guarantee_lineno_for_exits(struct assembler *a, int firstlineno) { |
| 7035 | int lineno = firstlineno; |
| 7036 | assert(lineno > 0); |
| 7037 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7038 | if (b->b_iused == 0) { |
| 7039 | continue; |
| 7040 | } |
| 7041 | struct instr *last = &b->b_instr[b->b_iused-1]; |
| 7042 | if (last->i_lineno < 0) { |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7043 | if (last->i_opcode == RETURN_VALUE) { |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7044 | for (int i = 0; i < b->b_iused; i++) { |
| 7045 | assert(b->b_instr[i].i_lineno < 0); |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7046 | |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7047 | b->b_instr[i].i_lineno = lineno; |
| 7048 | } |
| 7049 | } |
| 7050 | } |
| 7051 | else { |
| 7052 | lineno = last->i_lineno; |
| 7053 | } |
| 7054 | } |
| 7055 | } |
| 7056 | |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7057 | static void |
| 7058 | propagate_line_numbers(struct assembler *a); |
| 7059 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7060 | static PyCodeObject * |
| 7061 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 7062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7063 | basicblock *b, *entryblock; |
| 7064 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7065 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7066 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7067 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 7068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7069 | /* Make sure every block that falls off the end returns None. |
| 7070 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 7071 | block ends with a jump or return b_next shouldn't set. |
| 7072 | */ |
| 7073 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7074 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7075 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 7076 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7077 | ADDOP(c, RETURN_VALUE); |
| 7078 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7079 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7080 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7081 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 7082 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7083 | } |
| 7084 | } |
| 7085 | |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7086 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7087 | if (extend_block(b)) { |
| 7088 | return NULL; |
| 7089 | } |
| 7090 | } |
| 7091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7092 | nblocks = 0; |
| 7093 | entryblock = NULL; |
| 7094 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7095 | nblocks++; |
| 7096 | entryblock = b; |
| 7097 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 7098 | assert(entryblock != NULL); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7099 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 7100 | if (insert_generator_prefix(c, entryblock)) { |
| 7101 | goto error; |
| 7102 | } |
| 7103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7104 | /* Set firstlineno if it wasn't explicitly set. */ |
| 7105 | if (!c->u->u_firstlineno) { |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 7106 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7107 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7108 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7109 | c->u->u_firstlineno = 1; |
| 7110 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7112 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 7113 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7114 | a.a_entry = entryblock; |
| 7115 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7116 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7117 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 7118 | if (consts == NULL) { |
| 7119 | goto error; |
| 7120 | } |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7121 | |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7122 | if (optimize_cfg(c, &a, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7123 | goto error; |
| 7124 | } |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7125 | if (duplicate_exits_without_lineno(c)) { |
| 7126 | return NULL; |
| 7127 | } |
| 7128 | propagate_line_numbers(&a); |
Mark Shannon | 0acdf25 | 2021-05-13 14:11:41 +0100 | [diff] [blame] | 7129 | guarantee_lineno_for_exits(&a, c->u->u_firstlineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7130 | /* Can't modify the bytecode after computing jump offsets. */ |
| 7131 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 7132 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7133 | /* Emit code. */ |
| 7134 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7135 | for (j = 0; j < b->b_iused; j++) |
| 7136 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 7137 | goto error; |
| 7138 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7139 | if (!assemble_line_range(&a)) { |
| 7140 | return 0; |
| 7141 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 7142 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 7143 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7144 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 7145 | } |
| 7146 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7147 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 7148 | } |
| 7149 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 7150 | goto error; |
| 7151 | } |
| 7152 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 7153 | goto error; |
| 7154 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7155 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7156 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7157 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7158 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7159 | assemble_free(&a); |
| 7160 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 7161 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 7162 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7163 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 7164 | with LOAD_CONST (c1, c2, ... cn). |
| 7165 | The consts table must still be in list form so that the |
| 7166 | new constant (c1, c2, ... cn) can be appended. |
| 7167 | Called with codestr pointing to the first LOAD_CONST. |
| 7168 | */ |
| 7169 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7170 | fold_tuple_on_constants(struct compiler *c, |
| 7171 | struct instr *inst, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7172 | int n, PyObject *consts) |
| 7173 | { |
| 7174 | /* Pre-conditions */ |
| 7175 | assert(PyList_CheckExact(consts)); |
| 7176 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 7177 | assert(inst[n].i_oparg == n); |
| 7178 | |
| 7179 | for (int i = 0; i < n; i++) { |
| 7180 | if (inst[i].i_opcode != LOAD_CONST) { |
| 7181 | return 0; |
| 7182 | } |
| 7183 | } |
| 7184 | |
| 7185 | /* Buildup new tuple of constants */ |
| 7186 | PyObject *newconst = PyTuple_New(n); |
| 7187 | if (newconst == NULL) { |
| 7188 | return -1; |
| 7189 | } |
| 7190 | for (int i = 0; i < n; i++) { |
| 7191 | int arg = inst[i].i_oparg; |
| 7192 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 7193 | Py_INCREF(constant); |
| 7194 | PyTuple_SET_ITEM(newconst, i, constant); |
| 7195 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7196 | if (merge_const_one(c, &newconst) == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7197 | Py_DECREF(newconst); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7198 | return -1; |
| 7199 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7200 | |
| 7201 | Py_ssize_t index; |
| 7202 | for (index = 0; index < PyList_GET_SIZE(consts); index++) { |
| 7203 | if (PyList_GET_ITEM(consts, index) == newconst) { |
| 7204 | break; |
| 7205 | } |
| 7206 | } |
| 7207 | if (index == PyList_GET_SIZE(consts)) { |
| 7208 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
| 7209 | Py_DECREF(newconst); |
| 7210 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 7211 | return -1; |
| 7212 | } |
| 7213 | if (PyList_Append(consts, newconst)) { |
| 7214 | Py_DECREF(newconst); |
| 7215 | return -1; |
| 7216 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7217 | } |
| 7218 | Py_DECREF(newconst); |
| 7219 | for (int i = 0; i < n; i++) { |
| 7220 | inst[i].i_opcode = NOP; |
| 7221 | } |
| 7222 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 7223 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7224 | return 0; |
| 7225 | } |
| 7226 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7227 | |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 7228 | // Eliminate n * ROT_N(n). |
| 7229 | static void |
| 7230 | fold_rotations(struct instr *inst, int n) |
| 7231 | { |
| 7232 | for (int i = 0; i < n; i++) { |
| 7233 | int rot; |
| 7234 | switch (inst[i].i_opcode) { |
| 7235 | case ROT_N: |
| 7236 | rot = inst[i].i_oparg; |
| 7237 | break; |
| 7238 | case ROT_FOUR: |
| 7239 | rot = 4; |
| 7240 | break; |
| 7241 | case ROT_THREE: |
| 7242 | rot = 3; |
| 7243 | break; |
| 7244 | case ROT_TWO: |
| 7245 | rot = 2; |
| 7246 | break; |
| 7247 | default: |
| 7248 | return; |
| 7249 | } |
| 7250 | if (rot != n) { |
| 7251 | return; |
| 7252 | } |
| 7253 | } |
| 7254 | for (int i = 0; i < n; i++) { |
| 7255 | inst[i].i_opcode = NOP; |
| 7256 | } |
| 7257 | } |
| 7258 | |
| 7259 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7260 | static int |
| 7261 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 7262 | assert (bb->b_iused > 0); |
| 7263 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 7264 | assert (is_jump(inst)); |
| 7265 | assert (inst->i_target->b_iused > 0); |
| 7266 | struct instr *target = &inst->i_target->b_instr[0]; |
| 7267 | if (inst->i_target == target->i_target) { |
| 7268 | /* Nothing to do */ |
| 7269 | return 0; |
| 7270 | } |
| 7271 | int lineno = target->i_lineno; |
| 7272 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 7273 | return -1; |
| 7274 | } |
| 7275 | assert (bb->b_iused >= 2); |
| 7276 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 7277 | return 0; |
| 7278 | } |
| 7279 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7280 | /* Maximum size of basic block that should be copied in optimizer */ |
| 7281 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7282 | |
| 7283 | /* Optimization */ |
| 7284 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7285 | optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7286 | { |
| 7287 | assert(PyList_CheckExact(consts)); |
| 7288 | struct instr nop; |
| 7289 | nop.i_opcode = NOP; |
| 7290 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7291 | for (int i = 0; i < bb->b_iused; i++) { |
| 7292 | struct instr *inst = &bb->b_instr[i]; |
| 7293 | int oparg = inst->i_oparg; |
| 7294 | 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] | 7295 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7296 | /* Skip over empty basic blocks. */ |
| 7297 | while (inst->i_target->b_iused == 0) { |
| 7298 | inst->i_target = inst->i_target->b_next; |
| 7299 | } |
| 7300 | target = &inst->i_target->b_instr[0]; |
| 7301 | } |
| 7302 | else { |
| 7303 | target = &nop; |
| 7304 | } |
| 7305 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7306 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7307 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7308 | { |
| 7309 | PyObject* cnt; |
| 7310 | int is_true; |
| 7311 | int jump_if_true; |
| 7312 | switch(nextop) { |
| 7313 | case POP_JUMP_IF_FALSE: |
| 7314 | case POP_JUMP_IF_TRUE: |
| 7315 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7316 | is_true = PyObject_IsTrue(cnt); |
| 7317 | if (is_true == -1) { |
| 7318 | goto error; |
| 7319 | } |
| 7320 | inst->i_opcode = NOP; |
| 7321 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 7322 | if (is_true == jump_if_true) { |
| 7323 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7324 | bb->b_nofallthrough = 1; |
| 7325 | } |
| 7326 | else { |
| 7327 | bb->b_instr[i+1].i_opcode = NOP; |
| 7328 | } |
| 7329 | break; |
| 7330 | case JUMP_IF_FALSE_OR_POP: |
| 7331 | case JUMP_IF_TRUE_OR_POP: |
| 7332 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7333 | is_true = PyObject_IsTrue(cnt); |
| 7334 | if (is_true == -1) { |
| 7335 | goto error; |
| 7336 | } |
| 7337 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 7338 | if (is_true == jump_if_true) { |
| 7339 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7340 | bb->b_nofallthrough = 1; |
| 7341 | } |
| 7342 | else { |
| 7343 | inst->i_opcode = NOP; |
| 7344 | bb->b_instr[i+1].i_opcode = NOP; |
| 7345 | } |
| 7346 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7347 | } |
| 7348 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7349 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7350 | |
| 7351 | /* Try to fold tuples of constants. |
| 7352 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 7353 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 7354 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 7355 | case BUILD_TUPLE: |
| 7356 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 7357 | switch(oparg) { |
| 7358 | case 1: |
| 7359 | inst->i_opcode = NOP; |
| 7360 | bb->b_instr[i+1].i_opcode = NOP; |
| 7361 | break; |
| 7362 | case 2: |
| 7363 | inst->i_opcode = ROT_TWO; |
| 7364 | bb->b_instr[i+1].i_opcode = NOP; |
| 7365 | break; |
| 7366 | case 3: |
| 7367 | inst->i_opcode = ROT_THREE; |
| 7368 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 7369 | } |
| 7370 | break; |
| 7371 | } |
| 7372 | if (i >= oparg) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7373 | if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7374 | goto error; |
| 7375 | } |
| 7376 | } |
| 7377 | break; |
| 7378 | |
| 7379 | /* Simplify conditional jump to conditional jump where the |
| 7380 | result of the first test implies the success of a similar |
| 7381 | test or the failure of the opposite test. |
| 7382 | Arises in code like: |
| 7383 | "a and b or c" |
| 7384 | "(a and b) and c" |
| 7385 | "(a or b) or c" |
| 7386 | "(a or b) and c" |
| 7387 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 7388 | --> x:JUMP_IF_FALSE_OR_POP z |
| 7389 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 7390 | --> x:POP_JUMP_IF_FALSE y+1 |
| 7391 | where y+1 is the instruction following the second test. |
| 7392 | */ |
| 7393 | case JUMP_IF_FALSE_OR_POP: |
| 7394 | switch(target->i_opcode) { |
| 7395 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7396 | if (inst->i_lineno == target->i_lineno) { |
| 7397 | *inst = *target; |
| 7398 | i--; |
| 7399 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7400 | break; |
| 7401 | case JUMP_ABSOLUTE: |
| 7402 | case JUMP_FORWARD: |
| 7403 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7404 | if (inst->i_lineno == target->i_lineno && |
| 7405 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7406 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7407 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7408 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7409 | break; |
| 7410 | case JUMP_IF_TRUE_OR_POP: |
| 7411 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7412 | if (inst->i_lineno == target->i_lineno) { |
| 7413 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 7414 | inst->i_target = inst->i_target->b_next; |
| 7415 | --i; |
| 7416 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7417 | break; |
| 7418 | } |
| 7419 | break; |
| 7420 | |
| 7421 | case JUMP_IF_TRUE_OR_POP: |
| 7422 | switch(target->i_opcode) { |
| 7423 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7424 | if (inst->i_lineno == target->i_lineno) { |
| 7425 | *inst = *target; |
| 7426 | i--; |
| 7427 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7428 | break; |
| 7429 | case JUMP_ABSOLUTE: |
| 7430 | case JUMP_FORWARD: |
| 7431 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7432 | if (inst->i_lineno == target->i_lineno && |
| 7433 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7434 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7435 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7436 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7437 | break; |
| 7438 | case JUMP_IF_FALSE_OR_POP: |
| 7439 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7440 | if (inst->i_lineno == target->i_lineno) { |
| 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; |
| 7448 | |
| 7449 | case POP_JUMP_IF_FALSE: |
| 7450 | switch(target->i_opcode) { |
| 7451 | case JUMP_ABSOLUTE: |
| 7452 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7453 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7454 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7455 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7456 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7457 | break; |
| 7458 | } |
| 7459 | break; |
| 7460 | |
| 7461 | case POP_JUMP_IF_TRUE: |
| 7462 | switch(target->i_opcode) { |
| 7463 | case JUMP_ABSOLUTE: |
| 7464 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7465 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7466 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7467 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7468 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7469 | break; |
| 7470 | } |
| 7471 | break; |
| 7472 | |
| 7473 | case JUMP_ABSOLUTE: |
| 7474 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7475 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7476 | switch(target->i_opcode) { |
| 7477 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7478 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7479 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7480 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7481 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7482 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7483 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7484 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7485 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7486 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7487 | break; |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7488 | } |
| 7489 | break; |
| 7490 | case FOR_ITER: |
| 7491 | assert (i == bb->b_iused-1); |
| 7492 | if (target->i_opcode == JUMP_FORWARD) { |
| 7493 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7494 | goto error; |
| 7495 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7496 | } |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 7497 | break; |
| 7498 | case ROT_N: |
| 7499 | switch (oparg) { |
| 7500 | case 0: |
| 7501 | case 1: |
| 7502 | inst->i_opcode = NOP; |
| 7503 | continue; |
| 7504 | case 2: |
| 7505 | inst->i_opcode = ROT_TWO; |
| 7506 | break; |
| 7507 | case 3: |
| 7508 | inst->i_opcode = ROT_THREE; |
| 7509 | break; |
| 7510 | case 4: |
| 7511 | inst->i_opcode = ROT_FOUR; |
| 7512 | break; |
| 7513 | } |
| 7514 | if (i >= oparg - 1) { |
| 7515 | fold_rotations(inst - oparg + 1, oparg); |
| 7516 | } |
| 7517 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7518 | } |
| 7519 | } |
| 7520 | return 0; |
| 7521 | error: |
| 7522 | return -1; |
| 7523 | } |
| 7524 | |
Mark Shannon | 37686f7 | 2021-07-16 11:49:10 +0100 | [diff] [blame] | 7525 | /* If this block ends with an unconditional jump to an exit block, |
| 7526 | * then remove the jump and extend this block with the target. |
| 7527 | */ |
| 7528 | static int |
| 7529 | extend_block(basicblock *bb) { |
| 7530 | if (bb->b_iused == 0) { |
| 7531 | return 0; |
| 7532 | } |
| 7533 | struct instr *last = &bb->b_instr[bb->b_iused-1]; |
| 7534 | if (last->i_opcode != JUMP_ABSOLUTE && last->i_opcode != JUMP_FORWARD) { |
| 7535 | return 0; |
| 7536 | } |
| 7537 | if (last->i_target->b_exit && last->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7538 | basicblock *to_copy = last->i_target; |
| 7539 | last->i_opcode = NOP; |
| 7540 | for (int i = 0; i < to_copy->b_iused; i++) { |
| 7541 | int index = compiler_next_instr(bb); |
| 7542 | if (index < 0) { |
| 7543 | return -1; |
| 7544 | } |
| 7545 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7546 | } |
| 7547 | bb->b_exit = 1; |
| 7548 | } |
| 7549 | return 0; |
| 7550 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7551 | |
| 7552 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7553 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7554 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7555 | int dest = 0; |
| 7556 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7557 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7558 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7559 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7560 | if (lineno < 0) { |
| 7561 | continue; |
| 7562 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7563 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7564 | if (prev_lineno == lineno) { |
| 7565 | continue; |
| 7566 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7567 | /* 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] | 7568 | if (src < bb->b_iused - 1) { |
| 7569 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7570 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7571 | bb->b_instr[src+1].i_lineno = lineno; |
| 7572 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7573 | } |
| 7574 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7575 | else { |
| 7576 | basicblock* next = bb->b_next; |
| 7577 | while (next && next->b_iused == 0) { |
| 7578 | next = next->b_next; |
| 7579 | } |
| 7580 | /* or if last instruction in BB and next BB has same line number */ |
| 7581 | if (next) { |
| 7582 | if (lineno == next->b_instr[0].i_lineno) { |
| 7583 | continue; |
| 7584 | } |
| 7585 | } |
| 7586 | } |
| 7587 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7588 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7589 | if (dest != src) { |
| 7590 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7591 | } |
| 7592 | dest++; |
| 7593 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7594 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7595 | assert(dest <= bb->b_iused); |
| 7596 | bb->b_iused = dest; |
| 7597 | } |
| 7598 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7599 | static int |
| 7600 | normalize_basic_block(basicblock *bb) { |
| 7601 | /* Mark blocks as exit and/or nofallthrough. |
| 7602 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7603 | for (int i = 0; i < bb->b_iused; i++) { |
| 7604 | switch(bb->b_instr[i].i_opcode) { |
| 7605 | case RETURN_VALUE: |
| 7606 | case RAISE_VARARGS: |
| 7607 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7608 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7609 | bb->b_nofallthrough = 1; |
| 7610 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7611 | case JUMP_ABSOLUTE: |
| 7612 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7613 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7614 | /* fall through */ |
| 7615 | case POP_JUMP_IF_FALSE: |
| 7616 | case POP_JUMP_IF_TRUE: |
| 7617 | case JUMP_IF_FALSE_OR_POP: |
| 7618 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7619 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7620 | if (i != bb->b_iused-1) { |
| 7621 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7622 | return -1; |
| 7623 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7624 | /* Skip over empty basic blocks. */ |
| 7625 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7626 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7627 | } |
| 7628 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7629 | } |
| 7630 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7631 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7632 | } |
| 7633 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7634 | static int |
| 7635 | mark_reachable(struct assembler *a) { |
| 7636 | basicblock **stack, **sp; |
| 7637 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7638 | if (stack == NULL) { |
| 7639 | return -1; |
| 7640 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7641 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7642 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7643 | while (sp > stack) { |
| 7644 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7645 | if (b->b_next && !b->b_nofallthrough) { |
| 7646 | if (b->b_next->b_predecessors == 0) { |
| 7647 | *sp++ = b->b_next; |
| 7648 | } |
| 7649 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7650 | } |
| 7651 | for (int i = 0; i < b->b_iused; i++) { |
| 7652 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7653 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7654 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7655 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7656 | *sp++ = target; |
| 7657 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7658 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7659 | } |
| 7660 | } |
| 7661 | } |
| 7662 | PyObject_Free(stack); |
| 7663 | return 0; |
| 7664 | } |
| 7665 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7666 | static void |
| 7667 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7668 | /* Eliminate empty blocks */ |
| 7669 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7670 | basicblock *next = b->b_next; |
| 7671 | if (next) { |
| 7672 | while (next->b_iused == 0 && next->b_next) { |
| 7673 | next = next->b_next; |
| 7674 | } |
| 7675 | b->b_next = next; |
| 7676 | } |
| 7677 | } |
| 7678 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7679 | if (b->b_iused == 0) { |
| 7680 | continue; |
| 7681 | } |
| 7682 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7683 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7684 | while (target->b_iused == 0) { |
| 7685 | target = target->b_next; |
| 7686 | } |
| 7687 | b->b_instr[b->b_iused-1].i_target = target; |
| 7688 | } |
| 7689 | } |
| 7690 | } |
| 7691 | |
| 7692 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7693 | /* 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] | 7694 | * then copy the line number. If a successor block has no line number, and only |
| 7695 | * one predecessor, then inherit the line number. |
| 7696 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7697 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7698 | * but has no impact on the generated line number events. |
| 7699 | */ |
| 7700 | static void |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7701 | propagate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7702 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7703 | if (b->b_iused == 0) { |
| 7704 | continue; |
| 7705 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7706 | int prev_lineno = -1; |
| 7707 | for (int i = 0; i < b->b_iused; i++) { |
| 7708 | if (b->b_instr[i].i_lineno < 0) { |
| 7709 | b->b_instr[i].i_lineno = prev_lineno; |
| 7710 | } |
| 7711 | else { |
| 7712 | prev_lineno = b->b_instr[i].i_lineno; |
| 7713 | } |
| 7714 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7715 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7716 | assert(b->b_next->b_iused); |
| 7717 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7718 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7719 | } |
| 7720 | } |
| 7721 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7722 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7723 | /* Note: Only actual jumps, not exception handlers */ |
| 7724 | case SETUP_ASYNC_WITH: |
| 7725 | case SETUP_WITH: |
| 7726 | case SETUP_FINALLY: |
| 7727 | continue; |
| 7728 | } |
| 7729 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7730 | if (target->b_predecessors == 1) { |
| 7731 | if (target->b_instr[0].i_lineno < 0) { |
| 7732 | target->b_instr[0].i_lineno = prev_lineno; |
| 7733 | } |
| 7734 | } |
| 7735 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7736 | } |
| 7737 | } |
| 7738 | |
| 7739 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7740 | The consts object should still be in list form to allow new constants |
| 7741 | to be appended. |
| 7742 | |
| 7743 | All transformations keep the code size the same or smaller. |
| 7744 | For those that reduce size, the gaps are initially filled with |
| 7745 | NOPs. Later those NOPs are removed. |
| 7746 | */ |
| 7747 | |
| 7748 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7749 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7750 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7751 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7752 | if (optimize_basic_block(c, b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7753 | return -1; |
| 7754 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7755 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7756 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7757 | } |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7758 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7759 | if (extend_block(b)) { |
| 7760 | return -1; |
| 7761 | } |
| 7762 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7763 | if (mark_reachable(a)) { |
| 7764 | return -1; |
| 7765 | } |
| 7766 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7767 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7768 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7769 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7770 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7771 | } |
| 7772 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7773 | basicblock *pred = NULL; |
| 7774 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7775 | int prev_lineno = -1; |
| 7776 | if (pred && pred->b_iused) { |
| 7777 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7778 | } |
| 7779 | clean_basic_block(b, prev_lineno); |
| 7780 | pred = b->b_nofallthrough ? NULL : b; |
| 7781 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7782 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7783 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7784 | block ends with a jump instruction, check if the next non-empty block |
| 7785 | reached through normal flow control is the target of that jump. If it |
| 7786 | is, then the jump instruction is redundant and can be deleted. |
| 7787 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7788 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7789 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7790 | if (b->b_iused > 0) { |
| 7791 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7792 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7793 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7794 | if (b_last_instr->i_target == b->b_next) { |
| 7795 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7796 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7797 | b_last_instr->i_opcode = NOP; |
| 7798 | clean_basic_block(b, -1); |
| 7799 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7800 | } |
| 7801 | } |
| 7802 | } |
| 7803 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7804 | if (maybe_empty_blocks) { |
| 7805 | eliminate_empty_basic_blocks(a->a_entry); |
| 7806 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7807 | return 0; |
| 7808 | } |
| 7809 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7810 | static inline int |
| 7811 | is_exit_without_lineno(basicblock *b) { |
| 7812 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7813 | } |
| 7814 | |
| 7815 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7816 | * after a frame terminates. It would be prohibitively expensive |
| 7817 | * to continuously update the f_lineno field at runtime, |
| 7818 | * so we make sure that all exiting instruction (raises and returns) |
| 7819 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7820 | * We can do this by duplicating the exit blocks without line number |
| 7821 | * so that none have more than one predecessor. We can then safely |
| 7822 | * copy the line number from the sole predecessor block. |
| 7823 | */ |
| 7824 | static int |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7825 | duplicate_exits_without_lineno(struct compiler *c) |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7826 | { |
| 7827 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7828 | */ |
| 7829 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7830 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7831 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7832 | /* Note: Only actual jumps, not exception handlers */ |
| 7833 | case SETUP_ASYNC_WITH: |
| 7834 | case SETUP_WITH: |
| 7835 | case SETUP_FINALLY: |
| 7836 | continue; |
| 7837 | } |
| 7838 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7839 | if (is_exit_without_lineno(target) && target->b_predecessors > 1) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7840 | basicblock *new_target = compiler_copy_block(c, target); |
| 7841 | if (new_target == NULL) { |
| 7842 | return -1; |
| 7843 | } |
| 7844 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7845 | b->b_instr[b->b_iused-1].i_target = new_target; |
Mark Shannon | 762ef85 | 2021-08-09 10:54:48 +0100 | [diff] [blame] | 7846 | target->b_predecessors--; |
| 7847 | new_target->b_predecessors = 1; |
| 7848 | new_target->b_next = target->b_next; |
| 7849 | target->b_next = new_target; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7850 | } |
| 7851 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7852 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7853 | /* Eliminate empty blocks */ |
| 7854 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7855 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7856 | b->b_next = b->b_next->b_next; |
| 7857 | } |
| 7858 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7859 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7860 | * fall through, and thus can only have a single predecessor */ |
| 7861 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7862 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7863 | if (is_exit_without_lineno(b->b_next)) { |
| 7864 | assert(b->b_next->b_iused > 0); |
| 7865 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7866 | } |
| 7867 | } |
| 7868 | } |
| 7869 | return 0; |
| 7870 | } |
| 7871 | |
| 7872 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7873 | /* Retained for API compatibility. |
| 7874 | * Optimization is now done in optimize_cfg */ |
| 7875 | |
| 7876 | PyObject * |
| 7877 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7878 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7879 | { |
| 7880 | Py_INCREF(code); |
| 7881 | return code; |
| 7882 | } |