Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
| 3 | * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 4 | * The primary entry point is _PyAST_Compile(), which returns a |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5 | * PyCodeObject. The compiler makes several passes to build the code |
| 6 | * object: |
| 7 | * 1. Checks for future statements. See future.c |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | * 2. Builds a symbol table. See symtable.c. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | * 3. Generate code for basic blocks. See compiler_mod() in this file. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | * 4. Assemble the basic blocks into final code. See assemble() in |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | * this file. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 12 | * 5. Optimize the byte code (peephole optimizations). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | * |
| 14 | * Note that compiler_mod() suggests module, but the module ast type |
| 15 | * (mod_ty) has cases for expressions and interactive statements. |
Nick Coghlan | 944d3eb | 2005-11-16 12:46:55 +0000 | [diff] [blame] | 16 | * |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 17 | * CAUTION: The VISIT_* macros abort the current function when they |
| 18 | * encounter a problem. So don't invoke them when there is memory |
| 19 | * which needs to be released. Code blocks are OK, as the compiler |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | * structure takes care of releasing those. Use the arena to manage |
| 21 | * objects. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 24 | #include "Python.h" |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 25 | #include "pycore_ast.h" // _PyAST_GetDocString() |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 26 | #include "pycore_compile.h" // _PyFuture_FromAST() |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 27 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 28 | #include "pycore_long.h" // _PyLong_GetZero() |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 29 | #include "pycore_symtable.h" // PySTEntryObject |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 31 | #define NEED_OPCODE_JUMP_TABLES |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame] | 32 | #include "opcode.h" // EXTENDED_ARG |
| 33 | #include "wordcode_helpers.h" // instrsize() |
| 34 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 35 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 36 | #define DEFAULT_BLOCK_SIZE 16 |
| 37 | #define DEFAULT_BLOCKS 8 |
| 38 | #define DEFAULT_CODE_SIZE 128 |
| 39 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 40 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 41 | #define COMP_GENEXP 0 |
| 42 | #define COMP_LISTCOMP 1 |
| 43 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 44 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 45 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 46 | /* A soft limit for stack use, to avoid excessive |
| 47 | * memory use for large constants, etc. |
| 48 | * |
| 49 | * The value 30 is plucked out of thin air. |
| 50 | * Code that could use more stack than this is |
| 51 | * rare, so the exact value is unimportant. |
| 52 | */ |
| 53 | #define STACK_USE_GUIDELINE 30 |
| 54 | |
| 55 | /* If we exceed this limit, it should |
| 56 | * be considered a compiler bug. |
| 57 | * Currently it should be impossible |
| 58 | * to exceed STACK_USE_GUIDELINE * 100, |
| 59 | * as 100 is the maximum parse depth. |
| 60 | * For performance reasons we will |
| 61 | * want to reduce this to a |
| 62 | * few hundred in the future. |
| 63 | * |
| 64 | * NOTE: Whatever MAX_ALLOWED_STACK_USE is |
| 65 | * set to, it should never restrict what Python |
| 66 | * we can write, just how we compile it. |
| 67 | */ |
| 68 | #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100) |
| 69 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 70 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 71 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 72 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 73 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 74 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | unsigned char i_opcode; |
| 76 | int i_oparg; |
| 77 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 78 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 81 | #define LOG_BITS_PER_INT 5 |
| 82 | #define MASK_LOW_LOG_BITS 31 |
| 83 | |
| 84 | static inline int |
| 85 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 86 | /* Is the relevant bit set in the relevant word? */ |
| 87 | /* 256 bits fit into 8 32-bits words. |
| 88 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 89 | * Bit within word is the low bits of bitindex. |
| 90 | */ |
| 91 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 92 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 93 | } |
| 94 | |
| 95 | static inline int |
| 96 | is_relative_jump(struct instr *i) |
| 97 | { |
| 98 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 99 | } |
| 100 | |
| 101 | static inline int |
| 102 | is_jump(struct instr *i) |
| 103 | { |
| 104 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 105 | } |
| 106 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 107 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 109 | reverse order that the block are allocated. b_list points to the next |
| 110 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | struct basicblock_ *b_list; |
| 112 | /* number of instructions used */ |
| 113 | int b_iused; |
| 114 | /* length of instruction array (b_instr) */ |
| 115 | int b_ialloc; |
| 116 | /* pointer to an array of instructions, initially NULL */ |
| 117 | struct instr *b_instr; |
| 118 | /* If b_next is non-NULL, it is a pointer to the next |
| 119 | block reached by normal control flow. */ |
| 120 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 122 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 123 | /* Number of predecssors that a block has. */ |
| 124 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 125 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 126 | unsigned b_nofallthrough : 1; |
| 127 | /* Basic block exits scope (it ends with a return or raise) */ |
| 128 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 130 | int b_startdepth; |
| 131 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 132 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 133 | } basicblock; |
| 134 | |
| 135 | /* fblockinfo tracks the current frame block. |
| 136 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 137 | A frame block is used to handle loops, try/except, and try/finally. |
| 138 | It's called a frame block to distinguish it from a basic block in the |
| 139 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 140 | */ |
| 141 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 142 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 143 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER, |
| 144 | ASYNC_COMPREHENSION_GENERATOR }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 145 | |
| 146 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | enum fblocktype fb_type; |
| 148 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 149 | /* (optional) type-specific exit or cleanup block */ |
| 150 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 151 | /* (optional) additional information required for unwinding */ |
| 152 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 155 | enum { |
| 156 | COMPILER_SCOPE_MODULE, |
| 157 | COMPILER_SCOPE_CLASS, |
| 158 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 159 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 160 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 161 | COMPILER_SCOPE_COMPREHENSION, |
| 162 | }; |
| 163 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | /* The following items change on entry and exit of code blocks. |
| 165 | They must be saved and restored when returning to a block. |
| 166 | */ |
| 167 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 171 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 172 | int u_scope_type; |
| 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | /* The following fields are dicts that map objects to |
| 175 | the index of them in co_XXX. The index is used as |
| 176 | the argument for opcodes that refer to those collections. |
| 177 | */ |
| 178 | PyObject *u_consts; /* all constants */ |
| 179 | PyObject *u_names; /* all names */ |
| 180 | PyObject *u_varnames; /* local variables */ |
| 181 | PyObject *u_cellvars; /* cell variables */ |
| 182 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 186 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 187 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 188 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* Pointer to the most recently allocated block. By following b_list |
| 190 | members, you can reach all early allocated blocks. */ |
| 191 | basicblock *u_blocks; |
| 192 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | int u_nfblocks; |
| 195 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | int u_firstlineno; /* the first lineno of the block */ |
| 198 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 199 | int u_col_offset; /* the offset of the current stmt */ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 200 | int u_end_lineno; /* the end line of the current stmt */ |
| 201 | int u_end_col_offset; /* the end offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 205 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 206 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | for enclosing blocks are stored in c_stack. The u and c_stack are |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 208 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 209 | |
| 210 | Note that we don't track recursion levels during compilation - the |
| 211 | task of detecting and rejecting excessive levels of nesting is |
| 212 | handled by the symbol analysis pass. |
| 213 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | */ |
| 215 | |
| 216 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 217 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | struct symtable *c_st; |
| 219 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 220 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 222 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | int c_interactive; /* true if in interactive mode */ |
| 224 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 225 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 226 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | struct compiler_unit *u; /* compiler state for current block */ |
| 228 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 229 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 232 | typedef struct { |
| 233 | PyObject *stores; |
| 234 | int allow_irrefutable; |
| 235 | } pattern_context; |
| 236 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 237 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 238 | static void compiler_free(struct compiler *); |
| 239 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 240 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 241 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 242 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 243 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 244 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 245 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 246 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 247 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 248 | |
| 249 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 250 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 251 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 252 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 253 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 254 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 255 | static int compiler_subscript(struct compiler *, expr_ty); |
| 256 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 257 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 258 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 259 | static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 260 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 261 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 262 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 263 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 264 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 265 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 266 | asdl_expr_seq *args, |
| 267 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 268 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 269 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 270 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 271 | static int compiler_sync_comprehension_generator( |
| 272 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 273 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 274 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 275 | expr_ty elt, expr_ty val, int type); |
| 276 | |
| 277 | static int compiler_async_comprehension_generator( |
| 278 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 279 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 280 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 281 | expr_ty elt, expr_ty val, int type); |
| 282 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 283 | static int compiler_pattern(struct compiler *, expr_ty, pattern_context *); |
| 284 | static int compiler_match(struct compiler *, stmt_ty); |
| 285 | static int compiler_pattern_subpattern(struct compiler *, expr_ty, |
| 286 | pattern_context *); |
| 287 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 288 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 289 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 290 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 291 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 292 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 293 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 294 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | /* Name mangling: __private becomes _classname__private. |
| 297 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 298 | PyObject *result; |
| 299 | size_t nlen, plen, ipriv; |
| 300 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 302 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 303 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | Py_INCREF(ident); |
| 305 | return ident; |
| 306 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 307 | nlen = PyUnicode_GET_LENGTH(ident); |
| 308 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | The only time a name with a dot can occur is when |
| 312 | we are compiling an import statement that has a |
| 313 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | TODO(jhylton): Decide whether we want to support |
| 316 | mangling of the module name, e.g. __M.X. |
| 317 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 318 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 319 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 320 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | Py_INCREF(ident); |
| 322 | return ident; /* Don't mangle __whatever__ */ |
| 323 | } |
| 324 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 325 | ipriv = 0; |
| 326 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 327 | ipriv++; |
| 328 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | Py_INCREF(ident); |
| 330 | return ident; /* Don't mangle if class is just underscores */ |
| 331 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 332 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 334 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 335 | PyErr_SetString(PyExc_OverflowError, |
| 336 | "private identifier too large to be mangled"); |
| 337 | return NULL; |
| 338 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 339 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 340 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 341 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 342 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 343 | |
| 344 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 345 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 347 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 348 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 349 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 350 | Py_DECREF(result); |
| 351 | return NULL; |
| 352 | } |
| 353 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 354 | Py_DECREF(result); |
| 355 | return NULL; |
| 356 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 357 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 358 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | static int |
| 362 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 365 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 366 | c->c_const_cache = PyDict_New(); |
| 367 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | c->c_stack = PyList_New(0); |
| 372 | if (!c->c_stack) { |
| 373 | Py_CLEAR(c->c_const_cache); |
| 374 | return 0; |
| 375 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | PyCodeObject * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 381 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 382 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | struct compiler c; |
| 385 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 386 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (!__doc__) { |
| 390 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 391 | if (!__doc__) |
| 392 | return NULL; |
| 393 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 394 | if (!__annotations__) { |
| 395 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 396 | if (!__annotations__) |
| 397 | return NULL; |
| 398 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | if (!compiler_init(&c)) |
| 400 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 401 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | c.c_filename = filename; |
| 403 | c.c_arena = arena; |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 404 | c.c_future = _PyFuture_FromAST(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | if (c.c_future == NULL) |
| 406 | goto finally; |
| 407 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | flags = &local_flags; |
| 409 | } |
| 410 | merged = c.c_future->ff_features | flags->cf_flags; |
| 411 | c.c_future->ff_features = merged; |
| 412 | flags->cf_flags = merged; |
| 413 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 414 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 416 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 417 | _PyASTOptimizeState state; |
| 418 | state.optimize = c.c_optimize; |
| 419 | state.ff_features = merged; |
| 420 | |
| 421 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 422 | goto finally; |
| 423 | } |
| 424 | |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 425 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (c.c_st == NULL) { |
| 427 | if (!PyErr_Occurred()) |
| 428 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 429 | goto finally; |
| 430 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 433 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 434 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | compiler_free(&c); |
| 436 | assert(co || PyErr_Occurred()); |
| 437 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 440 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 441 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | if (c->c_st) |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 444 | _PySymtable_Free(c->c_st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | if (c->c_future) |
| 446 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 447 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 448 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 452 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 453 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | Py_ssize_t i, n; |
| 456 | PyObject *v, *k; |
| 457 | PyObject *dict = PyDict_New(); |
| 458 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | n = PyList_Size(list); |
| 461 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 462 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | if (!v) { |
| 464 | Py_DECREF(dict); |
| 465 | return NULL; |
| 466 | } |
| 467 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 468 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | Py_DECREF(v); |
| 470 | Py_DECREF(dict); |
| 471 | return NULL; |
| 472 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | Py_DECREF(v); |
| 474 | } |
| 475 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /* Return new dict containing names from src that match scope(s). |
| 479 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 480 | src is a symbol table dictionary. If the scope of a name matches |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | either scope_type or flag is set, insert it into the new dict. The |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 482 | values are integers, starting at offset and increasing by one for |
| 483 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 484 | */ |
| 485 | |
| 486 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 487 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 488 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 489 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 491 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | assert(offset >= 0); |
| 494 | if (dest == NULL) |
| 495 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 496 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 497 | /* Sort the keys so that we have a deterministic order on the indexes |
| 498 | saved in the returned dictionary. These indexes are used as indexes |
| 499 | into the free and cell var storage. Therefore if they aren't |
| 500 | deterministic, then the generated bytecode is not deterministic. |
| 501 | */ |
| 502 | sorted_keys = PyDict_Keys(src); |
| 503 | if (sorted_keys == NULL) |
| 504 | return NULL; |
| 505 | if (PyList_Sort(sorted_keys) != 0) { |
| 506 | Py_DECREF(sorted_keys); |
| 507 | return NULL; |
| 508 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 509 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 510 | |
| 511 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | /* XXX this should probably be a macro in symtable.h */ |
| 513 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 514 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 515 | v = PyDict_GetItemWithError(src, k); |
| 516 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | vi = PyLong_AS_LONG(v); |
| 518 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 521 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 523 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | Py_DECREF(dest); |
| 525 | return NULL; |
| 526 | } |
| 527 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 528 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 529 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | Py_DECREF(item); |
| 531 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | return NULL; |
| 533 | } |
| 534 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 537 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 541 | static void |
| 542 | compiler_unit_check(struct compiler_unit *u) |
| 543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | basicblock *block; |
| 545 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 546 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | if (block->b_instr != NULL) { |
| 548 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 549 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | assert(block->b_ialloc >= block->b_iused); |
| 551 | } |
| 552 | else { |
| 553 | assert (block->b_iused == 0); |
| 554 | assert (block->b_ialloc == 0); |
| 555 | } |
| 556 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static void |
| 560 | compiler_unit_free(struct compiler_unit *u) |
| 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | compiler_unit_check(u); |
| 565 | b = u->u_blocks; |
| 566 | while (b != NULL) { |
| 567 | if (b->b_instr) |
| 568 | PyObject_Free((void *)b->b_instr); |
| 569 | next = b->b_list; |
| 570 | PyObject_Free((void *)b); |
| 571 | b = next; |
| 572 | } |
| 573 | Py_CLEAR(u->u_ste); |
| 574 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 575 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | Py_CLEAR(u->u_consts); |
| 577 | Py_CLEAR(u->u_names); |
| 578 | Py_CLEAR(u->u_varnames); |
| 579 | Py_CLEAR(u->u_freevars); |
| 580 | Py_CLEAR(u->u_cellvars); |
| 581 | Py_CLEAR(u->u_private); |
| 582 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 586 | compiler_enter_scope(struct compiler *c, identifier name, |
| 587 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 590 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 591 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 592 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | struct compiler_unit)); |
| 594 | if (!u) { |
| 595 | PyErr_NoMemory(); |
| 596 | return 0; |
| 597 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 598 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 600 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | u->u_kwonlyargcount = 0; |
| 602 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 603 | if (!u->u_ste) { |
| 604 | compiler_unit_free(u); |
| 605 | return 0; |
| 606 | } |
| 607 | Py_INCREF(name); |
| 608 | u->u_name = name; |
| 609 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 610 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 611 | if (!u->u_varnames || !u->u_cellvars) { |
| 612 | compiler_unit_free(u); |
| 613 | return 0; |
| 614 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 615 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 616 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 617 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 618 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 619 | int res; |
| 620 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 621 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 622 | name = _PyUnicode_FromId(&PyId___class__); |
| 623 | if (!name) { |
| 624 | compiler_unit_free(u); |
| 625 | return 0; |
| 626 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 627 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 628 | if (res < 0) { |
| 629 | compiler_unit_free(u); |
| 630 | return 0; |
| 631 | } |
| 632 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 635 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (!u->u_freevars) { |
| 637 | compiler_unit_free(u); |
| 638 | return 0; |
| 639 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | u->u_blocks = NULL; |
| 642 | u->u_nfblocks = 0; |
| 643 | u->u_firstlineno = lineno; |
| 644 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 645 | u->u_col_offset = 0; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 646 | u->u_end_lineno = 0; |
| 647 | u->u_end_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | u->u_consts = PyDict_New(); |
| 649 | if (!u->u_consts) { |
| 650 | compiler_unit_free(u); |
| 651 | return 0; |
| 652 | } |
| 653 | u->u_names = PyDict_New(); |
| 654 | if (!u->u_names) { |
| 655 | compiler_unit_free(u); |
| 656 | return 0; |
| 657 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | /* Push the old compiler_unit on the stack. */ |
| 662 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 663 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 665 | Py_XDECREF(capsule); |
| 666 | compiler_unit_free(u); |
| 667 | return 0; |
| 668 | } |
| 669 | Py_DECREF(capsule); |
| 670 | u->u_private = c->u->u_private; |
| 671 | Py_XINCREF(u->u_private); |
| 672 | } |
| 673 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 676 | |
| 677 | block = compiler_new_block(c); |
| 678 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 680 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 681 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 682 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 683 | if (!compiler_set_qualname(c)) |
| 684 | return 0; |
| 685 | } |
| 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 690 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 691 | compiler_exit_scope(struct compiler *c) |
| 692 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 693 | // Don't call PySequence_DelItem() with an exception raised |
| 694 | PyObject *exc_type, *exc_val, *exc_tb; |
| 695 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | c->c_nestlevel--; |
| 698 | compiler_unit_free(c->u); |
| 699 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 700 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 702 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 703 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | assert(c->u); |
| 705 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 706 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 707 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 708 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 709 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | compiler_unit_check(c->u); |
| 711 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 712 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 714 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 715 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 716 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 719 | static int |
| 720 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 721 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 722 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 723 | _Py_static_string(dot_locals, ".<locals>"); |
| 724 | Py_ssize_t stack_size; |
| 725 | struct compiler_unit *u = c->u; |
| 726 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 727 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 728 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 729 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 730 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 731 | if (stack_size > 1) { |
| 732 | int scope, force_global = 0; |
| 733 | struct compiler_unit *parent; |
| 734 | PyObject *mangled, *capsule; |
| 735 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 736 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 737 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 738 | assert(parent); |
| 739 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 740 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 741 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 742 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 743 | assert(u->u_name); |
| 744 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 745 | if (!mangled) |
| 746 | return 0; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 747 | scope = _PyST_GetScope(parent->u_ste, mangled); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 748 | Py_DECREF(mangled); |
| 749 | assert(scope != GLOBAL_IMPLICIT); |
| 750 | if (scope == GLOBAL_EXPLICIT) |
| 751 | force_global = 1; |
| 752 | } |
| 753 | |
| 754 | if (!force_global) { |
| 755 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 756 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 757 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 758 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 759 | if (dot_locals_str == NULL) |
| 760 | return 0; |
| 761 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 762 | if (base == NULL) |
| 763 | return 0; |
| 764 | } |
| 765 | else { |
| 766 | Py_INCREF(parent->u_qualname); |
| 767 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 768 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 769 | } |
| 770 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 771 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 772 | if (base != NULL) { |
| 773 | dot_str = _PyUnicode_FromId(&dot); |
| 774 | if (dot_str == NULL) { |
| 775 | Py_DECREF(base); |
| 776 | return 0; |
| 777 | } |
| 778 | name = PyUnicode_Concat(base, dot_str); |
| 779 | Py_DECREF(base); |
| 780 | if (name == NULL) |
| 781 | return 0; |
| 782 | PyUnicode_Append(&name, u->u_name); |
| 783 | if (name == NULL) |
| 784 | return 0; |
| 785 | } |
| 786 | else { |
| 787 | Py_INCREF(u->u_name); |
| 788 | name = u->u_name; |
| 789 | } |
| 790 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 791 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 792 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 795 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | /* Allocate a new block and return a pointer to it. |
| 797 | Returns NULL on error. |
| 798 | */ |
| 799 | |
| 800 | static basicblock * |
| 801 | compiler_new_block(struct compiler *c) |
| 802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | basicblock *b; |
| 804 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 807 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | if (b == NULL) { |
| 809 | PyErr_NoMemory(); |
| 810 | return NULL; |
| 811 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | /* Extend the singly linked list of blocks with new block. */ |
| 813 | b->b_list = u->u_blocks; |
| 814 | u->u_blocks = b; |
| 815 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 818 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | compiler_next_block(struct compiler *c) |
| 820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | basicblock *block = compiler_new_block(c); |
| 822 | if (block == NULL) |
| 823 | return NULL; |
| 824 | c->u->u_curblock->b_next = block; |
| 825 | c->u->u_curblock = block; |
| 826 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static basicblock * |
| 830 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | assert(block != NULL); |
| 833 | c->u->u_curblock->b_next = block; |
| 834 | c->u->u_curblock = block; |
| 835 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 838 | static basicblock * |
| 839 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 840 | { |
| 841 | /* Cannot copy a block if it has a fallthrough, since |
| 842 | * a block can only have one fallthrough predecessor. |
| 843 | */ |
| 844 | assert(block->b_nofallthrough); |
| 845 | basicblock *result = compiler_next_block(c); |
| 846 | if (result == NULL) { |
| 847 | return NULL; |
| 848 | } |
| 849 | for (int i = 0; i < block->b_iused; i++) { |
| 850 | int n = compiler_next_instr(result); |
| 851 | if (n < 0) { |
| 852 | return NULL; |
| 853 | } |
| 854 | result->b_instr[n] = block->b_instr[i]; |
| 855 | } |
| 856 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 857 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 858 | return result; |
| 859 | } |
| 860 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | /* Returns the offset of the next instruction in the current block's |
| 862 | b_instr array. Resizes the b_instr as necessary. |
| 863 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 864 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | |
| 866 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 867 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | assert(b != NULL); |
| 870 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 871 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 872 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | if (b->b_instr == NULL) { |
| 874 | PyErr_NoMemory(); |
| 875 | return -1; |
| 876 | } |
| 877 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | } |
| 879 | else if (b->b_iused == b->b_ialloc) { |
| 880 | struct instr *tmp; |
| 881 | size_t oldsize, newsize; |
| 882 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 883 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 884 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 885 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | PyErr_NoMemory(); |
| 887 | return -1; |
| 888 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | if (newsize == 0) { |
| 891 | PyErr_NoMemory(); |
| 892 | return -1; |
| 893 | } |
| 894 | b->b_ialloc <<= 1; |
| 895 | tmp = (struct instr *)PyObject_Realloc( |
| 896 | (void *)b->b_instr, newsize); |
| 897 | if (tmp == NULL) { |
| 898 | PyErr_NoMemory(); |
| 899 | return -1; |
| 900 | } |
| 901 | b->b_instr = tmp; |
| 902 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 903 | } |
| 904 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 907 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 908 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 909 | The line number is reset in the following cases: |
| 910 | - when entering a new scope |
| 911 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 912 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 913 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 914 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 915 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 916 | #define SET_LOC(c, x) \ |
| 917 | (c)->u->u_lineno = (x)->lineno; \ |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 918 | (c)->u->u_col_offset = (x)->col_offset; \ |
| 919 | (c)->u->u_end_lineno = (x)->end_lineno; \ |
| 920 | (c)->u->u_end_col_offset = (x)->end_col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 921 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 922 | /* Return the stack effect of opcode with argument oparg. |
| 923 | |
| 924 | Some opcodes have different stack effect when jump to the target and |
| 925 | when not jump. The 'jump' parameter specifies the case: |
| 926 | |
| 927 | * 0 -- when not jump |
| 928 | * 1 -- when jump |
| 929 | * -1 -- maximal |
| 930 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 931 | static int |
| 932 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 935 | case NOP: |
| 936 | case EXTENDED_ARG: |
| 937 | return 0; |
| 938 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 939 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | case POP_TOP: |
| 941 | return -1; |
| 942 | case ROT_TWO: |
| 943 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 944 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | return 0; |
| 946 | case DUP_TOP: |
| 947 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 948 | case DUP_TOP_TWO: |
| 949 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 950 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 951 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case UNARY_POSITIVE: |
| 953 | case UNARY_NEGATIVE: |
| 954 | case UNARY_NOT: |
| 955 | case UNARY_INVERT: |
| 956 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | case SET_ADD: |
| 959 | case LIST_APPEND: |
| 960 | return -1; |
| 961 | case MAP_ADD: |
| 962 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 963 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 964 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | case BINARY_POWER: |
| 966 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 967 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | case BINARY_MODULO: |
| 969 | case BINARY_ADD: |
| 970 | case BINARY_SUBTRACT: |
| 971 | case BINARY_SUBSCR: |
| 972 | case BINARY_FLOOR_DIVIDE: |
| 973 | case BINARY_TRUE_DIVIDE: |
| 974 | return -1; |
| 975 | case INPLACE_FLOOR_DIVIDE: |
| 976 | case INPLACE_TRUE_DIVIDE: |
| 977 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | case INPLACE_ADD: |
| 980 | case INPLACE_SUBTRACT: |
| 981 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 982 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | case INPLACE_MODULO: |
| 984 | return -1; |
| 985 | case STORE_SUBSCR: |
| 986 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | case DELETE_SUBSCR: |
| 988 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | case BINARY_LSHIFT: |
| 991 | case BINARY_RSHIFT: |
| 992 | case BINARY_AND: |
| 993 | case BINARY_XOR: |
| 994 | case BINARY_OR: |
| 995 | return -1; |
| 996 | case INPLACE_POWER: |
| 997 | return -1; |
| 998 | case GET_ITER: |
| 999 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case PRINT_EXPR: |
| 1002 | return -1; |
| 1003 | case LOAD_BUILD_CLASS: |
| 1004 | return 1; |
| 1005 | case INPLACE_LSHIFT: |
| 1006 | case INPLACE_RSHIFT: |
| 1007 | case INPLACE_AND: |
| 1008 | case INPLACE_XOR: |
| 1009 | case INPLACE_OR: |
| 1010 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1013 | /* 1 in the normal flow. |
| 1014 | * Restore the stack position and push 6 values before jumping to |
| 1015 | * the handler if an exception be raised. */ |
| 1016 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | case RETURN_VALUE: |
| 1018 | return -1; |
| 1019 | case IMPORT_STAR: |
| 1020 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1021 | case SETUP_ANNOTATIONS: |
| 1022 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | case YIELD_VALUE: |
| 1024 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1025 | case YIELD_FROM: |
| 1026 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case POP_BLOCK: |
| 1028 | return 0; |
| 1029 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1030 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | case STORE_NAME: |
| 1033 | return -1; |
| 1034 | case DELETE_NAME: |
| 1035 | return 0; |
| 1036 | case UNPACK_SEQUENCE: |
| 1037 | return oparg-1; |
| 1038 | case UNPACK_EX: |
| 1039 | return (oparg&0xFF) + (oparg>>8); |
| 1040 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1041 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1042 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | case STORE_ATTR: |
| 1045 | return -2; |
| 1046 | case DELETE_ATTR: |
| 1047 | return -1; |
| 1048 | case STORE_GLOBAL: |
| 1049 | return -1; |
| 1050 | case DELETE_GLOBAL: |
| 1051 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | case LOAD_CONST: |
| 1053 | return 1; |
| 1054 | case LOAD_NAME: |
| 1055 | return 1; |
| 1056 | case BUILD_TUPLE: |
| 1057 | case BUILD_LIST: |
| 1058 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1059 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | return 1-oparg; |
| 1061 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1062 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1063 | case BUILD_CONST_KEY_MAP: |
| 1064 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | case LOAD_ATTR: |
| 1066 | return 0; |
| 1067 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1068 | case IS_OP: |
| 1069 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1071 | case JUMP_IF_NOT_EXC_MATCH: |
| 1072 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | case IMPORT_NAME: |
| 1074 | return -1; |
| 1075 | case IMPORT_FROM: |
| 1076 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1077 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1078 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | case JUMP_ABSOLUTE: |
| 1081 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1082 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1083 | case JUMP_IF_TRUE_OR_POP: |
| 1084 | case JUMP_IF_FALSE_OR_POP: |
| 1085 | return jump ? 0 : -1; |
| 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | case POP_JUMP_IF_FALSE: |
| 1088 | case POP_JUMP_IF_TRUE: |
| 1089 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | case LOAD_GLOBAL: |
| 1092 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1093 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1094 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1096 | /* 0 in the normal flow. |
| 1097 | * Restore the stack position and push 6 values before jumping to |
| 1098 | * the handler if an exception be raised. */ |
| 1099 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1100 | case RERAISE: |
| 1101 | return -3; |
| 1102 | |
| 1103 | case WITH_EXCEPT_START: |
| 1104 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | case LOAD_FAST: |
| 1107 | return 1; |
| 1108 | case STORE_FAST: |
| 1109 | return -1; |
| 1110 | case DELETE_FAST: |
| 1111 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | case RAISE_VARARGS: |
| 1114 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1115 | |
| 1116 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1118 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1119 | case CALL_METHOD: |
| 1120 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1122 | return -oparg-1; |
| 1123 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1124 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1125 | case MAKE_FUNCTION: |
| 1126 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1127 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | case BUILD_SLICE: |
| 1129 | if (oparg == 3) |
| 1130 | return -2; |
| 1131 | else |
| 1132 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1133 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1134 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | case LOAD_CLOSURE: |
| 1136 | return 1; |
| 1137 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1138 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | return 1; |
| 1140 | case STORE_DEREF: |
| 1141 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1142 | case DELETE_DEREF: |
| 1143 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1144 | |
| 1145 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1146 | case GET_AWAITABLE: |
| 1147 | return 0; |
| 1148 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1149 | /* 0 in the normal flow. |
| 1150 | * Restore the stack position to the position before the result |
| 1151 | * of __aenter__ and push 6 values before jumping to the handler |
| 1152 | * if an exception be raised. */ |
| 1153 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1154 | case BEFORE_ASYNC_WITH: |
| 1155 | return 1; |
| 1156 | case GET_AITER: |
| 1157 | return 0; |
| 1158 | case GET_ANEXT: |
| 1159 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1160 | case GET_YIELD_FROM_ITER: |
| 1161 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1162 | case END_ASYNC_FOR: |
| 1163 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1164 | case FORMAT_VALUE: |
| 1165 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1166 | else 1->1. */ |
| 1167 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1168 | case LOAD_METHOD: |
| 1169 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1170 | case LOAD_ASSERTION_ERROR: |
| 1171 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1172 | case LIST_TO_TUPLE: |
| 1173 | return 0; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 1174 | case GEN_START: |
| 1175 | return -1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1176 | case LIST_EXTEND: |
| 1177 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1178 | case DICT_MERGE: |
| 1179 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1180 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1181 | case COPY_DICT_WITHOUT_KEYS: |
| 1182 | return 0; |
| 1183 | case MATCH_CLASS: |
| 1184 | return -1; |
| 1185 | case GET_LEN: |
| 1186 | case MATCH_MAPPING: |
| 1187 | case MATCH_SEQUENCE: |
| 1188 | return 1; |
| 1189 | case MATCH_KEYS: |
| 1190 | return 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1192 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1194 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1197 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1198 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1199 | { |
| 1200 | return stack_effect(opcode, oparg, jump); |
| 1201 | } |
| 1202 | |
| 1203 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1204 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1205 | { |
| 1206 | return stack_effect(opcode, oparg, -1); |
| 1207 | } |
| 1208 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1209 | /* Add an opcode with no argument. |
| 1210 | Returns 0 on failure, 1 on success. |
| 1211 | */ |
| 1212 | |
| 1213 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1214 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1215 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | basicblock *b; |
| 1217 | struct instr *i; |
| 1218 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1219 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1220 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | if (off < 0) |
| 1222 | return 0; |
| 1223 | b = c->u->u_curblock; |
| 1224 | i = &b->b_instr[off]; |
| 1225 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1226 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | if (opcode == RETURN_VALUE) |
| 1228 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1229 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1233 | static int |
| 1234 | compiler_addop(struct compiler *c, int opcode) |
| 1235 | { |
| 1236 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1237 | } |
| 1238 | |
| 1239 | static int |
| 1240 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1241 | { |
| 1242 | return compiler_addop_line(c, opcode, -1); |
| 1243 | } |
| 1244 | |
| 1245 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1246 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1247 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1248 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1249 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1251 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1252 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1254 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1256 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1257 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1258 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | return -1; |
| 1261 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1262 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1263 | Py_DECREF(v); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | Py_DECREF(v); |
| 1267 | } |
| 1268 | else |
| 1269 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1270 | return arg; |
| 1271 | } |
| 1272 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1273 | // Merge const *o* recursively and return constant key object. |
| 1274 | static PyObject* |
| 1275 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1276 | { |
| 1277 | // None and Ellipsis are singleton, and key is the singleton. |
| 1278 | // No need to merge object and key. |
| 1279 | if (o == Py_None || o == Py_Ellipsis) { |
| 1280 | Py_INCREF(o); |
| 1281 | return o; |
| 1282 | } |
| 1283 | |
| 1284 | PyObject *key = _PyCode_ConstantKey(o); |
| 1285 | if (key == NULL) { |
| 1286 | return NULL; |
| 1287 | } |
| 1288 | |
| 1289 | // t is borrowed reference |
| 1290 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1291 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1292 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1293 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1294 | Py_DECREF(key); |
| 1295 | return t; |
| 1296 | } |
| 1297 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1298 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1299 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1300 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1301 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1302 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1303 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1304 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1305 | PyObject *u = merge_consts_recursive(c, item); |
| 1306 | if (u == NULL) { |
| 1307 | Py_DECREF(key); |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | // See _PyCode_ConstantKey() |
| 1312 | PyObject *v; // borrowed |
| 1313 | if (PyTuple_CheckExact(u)) { |
| 1314 | v = PyTuple_GET_ITEM(u, 1); |
| 1315 | } |
| 1316 | else { |
| 1317 | v = u; |
| 1318 | } |
| 1319 | if (v != item) { |
| 1320 | Py_INCREF(v); |
| 1321 | PyTuple_SET_ITEM(o, i, v); |
| 1322 | Py_DECREF(item); |
| 1323 | } |
| 1324 | |
| 1325 | Py_DECREF(u); |
| 1326 | } |
| 1327 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1328 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1329 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1330 | // constant keys. |
| 1331 | // See _PyCode_ConstantKey() for detail. |
| 1332 | assert(PyTuple_CheckExact(key)); |
| 1333 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1334 | |
| 1335 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1336 | if (len == 0) { // empty frozenset should not be re-created. |
| 1337 | return key; |
| 1338 | } |
| 1339 | PyObject *tuple = PyTuple_New(len); |
| 1340 | if (tuple == NULL) { |
| 1341 | Py_DECREF(key); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | Py_ssize_t i = 0, pos = 0; |
| 1345 | PyObject *item; |
| 1346 | Py_hash_t hash; |
| 1347 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1348 | PyObject *k = merge_consts_recursive(c, item); |
| 1349 | if (k == NULL) { |
| 1350 | Py_DECREF(tuple); |
| 1351 | Py_DECREF(key); |
| 1352 | return NULL; |
| 1353 | } |
| 1354 | PyObject *u; |
| 1355 | if (PyTuple_CheckExact(k)) { |
| 1356 | u = PyTuple_GET_ITEM(k, 1); |
| 1357 | Py_INCREF(u); |
| 1358 | Py_DECREF(k); |
| 1359 | } |
| 1360 | else { |
| 1361 | u = k; |
| 1362 | } |
| 1363 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1364 | i++; |
| 1365 | } |
| 1366 | |
| 1367 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1368 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1369 | PyObject *new = PyFrozenSet_New(tuple); |
| 1370 | Py_DECREF(tuple); |
| 1371 | if (new == NULL) { |
| 1372 | Py_DECREF(key); |
| 1373 | return NULL; |
| 1374 | } |
| 1375 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1376 | Py_DECREF(o); |
| 1377 | PyTuple_SET_ITEM(key, 1, new); |
| 1378 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1379 | |
| 1380 | return key; |
| 1381 | } |
| 1382 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1383 | static Py_ssize_t |
| 1384 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1385 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1386 | PyObject *key = merge_consts_recursive(c, o); |
| 1387 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1388 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1389 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1390 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1391 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1392 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1397 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1398 | { |
| 1399 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1400 | if (arg < 0) |
| 1401 | return 0; |
| 1402 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1403 | } |
| 1404 | |
| 1405 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1408 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1409 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1411 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1412 | return compiler_addop_i(c, opcode, arg); |
| 1413 | } |
| 1414 | |
| 1415 | static int |
| 1416 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1419 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1420 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1421 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1422 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1423 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1424 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1425 | Py_DECREF(mangled); |
| 1426 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1427 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | return compiler_addop_i(c, opcode, arg); |
| 1429 | } |
| 1430 | |
| 1431 | /* Add an opcode with an integer argument. |
| 1432 | Returns 0 on failure, 1 on success. |
| 1433 | */ |
| 1434 | |
| 1435 | static int |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1436 | compiler_addop_i_line(struct compiler *c, int opcode, Py_ssize_t oparg, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | struct instr *i; |
| 1439 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1440 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1441 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1442 | it in the C code (like Python/ceval.c). |
| 1443 | |
| 1444 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1445 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1446 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1447 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1448 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1449 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1450 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1451 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | if (off < 0) |
| 1453 | return 0; |
| 1454 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1455 | i->i_opcode = opcode; |
| 1456 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1457 | i->i_lineno = lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1461 | static int |
| 1462 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1463 | { |
| 1464 | return compiler_addop_i_line(c, opcode, oparg, c->u->u_lineno); |
| 1465 | } |
| 1466 | |
| 1467 | static int |
| 1468 | compiler_addop_i_noline(struct compiler *c, int opcode, Py_ssize_t oparg) |
| 1469 | { |
| 1470 | return compiler_addop_i_line(c, opcode, oparg, -1); |
| 1471 | } |
| 1472 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1473 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1474 | { |
| 1475 | assert(HAS_ARG(opcode)); |
| 1476 | assert(b != NULL); |
| 1477 | assert(target != NULL); |
| 1478 | |
| 1479 | int off = compiler_next_instr(b); |
| 1480 | struct instr *i = &b->b_instr[off]; |
| 1481 | if (off < 0) { |
| 1482 | return 0; |
| 1483 | } |
| 1484 | i->i_opcode = opcode; |
| 1485 | i->i_target = target; |
| 1486 | i->i_lineno = lineno; |
| 1487 | return 1; |
| 1488 | } |
| 1489 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1491 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1492 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1493 | return add_jump_to_block(c->u->u_curblock, opcode, c->u->u_lineno, b); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1496 | static int |
| 1497 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1498 | { |
| 1499 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1500 | } |
| 1501 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1502 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1503 | to the new block. |
| 1504 | |
| 1505 | The returns inside this macro make it impossible to decref objects |
| 1506 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1507 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | if (compiler_next_block((C)) == NULL) \ |
| 1510 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | if (!compiler_addop((C), (OP))) \ |
| 1515 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1518 | #define ADDOP_NOLINE(C, OP) { \ |
| 1519 | if (!compiler_addop_noline((C), (OP))) \ |
| 1520 | return 0; \ |
| 1521 | } |
| 1522 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1523 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | if (!compiler_addop((C), (OP))) { \ |
| 1525 | compiler_exit_scope(c); \ |
| 1526 | return 0; \ |
| 1527 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1530 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1531 | if (!compiler_addop_load_const((C), (O))) \ |
| 1532 | return 0; \ |
| 1533 | } |
| 1534 | |
| 1535 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1536 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1537 | PyObject *__new_const = (O); \ |
| 1538 | if (__new_const == NULL) { \ |
| 1539 | return 0; \ |
| 1540 | } \ |
| 1541 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1542 | Py_DECREF(__new_const); \ |
| 1543 | return 0; \ |
| 1544 | } \ |
| 1545 | Py_DECREF(__new_const); \ |
| 1546 | } |
| 1547 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1548 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1550 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1553 | /* Same as ADDOP_O, but steals a reference. */ |
| 1554 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1555 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1556 | Py_DECREF((O)); \ |
| 1557 | return 0; \ |
| 1558 | } \ |
| 1559 | Py_DECREF((O)); \ |
| 1560 | } |
| 1561 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1562 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1564 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1569 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 1572 | #define ADDOP_I_NOLINE(C, OP, O) { \ |
| 1573 | if (!compiler_addop_i_noline((C), (OP), (O))) \ |
| 1574 | return 0; \ |
| 1575 | } |
| 1576 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1577 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1578 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1582 | /* Add a jump with no line number. |
| 1583 | * Used for artificial jumps that have no corresponding |
| 1584 | * token in the source code. */ |
| 1585 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1586 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1587 | return 0; \ |
| 1588 | } |
| 1589 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1590 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1591 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1592 | return 0; \ |
| 1593 | } |
| 1594 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1595 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1596 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1597 | */ |
| 1598 | |
| 1599 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1601 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1604 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1606 | compiler_exit_scope(c); \ |
| 1607 | return 0; \ |
| 1608 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1611 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1613 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1618 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1620 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1621 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1622 | return 0; \ |
| 1623 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1626 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1628 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1630 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1631 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1632 | compiler_exit_scope(c); \ |
| 1633 | return 0; \ |
| 1634 | } \ |
| 1635 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1638 | #define RETURN_IF_FALSE(X) \ |
| 1639 | if (!(X)) { \ |
| 1640 | return 0; \ |
| 1641 | } |
| 1642 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1643 | /* Search if variable annotations are present statically in a block. */ |
| 1644 | |
| 1645 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1646 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1647 | { |
| 1648 | int i, j, res = 0; |
| 1649 | stmt_ty st; |
| 1650 | |
| 1651 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1652 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1653 | switch (st->kind) { |
| 1654 | case AnnAssign_kind: |
| 1655 | return 1; |
| 1656 | case For_kind: |
| 1657 | res = find_ann(st->v.For.body) || |
| 1658 | find_ann(st->v.For.orelse); |
| 1659 | break; |
| 1660 | case AsyncFor_kind: |
| 1661 | res = find_ann(st->v.AsyncFor.body) || |
| 1662 | find_ann(st->v.AsyncFor.orelse); |
| 1663 | break; |
| 1664 | case While_kind: |
| 1665 | res = find_ann(st->v.While.body) || |
| 1666 | find_ann(st->v.While.orelse); |
| 1667 | break; |
| 1668 | case If_kind: |
| 1669 | res = find_ann(st->v.If.body) || |
| 1670 | find_ann(st->v.If.orelse); |
| 1671 | break; |
| 1672 | case With_kind: |
| 1673 | res = find_ann(st->v.With.body); |
| 1674 | break; |
| 1675 | case AsyncWith_kind: |
| 1676 | res = find_ann(st->v.AsyncWith.body); |
| 1677 | break; |
| 1678 | case Try_kind: |
| 1679 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1680 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1681 | st->v.Try.handlers, j); |
| 1682 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1683 | return 1; |
| 1684 | } |
| 1685 | } |
| 1686 | res = find_ann(st->v.Try.body) || |
| 1687 | find_ann(st->v.Try.finalbody) || |
| 1688 | find_ann(st->v.Try.orelse); |
| 1689 | break; |
| 1690 | default: |
| 1691 | res = 0; |
| 1692 | } |
| 1693 | if (res) { |
| 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | return res; |
| 1698 | } |
| 1699 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1700 | /* |
| 1701 | * Frame block handling functions |
| 1702 | */ |
| 1703 | |
| 1704 | static int |
| 1705 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1706 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1707 | { |
| 1708 | struct fblockinfo *f; |
| 1709 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1710 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1711 | } |
| 1712 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1713 | f->fb_type = t; |
| 1714 | f->fb_block = b; |
| 1715 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1716 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1717 | return 1; |
| 1718 | } |
| 1719 | |
| 1720 | static void |
| 1721 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1722 | { |
| 1723 | struct compiler_unit *u = c->u; |
| 1724 | assert(u->u_nfblocks > 0); |
| 1725 | u->u_nfblocks--; |
| 1726 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1727 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1728 | } |
| 1729 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1730 | static int |
| 1731 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1732 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1733 | ADDOP(c, DUP_TOP); |
| 1734 | ADDOP(c, DUP_TOP); |
| 1735 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1736 | return 1; |
| 1737 | } |
| 1738 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1739 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1740 | * popping the blocks will be restored afterwards, unless another |
| 1741 | * return, break or continue is found. In which case, the TOS will |
| 1742 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1743 | */ |
| 1744 | static int |
| 1745 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1746 | int preserve_tos) |
| 1747 | { |
| 1748 | switch (info->fb_type) { |
| 1749 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1750 | case EXCEPTION_HANDLER: |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 1751 | case ASYNC_COMPREHENSION_GENERATOR: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1752 | return 1; |
| 1753 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1754 | case FOR_LOOP: |
| 1755 | /* Pop the iterator */ |
| 1756 | if (preserve_tos) { |
| 1757 | ADDOP(c, ROT_TWO); |
| 1758 | } |
| 1759 | ADDOP(c, POP_TOP); |
| 1760 | return 1; |
| 1761 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1762 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1763 | ADDOP(c, POP_BLOCK); |
| 1764 | return 1; |
| 1765 | |
| 1766 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1767 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1768 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1769 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1770 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1771 | return 0; |
| 1772 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1773 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1774 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1775 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1776 | if (preserve_tos) { |
| 1777 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1778 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1779 | /* The finally block should appear to execute after the |
| 1780 | * statement causing the unwinding, so make the unwinding |
| 1781 | * instruction artificial */ |
| 1782 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1783 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1784 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1785 | case FINALLY_END: |
| 1786 | if (preserve_tos) { |
| 1787 | ADDOP(c, ROT_FOUR); |
| 1788 | } |
| 1789 | ADDOP(c, POP_TOP); |
| 1790 | ADDOP(c, POP_TOP); |
| 1791 | ADDOP(c, POP_TOP); |
| 1792 | if (preserve_tos) { |
| 1793 | ADDOP(c, ROT_FOUR); |
| 1794 | } |
| 1795 | ADDOP(c, POP_EXCEPT); |
| 1796 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1797 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1798 | case WITH: |
| 1799 | case ASYNC_WITH: |
| 1800 | ADDOP(c, POP_BLOCK); |
| 1801 | if (preserve_tos) { |
| 1802 | ADDOP(c, ROT_TWO); |
| 1803 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1804 | if(!compiler_call_exit_with_nones(c)) { |
| 1805 | return 0; |
| 1806 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1807 | if (info->fb_type == ASYNC_WITH) { |
| 1808 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1809 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1810 | ADDOP(c, YIELD_FROM); |
| 1811 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1812 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1813 | return 1; |
| 1814 | |
| 1815 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1816 | if (info->fb_datum) { |
| 1817 | ADDOP(c, POP_BLOCK); |
| 1818 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1819 | if (preserve_tos) { |
| 1820 | ADDOP(c, ROT_FOUR); |
| 1821 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1822 | ADDOP(c, POP_EXCEPT); |
| 1823 | if (info->fb_datum) { |
| 1824 | ADDOP_LOAD_CONST(c, Py_None); |
| 1825 | compiler_nameop(c, info->fb_datum, Store); |
| 1826 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1827 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1828 | return 1; |
| 1829 | |
| 1830 | case POP_VALUE: |
| 1831 | if (preserve_tos) { |
| 1832 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1833 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1834 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1835 | return 1; |
| 1836 | } |
| 1837 | Py_UNREACHABLE(); |
| 1838 | } |
| 1839 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1840 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1841 | static int |
| 1842 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1843 | if (c->u->u_nfblocks == 0) { |
| 1844 | return 1; |
| 1845 | } |
| 1846 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1847 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1848 | *loop = top; |
| 1849 | return 1; |
| 1850 | } |
| 1851 | struct fblockinfo copy = *top; |
| 1852 | c->u->u_nfblocks--; |
| 1853 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1854 | return 0; |
| 1855 | } |
| 1856 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1857 | return 0; |
| 1858 | } |
| 1859 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1860 | c->u->u_nfblocks++; |
| 1861 | return 1; |
| 1862 | } |
| 1863 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1864 | /* Compile a sequence of statements, checking for a docstring |
| 1865 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1866 | |
| 1867 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1868 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1869 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1870 | int i = 0; |
| 1871 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1872 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1873 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1874 | /* Set current line number to the line number of first statement. |
| 1875 | This way line number for SETUP_ANNOTATIONS will always |
| 1876 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1877 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1878 | 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] | 1879 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1880 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1881 | } |
| 1882 | /* Every annotated class and module should have __annotations__. */ |
| 1883 | if (find_ann(stmts)) { |
| 1884 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1885 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1886 | if (!asdl_seq_LEN(stmts)) |
| 1887 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1888 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1889 | if (c->c_optimize < 2) { |
| 1890 | docstring = _PyAST_GetDocString(stmts); |
| 1891 | if (docstring) { |
| 1892 | i = 1; |
| 1893 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1894 | assert(st->kind == Expr_kind); |
| 1895 | VISIT(c, expr, st->v.Expr.value); |
| 1896 | if (!compiler_nameop(c, __doc__, Store)) |
| 1897 | return 0; |
| 1898 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1900 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1901 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | static PyCodeObject * |
| 1906 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | PyCodeObject *co; |
| 1909 | int addNone = 1; |
| 1910 | static PyObject *module; |
| 1911 | if (!module) { |
| 1912 | module = PyUnicode_InternFromString("<module>"); |
| 1913 | if (!module) |
| 1914 | return NULL; |
| 1915 | } |
| 1916 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1917 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | return NULL; |
| 1919 | switch (mod->kind) { |
| 1920 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1921 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | compiler_exit_scope(c); |
| 1923 | return 0; |
| 1924 | } |
| 1925 | break; |
| 1926 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1927 | if (find_ann(mod->v.Interactive.body)) { |
| 1928 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1929 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1931 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | break; |
| 1933 | case Expression_kind: |
| 1934 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1935 | addNone = 0; |
| 1936 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | default: |
| 1938 | PyErr_Format(PyExc_SystemError, |
| 1939 | "module kind %d should not be possible", |
| 1940 | mod->kind); |
| 1941 | return 0; |
| 1942 | } |
| 1943 | co = assemble(c, addNone); |
| 1944 | compiler_exit_scope(c); |
| 1945 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1948 | /* The test for LOCAL must come before the test for FREE in order to |
| 1949 | handle classes where name is both local and free. The local var is |
| 1950 | 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] | 1951 | */ |
| 1952 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1953 | static int |
| 1954 | get_ref_type(struct compiler *c, PyObject *name) |
| 1955 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1956 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1957 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1958 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1959 | return CELL; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1960 | scope = _PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1962 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1963 | "_PyST_GetScope(name=%R) failed: " |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1964 | "unknown scope in unit %S (%R); " |
| 1965 | "symbols: %R; locals: %R; globals: %R", |
| 1966 | name, |
| 1967 | c->u->u_name, c->u->u_ste->ste_id, |
| 1968 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1969 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | static int |
| 1975 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1976 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1977 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1978 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1979 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1980 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1981 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1985 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1986 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1987 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1988 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1989 | if (qualname == NULL) |
| 1990 | qualname = co->co_name; |
| 1991 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1992 | if (free) { |
| 1993 | for (i = 0; i < free; ++i) { |
| 1994 | /* Bypass com_addop_varname because it will generate |
| 1995 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1996 | */ |
| 1997 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1998 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1999 | /* Special case: If a class contains a method with a |
| 2000 | free variable that has the same name as a method, |
| 2001 | the name will be considered free *and* local in the |
| 2002 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 2003 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2004 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2005 | int reftype = get_ref_type(c, name); |
| 2006 | if (reftype == -1) { |
| 2007 | return 0; |
| 2008 | } |
| 2009 | int arg; |
| 2010 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2011 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2012 | } |
| 2013 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2014 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2015 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2016 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2017 | PyErr_Format(PyExc_SystemError, |
| 2018 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 2019 | "freevars of code %S: %R", |
| 2020 | name, |
| 2021 | reftype, |
| 2022 | c->u->u_name, |
| 2023 | co->co_name, |
| 2024 | co->co_freevars); |
| 2025 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2026 | } |
| 2027 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2029 | flags |= 0x08; |
| 2030 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2032 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 2033 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2034 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2039 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | if (!decos) |
| 2044 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2047 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2048 | } |
| 2049 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2053 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2054 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2055 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2056 | /* Push a dict of keyword-only default values. |
| 2057 | |
| 2058 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2059 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2060 | int i; |
| 2061 | PyObject *keys = NULL; |
| 2062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2064 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2065 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2066 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2067 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2068 | if (!mangled) { |
| 2069 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2071 | if (keys == NULL) { |
| 2072 | keys = PyList_New(1); |
| 2073 | if (keys == NULL) { |
| 2074 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2075 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2076 | } |
| 2077 | PyList_SET_ITEM(keys, 0, mangled); |
| 2078 | } |
| 2079 | else { |
| 2080 | int res = PyList_Append(keys, mangled); |
| 2081 | Py_DECREF(mangled); |
| 2082 | if (res == -1) { |
| 2083 | goto error; |
| 2084 | } |
| 2085 | } |
| 2086 | if (!compiler_visit_expr(c, default_)) { |
| 2087 | goto error; |
| 2088 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | } |
| 2090 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2091 | if (keys != NULL) { |
| 2092 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2093 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2094 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2095 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2096 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2097 | assert(default_count > 0); |
| 2098 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2099 | } |
| 2100 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2101 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | error: |
| 2105 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2106 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2110 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2111 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2112 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2113 | return 1; |
| 2114 | } |
| 2115 | |
| 2116 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2117 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2118 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2119 | { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2120 | if (!annotation) { |
| 2121 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | } |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 2123 | |
| 2124 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
| 2125 | if (!mangled) { |
| 2126 | return 0; |
| 2127 | } |
| 2128 | ADDOP_LOAD_CONST(c, mangled); |
| 2129 | Py_DECREF(mangled); |
| 2130 | |
| 2131 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2132 | VISIT(c, annexpr, annotation) |
| 2133 | } |
| 2134 | else { |
| 2135 | VISIT(c, expr, annotation); |
| 2136 | } |
| 2137 | *annotations_len += 2; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2138 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2142 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2143 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2144 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2145 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2147 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2148 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | c, |
| 2150 | arg->arg, |
| 2151 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2152 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2153 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2155 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | static int |
| 2159 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2160 | expr_ty returns) |
| 2161 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2162 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2163 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2164 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2165 | 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] | 2166 | */ |
| 2167 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2168 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2169 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2170 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2171 | return 0; |
| 2172 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2173 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2174 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2175 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2176 | args->vararg->annotation, &annotations_len)) |
| 2177 | return 0; |
| 2178 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2179 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2180 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2181 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2182 | args->kwarg->annotation, &annotations_len)) |
| 2183 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | if (!return_str) { |
| 2186 | return_str = PyUnicode_InternFromString("return"); |
| 2187 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2188 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2190 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2191 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2194 | if (annotations_len) { |
| 2195 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2196 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2198 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2199 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2203 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2204 | { |
| 2205 | VISIT_SEQ(c, expr, args->defaults); |
| 2206 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2207 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2210 | static Py_ssize_t |
| 2211 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2212 | { |
| 2213 | Py_ssize_t funcflags = 0; |
| 2214 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2215 | if (!compiler_visit_defaults(c, args)) |
| 2216 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2217 | funcflags |= 0x01; |
| 2218 | } |
| 2219 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2220 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2221 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2222 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2223 | return -1; |
| 2224 | } |
| 2225 | else if (res > 0) { |
| 2226 | funcflags |= 0x02; |
| 2227 | } |
| 2228 | } |
| 2229 | return funcflags; |
| 2230 | } |
| 2231 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2232 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2233 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2234 | { |
| 2235 | |
| 2236 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2237 | compiler_error(c, "cannot assign to __debug__"); |
| 2238 | return 1; |
| 2239 | } |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | static int |
| 2244 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2245 | { |
| 2246 | if (arg != NULL) { |
| 2247 | if (forbidden_name(c, arg->arg, Store)) |
| 2248 | return 0; |
| 2249 | } |
| 2250 | return 1; |
| 2251 | } |
| 2252 | |
| 2253 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2254 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2255 | { |
| 2256 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2257 | 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] | 2258 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2259 | return 0; |
| 2260 | } |
| 2261 | } |
| 2262 | return 1; |
| 2263 | } |
| 2264 | |
| 2265 | static int |
| 2266 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2267 | { |
| 2268 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2269 | return 0; |
| 2270 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2271 | return 0; |
| 2272 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2273 | return 0; |
| 2274 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2275 | return 0; |
| 2276 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2277 | return 0; |
| 2278 | return 1; |
| 2279 | } |
| 2280 | |
| 2281 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2282 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2283 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2285 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2286 | arguments_ty args; |
| 2287 | expr_ty returns; |
| 2288 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2289 | asdl_expr_seq* decos; |
| 2290 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2291 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2292 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2293 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2294 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2295 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2296 | if (is_async) { |
| 2297 | assert(s->kind == AsyncFunctionDef_kind); |
| 2298 | |
| 2299 | args = s->v.AsyncFunctionDef.args; |
| 2300 | returns = s->v.AsyncFunctionDef.returns; |
| 2301 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2302 | name = s->v.AsyncFunctionDef.name; |
| 2303 | body = s->v.AsyncFunctionDef.body; |
| 2304 | |
| 2305 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2306 | } else { |
| 2307 | assert(s->kind == FunctionDef_kind); |
| 2308 | |
| 2309 | args = s->v.FunctionDef.args; |
| 2310 | returns = s->v.FunctionDef.returns; |
| 2311 | decos = s->v.FunctionDef.decorator_list; |
| 2312 | name = s->v.FunctionDef.name; |
| 2313 | body = s->v.FunctionDef.body; |
| 2314 | |
| 2315 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2316 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2317 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2318 | if (!compiler_check_debug_args(c, args)) |
| 2319 | return 0; |
| 2320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | if (!compiler_decorators(c, decos)) |
| 2322 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2323 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2324 | firstlineno = s->lineno; |
| 2325 | if (asdl_seq_LEN(decos)) { |
| 2326 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2327 | } |
| 2328 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2329 | funcflags = compiler_default_arguments(c, args); |
| 2330 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2332 | } |
| 2333 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2334 | annotations = compiler_visit_annotations(c, args, returns); |
| 2335 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2338 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2339 | funcflags |= 0x04; |
| 2340 | } |
| 2341 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2342 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2343 | return 0; |
| 2344 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2345 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2346 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2347 | if (c->c_optimize < 2) { |
| 2348 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2349 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2350 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | compiler_exit_scope(c); |
| 2352 | return 0; |
| 2353 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2356 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2358 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2359 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2360 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2362 | qualname = c->u->u_qualname; |
| 2363 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2365 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2366 | Py_XDECREF(qualname); |
| 2367 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2369 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2370 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2371 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2372 | Py_DECREF(qualname); |
| 2373 | Py_DECREF(co); |
| 2374 | return 0; |
| 2375 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2376 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | /* decorators */ |
| 2380 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2381 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2382 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2383 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2384 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | static int |
| 2388 | compiler_class(struct compiler *c, stmt_ty s) |
| 2389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | PyCodeObject *co; |
| 2391 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2392 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2393 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | if (!compiler_decorators(c, decos)) |
| 2396 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2397 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2398 | firstlineno = s->lineno; |
| 2399 | if (asdl_seq_LEN(decos)) { |
| 2400 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2401 | } |
| 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | /* ultimately generate code for: |
| 2404 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2405 | where: |
| 2406 | <func> is a function/closure created from the class body; |
| 2407 | it has a single argument (__locals__) where the dict |
| 2408 | (or MutableSequence) representing the locals is passed |
| 2409 | <name> is the class name |
| 2410 | <bases> is the positional arguments and *varargs argument |
| 2411 | <keywords> is the keyword arguments and **kwds argument |
| 2412 | This borrows from compiler_call. |
| 2413 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2416 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2417 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2418 | return 0; |
| 2419 | /* this block represents what we do in the new scope */ |
| 2420 | { |
| 2421 | /* use the class name for name mangling */ |
| 2422 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2423 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | /* load (global) __name__ ... */ |
| 2425 | str = PyUnicode_InternFromString("__name__"); |
| 2426 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2427 | Py_XDECREF(str); |
| 2428 | compiler_exit_scope(c); |
| 2429 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2430 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | Py_DECREF(str); |
| 2432 | /* ... and store it as __module__ */ |
| 2433 | str = PyUnicode_InternFromString("__module__"); |
| 2434 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2435 | Py_XDECREF(str); |
| 2436 | compiler_exit_scope(c); |
| 2437 | return 0; |
| 2438 | } |
| 2439 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2440 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2441 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2442 | str = PyUnicode_InternFromString("__qualname__"); |
| 2443 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2444 | Py_XDECREF(str); |
| 2445 | compiler_exit_scope(c); |
| 2446 | return 0; |
| 2447 | } |
| 2448 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2450 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | compiler_exit_scope(c); |
| 2452 | return 0; |
| 2453 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2454 | /* The following code is artificial */ |
| 2455 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2456 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2457 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2458 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2459 | str = PyUnicode_InternFromString("__class__"); |
| 2460 | if (str == NULL) { |
| 2461 | compiler_exit_scope(c); |
| 2462 | return 0; |
| 2463 | } |
| 2464 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2465 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2466 | if (i < 0) { |
| 2467 | compiler_exit_scope(c); |
| 2468 | return 0; |
| 2469 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2470 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2473 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2474 | str = PyUnicode_InternFromString("__classcell__"); |
| 2475 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2476 | Py_XDECREF(str); |
| 2477 | compiler_exit_scope(c); |
| 2478 | return 0; |
| 2479 | } |
| 2480 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2482 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2483 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2484 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2485 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2486 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2487 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | /* create the code object */ |
| 2489 | co = assemble(c, 1); |
| 2490 | } |
| 2491 | /* leave the new scope */ |
| 2492 | compiler_exit_scope(c); |
| 2493 | if (co == NULL) |
| 2494 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | /* 2. load the 'build_class' function */ |
| 2497 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2498 | |
| 2499 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2500 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2501 | Py_DECREF(co); |
| 2502 | return 0; |
| 2503 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | Py_DECREF(co); |
| 2505 | |
| 2506 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2507 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2510 | 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] | 2511 | return 0; |
| 2512 | |
| 2513 | /* 6. apply decorators */ |
| 2514 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2515 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2516 | } |
| 2517 | |
| 2518 | /* 7. store into <name> */ |
| 2519 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2520 | return 0; |
| 2521 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2524 | /* Return 0 if the expression is a constant value except named singletons. |
| 2525 | Return 1 otherwise. */ |
| 2526 | static int |
| 2527 | check_is_arg(expr_ty e) |
| 2528 | { |
| 2529 | if (e->kind != Constant_kind) { |
| 2530 | return 1; |
| 2531 | } |
| 2532 | PyObject *value = e->v.Constant.value; |
| 2533 | return (value == Py_None |
| 2534 | || value == Py_False |
| 2535 | || value == Py_True |
| 2536 | || value == Py_Ellipsis); |
| 2537 | } |
| 2538 | |
| 2539 | /* Check operands of identity chacks ("is" and "is not"). |
| 2540 | Emit a warning if any operand is a constant except named singletons. |
| 2541 | Return 0 on error. |
| 2542 | */ |
| 2543 | static int |
| 2544 | check_compare(struct compiler *c, expr_ty e) |
| 2545 | { |
| 2546 | Py_ssize_t i, n; |
| 2547 | int left = check_is_arg(e->v.Compare.left); |
| 2548 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2549 | for (i = 0; i < n; i++) { |
| 2550 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2551 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2552 | if (op == Is || op == IsNot) { |
| 2553 | if (!right || !left) { |
| 2554 | const char *msg = (op == Is) |
| 2555 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2556 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2557 | return compiler_warn(c, msg); |
| 2558 | } |
| 2559 | } |
| 2560 | left = right; |
| 2561 | } |
| 2562 | return 1; |
| 2563 | } |
| 2564 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2565 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2566 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2567 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2568 | switch (op) { |
| 2569 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2570 | cmp = Py_EQ; |
| 2571 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2572 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2573 | cmp = Py_NE; |
| 2574 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2575 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2576 | cmp = Py_LT; |
| 2577 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2578 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2579 | cmp = Py_LE; |
| 2580 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2581 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2582 | cmp = Py_GT; |
| 2583 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2584 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2585 | cmp = Py_GE; |
| 2586 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2587 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2588 | ADDOP_I(c, IS_OP, 0); |
| 2589 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2590 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2591 | ADDOP_I(c, IS_OP, 1); |
| 2592 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2593 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2594 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2595 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2596 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2597 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2598 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2599 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2600 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2601 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2602 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2603 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2604 | } |
| 2605 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2606 | |
| 2607 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2608 | static int |
| 2609 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2610 | { |
| 2611 | switch (e->kind) { |
| 2612 | case UnaryOp_kind: |
| 2613 | if (e->v.UnaryOp.op == Not) |
| 2614 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2615 | /* fallback to general implementation */ |
| 2616 | break; |
| 2617 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2618 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2619 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2620 | assert(n >= 0); |
| 2621 | int cond2 = e->v.BoolOp.op == Or; |
| 2622 | basicblock *next2 = next; |
| 2623 | if (!cond2 != !cond) { |
| 2624 | next2 = compiler_new_block(c); |
| 2625 | if (next2 == NULL) |
| 2626 | return 0; |
| 2627 | } |
| 2628 | for (i = 0; i < n; ++i) { |
| 2629 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2630 | return 0; |
| 2631 | } |
| 2632 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2633 | return 0; |
| 2634 | if (next2 != next) |
| 2635 | compiler_use_next_block(c, next2); |
| 2636 | return 1; |
| 2637 | } |
| 2638 | case IfExp_kind: { |
| 2639 | basicblock *end, *next2; |
| 2640 | end = compiler_new_block(c); |
| 2641 | if (end == NULL) |
| 2642 | return 0; |
| 2643 | next2 = compiler_new_block(c); |
| 2644 | if (next2 == NULL) |
| 2645 | return 0; |
| 2646 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2647 | return 0; |
| 2648 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2649 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2650 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2651 | compiler_use_next_block(c, next2); |
| 2652 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2653 | return 0; |
| 2654 | compiler_use_next_block(c, end); |
| 2655 | return 1; |
| 2656 | } |
| 2657 | case Compare_kind: { |
| 2658 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2659 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2660 | if (!check_compare(c, e)) { |
| 2661 | return 0; |
| 2662 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2663 | basicblock *cleanup = compiler_new_block(c); |
| 2664 | if (cleanup == NULL) |
| 2665 | return 0; |
| 2666 | VISIT(c, expr, e->v.Compare.left); |
| 2667 | for (i = 0; i < n; i++) { |
| 2668 | VISIT(c, expr, |
| 2669 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2670 | ADDOP(c, DUP_TOP); |
| 2671 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2672 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2673 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2674 | NEXT_BLOCK(c); |
| 2675 | } |
| 2676 | 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] | 2677 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2678 | 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] | 2679 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2680 | basicblock *end = compiler_new_block(c); |
| 2681 | if (end == NULL) |
| 2682 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2683 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2684 | compiler_use_next_block(c, cleanup); |
| 2685 | ADDOP(c, POP_TOP); |
| 2686 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2687 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2688 | } |
| 2689 | compiler_use_next_block(c, end); |
| 2690 | return 1; |
| 2691 | } |
| 2692 | /* fallback to general implementation */ |
| 2693 | break; |
| 2694 | } |
| 2695 | default: |
| 2696 | /* fallback to general implementation */ |
| 2697 | break; |
| 2698 | } |
| 2699 | |
| 2700 | /* general implementation */ |
| 2701 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2702 | 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] | 2703 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2704 | return 1; |
| 2705 | } |
| 2706 | |
| 2707 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2708 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2710 | basicblock *end, *next; |
| 2711 | |
| 2712 | assert(e->kind == IfExp_kind); |
| 2713 | end = compiler_new_block(c); |
| 2714 | if (end == NULL) |
| 2715 | return 0; |
| 2716 | next = compiler_new_block(c); |
| 2717 | if (next == NULL) |
| 2718 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2719 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2720 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2722 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | compiler_use_next_block(c, next); |
| 2724 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2725 | compiler_use_next_block(c, end); |
| 2726 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2730 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2733 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2735 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | arguments_ty args = e->v.Lambda.args; |
| 2737 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2739 | if (!compiler_check_debug_args(c, args)) |
| 2740 | return 0; |
| 2741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | if (!name) { |
| 2743 | name = PyUnicode_InternFromString("<lambda>"); |
| 2744 | if (!name) |
| 2745 | return 0; |
| 2746 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2747 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2748 | funcflags = compiler_default_arguments(c, args); |
| 2749 | if (funcflags == -1) { |
| 2750 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2752 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2753 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2754 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | /* Make None the first constant, so the lambda can't have a |
| 2758 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2759 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2762 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2763 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2765 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2766 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2767 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2768 | } |
| 2769 | else { |
| 2770 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2771 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2773 | qualname = c->u->u_qualname; |
| 2774 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2776 | if (co == NULL) { |
| 2777 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2779 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2780 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2781 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2782 | Py_DECREF(qualname); |
| 2783 | Py_DECREF(co); |
| 2784 | return 0; |
| 2785 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2786 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | Py_DECREF(co); |
| 2788 | |
| 2789 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2793 | compiler_if(struct compiler *c, stmt_ty s) |
| 2794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | assert(s->kind == If_kind); |
| 2797 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2798 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2800 | } |
| 2801 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2802 | next = compiler_new_block(c); |
| 2803 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2804 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2805 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2806 | } |
| 2807 | else { |
| 2808 | next = end; |
| 2809 | } |
| 2810 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2811 | return 0; |
| 2812 | } |
| 2813 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2814 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2815 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2816 | compiler_use_next_block(c, next); |
| 2817 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | } |
| 2819 | compiler_use_next_block(c, end); |
| 2820 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | static int |
| 2824 | compiler_for(struct compiler *c, stmt_ty s) |
| 2825 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2826 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2829 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | cleanup = compiler_new_block(c); |
| 2831 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2832 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2834 | } |
| 2835 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2837 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2838 | VISIT(c, expr, s->v.For.iter); |
| 2839 | ADDOP(c, GET_ITER); |
| 2840 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2841 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2842 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | VISIT(c, expr, s->v.For.target); |
| 2844 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2845 | /* Mark jump as artificial */ |
| 2846 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2847 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2849 | |
| 2850 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2853 | compiler_use_next_block(c, end); |
| 2854 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2857 | |
| 2858 | static int |
| 2859 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2860 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2861 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2862 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2863 | c->u->u_ste->ste_coroutine = 1; |
| 2864 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2865 | return compiler_error(c, "'async for' outside async function"); |
| 2866 | } |
| 2867 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2868 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2869 | except = compiler_new_block(c); |
| 2870 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2871 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2872 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2873 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2874 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2875 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2876 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2877 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2878 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2879 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2880 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2881 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2882 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2883 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2884 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2885 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2886 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2887 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2888 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2889 | /* Success block for __anext__ */ |
| 2890 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2891 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2892 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2893 | |
| 2894 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2895 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2896 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2897 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2898 | |
| 2899 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2900 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2901 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2902 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2903 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2904 | |
| 2905 | compiler_use_next_block(c, end); |
| 2906 | |
| 2907 | return 1; |
| 2908 | } |
| 2909 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2910 | static int |
| 2911 | compiler_while(struct compiler *c, stmt_ty s) |
| 2912 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2913 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2915 | body = compiler_new_block(c); |
| 2916 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2918 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2919 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2922 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2925 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2926 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2930 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2931 | SET_LOC(c, s); |
| 2932 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2933 | return 0; |
| 2934 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2935 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2936 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2937 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2938 | compiler_use_next_block(c, anchor); |
| 2939 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2941 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2942 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2948 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2949 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2950 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2951 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2952 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2953 | return compiler_error(c, "'return' outside function"); |
| 2954 | if (s->v.Return.value != NULL && |
| 2955 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2956 | { |
| 2957 | return compiler_error( |
| 2958 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2960 | if (preserve_tos) { |
| 2961 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2962 | } else { |
| 2963 | /* Emit instruction with line number for expression */ |
| 2964 | if (s->v.Return.value != NULL) { |
| 2965 | SET_LOC(c, s->v.Return.value); |
| 2966 | ADDOP(c, NOP); |
| 2967 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2968 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2969 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2970 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2971 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2972 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2973 | } |
| 2974 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2975 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2976 | } |
| 2977 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2978 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2983 | static int |
| 2984 | compiler_break(struct compiler *c) |
| 2985 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2986 | struct fblockinfo *loop = NULL; |
| 2987 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2988 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2989 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2990 | if (loop == NULL) { |
| 2991 | return compiler_error(c, "'break' outside loop"); |
| 2992 | } |
| 2993 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2994 | return 0; |
| 2995 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2996 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2997 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2998 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | static int |
| 3002 | compiler_continue(struct compiler *c) |
| 3003 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3004 | struct fblockinfo *loop = NULL; |
| 3005 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 3006 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3007 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3008 | if (loop == NULL) { |
| 3009 | return compiler_error(c, "'continue' not properly in loop"); |
| 3010 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3011 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3012 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3013 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3017 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | |
| 3019 | SETUP_FINALLY L |
| 3020 | <code for body> |
| 3021 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3022 | <code for finalbody> |
| 3023 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3024 | L: |
| 3025 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3026 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3028 | The special instructions use the block stack. Each block |
| 3029 | stack entry contains the instruction that created it (here |
| 3030 | SETUP_FINALLY), the level of the value stack at the time the |
| 3031 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3033 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | Pushes the current value stack level and the label |
| 3035 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3036 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3037 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3039 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3040 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 3041 | exceptions are pushed onto the value stack (and the exception |
| 3042 | condition is cleared), and the interpreter jumps to the label |
| 3043 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3044 | */ |
| 3045 | |
| 3046 | static int |
| 3047 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 3048 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3049 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | body = compiler_new_block(c); |
| 3052 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3053 | exit = compiler_new_block(c); |
| 3054 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3056 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3057 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3058 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3060 | 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] | 3061 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3062 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3063 | if (!compiler_try_except(c, s)) |
| 3064 | return 0; |
| 3065 | } |
| 3066 | else { |
| 3067 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3068 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3069 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3070 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3071 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3072 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3073 | /* `finally` block */ |
| 3074 | compiler_use_next_block(c, end); |
| 3075 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3076 | return 0; |
| 3077 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3078 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3079 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3080 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3085 | 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] | 3086 | (The contents of the value stack is shown in [], with the top |
| 3087 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3088 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | |
| 3090 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3091 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | [] <code for S> |
| 3093 | [] POP_BLOCK |
| 3094 | [] JUMP_FORWARD L0 |
| 3095 | |
| 3096 | [tb, val, exc] L1: DUP ) |
| 3097 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3098 | [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] | 3099 | [tb, val, exc] POP |
| 3100 | [tb, val] <assign to V1> (or POP if no V1) |
| 3101 | [tb] POP |
| 3102 | [] <code for S1> |
| 3103 | JUMP_FORWARD L0 |
| 3104 | |
| 3105 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3106 | .............................etc....................... |
| 3107 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3108 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | |
| 3110 | [] L0: <next statement> |
| 3111 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 | Of course, parts are not generated if Vi or Ei is not present. |
| 3113 | */ |
| 3114 | static int |
| 3115 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3118 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | body = compiler_new_block(c); |
| 3121 | except = compiler_new_block(c); |
| 3122 | orelse = compiler_new_block(c); |
| 3123 | end = compiler_new_block(c); |
| 3124 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3125 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3126 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3128 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3130 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3131 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3132 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3133 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3134 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3136 | /* Runtime will push a block here, so we need to account for that */ |
| 3137 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3138 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | for (i = 0; i < n; i++) { |
| 3140 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3141 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3143 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3144 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | except = compiler_new_block(c); |
| 3146 | if (except == NULL) |
| 3147 | return 0; |
| 3148 | if (handler->v.ExceptHandler.type) { |
| 3149 | ADDOP(c, DUP_TOP); |
| 3150 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3151 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3152 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | } |
| 3154 | ADDOP(c, POP_TOP); |
| 3155 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3156 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3157 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3158 | cleanup_end = compiler_new_block(c); |
| 3159 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3160 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3161 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3162 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3163 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3164 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3165 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3166 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3167 | /* |
| 3168 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3169 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3170 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3171 | try: |
| 3172 | # body |
| 3173 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3174 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3175 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3176 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3178 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3179 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3180 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3181 | 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] | 3182 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3184 | /* second # body */ |
| 3185 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3186 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3187 | ADDOP(c, POP_BLOCK); |
| 3188 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3189 | /* name = None; del name; # Mark as artificial */ |
| 3190 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3191 | ADDOP_LOAD_CONST(c, Py_None); |
| 3192 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3193 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3194 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3196 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3197 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3199 | /* name = None; del name; # Mark as artificial */ |
| 3200 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3201 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3202 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3203 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3205 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | } |
| 3207 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3208 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3210 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3211 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3212 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3214 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3215 | ADDOP(c, POP_TOP); |
| 3216 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3217 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3218 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3220 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3221 | /* name = None; del name; # Mark as artificial */ |
| 3222 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3223 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3224 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | compiler_use_next_block(c, except); |
| 3227 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3228 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3229 | /* Mark as artificial */ |
| 3230 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3231 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3233 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | compiler_use_next_block(c, end); |
| 3235 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3239 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3240 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3241 | return compiler_try_finally(c, s); |
| 3242 | else |
| 3243 | return compiler_try_except(c, s); |
| 3244 | } |
| 3245 | |
| 3246 | |
| 3247 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3248 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | /* The IMPORT_NAME opcode was already generated. This function |
| 3251 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | 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] | 3254 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3256 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3257 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3258 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3259 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3260 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3262 | while (1) { |
| 3263 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3265 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3266 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3267 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3268 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3270 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3271 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3272 | if (dot == -1) { |
| 3273 | break; |
| 3274 | } |
| 3275 | ADDOP(c, ROT_TWO); |
| 3276 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3278 | if (!compiler_nameop(c, asname, Store)) { |
| 3279 | return 0; |
| 3280 | } |
| 3281 | ADDOP(c, POP_TOP); |
| 3282 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | } |
| 3284 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | static int |
| 3288 | compiler_import(struct compiler *c, stmt_ty s) |
| 3289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | /* The Import node stores a module name like a.b.c as a single |
| 3291 | string. This is convenient for all cases except |
| 3292 | import a.b.c as d |
| 3293 | where we need to parse that string to extract the individual |
| 3294 | module names. |
| 3295 | XXX Perhaps change the representation to make this case simpler? |
| 3296 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3297 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3298 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3299 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | for (i = 0; i < n; i++) { |
| 3301 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3302 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3304 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3305 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3308 | if (alias->asname) { |
| 3309 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3310 | if (!r) |
| 3311 | return r; |
| 3312 | } |
| 3313 | else { |
| 3314 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3315 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3316 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3317 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3318 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3319 | if (tmp == NULL) |
| 3320 | return 0; |
| 3321 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3323 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | Py_DECREF(tmp); |
| 3325 | } |
| 3326 | if (!r) |
| 3327 | return r; |
| 3328 | } |
| 3329 | } |
| 3330 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | static int |
| 3334 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3335 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3336 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3337 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3338 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | if (!empty_string) { |
| 3341 | empty_string = PyUnicode_FromString(""); |
| 3342 | if (!empty_string) |
| 3343 | return 0; |
| 3344 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3345 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3346 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3347 | |
| 3348 | names = PyTuple_New(n); |
| 3349 | if (!names) |
| 3350 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | /* build up the names */ |
| 3353 | for (i = 0; i < n; i++) { |
| 3354 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3355 | Py_INCREF(alias->name); |
| 3356 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3357 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3360 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | Py_DECREF(names); |
| 3362 | return compiler_error(c, "from __future__ imports must occur " |
| 3363 | "at the beginning of the file"); |
| 3364 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3365 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | if (s->v.ImportFrom.module) { |
| 3368 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3369 | } |
| 3370 | else { |
| 3371 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3372 | } |
| 3373 | for (i = 0; i < n; i++) { |
| 3374 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3375 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3376 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3377 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | assert(n == 1); |
| 3379 | ADDOP(c, IMPORT_STAR); |
| 3380 | return 1; |
| 3381 | } |
| 3382 | |
| 3383 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3384 | store_name = alias->name; |
| 3385 | if (alias->asname) |
| 3386 | store_name = alias->asname; |
| 3387 | |
| 3388 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | return 0; |
| 3390 | } |
| 3391 | } |
| 3392 | /* remove imported module */ |
| 3393 | ADDOP(c, POP_TOP); |
| 3394 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | static int |
| 3398 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3401 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3402 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3403 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3404 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3405 | (s->v.Assert.test->kind == Constant_kind && |
| 3406 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3407 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3408 | { |
| 3409 | if (!compiler_warn(c, "assertion is always true, " |
| 3410 | "perhaps remove parentheses?")) |
| 3411 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3412 | return 0; |
| 3413 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3415 | if (c->c_optimize) |
| 3416 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | end = compiler_new_block(c); |
| 3418 | if (end == NULL) |
| 3419 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3420 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3421 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3422 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | if (s->v.Assert.msg) { |
| 3424 | VISIT(c, expr, s->v.Assert.msg); |
| 3425 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3426 | } |
| 3427 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3428 | compiler_use_next_block(c, end); |
| 3429 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3433 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3434 | { |
| 3435 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3436 | VISIT(c, expr, value); |
| 3437 | ADDOP(c, PRINT_EXPR); |
| 3438 | return 1; |
| 3439 | } |
| 3440 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3441 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3442 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3443 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3444 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3448 | /* Mark POP_TOP as artificial */ |
| 3449 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3450 | ADDOP(c, POP_TOP); |
| 3451 | return 1; |
| 3452 | } |
| 3453 | |
| 3454 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3455 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3456 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3457 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3460 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | switch (s->kind) { |
| 3463 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3464 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | case ClassDef_kind: |
| 3466 | return compiler_class(c, s); |
| 3467 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3468 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | case Delete_kind: |
| 3470 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3471 | break; |
| 3472 | case Assign_kind: |
| 3473 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3474 | VISIT(c, expr, s->v.Assign.value); |
| 3475 | for (i = 0; i < n; i++) { |
| 3476 | if (i < n - 1) |
| 3477 | ADDOP(c, DUP_TOP); |
| 3478 | VISIT(c, expr, |
| 3479 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3480 | } |
| 3481 | break; |
| 3482 | case AugAssign_kind: |
| 3483 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3484 | case AnnAssign_kind: |
| 3485 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | case For_kind: |
| 3487 | return compiler_for(c, s); |
| 3488 | case While_kind: |
| 3489 | return compiler_while(c, s); |
| 3490 | case If_kind: |
| 3491 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3492 | case Match_kind: |
| 3493 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3494 | case Raise_kind: |
| 3495 | n = 0; |
| 3496 | if (s->v.Raise.exc) { |
| 3497 | VISIT(c, expr, s->v.Raise.exc); |
| 3498 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3499 | if (s->v.Raise.cause) { |
| 3500 | VISIT(c, expr, s->v.Raise.cause); |
| 3501 | n++; |
| 3502 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3503 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3504 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3505 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3506 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3507 | case Try_kind: |
| 3508 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | case Assert_kind: |
| 3510 | return compiler_assert(c, s); |
| 3511 | case Import_kind: |
| 3512 | return compiler_import(c, s); |
| 3513 | case ImportFrom_kind: |
| 3514 | return compiler_from_import(c, s); |
| 3515 | case Global_kind: |
| 3516 | case Nonlocal_kind: |
| 3517 | break; |
| 3518 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3519 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3520 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3521 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | break; |
| 3523 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3524 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3525 | case Continue_kind: |
| 3526 | return compiler_continue(c); |
| 3527 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3528 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3529 | case AsyncFunctionDef_kind: |
| 3530 | return compiler_function(c, s, 1); |
| 3531 | case AsyncWith_kind: |
| 3532 | return compiler_async_with(c, s, 0); |
| 3533 | case AsyncFor_kind: |
| 3534 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3535 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | static int |
| 3541 | unaryop(unaryop_ty op) |
| 3542 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3543 | switch (op) { |
| 3544 | case Invert: |
| 3545 | return UNARY_INVERT; |
| 3546 | case Not: |
| 3547 | return UNARY_NOT; |
| 3548 | case UAdd: |
| 3549 | return UNARY_POSITIVE; |
| 3550 | case USub: |
| 3551 | return UNARY_NEGATIVE; |
| 3552 | default: |
| 3553 | PyErr_Format(PyExc_SystemError, |
| 3554 | "unary op %d should not be possible", op); |
| 3555 | return 0; |
| 3556 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3560 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | switch (op) { |
| 3563 | case Add: |
| 3564 | return BINARY_ADD; |
| 3565 | case Sub: |
| 3566 | return BINARY_SUBTRACT; |
| 3567 | case Mult: |
| 3568 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3569 | case MatMult: |
| 3570 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | case Div: |
| 3572 | return BINARY_TRUE_DIVIDE; |
| 3573 | case Mod: |
| 3574 | return BINARY_MODULO; |
| 3575 | case Pow: |
| 3576 | return BINARY_POWER; |
| 3577 | case LShift: |
| 3578 | return BINARY_LSHIFT; |
| 3579 | case RShift: |
| 3580 | return BINARY_RSHIFT; |
| 3581 | case BitOr: |
| 3582 | return BINARY_OR; |
| 3583 | case BitXor: |
| 3584 | return BINARY_XOR; |
| 3585 | case BitAnd: |
| 3586 | return BINARY_AND; |
| 3587 | case FloorDiv: |
| 3588 | return BINARY_FLOOR_DIVIDE; |
| 3589 | default: |
| 3590 | PyErr_Format(PyExc_SystemError, |
| 3591 | "binary op %d should not be possible", op); |
| 3592 | return 0; |
| 3593 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3597 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | switch (op) { |
| 3600 | case Add: |
| 3601 | return INPLACE_ADD; |
| 3602 | case Sub: |
| 3603 | return INPLACE_SUBTRACT; |
| 3604 | case Mult: |
| 3605 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3606 | case MatMult: |
| 3607 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | case Div: |
| 3609 | return INPLACE_TRUE_DIVIDE; |
| 3610 | case Mod: |
| 3611 | return INPLACE_MODULO; |
| 3612 | case Pow: |
| 3613 | return INPLACE_POWER; |
| 3614 | case LShift: |
| 3615 | return INPLACE_LSHIFT; |
| 3616 | case RShift: |
| 3617 | return INPLACE_RSHIFT; |
| 3618 | case BitOr: |
| 3619 | return INPLACE_OR; |
| 3620 | case BitXor: |
| 3621 | return INPLACE_XOR; |
| 3622 | case BitAnd: |
| 3623 | return INPLACE_AND; |
| 3624 | case FloorDiv: |
| 3625 | return INPLACE_FLOOR_DIVIDE; |
| 3626 | default: |
| 3627 | PyErr_Format(PyExc_SystemError, |
| 3628 | "inplace binary op %d should not be possible", op); |
| 3629 | return 0; |
| 3630 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | static int |
| 3634 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3635 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3636 | int op, scope; |
| 3637 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
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 | PyObject *dict = c->u->u_names; |
| 3641 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3642 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3643 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3644 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3645 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3646 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3647 | if (forbidden_name(c, name, ctx)) |
| 3648 | return 0; |
| 3649 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3650 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3651 | if (!mangled) |
| 3652 | return 0; |
| 3653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | op = 0; |
| 3655 | optype = OP_NAME; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 3656 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3657 | switch (scope) { |
| 3658 | case FREE: |
| 3659 | dict = c->u->u_freevars; |
| 3660 | optype = OP_DEREF; |
| 3661 | break; |
| 3662 | case CELL: |
| 3663 | dict = c->u->u_cellvars; |
| 3664 | optype = OP_DEREF; |
| 3665 | break; |
| 3666 | case LOCAL: |
| 3667 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3668 | optype = OP_FAST; |
| 3669 | break; |
| 3670 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3671 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3672 | optype = OP_GLOBAL; |
| 3673 | break; |
| 3674 | case GLOBAL_EXPLICIT: |
| 3675 | optype = OP_GLOBAL; |
| 3676 | break; |
| 3677 | default: |
| 3678 | /* scope can be 0 */ |
| 3679 | break; |
| 3680 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3682 | /* 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] | 3683 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3685 | switch (optype) { |
| 3686 | case OP_DEREF: |
| 3687 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3688 | case Load: |
| 3689 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3690 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3691 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3692 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3693 | } |
| 3694 | break; |
| 3695 | case OP_FAST: |
| 3696 | switch (ctx) { |
| 3697 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3698 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3699 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3700 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3701 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3702 | return 1; |
| 3703 | case OP_GLOBAL: |
| 3704 | switch (ctx) { |
| 3705 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3706 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3707 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3708 | } |
| 3709 | break; |
| 3710 | case OP_NAME: |
| 3711 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3712 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3713 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3715 | } |
| 3716 | break; |
| 3717 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3719 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3720 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3721 | Py_DECREF(mangled); |
| 3722 | if (arg < 0) |
| 3723 | return 0; |
| 3724 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | static int |
| 3728 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3730 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3731 | int jumpi; |
| 3732 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3733 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3735 | assert(e->kind == BoolOp_kind); |
| 3736 | if (e->v.BoolOp.op == And) |
| 3737 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3738 | else |
| 3739 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3740 | end = compiler_new_block(c); |
| 3741 | if (end == NULL) |
| 3742 | return 0; |
| 3743 | s = e->v.BoolOp.values; |
| 3744 | n = asdl_seq_LEN(s) - 1; |
| 3745 | assert(n >= 0); |
| 3746 | for (i = 0; i < n; ++i) { |
| 3747 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3748 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3749 | basicblock *next = compiler_new_block(c); |
| 3750 | if (next == NULL) { |
| 3751 | return 0; |
| 3752 | } |
| 3753 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | } |
| 3755 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3756 | compiler_use_next_block(c, end); |
| 3757 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3761 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3762 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3763 | { |
| 3764 | Py_ssize_t n = asdl_seq_LEN(elts); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3765 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3766 | PyObject *folded = PyTuple_New(n); |
| 3767 | if (folded == NULL) { |
| 3768 | return 0; |
| 3769 | } |
| 3770 | PyObject *val; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3771 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3772 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3773 | Py_INCREF(val); |
| 3774 | PyTuple_SET_ITEM(folded, i, val); |
| 3775 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3776 | if (tuple) { |
| 3777 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3778 | } else { |
| 3779 | if (add == SET_ADD) { |
| 3780 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3781 | if (folded == NULL) { |
| 3782 | return 0; |
| 3783 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3784 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3785 | ADDOP_I(c, build, pushed); |
| 3786 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3787 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3788 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3789 | return 1; |
| 3790 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3791 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3792 | int big = n+pushed > STACK_USE_GUIDELINE; |
| 3793 | int seen_star = 0; |
| 3794 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3795 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3796 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3797 | seen_star = 1; |
| 3798 | } |
| 3799 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3800 | if (!seen_star && !big) { |
| 3801 | for (Py_ssize_t i = 0; i < n; i++) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3802 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3803 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3804 | } |
| 3805 | if (tuple) { |
| 3806 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3807 | } else { |
| 3808 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3809 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3810 | return 1; |
| 3811 | } |
| 3812 | int sequence_built = 0; |
| 3813 | if (big) { |
| 3814 | ADDOP_I(c, build, pushed); |
| 3815 | sequence_built = 1; |
| 3816 | } |
| 3817 | for (Py_ssize_t i = 0; i < n; i++) { |
| 3818 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3819 | if (elt->kind == Starred_kind) { |
| 3820 | if (sequence_built == 0) { |
| 3821 | ADDOP_I(c, build, i+pushed); |
| 3822 | sequence_built = 1; |
| 3823 | } |
| 3824 | VISIT(c, expr, elt->v.Starred.value); |
| 3825 | ADDOP_I(c, extend, 1); |
| 3826 | } |
| 3827 | else { |
| 3828 | VISIT(c, expr, elt); |
| 3829 | if (sequence_built) { |
| 3830 | ADDOP_I(c, add, 1); |
| 3831 | } |
| 3832 | } |
| 3833 | } |
| 3834 | assert(sequence_built); |
| 3835 | if (tuple) { |
| 3836 | ADDOP(c, LIST_TO_TUPLE); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3837 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3838 | return 1; |
| 3839 | } |
| 3840 | |
| 3841 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3842 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3843 | { |
| 3844 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3845 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3846 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3847 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3848 | if (elt->kind == Starred_kind && !seen_star) { |
| 3849 | if ((i >= (1 << 8)) || |
| 3850 | (n-i-1 >= (INT_MAX >> 8))) |
| 3851 | return compiler_error(c, |
| 3852 | "too many expressions in " |
| 3853 | "star-unpacking assignment"); |
| 3854 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3855 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3856 | } |
| 3857 | else if (elt->kind == Starred_kind) { |
| 3858 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3859 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3860 | } |
| 3861 | } |
| 3862 | if (!seen_star) { |
| 3863 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3864 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3865 | return 1; |
| 3866 | } |
| 3867 | |
| 3868 | static int |
| 3869 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3870 | { |
| 3871 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3872 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3873 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3874 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3875 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3876 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3877 | return 1; |
| 3878 | } |
| 3879 | |
| 3880 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3881 | compiler_list(struct compiler *c, expr_ty e) |
| 3882 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3883 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3884 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3885 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3886 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3887 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3888 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3889 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3890 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3891 | else |
| 3892 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | static int |
| 3897 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3898 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3899 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3900 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3901 | return assignment_helper(c, elts); |
| 3902 | } |
| 3903 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3904 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3905 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3906 | } |
| 3907 | else |
| 3908 | VISIT_SEQ(c, expr, elts); |
| 3909 | return 1; |
| 3910 | } |
| 3911 | |
| 3912 | static int |
| 3913 | compiler_set(struct compiler *c, expr_ty e) |
| 3914 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3915 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3916 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3920 | 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] | 3921 | { |
| 3922 | Py_ssize_t i; |
| 3923 | for (i = begin; i < end; i++) { |
| 3924 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3925 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3926 | return 0; |
| 3927 | } |
| 3928 | return 1; |
| 3929 | } |
| 3930 | |
| 3931 | static int |
| 3932 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3933 | { |
| 3934 | Py_ssize_t i, n = end - begin; |
| 3935 | PyObject *keys, *key; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3936 | int big = n*2 > STACK_USE_GUIDELINE; |
| 3937 | 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] | 3938 | for (i = begin; i < end; i++) { |
| 3939 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3940 | } |
| 3941 | keys = PyTuple_New(n); |
| 3942 | if (keys == NULL) { |
| 3943 | return 0; |
| 3944 | } |
| 3945 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3946 | 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] | 3947 | Py_INCREF(key); |
| 3948 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3949 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3950 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3951 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3952 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3953 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3954 | if (big) { |
| 3955 | ADDOP_I(c, BUILD_MAP, 0); |
| 3956 | } |
| 3957 | for (i = begin; i < end; i++) { |
| 3958 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3959 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3960 | if (big) { |
| 3961 | ADDOP_I(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3962 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 3963 | } |
| 3964 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3965 | ADDOP_I(c, BUILD_MAP, n); |
| 3966 | } |
| 3967 | return 1; |
| 3968 | } |
| 3969 | |
| 3970 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3971 | compiler_dict(struct compiler *c, expr_ty e) |
| 3972 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3973 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3974 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3975 | int is_unpacking = 0; |
| 3976 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3977 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3978 | elements = 0; |
| 3979 | for (i = 0; i < n; i++) { |
| 3980 | 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] | 3981 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3982 | if (elements) { |
| 3983 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3984 | return 0; |
| 3985 | } |
| 3986 | if (have_dict) { |
| 3987 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3988 | } |
| 3989 | have_dict = 1; |
| 3990 | elements = 0; |
| 3991 | } |
| 3992 | if (have_dict == 0) { |
| 3993 | ADDOP_I(c, BUILD_MAP, 0); |
| 3994 | have_dict = 1; |
| 3995 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3996 | 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] | 3997 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3998 | } |
| 3999 | else { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4000 | if (elements*2 > STACK_USE_GUIDELINE) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 4001 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4002 | return 0; |
| 4003 | } |
| 4004 | if (have_dict) { |
| 4005 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4006 | } |
| 4007 | have_dict = 1; |
| 4008 | elements = 0; |
| 4009 | } |
| 4010 | else { |
| 4011 | elements++; |
| 4012 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4013 | } |
| 4014 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4015 | if (elements) { |
| 4016 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4017 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4018 | } |
| 4019 | if (have_dict) { |
| 4020 | ADDOP_I(c, DICT_UPDATE, 1); |
| 4021 | } |
| 4022 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4023 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4024 | if (!have_dict) { |
| 4025 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4026 | } |
| 4027 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | static int |
| 4031 | compiler_compare(struct compiler *c, expr_ty e) |
| 4032 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4033 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4034 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 4035 | if (!check_compare(c, e)) { |
| 4036 | return 0; |
| 4037 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4038 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4039 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 4040 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 4041 | if (n == 0) { |
| 4042 | 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] | 4043 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4044 | } |
| 4045 | else { |
| 4046 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4047 | if (cleanup == NULL) |
| 4048 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4049 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4050 | VISIT(c, expr, |
| 4051 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4052 | ADDOP(c, DUP_TOP); |
| 4053 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 4054 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4055 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 4056 | NEXT_BLOCK(c); |
| 4057 | } |
| 4058 | 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] | 4059 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4060 | basicblock *end = compiler_new_block(c); |
| 4061 | if (end == NULL) |
| 4062 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 4063 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | compiler_use_next_block(c, cleanup); |
| 4065 | ADDOP(c, ROT_TWO); |
| 4066 | ADDOP(c, POP_TOP); |
| 4067 | compiler_use_next_block(c, end); |
| 4068 | } |
| 4069 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4072 | static PyTypeObject * |
| 4073 | infer_type(expr_ty e) |
| 4074 | { |
| 4075 | switch (e->kind) { |
| 4076 | case Tuple_kind: |
| 4077 | return &PyTuple_Type; |
| 4078 | case List_kind: |
| 4079 | case ListComp_kind: |
| 4080 | return &PyList_Type; |
| 4081 | case Dict_kind: |
| 4082 | case DictComp_kind: |
| 4083 | return &PyDict_Type; |
| 4084 | case Set_kind: |
| 4085 | case SetComp_kind: |
| 4086 | return &PySet_Type; |
| 4087 | case GeneratorExp_kind: |
| 4088 | return &PyGen_Type; |
| 4089 | case Lambda_kind: |
| 4090 | return &PyFunction_Type; |
| 4091 | case JoinedStr_kind: |
| 4092 | case FormattedValue_kind: |
| 4093 | return &PyUnicode_Type; |
| 4094 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4095 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4096 | default: |
| 4097 | return NULL; |
| 4098 | } |
| 4099 | } |
| 4100 | |
| 4101 | static int |
| 4102 | check_caller(struct compiler *c, expr_ty e) |
| 4103 | { |
| 4104 | switch (e->kind) { |
| 4105 | case Constant_kind: |
| 4106 | case Tuple_kind: |
| 4107 | case List_kind: |
| 4108 | case ListComp_kind: |
| 4109 | case Dict_kind: |
| 4110 | case DictComp_kind: |
| 4111 | case Set_kind: |
| 4112 | case SetComp_kind: |
| 4113 | case GeneratorExp_kind: |
| 4114 | case JoinedStr_kind: |
| 4115 | case FormattedValue_kind: |
| 4116 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4117 | "perhaps you missed a comma?", |
| 4118 | infer_type(e)->tp_name); |
| 4119 | default: |
| 4120 | return 1; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | static int |
| 4125 | check_subscripter(struct compiler *c, expr_ty e) |
| 4126 | { |
| 4127 | PyObject *v; |
| 4128 | |
| 4129 | switch (e->kind) { |
| 4130 | case Constant_kind: |
| 4131 | v = e->v.Constant.value; |
| 4132 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4133 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4134 | PyAnySet_Check(v))) |
| 4135 | { |
| 4136 | return 1; |
| 4137 | } |
| 4138 | /* fall through */ |
| 4139 | case Set_kind: |
| 4140 | case SetComp_kind: |
| 4141 | case GeneratorExp_kind: |
| 4142 | case Lambda_kind: |
| 4143 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4144 | "perhaps you missed a comma?", |
| 4145 | infer_type(e)->tp_name); |
| 4146 | default: |
| 4147 | return 1; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4152 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4153 | { |
| 4154 | PyObject *v; |
| 4155 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4156 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4157 | if (index_type == NULL |
| 4158 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4159 | || index_type == &PySlice_Type) { |
| 4160 | return 1; |
| 4161 | } |
| 4162 | |
| 4163 | switch (e->kind) { |
| 4164 | case Constant_kind: |
| 4165 | v = e->v.Constant.value; |
| 4166 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4167 | return 1; |
| 4168 | } |
| 4169 | /* fall through */ |
| 4170 | case Tuple_kind: |
| 4171 | case List_kind: |
| 4172 | case ListComp_kind: |
| 4173 | case JoinedStr_kind: |
| 4174 | case FormattedValue_kind: |
| 4175 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4176 | "not %.200s; " |
| 4177 | "perhaps you missed a comma?", |
| 4178 | infer_type(e)->tp_name, |
| 4179 | index_type->tp_name); |
| 4180 | default: |
| 4181 | return 1; |
| 4182 | } |
| 4183 | } |
| 4184 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4185 | // 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] | 4186 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4187 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4188 | { |
| 4189 | Py_ssize_t argsl, i; |
| 4190 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4191 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4192 | |
| 4193 | /* Check that the call node is an attribute access, and that |
| 4194 | the call doesn't have keyword parameters. */ |
| 4195 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4196 | asdl_seq_LEN(e->v.Call.keywords)) { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4197 | return -1; |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4198 | } |
| 4199 | /* Check that there aren't too many arguments */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4200 | argsl = asdl_seq_LEN(args); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4201 | if (argsl >= STACK_USE_GUIDELINE) { |
| 4202 | return -1; |
| 4203 | } |
| 4204 | /* Check that there are no *varargs types of arguments. */ |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4205 | for (i = 0; i < argsl; i++) { |
| 4206 | expr_ty elt = asdl_seq_GET(args, i); |
| 4207 | if (elt->kind == Starred_kind) { |
| 4208 | return -1; |
| 4209 | } |
| 4210 | } |
| 4211 | |
| 4212 | /* Alright, we can optimize the code. */ |
| 4213 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4214 | int old_lineno = c->u->u_lineno; |
| 4215 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4216 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4217 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4218 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4219 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4220 | return 1; |
| 4221 | } |
| 4222 | |
| 4223 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4224 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4225 | { |
| 4226 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4227 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4228 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4229 | if (key->arg == NULL) { |
| 4230 | continue; |
| 4231 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4232 | if (forbidden_name(c, key->arg, Store)) { |
| 4233 | return -1; |
| 4234 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4235 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4236 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4237 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4238 | c->u->u_col_offset = other->col_offset; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4239 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4240 | return -1; |
| 4241 | } |
| 4242 | } |
| 4243 | } |
| 4244 | return 0; |
| 4245 | } |
| 4246 | |
| 4247 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4248 | compiler_call(struct compiler *c, expr_ty e) |
| 4249 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4250 | int ret = maybe_optimize_method_call(c, e); |
| 4251 | if (ret >= 0) { |
| 4252 | return ret; |
| 4253 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4254 | if (!check_caller(c, e->v.Call.func)) { |
| 4255 | return 0; |
| 4256 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4257 | VISIT(c, expr, e->v.Call.func); |
| 4258 | return compiler_call_helper(c, 0, |
| 4259 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4260 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4263 | static int |
| 4264 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4265 | { |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4266 | |
| 4267 | Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); |
| 4268 | if (value_count > STACK_USE_GUIDELINE) { |
| 4269 | ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0)); |
| 4270 | PyObject *join = _PyUnicode_FromASCII("join", 4); |
| 4271 | if (join == NULL) { |
| 4272 | return 0; |
| 4273 | } |
| 4274 | ADDOP_NAME(c, LOAD_METHOD, join, names); |
| 4275 | Py_DECREF(join); |
| 4276 | ADDOP_I(c, BUILD_LIST, 0); |
| 4277 | for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { |
| 4278 | VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); |
| 4279 | ADDOP_I(c, LIST_APPEND, 1); |
| 4280 | } |
| 4281 | ADDOP_I(c, CALL_METHOD, 1); |
| 4282 | } |
| 4283 | else { |
| 4284 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
| 4285 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { |
| 4286 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
| 4287 | } |
| 4288 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4289 | return 1; |
| 4290 | } |
| 4291 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4292 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4293 | static int |
| 4294 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4295 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4296 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4297 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4298 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4299 | Convert the conversion char to 3 bits: |
| 4300 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4301 | !s : 001 0x1 FVC_STR |
| 4302 | !r : 010 0x2 FVC_REPR |
| 4303 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4304 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4305 | next bit is whether or not we have a format spec: |
| 4306 | yes : 100 0x4 |
| 4307 | no : 000 0x0 |
| 4308 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4309 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4310 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4311 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4312 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4313 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4314 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4315 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4316 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4317 | case 's': oparg = FVC_STR; break; |
| 4318 | case 'r': oparg = FVC_REPR; break; |
| 4319 | case 'a': oparg = FVC_ASCII; break; |
| 4320 | case -1: oparg = FVC_NONE; break; |
| 4321 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4322 | PyErr_Format(PyExc_SystemError, |
| 4323 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4324 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4325 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4326 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4327 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4328 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4329 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4330 | } |
| 4331 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4332 | /* And push our opcode and oparg */ |
| 4333 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4334 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4335 | return 1; |
| 4336 | } |
| 4337 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4338 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4339 | 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] | 4340 | { |
| 4341 | Py_ssize_t i, n = end - begin; |
| 4342 | keyword_ty kw; |
| 4343 | PyObject *keys, *key; |
| 4344 | assert(n > 0); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4345 | int big = n*2 > STACK_USE_GUIDELINE; |
| 4346 | if (n > 1 && !big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4347 | for (i = begin; i < end; i++) { |
| 4348 | kw = asdl_seq_GET(keywords, i); |
| 4349 | VISIT(c, expr, kw->value); |
| 4350 | } |
| 4351 | keys = PyTuple_New(n); |
| 4352 | if (keys == NULL) { |
| 4353 | return 0; |
| 4354 | } |
| 4355 | for (i = begin; i < end; i++) { |
| 4356 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4357 | Py_INCREF(key); |
| 4358 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4359 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4360 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4361 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4362 | return 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4363 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4364 | if (big) { |
| 4365 | ADDOP_I_NOLINE(c, BUILD_MAP, 0); |
| 4366 | } |
| 4367 | for (i = begin; i < end; i++) { |
| 4368 | kw = asdl_seq_GET(keywords, i); |
| 4369 | ADDOP_LOAD_CONST(c, kw->arg); |
| 4370 | VISIT(c, expr, kw->value); |
| 4371 | if (big) { |
| 4372 | ADDOP_I_NOLINE(c, MAP_ADD, 1); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4373 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4374 | } |
| 4375 | if (!big) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4376 | ADDOP_I(c, BUILD_MAP, n); |
| 4377 | } |
| 4378 | return 1; |
| 4379 | } |
| 4380 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4381 | /* shared code between compiler_call and compiler_class */ |
| 4382 | static int |
| 4383 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4384 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4385 | asdl_expr_seq *args, |
| 4386 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4387 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4388 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4389 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4390 | if (validate_keywords(c, keywords) == -1) { |
| 4391 | return 0; |
| 4392 | } |
| 4393 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4394 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4395 | nkwelts = asdl_seq_LEN(keywords); |
| 4396 | |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 4397 | if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) { |
| 4398 | goto ex_call; |
| 4399 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4400 | for (i = 0; i < nelts; i++) { |
| 4401 | expr_ty elt = asdl_seq_GET(args, i); |
| 4402 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4403 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4404 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4405 | } |
| 4406 | for (i = 0; i < nkwelts; i++) { |
| 4407 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4408 | if (kw->arg == NULL) { |
| 4409 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4410 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4412 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4413 | /* No * or ** args, so can use faster calling sequence */ |
| 4414 | for (i = 0; i < nelts; i++) { |
| 4415 | expr_ty elt = asdl_seq_GET(args, i); |
| 4416 | assert(elt->kind != Starred_kind); |
| 4417 | VISIT(c, expr, elt); |
| 4418 | } |
| 4419 | if (nkwelts) { |
| 4420 | PyObject *names; |
| 4421 | VISIT_SEQ(c, keyword, keywords); |
| 4422 | names = PyTuple_New(nkwelts); |
| 4423 | if (names == NULL) { |
| 4424 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4425 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4426 | for (i = 0; i < nkwelts; i++) { |
| 4427 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4428 | Py_INCREF(kw->arg); |
| 4429 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4430 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4431 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4432 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4433 | return 1; |
| 4434 | } |
| 4435 | else { |
| 4436 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | |
| 4440 | ex_call: |
| 4441 | |
| 4442 | /* Do positional arguments. */ |
| 4443 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4444 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4445 | } |
| 4446 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4447 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4448 | return 0; |
| 4449 | } |
| 4450 | /* Then keyword arguments */ |
| 4451 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4452 | /* Has a new dict been pushed */ |
| 4453 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4454 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4455 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4456 | for (i = 0; i < nkwelts; i++) { |
| 4457 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4458 | if (kw->arg == NULL) { |
| 4459 | /* A keyword argument unpacking. */ |
| 4460 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4461 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4462 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4463 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4464 | if (have_dict) { |
| 4465 | ADDOP_I(c, DICT_MERGE, 1); |
| 4466 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4467 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4468 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4469 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4470 | if (!have_dict) { |
| 4471 | ADDOP_I(c, BUILD_MAP, 0); |
| 4472 | have_dict = 1; |
| 4473 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4474 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4475 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4476 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4477 | else { |
| 4478 | nseen++; |
| 4479 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4480 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4481 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4482 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4483 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4484 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4485 | } |
| 4486 | if (have_dict) { |
| 4487 | ADDOP_I(c, DICT_MERGE, 1); |
| 4488 | } |
| 4489 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4490 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4491 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4492 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4493 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4494 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4497 | |
| 4498 | /* List and set comprehensions and generator expressions work by creating a |
| 4499 | nested function to perform the actual iteration. This means that the |
| 4500 | iteration variables don't leak into the current scope. |
| 4501 | The defined function is called immediately following its definition, with the |
| 4502 | result of that call being the result of the expression. |
| 4503 | The LC/SC version returns the populated container, while the GE version is |
| 4504 | flagged in symtable.c as a generator, so it returns the generator object |
| 4505 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4506 | |
| 4507 | Possible cleanups: |
| 4508 | - iterate over the generator sequence instead of using recursion |
| 4509 | */ |
| 4510 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4511 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4512 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4513 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4514 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4515 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4517 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4518 | comprehension_ty gen; |
| 4519 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4520 | if (gen->is_async) { |
| 4521 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4522 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4523 | } else { |
| 4524 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4525 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | static int |
| 4530 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4531 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4532 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4533 | expr_ty elt, expr_ty val, int type) |
| 4534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4535 | /* generate code for the iterator, then each of the ifs, |
| 4536 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | comprehension_ty gen; |
| 4539 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4540 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | start = compiler_new_block(c); |
| 4543 | skip = compiler_new_block(c); |
| 4544 | if_cleanup = compiler_new_block(c); |
| 4545 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4548 | anchor == NULL) |
| 4549 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | if (gen_index == 0) { |
| 4554 | /* Receive outermost iter as an implicit argument */ |
| 4555 | c->u->u_argcount = 1; |
| 4556 | ADDOP_I(c, LOAD_FAST, 0); |
| 4557 | } |
| 4558 | else { |
| 4559 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4560 | /* Fast path for the temporary variable assignment idiom: |
| 4561 | for y in [f(x)] |
| 4562 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4563 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4564 | switch (gen->iter->kind) { |
| 4565 | case List_kind: |
| 4566 | elts = gen->iter->v.List.elts; |
| 4567 | break; |
| 4568 | case Tuple_kind: |
| 4569 | elts = gen->iter->v.Tuple.elts; |
| 4570 | break; |
| 4571 | default: |
| 4572 | elts = NULL; |
| 4573 | } |
| 4574 | if (asdl_seq_LEN(elts) == 1) { |
| 4575 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4576 | if (elt->kind != Starred_kind) { |
| 4577 | VISIT(c, expr, elt); |
| 4578 | start = NULL; |
| 4579 | } |
| 4580 | } |
| 4581 | if (start) { |
| 4582 | VISIT(c, expr, gen->iter); |
| 4583 | ADDOP(c, GET_ITER); |
| 4584 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4585 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4586 | if (start) { |
| 4587 | depth++; |
| 4588 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4589 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4590 | NEXT_BLOCK(c); |
| 4591 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4592 | VISIT(c, expr, gen->target); |
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 | /* XXX this needs to be cleaned up...a lot! */ |
| 4595 | n = asdl_seq_LEN(gen->ifs); |
| 4596 | for (i = 0; i < n; i++) { |
| 4597 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4598 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4599 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4600 | NEXT_BLOCK(c); |
| 4601 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4604 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4605 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4606 | elt, val, type)) |
| 4607 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4609 | /* only append after the last for generator */ |
| 4610 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4611 | /* comprehension specific code */ |
| 4612 | switch (type) { |
| 4613 | case COMP_GENEXP: |
| 4614 | VISIT(c, expr, elt); |
| 4615 | ADDOP(c, YIELD_VALUE); |
| 4616 | ADDOP(c, POP_TOP); |
| 4617 | break; |
| 4618 | case COMP_LISTCOMP: |
| 4619 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4620 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4621 | break; |
| 4622 | case COMP_SETCOMP: |
| 4623 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4624 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4625 | break; |
| 4626 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4627 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4628 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4630 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4631 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4632 | break; |
| 4633 | default: |
| 4634 | return 0; |
| 4635 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | compiler_use_next_block(c, skip); |
| 4638 | } |
| 4639 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4640 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4641 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4642 | compiler_use_next_block(c, anchor); |
| 4643 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4644 | |
| 4645 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4649 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4650 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4651 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4652 | expr_ty elt, expr_ty val, int type) |
| 4653 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4654 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4655 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4656 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4657 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4658 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4659 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4660 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4661 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4662 | return 0; |
| 4663 | } |
| 4664 | |
| 4665 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4666 | |
| 4667 | if (gen_index == 0) { |
| 4668 | /* Receive outermost iter as an implicit argument */ |
| 4669 | c->u->u_argcount = 1; |
| 4670 | ADDOP_I(c, LOAD_FAST, 0); |
| 4671 | } |
| 4672 | else { |
| 4673 | /* Sub-iter - calculate on the fly */ |
| 4674 | VISIT(c, expr, gen->iter); |
| 4675 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4676 | } |
| 4677 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4678 | compiler_use_next_block(c, start); |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4679 | /* Runtime will push a block here, so we need to account for that */ |
| 4680 | if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start, |
| 4681 | NULL, NULL)) { |
| 4682 | return 0; |
| 4683 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4684 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4685 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4686 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4687 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4688 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4689 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4690 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4691 | |
| 4692 | n = asdl_seq_LEN(gen->ifs); |
| 4693 | for (i = 0; i < n; i++) { |
| 4694 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4695 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4696 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4697 | NEXT_BLOCK(c); |
| 4698 | } |
| 4699 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4700 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4701 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4702 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4703 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4704 | elt, val, type)) |
| 4705 | return 0; |
| 4706 | |
| 4707 | /* only append after the last for generator */ |
| 4708 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4709 | /* comprehension specific code */ |
| 4710 | switch (type) { |
| 4711 | case COMP_GENEXP: |
| 4712 | VISIT(c, expr, elt); |
| 4713 | ADDOP(c, YIELD_VALUE); |
| 4714 | ADDOP(c, POP_TOP); |
| 4715 | break; |
| 4716 | case COMP_LISTCOMP: |
| 4717 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4718 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4719 | break; |
| 4720 | case COMP_SETCOMP: |
| 4721 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4722 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4723 | break; |
| 4724 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4725 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4726 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4727 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4728 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4729 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4730 | break; |
| 4731 | default: |
| 4732 | return 0; |
| 4733 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4734 | } |
| 4735 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4736 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4737 | |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame] | 4738 | compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); |
| 4739 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4740 | compiler_use_next_block(c, except); |
| 4741 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4742 | |
| 4743 | return 1; |
| 4744 | } |
| 4745 | |
| 4746 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4747 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4748 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4749 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4752 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4753 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4754 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4755 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4756 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4757 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4758 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4759 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4760 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4761 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4762 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4768 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4769 | 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] | 4770 | compiler_error(c, "asynchronous comprehension outside of " |
| 4771 | "an asynchronous function"); |
| 4772 | goto error_in_scope; |
| 4773 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | if (type != COMP_GENEXP) { |
| 4776 | int op; |
| 4777 | switch (type) { |
| 4778 | case COMP_LISTCOMP: |
| 4779 | op = BUILD_LIST; |
| 4780 | break; |
| 4781 | case COMP_SETCOMP: |
| 4782 | op = BUILD_SET; |
| 4783 | break; |
| 4784 | case COMP_DICTCOMP: |
| 4785 | op = BUILD_MAP; |
| 4786 | break; |
| 4787 | default: |
| 4788 | PyErr_Format(PyExc_SystemError, |
| 4789 | "unknown comprehension type %d", type); |
| 4790 | goto error_in_scope; |
| 4791 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | ADDOP_I(c, op, 0); |
| 4794 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4795 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4796 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4797 | val, type)) |
| 4798 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | if (type != COMP_GENEXP) { |
| 4801 | ADDOP(c, RETURN_VALUE); |
| 4802 | } |
| 4803 | |
| 4804 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4805 | qualname = c->u->u_qualname; |
| 4806 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4807 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4808 | if (top_level_await && is_async_generator){ |
| 4809 | c->u->u_ste->ste_coroutine = 1; |
| 4810 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4811 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4812 | goto error; |
| 4813 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4814 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4816 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4817 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4818 | Py_DECREF(co); |
| 4819 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4820 | VISIT(c, expr, outermost->iter); |
| 4821 | |
| 4822 | if (outermost->is_async) { |
| 4823 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4824 | } else { |
| 4825 | ADDOP(c, GET_ITER); |
| 4826 | } |
| 4827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4828 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4829 | |
| 4830 | if (is_async_generator && type != COMP_GENEXP) { |
| 4831 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4832 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4833 | ADDOP(c, YIELD_FROM); |
| 4834 | } |
| 4835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4837 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4839 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4840 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4841 | Py_XDECREF(co); |
| 4842 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4846 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4848 | static identifier name; |
| 4849 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4850 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4851 | if (!name) |
| 4852 | return 0; |
| 4853 | } |
| 4854 | assert(e->kind == GeneratorExp_kind); |
| 4855 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4856 | e->v.GeneratorExp.generators, |
| 4857 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4861 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4863 | static identifier name; |
| 4864 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4865 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4866 | if (!name) |
| 4867 | return 0; |
| 4868 | } |
| 4869 | assert(e->kind == ListComp_kind); |
| 4870 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4871 | e->v.ListComp.generators, |
| 4872 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | static int |
| 4876 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4878 | static identifier name; |
| 4879 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4880 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4881 | if (!name) |
| 4882 | return 0; |
| 4883 | } |
| 4884 | assert(e->kind == SetComp_kind); |
| 4885 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4886 | e->v.SetComp.generators, |
| 4887 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | |
| 4891 | static int |
| 4892 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | static identifier name; |
| 4895 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4896 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4897 | if (!name) |
| 4898 | return 0; |
| 4899 | } |
| 4900 | assert(e->kind == DictComp_kind); |
| 4901 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4902 | e->v.DictComp.generators, |
| 4903 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | |
| 4907 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4908 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | VISIT(c, expr, k->value); |
| 4911 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4914 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4915 | whether they are true or false. |
| 4916 | |
| 4917 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4918 | */ |
| 4919 | |
| 4920 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4921 | compiler_with_except_finish(struct compiler *c) { |
| 4922 | basicblock *exit; |
| 4923 | exit = compiler_new_block(c); |
| 4924 | if (exit == NULL) |
| 4925 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4926 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4927 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4928 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4929 | compiler_use_next_block(c, exit); |
| 4930 | ADDOP(c, POP_TOP); |
| 4931 | ADDOP(c, POP_TOP); |
| 4932 | ADDOP(c, POP_TOP); |
| 4933 | ADDOP(c, POP_EXCEPT); |
| 4934 | ADDOP(c, POP_TOP); |
| 4935 | return 1; |
| 4936 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4937 | |
| 4938 | /* |
| 4939 | Implements the async with statement. |
| 4940 | |
| 4941 | The semantics outlined in that PEP are as follows: |
| 4942 | |
| 4943 | async with EXPR as VAR: |
| 4944 | BLOCK |
| 4945 | |
| 4946 | It is implemented roughly as: |
| 4947 | |
| 4948 | context = EXPR |
| 4949 | exit = context.__aexit__ # not calling it |
| 4950 | value = await context.__aenter__() |
| 4951 | try: |
| 4952 | VAR = value # if VAR present in the syntax |
| 4953 | BLOCK |
| 4954 | finally: |
| 4955 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4956 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4957 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4958 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4959 | if not (await exit(*exc)): |
| 4960 | raise |
| 4961 | */ |
| 4962 | static int |
| 4963 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4964 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4965 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4966 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4967 | |
| 4968 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4969 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4970 | c->u->u_ste->ste_coroutine = 1; |
| 4971 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4972 | return compiler_error(c, "'async with' outside async function"); |
| 4973 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4974 | |
| 4975 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4976 | final = compiler_new_block(c); |
| 4977 | exit = compiler_new_block(c); |
| 4978 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4979 | return 0; |
| 4980 | |
| 4981 | /* Evaluate EXPR */ |
| 4982 | VISIT(c, expr, item->context_expr); |
| 4983 | |
| 4984 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4985 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4986 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4987 | ADDOP(c, YIELD_FROM); |
| 4988 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4989 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4990 | |
| 4991 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4992 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4993 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4994 | return 0; |
| 4995 | } |
| 4996 | |
| 4997 | if (item->optional_vars) { |
| 4998 | VISIT(c, expr, item->optional_vars); |
| 4999 | } |
| 5000 | else { |
| 5001 | /* Discard result from context.__aenter__() */ |
| 5002 | ADDOP(c, POP_TOP); |
| 5003 | } |
| 5004 | |
| 5005 | pos++; |
| 5006 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 5007 | /* BLOCK code */ |
| 5008 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 5009 | else if (!compiler_async_with(c, s, pos)) |
| 5010 | return 0; |
| 5011 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5012 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5013 | ADDOP(c, POP_BLOCK); |
| 5014 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5015 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5016 | /* For successful outcome: |
| 5017 | * call __exit__(None, None, None) |
| 5018 | */ |
| 5019 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5020 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5021 | ADDOP(c, GET_AWAITABLE); |
| 5022 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 5023 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5024 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5025 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5026 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5027 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5028 | |
| 5029 | /* For exceptional outcome: */ |
| 5030 | compiler_use_next_block(c, final); |
| 5031 | |
| 5032 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5033 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5034 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5035 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5036 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5037 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5038 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5039 | return 1; |
| 5040 | } |
| 5041 | |
| 5042 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5043 | /* |
| 5044 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5045 | with EXPR as VAR: |
| 5046 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5047 | is implemented as: |
| 5048 | <code for EXPR> |
| 5049 | SETUP_WITH E |
| 5050 | <code to store to VAR> or POP_TOP |
| 5051 | <code for BLOCK> |
| 5052 | LOAD_CONST (None, None, None) |
| 5053 | CALL_FUNCTION_EX 0 |
| 5054 | JUMP_FORWARD EXIT |
| 5055 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 5056 | POP_JUMP_IF_TRUE T: |
| 5057 | RERAISE |
| 5058 | T: POP_TOP * 3 (remove exception from stack) |
| 5059 | POP_EXCEPT |
| 5060 | POP_TOP |
| 5061 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5062 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5063 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5064 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5065 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5066 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5067 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5068 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5069 | |
| 5070 | assert(s->kind == With_kind); |
| 5071 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5072 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5073 | final = compiler_new_block(c); |
| 5074 | exit = compiler_new_block(c); |
| 5075 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5076 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5077 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5078 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5079 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5080 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5081 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5082 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5083 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5084 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5085 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5086 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5089 | if (item->optional_vars) { |
| 5090 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 5091 | } |
| 5092 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5094 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 5097 | pos++; |
| 5098 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 5099 | /* BLOCK code */ |
| 5100 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 5101 | else if (!compiler_with(c, s, pos)) |
| 5102 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5103 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 5104 | |
| 5105 | /* Mark all following code as artificial */ |
| 5106 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5107 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5108 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5109 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5110 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5111 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5112 | /* For successful outcome: |
| 5113 | * call __exit__(None, None, None) |
| 5114 | */ |
| 5115 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5116 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5117 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5118 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5119 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5120 | /* For exceptional outcome: */ |
| 5121 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5122 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5123 | ADDOP(c, WITH_EXCEPT_START); |
| 5124 | compiler_with_except_finish(c); |
| 5125 | |
| 5126 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5127 | return 1; |
| 5128 | } |
| 5129 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5130 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5131 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5133 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5134 | case NamedExpr_kind: |
| 5135 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5136 | ADDOP(c, DUP_TOP); |
| 5137 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5138 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5139 | case BoolOp_kind: |
| 5140 | return compiler_boolop(c, e); |
| 5141 | case BinOp_kind: |
| 5142 | VISIT(c, expr, e->v.BinOp.left); |
| 5143 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5144 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | break; |
| 5146 | case UnaryOp_kind: |
| 5147 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5148 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5149 | break; |
| 5150 | case Lambda_kind: |
| 5151 | return compiler_lambda(c, e); |
| 5152 | case IfExp_kind: |
| 5153 | return compiler_ifexp(c, e); |
| 5154 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5155 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5156 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5157 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | case GeneratorExp_kind: |
| 5159 | return compiler_genexp(c, e); |
| 5160 | case ListComp_kind: |
| 5161 | return compiler_listcomp(c, e); |
| 5162 | case SetComp_kind: |
| 5163 | return compiler_setcomp(c, e); |
| 5164 | case DictComp_kind: |
| 5165 | return compiler_dictcomp(c, e); |
| 5166 | case Yield_kind: |
| 5167 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5168 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5169 | if (e->v.Yield.value) { |
| 5170 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5171 | } |
| 5172 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5173 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5175 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5177 | case YieldFrom_kind: |
| 5178 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5179 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5180 | |
| 5181 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5182 | return compiler_error(c, "'yield from' inside async function"); |
| 5183 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5184 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5185 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5186 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5187 | ADDOP(c, YIELD_FROM); |
| 5188 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5189 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5190 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5191 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5192 | return compiler_error(c, "'await' outside function"); |
| 5193 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5194 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5195 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5196 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5197 | return compiler_error(c, "'await' outside async function"); |
| 5198 | } |
| 5199 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5200 | |
| 5201 | VISIT(c, expr, e->v.Await.value); |
| 5202 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5203 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5204 | ADDOP(c, YIELD_FROM); |
| 5205 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | case Compare_kind: |
| 5207 | return compiler_compare(c, e); |
| 5208 | case Call_kind: |
| 5209 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5210 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5211 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5212 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5213 | case JoinedStr_kind: |
| 5214 | return compiler_joined_str(c, e); |
| 5215 | case FormattedValue_kind: |
| 5216 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | /* The following exprs can be assignment targets. */ |
| 5218 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5219 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5222 | { |
| 5223 | int old_lineno = c->u->u_lineno; |
| 5224 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5225 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5226 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5227 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5228 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5230 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5231 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5232 | } |
| 5233 | int old_lineno = c->u->u_lineno; |
| 5234 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5235 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5236 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5237 | break; |
| 5238 | case Del: |
| 5239 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5240 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5241 | } |
| 5242 | break; |
| 5243 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5244 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5245 | case Starred_kind: |
| 5246 | switch (e->v.Starred.ctx) { |
| 5247 | case Store: |
| 5248 | /* In all legitimate cases, the Starred node was already replaced |
| 5249 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5250 | return compiler_error(c, |
| 5251 | "starred assignment target must be in a list or tuple"); |
| 5252 | default: |
| 5253 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5254 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5255 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5256 | break; |
| 5257 | case Slice_kind: |
| 5258 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5259 | case Name_kind: |
| 5260 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5261 | /* child nodes of List and Tuple will have expr_context set */ |
| 5262 | case List_kind: |
| 5263 | return compiler_list(c, e); |
| 5264 | case Tuple_kind: |
| 5265 | return compiler_tuple(c, e); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5266 | case MatchAs_kind: |
| 5267 | case MatchOr_kind: |
| 5268 | // Can only occur in patterns, which are handled elsewhere. |
| 5269 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5270 | } |
| 5271 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5275 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5276 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5277 | int old_lineno = c->u->u_lineno; |
| 5278 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5279 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5280 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5281 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5282 | c->u->u_col_offset = old_col_offset; |
| 5283 | return res; |
| 5284 | } |
| 5285 | |
| 5286 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5287 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5288 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5290 | expr_ty e = s->v.AugAssign.target; |
| 5291 | |
| 5292 | int old_lineno = c->u->u_lineno; |
| 5293 | int old_col_offset = c->u->u_col_offset; |
| 5294 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5296 | switch (e->kind) { |
| 5297 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5298 | VISIT(c, expr, e->v.Attribute.value); |
| 5299 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5300 | int old_lineno = c->u->u_lineno; |
| 5301 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5302 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5303 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | break; |
| 5305 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5306 | VISIT(c, expr, e->v.Subscript.value); |
| 5307 | VISIT(c, expr, e->v.Subscript.slice); |
| 5308 | ADDOP(c, DUP_TOP_TWO); |
| 5309 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | break; |
| 5311 | case Name_kind: |
| 5312 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5313 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5314 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5315 | default: |
| 5316 | PyErr_Format(PyExc_SystemError, |
| 5317 | "invalid node type (%d) for augmented assignment", |
| 5318 | e->kind); |
| 5319 | return 0; |
| 5320 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5321 | |
| 5322 | c->u->u_lineno = old_lineno; |
| 5323 | c->u->u_col_offset = old_col_offset; |
| 5324 | |
| 5325 | VISIT(c, expr, s->v.AugAssign.value); |
| 5326 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5327 | |
| 5328 | SET_LOC(c, e); |
| 5329 | |
| 5330 | switch (e->kind) { |
| 5331 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5332 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5333 | ADDOP(c, ROT_TWO); |
| 5334 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5335 | break; |
| 5336 | case Subscript_kind: |
| 5337 | ADDOP(c, ROT_THREE); |
| 5338 | ADDOP(c, STORE_SUBSCR); |
| 5339 | break; |
| 5340 | case Name_kind: |
| 5341 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5342 | default: |
| 5343 | Py_UNREACHABLE(); |
| 5344 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5345 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5349 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5350 | { |
| 5351 | VISIT(c, expr, e); |
| 5352 | ADDOP(c, POP_TOP); |
| 5353 | return 1; |
| 5354 | } |
| 5355 | |
| 5356 | static int |
| 5357 | check_annotation(struct compiler *c, stmt_ty s) |
| 5358 | { |
| 5359 | /* Annotations are only evaluated in a module or class. */ |
| 5360 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5361 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5362 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5363 | } |
| 5364 | return 1; |
| 5365 | } |
| 5366 | |
| 5367 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5368 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5369 | { |
| 5370 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5371 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5372 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5373 | 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] | 5374 | return 0; |
| 5375 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5376 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5377 | return 0; |
| 5378 | } |
| 5379 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5380 | return 0; |
| 5381 | } |
| 5382 | return 1; |
| 5383 | case Tuple_kind: { |
| 5384 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5385 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5386 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5387 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5388 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5389 | return 0; |
| 5390 | } |
| 5391 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5392 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5393 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5394 | default: |
| 5395 | return check_ann_expr(c, e); |
| 5396 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | static int |
| 5400 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5401 | { |
| 5402 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5403 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5404 | |
| 5405 | assert(s->kind == AnnAssign_kind); |
| 5406 | |
| 5407 | /* We perform the actual assignment first. */ |
| 5408 | if (s->v.AnnAssign.value) { |
| 5409 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5410 | VISIT(c, expr, targ); |
| 5411 | } |
| 5412 | switch (targ->kind) { |
| 5413 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5414 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5415 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5416 | /* If we have a simple name in a module or class, store annotation. */ |
| 5417 | if (s->v.AnnAssign.simple && |
| 5418 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5419 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Pablo Galindo | b0544ba | 2021-04-21 12:41:19 +0100 | [diff] [blame] | 5420 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5421 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5422 | } |
| 5423 | else { |
| 5424 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5425 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5426 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5427 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5428 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5429 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5430 | } |
| 5431 | break; |
| 5432 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5433 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5434 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5435 | if (!s->v.AnnAssign.value && |
| 5436 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5437 | return 0; |
| 5438 | } |
| 5439 | break; |
| 5440 | case Subscript_kind: |
| 5441 | if (!s->v.AnnAssign.value && |
| 5442 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5443 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5444 | return 0; |
| 5445 | } |
| 5446 | break; |
| 5447 | default: |
| 5448 | PyErr_Format(PyExc_SystemError, |
| 5449 | "invalid node type (%d) for annotated assignment", |
| 5450 | targ->kind); |
| 5451 | return 0; |
| 5452 | } |
| 5453 | /* Annotation is evaluated last. */ |
| 5454 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5455 | return 0; |
| 5456 | } |
| 5457 | return 1; |
| 5458 | } |
| 5459 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5460 | /* Raises a SyntaxError and returns 0. |
| 5461 | If something goes wrong, a different exception may be raised. |
| 5462 | */ |
| 5463 | |
| 5464 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5465 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5466 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5467 | va_list vargs; |
| 5468 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5469 | va_start(vargs, format); |
| 5470 | #else |
| 5471 | va_start(vargs); |
| 5472 | #endif |
| 5473 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5474 | va_end(vargs); |
| 5475 | if (msg == NULL) { |
| 5476 | return 0; |
| 5477 | } |
| 5478 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5479 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5480 | Py_INCREF(Py_None); |
| 5481 | loc = Py_None; |
| 5482 | } |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame] | 5483 | PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename, |
| 5484 | c->u->u_lineno, c->u->u_col_offset + 1, loc, |
| 5485 | c->u->u_end_lineno, c->u->u_end_col_offset + 1); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5486 | Py_DECREF(msg); |
| 5487 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5488 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5489 | } |
| 5490 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5491 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5493 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5497 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5498 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5499 | and returns 0. |
| 5500 | */ |
| 5501 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5502 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5503 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5504 | va_list vargs; |
| 5505 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5506 | va_start(vargs, format); |
| 5507 | #else |
| 5508 | va_start(vargs); |
| 5509 | #endif |
| 5510 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5511 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5512 | if (msg == NULL) { |
| 5513 | return 0; |
| 5514 | } |
| 5515 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5516 | c->u->u_lineno, NULL, NULL) < 0) |
| 5517 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5518 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5519 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5520 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5521 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5522 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5523 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5524 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5525 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5526 | return 0; |
| 5527 | } |
| 5528 | Py_DECREF(msg); |
| 5529 | return 1; |
| 5530 | } |
| 5531 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5532 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5533 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5534 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5535 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5537 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5538 | if (ctx == Load) { |
| 5539 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5540 | return 0; |
| 5541 | } |
| 5542 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5543 | return 0; |
| 5544 | } |
| 5545 | } |
| 5546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5549 | case Store: op = STORE_SUBSCR; break; |
| 5550 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5551 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5552 | assert(op); |
| 5553 | VISIT(c, expr, e->v.Subscript.value); |
| 5554 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | ADDOP(c, op); |
| 5556 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5560 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | int n = 2; |
| 5563 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5565 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5566 | if (s->v.Slice.lower) { |
| 5567 | VISIT(c, expr, s->v.Slice.lower); |
| 5568 | } |
| 5569 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5570 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5571 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5573 | if (s->v.Slice.upper) { |
| 5574 | VISIT(c, expr, s->v.Slice.upper); |
| 5575 | } |
| 5576 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5577 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | if (s->v.Slice.step) { |
| 5581 | n++; |
| 5582 | VISIT(c, expr, s->v.Slice.step); |
| 5583 | } |
| 5584 | ADDOP_I(c, BUILD_SLICE, n); |
| 5585 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5588 | |
| 5589 | // PEP 634: Structural Pattern Matching |
| 5590 | |
| 5591 | // To keep things simple, all compiler_pattern_* routines follow the convention |
| 5592 | // of replacing TOS (the subject for the given pattern) with either True (match) |
| 5593 | // or False (no match). We do this even for irrefutable patterns; the idea is |
| 5594 | // that it's much easier to smooth out any redundant pushing, popping, and |
| 5595 | // jumping in the peephole optimizer than to detect or predict it here. |
| 5596 | |
| 5597 | |
| 5598 | #define WILDCARD_CHECK(N) \ |
| 5599 | ((N)->kind == Name_kind && \ |
| 5600 | _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_")) |
| 5601 | |
| 5602 | |
| 5603 | static int |
| 5604 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5605 | { |
| 5606 | assert(!_PyUnicode_EqualToASCIIString(n, "_")); |
| 5607 | // Can't assign to the same name twice: |
| 5608 | if (pc->stores == NULL) { |
| 5609 | RETURN_IF_FALSE(pc->stores = PySet_New(NULL)); |
| 5610 | } |
| 5611 | else { |
| 5612 | int duplicate = PySet_Contains(pc->stores, n); |
| 5613 | if (duplicate < 0) { |
| 5614 | return 0; |
| 5615 | } |
| 5616 | if (duplicate) { |
| 5617 | const char *e = "multiple assignments to name %R in pattern"; |
| 5618 | return compiler_error(c, e, n); |
| 5619 | } |
| 5620 | } |
| 5621 | RETURN_IF_FALSE(!PySet_Add(pc->stores, n)); |
| 5622 | RETURN_IF_FALSE(compiler_nameop(c, n, Store)); |
| 5623 | return 1; |
| 5624 | } |
| 5625 | |
| 5626 | |
| 5627 | static int |
| 5628 | pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values, |
| 5629 | Py_ssize_t star, pattern_context *pc) |
| 5630 | { |
| 5631 | RETURN_IF_FALSE(unpack_helper(c, values)); |
| 5632 | // We've now got a bunch of new subjects on the stack. If any of them fail |
| 5633 | // to match, we need to pop everything else off, then finally push False. |
| 5634 | // fails is an array of blocks that correspond to the necessary amount of |
| 5635 | // popping for each element: |
| 5636 | basicblock **fails; |
| 5637 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5638 | fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size); |
| 5639 | if (fails == NULL) { |
| 5640 | PyErr_NoMemory(); |
| 5641 | return 0; |
| 5642 | } |
| 5643 | // NOTE: Can't use our nice returning macros anymore: they'll leak memory! |
| 5644 | // goto error on error. |
| 5645 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5646 | fails[i] = compiler_new_block(c); |
| 5647 | if (fails[i] == NULL) { |
| 5648 | goto error; |
| 5649 | } |
| 5650 | } |
| 5651 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5652 | expr_ty value = asdl_seq_GET(values, i); |
| 5653 | if (i == star) { |
| 5654 | assert(value->kind == Starred_kind); |
| 5655 | value = value->v.Starred.value; |
| 5656 | } |
| 5657 | if (!compiler_pattern_subpattern(c, value, pc) || |
| 5658 | !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) || |
| 5659 | compiler_next_block(c) == NULL) |
| 5660 | { |
| 5661 | goto error; |
| 5662 | } |
| 5663 | } |
| 5664 | // Success! |
| 5665 | basicblock *end = compiler_new_block(c); |
| 5666 | if (end == NULL || |
| 5667 | !compiler_addop_load_const(c, Py_True) || |
| 5668 | !compiler_addop_j(c, JUMP_FORWARD, end)) |
| 5669 | { |
| 5670 | goto error; |
| 5671 | } |
| 5672 | // This is where we handle failed sub-patterns. For a sequence pattern like |
| 5673 | // [a, b, c, d], this will look like: |
| 5674 | // fails[0]: POP_TOP |
| 5675 | // fails[1]: POP_TOP |
| 5676 | // fails[2]: POP_TOP |
| 5677 | // fails[3]: LOAD_CONST False |
| 5678 | for (Py_ssize_t i = 0; i < size - 1; i++) { |
| 5679 | compiler_use_next_block(c, fails[i]); |
| 5680 | if (!compiler_addop(c, POP_TOP)) { |
| 5681 | goto error; |
| 5682 | } |
| 5683 | } |
| 5684 | compiler_use_next_block(c, fails[size - 1]); |
| 5685 | if (!compiler_addop_load_const(c, Py_False)) { |
| 5686 | goto error; |
| 5687 | } |
| 5688 | compiler_use_next_block(c, end); |
| 5689 | PyObject_Free(fails); |
| 5690 | return 1; |
| 5691 | error: |
| 5692 | PyObject_Free(fails); |
| 5693 | return 0; |
| 5694 | } |
| 5695 | |
| 5696 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5697 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5698 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5699 | static int |
| 5700 | pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values, |
| 5701 | Py_ssize_t star, pattern_context *pc) |
| 5702 | { |
| 5703 | basicblock *end, *fail_pop_1; |
| 5704 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5705 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5706 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5707 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5708 | expr_ty value = asdl_seq_GET(values, i); |
| 5709 | if (WILDCARD_CHECK(value)) { |
| 5710 | continue; |
| 5711 | } |
| 5712 | if (i == star) { |
| 5713 | assert(value->kind == Starred_kind); |
| 5714 | assert(WILDCARD_CHECK(value->v.Starred.value)); |
| 5715 | continue; |
| 5716 | } |
| 5717 | ADDOP(c, DUP_TOP); |
| 5718 | if (i < star) { |
| 5719 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5720 | } |
| 5721 | else { |
| 5722 | // The subject may not support negative indexing! Compute a |
| 5723 | // nonnegative index: |
| 5724 | ADDOP(c, GET_LEN); |
| 5725 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5726 | ADDOP(c, BINARY_SUBTRACT); |
| 5727 | } |
| 5728 | ADDOP(c, BINARY_SUBSCR); |
| 5729 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5730 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5731 | NEXT_BLOCK(c); |
| 5732 | } |
| 5733 | ADDOP(c, POP_TOP); |
| 5734 | ADDOP_LOAD_CONST(c, Py_True); |
| 5735 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5736 | compiler_use_next_block(c, fail_pop_1); |
| 5737 | ADDOP(c, POP_TOP); |
| 5738 | ADDOP_LOAD_CONST(c, Py_False); |
| 5739 | compiler_use_next_block(c, end); |
| 5740 | return 1; |
| 5741 | } |
| 5742 | |
| 5743 | |
| 5744 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5745 | static int |
| 5746 | compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5747 | { |
| 5748 | int allow_irrefutable = pc->allow_irrefutable; |
| 5749 | pc->allow_irrefutable = 1; |
| 5750 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5751 | pc->allow_irrefutable = allow_irrefutable; |
| 5752 | return 1; |
| 5753 | } |
| 5754 | |
| 5755 | |
| 5756 | static int |
| 5757 | compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5758 | { |
| 5759 | assert(p->kind == MatchAs_kind); |
| 5760 | basicblock *end, *fail_pop_1; |
| 5761 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5762 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5763 | // Need to make a copy for (possibly) storing later: |
| 5764 | ADDOP(c, DUP_TOP); |
| 5765 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
| 5766 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5767 | NEXT_BLOCK(c); |
| 5768 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5769 | ADDOP_LOAD_CONST(c, Py_True); |
| 5770 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5771 | compiler_use_next_block(c, fail_pop_1); |
| 5772 | // Need to pop that unused copy from before: |
| 5773 | ADDOP(c, POP_TOP); |
| 5774 | ADDOP_LOAD_CONST(c, Py_False); |
| 5775 | compiler_use_next_block(c, end); |
| 5776 | return 1; |
| 5777 | } |
| 5778 | |
| 5779 | |
| 5780 | static int |
| 5781 | compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5782 | { |
| 5783 | assert(p->kind == Name_kind); |
| 5784 | assert(p->v.Name.ctx == Store); |
| 5785 | assert(!WILDCARD_CHECK(p)); |
| 5786 | if (!pc->allow_irrefutable) { |
| 5787 | // Whoops, can't have a name capture here! |
| 5788 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5789 | return compiler_error(c, e, p->v.Name.id); |
| 5790 | } |
| 5791 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc)); |
| 5792 | ADDOP_LOAD_CONST(c, Py_True); |
| 5793 | return 1; |
| 5794 | } |
| 5795 | |
| 5796 | |
| 5797 | static int |
| 5798 | compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5799 | { |
| 5800 | asdl_expr_seq *args = p->v.Call.args; |
| 5801 | asdl_keyword_seq *kwargs = p->v.Call.keywords; |
| 5802 | Py_ssize_t nargs = asdl_seq_LEN(args); |
| 5803 | Py_ssize_t nkwargs = asdl_seq_LEN(kwargs); |
| 5804 | if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) { |
| 5805 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5806 | return compiler_error(c, e, p->v.Call.func); |
| 5807 | } |
| 5808 | RETURN_IF_FALSE(!validate_keywords(c, kwargs)); |
| 5809 | basicblock *end, *fail_pop_1; |
| 5810 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5811 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5812 | VISIT(c, expr, p->v.Call.func); |
| 5813 | PyObject *kwnames; |
| 5814 | RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs)); |
| 5815 | Py_ssize_t i; |
| 5816 | for (i = 0; i < nkwargs; i++) { |
| 5817 | PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg; |
| 5818 | Py_INCREF(name); |
| 5819 | PyTuple_SET_ITEM(kwnames, i, name); |
| 5820 | } |
| 5821 | ADDOP_LOAD_CONST_NEW(c, kwnames); |
| 5822 | ADDOP_I(c, MATCH_CLASS, nargs); |
| 5823 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5824 | NEXT_BLOCK(c); |
| 5825 | // TOS is now a tuple of (nargs + nkwargs) attributes. |
| 5826 | for (i = 0; i < nargs + nkwargs; i++) { |
| 5827 | expr_ty arg; |
| 5828 | if (i < nargs) { |
| 5829 | // Positional: |
| 5830 | arg = asdl_seq_GET(args, i); |
| 5831 | } |
| 5832 | else { |
| 5833 | // Keyword: |
| 5834 | arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value; |
| 5835 | } |
| 5836 | if (WILDCARD_CHECK(arg)) { |
| 5837 | continue; |
| 5838 | } |
| 5839 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5840 | ADDOP(c, DUP_TOP); |
| 5841 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5842 | ADDOP(c, BINARY_SUBSCR); |
| 5843 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc)); |
| 5844 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5845 | NEXT_BLOCK(c); |
| 5846 | } |
| 5847 | // Success! Pop the tuple of attributes: |
| 5848 | ADDOP(c, POP_TOP); |
| 5849 | ADDOP_LOAD_CONST(c, Py_True); |
| 5850 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5851 | compiler_use_next_block(c, fail_pop_1); |
| 5852 | ADDOP(c, POP_TOP); |
| 5853 | ADDOP_LOAD_CONST(c, Py_False); |
| 5854 | compiler_use_next_block(c, end); |
| 5855 | return 1; |
| 5856 | } |
| 5857 | |
| 5858 | |
| 5859 | static int |
| 5860 | compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5861 | { |
| 5862 | assert(p->kind == Constant_kind); |
| 5863 | PyObject *v = p->v.Constant.value; |
| 5864 | ADDOP_LOAD_CONST(c, v); |
| 5865 | // Literal True, False, and None are compared by identity. All others use |
| 5866 | // equality: |
| 5867 | ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq); |
| 5868 | return 1; |
| 5869 | } |
| 5870 | |
| 5871 | |
| 5872 | static int |
| 5873 | compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5874 | { |
| 5875 | basicblock *end, *fail_pop_1, *fail_pop_3; |
| 5876 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5877 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5878 | RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c)); |
| 5879 | asdl_expr_seq *keys = p->v.Dict.keys; |
| 5880 | asdl_expr_seq *values = p->v.Dict.values; |
| 5881 | Py_ssize_t size = asdl_seq_LEN(values); |
Ikko Ashimine | 57827f8 | 2021-03-10 19:39:51 +0900 | [diff] [blame] | 5882 | // A starred pattern will be a keyless value. It is guaranteed to be last: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5883 | int star = size ? !asdl_seq_GET(keys, size - 1) : 0; |
| 5884 | ADDOP(c, MATCH_MAPPING); |
| 5885 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5886 | NEXT_BLOCK(c); |
| 5887 | if (!size) { |
| 5888 | // If the pattern is just "{}", we're done! |
| 5889 | ADDOP(c, POP_TOP); |
| 5890 | ADDOP_LOAD_CONST(c, Py_True); |
| 5891 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5892 | compiler_use_next_block(c, fail_pop_1); |
| 5893 | ADDOP(c, POP_TOP); |
| 5894 | ADDOP_LOAD_CONST(c, Py_False); |
| 5895 | compiler_use_next_block(c, end); |
| 5896 | return 1; |
| 5897 | } |
| 5898 | if (size - star) { |
| 5899 | // If the pattern has any keys in it, perform a length check: |
| 5900 | ADDOP(c, GET_LEN); |
| 5901 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star)); |
| 5902 | ADDOP_COMPARE(c, GtE); |
| 5903 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5904 | NEXT_BLOCK(c); |
| 5905 | } |
| 5906 | if (INT_MAX < size - star - 1) { |
| 5907 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 5908 | } |
| 5909 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 5910 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
| 5911 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5912 | expr_ty key = asdl_seq_GET(keys, i); |
| 5913 | if (key == NULL) { |
| 5914 | const char *e = "can't use starred name here " |
| 5915 | "(consider moving to end)"; |
| 5916 | return compiler_error(c, e); |
| 5917 | } |
| 5918 | VISIT(c, expr, key); |
| 5919 | } |
| 5920 | ADDOP_I(c, BUILD_TUPLE, size - star); |
| 5921 | ADDOP(c, MATCH_KEYS); |
| 5922 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5923 | NEXT_BLOCK(c); |
| 5924 | // So far so good. There's now a tuple of values on the stack to match |
| 5925 | // sub-patterns against: |
| 5926 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5927 | expr_ty value = asdl_seq_GET(values, i); |
| 5928 | if (WILDCARD_CHECK(value)) { |
| 5929 | continue; |
| 5930 | } |
| 5931 | ADDOP(c, DUP_TOP); |
| 5932 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5933 | ADDOP(c, BINARY_SUBSCR); |
| 5934 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5935 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5936 | NEXT_BLOCK(c); |
| 5937 | } |
| 5938 | // If we get this far, it's a match! We're done with that tuple of values. |
| 5939 | ADDOP(c, POP_TOP); |
| 5940 | if (star) { |
| 5941 | // If we had a starred name, bind a dict of remaining items to it: |
| 5942 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
| 5943 | PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id; |
| 5944 | RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc)); |
| 5945 | } |
| 5946 | else { |
| 5947 | // Otherwise, we don't care about this tuple of keys anymore: |
| 5948 | ADDOP(c, POP_TOP); |
| 5949 | } |
| 5950 | // Pop the subject: |
| 5951 | ADDOP(c, POP_TOP); |
| 5952 | ADDOP_LOAD_CONST(c, Py_True); |
| 5953 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5954 | // The top two items are a tuple of values or None, followed by a tuple of |
| 5955 | // keys. Pop them both: |
| 5956 | compiler_use_next_block(c, fail_pop_3); |
| 5957 | ADDOP(c, POP_TOP); |
| 5958 | ADDOP(c, POP_TOP); |
| 5959 | compiler_use_next_block(c, fail_pop_1); |
| 5960 | // Pop the subject: |
| 5961 | ADDOP(c, POP_TOP); |
| 5962 | ADDOP_LOAD_CONST(c, Py_False); |
| 5963 | compiler_use_next_block(c, end); |
| 5964 | return 1; |
| 5965 | } |
| 5966 | |
| 5967 | |
| 5968 | static int |
| 5969 | compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5970 | { |
| 5971 | assert(p->kind == MatchOr_kind); |
| 5972 | // control is the set of names bound by the first alternative. If all of the |
| 5973 | // others bind the same names (they should), then this becomes pc->stores. |
| 5974 | PyObject *control = NULL; |
| 5975 | basicblock *end, *pass_pop_1; |
| 5976 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5977 | RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c)); |
| 5978 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 5979 | assert(size > 1); |
| 5980 | // We're going to be messing with pc. Keep the original info handy: |
| 5981 | PyObject *stores_init = pc->stores; |
| 5982 | int allow_irrefutable = pc->allow_irrefutable; |
| 5983 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5984 | // NOTE: Can't use our nice returning macros in here: they'll leak sets! |
| 5985 | expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
| 5986 | pc->stores = PySet_New(stores_init); |
| 5987 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 5988 | int is_last = i == size - 1; |
| 5989 | pc->allow_irrefutable = allow_irrefutable && is_last; |
| 5990 | SET_LOC(c, alt); |
| 5991 | if (pc->stores == NULL || |
| 5992 | // Only copy the subject if we're *not* on the last alternative: |
| 5993 | (!is_last && !compiler_addop(c, DUP_TOP)) || |
| 5994 | !compiler_pattern(c, alt, pc) || |
| 5995 | // Only jump if we're *not* on the last alternative: |
| 5996 | (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) || |
| 5997 | !compiler_next_block(c)) |
| 5998 | { |
| 5999 | goto fail; |
| 6000 | } |
| 6001 | if (!i) { |
| 6002 | // If this is the first alternative, save its stores as a "control" |
| 6003 | // for the others (they can't bind a different set of names): |
| 6004 | control = pc->stores; |
| 6005 | continue; |
| 6006 | } |
| 6007 | if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) { |
| 6008 | // Otherwise, check to see if we differ from the control set: |
| 6009 | PyObject *diff = PyNumber_InPlaceXor(pc->stores, control); |
| 6010 | if (diff == NULL) { |
| 6011 | goto fail; |
| 6012 | } |
| 6013 | if (PySet_GET_SIZE(diff)) { |
| 6014 | // The names differ! Raise. |
| 6015 | Py_DECREF(diff); |
| 6016 | compiler_error(c, "alternative patterns bind different names"); |
| 6017 | goto fail; |
| 6018 | } |
| 6019 | Py_DECREF(diff); |
| 6020 | } |
| 6021 | Py_DECREF(pc->stores); |
| 6022 | } |
| 6023 | Py_XDECREF(stores_init); |
| 6024 | // Update pc->stores and restore pc->allow_irrefutable: |
| 6025 | pc->stores = control; |
| 6026 | pc->allow_irrefutable = allow_irrefutable; |
| 6027 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6028 | compiler_use_next_block(c, pass_pop_1); |
| 6029 | ADDOP(c, POP_TOP); |
| 6030 | ADDOP_LOAD_CONST(c, Py_True); |
| 6031 | compiler_use_next_block(c, end); |
| 6032 | return 1; |
| 6033 | fail: |
| 6034 | Py_XDECREF(stores_init); |
| 6035 | Py_XDECREF(control); |
| 6036 | return 0; |
| 6037 | } |
| 6038 | |
| 6039 | |
| 6040 | static int |
| 6041 | compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6042 | { |
| 6043 | assert(p->kind == List_kind || p->kind == Tuple_kind); |
| 6044 | asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts |
| 6045 | : p->v.List.elts; |
| 6046 | Py_ssize_t size = asdl_seq_LEN(values); |
| 6047 | Py_ssize_t star = -1; |
| 6048 | int only_wildcard = 1; |
| 6049 | int star_wildcard = 0; |
| 6050 | // Find a starred name, if it exists. There may be at most one: |
| 6051 | for (Py_ssize_t i = 0; i < size; i++) { |
| 6052 | expr_ty value = asdl_seq_GET(values, i); |
| 6053 | if (value->kind == Starred_kind) { |
| 6054 | value = value->v.Starred.value; |
| 6055 | if (star >= 0) { |
| 6056 | const char *e = "multiple starred names in sequence pattern"; |
| 6057 | return compiler_error(c, e); |
| 6058 | } |
| 6059 | star_wildcard = WILDCARD_CHECK(value); |
| 6060 | star = i; |
| 6061 | } |
| 6062 | only_wildcard &= WILDCARD_CHECK(value); |
| 6063 | } |
| 6064 | basicblock *end, *fail_pop_1; |
| 6065 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6066 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 6067 | ADDOP(c, MATCH_SEQUENCE); |
| 6068 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 6069 | NEXT_BLOCK(c); |
| 6070 | if (star < 0) { |
| 6071 | // No star: len(subject) == size |
| 6072 | ADDOP(c, GET_LEN); |
| 6073 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 6074 | ADDOP_COMPARE(c, Eq); |
| 6075 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 6076 | NEXT_BLOCK(c); |
| 6077 | } |
| 6078 | else if (size > 1) { |
| 6079 | // Star: len(subject) >= size - 1 |
| 6080 | ADDOP(c, GET_LEN); |
| 6081 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 6082 | ADDOP_COMPARE(c, GtE); |
| 6083 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 6084 | NEXT_BLOCK(c); |
| 6085 | } |
| 6086 | if (only_wildcard) { |
| 6087 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 6088 | ADDOP(c, POP_TOP); |
| 6089 | ADDOP_LOAD_CONST(c, Py_True); |
| 6090 | } |
| 6091 | else if (star_wildcard) { |
| 6092 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc)); |
| 6093 | } |
| 6094 | else { |
| 6095 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc)); |
| 6096 | } |
| 6097 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6098 | compiler_use_next_block(c, fail_pop_1); |
| 6099 | ADDOP(c, POP_TOP) |
| 6100 | ADDOP_LOAD_CONST(c, Py_False); |
| 6101 | compiler_use_next_block(c, end); |
| 6102 | return 1; |
| 6103 | } |
| 6104 | |
| 6105 | |
| 6106 | static int |
| 6107 | compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6108 | { |
| 6109 | assert(p->kind == Attribute_kind); |
| 6110 | assert(p->v.Attribute.ctx == Load); |
| 6111 | VISIT(c, expr, p); |
| 6112 | ADDOP_COMPARE(c, Eq); |
| 6113 | return 1; |
| 6114 | } |
| 6115 | |
| 6116 | |
| 6117 | static int |
| 6118 | compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6119 | { |
| 6120 | assert(p->kind == Name_kind); |
| 6121 | assert(p->v.Name.ctx == Store); |
| 6122 | assert(WILDCARD_CHECK(p)); |
| 6123 | if (!pc->allow_irrefutable) { |
| 6124 | // Whoops, can't have a wildcard here! |
| 6125 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 6126 | return compiler_error(c, e); |
| 6127 | } |
| 6128 | ADDOP(c, POP_TOP); |
| 6129 | ADDOP_LOAD_CONST(c, Py_True); |
| 6130 | return 1; |
| 6131 | } |
| 6132 | |
| 6133 | |
| 6134 | static int |
| 6135 | compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6136 | { |
| 6137 | SET_LOC(c, p); |
| 6138 | switch (p->kind) { |
| 6139 | case Attribute_kind: |
| 6140 | return compiler_pattern_value(c, p, pc); |
| 6141 | case BinOp_kind: |
| 6142 | // Because we allow "2+2j", things like "2+2" make it this far: |
| 6143 | return compiler_error(c, "patterns cannot include operators"); |
| 6144 | case Call_kind: |
| 6145 | return compiler_pattern_class(c, p, pc); |
| 6146 | case Constant_kind: |
| 6147 | return compiler_pattern_literal(c, p, pc); |
| 6148 | case Dict_kind: |
| 6149 | return compiler_pattern_mapping(c, p, pc); |
| 6150 | case JoinedStr_kind: |
| 6151 | // Because we allow strings, f-strings make it this far: |
| 6152 | return compiler_error(c, "patterns cannot include f-strings"); |
| 6153 | case List_kind: |
| 6154 | case Tuple_kind: |
| 6155 | return compiler_pattern_sequence(c, p, pc); |
| 6156 | case MatchAs_kind: |
| 6157 | return compiler_pattern_as(c, p, pc); |
| 6158 | case MatchOr_kind: |
| 6159 | return compiler_pattern_or(c, p, pc); |
| 6160 | case Name_kind: |
| 6161 | if (WILDCARD_CHECK(p)) { |
| 6162 | return compiler_pattern_wildcard(c, p, pc); |
| 6163 | } |
| 6164 | return compiler_pattern_capture(c, p, pc); |
| 6165 | default: |
| 6166 | Py_UNREACHABLE(); |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | |
| 6171 | static int |
| 6172 | compiler_match(struct compiler *c, stmt_ty s) |
| 6173 | { |
| 6174 | VISIT(c, expr, s->v.Match.subject); |
| 6175 | basicblock *next, *end; |
| 6176 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6177 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
| 6178 | assert(cases); |
| 6179 | pattern_context pc; |
| 6180 | // We use pc.stores to track: |
| 6181 | // - Repeated name assignments in the same pattern. |
| 6182 | // - Different name assignments in alternatives. |
| 6183 | // It's a set of names, but we don't create it until it's needed: |
| 6184 | pc.stores = NULL; |
| 6185 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6186 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6187 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6188 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6189 | SET_LOC(c, m->pattern); |
| 6190 | RETURN_IF_FALSE(next = compiler_new_block(c)); |
| 6191 | // If pc.allow_irrefutable is 0, any name captures against our subject |
| 6192 | // will raise. Irrefutable cases must be either guarded, last, or both: |
| 6193 | pc.allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6194 | // Only copy the subject if we're *not* on the last case: |
| 6195 | if (i != cases - has_default - 1) { |
| 6196 | ADDOP(c, DUP_TOP); |
| 6197 | } |
| 6198 | int result = compiler_pattern(c, m->pattern, &pc); |
| 6199 | Py_CLEAR(pc.stores); |
| 6200 | RETURN_IF_FALSE(result); |
| 6201 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next); |
| 6202 | NEXT_BLOCK(c); |
| 6203 | if (m->guard) { |
| 6204 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0)); |
| 6205 | } |
| 6206 | // Success! Pop the subject off, we're done with it: |
| 6207 | if (i != cases - has_default - 1) { |
| 6208 | ADDOP(c, POP_TOP); |
| 6209 | } |
| 6210 | VISIT_SEQ(c, stmt, m->body); |
| 6211 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6212 | compiler_use_next_block(c, next); |
| 6213 | } |
| 6214 | if (has_default) { |
| 6215 | if (cases == 1) { |
| 6216 | // No matches. Done with the subject: |
| 6217 | ADDOP(c, POP_TOP); |
| 6218 | } |
| 6219 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6220 | // pushing and popping in the loop above: |
| 6221 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6222 | SET_LOC(c, m->pattern); |
| 6223 | if (m->guard) { |
| 6224 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6225 | } |
| 6226 | VISIT_SEQ(c, stmt, m->body); |
| 6227 | } |
| 6228 | compiler_use_next_block(c, end); |
| 6229 | return 1; |
| 6230 | } |
| 6231 | |
| 6232 | |
| 6233 | #undef WILDCARD_CHECK |
| 6234 | |
| 6235 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6236 | /* End of the compiler section, beginning of the assembler section */ |
| 6237 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6238 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6239 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6240 | |
| 6241 | XXX must handle implicit jumps from one block to next |
| 6242 | */ |
| 6243 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6244 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6245 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6246 | int a_offset; /* offset into bytecode */ |
| 6247 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6248 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6249 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6250 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6251 | int a_lineno; /* lineno of last emitted instruction */ |
| 6252 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6253 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6254 | }; |
| 6255 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6256 | Py_LOCAL_INLINE(void) |
| 6257 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6258 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6259 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6260 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6261 | assert(b->b_startdepth < 0); |
| 6262 | b->b_startdepth = depth; |
| 6263 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6264 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6265 | } |
| 6266 | |
| 6267 | /* Find the flow path that needs the largest stack. We assume that |
| 6268 | * cycles in the flow graph have no net effect on the stack depth. |
| 6269 | */ |
| 6270 | static int |
| 6271 | stackdepth(struct compiler *c) |
| 6272 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6273 | basicblock *b, *entryblock = NULL; |
| 6274 | basicblock **stack, **sp; |
| 6275 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6276 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6277 | b->b_startdepth = INT_MIN; |
| 6278 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6279 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6280 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6281 | assert(entryblock!= NULL); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6282 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6283 | if (!stack) { |
| 6284 | PyErr_NoMemory(); |
| 6285 | return -1; |
| 6286 | } |
| 6287 | |
| 6288 | sp = stack; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6289 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
| 6290 | stackdepth_push(&sp, entryblock, 1); |
| 6291 | } else { |
| 6292 | stackdepth_push(&sp, entryblock, 0); |
| 6293 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6294 | while (sp != stack) { |
| 6295 | b = *--sp; |
| 6296 | int depth = b->b_startdepth; |
| 6297 | assert(depth >= 0); |
| 6298 | basicblock *next = b->b_next; |
| 6299 | for (int i = 0; i < b->b_iused; i++) { |
| 6300 | struct instr *instr = &b->b_instr[i]; |
| 6301 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6302 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6303 | PyErr_Format(PyExc_SystemError, |
| 6304 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6305 | instr->i_opcode, instr->i_oparg); |
| 6306 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6307 | } |
| 6308 | int new_depth = depth + effect; |
| 6309 | if (new_depth > maxdepth) { |
| 6310 | maxdepth = new_depth; |
| 6311 | } |
| 6312 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6313 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6314 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6315 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6316 | int target_depth = depth + effect; |
| 6317 | if (target_depth > maxdepth) { |
| 6318 | maxdepth = target_depth; |
| 6319 | } |
| 6320 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6321 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6322 | } |
| 6323 | depth = new_depth; |
| 6324 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6325 | instr->i_opcode == JUMP_FORWARD || |
| 6326 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6327 | instr->i_opcode == RAISE_VARARGS || |
| 6328 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6329 | { |
| 6330 | /* remaining code is dead */ |
| 6331 | next = NULL; |
| 6332 | break; |
| 6333 | } |
| 6334 | } |
| 6335 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6336 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6337 | stackdepth_push(&sp, next, depth); |
| 6338 | } |
| 6339 | } |
| 6340 | PyObject_Free(stack); |
| 6341 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
| 6344 | static int |
| 6345 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6347 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6348 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6349 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6350 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6351 | if (a->a_bytecode == NULL) { |
| 6352 | goto error; |
| 6353 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6354 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6355 | if (a->a_lnotab == NULL) { |
| 6356 | goto error; |
| 6357 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6358 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6359 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6360 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6361 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6362 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6363 | error: |
| 6364 | Py_XDECREF(a->a_bytecode); |
| 6365 | Py_XDECREF(a->a_lnotab); |
| 6366 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | static void |
| 6370 | assemble_free(struct assembler *a) |
| 6371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6372 | Py_XDECREF(a->a_bytecode); |
| 6373 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6374 | } |
| 6375 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6376 | static int |
| 6377 | blocksize(basicblock *b) |
| 6378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6379 | int i; |
| 6380 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6382 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6383 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6384 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6387 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6388 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6389 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6390 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6391 | if (a->a_lnotab_off + 2 >= len) { |
| 6392 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6393 | return 0; |
| 6394 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6395 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6396 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6397 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6398 | *lnotab++ = bdelta; |
| 6399 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6403 | /* Appends a range to the end of the line number table. See |
| 6404 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6405 | |
| 6406 | static int |
| 6407 | assemble_line_range(struct assembler *a) |
| 6408 | { |
| 6409 | int ldelta, bdelta; |
| 6410 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6411 | if (bdelta == 0) { |
| 6412 | return 1; |
| 6413 | } |
| 6414 | if (a->a_lineno < 0) { |
| 6415 | ldelta = -128; |
| 6416 | } |
| 6417 | else { |
| 6418 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6419 | a->a_prevlineno = a->a_lineno; |
| 6420 | while (ldelta > 127) { |
| 6421 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6422 | return 0; |
| 6423 | } |
| 6424 | ldelta -= 127; |
| 6425 | } |
| 6426 | while (ldelta < -127) { |
| 6427 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6428 | return 0; |
| 6429 | } |
| 6430 | ldelta += 127; |
| 6431 | } |
| 6432 | } |
| 6433 | assert(-128 <= ldelta && ldelta < 128); |
| 6434 | while (bdelta > 254) { |
| 6435 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6436 | return 0; |
| 6437 | } |
| 6438 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6439 | bdelta -= 254; |
| 6440 | } |
| 6441 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6442 | return 0; |
| 6443 | } |
| 6444 | a->a_lineno_start = a->a_offset; |
| 6445 | return 1; |
| 6446 | } |
| 6447 | |
| 6448 | static int |
| 6449 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6450 | { |
| 6451 | if (i->i_lineno == a->a_lineno) { |
| 6452 | return 1; |
| 6453 | } |
| 6454 | if (!assemble_line_range(a)) { |
| 6455 | return 0; |
| 6456 | } |
| 6457 | a->a_lineno = i->i_lineno; |
| 6458 | return 1; |
| 6459 | } |
| 6460 | |
| 6461 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6462 | /* assemble_emit() |
| 6463 | Extend the bytecode with a new instruction. |
| 6464 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6465 | */ |
| 6466 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6467 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6468 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6469 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6470 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6471 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6472 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6473 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6474 | arg = i->i_oparg; |
| 6475 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6476 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6477 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6478 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6479 | if (len > PY_SSIZE_T_MAX / 2) |
| 6480 | return 0; |
| 6481 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6482 | return 0; |
| 6483 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6484 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6485 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6486 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6487 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6490 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6491 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6494 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6495 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6497 | /* Compute the size of each block and fixup jump args. |
| 6498 | Replace block pointer with position in bytecode. */ |
| 6499 | do { |
| 6500 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6501 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6502 | bsize = blocksize(b); |
| 6503 | b->b_offset = totsize; |
| 6504 | totsize += bsize; |
| 6505 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6506 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6507 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6508 | bsize = b->b_offset; |
| 6509 | for (i = 0; i < b->b_iused; i++) { |
| 6510 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6511 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6512 | /* Relative jumps are computed relative to |
| 6513 | the instruction pointer after fetching |
| 6514 | the jump instruction. |
| 6515 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6516 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6517 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6518 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6519 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6520 | instr->i_oparg -= bsize; |
| 6521 | } |
| 6522 | if (instrsize(instr->i_oparg) != isize) { |
| 6523 | extended_arg_recompile = 1; |
| 6524 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6525 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6526 | } |
| 6527 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6529 | /* XXX: This is an awful hack that could hurt performance, but |
| 6530 | on the bright side it should work until we come up |
| 6531 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6533 | The issue is that in the first loop blocksize() is called |
| 6534 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6535 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6536 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6538 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6539 | The only EXTENDED_ARGs that could be popping up are |
| 6540 | ones in jump instructions. So this should converge |
| 6541 | fairly quickly. |
| 6542 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6543 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6546 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6547 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6549 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6550 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | tuple = PyTuple_New(size); |
| 6553 | if (tuple == NULL) |
| 6554 | return NULL; |
| 6555 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6556 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6557 | Py_INCREF(k); |
| 6558 | assert((i - offset) < size); |
| 6559 | assert((i - offset) >= 0); |
| 6560 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6561 | } |
| 6562 | return tuple; |
| 6563 | } |
| 6564 | |
| 6565 | static PyObject * |
| 6566 | consts_dict_keys_inorder(PyObject *dict) |
| 6567 | { |
| 6568 | PyObject *consts, *k, *v; |
| 6569 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6570 | |
| 6571 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6572 | if (consts == NULL) |
| 6573 | return NULL; |
| 6574 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6575 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6576 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6577 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6578 | * the object we want is always second. */ |
| 6579 | if (PyTuple_CheckExact(k)) { |
| 6580 | k = PyTuple_GET_ITEM(k, 1); |
| 6581 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6582 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6583 | assert(i < size); |
| 6584 | assert(i >= 0); |
| 6585 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6586 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6587 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6590 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6591 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6594 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6596 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6597 | if (ste->ste_nested) |
| 6598 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6599 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6600 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6601 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6602 | flags |= CO_COROUTINE; |
| 6603 | if (ste->ste_generator && ste->ste_coroutine) |
| 6604 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6605 | if (ste->ste_varargs) |
| 6606 | flags |= CO_VARARGS; |
| 6607 | if (ste->ste_varkeywords) |
| 6608 | flags |= CO_VARKEYWORDS; |
| 6609 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6612 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6613 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6614 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6615 | ste->ste_coroutine && |
| 6616 | !ste->ste_generator) { |
| 6617 | flags |= CO_COROUTINE; |
| 6618 | } |
| 6619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6620 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6623 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6624 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6625 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6626 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6627 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6628 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6629 | if (key == NULL) { |
| 6630 | return 0; |
| 6631 | } |
| 6632 | |
| 6633 | // t is borrowed reference |
| 6634 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6635 | Py_DECREF(key); |
| 6636 | if (t == NULL) { |
| 6637 | return 0; |
| 6638 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6639 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6640 | return 1; |
| 6641 | } |
| 6642 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6643 | if (PyTuple_CheckExact(t)) { |
| 6644 | // t is still borrowed reference |
| 6645 | t = PyTuple_GET_ITEM(t, 1); |
| 6646 | } |
| 6647 | |
| 6648 | Py_INCREF(t); |
| 6649 | Py_DECREF(*obj); |
| 6650 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6651 | return 1; |
| 6652 | } |
| 6653 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6654 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6655 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6657 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6658 | PyObject *names = NULL; |
| 6659 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6660 | PyObject *name = NULL; |
| 6661 | PyObject *freevars = NULL; |
| 6662 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6663 | Py_ssize_t nlocals; |
| 6664 | int nlocals_int; |
| 6665 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6666 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6668 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6669 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6670 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6671 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6672 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6673 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6674 | if (!cellvars) |
| 6675 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6676 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6677 | if (!freevars) |
| 6678 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6679 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6680 | if (!merge_const_one(c, &names) || |
| 6681 | !merge_const_one(c, &varnames) || |
| 6682 | !merge_const_one(c, &cellvars) || |
| 6683 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6684 | { |
| 6685 | goto error; |
| 6686 | } |
| 6687 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6688 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6689 | assert(nlocals < INT_MAX); |
| 6690 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6692 | flags = compute_code_flags(c); |
| 6693 | if (flags < 0) |
| 6694 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6695 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6696 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6697 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6698 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6699 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6700 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6701 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6702 | goto error; |
| 6703 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6704 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6705 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6706 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6707 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6708 | maxdepth = stackdepth(c); |
| 6709 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6710 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6711 | goto error; |
| 6712 | } |
Mark Shannon | 11e0b29 | 2021-04-15 14:28:56 +0100 | [diff] [blame] | 6713 | if (maxdepth > MAX_ALLOWED_STACK_USE) { |
| 6714 | PyErr_Format(PyExc_SystemError, |
| 6715 | "excessive stack use: stack is %d deep", |
| 6716 | maxdepth); |
| 6717 | Py_DECREF(consts); |
| 6718 | goto error; |
| 6719 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6720 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6721 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6722 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6723 | varnames, freevars, cellvars, c->c_filename, |
| 6724 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6725 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6726 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6727 | Py_XDECREF(names); |
| 6728 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6729 | Py_XDECREF(name); |
| 6730 | Py_XDECREF(freevars); |
| 6731 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6732 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6735 | |
| 6736 | /* For debugging purposes only */ |
| 6737 | #if 0 |
| 6738 | static void |
| 6739 | dump_instr(const struct instr *i) |
| 6740 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6741 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 6742 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6743 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6745 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6746 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6747 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6748 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6749 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6750 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | static void |
| 6754 | dump_basicblock(const basicblock *b) |
| 6755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6756 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6757 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6758 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6759 | if (b->b_instr) { |
| 6760 | int i; |
| 6761 | for (i = 0; i < b->b_iused; i++) { |
| 6762 | fprintf(stderr, " [%02d] ", i); |
| 6763 | dump_instr(b->b_instr + i); |
| 6764 | } |
| 6765 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6766 | } |
| 6767 | #endif |
| 6768 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6769 | |
| 6770 | static int |
| 6771 | normalize_basic_block(basicblock *bb); |
| 6772 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6773 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6774 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6775 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6776 | static int |
| 6777 | ensure_exits_have_lineno(struct compiler *c); |
| 6778 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6779 | static int |
| 6780 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
| 6781 | |
| 6782 | int flags = compute_code_flags(c); |
| 6783 | if (flags < 0) { |
| 6784 | return -1; |
| 6785 | } |
| 6786 | int kind; |
| 6787 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 6788 | if (flags & CO_COROUTINE) { |
| 6789 | kind = 1; |
| 6790 | } |
| 6791 | else if (flags & CO_ASYNC_GENERATOR) { |
| 6792 | kind = 2; |
| 6793 | } |
| 6794 | else { |
| 6795 | kind = 0; |
| 6796 | } |
| 6797 | } |
| 6798 | else { |
| 6799 | return 0; |
| 6800 | } |
| 6801 | if (compiler_next_instr(entryblock) < 0) { |
| 6802 | return -1; |
| 6803 | } |
| 6804 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
| 6805 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
| 6806 | } |
| 6807 | entryblock->b_instr[0].i_opcode = GEN_START; |
| 6808 | entryblock->b_instr[0].i_oparg = kind; |
| 6809 | entryblock->b_instr[0].i_lineno = -1; |
| 6810 | entryblock->b_instr[0].i_target = NULL; |
| 6811 | return 0; |
| 6812 | } |
| 6813 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6814 | static PyCodeObject * |
| 6815 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6817 | basicblock *b, *entryblock; |
| 6818 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6819 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6820 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6821 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6823 | /* Make sure every block that falls off the end returns None. |
| 6824 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6825 | block ends with a jump or return b_next shouldn't set. |
| 6826 | */ |
| 6827 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6828 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6829 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6830 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6831 | ADDOP(c, RETURN_VALUE); |
| 6832 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6833 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6834 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6835 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6836 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6837 | } |
| 6838 | } |
| 6839 | |
| 6840 | if (ensure_exits_have_lineno(c)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6841 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6842 | } |
| 6843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6844 | nblocks = 0; |
| 6845 | entryblock = NULL; |
| 6846 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6847 | nblocks++; |
| 6848 | entryblock = b; |
| 6849 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6850 | assert(entryblock != NULL); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6851 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6852 | if (insert_generator_prefix(c, entryblock)) { |
| 6853 | goto error; |
| 6854 | } |
| 6855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6856 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6857 | if (!c->u->u_firstlineno) { |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6858 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6859 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6860 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6861 | c->u->u_firstlineno = 1; |
| 6862 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6864 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6865 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6866 | a.a_entry = entryblock; |
| 6867 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6868 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6869 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6870 | if (consts == NULL) { |
| 6871 | goto error; |
| 6872 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6873 | if (optimize_cfg(c, &a, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6874 | goto error; |
| 6875 | } |
| 6876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6877 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6878 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6879 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6880 | /* Emit code. */ |
| 6881 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6882 | for (j = 0; j < b->b_iused; j++) |
| 6883 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6884 | goto error; |
| 6885 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6886 | if (!assemble_line_range(&a)) { |
| 6887 | return 0; |
| 6888 | } |
| 6889 | /* Emit sentinel at end of line number table */ |
| 6890 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6891 | goto error; |
| 6892 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6893 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6894 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6895 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6896 | } |
| 6897 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6898 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6899 | } |
| 6900 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 6901 | goto error; |
| 6902 | } |
| 6903 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 6904 | goto error; |
| 6905 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6906 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6907 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6908 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6909 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6910 | assemble_free(&a); |
| 6911 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6912 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6913 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6914 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6915 | with LOAD_CONST (c1, c2, ... cn). |
| 6916 | The consts table must still be in list form so that the |
| 6917 | new constant (c1, c2, ... cn) can be appended. |
| 6918 | Called with codestr pointing to the first LOAD_CONST. |
| 6919 | */ |
| 6920 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6921 | fold_tuple_on_constants(struct compiler *c, |
| 6922 | struct instr *inst, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6923 | int n, PyObject *consts) |
| 6924 | { |
| 6925 | /* Pre-conditions */ |
| 6926 | assert(PyList_CheckExact(consts)); |
| 6927 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6928 | assert(inst[n].i_oparg == n); |
| 6929 | |
| 6930 | for (int i = 0; i < n; i++) { |
| 6931 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6932 | return 0; |
| 6933 | } |
| 6934 | } |
| 6935 | |
| 6936 | /* Buildup new tuple of constants */ |
| 6937 | PyObject *newconst = PyTuple_New(n); |
| 6938 | if (newconst == NULL) { |
| 6939 | return -1; |
| 6940 | } |
| 6941 | for (int i = 0; i < n; i++) { |
| 6942 | int arg = inst[i].i_oparg; |
| 6943 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6944 | Py_INCREF(constant); |
| 6945 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6946 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6947 | if (merge_const_one(c, &newconst) == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6948 | Py_DECREF(newconst); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6949 | return -1; |
| 6950 | } |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 6951 | |
| 6952 | Py_ssize_t index; |
| 6953 | for (index = 0; index < PyList_GET_SIZE(consts); index++) { |
| 6954 | if (PyList_GET_ITEM(consts, index) == newconst) { |
| 6955 | break; |
| 6956 | } |
| 6957 | } |
| 6958 | if (index == PyList_GET_SIZE(consts)) { |
| 6959 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
| 6960 | Py_DECREF(newconst); |
| 6961 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6962 | return -1; |
| 6963 | } |
| 6964 | if (PyList_Append(consts, newconst)) { |
| 6965 | Py_DECREF(newconst); |
| 6966 | return -1; |
| 6967 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6968 | } |
| 6969 | Py_DECREF(newconst); |
| 6970 | for (int i = 0; i < n; i++) { |
| 6971 | inst[i].i_opcode = NOP; |
| 6972 | } |
| 6973 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6974 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6975 | return 0; |
| 6976 | } |
| 6977 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6978 | |
| 6979 | static int |
| 6980 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 6981 | assert (bb->b_iused > 0); |
| 6982 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 6983 | assert (is_jump(inst)); |
| 6984 | assert (inst->i_target->b_iused > 0); |
| 6985 | struct instr *target = &inst->i_target->b_instr[0]; |
| 6986 | if (inst->i_target == target->i_target) { |
| 6987 | /* Nothing to do */ |
| 6988 | return 0; |
| 6989 | } |
| 6990 | int lineno = target->i_lineno; |
| 6991 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 6992 | return -1; |
| 6993 | } |
| 6994 | assert (bb->b_iused >= 2); |
| 6995 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 6996 | return 0; |
| 6997 | } |
| 6998 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6999 | /* Maximum size of basic block that should be copied in optimizer */ |
| 7000 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7001 | |
| 7002 | /* Optimization */ |
| 7003 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7004 | optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7005 | { |
| 7006 | assert(PyList_CheckExact(consts)); |
| 7007 | struct instr nop; |
| 7008 | nop.i_opcode = NOP; |
| 7009 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7010 | for (int i = 0; i < bb->b_iused; i++) { |
| 7011 | struct instr *inst = &bb->b_instr[i]; |
| 7012 | int oparg = inst->i_oparg; |
| 7013 | 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] | 7014 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7015 | /* Skip over empty basic blocks. */ |
| 7016 | while (inst->i_target->b_iused == 0) { |
| 7017 | inst->i_target = inst->i_target->b_next; |
| 7018 | } |
| 7019 | target = &inst->i_target->b_instr[0]; |
| 7020 | } |
| 7021 | else { |
| 7022 | target = &nop; |
| 7023 | } |
| 7024 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7025 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7026 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7027 | { |
| 7028 | PyObject* cnt; |
| 7029 | int is_true; |
| 7030 | int jump_if_true; |
| 7031 | switch(nextop) { |
| 7032 | case POP_JUMP_IF_FALSE: |
| 7033 | case POP_JUMP_IF_TRUE: |
| 7034 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7035 | is_true = PyObject_IsTrue(cnt); |
| 7036 | if (is_true == -1) { |
| 7037 | goto error; |
| 7038 | } |
| 7039 | inst->i_opcode = NOP; |
| 7040 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 7041 | if (is_true == jump_if_true) { |
| 7042 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7043 | bb->b_nofallthrough = 1; |
| 7044 | } |
| 7045 | else { |
| 7046 | bb->b_instr[i+1].i_opcode = NOP; |
| 7047 | } |
| 7048 | break; |
| 7049 | case JUMP_IF_FALSE_OR_POP: |
| 7050 | case JUMP_IF_TRUE_OR_POP: |
| 7051 | cnt = PyList_GET_ITEM(consts, oparg); |
| 7052 | is_true = PyObject_IsTrue(cnt); |
| 7053 | if (is_true == -1) { |
| 7054 | goto error; |
| 7055 | } |
| 7056 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 7057 | if (is_true == jump_if_true) { |
| 7058 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 7059 | bb->b_nofallthrough = 1; |
| 7060 | } |
| 7061 | else { |
| 7062 | inst->i_opcode = NOP; |
| 7063 | bb->b_instr[i+1].i_opcode = NOP; |
| 7064 | } |
| 7065 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7066 | } |
| 7067 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7068 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7069 | |
| 7070 | /* Try to fold tuples of constants. |
| 7071 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 7072 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 7073 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 7074 | case BUILD_TUPLE: |
| 7075 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 7076 | switch(oparg) { |
| 7077 | case 1: |
| 7078 | inst->i_opcode = NOP; |
| 7079 | bb->b_instr[i+1].i_opcode = NOP; |
| 7080 | break; |
| 7081 | case 2: |
| 7082 | inst->i_opcode = ROT_TWO; |
| 7083 | bb->b_instr[i+1].i_opcode = NOP; |
| 7084 | break; |
| 7085 | case 3: |
| 7086 | inst->i_opcode = ROT_THREE; |
| 7087 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 7088 | } |
| 7089 | break; |
| 7090 | } |
| 7091 | if (i >= oparg) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7092 | if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7093 | goto error; |
| 7094 | } |
| 7095 | } |
| 7096 | break; |
| 7097 | |
| 7098 | /* Simplify conditional jump to conditional jump where the |
| 7099 | result of the first test implies the success of a similar |
| 7100 | test or the failure of the opposite test. |
| 7101 | Arises in code like: |
| 7102 | "a and b or c" |
| 7103 | "(a and b) and c" |
| 7104 | "(a or b) or c" |
| 7105 | "(a or b) and c" |
| 7106 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 7107 | --> x:JUMP_IF_FALSE_OR_POP z |
| 7108 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 7109 | --> x:POP_JUMP_IF_FALSE y+1 |
| 7110 | where y+1 is the instruction following the second test. |
| 7111 | */ |
| 7112 | case JUMP_IF_FALSE_OR_POP: |
| 7113 | switch(target->i_opcode) { |
| 7114 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7115 | if (inst->i_lineno == target->i_lineno) { |
| 7116 | *inst = *target; |
| 7117 | i--; |
| 7118 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7119 | break; |
| 7120 | case JUMP_ABSOLUTE: |
| 7121 | case JUMP_FORWARD: |
| 7122 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7123 | if (inst->i_lineno == target->i_lineno && |
| 7124 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7125 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7126 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7127 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7128 | break; |
| 7129 | case JUMP_IF_TRUE_OR_POP: |
| 7130 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7131 | if (inst->i_lineno == target->i_lineno) { |
| 7132 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 7133 | inst->i_target = inst->i_target->b_next; |
| 7134 | --i; |
| 7135 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7136 | break; |
| 7137 | } |
| 7138 | break; |
| 7139 | |
| 7140 | case JUMP_IF_TRUE_OR_POP: |
| 7141 | switch(target->i_opcode) { |
| 7142 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7143 | if (inst->i_lineno == target->i_lineno) { |
| 7144 | *inst = *target; |
| 7145 | i--; |
| 7146 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7147 | break; |
| 7148 | case JUMP_ABSOLUTE: |
| 7149 | case JUMP_FORWARD: |
| 7150 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7151 | if (inst->i_lineno == target->i_lineno && |
| 7152 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7153 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7154 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7155 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7156 | break; |
| 7157 | case JUMP_IF_FALSE_OR_POP: |
| 7158 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7159 | if (inst->i_lineno == target->i_lineno) { |
| 7160 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 7161 | inst->i_target = inst->i_target->b_next; |
| 7162 | --i; |
| 7163 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7164 | break; |
| 7165 | } |
| 7166 | break; |
| 7167 | |
| 7168 | case POP_JUMP_IF_FALSE: |
| 7169 | switch(target->i_opcode) { |
| 7170 | case JUMP_ABSOLUTE: |
| 7171 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7172 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7173 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7174 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7175 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7176 | break; |
| 7177 | } |
| 7178 | break; |
| 7179 | |
| 7180 | case POP_JUMP_IF_TRUE: |
| 7181 | switch(target->i_opcode) { |
| 7182 | case JUMP_ABSOLUTE: |
| 7183 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7184 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7185 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7186 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7187 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7188 | break; |
| 7189 | } |
| 7190 | break; |
| 7191 | |
| 7192 | case JUMP_ABSOLUTE: |
| 7193 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7194 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7195 | switch(target->i_opcode) { |
| 7196 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7197 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7198 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7199 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7200 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7201 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7202 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7203 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7204 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7205 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7206 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7207 | default: |
| 7208 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7209 | basicblock *to_copy = inst->i_target; |
| 7210 | inst->i_opcode = NOP; |
| 7211 | for (i = 0; i < to_copy->b_iused; i++) { |
| 7212 | int index = compiler_next_instr(bb); |
| 7213 | if (index < 0) { |
| 7214 | return -1; |
| 7215 | } |
| 7216 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7217 | } |
| 7218 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7219 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7220 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7221 | } |
| 7222 | } |
| 7223 | return 0; |
| 7224 | error: |
| 7225 | return -1; |
| 7226 | } |
| 7227 | |
| 7228 | |
| 7229 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7230 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7231 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7232 | int dest = 0; |
| 7233 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7234 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7235 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7236 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7237 | if (lineno < 0) { |
| 7238 | continue; |
| 7239 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7240 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7241 | if (prev_lineno == lineno) { |
| 7242 | continue; |
| 7243 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7244 | /* 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] | 7245 | if (src < bb->b_iused - 1) { |
| 7246 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7247 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7248 | bb->b_instr[src+1].i_lineno = lineno; |
| 7249 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7250 | } |
| 7251 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7252 | else { |
| 7253 | basicblock* next = bb->b_next; |
| 7254 | while (next && next->b_iused == 0) { |
| 7255 | next = next->b_next; |
| 7256 | } |
| 7257 | /* or if last instruction in BB and next BB has same line number */ |
| 7258 | if (next) { |
| 7259 | if (lineno == next->b_instr[0].i_lineno) { |
| 7260 | continue; |
| 7261 | } |
| 7262 | } |
| 7263 | } |
| 7264 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7265 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7266 | if (dest != src) { |
| 7267 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7268 | } |
| 7269 | dest++; |
| 7270 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7271 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7272 | assert(dest <= bb->b_iused); |
| 7273 | bb->b_iused = dest; |
| 7274 | } |
| 7275 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7276 | static int |
| 7277 | normalize_basic_block(basicblock *bb) { |
| 7278 | /* Mark blocks as exit and/or nofallthrough. |
| 7279 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7280 | for (int i = 0; i < bb->b_iused; i++) { |
| 7281 | switch(bb->b_instr[i].i_opcode) { |
| 7282 | case RETURN_VALUE: |
| 7283 | case RAISE_VARARGS: |
| 7284 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7285 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7286 | bb->b_nofallthrough = 1; |
| 7287 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7288 | case JUMP_ABSOLUTE: |
| 7289 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7290 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7291 | /* fall through */ |
| 7292 | case POP_JUMP_IF_FALSE: |
| 7293 | case POP_JUMP_IF_TRUE: |
| 7294 | case JUMP_IF_FALSE_OR_POP: |
| 7295 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7296 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7297 | if (i != bb->b_iused-1) { |
| 7298 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7299 | return -1; |
| 7300 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7301 | /* Skip over empty basic blocks. */ |
| 7302 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7303 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7304 | } |
| 7305 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7306 | } |
| 7307 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7308 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7309 | } |
| 7310 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7311 | static int |
| 7312 | mark_reachable(struct assembler *a) { |
| 7313 | basicblock **stack, **sp; |
| 7314 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7315 | if (stack == NULL) { |
| 7316 | return -1; |
| 7317 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7318 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7319 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7320 | while (sp > stack) { |
| 7321 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7322 | if (b->b_next && !b->b_nofallthrough) { |
| 7323 | if (b->b_next->b_predecessors == 0) { |
| 7324 | *sp++ = b->b_next; |
| 7325 | } |
| 7326 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7327 | } |
| 7328 | for (int i = 0; i < b->b_iused; i++) { |
| 7329 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7330 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7331 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7332 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7333 | *sp++ = target; |
| 7334 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7335 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7336 | } |
| 7337 | } |
| 7338 | } |
| 7339 | PyObject_Free(stack); |
| 7340 | return 0; |
| 7341 | } |
| 7342 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7343 | static void |
| 7344 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7345 | /* Eliminate empty blocks */ |
| 7346 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7347 | basicblock *next = b->b_next; |
| 7348 | if (next) { |
| 7349 | while (next->b_iused == 0 && next->b_next) { |
| 7350 | next = next->b_next; |
| 7351 | } |
| 7352 | b->b_next = next; |
| 7353 | } |
| 7354 | } |
| 7355 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7356 | if (b->b_iused == 0) { |
| 7357 | continue; |
| 7358 | } |
| 7359 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7360 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7361 | while (target->b_iused == 0) { |
| 7362 | target = target->b_next; |
| 7363 | } |
| 7364 | b->b_instr[b->b_iused-1].i_target = target; |
| 7365 | } |
| 7366 | } |
| 7367 | } |
| 7368 | |
| 7369 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7370 | /* 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] | 7371 | * then copy the line number. If a successor block has no line number, and only |
| 7372 | * one predecessor, then inherit the line number. |
| 7373 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7374 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7375 | * but has no impact on the generated line number events. |
| 7376 | */ |
| 7377 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7378 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7379 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7380 | if (b->b_iused == 0) { |
| 7381 | continue; |
| 7382 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7383 | int prev_lineno = -1; |
| 7384 | for (int i = 0; i < b->b_iused; i++) { |
| 7385 | if (b->b_instr[i].i_lineno < 0) { |
| 7386 | b->b_instr[i].i_lineno = prev_lineno; |
| 7387 | } |
| 7388 | else { |
| 7389 | prev_lineno = b->b_instr[i].i_lineno; |
| 7390 | } |
| 7391 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7392 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7393 | assert(b->b_next->b_iused); |
| 7394 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7395 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7396 | } |
| 7397 | } |
| 7398 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7399 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7400 | /* Note: Only actual jumps, not exception handlers */ |
| 7401 | case SETUP_ASYNC_WITH: |
| 7402 | case SETUP_WITH: |
| 7403 | case SETUP_FINALLY: |
| 7404 | continue; |
| 7405 | } |
| 7406 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7407 | if (target->b_predecessors == 1) { |
| 7408 | if (target->b_instr[0].i_lineno < 0) { |
| 7409 | target->b_instr[0].i_lineno = prev_lineno; |
| 7410 | } |
| 7411 | } |
| 7412 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7413 | } |
| 7414 | } |
| 7415 | |
| 7416 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7417 | The consts object should still be in list form to allow new constants |
| 7418 | to be appended. |
| 7419 | |
| 7420 | All transformations keep the code size the same or smaller. |
| 7421 | For those that reduce size, the gaps are initially filled with |
| 7422 | NOPs. Later those NOPs are removed. |
| 7423 | */ |
| 7424 | |
| 7425 | static int |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7426 | optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts) |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7427 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7428 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Inada Naoki | 8a232c7 | 2021-04-16 14:01:04 +0900 | [diff] [blame] | 7429 | if (optimize_basic_block(c, b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7430 | return -1; |
| 7431 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7432 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7433 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7434 | } |
| 7435 | if (mark_reachable(a)) { |
| 7436 | return -1; |
| 7437 | } |
| 7438 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7439 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7440 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7441 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7442 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7443 | } |
| 7444 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7445 | basicblock *pred = NULL; |
| 7446 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7447 | int prev_lineno = -1; |
| 7448 | if (pred && pred->b_iused) { |
| 7449 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7450 | } |
| 7451 | clean_basic_block(b, prev_lineno); |
| 7452 | pred = b->b_nofallthrough ? NULL : b; |
| 7453 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7454 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7455 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7456 | block ends with a jump instruction, check if the next non-empty block |
| 7457 | reached through normal flow control is the target of that jump. If it |
| 7458 | is, then the jump instruction is redundant and can be deleted. |
| 7459 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7460 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7461 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7462 | if (b->b_iused > 0) { |
| 7463 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7464 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7465 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7466 | if (b_last_instr->i_target == b->b_next) { |
| 7467 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7468 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7469 | b_last_instr->i_opcode = NOP; |
| 7470 | clean_basic_block(b, -1); |
| 7471 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7472 | } |
| 7473 | } |
| 7474 | } |
| 7475 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7476 | if (maybe_empty_blocks) { |
| 7477 | eliminate_empty_basic_blocks(a->a_entry); |
| 7478 | } |
| 7479 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7480 | return 0; |
| 7481 | } |
| 7482 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7483 | static inline int |
| 7484 | is_exit_without_lineno(basicblock *b) { |
| 7485 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7486 | } |
| 7487 | |
| 7488 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7489 | * after a frame terminates. It would be prohibitively expensive |
| 7490 | * to continuously update the f_lineno field at runtime, |
| 7491 | * so we make sure that all exiting instruction (raises and returns) |
| 7492 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7493 | * We can do this by duplicating the exit blocks without line number |
| 7494 | * so that none have more than one predecessor. We can then safely |
| 7495 | * copy the line number from the sole predecessor block. |
| 7496 | */ |
| 7497 | static int |
| 7498 | ensure_exits_have_lineno(struct compiler *c) |
| 7499 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7500 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7501 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7502 | */ |
| 7503 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7504 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7505 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7506 | /* Note: Only actual jumps, not exception handlers */ |
| 7507 | case SETUP_ASYNC_WITH: |
| 7508 | case SETUP_WITH: |
| 7509 | case SETUP_FINALLY: |
| 7510 | continue; |
| 7511 | } |
| 7512 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7513 | if (is_exit_without_lineno(target)) { |
| 7514 | basicblock *new_target = compiler_copy_block(c, target); |
| 7515 | if (new_target == NULL) { |
| 7516 | return -1; |
| 7517 | } |
| 7518 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7519 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 7520 | } |
| 7521 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7522 | entry = b; |
| 7523 | } |
| 7524 | assert(entry != NULL); |
| 7525 | if (is_exit_without_lineno(entry)) { |
| 7526 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7527 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7528 | /* Eliminate empty blocks */ |
| 7529 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7530 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7531 | b->b_next = b->b_next->b_next; |
| 7532 | } |
| 7533 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7534 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7535 | * fall through, and thus can only have a single predecessor */ |
| 7536 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7537 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7538 | if (is_exit_without_lineno(b->b_next)) { |
| 7539 | assert(b->b_next->b_iused > 0); |
| 7540 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7541 | } |
| 7542 | } |
| 7543 | } |
| 7544 | return 0; |
| 7545 | } |
| 7546 | |
| 7547 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7548 | /* Retained for API compatibility. |
| 7549 | * Optimization is now done in optimize_cfg */ |
| 7550 | |
| 7551 | PyObject * |
| 7552 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7553 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7554 | { |
| 7555 | Py_INCREF(code); |
| 7556 | return code; |
| 7557 | } |