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 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 46 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 47 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 48 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 49 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 50 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | unsigned char i_opcode; |
| 52 | int i_oparg; |
| 53 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 54 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 57 | #define LOG_BITS_PER_INT 5 |
| 58 | #define MASK_LOW_LOG_BITS 31 |
| 59 | |
| 60 | static inline int |
| 61 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 62 | /* Is the relevant bit set in the relevant word? */ |
| 63 | /* 256 bits fit into 8 32-bits words. |
| 64 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 65 | * Bit within word is the low bits of bitindex. |
| 66 | */ |
| 67 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 68 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 69 | } |
| 70 | |
| 71 | static inline int |
| 72 | is_relative_jump(struct instr *i) |
| 73 | { |
| 74 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 75 | } |
| 76 | |
| 77 | static inline int |
| 78 | is_jump(struct instr *i) |
| 79 | { |
| 80 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 81 | } |
| 82 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 83 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 85 | reverse order that the block are allocated. b_list points to the next |
| 86 | 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] | 87 | struct basicblock_ *b_list; |
| 88 | /* number of instructions used */ |
| 89 | int b_iused; |
| 90 | /* length of instruction array (b_instr) */ |
| 91 | int b_ialloc; |
| 92 | /* pointer to an array of instructions, initially NULL */ |
| 93 | struct instr *b_instr; |
| 94 | /* If b_next is non-NULL, it is a pointer to the next |
| 95 | block reached by normal control flow. */ |
| 96 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 98 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 99 | /* Number of predecssors that a block has. */ |
| 100 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 101 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 102 | unsigned b_nofallthrough : 1; |
| 103 | /* Basic block exits scope (it ends with a return or raise) */ |
| 104 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 106 | int b_startdepth; |
| 107 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 108 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | } basicblock; |
| 110 | |
| 111 | /* fblockinfo tracks the current frame block. |
| 112 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 113 | A frame block is used to handle loops, try/except, and try/finally. |
| 114 | It's called a frame block to distinguish it from a basic block in the |
| 115 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 116 | */ |
| 117 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 118 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame^] | 119 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER, |
| 120 | ASYNC_COMPREHENSION_GENERATOR }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 121 | |
| 122 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | enum fblocktype fb_type; |
| 124 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 125 | /* (optional) type-specific exit or cleanup block */ |
| 126 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 127 | /* (optional) additional information required for unwinding */ |
| 128 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 131 | enum { |
| 132 | COMPILER_SCOPE_MODULE, |
| 133 | COMPILER_SCOPE_CLASS, |
| 134 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 135 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 136 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 137 | COMPILER_SCOPE_COMPREHENSION, |
| 138 | }; |
| 139 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 140 | /* The following items change on entry and exit of code blocks. |
| 141 | They must be saved and restored when returning to a block. |
| 142 | */ |
| 143 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 147 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 148 | int u_scope_type; |
| 149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | /* The following fields are dicts that map objects to |
| 151 | the index of them in co_XXX. The index is used as |
| 152 | the argument for opcodes that refer to those collections. |
| 153 | */ |
| 154 | PyObject *u_consts; /* all constants */ |
| 155 | PyObject *u_names; /* all names */ |
| 156 | PyObject *u_varnames; /* local variables */ |
| 157 | PyObject *u_cellvars; /* cell variables */ |
| 158 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 161 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 162 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 163 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 164 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | /* Pointer to the most recently allocated block. By following b_list |
| 166 | members, you can reach all early allocated blocks. */ |
| 167 | basicblock *u_blocks; |
| 168 | basicblock *u_curblock; /* pointer to current block */ |
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 | int u_nfblocks; |
| 171 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | int u_firstlineno; /* the first lineno of the block */ |
| 174 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 175 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 179 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 180 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | 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] | 182 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 183 | |
| 184 | Note that we don't track recursion levels during compilation - the |
| 185 | task of detecting and rejecting excessive levels of nesting is |
| 186 | handled by the symbol analysis pass. |
| 187 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 188 | */ |
| 189 | |
| 190 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 191 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | struct symtable *c_st; |
| 193 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 194 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 195 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 196 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | int c_interactive; /* true if in interactive mode */ |
| 198 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 199 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 200 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | struct compiler_unit *u; /* compiler state for current block */ |
| 202 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 203 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 206 | typedef struct { |
| 207 | PyObject *stores; |
| 208 | int allow_irrefutable; |
| 209 | } pattern_context; |
| 210 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 211 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 212 | static void compiler_free(struct compiler *); |
| 213 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 214 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 215 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 216 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 217 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 218 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 219 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 220 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 221 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 222 | |
| 223 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 224 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 225 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 226 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 227 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 228 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 229 | static int compiler_subscript(struct compiler *, expr_ty); |
| 230 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 231 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 232 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 233 | 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] | 234 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 235 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 236 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 237 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 238 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 239 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 240 | asdl_expr_seq *args, |
| 241 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 242 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 243 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 244 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 245 | static int compiler_sync_comprehension_generator( |
| 246 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 247 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 248 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 249 | expr_ty elt, expr_ty val, int type); |
| 250 | |
| 251 | static int compiler_async_comprehension_generator( |
| 252 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 253 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 254 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 255 | expr_ty elt, expr_ty val, int type); |
| 256 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 257 | static int compiler_pattern(struct compiler *, expr_ty, pattern_context *); |
| 258 | static int compiler_match(struct compiler *, stmt_ty); |
| 259 | static int compiler_pattern_subpattern(struct compiler *, expr_ty, |
| 260 | pattern_context *); |
| 261 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 262 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 263 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 264 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 265 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 266 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 267 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 268 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | /* Name mangling: __private becomes _classname__private. |
| 271 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 272 | PyObject *result; |
| 273 | size_t nlen, plen, ipriv; |
| 274 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 276 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 277 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | Py_INCREF(ident); |
| 279 | return ident; |
| 280 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 281 | nlen = PyUnicode_GET_LENGTH(ident); |
| 282 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | The only time a name with a dot can occur is when |
| 286 | we are compiling an import statement that has a |
| 287 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | TODO(jhylton): Decide whether we want to support |
| 290 | mangling of the module name, e.g. __M.X. |
| 291 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 292 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 293 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 294 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | Py_INCREF(ident); |
| 296 | return ident; /* Don't mangle __whatever__ */ |
| 297 | } |
| 298 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 299 | ipriv = 0; |
| 300 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 301 | ipriv++; |
| 302 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | Py_INCREF(ident); |
| 304 | return ident; /* Don't mangle if class is just underscores */ |
| 305 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 306 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 308 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 309 | PyErr_SetString(PyExc_OverflowError, |
| 310 | "private identifier too large to be mangled"); |
| 311 | return NULL; |
| 312 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 314 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 315 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 316 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 317 | |
| 318 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 319 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 321 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 322 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 323 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 324 | Py_DECREF(result); |
| 325 | return NULL; |
| 326 | } |
| 327 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 328 | Py_DECREF(result); |
| 329 | return NULL; |
| 330 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 331 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 332 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 335 | static int |
| 336 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 337 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 339 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 340 | c->c_const_cache = PyDict_New(); |
| 341 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | c->c_stack = PyList_New(0); |
| 346 | if (!c->c_stack) { |
| 347 | Py_CLEAR(c->c_const_cache); |
| 348 | return 0; |
| 349 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | PyCodeObject * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 355 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 356 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | struct compiler c; |
| 359 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 360 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | if (!__doc__) { |
| 364 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 365 | if (!__doc__) |
| 366 | return NULL; |
| 367 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 368 | if (!__annotations__) { |
| 369 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 370 | if (!__annotations__) |
| 371 | return NULL; |
| 372 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | if (!compiler_init(&c)) |
| 374 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 375 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | c.c_filename = filename; |
| 377 | c.c_arena = arena; |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 378 | c.c_future = _PyFuture_FromAST(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | if (c.c_future == NULL) |
| 380 | goto finally; |
| 381 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | flags = &local_flags; |
| 383 | } |
| 384 | merged = c.c_future->ff_features | flags->cf_flags; |
| 385 | c.c_future->ff_features = merged; |
| 386 | flags->cf_flags = merged; |
| 387 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 388 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 390 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 391 | _PyASTOptimizeState state; |
| 392 | state.optimize = c.c_optimize; |
| 393 | state.ff_features = merged; |
| 394 | |
| 395 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 396 | goto finally; |
| 397 | } |
| 398 | |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 399 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | if (c.c_st == NULL) { |
| 401 | if (!PyErr_Occurred()) |
| 402 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 403 | goto finally; |
| 404 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 407 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 408 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | compiler_free(&c); |
| 410 | assert(co || PyErr_Occurred()); |
| 411 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 414 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 415 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | if (c->c_st) |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 418 | _PySymtable_Free(c->c_st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | if (c->c_future) |
| 420 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 421 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 422 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 426 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 427 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | Py_ssize_t i, n; |
| 430 | PyObject *v, *k; |
| 431 | PyObject *dict = PyDict_New(); |
| 432 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | n = PyList_Size(list); |
| 435 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 436 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | if (!v) { |
| 438 | Py_DECREF(dict); |
| 439 | return NULL; |
| 440 | } |
| 441 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 442 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | Py_DECREF(v); |
| 444 | Py_DECREF(dict); |
| 445 | return NULL; |
| 446 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | Py_DECREF(v); |
| 448 | } |
| 449 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Return new dict containing names from src that match scope(s). |
| 453 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 454 | 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] | 455 | 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] | 456 | values are integers, starting at offset and increasing by one for |
| 457 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 458 | */ |
| 459 | |
| 460 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 461 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 462 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 463 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 465 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | assert(offset >= 0); |
| 468 | if (dest == NULL) |
| 469 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 470 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 471 | /* Sort the keys so that we have a deterministic order on the indexes |
| 472 | saved in the returned dictionary. These indexes are used as indexes |
| 473 | into the free and cell var storage. Therefore if they aren't |
| 474 | deterministic, then the generated bytecode is not deterministic. |
| 475 | */ |
| 476 | sorted_keys = PyDict_Keys(src); |
| 477 | if (sorted_keys == NULL) |
| 478 | return NULL; |
| 479 | if (PyList_Sort(sorted_keys) != 0) { |
| 480 | Py_DECREF(sorted_keys); |
| 481 | return NULL; |
| 482 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 483 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 484 | |
| 485 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | /* XXX this should probably be a macro in symtable.h */ |
| 487 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 488 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 489 | v = PyDict_GetItemWithError(src, k); |
| 490 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | vi = PyLong_AS_LONG(v); |
| 492 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 495 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 497 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | Py_DECREF(dest); |
| 499 | return NULL; |
| 500 | } |
| 501 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 502 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 503 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | Py_DECREF(item); |
| 505 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | return NULL; |
| 507 | } |
| 508 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 511 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 515 | static void |
| 516 | compiler_unit_check(struct compiler_unit *u) |
| 517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | basicblock *block; |
| 519 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 520 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | if (block->b_instr != NULL) { |
| 522 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 523 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | assert(block->b_ialloc >= block->b_iused); |
| 525 | } |
| 526 | else { |
| 527 | assert (block->b_iused == 0); |
| 528 | assert (block->b_ialloc == 0); |
| 529 | } |
| 530 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static void |
| 534 | compiler_unit_free(struct compiler_unit *u) |
| 535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | compiler_unit_check(u); |
| 539 | b = u->u_blocks; |
| 540 | while (b != NULL) { |
| 541 | if (b->b_instr) |
| 542 | PyObject_Free((void *)b->b_instr); |
| 543 | next = b->b_list; |
| 544 | PyObject_Free((void *)b); |
| 545 | b = next; |
| 546 | } |
| 547 | Py_CLEAR(u->u_ste); |
| 548 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 549 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | Py_CLEAR(u->u_consts); |
| 551 | Py_CLEAR(u->u_names); |
| 552 | Py_CLEAR(u->u_varnames); |
| 553 | Py_CLEAR(u->u_freevars); |
| 554 | Py_CLEAR(u->u_cellvars); |
| 555 | Py_CLEAR(u->u_private); |
| 556 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 560 | compiler_enter_scope(struct compiler *c, identifier name, |
| 561 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 564 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 565 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 566 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | struct compiler_unit)); |
| 568 | if (!u) { |
| 569 | PyErr_NoMemory(); |
| 570 | return 0; |
| 571 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 572 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 574 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | u->u_kwonlyargcount = 0; |
| 576 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 577 | if (!u->u_ste) { |
| 578 | compiler_unit_free(u); |
| 579 | return 0; |
| 580 | } |
| 581 | Py_INCREF(name); |
| 582 | u->u_name = name; |
| 583 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 584 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 585 | if (!u->u_varnames || !u->u_cellvars) { |
| 586 | compiler_unit_free(u); |
| 587 | return 0; |
| 588 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 589 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 590 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 591 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 592 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 593 | int res; |
| 594 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 595 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 596 | name = _PyUnicode_FromId(&PyId___class__); |
| 597 | if (!name) { |
| 598 | compiler_unit_free(u); |
| 599 | return 0; |
| 600 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 601 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 602 | if (res < 0) { |
| 603 | compiler_unit_free(u); |
| 604 | return 0; |
| 605 | } |
| 606 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | 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] | 609 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | if (!u->u_freevars) { |
| 611 | compiler_unit_free(u); |
| 612 | return 0; |
| 613 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | u->u_blocks = NULL; |
| 616 | u->u_nfblocks = 0; |
| 617 | u->u_firstlineno = lineno; |
| 618 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 619 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | u->u_consts = PyDict_New(); |
| 621 | if (!u->u_consts) { |
| 622 | compiler_unit_free(u); |
| 623 | return 0; |
| 624 | } |
| 625 | u->u_names = PyDict_New(); |
| 626 | if (!u->u_names) { |
| 627 | compiler_unit_free(u); |
| 628 | return 0; |
| 629 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | /* Push the old compiler_unit on the stack. */ |
| 634 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 635 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 637 | Py_XDECREF(capsule); |
| 638 | compiler_unit_free(u); |
| 639 | return 0; |
| 640 | } |
| 641 | Py_DECREF(capsule); |
| 642 | u->u_private = c->u->u_private; |
| 643 | Py_XINCREF(u->u_private); |
| 644 | } |
| 645 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 648 | |
| 649 | block = compiler_new_block(c); |
| 650 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 652 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 653 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 654 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 655 | if (!compiler_set_qualname(c)) |
| 656 | return 0; |
| 657 | } |
| 658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 662 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 663 | compiler_exit_scope(struct compiler *c) |
| 664 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 665 | // Don't call PySequence_DelItem() with an exception raised |
| 666 | PyObject *exc_type, *exc_val, *exc_tb; |
| 667 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | c->c_nestlevel--; |
| 670 | compiler_unit_free(c->u); |
| 671 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 672 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 674 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 675 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | assert(c->u); |
| 677 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 678 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 679 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 680 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 681 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | compiler_unit_check(c->u); |
| 683 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 684 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 686 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 687 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 688 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 691 | static int |
| 692 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 693 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 694 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 695 | _Py_static_string(dot_locals, ".<locals>"); |
| 696 | Py_ssize_t stack_size; |
| 697 | struct compiler_unit *u = c->u; |
| 698 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 699 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 700 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 701 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 702 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 703 | if (stack_size > 1) { |
| 704 | int scope, force_global = 0; |
| 705 | struct compiler_unit *parent; |
| 706 | PyObject *mangled, *capsule; |
| 707 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 708 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 709 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 710 | assert(parent); |
| 711 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 712 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 713 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 714 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 715 | assert(u->u_name); |
| 716 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 717 | if (!mangled) |
| 718 | return 0; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 719 | scope = _PyST_GetScope(parent->u_ste, mangled); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 720 | Py_DECREF(mangled); |
| 721 | assert(scope != GLOBAL_IMPLICIT); |
| 722 | if (scope == GLOBAL_EXPLICIT) |
| 723 | force_global = 1; |
| 724 | } |
| 725 | |
| 726 | if (!force_global) { |
| 727 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 728 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 729 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 730 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 731 | if (dot_locals_str == NULL) |
| 732 | return 0; |
| 733 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 734 | if (base == NULL) |
| 735 | return 0; |
| 736 | } |
| 737 | else { |
| 738 | Py_INCREF(parent->u_qualname); |
| 739 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 740 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 743 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 744 | if (base != NULL) { |
| 745 | dot_str = _PyUnicode_FromId(&dot); |
| 746 | if (dot_str == NULL) { |
| 747 | Py_DECREF(base); |
| 748 | return 0; |
| 749 | } |
| 750 | name = PyUnicode_Concat(base, dot_str); |
| 751 | Py_DECREF(base); |
| 752 | if (name == NULL) |
| 753 | return 0; |
| 754 | PyUnicode_Append(&name, u->u_name); |
| 755 | if (name == NULL) |
| 756 | return 0; |
| 757 | } |
| 758 | else { |
| 759 | Py_INCREF(u->u_name); |
| 760 | name = u->u_name; |
| 761 | } |
| 762 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 763 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 764 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 767 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | /* Allocate a new block and return a pointer to it. |
| 769 | Returns NULL on error. |
| 770 | */ |
| 771 | |
| 772 | static basicblock * |
| 773 | compiler_new_block(struct compiler *c) |
| 774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | basicblock *b; |
| 776 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 779 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | if (b == NULL) { |
| 781 | PyErr_NoMemory(); |
| 782 | return NULL; |
| 783 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | /* Extend the singly linked list of blocks with new block. */ |
| 785 | b->b_list = u->u_blocks; |
| 786 | u->u_blocks = b; |
| 787 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | compiler_next_block(struct compiler *c) |
| 792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | basicblock *block = compiler_new_block(c); |
| 794 | if (block == NULL) |
| 795 | return NULL; |
| 796 | c->u->u_curblock->b_next = block; |
| 797 | c->u->u_curblock = block; |
| 798 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static basicblock * |
| 802 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | assert(block != NULL); |
| 805 | c->u->u_curblock->b_next = block; |
| 806 | c->u->u_curblock = block; |
| 807 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 810 | static basicblock * |
| 811 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 812 | { |
| 813 | /* Cannot copy a block if it has a fallthrough, since |
| 814 | * a block can only have one fallthrough predecessor. |
| 815 | */ |
| 816 | assert(block->b_nofallthrough); |
| 817 | basicblock *result = compiler_next_block(c); |
| 818 | if (result == NULL) { |
| 819 | return NULL; |
| 820 | } |
| 821 | for (int i = 0; i < block->b_iused; i++) { |
| 822 | int n = compiler_next_instr(result); |
| 823 | if (n < 0) { |
| 824 | return NULL; |
| 825 | } |
| 826 | result->b_instr[n] = block->b_instr[i]; |
| 827 | } |
| 828 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 829 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 830 | return result; |
| 831 | } |
| 832 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 833 | /* Returns the offset of the next instruction in the current block's |
| 834 | b_instr array. Resizes the b_instr as necessary. |
| 835 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 836 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | |
| 838 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 839 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | assert(b != NULL); |
| 842 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 843 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 844 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | if (b->b_instr == NULL) { |
| 846 | PyErr_NoMemory(); |
| 847 | return -1; |
| 848 | } |
| 849 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | } |
| 851 | else if (b->b_iused == b->b_ialloc) { |
| 852 | struct instr *tmp; |
| 853 | size_t oldsize, newsize; |
| 854 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 855 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 856 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 857 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | PyErr_NoMemory(); |
| 859 | return -1; |
| 860 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | if (newsize == 0) { |
| 863 | PyErr_NoMemory(); |
| 864 | return -1; |
| 865 | } |
| 866 | b->b_ialloc <<= 1; |
| 867 | tmp = (struct instr *)PyObject_Realloc( |
| 868 | (void *)b->b_instr, newsize); |
| 869 | if (tmp == NULL) { |
| 870 | PyErr_NoMemory(); |
| 871 | return -1; |
| 872 | } |
| 873 | b->b_instr = tmp; |
| 874 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 875 | } |
| 876 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 879 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 880 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 881 | The line number is reset in the following cases: |
| 882 | - when entering a new scope |
| 883 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 884 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 885 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 886 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 887 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 888 | #define SET_LOC(c, x) \ |
| 889 | (c)->u->u_lineno = (x)->lineno; \ |
| 890 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 891 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 892 | /* Return the stack effect of opcode with argument oparg. |
| 893 | |
| 894 | Some opcodes have different stack effect when jump to the target and |
| 895 | when not jump. The 'jump' parameter specifies the case: |
| 896 | |
| 897 | * 0 -- when not jump |
| 898 | * 1 -- when jump |
| 899 | * -1 -- maximal |
| 900 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 901 | static int |
| 902 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 903 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 905 | case NOP: |
| 906 | case EXTENDED_ARG: |
| 907 | return 0; |
| 908 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 909 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | case POP_TOP: |
| 911 | return -1; |
| 912 | case ROT_TWO: |
| 913 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 914 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | return 0; |
| 916 | case DUP_TOP: |
| 917 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 918 | case DUP_TOP_TWO: |
| 919 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 920 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 921 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | case UNARY_POSITIVE: |
| 923 | case UNARY_NEGATIVE: |
| 924 | case UNARY_NOT: |
| 925 | case UNARY_INVERT: |
| 926 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | case SET_ADD: |
| 929 | case LIST_APPEND: |
| 930 | return -1; |
| 931 | case MAP_ADD: |
| 932 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 933 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 934 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | case BINARY_POWER: |
| 936 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 937 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | case BINARY_MODULO: |
| 939 | case BINARY_ADD: |
| 940 | case BINARY_SUBTRACT: |
| 941 | case BINARY_SUBSCR: |
| 942 | case BINARY_FLOOR_DIVIDE: |
| 943 | case BINARY_TRUE_DIVIDE: |
| 944 | return -1; |
| 945 | case INPLACE_FLOOR_DIVIDE: |
| 946 | case INPLACE_TRUE_DIVIDE: |
| 947 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | case INPLACE_ADD: |
| 950 | case INPLACE_SUBTRACT: |
| 951 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 952 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | case INPLACE_MODULO: |
| 954 | return -1; |
| 955 | case STORE_SUBSCR: |
| 956 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | case DELETE_SUBSCR: |
| 958 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | case BINARY_LSHIFT: |
| 961 | case BINARY_RSHIFT: |
| 962 | case BINARY_AND: |
| 963 | case BINARY_XOR: |
| 964 | case BINARY_OR: |
| 965 | return -1; |
| 966 | case INPLACE_POWER: |
| 967 | return -1; |
| 968 | case GET_ITER: |
| 969 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | case PRINT_EXPR: |
| 972 | return -1; |
| 973 | case LOAD_BUILD_CLASS: |
| 974 | return 1; |
| 975 | case INPLACE_LSHIFT: |
| 976 | case INPLACE_RSHIFT: |
| 977 | case INPLACE_AND: |
| 978 | case INPLACE_XOR: |
| 979 | case INPLACE_OR: |
| 980 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 983 | /* 1 in the normal flow. |
| 984 | * Restore the stack position and push 6 values before jumping to |
| 985 | * the handler if an exception be raised. */ |
| 986 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | case RETURN_VALUE: |
| 988 | return -1; |
| 989 | case IMPORT_STAR: |
| 990 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 991 | case SETUP_ANNOTATIONS: |
| 992 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | case YIELD_VALUE: |
| 994 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 995 | case YIELD_FROM: |
| 996 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | case POP_BLOCK: |
| 998 | return 0; |
| 999 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1000 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | case STORE_NAME: |
| 1003 | return -1; |
| 1004 | case DELETE_NAME: |
| 1005 | return 0; |
| 1006 | case UNPACK_SEQUENCE: |
| 1007 | return oparg-1; |
| 1008 | case UNPACK_EX: |
| 1009 | return (oparg&0xFF) + (oparg>>8); |
| 1010 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1011 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1012 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | case STORE_ATTR: |
| 1015 | return -2; |
| 1016 | case DELETE_ATTR: |
| 1017 | return -1; |
| 1018 | case STORE_GLOBAL: |
| 1019 | return -1; |
| 1020 | case DELETE_GLOBAL: |
| 1021 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | case LOAD_CONST: |
| 1023 | return 1; |
| 1024 | case LOAD_NAME: |
| 1025 | return 1; |
| 1026 | case BUILD_TUPLE: |
| 1027 | case BUILD_LIST: |
| 1028 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1029 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | return 1-oparg; |
| 1031 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1032 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1033 | case BUILD_CONST_KEY_MAP: |
| 1034 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | case LOAD_ATTR: |
| 1036 | return 0; |
| 1037 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1038 | case IS_OP: |
| 1039 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1041 | case JUMP_IF_NOT_EXC_MATCH: |
| 1042 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | case IMPORT_NAME: |
| 1044 | return -1; |
| 1045 | case IMPORT_FROM: |
| 1046 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1047 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1048 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | case JUMP_ABSOLUTE: |
| 1051 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1052 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1053 | case JUMP_IF_TRUE_OR_POP: |
| 1054 | case JUMP_IF_FALSE_OR_POP: |
| 1055 | return jump ? 0 : -1; |
| 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | case POP_JUMP_IF_FALSE: |
| 1058 | case POP_JUMP_IF_TRUE: |
| 1059 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | case LOAD_GLOBAL: |
| 1062 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1063 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1064 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1066 | /* 0 in the normal flow. |
| 1067 | * Restore the stack position and push 6 values before jumping to |
| 1068 | * the handler if an exception be raised. */ |
| 1069 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1070 | case RERAISE: |
| 1071 | return -3; |
| 1072 | |
| 1073 | case WITH_EXCEPT_START: |
| 1074 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | case LOAD_FAST: |
| 1077 | return 1; |
| 1078 | case STORE_FAST: |
| 1079 | return -1; |
| 1080 | case DELETE_FAST: |
| 1081 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | case RAISE_VARARGS: |
| 1084 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1085 | |
| 1086 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1088 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1089 | case CALL_METHOD: |
| 1090 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1092 | return -oparg-1; |
| 1093 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1094 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1095 | case MAKE_FUNCTION: |
| 1096 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1097 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | case BUILD_SLICE: |
| 1099 | if (oparg == 3) |
| 1100 | return -2; |
| 1101 | else |
| 1102 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1103 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1104 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | case LOAD_CLOSURE: |
| 1106 | return 1; |
| 1107 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1108 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | return 1; |
| 1110 | case STORE_DEREF: |
| 1111 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1112 | case DELETE_DEREF: |
| 1113 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1114 | |
| 1115 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1116 | case GET_AWAITABLE: |
| 1117 | return 0; |
| 1118 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1119 | /* 0 in the normal flow. |
| 1120 | * Restore the stack position to the position before the result |
| 1121 | * of __aenter__ and push 6 values before jumping to the handler |
| 1122 | * if an exception be raised. */ |
| 1123 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1124 | case BEFORE_ASYNC_WITH: |
| 1125 | return 1; |
| 1126 | case GET_AITER: |
| 1127 | return 0; |
| 1128 | case GET_ANEXT: |
| 1129 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1130 | case GET_YIELD_FROM_ITER: |
| 1131 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1132 | case END_ASYNC_FOR: |
| 1133 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1134 | case FORMAT_VALUE: |
| 1135 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1136 | else 1->1. */ |
| 1137 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1138 | case LOAD_METHOD: |
| 1139 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1140 | case LOAD_ASSERTION_ERROR: |
| 1141 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1142 | case LIST_TO_TUPLE: |
| 1143 | return 0; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 1144 | case GEN_START: |
| 1145 | return -1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1146 | case LIST_EXTEND: |
| 1147 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1148 | case DICT_MERGE: |
| 1149 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1150 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1151 | case COPY_DICT_WITHOUT_KEYS: |
| 1152 | return 0; |
| 1153 | case MATCH_CLASS: |
| 1154 | return -1; |
| 1155 | case GET_LEN: |
| 1156 | case MATCH_MAPPING: |
| 1157 | case MATCH_SEQUENCE: |
| 1158 | return 1; |
| 1159 | case MATCH_KEYS: |
| 1160 | return 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1162 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1164 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1167 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1168 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1169 | { |
| 1170 | return stack_effect(opcode, oparg, jump); |
| 1171 | } |
| 1172 | |
| 1173 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1174 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1175 | { |
| 1176 | return stack_effect(opcode, oparg, -1); |
| 1177 | } |
| 1178 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1179 | /* Add an opcode with no argument. |
| 1180 | Returns 0 on failure, 1 on success. |
| 1181 | */ |
| 1182 | |
| 1183 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1184 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1185 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | basicblock *b; |
| 1187 | struct instr *i; |
| 1188 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1189 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1190 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | if (off < 0) |
| 1192 | return 0; |
| 1193 | b = c->u->u_curblock; |
| 1194 | i = &b->b_instr[off]; |
| 1195 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1196 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | if (opcode == RETURN_VALUE) |
| 1198 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1199 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1203 | static int |
| 1204 | compiler_addop(struct compiler *c, int opcode) |
| 1205 | { |
| 1206 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1207 | } |
| 1208 | |
| 1209 | static int |
| 1210 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1211 | { |
| 1212 | return compiler_addop_line(c, opcode, -1); |
| 1213 | } |
| 1214 | |
| 1215 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1216 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1217 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1218 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1219 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1221 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1222 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1224 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1226 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1227 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1228 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return -1; |
| 1231 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1232 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | Py_DECREF(v); |
| 1234 | return -1; |
| 1235 | } |
| 1236 | Py_DECREF(v); |
| 1237 | } |
| 1238 | else |
| 1239 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1240 | return arg; |
| 1241 | } |
| 1242 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1243 | // Merge const *o* recursively and return constant key object. |
| 1244 | static PyObject* |
| 1245 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1246 | { |
| 1247 | // None and Ellipsis are singleton, and key is the singleton. |
| 1248 | // No need to merge object and key. |
| 1249 | if (o == Py_None || o == Py_Ellipsis) { |
| 1250 | Py_INCREF(o); |
| 1251 | return o; |
| 1252 | } |
| 1253 | |
| 1254 | PyObject *key = _PyCode_ConstantKey(o); |
| 1255 | if (key == NULL) { |
| 1256 | return NULL; |
| 1257 | } |
| 1258 | |
| 1259 | // t is borrowed reference |
| 1260 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1261 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1262 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1263 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1264 | Py_DECREF(key); |
| 1265 | return t; |
| 1266 | } |
| 1267 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1268 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1269 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1270 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1271 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1272 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1273 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1274 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1275 | PyObject *u = merge_consts_recursive(c, item); |
| 1276 | if (u == NULL) { |
| 1277 | Py_DECREF(key); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
| 1281 | // See _PyCode_ConstantKey() |
| 1282 | PyObject *v; // borrowed |
| 1283 | if (PyTuple_CheckExact(u)) { |
| 1284 | v = PyTuple_GET_ITEM(u, 1); |
| 1285 | } |
| 1286 | else { |
| 1287 | v = u; |
| 1288 | } |
| 1289 | if (v != item) { |
| 1290 | Py_INCREF(v); |
| 1291 | PyTuple_SET_ITEM(o, i, v); |
| 1292 | Py_DECREF(item); |
| 1293 | } |
| 1294 | |
| 1295 | Py_DECREF(u); |
| 1296 | } |
| 1297 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1298 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1299 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1300 | // constant keys. |
| 1301 | // See _PyCode_ConstantKey() for detail. |
| 1302 | assert(PyTuple_CheckExact(key)); |
| 1303 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1304 | |
| 1305 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1306 | if (len == 0) { // empty frozenset should not be re-created. |
| 1307 | return key; |
| 1308 | } |
| 1309 | PyObject *tuple = PyTuple_New(len); |
| 1310 | if (tuple == NULL) { |
| 1311 | Py_DECREF(key); |
| 1312 | return NULL; |
| 1313 | } |
| 1314 | Py_ssize_t i = 0, pos = 0; |
| 1315 | PyObject *item; |
| 1316 | Py_hash_t hash; |
| 1317 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1318 | PyObject *k = merge_consts_recursive(c, item); |
| 1319 | if (k == NULL) { |
| 1320 | Py_DECREF(tuple); |
| 1321 | Py_DECREF(key); |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | PyObject *u; |
| 1325 | if (PyTuple_CheckExact(k)) { |
| 1326 | u = PyTuple_GET_ITEM(k, 1); |
| 1327 | Py_INCREF(u); |
| 1328 | Py_DECREF(k); |
| 1329 | } |
| 1330 | else { |
| 1331 | u = k; |
| 1332 | } |
| 1333 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1334 | i++; |
| 1335 | } |
| 1336 | |
| 1337 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1338 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1339 | PyObject *new = PyFrozenSet_New(tuple); |
| 1340 | Py_DECREF(tuple); |
| 1341 | if (new == NULL) { |
| 1342 | Py_DECREF(key); |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1346 | Py_DECREF(o); |
| 1347 | PyTuple_SET_ITEM(key, 1, new); |
| 1348 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1349 | |
| 1350 | return key; |
| 1351 | } |
| 1352 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1353 | static Py_ssize_t |
| 1354 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1355 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1356 | PyObject *key = merge_consts_recursive(c, o); |
| 1357 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1358 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1359 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1360 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1361 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1362 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1367 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1368 | { |
| 1369 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1370 | if (arg < 0) |
| 1371 | return 0; |
| 1372 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1373 | } |
| 1374 | |
| 1375 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1376 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1378 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1379 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1380 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1381 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1382 | return compiler_addop_i(c, opcode, arg); |
| 1383 | } |
| 1384 | |
| 1385 | static int |
| 1386 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1388 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1389 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1390 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1391 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1392 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1393 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1394 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1395 | Py_DECREF(mangled); |
| 1396 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1397 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1398 | return compiler_addop_i(c, opcode, arg); |
| 1399 | } |
| 1400 | |
| 1401 | /* Add an opcode with an integer argument. |
| 1402 | Returns 0 on failure, 1 on success. |
| 1403 | */ |
| 1404 | |
| 1405 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1406 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | struct instr *i; |
| 1409 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1410 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1411 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1412 | it in the C code (like Python/ceval.c). |
| 1413 | |
| 1414 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1415 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1416 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1417 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1418 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1419 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1420 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1421 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | if (off < 0) |
| 1423 | return 0; |
| 1424 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1425 | i->i_opcode = opcode; |
| 1426 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1427 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1431 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1432 | { |
| 1433 | assert(HAS_ARG(opcode)); |
| 1434 | assert(b != NULL); |
| 1435 | assert(target != NULL); |
| 1436 | |
| 1437 | int off = compiler_next_instr(b); |
| 1438 | struct instr *i = &b->b_instr[off]; |
| 1439 | if (off < 0) { |
| 1440 | return 0; |
| 1441 | } |
| 1442 | i->i_opcode = opcode; |
| 1443 | i->i_target = target; |
| 1444 | i->i_lineno = lineno; |
| 1445 | return 1; |
| 1446 | } |
| 1447 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1448 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1449 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1450 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1451 | 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] | 1452 | } |
| 1453 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1454 | static int |
| 1455 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1456 | { |
| 1457 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1458 | } |
| 1459 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1460 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1461 | to the new block. |
| 1462 | |
| 1463 | The returns inside this macro make it impossible to decref objects |
| 1464 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1465 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | if (compiler_next_block((C)) == NULL) \ |
| 1468 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | if (!compiler_addop((C), (OP))) \ |
| 1473 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1476 | #define ADDOP_NOLINE(C, OP) { \ |
| 1477 | if (!compiler_addop_noline((C), (OP))) \ |
| 1478 | return 0; \ |
| 1479 | } |
| 1480 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1481 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | if (!compiler_addop((C), (OP))) { \ |
| 1483 | compiler_exit_scope(c); \ |
| 1484 | return 0; \ |
| 1485 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1488 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1489 | if (!compiler_addop_load_const((C), (O))) \ |
| 1490 | return 0; \ |
| 1491 | } |
| 1492 | |
| 1493 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1494 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1495 | PyObject *__new_const = (O); \ |
| 1496 | if (__new_const == NULL) { \ |
| 1497 | return 0; \ |
| 1498 | } \ |
| 1499 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1500 | Py_DECREF(__new_const); \ |
| 1501 | return 0; \ |
| 1502 | } \ |
| 1503 | Py_DECREF(__new_const); \ |
| 1504 | } |
| 1505 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1508 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1511 | /* Same as ADDOP_O, but steals a reference. */ |
| 1512 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1513 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1514 | Py_DECREF((O)); \ |
| 1515 | return 0; \ |
| 1516 | } \ |
| 1517 | Py_DECREF((O)); \ |
| 1518 | } |
| 1519 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1522 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1527 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1530 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1531 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1535 | /* Add a jump with no line number. |
| 1536 | * Used for artificial jumps that have no corresponding |
| 1537 | * token in the source code. */ |
| 1538 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1539 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1540 | return 0; \ |
| 1541 | } |
| 1542 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1543 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1544 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1545 | return 0; \ |
| 1546 | } |
| 1547 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1548 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1549 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1550 | */ |
| 1551 | |
| 1552 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1554 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1557 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1559 | compiler_exit_scope(c); \ |
| 1560 | return 0; \ |
| 1561 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1564 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1566 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1571 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1573 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1574 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1575 | return 0; \ |
| 1576 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1579 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1581 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1583 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1584 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1585 | compiler_exit_scope(c); \ |
| 1586 | return 0; \ |
| 1587 | } \ |
| 1588 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1591 | #define RETURN_IF_FALSE(X) \ |
| 1592 | if (!(X)) { \ |
| 1593 | return 0; \ |
| 1594 | } |
| 1595 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1596 | /* Search if variable annotations are present statically in a block. */ |
| 1597 | |
| 1598 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1599 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1600 | { |
| 1601 | int i, j, res = 0; |
| 1602 | stmt_ty st; |
| 1603 | |
| 1604 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1605 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1606 | switch (st->kind) { |
| 1607 | case AnnAssign_kind: |
| 1608 | return 1; |
| 1609 | case For_kind: |
| 1610 | res = find_ann(st->v.For.body) || |
| 1611 | find_ann(st->v.For.orelse); |
| 1612 | break; |
| 1613 | case AsyncFor_kind: |
| 1614 | res = find_ann(st->v.AsyncFor.body) || |
| 1615 | find_ann(st->v.AsyncFor.orelse); |
| 1616 | break; |
| 1617 | case While_kind: |
| 1618 | res = find_ann(st->v.While.body) || |
| 1619 | find_ann(st->v.While.orelse); |
| 1620 | break; |
| 1621 | case If_kind: |
| 1622 | res = find_ann(st->v.If.body) || |
| 1623 | find_ann(st->v.If.orelse); |
| 1624 | break; |
| 1625 | case With_kind: |
| 1626 | res = find_ann(st->v.With.body); |
| 1627 | break; |
| 1628 | case AsyncWith_kind: |
| 1629 | res = find_ann(st->v.AsyncWith.body); |
| 1630 | break; |
| 1631 | case Try_kind: |
| 1632 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1633 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1634 | st->v.Try.handlers, j); |
| 1635 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1636 | return 1; |
| 1637 | } |
| 1638 | } |
| 1639 | res = find_ann(st->v.Try.body) || |
| 1640 | find_ann(st->v.Try.finalbody) || |
| 1641 | find_ann(st->v.Try.orelse); |
| 1642 | break; |
| 1643 | default: |
| 1644 | res = 0; |
| 1645 | } |
| 1646 | if (res) { |
| 1647 | break; |
| 1648 | } |
| 1649 | } |
| 1650 | return res; |
| 1651 | } |
| 1652 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1653 | /* |
| 1654 | * Frame block handling functions |
| 1655 | */ |
| 1656 | |
| 1657 | static int |
| 1658 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1659 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1660 | { |
| 1661 | struct fblockinfo *f; |
| 1662 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1663 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1664 | } |
| 1665 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1666 | f->fb_type = t; |
| 1667 | f->fb_block = b; |
| 1668 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1669 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1670 | return 1; |
| 1671 | } |
| 1672 | |
| 1673 | static void |
| 1674 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1675 | { |
| 1676 | struct compiler_unit *u = c->u; |
| 1677 | assert(u->u_nfblocks > 0); |
| 1678 | u->u_nfblocks--; |
| 1679 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1680 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1681 | } |
| 1682 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1683 | static int |
| 1684 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1685 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1686 | ADDOP(c, DUP_TOP); |
| 1687 | ADDOP(c, DUP_TOP); |
| 1688 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1689 | return 1; |
| 1690 | } |
| 1691 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1692 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1693 | * popping the blocks will be restored afterwards, unless another |
| 1694 | * return, break or continue is found. In which case, the TOS will |
| 1695 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1696 | */ |
| 1697 | static int |
| 1698 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1699 | int preserve_tos) |
| 1700 | { |
| 1701 | switch (info->fb_type) { |
| 1702 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1703 | case EXCEPTION_HANDLER: |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame^] | 1704 | case ASYNC_COMPREHENSION_GENERATOR: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1705 | return 1; |
| 1706 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1707 | case FOR_LOOP: |
| 1708 | /* Pop the iterator */ |
| 1709 | if (preserve_tos) { |
| 1710 | ADDOP(c, ROT_TWO); |
| 1711 | } |
| 1712 | ADDOP(c, POP_TOP); |
| 1713 | return 1; |
| 1714 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1715 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1716 | ADDOP(c, POP_BLOCK); |
| 1717 | return 1; |
| 1718 | |
| 1719 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1720 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1721 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1722 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1723 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1724 | return 0; |
| 1725 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1726 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1727 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1728 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1729 | if (preserve_tos) { |
| 1730 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1731 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1732 | /* The finally block should appear to execute after the |
| 1733 | * statement causing the unwinding, so make the unwinding |
| 1734 | * instruction artificial */ |
| 1735 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1736 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1737 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1738 | case FINALLY_END: |
| 1739 | if (preserve_tos) { |
| 1740 | ADDOP(c, ROT_FOUR); |
| 1741 | } |
| 1742 | ADDOP(c, POP_TOP); |
| 1743 | ADDOP(c, POP_TOP); |
| 1744 | ADDOP(c, POP_TOP); |
| 1745 | if (preserve_tos) { |
| 1746 | ADDOP(c, ROT_FOUR); |
| 1747 | } |
| 1748 | ADDOP(c, POP_EXCEPT); |
| 1749 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1750 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1751 | case WITH: |
| 1752 | case ASYNC_WITH: |
| 1753 | ADDOP(c, POP_BLOCK); |
| 1754 | if (preserve_tos) { |
| 1755 | ADDOP(c, ROT_TWO); |
| 1756 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1757 | if(!compiler_call_exit_with_nones(c)) { |
| 1758 | return 0; |
| 1759 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1760 | if (info->fb_type == ASYNC_WITH) { |
| 1761 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1762 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1763 | ADDOP(c, YIELD_FROM); |
| 1764 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1765 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1766 | return 1; |
| 1767 | |
| 1768 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1769 | if (info->fb_datum) { |
| 1770 | ADDOP(c, POP_BLOCK); |
| 1771 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1772 | if (preserve_tos) { |
| 1773 | ADDOP(c, ROT_FOUR); |
| 1774 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1775 | ADDOP(c, POP_EXCEPT); |
| 1776 | if (info->fb_datum) { |
| 1777 | ADDOP_LOAD_CONST(c, Py_None); |
| 1778 | compiler_nameop(c, info->fb_datum, Store); |
| 1779 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1780 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1781 | return 1; |
| 1782 | |
| 1783 | case POP_VALUE: |
| 1784 | if (preserve_tos) { |
| 1785 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1786 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1787 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1788 | return 1; |
| 1789 | } |
| 1790 | Py_UNREACHABLE(); |
| 1791 | } |
| 1792 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1793 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1794 | static int |
| 1795 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1796 | if (c->u->u_nfblocks == 0) { |
| 1797 | return 1; |
| 1798 | } |
| 1799 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1800 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1801 | *loop = top; |
| 1802 | return 1; |
| 1803 | } |
| 1804 | struct fblockinfo copy = *top; |
| 1805 | c->u->u_nfblocks--; |
| 1806 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1807 | return 0; |
| 1808 | } |
| 1809 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1810 | return 0; |
| 1811 | } |
| 1812 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1813 | c->u->u_nfblocks++; |
| 1814 | return 1; |
| 1815 | } |
| 1816 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1817 | /* Compile a sequence of statements, checking for a docstring |
| 1818 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | |
| 1820 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1821 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1822 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1823 | int i = 0; |
| 1824 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1825 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1826 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1827 | /* Set current line number to the line number of first statement. |
| 1828 | This way line number for SETUP_ANNOTATIONS will always |
| 1829 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1830 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1831 | 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] | 1832 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1833 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1834 | } |
| 1835 | /* Every annotated class and module should have __annotations__. */ |
| 1836 | if (find_ann(stmts)) { |
| 1837 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1838 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1839 | if (!asdl_seq_LEN(stmts)) |
| 1840 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1841 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1842 | if (c->c_optimize < 2) { |
| 1843 | docstring = _PyAST_GetDocString(stmts); |
| 1844 | if (docstring) { |
| 1845 | i = 1; |
| 1846 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1847 | assert(st->kind == Expr_kind); |
| 1848 | VISIT(c, expr, st->v.Expr.value); |
| 1849 | if (!compiler_nameop(c, __doc__, Store)) |
| 1850 | return 0; |
| 1851 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1853 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1854 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | static PyCodeObject * |
| 1859 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | PyCodeObject *co; |
| 1862 | int addNone = 1; |
| 1863 | static PyObject *module; |
| 1864 | if (!module) { |
| 1865 | module = PyUnicode_InternFromString("<module>"); |
| 1866 | if (!module) |
| 1867 | return NULL; |
| 1868 | } |
| 1869 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1870 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | return NULL; |
| 1872 | switch (mod->kind) { |
| 1873 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1874 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | compiler_exit_scope(c); |
| 1876 | return 0; |
| 1877 | } |
| 1878 | break; |
| 1879 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1880 | if (find_ann(mod->v.Interactive.body)) { |
| 1881 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1882 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1884 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | break; |
| 1886 | case Expression_kind: |
| 1887 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1888 | addNone = 0; |
| 1889 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | default: |
| 1891 | PyErr_Format(PyExc_SystemError, |
| 1892 | "module kind %d should not be possible", |
| 1893 | mod->kind); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | co = assemble(c, addNone); |
| 1897 | compiler_exit_scope(c); |
| 1898 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1901 | /* The test for LOCAL must come before the test for FREE in order to |
| 1902 | handle classes where name is both local and free. The local var is |
| 1903 | 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] | 1904 | */ |
| 1905 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | static int |
| 1907 | get_ref_type(struct compiler *c, PyObject *name) |
| 1908 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1909 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1910 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1911 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1912 | return CELL; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1913 | scope = _PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1915 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1916 | "_PyST_GetScope(name=%R) failed: " |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1917 | "unknown scope in unit %S (%R); " |
| 1918 | "symbols: %R; locals: %R; globals: %R", |
| 1919 | name, |
| 1920 | c->u->u_name, c->u->u_ste->ste_id, |
| 1921 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1922 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | static int |
| 1928 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1929 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1930 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1931 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1932 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1933 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1934 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1938 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1939 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1940 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1941 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1942 | if (qualname == NULL) |
| 1943 | qualname = co->co_name; |
| 1944 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1945 | if (free) { |
| 1946 | for (i = 0; i < free; ++i) { |
| 1947 | /* Bypass com_addop_varname because it will generate |
| 1948 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1949 | */ |
| 1950 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1951 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1952 | /* Special case: If a class contains a method with a |
| 1953 | free variable that has the same name as a method, |
| 1954 | the name will be considered free *and* local in the |
| 1955 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1956 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1957 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1958 | int reftype = get_ref_type(c, name); |
| 1959 | if (reftype == -1) { |
| 1960 | return 0; |
| 1961 | } |
| 1962 | int arg; |
| 1963 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1964 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1965 | } |
| 1966 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1967 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1968 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1969 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1970 | PyErr_Format(PyExc_SystemError, |
| 1971 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 1972 | "freevars of code %S: %R", |
| 1973 | name, |
| 1974 | reftype, |
| 1975 | c->u->u_name, |
| 1976 | co->co_name, |
| 1977 | co->co_freevars); |
| 1978 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1979 | } |
| 1980 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1982 | flags |= 0x08; |
| 1983 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1985 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1986 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1987 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1992 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1993 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | if (!decos) |
| 1997 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2000 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2001 | } |
| 2002 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2006 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2007 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2008 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2009 | /* Push a dict of keyword-only default values. |
| 2010 | |
| 2011 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2012 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2013 | int i; |
| 2014 | PyObject *keys = NULL; |
| 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2017 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2018 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2019 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2020 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2021 | if (!mangled) { |
| 2022 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2024 | if (keys == NULL) { |
| 2025 | keys = PyList_New(1); |
| 2026 | if (keys == NULL) { |
| 2027 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2028 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2029 | } |
| 2030 | PyList_SET_ITEM(keys, 0, mangled); |
| 2031 | } |
| 2032 | else { |
| 2033 | int res = PyList_Append(keys, mangled); |
| 2034 | Py_DECREF(mangled); |
| 2035 | if (res == -1) { |
| 2036 | goto error; |
| 2037 | } |
| 2038 | } |
| 2039 | if (!compiler_visit_expr(c, default_)) { |
| 2040 | goto error; |
| 2041 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | } |
| 2043 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2044 | if (keys != NULL) { |
| 2045 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2046 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2047 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2048 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2049 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2050 | assert(default_count > 0); |
| 2051 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2052 | } |
| 2053 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2054 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | error: |
| 2058 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2059 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2063 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2064 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2065 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2066 | return 1; |
| 2067 | } |
| 2068 | |
| 2069 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2070 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2071 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2074 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2075 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2076 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2077 | |
| 2078 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2079 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2080 | VISIT(c, annexpr, annotation); |
| 2081 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2083 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2087 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2088 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2089 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2090 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2092 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2093 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | c, |
| 2095 | arg->arg, |
| 2096 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2097 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2098 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2100 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | static int |
| 2104 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2105 | expr_ty returns) |
| 2106 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2107 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2108 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2109 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2110 | 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] | 2111 | */ |
| 2112 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2113 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2114 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2115 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2116 | return 0; |
| 2117 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2118 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2119 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2120 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2121 | args->vararg->annotation, &annotations_len)) |
| 2122 | return 0; |
| 2123 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2124 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2125 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2126 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2127 | args->kwarg->annotation, &annotations_len)) |
| 2128 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | if (!return_str) { |
| 2131 | return_str = PyUnicode_InternFromString("return"); |
| 2132 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2133 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2135 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2136 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2139 | if (annotations_len) { |
| 2140 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2141 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2143 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2144 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2148 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2149 | { |
| 2150 | VISIT_SEQ(c, expr, args->defaults); |
| 2151 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2152 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2155 | static Py_ssize_t |
| 2156 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2157 | { |
| 2158 | Py_ssize_t funcflags = 0; |
| 2159 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2160 | if (!compiler_visit_defaults(c, args)) |
| 2161 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2162 | funcflags |= 0x01; |
| 2163 | } |
| 2164 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2165 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2166 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2167 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2168 | return -1; |
| 2169 | } |
| 2170 | else if (res > 0) { |
| 2171 | funcflags |= 0x02; |
| 2172 | } |
| 2173 | } |
| 2174 | return funcflags; |
| 2175 | } |
| 2176 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2177 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2178 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2179 | { |
| 2180 | |
| 2181 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2182 | compiler_error(c, "cannot assign to __debug__"); |
| 2183 | return 1; |
| 2184 | } |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | static int |
| 2189 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2190 | { |
| 2191 | if (arg != NULL) { |
| 2192 | if (forbidden_name(c, arg->arg, Store)) |
| 2193 | return 0; |
| 2194 | } |
| 2195 | return 1; |
| 2196 | } |
| 2197 | |
| 2198 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2199 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2200 | { |
| 2201 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2202 | 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] | 2203 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2204 | return 0; |
| 2205 | } |
| 2206 | } |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
| 2210 | static int |
| 2211 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2212 | { |
| 2213 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2214 | return 0; |
| 2215 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2216 | return 0; |
| 2217 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2218 | return 0; |
| 2219 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2220 | return 0; |
| 2221 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2222 | return 0; |
| 2223 | return 1; |
| 2224 | } |
| 2225 | |
| 2226 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2227 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2230 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2231 | arguments_ty args; |
| 2232 | expr_ty returns; |
| 2233 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2234 | asdl_expr_seq* decos; |
| 2235 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2236 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2237 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2238 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2239 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2240 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2241 | if (is_async) { |
| 2242 | assert(s->kind == AsyncFunctionDef_kind); |
| 2243 | |
| 2244 | args = s->v.AsyncFunctionDef.args; |
| 2245 | returns = s->v.AsyncFunctionDef.returns; |
| 2246 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2247 | name = s->v.AsyncFunctionDef.name; |
| 2248 | body = s->v.AsyncFunctionDef.body; |
| 2249 | |
| 2250 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2251 | } else { |
| 2252 | assert(s->kind == FunctionDef_kind); |
| 2253 | |
| 2254 | args = s->v.FunctionDef.args; |
| 2255 | returns = s->v.FunctionDef.returns; |
| 2256 | decos = s->v.FunctionDef.decorator_list; |
| 2257 | name = s->v.FunctionDef.name; |
| 2258 | body = s->v.FunctionDef.body; |
| 2259 | |
| 2260 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2261 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2262 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2263 | if (!compiler_check_debug_args(c, args)) |
| 2264 | return 0; |
| 2265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | if (!compiler_decorators(c, decos)) |
| 2267 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2268 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2269 | firstlineno = s->lineno; |
| 2270 | if (asdl_seq_LEN(decos)) { |
| 2271 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2272 | } |
| 2273 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2274 | funcflags = compiler_default_arguments(c, args); |
| 2275 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2277 | } |
| 2278 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2279 | annotations = compiler_visit_annotations(c, args, returns); |
| 2280 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2281 | return 0; |
| 2282 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2283 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2284 | funcflags |= 0x04; |
| 2285 | } |
| 2286 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2287 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2288 | return 0; |
| 2289 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2290 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2291 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2292 | if (c->c_optimize < 2) { |
| 2293 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2294 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2295 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | compiler_exit_scope(c); |
| 2297 | return 0; |
| 2298 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2301 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2303 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2304 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2305 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2307 | qualname = c->u->u_qualname; |
| 2308 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2309 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2310 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2311 | Py_XDECREF(qualname); |
| 2312 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2314 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2315 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2316 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2317 | Py_DECREF(qualname); |
| 2318 | Py_DECREF(co); |
| 2319 | return 0; |
| 2320 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2321 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | /* decorators */ |
| 2325 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2326 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2327 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2328 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2329 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | static int |
| 2333 | compiler_class(struct compiler *c, stmt_ty s) |
| 2334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | PyCodeObject *co; |
| 2336 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2337 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2338 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | if (!compiler_decorators(c, decos)) |
| 2341 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2342 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2343 | firstlineno = s->lineno; |
| 2344 | if (asdl_seq_LEN(decos)) { |
| 2345 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2346 | } |
| 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | /* ultimately generate code for: |
| 2349 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2350 | where: |
| 2351 | <func> is a function/closure created from the class body; |
| 2352 | it has a single argument (__locals__) where the dict |
| 2353 | (or MutableSequence) representing the locals is passed |
| 2354 | <name> is the class name |
| 2355 | <bases> is the positional arguments and *varargs argument |
| 2356 | <keywords> is the keyword arguments and **kwds argument |
| 2357 | This borrows from compiler_call. |
| 2358 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2361 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2362 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | return 0; |
| 2364 | /* this block represents what we do in the new scope */ |
| 2365 | { |
| 2366 | /* use the class name for name mangling */ |
| 2367 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2368 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | /* load (global) __name__ ... */ |
| 2370 | str = PyUnicode_InternFromString("__name__"); |
| 2371 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2372 | Py_XDECREF(str); |
| 2373 | compiler_exit_scope(c); |
| 2374 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2375 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2376 | Py_DECREF(str); |
| 2377 | /* ... and store it as __module__ */ |
| 2378 | str = PyUnicode_InternFromString("__module__"); |
| 2379 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2380 | Py_XDECREF(str); |
| 2381 | compiler_exit_scope(c); |
| 2382 | return 0; |
| 2383 | } |
| 2384 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2385 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2386 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2387 | str = PyUnicode_InternFromString("__qualname__"); |
| 2388 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2389 | Py_XDECREF(str); |
| 2390 | compiler_exit_scope(c); |
| 2391 | return 0; |
| 2392 | } |
| 2393 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2395 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | compiler_exit_scope(c); |
| 2397 | return 0; |
| 2398 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2399 | /* The following code is artificial */ |
| 2400 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2401 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2402 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2403 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2404 | str = PyUnicode_InternFromString("__class__"); |
| 2405 | if (str == NULL) { |
| 2406 | compiler_exit_scope(c); |
| 2407 | return 0; |
| 2408 | } |
| 2409 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2410 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2411 | if (i < 0) { |
| 2412 | compiler_exit_scope(c); |
| 2413 | return 0; |
| 2414 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2415 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2417 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2418 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2419 | str = PyUnicode_InternFromString("__classcell__"); |
| 2420 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2421 | Py_XDECREF(str); |
| 2422 | compiler_exit_scope(c); |
| 2423 | return 0; |
| 2424 | } |
| 2425 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2426 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2427 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2428 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2429 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2430 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2431 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2432 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2433 | /* create the code object */ |
| 2434 | co = assemble(c, 1); |
| 2435 | } |
| 2436 | /* leave the new scope */ |
| 2437 | compiler_exit_scope(c); |
| 2438 | if (co == NULL) |
| 2439 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 | /* 2. load the 'build_class' function */ |
| 2442 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2443 | |
| 2444 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2445 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2446 | Py_DECREF(co); |
| 2447 | return 0; |
| 2448 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | Py_DECREF(co); |
| 2450 | |
| 2451 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2452 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | |
| 2454 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2455 | 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] | 2456 | return 0; |
| 2457 | |
| 2458 | /* 6. apply decorators */ |
| 2459 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2460 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2461 | } |
| 2462 | |
| 2463 | /* 7. store into <name> */ |
| 2464 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2465 | return 0; |
| 2466 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2469 | /* Return 0 if the expression is a constant value except named singletons. |
| 2470 | Return 1 otherwise. */ |
| 2471 | static int |
| 2472 | check_is_arg(expr_ty e) |
| 2473 | { |
| 2474 | if (e->kind != Constant_kind) { |
| 2475 | return 1; |
| 2476 | } |
| 2477 | PyObject *value = e->v.Constant.value; |
| 2478 | return (value == Py_None |
| 2479 | || value == Py_False |
| 2480 | || value == Py_True |
| 2481 | || value == Py_Ellipsis); |
| 2482 | } |
| 2483 | |
| 2484 | /* Check operands of identity chacks ("is" and "is not"). |
| 2485 | Emit a warning if any operand is a constant except named singletons. |
| 2486 | Return 0 on error. |
| 2487 | */ |
| 2488 | static int |
| 2489 | check_compare(struct compiler *c, expr_ty e) |
| 2490 | { |
| 2491 | Py_ssize_t i, n; |
| 2492 | int left = check_is_arg(e->v.Compare.left); |
| 2493 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2494 | for (i = 0; i < n; i++) { |
| 2495 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2496 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2497 | if (op == Is || op == IsNot) { |
| 2498 | if (!right || !left) { |
| 2499 | const char *msg = (op == Is) |
| 2500 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2501 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2502 | return compiler_warn(c, msg); |
| 2503 | } |
| 2504 | } |
| 2505 | left = right; |
| 2506 | } |
| 2507 | return 1; |
| 2508 | } |
| 2509 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2510 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2511 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2512 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2513 | switch (op) { |
| 2514 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2515 | cmp = Py_EQ; |
| 2516 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2517 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2518 | cmp = Py_NE; |
| 2519 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2520 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2521 | cmp = Py_LT; |
| 2522 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2523 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2524 | cmp = Py_LE; |
| 2525 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2526 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2527 | cmp = Py_GT; |
| 2528 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2529 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2530 | cmp = Py_GE; |
| 2531 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2532 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2533 | ADDOP_I(c, IS_OP, 0); |
| 2534 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2535 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2536 | ADDOP_I(c, IS_OP, 1); |
| 2537 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2538 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2539 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2540 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2541 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2542 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2543 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2544 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2545 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2546 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2547 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2548 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2549 | } |
| 2550 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2551 | |
| 2552 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2553 | static int |
| 2554 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2555 | { |
| 2556 | switch (e->kind) { |
| 2557 | case UnaryOp_kind: |
| 2558 | if (e->v.UnaryOp.op == Not) |
| 2559 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2560 | /* fallback to general implementation */ |
| 2561 | break; |
| 2562 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2563 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2564 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2565 | assert(n >= 0); |
| 2566 | int cond2 = e->v.BoolOp.op == Or; |
| 2567 | basicblock *next2 = next; |
| 2568 | if (!cond2 != !cond) { |
| 2569 | next2 = compiler_new_block(c); |
| 2570 | if (next2 == NULL) |
| 2571 | return 0; |
| 2572 | } |
| 2573 | for (i = 0; i < n; ++i) { |
| 2574 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2575 | return 0; |
| 2576 | } |
| 2577 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2578 | return 0; |
| 2579 | if (next2 != next) |
| 2580 | compiler_use_next_block(c, next2); |
| 2581 | return 1; |
| 2582 | } |
| 2583 | case IfExp_kind: { |
| 2584 | basicblock *end, *next2; |
| 2585 | end = compiler_new_block(c); |
| 2586 | if (end == NULL) |
| 2587 | return 0; |
| 2588 | next2 = compiler_new_block(c); |
| 2589 | if (next2 == NULL) |
| 2590 | return 0; |
| 2591 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2592 | return 0; |
| 2593 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2594 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2595 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2596 | compiler_use_next_block(c, next2); |
| 2597 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2598 | return 0; |
| 2599 | compiler_use_next_block(c, end); |
| 2600 | return 1; |
| 2601 | } |
| 2602 | case Compare_kind: { |
| 2603 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2604 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2605 | if (!check_compare(c, e)) { |
| 2606 | return 0; |
| 2607 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2608 | basicblock *cleanup = compiler_new_block(c); |
| 2609 | if (cleanup == NULL) |
| 2610 | return 0; |
| 2611 | VISIT(c, expr, e->v.Compare.left); |
| 2612 | for (i = 0; i < n; i++) { |
| 2613 | VISIT(c, expr, |
| 2614 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2615 | ADDOP(c, DUP_TOP); |
| 2616 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2617 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2618 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2619 | NEXT_BLOCK(c); |
| 2620 | } |
| 2621 | 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] | 2622 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2623 | 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] | 2624 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2625 | basicblock *end = compiler_new_block(c); |
| 2626 | if (end == NULL) |
| 2627 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2628 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2629 | compiler_use_next_block(c, cleanup); |
| 2630 | ADDOP(c, POP_TOP); |
| 2631 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2632 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2633 | } |
| 2634 | compiler_use_next_block(c, end); |
| 2635 | return 1; |
| 2636 | } |
| 2637 | /* fallback to general implementation */ |
| 2638 | break; |
| 2639 | } |
| 2640 | default: |
| 2641 | /* fallback to general implementation */ |
| 2642 | break; |
| 2643 | } |
| 2644 | |
| 2645 | /* general implementation */ |
| 2646 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2647 | 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] | 2648 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2649 | return 1; |
| 2650 | } |
| 2651 | |
| 2652 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2653 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | basicblock *end, *next; |
| 2656 | |
| 2657 | assert(e->kind == IfExp_kind); |
| 2658 | end = compiler_new_block(c); |
| 2659 | if (end == NULL) |
| 2660 | return 0; |
| 2661 | next = compiler_new_block(c); |
| 2662 | if (next == NULL) |
| 2663 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2664 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2665 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2667 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | compiler_use_next_block(c, next); |
| 2669 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2670 | compiler_use_next_block(c, end); |
| 2671 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2675 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2678 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2680 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | arguments_ty args = e->v.Lambda.args; |
| 2682 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2683 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2684 | if (!compiler_check_debug_args(c, args)) |
| 2685 | return 0; |
| 2686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | if (!name) { |
| 2688 | name = PyUnicode_InternFromString("<lambda>"); |
| 2689 | if (!name) |
| 2690 | return 0; |
| 2691 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2693 | funcflags = compiler_default_arguments(c, args); |
| 2694 | if (funcflags == -1) { |
| 2695 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2697 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2698 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2699 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | /* Make None the first constant, so the lambda can't have a |
| 2703 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2704 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2708 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2710 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2711 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2712 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2713 | } |
| 2714 | else { |
| 2715 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2716 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2718 | qualname = c->u->u_qualname; |
| 2719 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2721 | if (co == NULL) { |
| 2722 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2724 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2725 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2726 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2727 | Py_DECREF(qualname); |
| 2728 | Py_DECREF(co); |
| 2729 | return 0; |
| 2730 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2731 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 | Py_DECREF(co); |
| 2733 | |
| 2734 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 | compiler_if(struct compiler *c, stmt_ty s) |
| 2739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2740 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | assert(s->kind == If_kind); |
| 2742 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2743 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2745 | } |
| 2746 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2747 | next = compiler_new_block(c); |
| 2748 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2749 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2750 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2751 | } |
| 2752 | else { |
| 2753 | next = end; |
| 2754 | } |
| 2755 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2756 | return 0; |
| 2757 | } |
| 2758 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2759 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2760 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2761 | compiler_use_next_block(c, next); |
| 2762 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | } |
| 2764 | compiler_use_next_block(c, end); |
| 2765 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | static int |
| 2769 | compiler_for(struct compiler *c, stmt_ty s) |
| 2770 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2771 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2774 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | cleanup = compiler_new_block(c); |
| 2776 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2777 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2779 | } |
| 2780 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2782 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | VISIT(c, expr, s->v.For.iter); |
| 2784 | ADDOP(c, GET_ITER); |
| 2785 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2786 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2787 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | VISIT(c, expr, s->v.For.target); |
| 2789 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2790 | /* Mark jump as artificial */ |
| 2791 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2792 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2794 | |
| 2795 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2797 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2798 | compiler_use_next_block(c, end); |
| 2799 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2802 | |
| 2803 | static int |
| 2804 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2805 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2806 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2807 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2808 | c->u->u_ste->ste_coroutine = 1; |
| 2809 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2810 | return compiler_error(c, "'async for' outside async function"); |
| 2811 | } |
| 2812 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2813 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2814 | except = compiler_new_block(c); |
| 2815 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2816 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2817 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2818 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2819 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2820 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2821 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2822 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2823 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2824 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2825 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2826 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2827 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2828 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2829 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2830 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2831 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2832 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2833 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2834 | /* Success block for __anext__ */ |
| 2835 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2836 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2837 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2838 | |
| 2839 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2840 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2841 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2842 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2843 | |
| 2844 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2845 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2846 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2847 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2848 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2849 | |
| 2850 | compiler_use_next_block(c, end); |
| 2851 | |
| 2852 | return 1; |
| 2853 | } |
| 2854 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2855 | static int |
| 2856 | compiler_while(struct compiler *c, stmt_ty s) |
| 2857 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2858 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2860 | body = compiler_new_block(c); |
| 2861 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2863 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2867 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2870 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2871 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2876 | SET_LOC(c, s); |
| 2877 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2878 | return 0; |
| 2879 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2881 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2882 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2883 | compiler_use_next_block(c, anchor); |
| 2884 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2886 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2893 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2894 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2895 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2896 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2897 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2898 | return compiler_error(c, "'return' outside function"); |
| 2899 | if (s->v.Return.value != NULL && |
| 2900 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2901 | { |
| 2902 | return compiler_error( |
| 2903 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2905 | if (preserve_tos) { |
| 2906 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2907 | } else { |
| 2908 | /* Emit instruction with line number for expression */ |
| 2909 | if (s->v.Return.value != NULL) { |
| 2910 | SET_LOC(c, s->v.Return.value); |
| 2911 | ADDOP(c, NOP); |
| 2912 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2913 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2914 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2915 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2916 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2917 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2918 | } |
| 2919 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2920 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2921 | } |
| 2922 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2923 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2928 | static int |
| 2929 | compiler_break(struct compiler *c) |
| 2930 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2931 | struct fblockinfo *loop = NULL; |
| 2932 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2933 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2934 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2935 | if (loop == NULL) { |
| 2936 | return compiler_error(c, "'break' outside loop"); |
| 2937 | } |
| 2938 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2939 | return 0; |
| 2940 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2941 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2942 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2943 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | static int |
| 2947 | compiler_continue(struct compiler *c) |
| 2948 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2949 | struct fblockinfo *loop = NULL; |
| 2950 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2951 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2952 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2953 | if (loop == NULL) { |
| 2954 | return compiler_error(c, "'continue' not properly in loop"); |
| 2955 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2956 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2957 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2958 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2962 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | |
| 2964 | SETUP_FINALLY L |
| 2965 | <code for body> |
| 2966 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2967 | <code for finalbody> |
| 2968 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2969 | L: |
| 2970 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2971 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2973 | The special instructions use the block stack. Each block |
| 2974 | stack entry contains the instruction that created it (here |
| 2975 | SETUP_FINALLY), the level of the value stack at the time the |
| 2976 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2978 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | Pushes the current value stack level and the label |
| 2980 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2981 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2982 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2984 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2985 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2986 | exceptions are pushed onto the value stack (and the exception |
| 2987 | condition is cleared), and the interpreter jumps to the label |
| 2988 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2989 | */ |
| 2990 | |
| 2991 | static int |
| 2992 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2993 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2994 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | body = compiler_new_block(c); |
| 2997 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2998 | exit = compiler_new_block(c); |
| 2999 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3000 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3001 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3002 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3003 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3005 | 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] | 3006 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3007 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3008 | if (!compiler_try_except(c, s)) |
| 3009 | return 0; |
| 3010 | } |
| 3011 | else { |
| 3012 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3013 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3014 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3015 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3016 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3017 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3018 | /* `finally` block */ |
| 3019 | compiler_use_next_block(c, end); |
| 3020 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3021 | return 0; |
| 3022 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3023 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3024 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3025 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3026 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3030 | 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] | 3031 | (The contents of the value stack is shown in [], with the top |
| 3032 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3033 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | |
| 3035 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3036 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | [] <code for S> |
| 3038 | [] POP_BLOCK |
| 3039 | [] JUMP_FORWARD L0 |
| 3040 | |
| 3041 | [tb, val, exc] L1: DUP ) |
| 3042 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3043 | [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] | 3044 | [tb, val, exc] POP |
| 3045 | [tb, val] <assign to V1> (or POP if no V1) |
| 3046 | [tb] POP |
| 3047 | [] <code for S1> |
| 3048 | JUMP_FORWARD L0 |
| 3049 | |
| 3050 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3051 | .............................etc....................... |
| 3052 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3053 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | |
| 3055 | [] L0: <next statement> |
| 3056 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | Of course, parts are not generated if Vi or Ei is not present. |
| 3058 | */ |
| 3059 | static int |
| 3060 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3061 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3063 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | body = compiler_new_block(c); |
| 3066 | except = compiler_new_block(c); |
| 3067 | orelse = compiler_new_block(c); |
| 3068 | end = compiler_new_block(c); |
| 3069 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3070 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3071 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3073 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3075 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3076 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3077 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3078 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3079 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3081 | /* Runtime will push a block here, so we need to account for that */ |
| 3082 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3083 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | for (i = 0; i < n; i++) { |
| 3085 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3086 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3088 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3089 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | except = compiler_new_block(c); |
| 3091 | if (except == NULL) |
| 3092 | return 0; |
| 3093 | if (handler->v.ExceptHandler.type) { |
| 3094 | ADDOP(c, DUP_TOP); |
| 3095 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3096 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3097 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | } |
| 3099 | ADDOP(c, POP_TOP); |
| 3100 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3101 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3102 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3103 | cleanup_end = compiler_new_block(c); |
| 3104 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3105 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3106 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3107 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3108 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3109 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3110 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3112 | /* |
| 3113 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3114 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3115 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3116 | try: |
| 3117 | # body |
| 3118 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3119 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3120 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3121 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3123 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3124 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3125 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3126 | 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] | 3127 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3129 | /* second # body */ |
| 3130 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3131 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3132 | ADDOP(c, POP_BLOCK); |
| 3133 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3134 | /* name = None; del name; # Mark as artificial */ |
| 3135 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3136 | ADDOP_LOAD_CONST(c, Py_None); |
| 3137 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3138 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3139 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3141 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3142 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3144 | /* name = None; del name; # Mark as artificial */ |
| 3145 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3146 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3147 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3148 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3150 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | } |
| 3152 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3153 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3155 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3156 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3157 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3159 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3160 | ADDOP(c, POP_TOP); |
| 3161 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3162 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3163 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3165 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3166 | /* name = None; del name; # Mark as artificial */ |
| 3167 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3168 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3169 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | compiler_use_next_block(c, except); |
| 3172 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3173 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3174 | /* Mark as artificial */ |
| 3175 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3176 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3178 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3179 | compiler_use_next_block(c, end); |
| 3180 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3184 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3185 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3186 | return compiler_try_finally(c, s); |
| 3187 | else |
| 3188 | return compiler_try_except(c, s); |
| 3189 | } |
| 3190 | |
| 3191 | |
| 3192 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3193 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | /* The IMPORT_NAME opcode was already generated. This function |
| 3196 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | 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] | 3199 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3201 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3202 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3203 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3204 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3205 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3207 | while (1) { |
| 3208 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3210 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3211 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3212 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3213 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3215 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3216 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3217 | if (dot == -1) { |
| 3218 | break; |
| 3219 | } |
| 3220 | ADDOP(c, ROT_TWO); |
| 3221 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3223 | if (!compiler_nameop(c, asname, Store)) { |
| 3224 | return 0; |
| 3225 | } |
| 3226 | ADDOP(c, POP_TOP); |
| 3227 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | } |
| 3229 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | static int |
| 3233 | compiler_import(struct compiler *c, stmt_ty s) |
| 3234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | /* The Import node stores a module name like a.b.c as a single |
| 3236 | string. This is convenient for all cases except |
| 3237 | import a.b.c as d |
| 3238 | where we need to parse that string to extract the individual |
| 3239 | module names. |
| 3240 | XXX Perhaps change the representation to make this case simpler? |
| 3241 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3242 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3243 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3244 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | for (i = 0; i < n; i++) { |
| 3246 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3247 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3248 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3249 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3250 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
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 (alias->asname) { |
| 3254 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3255 | if (!r) |
| 3256 | return r; |
| 3257 | } |
| 3258 | else { |
| 3259 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3260 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3261 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3262 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3263 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3264 | if (tmp == NULL) |
| 3265 | return 0; |
| 3266 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3268 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | Py_DECREF(tmp); |
| 3270 | } |
| 3271 | if (!r) |
| 3272 | return r; |
| 3273 | } |
| 3274 | } |
| 3275 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | static int |
| 3279 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3280 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3281 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3282 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | if (!empty_string) { |
| 3286 | empty_string = PyUnicode_FromString(""); |
| 3287 | if (!empty_string) |
| 3288 | return 0; |
| 3289 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3290 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3291 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3292 | |
| 3293 | names = PyTuple_New(n); |
| 3294 | if (!names) |
| 3295 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | /* build up the names */ |
| 3298 | for (i = 0; i < n; i++) { |
| 3299 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3300 | Py_INCREF(alias->name); |
| 3301 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3302 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3305 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | Py_DECREF(names); |
| 3307 | return compiler_error(c, "from __future__ imports must occur " |
| 3308 | "at the beginning of the file"); |
| 3309 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3310 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3312 | if (s->v.ImportFrom.module) { |
| 3313 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3314 | } |
| 3315 | else { |
| 3316 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3317 | } |
| 3318 | for (i = 0; i < n; i++) { |
| 3319 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3320 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3321 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3322 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | assert(n == 1); |
| 3324 | ADDOP(c, IMPORT_STAR); |
| 3325 | return 1; |
| 3326 | } |
| 3327 | |
| 3328 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3329 | store_name = alias->name; |
| 3330 | if (alias->asname) |
| 3331 | store_name = alias->asname; |
| 3332 | |
| 3333 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | return 0; |
| 3335 | } |
| 3336 | } |
| 3337 | /* remove imported module */ |
| 3338 | ADDOP(c, POP_TOP); |
| 3339 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | static int |
| 3343 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3346 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3347 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3348 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3349 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3350 | (s->v.Assert.test->kind == Constant_kind && |
| 3351 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3352 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3353 | { |
| 3354 | if (!compiler_warn(c, "assertion is always true, " |
| 3355 | "perhaps remove parentheses?")) |
| 3356 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3357 | return 0; |
| 3358 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3360 | if (c->c_optimize) |
| 3361 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3362 | end = compiler_new_block(c); |
| 3363 | if (end == NULL) |
| 3364 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3365 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3366 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3367 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | if (s->v.Assert.msg) { |
| 3369 | VISIT(c, expr, s->v.Assert.msg); |
| 3370 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3371 | } |
| 3372 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3373 | compiler_use_next_block(c, end); |
| 3374 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3378 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3379 | { |
| 3380 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3381 | VISIT(c, expr, value); |
| 3382 | ADDOP(c, PRINT_EXPR); |
| 3383 | return 1; |
| 3384 | } |
| 3385 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3386 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3387 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3388 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3389 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3393 | /* Mark POP_TOP as artificial */ |
| 3394 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3395 | ADDOP(c, POP_TOP); |
| 3396 | return 1; |
| 3397 | } |
| 3398 | |
| 3399 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3400 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3401 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3402 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3405 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3407 | switch (s->kind) { |
| 3408 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3409 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3410 | case ClassDef_kind: |
| 3411 | return compiler_class(c, s); |
| 3412 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3413 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | case Delete_kind: |
| 3415 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3416 | break; |
| 3417 | case Assign_kind: |
| 3418 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3419 | VISIT(c, expr, s->v.Assign.value); |
| 3420 | for (i = 0; i < n; i++) { |
| 3421 | if (i < n - 1) |
| 3422 | ADDOP(c, DUP_TOP); |
| 3423 | VISIT(c, expr, |
| 3424 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3425 | } |
| 3426 | break; |
| 3427 | case AugAssign_kind: |
| 3428 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3429 | case AnnAssign_kind: |
| 3430 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | case For_kind: |
| 3432 | return compiler_for(c, s); |
| 3433 | case While_kind: |
| 3434 | return compiler_while(c, s); |
| 3435 | case If_kind: |
| 3436 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3437 | case Match_kind: |
| 3438 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | case Raise_kind: |
| 3440 | n = 0; |
| 3441 | if (s->v.Raise.exc) { |
| 3442 | VISIT(c, expr, s->v.Raise.exc); |
| 3443 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3444 | if (s->v.Raise.cause) { |
| 3445 | VISIT(c, expr, s->v.Raise.cause); |
| 3446 | n++; |
| 3447 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3449 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3450 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3452 | case Try_kind: |
| 3453 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | case Assert_kind: |
| 3455 | return compiler_assert(c, s); |
| 3456 | case Import_kind: |
| 3457 | return compiler_import(c, s); |
| 3458 | case ImportFrom_kind: |
| 3459 | return compiler_from_import(c, s); |
| 3460 | case Global_kind: |
| 3461 | case Nonlocal_kind: |
| 3462 | break; |
| 3463 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3464 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3466 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3467 | break; |
| 3468 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3469 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | case Continue_kind: |
| 3471 | return compiler_continue(c); |
| 3472 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3473 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3474 | case AsyncFunctionDef_kind: |
| 3475 | return compiler_function(c, s, 1); |
| 3476 | case AsyncWith_kind: |
| 3477 | return compiler_async_with(c, s, 0); |
| 3478 | case AsyncFor_kind: |
| 3479 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | static int |
| 3486 | unaryop(unaryop_ty op) |
| 3487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3488 | switch (op) { |
| 3489 | case Invert: |
| 3490 | return UNARY_INVERT; |
| 3491 | case Not: |
| 3492 | return UNARY_NOT; |
| 3493 | case UAdd: |
| 3494 | return UNARY_POSITIVE; |
| 3495 | case USub: |
| 3496 | return UNARY_NEGATIVE; |
| 3497 | default: |
| 3498 | PyErr_Format(PyExc_SystemError, |
| 3499 | "unary op %d should not be possible", op); |
| 3500 | return 0; |
| 3501 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3505 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | switch (op) { |
| 3508 | case Add: |
| 3509 | return BINARY_ADD; |
| 3510 | case Sub: |
| 3511 | return BINARY_SUBTRACT; |
| 3512 | case Mult: |
| 3513 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3514 | case MatMult: |
| 3515 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | case Div: |
| 3517 | return BINARY_TRUE_DIVIDE; |
| 3518 | case Mod: |
| 3519 | return BINARY_MODULO; |
| 3520 | case Pow: |
| 3521 | return BINARY_POWER; |
| 3522 | case LShift: |
| 3523 | return BINARY_LSHIFT; |
| 3524 | case RShift: |
| 3525 | return BINARY_RSHIFT; |
| 3526 | case BitOr: |
| 3527 | return BINARY_OR; |
| 3528 | case BitXor: |
| 3529 | return BINARY_XOR; |
| 3530 | case BitAnd: |
| 3531 | return BINARY_AND; |
| 3532 | case FloorDiv: |
| 3533 | return BINARY_FLOOR_DIVIDE; |
| 3534 | default: |
| 3535 | PyErr_Format(PyExc_SystemError, |
| 3536 | "binary op %d should not be possible", op); |
| 3537 | return 0; |
| 3538 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3542 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | switch (op) { |
| 3545 | case Add: |
| 3546 | return INPLACE_ADD; |
| 3547 | case Sub: |
| 3548 | return INPLACE_SUBTRACT; |
| 3549 | case Mult: |
| 3550 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3551 | case MatMult: |
| 3552 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | case Div: |
| 3554 | return INPLACE_TRUE_DIVIDE; |
| 3555 | case Mod: |
| 3556 | return INPLACE_MODULO; |
| 3557 | case Pow: |
| 3558 | return INPLACE_POWER; |
| 3559 | case LShift: |
| 3560 | return INPLACE_LSHIFT; |
| 3561 | case RShift: |
| 3562 | return INPLACE_RSHIFT; |
| 3563 | case BitOr: |
| 3564 | return INPLACE_OR; |
| 3565 | case BitXor: |
| 3566 | return INPLACE_XOR; |
| 3567 | case BitAnd: |
| 3568 | return INPLACE_AND; |
| 3569 | case FloorDiv: |
| 3570 | return INPLACE_FLOOR_DIVIDE; |
| 3571 | default: |
| 3572 | PyErr_Format(PyExc_SystemError, |
| 3573 | "inplace binary op %d should not be possible", op); |
| 3574 | return 0; |
| 3575 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | static int |
| 3579 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3580 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3581 | int op, scope; |
| 3582 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | PyObject *dict = c->u->u_names; |
| 3586 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3587 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3588 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3589 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3590 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3591 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3592 | if (forbidden_name(c, name, ctx)) |
| 3593 | return 0; |
| 3594 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3595 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3596 | if (!mangled) |
| 3597 | return 0; |
| 3598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | op = 0; |
| 3600 | optype = OP_NAME; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 3601 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | switch (scope) { |
| 3603 | case FREE: |
| 3604 | dict = c->u->u_freevars; |
| 3605 | optype = OP_DEREF; |
| 3606 | break; |
| 3607 | case CELL: |
| 3608 | dict = c->u->u_cellvars; |
| 3609 | optype = OP_DEREF; |
| 3610 | break; |
| 3611 | case LOCAL: |
| 3612 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3613 | optype = OP_FAST; |
| 3614 | break; |
| 3615 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3616 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | optype = OP_GLOBAL; |
| 3618 | break; |
| 3619 | case GLOBAL_EXPLICIT: |
| 3620 | optype = OP_GLOBAL; |
| 3621 | break; |
| 3622 | default: |
| 3623 | /* scope can be 0 */ |
| 3624 | break; |
| 3625 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3627 | /* 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] | 3628 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | switch (optype) { |
| 3631 | case OP_DEREF: |
| 3632 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3633 | case Load: |
| 3634 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3635 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3636 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3637 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | } |
| 3639 | break; |
| 3640 | case OP_FAST: |
| 3641 | switch (ctx) { |
| 3642 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3643 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3646 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3647 | return 1; |
| 3648 | case OP_GLOBAL: |
| 3649 | switch (ctx) { |
| 3650 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3651 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3652 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | } |
| 3654 | break; |
| 3655 | case OP_NAME: |
| 3656 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3657 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3658 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3659 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3660 | } |
| 3661 | break; |
| 3662 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3665 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3666 | Py_DECREF(mangled); |
| 3667 | if (arg < 0) |
| 3668 | return 0; |
| 3669 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | static int |
| 3673 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3676 | int jumpi; |
| 3677 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3678 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3680 | assert(e->kind == BoolOp_kind); |
| 3681 | if (e->v.BoolOp.op == And) |
| 3682 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3683 | else |
| 3684 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3685 | end = compiler_new_block(c); |
| 3686 | if (end == NULL) |
| 3687 | return 0; |
| 3688 | s = e->v.BoolOp.values; |
| 3689 | n = asdl_seq_LEN(s) - 1; |
| 3690 | assert(n >= 0); |
| 3691 | for (i = 0; i < n; ++i) { |
| 3692 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3693 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3694 | basicblock *next = compiler_new_block(c); |
| 3695 | if (next == NULL) { |
| 3696 | return 0; |
| 3697 | } |
| 3698 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3699 | } |
| 3700 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3701 | compiler_use_next_block(c, end); |
| 3702 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3706 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3707 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3708 | { |
| 3709 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3710 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3711 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3712 | PyObject *folded = PyTuple_New(n); |
| 3713 | if (folded == NULL) { |
| 3714 | return 0; |
| 3715 | } |
| 3716 | PyObject *val; |
| 3717 | for (i = 0; i < n; i++) { |
| 3718 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3719 | Py_INCREF(val); |
| 3720 | PyTuple_SET_ITEM(folded, i, val); |
| 3721 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3722 | if (tuple) { |
| 3723 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3724 | } else { |
| 3725 | if (add == SET_ADD) { |
| 3726 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3727 | if (folded == NULL) { |
| 3728 | return 0; |
| 3729 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3730 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3731 | ADDOP_I(c, build, pushed); |
| 3732 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3733 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3734 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3735 | return 1; |
| 3736 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3737 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3738 | for (i = 0; i < n; i++) { |
| 3739 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3740 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3741 | seen_star = 1; |
| 3742 | } |
| 3743 | } |
| 3744 | if (seen_star) { |
| 3745 | seen_star = 0; |
| 3746 | for (i = 0; i < n; i++) { |
| 3747 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3748 | if (elt->kind == Starred_kind) { |
| 3749 | if (seen_star == 0) { |
| 3750 | ADDOP_I(c, build, i+pushed); |
| 3751 | seen_star = 1; |
| 3752 | } |
| 3753 | VISIT(c, expr, elt->v.Starred.value); |
| 3754 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3755 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3756 | else { |
| 3757 | VISIT(c, expr, elt); |
| 3758 | if (seen_star) { |
| 3759 | ADDOP_I(c, add, 1); |
| 3760 | } |
| 3761 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3762 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3763 | assert(seen_star); |
| 3764 | if (tuple) { |
| 3765 | ADDOP(c, LIST_TO_TUPLE); |
| 3766 | } |
| 3767 | } |
| 3768 | else { |
| 3769 | for (i = 0; i < n; i++) { |
| 3770 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3771 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3772 | } |
| 3773 | if (tuple) { |
| 3774 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3775 | } else { |
| 3776 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3777 | } |
| 3778 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3779 | return 1; |
| 3780 | } |
| 3781 | |
| 3782 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3783 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3784 | { |
| 3785 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3786 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3787 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3788 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3789 | if (elt->kind == Starred_kind && !seen_star) { |
| 3790 | if ((i >= (1 << 8)) || |
| 3791 | (n-i-1 >= (INT_MAX >> 8))) |
| 3792 | return compiler_error(c, |
| 3793 | "too many expressions in " |
| 3794 | "star-unpacking assignment"); |
| 3795 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3796 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3797 | } |
| 3798 | else if (elt->kind == Starred_kind) { |
| 3799 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3800 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3801 | } |
| 3802 | } |
| 3803 | if (!seen_star) { |
| 3804 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3805 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3806 | return 1; |
| 3807 | } |
| 3808 | |
| 3809 | static int |
| 3810 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3811 | { |
| 3812 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3813 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3814 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3815 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3816 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3817 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3818 | return 1; |
| 3819 | } |
| 3820 | |
| 3821 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3822 | compiler_list(struct compiler *c, expr_ty e) |
| 3823 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3824 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3825 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3826 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3827 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3828 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3829 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3830 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3831 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3832 | else |
| 3833 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3834 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | static int |
| 3838 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3839 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3840 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3841 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3842 | return assignment_helper(c, elts); |
| 3843 | } |
| 3844 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3845 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3846 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3847 | } |
| 3848 | else |
| 3849 | VISIT_SEQ(c, expr, elts); |
| 3850 | return 1; |
| 3851 | } |
| 3852 | |
| 3853 | static int |
| 3854 | compiler_set(struct compiler *c, expr_ty e) |
| 3855 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3856 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3857 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3861 | 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] | 3862 | { |
| 3863 | Py_ssize_t i; |
| 3864 | for (i = begin; i < end; i++) { |
| 3865 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3866 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
| 3869 | return 1; |
| 3870 | } |
| 3871 | |
| 3872 | static int |
| 3873 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3874 | { |
| 3875 | Py_ssize_t i, n = end - begin; |
| 3876 | PyObject *keys, *key; |
| 3877 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3878 | for (i = begin; i < end; i++) { |
| 3879 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3880 | } |
| 3881 | keys = PyTuple_New(n); |
| 3882 | if (keys == NULL) { |
| 3883 | return 0; |
| 3884 | } |
| 3885 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3886 | 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] | 3887 | Py_INCREF(key); |
| 3888 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3889 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3890 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3891 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3892 | } |
| 3893 | else { |
| 3894 | for (i = begin; i < end; i++) { |
| 3895 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3896 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3897 | } |
| 3898 | ADDOP_I(c, BUILD_MAP, n); |
| 3899 | } |
| 3900 | return 1; |
| 3901 | } |
| 3902 | |
| 3903 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3904 | compiler_dict(struct compiler *c, expr_ty e) |
| 3905 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3906 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3907 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3908 | int is_unpacking = 0; |
| 3909 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3910 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3911 | elements = 0; |
| 3912 | for (i = 0; i < n; i++) { |
| 3913 | 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] | 3914 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3915 | if (elements) { |
| 3916 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3917 | return 0; |
| 3918 | } |
| 3919 | if (have_dict) { |
| 3920 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3921 | } |
| 3922 | have_dict = 1; |
| 3923 | elements = 0; |
| 3924 | } |
| 3925 | if (have_dict == 0) { |
| 3926 | ADDOP_I(c, BUILD_MAP, 0); |
| 3927 | have_dict = 1; |
| 3928 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3929 | 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] | 3930 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3931 | } |
| 3932 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3933 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3934 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3935 | return 0; |
| 3936 | } |
| 3937 | if (have_dict) { |
| 3938 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3939 | } |
| 3940 | have_dict = 1; |
| 3941 | elements = 0; |
| 3942 | } |
| 3943 | else { |
| 3944 | elements++; |
| 3945 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3946 | } |
| 3947 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3948 | if (elements) { |
| 3949 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3950 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3951 | } |
| 3952 | if (have_dict) { |
| 3953 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3954 | } |
| 3955 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3956 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3957 | if (!have_dict) { |
| 3958 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3959 | } |
| 3960 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | static int |
| 3964 | compiler_compare(struct compiler *c, expr_ty e) |
| 3965 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3966 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3967 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3968 | if (!check_compare(c, e)) { |
| 3969 | return 0; |
| 3970 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3971 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3972 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3973 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3974 | if (n == 0) { |
| 3975 | 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] | 3976 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3977 | } |
| 3978 | else { |
| 3979 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 | if (cleanup == NULL) |
| 3981 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3982 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3983 | VISIT(c, expr, |
| 3984 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3985 | ADDOP(c, DUP_TOP); |
| 3986 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3987 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3988 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3989 | NEXT_BLOCK(c); |
| 3990 | } |
| 3991 | 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] | 3992 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3993 | basicblock *end = compiler_new_block(c); |
| 3994 | if (end == NULL) |
| 3995 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3996 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3997 | compiler_use_next_block(c, cleanup); |
| 3998 | ADDOP(c, ROT_TWO); |
| 3999 | ADDOP(c, POP_TOP); |
| 4000 | compiler_use_next_block(c, end); |
| 4001 | } |
| 4002 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4005 | static PyTypeObject * |
| 4006 | infer_type(expr_ty e) |
| 4007 | { |
| 4008 | switch (e->kind) { |
| 4009 | case Tuple_kind: |
| 4010 | return &PyTuple_Type; |
| 4011 | case List_kind: |
| 4012 | case ListComp_kind: |
| 4013 | return &PyList_Type; |
| 4014 | case Dict_kind: |
| 4015 | case DictComp_kind: |
| 4016 | return &PyDict_Type; |
| 4017 | case Set_kind: |
| 4018 | case SetComp_kind: |
| 4019 | return &PySet_Type; |
| 4020 | case GeneratorExp_kind: |
| 4021 | return &PyGen_Type; |
| 4022 | case Lambda_kind: |
| 4023 | return &PyFunction_Type; |
| 4024 | case JoinedStr_kind: |
| 4025 | case FormattedValue_kind: |
| 4026 | return &PyUnicode_Type; |
| 4027 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4028 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4029 | default: |
| 4030 | return NULL; |
| 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | static int |
| 4035 | check_caller(struct compiler *c, expr_ty e) |
| 4036 | { |
| 4037 | switch (e->kind) { |
| 4038 | case Constant_kind: |
| 4039 | case Tuple_kind: |
| 4040 | case List_kind: |
| 4041 | case ListComp_kind: |
| 4042 | case Dict_kind: |
| 4043 | case DictComp_kind: |
| 4044 | case Set_kind: |
| 4045 | case SetComp_kind: |
| 4046 | case GeneratorExp_kind: |
| 4047 | case JoinedStr_kind: |
| 4048 | case FormattedValue_kind: |
| 4049 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4050 | "perhaps you missed a comma?", |
| 4051 | infer_type(e)->tp_name); |
| 4052 | default: |
| 4053 | return 1; |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | static int |
| 4058 | check_subscripter(struct compiler *c, expr_ty e) |
| 4059 | { |
| 4060 | PyObject *v; |
| 4061 | |
| 4062 | switch (e->kind) { |
| 4063 | case Constant_kind: |
| 4064 | v = e->v.Constant.value; |
| 4065 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4066 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4067 | PyAnySet_Check(v))) |
| 4068 | { |
| 4069 | return 1; |
| 4070 | } |
| 4071 | /* fall through */ |
| 4072 | case Set_kind: |
| 4073 | case SetComp_kind: |
| 4074 | case GeneratorExp_kind: |
| 4075 | case Lambda_kind: |
| 4076 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4077 | "perhaps you missed a comma?", |
| 4078 | infer_type(e)->tp_name); |
| 4079 | default: |
| 4080 | return 1; |
| 4081 | } |
| 4082 | } |
| 4083 | |
| 4084 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4085 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4086 | { |
| 4087 | PyObject *v; |
| 4088 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4089 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4090 | if (index_type == NULL |
| 4091 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4092 | || index_type == &PySlice_Type) { |
| 4093 | return 1; |
| 4094 | } |
| 4095 | |
| 4096 | switch (e->kind) { |
| 4097 | case Constant_kind: |
| 4098 | v = e->v.Constant.value; |
| 4099 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4100 | return 1; |
| 4101 | } |
| 4102 | /* fall through */ |
| 4103 | case Tuple_kind: |
| 4104 | case List_kind: |
| 4105 | case ListComp_kind: |
| 4106 | case JoinedStr_kind: |
| 4107 | case FormattedValue_kind: |
| 4108 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4109 | "not %.200s; " |
| 4110 | "perhaps you missed a comma?", |
| 4111 | infer_type(e)->tp_name, |
| 4112 | index_type->tp_name); |
| 4113 | default: |
| 4114 | return 1; |
| 4115 | } |
| 4116 | } |
| 4117 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4118 | // 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] | 4119 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4120 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4121 | { |
| 4122 | Py_ssize_t argsl, i; |
| 4123 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4124 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4125 | |
| 4126 | /* Check that the call node is an attribute access, and that |
| 4127 | the call doesn't have keyword parameters. */ |
| 4128 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4129 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4130 | return -1; |
| 4131 | |
| 4132 | /* Check that there are no *varargs types of arguments. */ |
| 4133 | argsl = asdl_seq_LEN(args); |
| 4134 | for (i = 0; i < argsl; i++) { |
| 4135 | expr_ty elt = asdl_seq_GET(args, i); |
| 4136 | if (elt->kind == Starred_kind) { |
| 4137 | return -1; |
| 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | /* Alright, we can optimize the code. */ |
| 4142 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4143 | int old_lineno = c->u->u_lineno; |
| 4144 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4145 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4146 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4147 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4148 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4149 | return 1; |
| 4150 | } |
| 4151 | |
| 4152 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4153 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4154 | { |
| 4155 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4156 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4157 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4158 | if (key->arg == NULL) { |
| 4159 | continue; |
| 4160 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4161 | if (forbidden_name(c, key->arg, Store)) { |
| 4162 | return -1; |
| 4163 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4164 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4165 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4166 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4167 | c->u->u_col_offset = other->col_offset; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4168 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4169 | return -1; |
| 4170 | } |
| 4171 | } |
| 4172 | } |
| 4173 | return 0; |
| 4174 | } |
| 4175 | |
| 4176 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4177 | compiler_call(struct compiler *c, expr_ty e) |
| 4178 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4179 | int ret = maybe_optimize_method_call(c, e); |
| 4180 | if (ret >= 0) { |
| 4181 | return ret; |
| 4182 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4183 | if (!check_caller(c, e->v.Call.func)) { |
| 4184 | return 0; |
| 4185 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | VISIT(c, expr, e->v.Call.func); |
| 4187 | return compiler_call_helper(c, 0, |
| 4188 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4189 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4192 | static int |
| 4193 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4194 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4195 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4196 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4197 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4198 | return 1; |
| 4199 | } |
| 4200 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4201 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4202 | static int |
| 4203 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4204 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4205 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4206 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4207 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4208 | Convert the conversion char to 3 bits: |
| 4209 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4210 | !s : 001 0x1 FVC_STR |
| 4211 | !r : 010 0x2 FVC_REPR |
| 4212 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4213 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4214 | next bit is whether or not we have a format spec: |
| 4215 | yes : 100 0x4 |
| 4216 | no : 000 0x0 |
| 4217 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4218 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4219 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4220 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4221 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4222 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4223 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4224 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4225 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4226 | case 's': oparg = FVC_STR; break; |
| 4227 | case 'r': oparg = FVC_REPR; break; |
| 4228 | case 'a': oparg = FVC_ASCII; break; |
| 4229 | case -1: oparg = FVC_NONE; break; |
| 4230 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4231 | PyErr_Format(PyExc_SystemError, |
| 4232 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4233 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4234 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4235 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4236 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4237 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4238 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4239 | } |
| 4240 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4241 | /* And push our opcode and oparg */ |
| 4242 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4243 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4244 | return 1; |
| 4245 | } |
| 4246 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4247 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4248 | 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] | 4249 | { |
| 4250 | Py_ssize_t i, n = end - begin; |
| 4251 | keyword_ty kw; |
| 4252 | PyObject *keys, *key; |
| 4253 | assert(n > 0); |
| 4254 | if (n > 1) { |
| 4255 | for (i = begin; i < end; i++) { |
| 4256 | kw = asdl_seq_GET(keywords, i); |
| 4257 | VISIT(c, expr, kw->value); |
| 4258 | } |
| 4259 | keys = PyTuple_New(n); |
| 4260 | if (keys == NULL) { |
| 4261 | return 0; |
| 4262 | } |
| 4263 | for (i = begin; i < end; i++) { |
| 4264 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4265 | Py_INCREF(key); |
| 4266 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4267 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4268 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4269 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4270 | } |
| 4271 | else { |
| 4272 | /* a for loop only executes once */ |
| 4273 | for (i = begin; i < end; i++) { |
| 4274 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4275 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4276 | VISIT(c, expr, kw->value); |
| 4277 | } |
| 4278 | ADDOP_I(c, BUILD_MAP, n); |
| 4279 | } |
| 4280 | return 1; |
| 4281 | } |
| 4282 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4283 | /* shared code between compiler_call and compiler_class */ |
| 4284 | static int |
| 4285 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4286 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4287 | asdl_expr_seq *args, |
| 4288 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4289 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4290 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4291 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4292 | if (validate_keywords(c, keywords) == -1) { |
| 4293 | return 0; |
| 4294 | } |
| 4295 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4296 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4297 | nkwelts = asdl_seq_LEN(keywords); |
| 4298 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4299 | for (i = 0; i < nelts; i++) { |
| 4300 | expr_ty elt = asdl_seq_GET(args, i); |
| 4301 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4302 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4303 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4304 | } |
| 4305 | for (i = 0; i < nkwelts; i++) { |
| 4306 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4307 | if (kw->arg == NULL) { |
| 4308 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4309 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4311 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4312 | /* No * or ** args, so can use faster calling sequence */ |
| 4313 | for (i = 0; i < nelts; i++) { |
| 4314 | expr_ty elt = asdl_seq_GET(args, i); |
| 4315 | assert(elt->kind != Starred_kind); |
| 4316 | VISIT(c, expr, elt); |
| 4317 | } |
| 4318 | if (nkwelts) { |
| 4319 | PyObject *names; |
| 4320 | VISIT_SEQ(c, keyword, keywords); |
| 4321 | names = PyTuple_New(nkwelts); |
| 4322 | if (names == NULL) { |
| 4323 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4324 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4325 | for (i = 0; i < nkwelts; i++) { |
| 4326 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4327 | Py_INCREF(kw->arg); |
| 4328 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4329 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4330 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4331 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4332 | return 1; |
| 4333 | } |
| 4334 | else { |
| 4335 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4336 | return 1; |
| 4337 | } |
| 4338 | |
| 4339 | ex_call: |
| 4340 | |
| 4341 | /* Do positional arguments. */ |
| 4342 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4343 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4344 | } |
| 4345 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4346 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4347 | return 0; |
| 4348 | } |
| 4349 | /* Then keyword arguments */ |
| 4350 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4351 | /* Has a new dict been pushed */ |
| 4352 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4353 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4354 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4355 | for (i = 0; i < nkwelts; i++) { |
| 4356 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4357 | if (kw->arg == NULL) { |
| 4358 | /* A keyword argument unpacking. */ |
| 4359 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4360 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4361 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4362 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4363 | if (have_dict) { |
| 4364 | ADDOP_I(c, DICT_MERGE, 1); |
| 4365 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4366 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4367 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4368 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4369 | if (!have_dict) { |
| 4370 | ADDOP_I(c, BUILD_MAP, 0); |
| 4371 | have_dict = 1; |
| 4372 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4373 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4374 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4375 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4376 | else { |
| 4377 | nseen++; |
| 4378 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4379 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4380 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4381 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4382 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4383 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4384 | } |
| 4385 | if (have_dict) { |
| 4386 | ADDOP_I(c, DICT_MERGE, 1); |
| 4387 | } |
| 4388 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4389 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4390 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4392 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4393 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4396 | |
| 4397 | /* List and set comprehensions and generator expressions work by creating a |
| 4398 | nested function to perform the actual iteration. This means that the |
| 4399 | iteration variables don't leak into the current scope. |
| 4400 | The defined function is called immediately following its definition, with the |
| 4401 | result of that call being the result of the expression. |
| 4402 | The LC/SC version returns the populated container, while the GE version is |
| 4403 | flagged in symtable.c as a generator, so it returns the generator object |
| 4404 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4405 | |
| 4406 | Possible cleanups: |
| 4407 | - iterate over the generator sequence instead of using recursion |
| 4408 | */ |
| 4409 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4410 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4411 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4413 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4414 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4416 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4417 | comprehension_ty gen; |
| 4418 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4419 | if (gen->is_async) { |
| 4420 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4421 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4422 | } else { |
| 4423 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4424 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | static int |
| 4429 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4430 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4431 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4432 | expr_ty elt, expr_ty val, int type) |
| 4433 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | /* generate code for the iterator, then each of the ifs, |
| 4435 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4437 | comprehension_ty gen; |
| 4438 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4439 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | start = compiler_new_block(c); |
| 4442 | skip = compiler_new_block(c); |
| 4443 | if_cleanup = compiler_new_block(c); |
| 4444 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4446 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4447 | anchor == NULL) |
| 4448 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4450 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | if (gen_index == 0) { |
| 4453 | /* Receive outermost iter as an implicit argument */ |
| 4454 | c->u->u_argcount = 1; |
| 4455 | ADDOP_I(c, LOAD_FAST, 0); |
| 4456 | } |
| 4457 | else { |
| 4458 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4459 | /* Fast path for the temporary variable assignment idiom: |
| 4460 | for y in [f(x)] |
| 4461 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4462 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4463 | switch (gen->iter->kind) { |
| 4464 | case List_kind: |
| 4465 | elts = gen->iter->v.List.elts; |
| 4466 | break; |
| 4467 | case Tuple_kind: |
| 4468 | elts = gen->iter->v.Tuple.elts; |
| 4469 | break; |
| 4470 | default: |
| 4471 | elts = NULL; |
| 4472 | } |
| 4473 | if (asdl_seq_LEN(elts) == 1) { |
| 4474 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4475 | if (elt->kind != Starred_kind) { |
| 4476 | VISIT(c, expr, elt); |
| 4477 | start = NULL; |
| 4478 | } |
| 4479 | } |
| 4480 | if (start) { |
| 4481 | VISIT(c, expr, gen->iter); |
| 4482 | ADDOP(c, GET_ITER); |
| 4483 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4485 | if (start) { |
| 4486 | depth++; |
| 4487 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4488 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4489 | NEXT_BLOCK(c); |
| 4490 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | /* XXX this needs to be cleaned up...a lot! */ |
| 4494 | n = asdl_seq_LEN(gen->ifs); |
| 4495 | for (i = 0; i < n; i++) { |
| 4496 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4497 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4498 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | NEXT_BLOCK(c); |
| 4500 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4502 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4503 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4504 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | elt, val, type)) |
| 4506 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | /* only append after the last for generator */ |
| 4509 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4510 | /* comprehension specific code */ |
| 4511 | switch (type) { |
| 4512 | case COMP_GENEXP: |
| 4513 | VISIT(c, expr, elt); |
| 4514 | ADDOP(c, YIELD_VALUE); |
| 4515 | ADDOP(c, POP_TOP); |
| 4516 | break; |
| 4517 | case COMP_LISTCOMP: |
| 4518 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4519 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | break; |
| 4521 | case COMP_SETCOMP: |
| 4522 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4523 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | break; |
| 4525 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4526 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4527 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4529 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4530 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4531 | break; |
| 4532 | default: |
| 4533 | return 0; |
| 4534 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4536 | compiler_use_next_block(c, skip); |
| 4537 | } |
| 4538 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4539 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4540 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4541 | compiler_use_next_block(c, anchor); |
| 4542 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4543 | |
| 4544 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4548 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4549 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4550 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4551 | expr_ty elt, expr_ty val, int type) |
| 4552 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4553 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4554 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4555 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4556 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4557 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4558 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4560 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4561 | return 0; |
| 4562 | } |
| 4563 | |
| 4564 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4565 | |
| 4566 | if (gen_index == 0) { |
| 4567 | /* Receive outermost iter as an implicit argument */ |
| 4568 | c->u->u_argcount = 1; |
| 4569 | ADDOP_I(c, LOAD_FAST, 0); |
| 4570 | } |
| 4571 | else { |
| 4572 | /* Sub-iter - calculate on the fly */ |
| 4573 | VISIT(c, expr, gen->iter); |
| 4574 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4575 | } |
| 4576 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4577 | compiler_use_next_block(c, start); |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame^] | 4578 | /* Runtime will push a block here, so we need to account for that */ |
| 4579 | if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start, |
| 4580 | NULL, NULL)) { |
| 4581 | return 0; |
| 4582 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4583 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4584 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4585 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4586 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4587 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4588 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4589 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4590 | |
| 4591 | n = asdl_seq_LEN(gen->ifs); |
| 4592 | for (i = 0; i < n; i++) { |
| 4593 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4594 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4595 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4596 | NEXT_BLOCK(c); |
| 4597 | } |
| 4598 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4599 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4600 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4601 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4602 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4603 | elt, val, type)) |
| 4604 | return 0; |
| 4605 | |
| 4606 | /* only append after the last for generator */ |
| 4607 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4608 | /* comprehension specific code */ |
| 4609 | switch (type) { |
| 4610 | case COMP_GENEXP: |
| 4611 | VISIT(c, expr, elt); |
| 4612 | ADDOP(c, YIELD_VALUE); |
| 4613 | ADDOP(c, POP_TOP); |
| 4614 | break; |
| 4615 | case COMP_LISTCOMP: |
| 4616 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4617 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4618 | break; |
| 4619 | case COMP_SETCOMP: |
| 4620 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4621 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4622 | break; |
| 4623 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4624 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4625 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4626 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4627 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4628 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4629 | break; |
| 4630 | default: |
| 4631 | return 0; |
| 4632 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4633 | } |
| 4634 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4635 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4636 | |
tomKPZ | 7a7ba3d | 2021-04-07 07:43:45 -0700 | [diff] [blame^] | 4637 | compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); |
| 4638 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4639 | compiler_use_next_block(c, except); |
| 4640 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4641 | |
| 4642 | return 1; |
| 4643 | } |
| 4644 | |
| 4645 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4646 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4647 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4648 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4651 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4652 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4653 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4654 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4655 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4656 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4657 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4658 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4659 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4660 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4661 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4667 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4668 | 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] | 4669 | compiler_error(c, "asynchronous comprehension outside of " |
| 4670 | "an asynchronous function"); |
| 4671 | goto error_in_scope; |
| 4672 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4674 | if (type != COMP_GENEXP) { |
| 4675 | int op; |
| 4676 | switch (type) { |
| 4677 | case COMP_LISTCOMP: |
| 4678 | op = BUILD_LIST; |
| 4679 | break; |
| 4680 | case COMP_SETCOMP: |
| 4681 | op = BUILD_SET; |
| 4682 | break; |
| 4683 | case COMP_DICTCOMP: |
| 4684 | op = BUILD_MAP; |
| 4685 | break; |
| 4686 | default: |
| 4687 | PyErr_Format(PyExc_SystemError, |
| 4688 | "unknown comprehension type %d", type); |
| 4689 | goto error_in_scope; |
| 4690 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | ADDOP_I(c, op, 0); |
| 4693 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4694 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4695 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4696 | val, type)) |
| 4697 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | if (type != COMP_GENEXP) { |
| 4700 | ADDOP(c, RETURN_VALUE); |
| 4701 | } |
| 4702 | |
| 4703 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4704 | qualname = c->u->u_qualname; |
| 4705 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4707 | if (top_level_await && is_async_generator){ |
| 4708 | c->u->u_ste->ste_coroutine = 1; |
| 4709 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4710 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | goto error; |
| 4712 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4713 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4715 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4716 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | Py_DECREF(co); |
| 4718 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4719 | VISIT(c, expr, outermost->iter); |
| 4720 | |
| 4721 | if (outermost->is_async) { |
| 4722 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4723 | } else { |
| 4724 | ADDOP(c, GET_ITER); |
| 4725 | } |
| 4726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4727 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4728 | |
| 4729 | if (is_async_generator && type != COMP_GENEXP) { |
| 4730 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4731 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4732 | ADDOP(c, YIELD_FROM); |
| 4733 | } |
| 4734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4736 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4738 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4739 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4740 | Py_XDECREF(co); |
| 4741 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4745 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | static identifier name; |
| 4748 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4749 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4750 | if (!name) |
| 4751 | return 0; |
| 4752 | } |
| 4753 | assert(e->kind == GeneratorExp_kind); |
| 4754 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4755 | e->v.GeneratorExp.generators, |
| 4756 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4760 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | static identifier name; |
| 4763 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4764 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4765 | if (!name) |
| 4766 | return 0; |
| 4767 | } |
| 4768 | assert(e->kind == ListComp_kind); |
| 4769 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4770 | e->v.ListComp.generators, |
| 4771 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | static int |
| 4775 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4777 | static identifier name; |
| 4778 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4779 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | if (!name) |
| 4781 | return 0; |
| 4782 | } |
| 4783 | assert(e->kind == SetComp_kind); |
| 4784 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4785 | e->v.SetComp.generators, |
| 4786 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | |
| 4790 | static int |
| 4791 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4793 | static identifier name; |
| 4794 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4795 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | if (!name) |
| 4797 | return 0; |
| 4798 | } |
| 4799 | assert(e->kind == DictComp_kind); |
| 4800 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4801 | e->v.DictComp.generators, |
| 4802 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | |
| 4806 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4807 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4809 | VISIT(c, expr, k->value); |
| 4810 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4813 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4814 | whether they are true or false. |
| 4815 | |
| 4816 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4817 | */ |
| 4818 | |
| 4819 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4820 | compiler_with_except_finish(struct compiler *c) { |
| 4821 | basicblock *exit; |
| 4822 | exit = compiler_new_block(c); |
| 4823 | if (exit == NULL) |
| 4824 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4825 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4826 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4827 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4828 | compiler_use_next_block(c, exit); |
| 4829 | ADDOP(c, POP_TOP); |
| 4830 | ADDOP(c, POP_TOP); |
| 4831 | ADDOP(c, POP_TOP); |
| 4832 | ADDOP(c, POP_EXCEPT); |
| 4833 | ADDOP(c, POP_TOP); |
| 4834 | return 1; |
| 4835 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4836 | |
| 4837 | /* |
| 4838 | Implements the async with statement. |
| 4839 | |
| 4840 | The semantics outlined in that PEP are as follows: |
| 4841 | |
| 4842 | async with EXPR as VAR: |
| 4843 | BLOCK |
| 4844 | |
| 4845 | It is implemented roughly as: |
| 4846 | |
| 4847 | context = EXPR |
| 4848 | exit = context.__aexit__ # not calling it |
| 4849 | value = await context.__aenter__() |
| 4850 | try: |
| 4851 | VAR = value # if VAR present in the syntax |
| 4852 | BLOCK |
| 4853 | finally: |
| 4854 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4855 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4856 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4857 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4858 | if not (await exit(*exc)): |
| 4859 | raise |
| 4860 | */ |
| 4861 | static int |
| 4862 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4863 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4864 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4865 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4866 | |
| 4867 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4868 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4869 | c->u->u_ste->ste_coroutine = 1; |
| 4870 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4871 | return compiler_error(c, "'async with' outside async function"); |
| 4872 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4873 | |
| 4874 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4875 | final = compiler_new_block(c); |
| 4876 | exit = compiler_new_block(c); |
| 4877 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4878 | return 0; |
| 4879 | |
| 4880 | /* Evaluate EXPR */ |
| 4881 | VISIT(c, expr, item->context_expr); |
| 4882 | |
| 4883 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4884 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4885 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4886 | ADDOP(c, YIELD_FROM); |
| 4887 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4888 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4889 | |
| 4890 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4891 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4892 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | if (item->optional_vars) { |
| 4897 | VISIT(c, expr, item->optional_vars); |
| 4898 | } |
| 4899 | else { |
| 4900 | /* Discard result from context.__aenter__() */ |
| 4901 | ADDOP(c, POP_TOP); |
| 4902 | } |
| 4903 | |
| 4904 | pos++; |
| 4905 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4906 | /* BLOCK code */ |
| 4907 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4908 | else if (!compiler_async_with(c, s, pos)) |
| 4909 | return 0; |
| 4910 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4911 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4912 | ADDOP(c, POP_BLOCK); |
| 4913 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4914 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4915 | /* For successful outcome: |
| 4916 | * call __exit__(None, None, None) |
| 4917 | */ |
| 4918 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4919 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4920 | ADDOP(c, GET_AWAITABLE); |
| 4921 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4922 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4923 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4924 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4925 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4926 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4927 | |
| 4928 | /* For exceptional outcome: */ |
| 4929 | compiler_use_next_block(c, final); |
| 4930 | |
| 4931 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4932 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4933 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4934 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4935 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4936 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4937 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4938 | return 1; |
| 4939 | } |
| 4940 | |
| 4941 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4942 | /* |
| 4943 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4944 | with EXPR as VAR: |
| 4945 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4946 | is implemented as: |
| 4947 | <code for EXPR> |
| 4948 | SETUP_WITH E |
| 4949 | <code to store to VAR> or POP_TOP |
| 4950 | <code for BLOCK> |
| 4951 | LOAD_CONST (None, None, None) |
| 4952 | CALL_FUNCTION_EX 0 |
| 4953 | JUMP_FORWARD EXIT |
| 4954 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4955 | POP_JUMP_IF_TRUE T: |
| 4956 | RERAISE |
| 4957 | T: POP_TOP * 3 (remove exception from stack) |
| 4958 | POP_EXCEPT |
| 4959 | POP_TOP |
| 4960 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4961 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4962 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4963 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4964 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4965 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4966 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4967 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4968 | |
| 4969 | assert(s->kind == With_kind); |
| 4970 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4971 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4972 | final = compiler_new_block(c); |
| 4973 | exit = compiler_new_block(c); |
| 4974 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4975 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4976 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4977 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4978 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4979 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4980 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4981 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4982 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4983 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4984 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4985 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4988 | if (item->optional_vars) { |
| 4989 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4990 | } |
| 4991 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4993 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4996 | pos++; |
| 4997 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4998 | /* BLOCK code */ |
| 4999 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 5000 | else if (!compiler_with(c, s, pos)) |
| 5001 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5002 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 5003 | |
| 5004 | /* Mark all following code as artificial */ |
| 5005 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5006 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5007 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5008 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5009 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5010 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5011 | /* For successful outcome: |
| 5012 | * call __exit__(None, None, None) |
| 5013 | */ |
| 5014 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5015 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5016 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5017 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5018 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5019 | /* For exceptional outcome: */ |
| 5020 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5021 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5022 | ADDOP(c, WITH_EXCEPT_START); |
| 5023 | compiler_with_except_finish(c); |
| 5024 | |
| 5025 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5026 | return 1; |
| 5027 | } |
| 5028 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5029 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5030 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5031 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5033 | case NamedExpr_kind: |
| 5034 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5035 | ADDOP(c, DUP_TOP); |
| 5036 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5037 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5038 | case BoolOp_kind: |
| 5039 | return compiler_boolop(c, e); |
| 5040 | case BinOp_kind: |
| 5041 | VISIT(c, expr, e->v.BinOp.left); |
| 5042 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5043 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | break; |
| 5045 | case UnaryOp_kind: |
| 5046 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5047 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5048 | break; |
| 5049 | case Lambda_kind: |
| 5050 | return compiler_lambda(c, e); |
| 5051 | case IfExp_kind: |
| 5052 | return compiler_ifexp(c, e); |
| 5053 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5054 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5056 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5057 | case GeneratorExp_kind: |
| 5058 | return compiler_genexp(c, e); |
| 5059 | case ListComp_kind: |
| 5060 | return compiler_listcomp(c, e); |
| 5061 | case SetComp_kind: |
| 5062 | return compiler_setcomp(c, e); |
| 5063 | case DictComp_kind: |
| 5064 | return compiler_dictcomp(c, e); |
| 5065 | case Yield_kind: |
| 5066 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5067 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5068 | if (e->v.Yield.value) { |
| 5069 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | } |
| 5071 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5072 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5074 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5075 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5076 | case YieldFrom_kind: |
| 5077 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5078 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5079 | |
| 5080 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5081 | return compiler_error(c, "'yield from' inside async function"); |
| 5082 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5083 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5084 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5085 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5086 | ADDOP(c, YIELD_FROM); |
| 5087 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5088 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5089 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5090 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5091 | return compiler_error(c, "'await' outside function"); |
| 5092 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5093 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5094 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5095 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5096 | return compiler_error(c, "'await' outside async function"); |
| 5097 | } |
| 5098 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5099 | |
| 5100 | VISIT(c, expr, e->v.Await.value); |
| 5101 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5102 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5103 | ADDOP(c, YIELD_FROM); |
| 5104 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | case Compare_kind: |
| 5106 | return compiler_compare(c, e); |
| 5107 | case Call_kind: |
| 5108 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5109 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5110 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5111 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5112 | case JoinedStr_kind: |
| 5113 | return compiler_joined_str(c, e); |
| 5114 | case FormattedValue_kind: |
| 5115 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | /* The following exprs can be assignment targets. */ |
| 5117 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5118 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5120 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5121 | { |
| 5122 | int old_lineno = c->u->u_lineno; |
| 5123 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5124 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5125 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5127 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5128 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5129 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5130 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5131 | } |
| 5132 | int old_lineno = c->u->u_lineno; |
| 5133 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5134 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5135 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5136 | break; |
| 5137 | case Del: |
| 5138 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5139 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5140 | } |
| 5141 | break; |
| 5142 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5143 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5144 | case Starred_kind: |
| 5145 | switch (e->v.Starred.ctx) { |
| 5146 | case Store: |
| 5147 | /* In all legitimate cases, the Starred node was already replaced |
| 5148 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5149 | return compiler_error(c, |
| 5150 | "starred assignment target must be in a list or tuple"); |
| 5151 | default: |
| 5152 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5153 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5154 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5155 | break; |
| 5156 | case Slice_kind: |
| 5157 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | case Name_kind: |
| 5159 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5160 | /* child nodes of List and Tuple will have expr_context set */ |
| 5161 | case List_kind: |
| 5162 | return compiler_list(c, e); |
| 5163 | case Tuple_kind: |
| 5164 | return compiler_tuple(c, e); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5165 | case MatchAs_kind: |
| 5166 | case MatchOr_kind: |
| 5167 | // Can only occur in patterns, which are handled elsewhere. |
| 5168 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5169 | } |
| 5170 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5174 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5175 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5176 | int old_lineno = c->u->u_lineno; |
| 5177 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5178 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5179 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5180 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5181 | c->u->u_col_offset = old_col_offset; |
| 5182 | return res; |
| 5183 | } |
| 5184 | |
| 5185 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5186 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5188 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5189 | expr_ty e = s->v.AugAssign.target; |
| 5190 | |
| 5191 | int old_lineno = c->u->u_lineno; |
| 5192 | int old_col_offset = c->u->u_col_offset; |
| 5193 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5195 | switch (e->kind) { |
| 5196 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5197 | VISIT(c, expr, e->v.Attribute.value); |
| 5198 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5199 | int old_lineno = c->u->u_lineno; |
| 5200 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5201 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5202 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | break; |
| 5204 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5205 | VISIT(c, expr, e->v.Subscript.value); |
| 5206 | VISIT(c, expr, e->v.Subscript.slice); |
| 5207 | ADDOP(c, DUP_TOP_TWO); |
| 5208 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | break; |
| 5210 | case Name_kind: |
| 5211 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5212 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5213 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5214 | default: |
| 5215 | PyErr_Format(PyExc_SystemError, |
| 5216 | "invalid node type (%d) for augmented assignment", |
| 5217 | e->kind); |
| 5218 | return 0; |
| 5219 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5220 | |
| 5221 | c->u->u_lineno = old_lineno; |
| 5222 | c->u->u_col_offset = old_col_offset; |
| 5223 | |
| 5224 | VISIT(c, expr, s->v.AugAssign.value); |
| 5225 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5226 | |
| 5227 | SET_LOC(c, e); |
| 5228 | |
| 5229 | switch (e->kind) { |
| 5230 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5231 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5232 | ADDOP(c, ROT_TWO); |
| 5233 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5234 | break; |
| 5235 | case Subscript_kind: |
| 5236 | ADDOP(c, ROT_THREE); |
| 5237 | ADDOP(c, STORE_SUBSCR); |
| 5238 | break; |
| 5239 | case Name_kind: |
| 5240 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5241 | default: |
| 5242 | Py_UNREACHABLE(); |
| 5243 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5244 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5248 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5249 | { |
| 5250 | VISIT(c, expr, e); |
| 5251 | ADDOP(c, POP_TOP); |
| 5252 | return 1; |
| 5253 | } |
| 5254 | |
| 5255 | static int |
| 5256 | check_annotation(struct compiler *c, stmt_ty s) |
| 5257 | { |
| 5258 | /* Annotations are only evaluated in a module or class. */ |
| 5259 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5260 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5261 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5262 | } |
| 5263 | return 1; |
| 5264 | } |
| 5265 | |
| 5266 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5267 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5268 | { |
| 5269 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5270 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5271 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5272 | 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] | 5273 | return 0; |
| 5274 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5275 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5276 | return 0; |
| 5277 | } |
| 5278 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5279 | return 0; |
| 5280 | } |
| 5281 | return 1; |
| 5282 | case Tuple_kind: { |
| 5283 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5284 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5285 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5286 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5287 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5288 | return 0; |
| 5289 | } |
| 5290 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5291 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5292 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5293 | default: |
| 5294 | return check_ann_expr(c, e); |
| 5295 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | static int |
| 5299 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5300 | { |
| 5301 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5302 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5303 | |
| 5304 | assert(s->kind == AnnAssign_kind); |
| 5305 | |
| 5306 | /* We perform the actual assignment first. */ |
| 5307 | if (s->v.AnnAssign.value) { |
| 5308 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5309 | VISIT(c, expr, targ); |
| 5310 | } |
| 5311 | switch (targ->kind) { |
| 5312 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5313 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5314 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5315 | /* If we have a simple name in a module or class, store annotation. */ |
| 5316 | if (s->v.AnnAssign.simple && |
| 5317 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5318 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5319 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5320 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5321 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5322 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5323 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5324 | } |
| 5325 | break; |
| 5326 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5327 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5328 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5329 | if (!s->v.AnnAssign.value && |
| 5330 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5331 | return 0; |
| 5332 | } |
| 5333 | break; |
| 5334 | case Subscript_kind: |
| 5335 | if (!s->v.AnnAssign.value && |
| 5336 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5337 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5338 | return 0; |
| 5339 | } |
| 5340 | break; |
| 5341 | default: |
| 5342 | PyErr_Format(PyExc_SystemError, |
| 5343 | "invalid node type (%d) for annotated assignment", |
| 5344 | targ->kind); |
| 5345 | return 0; |
| 5346 | } |
| 5347 | /* Annotation is evaluated last. */ |
| 5348 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5349 | return 0; |
| 5350 | } |
| 5351 | return 1; |
| 5352 | } |
| 5353 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5354 | /* Raises a SyntaxError and returns 0. |
| 5355 | If something goes wrong, a different exception may be raised. |
| 5356 | */ |
| 5357 | |
| 5358 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5359 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5360 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5361 | va_list vargs; |
| 5362 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5363 | va_start(vargs, format); |
| 5364 | #else |
| 5365 | va_start(vargs); |
| 5366 | #endif |
| 5367 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5368 | va_end(vargs); |
| 5369 | if (msg == NULL) { |
| 5370 | return 0; |
| 5371 | } |
| 5372 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5373 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5374 | Py_INCREF(Py_None); |
| 5375 | loc = Py_None; |
| 5376 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5377 | PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename, |
| 5378 | c->u->u_lineno, c->u->u_col_offset + 1, loc); |
| 5379 | Py_DECREF(msg); |
| 5380 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5382 | } |
| 5383 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5384 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5385 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5386 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5390 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5391 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5392 | and returns 0. |
| 5393 | */ |
| 5394 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5395 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5396 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5397 | va_list vargs; |
| 5398 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5399 | va_start(vargs, format); |
| 5400 | #else |
| 5401 | va_start(vargs); |
| 5402 | #endif |
| 5403 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5404 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5405 | if (msg == NULL) { |
| 5406 | return 0; |
| 5407 | } |
| 5408 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5409 | c->u->u_lineno, NULL, NULL) < 0) |
| 5410 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5411 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5412 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5413 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5414 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5415 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5416 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5417 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5418 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5419 | return 0; |
| 5420 | } |
| 5421 | Py_DECREF(msg); |
| 5422 | return 1; |
| 5423 | } |
| 5424 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5425 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5426 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5427 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5428 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5429 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5430 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5431 | if (ctx == Load) { |
| 5432 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5433 | return 0; |
| 5434 | } |
| 5435 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5436 | return 0; |
| 5437 | } |
| 5438 | } |
| 5439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | case Store: op = STORE_SUBSCR; break; |
| 5443 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5445 | assert(op); |
| 5446 | VISIT(c, expr, e->v.Subscript.value); |
| 5447 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5448 | ADDOP(c, op); |
| 5449 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5453 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5455 | int n = 2; |
| 5456 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5458 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5459 | if (s->v.Slice.lower) { |
| 5460 | VISIT(c, expr, s->v.Slice.lower); |
| 5461 | } |
| 5462 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5463 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5464 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | if (s->v.Slice.upper) { |
| 5467 | VISIT(c, expr, s->v.Slice.upper); |
| 5468 | } |
| 5469 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5470 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
| 5473 | if (s->v.Slice.step) { |
| 5474 | n++; |
| 5475 | VISIT(c, expr, s->v.Slice.step); |
| 5476 | } |
| 5477 | ADDOP_I(c, BUILD_SLICE, n); |
| 5478 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5481 | |
| 5482 | // PEP 634: Structural Pattern Matching |
| 5483 | |
| 5484 | // To keep things simple, all compiler_pattern_* routines follow the convention |
| 5485 | // of replacing TOS (the subject for the given pattern) with either True (match) |
| 5486 | // or False (no match). We do this even for irrefutable patterns; the idea is |
| 5487 | // that it's much easier to smooth out any redundant pushing, popping, and |
| 5488 | // jumping in the peephole optimizer than to detect or predict it here. |
| 5489 | |
| 5490 | |
| 5491 | #define WILDCARD_CHECK(N) \ |
| 5492 | ((N)->kind == Name_kind && \ |
| 5493 | _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_")) |
| 5494 | |
| 5495 | |
| 5496 | static int |
| 5497 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5498 | { |
| 5499 | assert(!_PyUnicode_EqualToASCIIString(n, "_")); |
| 5500 | // Can't assign to the same name twice: |
| 5501 | if (pc->stores == NULL) { |
| 5502 | RETURN_IF_FALSE(pc->stores = PySet_New(NULL)); |
| 5503 | } |
| 5504 | else { |
| 5505 | int duplicate = PySet_Contains(pc->stores, n); |
| 5506 | if (duplicate < 0) { |
| 5507 | return 0; |
| 5508 | } |
| 5509 | if (duplicate) { |
| 5510 | const char *e = "multiple assignments to name %R in pattern"; |
| 5511 | return compiler_error(c, e, n); |
| 5512 | } |
| 5513 | } |
| 5514 | RETURN_IF_FALSE(!PySet_Add(pc->stores, n)); |
| 5515 | RETURN_IF_FALSE(compiler_nameop(c, n, Store)); |
| 5516 | return 1; |
| 5517 | } |
| 5518 | |
| 5519 | |
| 5520 | static int |
| 5521 | pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values, |
| 5522 | Py_ssize_t star, pattern_context *pc) |
| 5523 | { |
| 5524 | RETURN_IF_FALSE(unpack_helper(c, values)); |
| 5525 | // We've now got a bunch of new subjects on the stack. If any of them fail |
| 5526 | // to match, we need to pop everything else off, then finally push False. |
| 5527 | // fails is an array of blocks that correspond to the necessary amount of |
| 5528 | // popping for each element: |
| 5529 | basicblock **fails; |
| 5530 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5531 | fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size); |
| 5532 | if (fails == NULL) { |
| 5533 | PyErr_NoMemory(); |
| 5534 | return 0; |
| 5535 | } |
| 5536 | // NOTE: Can't use our nice returning macros anymore: they'll leak memory! |
| 5537 | // goto error on error. |
| 5538 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5539 | fails[i] = compiler_new_block(c); |
| 5540 | if (fails[i] == NULL) { |
| 5541 | goto error; |
| 5542 | } |
| 5543 | } |
| 5544 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5545 | expr_ty value = asdl_seq_GET(values, i); |
| 5546 | if (i == star) { |
| 5547 | assert(value->kind == Starred_kind); |
| 5548 | value = value->v.Starred.value; |
| 5549 | } |
| 5550 | if (!compiler_pattern_subpattern(c, value, pc) || |
| 5551 | !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) || |
| 5552 | compiler_next_block(c) == NULL) |
| 5553 | { |
| 5554 | goto error; |
| 5555 | } |
| 5556 | } |
| 5557 | // Success! |
| 5558 | basicblock *end = compiler_new_block(c); |
| 5559 | if (end == NULL || |
| 5560 | !compiler_addop_load_const(c, Py_True) || |
| 5561 | !compiler_addop_j(c, JUMP_FORWARD, end)) |
| 5562 | { |
| 5563 | goto error; |
| 5564 | } |
| 5565 | // This is where we handle failed sub-patterns. For a sequence pattern like |
| 5566 | // [a, b, c, d], this will look like: |
| 5567 | // fails[0]: POP_TOP |
| 5568 | // fails[1]: POP_TOP |
| 5569 | // fails[2]: POP_TOP |
| 5570 | // fails[3]: LOAD_CONST False |
| 5571 | for (Py_ssize_t i = 0; i < size - 1; i++) { |
| 5572 | compiler_use_next_block(c, fails[i]); |
| 5573 | if (!compiler_addop(c, POP_TOP)) { |
| 5574 | goto error; |
| 5575 | } |
| 5576 | } |
| 5577 | compiler_use_next_block(c, fails[size - 1]); |
| 5578 | if (!compiler_addop_load_const(c, Py_False)) { |
| 5579 | goto error; |
| 5580 | } |
| 5581 | compiler_use_next_block(c, end); |
| 5582 | PyObject_Free(fails); |
| 5583 | return 1; |
| 5584 | error: |
| 5585 | PyObject_Free(fails); |
| 5586 | return 0; |
| 5587 | } |
| 5588 | |
| 5589 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5590 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5591 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5592 | static int |
| 5593 | pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values, |
| 5594 | Py_ssize_t star, pattern_context *pc) |
| 5595 | { |
| 5596 | basicblock *end, *fail_pop_1; |
| 5597 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5598 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5599 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5600 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5601 | expr_ty value = asdl_seq_GET(values, i); |
| 5602 | if (WILDCARD_CHECK(value)) { |
| 5603 | continue; |
| 5604 | } |
| 5605 | if (i == star) { |
| 5606 | assert(value->kind == Starred_kind); |
| 5607 | assert(WILDCARD_CHECK(value->v.Starred.value)); |
| 5608 | continue; |
| 5609 | } |
| 5610 | ADDOP(c, DUP_TOP); |
| 5611 | if (i < star) { |
| 5612 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5613 | } |
| 5614 | else { |
| 5615 | // The subject may not support negative indexing! Compute a |
| 5616 | // nonnegative index: |
| 5617 | ADDOP(c, GET_LEN); |
| 5618 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5619 | ADDOP(c, BINARY_SUBTRACT); |
| 5620 | } |
| 5621 | ADDOP(c, BINARY_SUBSCR); |
| 5622 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5623 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5624 | NEXT_BLOCK(c); |
| 5625 | } |
| 5626 | ADDOP(c, POP_TOP); |
| 5627 | ADDOP_LOAD_CONST(c, Py_True); |
| 5628 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5629 | compiler_use_next_block(c, fail_pop_1); |
| 5630 | ADDOP(c, POP_TOP); |
| 5631 | ADDOP_LOAD_CONST(c, Py_False); |
| 5632 | compiler_use_next_block(c, end); |
| 5633 | return 1; |
| 5634 | } |
| 5635 | |
| 5636 | |
| 5637 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5638 | static int |
| 5639 | compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5640 | { |
| 5641 | int allow_irrefutable = pc->allow_irrefutable; |
| 5642 | pc->allow_irrefutable = 1; |
| 5643 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5644 | pc->allow_irrefutable = allow_irrefutable; |
| 5645 | return 1; |
| 5646 | } |
| 5647 | |
| 5648 | |
| 5649 | static int |
| 5650 | compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5651 | { |
| 5652 | assert(p->kind == MatchAs_kind); |
| 5653 | basicblock *end, *fail_pop_1; |
| 5654 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5655 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5656 | // Need to make a copy for (possibly) storing later: |
| 5657 | ADDOP(c, DUP_TOP); |
| 5658 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
| 5659 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5660 | NEXT_BLOCK(c); |
| 5661 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5662 | ADDOP_LOAD_CONST(c, Py_True); |
| 5663 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5664 | compiler_use_next_block(c, fail_pop_1); |
| 5665 | // Need to pop that unused copy from before: |
| 5666 | ADDOP(c, POP_TOP); |
| 5667 | ADDOP_LOAD_CONST(c, Py_False); |
| 5668 | compiler_use_next_block(c, end); |
| 5669 | return 1; |
| 5670 | } |
| 5671 | |
| 5672 | |
| 5673 | static int |
| 5674 | compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5675 | { |
| 5676 | assert(p->kind == Name_kind); |
| 5677 | assert(p->v.Name.ctx == Store); |
| 5678 | assert(!WILDCARD_CHECK(p)); |
| 5679 | if (!pc->allow_irrefutable) { |
| 5680 | // Whoops, can't have a name capture here! |
| 5681 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5682 | return compiler_error(c, e, p->v.Name.id); |
| 5683 | } |
| 5684 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc)); |
| 5685 | ADDOP_LOAD_CONST(c, Py_True); |
| 5686 | return 1; |
| 5687 | } |
| 5688 | |
| 5689 | |
| 5690 | static int |
| 5691 | compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5692 | { |
| 5693 | asdl_expr_seq *args = p->v.Call.args; |
| 5694 | asdl_keyword_seq *kwargs = p->v.Call.keywords; |
| 5695 | Py_ssize_t nargs = asdl_seq_LEN(args); |
| 5696 | Py_ssize_t nkwargs = asdl_seq_LEN(kwargs); |
| 5697 | if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) { |
| 5698 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5699 | return compiler_error(c, e, p->v.Call.func); |
| 5700 | } |
| 5701 | RETURN_IF_FALSE(!validate_keywords(c, kwargs)); |
| 5702 | basicblock *end, *fail_pop_1; |
| 5703 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5704 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5705 | VISIT(c, expr, p->v.Call.func); |
| 5706 | PyObject *kwnames; |
| 5707 | RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs)); |
| 5708 | Py_ssize_t i; |
| 5709 | for (i = 0; i < nkwargs; i++) { |
| 5710 | PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg; |
| 5711 | Py_INCREF(name); |
| 5712 | PyTuple_SET_ITEM(kwnames, i, name); |
| 5713 | } |
| 5714 | ADDOP_LOAD_CONST_NEW(c, kwnames); |
| 5715 | ADDOP_I(c, MATCH_CLASS, nargs); |
| 5716 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5717 | NEXT_BLOCK(c); |
| 5718 | // TOS is now a tuple of (nargs + nkwargs) attributes. |
| 5719 | for (i = 0; i < nargs + nkwargs; i++) { |
| 5720 | expr_ty arg; |
| 5721 | if (i < nargs) { |
| 5722 | // Positional: |
| 5723 | arg = asdl_seq_GET(args, i); |
| 5724 | } |
| 5725 | else { |
| 5726 | // Keyword: |
| 5727 | arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value; |
| 5728 | } |
| 5729 | if (WILDCARD_CHECK(arg)) { |
| 5730 | continue; |
| 5731 | } |
| 5732 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5733 | ADDOP(c, DUP_TOP); |
| 5734 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5735 | ADDOP(c, BINARY_SUBSCR); |
| 5736 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc)); |
| 5737 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5738 | NEXT_BLOCK(c); |
| 5739 | } |
| 5740 | // Success! Pop the tuple of attributes: |
| 5741 | ADDOP(c, POP_TOP); |
| 5742 | ADDOP_LOAD_CONST(c, Py_True); |
| 5743 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5744 | compiler_use_next_block(c, fail_pop_1); |
| 5745 | ADDOP(c, POP_TOP); |
| 5746 | ADDOP_LOAD_CONST(c, Py_False); |
| 5747 | compiler_use_next_block(c, end); |
| 5748 | return 1; |
| 5749 | } |
| 5750 | |
| 5751 | |
| 5752 | static int |
| 5753 | compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5754 | { |
| 5755 | assert(p->kind == Constant_kind); |
| 5756 | PyObject *v = p->v.Constant.value; |
| 5757 | ADDOP_LOAD_CONST(c, v); |
| 5758 | // Literal True, False, and None are compared by identity. All others use |
| 5759 | // equality: |
| 5760 | ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq); |
| 5761 | return 1; |
| 5762 | } |
| 5763 | |
| 5764 | |
| 5765 | static int |
| 5766 | compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5767 | { |
| 5768 | basicblock *end, *fail_pop_1, *fail_pop_3; |
| 5769 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5770 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5771 | RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c)); |
| 5772 | asdl_expr_seq *keys = p->v.Dict.keys; |
| 5773 | asdl_expr_seq *values = p->v.Dict.values; |
| 5774 | Py_ssize_t size = asdl_seq_LEN(values); |
Ikko Ashimine | 57827f8 | 2021-03-10 19:39:51 +0900 | [diff] [blame] | 5775 | // 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] | 5776 | int star = size ? !asdl_seq_GET(keys, size - 1) : 0; |
| 5777 | ADDOP(c, MATCH_MAPPING); |
| 5778 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5779 | NEXT_BLOCK(c); |
| 5780 | if (!size) { |
| 5781 | // If the pattern is just "{}", we're done! |
| 5782 | ADDOP(c, POP_TOP); |
| 5783 | ADDOP_LOAD_CONST(c, Py_True); |
| 5784 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5785 | compiler_use_next_block(c, fail_pop_1); |
| 5786 | ADDOP(c, POP_TOP); |
| 5787 | ADDOP_LOAD_CONST(c, Py_False); |
| 5788 | compiler_use_next_block(c, end); |
| 5789 | return 1; |
| 5790 | } |
| 5791 | if (size - star) { |
| 5792 | // If the pattern has any keys in it, perform a length check: |
| 5793 | ADDOP(c, GET_LEN); |
| 5794 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star)); |
| 5795 | ADDOP_COMPARE(c, GtE); |
| 5796 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5797 | NEXT_BLOCK(c); |
| 5798 | } |
| 5799 | if (INT_MAX < size - star - 1) { |
| 5800 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 5801 | } |
| 5802 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 5803 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
| 5804 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5805 | expr_ty key = asdl_seq_GET(keys, i); |
| 5806 | if (key == NULL) { |
| 5807 | const char *e = "can't use starred name here " |
| 5808 | "(consider moving to end)"; |
| 5809 | return compiler_error(c, e); |
| 5810 | } |
| 5811 | VISIT(c, expr, key); |
| 5812 | } |
| 5813 | ADDOP_I(c, BUILD_TUPLE, size - star); |
| 5814 | ADDOP(c, MATCH_KEYS); |
| 5815 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5816 | NEXT_BLOCK(c); |
| 5817 | // So far so good. There's now a tuple of values on the stack to match |
| 5818 | // sub-patterns against: |
| 5819 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5820 | expr_ty value = asdl_seq_GET(values, i); |
| 5821 | if (WILDCARD_CHECK(value)) { |
| 5822 | continue; |
| 5823 | } |
| 5824 | ADDOP(c, DUP_TOP); |
| 5825 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5826 | ADDOP(c, BINARY_SUBSCR); |
| 5827 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5828 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5829 | NEXT_BLOCK(c); |
| 5830 | } |
| 5831 | // If we get this far, it's a match! We're done with that tuple of values. |
| 5832 | ADDOP(c, POP_TOP); |
| 5833 | if (star) { |
| 5834 | // If we had a starred name, bind a dict of remaining items to it: |
| 5835 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
| 5836 | PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id; |
| 5837 | RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc)); |
| 5838 | } |
| 5839 | else { |
| 5840 | // Otherwise, we don't care about this tuple of keys anymore: |
| 5841 | ADDOP(c, POP_TOP); |
| 5842 | } |
| 5843 | // Pop the subject: |
| 5844 | ADDOP(c, POP_TOP); |
| 5845 | ADDOP_LOAD_CONST(c, Py_True); |
| 5846 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5847 | // The top two items are a tuple of values or None, followed by a tuple of |
| 5848 | // keys. Pop them both: |
| 5849 | compiler_use_next_block(c, fail_pop_3); |
| 5850 | ADDOP(c, POP_TOP); |
| 5851 | ADDOP(c, POP_TOP); |
| 5852 | compiler_use_next_block(c, fail_pop_1); |
| 5853 | // Pop the subject: |
| 5854 | ADDOP(c, POP_TOP); |
| 5855 | ADDOP_LOAD_CONST(c, Py_False); |
| 5856 | compiler_use_next_block(c, end); |
| 5857 | return 1; |
| 5858 | } |
| 5859 | |
| 5860 | |
| 5861 | static int |
| 5862 | compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5863 | { |
| 5864 | assert(p->kind == MatchOr_kind); |
| 5865 | // control is the set of names bound by the first alternative. If all of the |
| 5866 | // others bind the same names (they should), then this becomes pc->stores. |
| 5867 | PyObject *control = NULL; |
| 5868 | basicblock *end, *pass_pop_1; |
| 5869 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5870 | RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c)); |
| 5871 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 5872 | assert(size > 1); |
| 5873 | // We're going to be messing with pc. Keep the original info handy: |
| 5874 | PyObject *stores_init = pc->stores; |
| 5875 | int allow_irrefutable = pc->allow_irrefutable; |
| 5876 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5877 | // NOTE: Can't use our nice returning macros in here: they'll leak sets! |
| 5878 | expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
| 5879 | pc->stores = PySet_New(stores_init); |
| 5880 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 5881 | int is_last = i == size - 1; |
| 5882 | pc->allow_irrefutable = allow_irrefutable && is_last; |
| 5883 | SET_LOC(c, alt); |
| 5884 | if (pc->stores == NULL || |
| 5885 | // Only copy the subject if we're *not* on the last alternative: |
| 5886 | (!is_last && !compiler_addop(c, DUP_TOP)) || |
| 5887 | !compiler_pattern(c, alt, pc) || |
| 5888 | // Only jump if we're *not* on the last alternative: |
| 5889 | (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) || |
| 5890 | !compiler_next_block(c)) |
| 5891 | { |
| 5892 | goto fail; |
| 5893 | } |
| 5894 | if (!i) { |
| 5895 | // If this is the first alternative, save its stores as a "control" |
| 5896 | // for the others (they can't bind a different set of names): |
| 5897 | control = pc->stores; |
| 5898 | continue; |
| 5899 | } |
| 5900 | if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) { |
| 5901 | // Otherwise, check to see if we differ from the control set: |
| 5902 | PyObject *diff = PyNumber_InPlaceXor(pc->stores, control); |
| 5903 | if (diff == NULL) { |
| 5904 | goto fail; |
| 5905 | } |
| 5906 | if (PySet_GET_SIZE(diff)) { |
| 5907 | // The names differ! Raise. |
| 5908 | Py_DECREF(diff); |
| 5909 | compiler_error(c, "alternative patterns bind different names"); |
| 5910 | goto fail; |
| 5911 | } |
| 5912 | Py_DECREF(diff); |
| 5913 | } |
| 5914 | Py_DECREF(pc->stores); |
| 5915 | } |
| 5916 | Py_XDECREF(stores_init); |
| 5917 | // Update pc->stores and restore pc->allow_irrefutable: |
| 5918 | pc->stores = control; |
| 5919 | pc->allow_irrefutable = allow_irrefutable; |
| 5920 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5921 | compiler_use_next_block(c, pass_pop_1); |
| 5922 | ADDOP(c, POP_TOP); |
| 5923 | ADDOP_LOAD_CONST(c, Py_True); |
| 5924 | compiler_use_next_block(c, end); |
| 5925 | return 1; |
| 5926 | fail: |
| 5927 | Py_XDECREF(stores_init); |
| 5928 | Py_XDECREF(control); |
| 5929 | return 0; |
| 5930 | } |
| 5931 | |
| 5932 | |
| 5933 | static int |
| 5934 | compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5935 | { |
| 5936 | assert(p->kind == List_kind || p->kind == Tuple_kind); |
| 5937 | asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts |
| 5938 | : p->v.List.elts; |
| 5939 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5940 | Py_ssize_t star = -1; |
| 5941 | int only_wildcard = 1; |
| 5942 | int star_wildcard = 0; |
| 5943 | // Find a starred name, if it exists. There may be at most one: |
| 5944 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5945 | expr_ty value = asdl_seq_GET(values, i); |
| 5946 | if (value->kind == Starred_kind) { |
| 5947 | value = value->v.Starred.value; |
| 5948 | if (star >= 0) { |
| 5949 | const char *e = "multiple starred names in sequence pattern"; |
| 5950 | return compiler_error(c, e); |
| 5951 | } |
| 5952 | star_wildcard = WILDCARD_CHECK(value); |
| 5953 | star = i; |
| 5954 | } |
| 5955 | only_wildcard &= WILDCARD_CHECK(value); |
| 5956 | } |
| 5957 | basicblock *end, *fail_pop_1; |
| 5958 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5959 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5960 | ADDOP(c, MATCH_SEQUENCE); |
| 5961 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5962 | NEXT_BLOCK(c); |
| 5963 | if (star < 0) { |
| 5964 | // No star: len(subject) == size |
| 5965 | ADDOP(c, GET_LEN); |
| 5966 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 5967 | ADDOP_COMPARE(c, Eq); |
| 5968 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5969 | NEXT_BLOCK(c); |
| 5970 | } |
| 5971 | else if (size > 1) { |
| 5972 | // Star: len(subject) >= size - 1 |
| 5973 | ADDOP(c, GET_LEN); |
| 5974 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 5975 | ADDOP_COMPARE(c, GtE); |
| 5976 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5977 | NEXT_BLOCK(c); |
| 5978 | } |
| 5979 | if (only_wildcard) { |
| 5980 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 5981 | ADDOP(c, POP_TOP); |
| 5982 | ADDOP_LOAD_CONST(c, Py_True); |
| 5983 | } |
| 5984 | else if (star_wildcard) { |
| 5985 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc)); |
| 5986 | } |
| 5987 | else { |
| 5988 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc)); |
| 5989 | } |
| 5990 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5991 | compiler_use_next_block(c, fail_pop_1); |
| 5992 | ADDOP(c, POP_TOP) |
| 5993 | ADDOP_LOAD_CONST(c, Py_False); |
| 5994 | compiler_use_next_block(c, end); |
| 5995 | return 1; |
| 5996 | } |
| 5997 | |
| 5998 | |
| 5999 | static int |
| 6000 | compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6001 | { |
| 6002 | assert(p->kind == Attribute_kind); |
| 6003 | assert(p->v.Attribute.ctx == Load); |
| 6004 | VISIT(c, expr, p); |
| 6005 | ADDOP_COMPARE(c, Eq); |
| 6006 | return 1; |
| 6007 | } |
| 6008 | |
| 6009 | |
| 6010 | static int |
| 6011 | compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6012 | { |
| 6013 | assert(p->kind == Name_kind); |
| 6014 | assert(p->v.Name.ctx == Store); |
| 6015 | assert(WILDCARD_CHECK(p)); |
| 6016 | if (!pc->allow_irrefutable) { |
| 6017 | // Whoops, can't have a wildcard here! |
| 6018 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 6019 | return compiler_error(c, e); |
| 6020 | } |
| 6021 | ADDOP(c, POP_TOP); |
| 6022 | ADDOP_LOAD_CONST(c, Py_True); |
| 6023 | return 1; |
| 6024 | } |
| 6025 | |
| 6026 | |
| 6027 | static int |
| 6028 | compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6029 | { |
| 6030 | SET_LOC(c, p); |
| 6031 | switch (p->kind) { |
| 6032 | case Attribute_kind: |
| 6033 | return compiler_pattern_value(c, p, pc); |
| 6034 | case BinOp_kind: |
| 6035 | // Because we allow "2+2j", things like "2+2" make it this far: |
| 6036 | return compiler_error(c, "patterns cannot include operators"); |
| 6037 | case Call_kind: |
| 6038 | return compiler_pattern_class(c, p, pc); |
| 6039 | case Constant_kind: |
| 6040 | return compiler_pattern_literal(c, p, pc); |
| 6041 | case Dict_kind: |
| 6042 | return compiler_pattern_mapping(c, p, pc); |
| 6043 | case JoinedStr_kind: |
| 6044 | // Because we allow strings, f-strings make it this far: |
| 6045 | return compiler_error(c, "patterns cannot include f-strings"); |
| 6046 | case List_kind: |
| 6047 | case Tuple_kind: |
| 6048 | return compiler_pattern_sequence(c, p, pc); |
| 6049 | case MatchAs_kind: |
| 6050 | return compiler_pattern_as(c, p, pc); |
| 6051 | case MatchOr_kind: |
| 6052 | return compiler_pattern_or(c, p, pc); |
| 6053 | case Name_kind: |
| 6054 | if (WILDCARD_CHECK(p)) { |
| 6055 | return compiler_pattern_wildcard(c, p, pc); |
| 6056 | } |
| 6057 | return compiler_pattern_capture(c, p, pc); |
| 6058 | default: |
| 6059 | Py_UNREACHABLE(); |
| 6060 | } |
| 6061 | } |
| 6062 | |
| 6063 | |
| 6064 | static int |
| 6065 | compiler_match(struct compiler *c, stmt_ty s) |
| 6066 | { |
| 6067 | VISIT(c, expr, s->v.Match.subject); |
| 6068 | basicblock *next, *end; |
| 6069 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6070 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
| 6071 | assert(cases); |
| 6072 | pattern_context pc; |
| 6073 | // We use pc.stores to track: |
| 6074 | // - Repeated name assignments in the same pattern. |
| 6075 | // - Different name assignments in alternatives. |
| 6076 | // It's a set of names, but we don't create it until it's needed: |
| 6077 | pc.stores = NULL; |
| 6078 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6079 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6080 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6081 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6082 | SET_LOC(c, m->pattern); |
| 6083 | RETURN_IF_FALSE(next = compiler_new_block(c)); |
| 6084 | // If pc.allow_irrefutable is 0, any name captures against our subject |
| 6085 | // will raise. Irrefutable cases must be either guarded, last, or both: |
| 6086 | pc.allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6087 | // Only copy the subject if we're *not* on the last case: |
| 6088 | if (i != cases - has_default - 1) { |
| 6089 | ADDOP(c, DUP_TOP); |
| 6090 | } |
| 6091 | int result = compiler_pattern(c, m->pattern, &pc); |
| 6092 | Py_CLEAR(pc.stores); |
| 6093 | RETURN_IF_FALSE(result); |
| 6094 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next); |
| 6095 | NEXT_BLOCK(c); |
| 6096 | if (m->guard) { |
| 6097 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0)); |
| 6098 | } |
| 6099 | // Success! Pop the subject off, we're done with it: |
| 6100 | if (i != cases - has_default - 1) { |
| 6101 | ADDOP(c, POP_TOP); |
| 6102 | } |
| 6103 | VISIT_SEQ(c, stmt, m->body); |
| 6104 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6105 | compiler_use_next_block(c, next); |
| 6106 | } |
| 6107 | if (has_default) { |
| 6108 | if (cases == 1) { |
| 6109 | // No matches. Done with the subject: |
| 6110 | ADDOP(c, POP_TOP); |
| 6111 | } |
| 6112 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6113 | // pushing and popping in the loop above: |
| 6114 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6115 | SET_LOC(c, m->pattern); |
| 6116 | if (m->guard) { |
| 6117 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6118 | } |
| 6119 | VISIT_SEQ(c, stmt, m->body); |
| 6120 | } |
| 6121 | compiler_use_next_block(c, end); |
| 6122 | return 1; |
| 6123 | } |
| 6124 | |
| 6125 | |
| 6126 | #undef WILDCARD_CHECK |
| 6127 | |
| 6128 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6129 | /* End of the compiler section, beginning of the assembler section */ |
| 6130 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6131 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6132 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6133 | |
| 6134 | XXX must handle implicit jumps from one block to next |
| 6135 | */ |
| 6136 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6137 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6138 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6139 | int a_offset; /* offset into bytecode */ |
| 6140 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6142 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6143 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6144 | int a_lineno; /* lineno of last emitted instruction */ |
| 6145 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6146 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6147 | }; |
| 6148 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6149 | Py_LOCAL_INLINE(void) |
| 6150 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6151 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6152 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6153 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6154 | assert(b->b_startdepth < 0); |
| 6155 | b->b_startdepth = depth; |
| 6156 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6157 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
| 6160 | /* Find the flow path that needs the largest stack. We assume that |
| 6161 | * cycles in the flow graph have no net effect on the stack depth. |
| 6162 | */ |
| 6163 | static int |
| 6164 | stackdepth(struct compiler *c) |
| 6165 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6166 | basicblock *b, *entryblock = NULL; |
| 6167 | basicblock **stack, **sp; |
| 6168 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6169 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6170 | b->b_startdepth = INT_MIN; |
| 6171 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6172 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6173 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6174 | assert(entryblock!= NULL); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6175 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6176 | if (!stack) { |
| 6177 | PyErr_NoMemory(); |
| 6178 | return -1; |
| 6179 | } |
| 6180 | |
| 6181 | sp = stack; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6182 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
| 6183 | stackdepth_push(&sp, entryblock, 1); |
| 6184 | } else { |
| 6185 | stackdepth_push(&sp, entryblock, 0); |
| 6186 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6187 | while (sp != stack) { |
| 6188 | b = *--sp; |
| 6189 | int depth = b->b_startdepth; |
| 6190 | assert(depth >= 0); |
| 6191 | basicblock *next = b->b_next; |
| 6192 | for (int i = 0; i < b->b_iused; i++) { |
| 6193 | struct instr *instr = &b->b_instr[i]; |
| 6194 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6195 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6196 | PyErr_Format(PyExc_SystemError, |
| 6197 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6198 | instr->i_opcode, instr->i_oparg); |
| 6199 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6200 | } |
| 6201 | int new_depth = depth + effect; |
| 6202 | if (new_depth > maxdepth) { |
| 6203 | maxdepth = new_depth; |
| 6204 | } |
| 6205 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6206 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6207 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6208 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6209 | int target_depth = depth + effect; |
| 6210 | if (target_depth > maxdepth) { |
| 6211 | maxdepth = target_depth; |
| 6212 | } |
| 6213 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6214 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6215 | } |
| 6216 | depth = new_depth; |
| 6217 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6218 | instr->i_opcode == JUMP_FORWARD || |
| 6219 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6220 | instr->i_opcode == RAISE_VARARGS || |
| 6221 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6222 | { |
| 6223 | /* remaining code is dead */ |
| 6224 | next = NULL; |
| 6225 | break; |
| 6226 | } |
| 6227 | } |
| 6228 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6229 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6230 | stackdepth_push(&sp, next, depth); |
| 6231 | } |
| 6232 | } |
| 6233 | PyObject_Free(stack); |
| 6234 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | static int |
| 6238 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6240 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6241 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6242 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6243 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6244 | if (a->a_bytecode == NULL) { |
| 6245 | goto error; |
| 6246 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6247 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6248 | if (a->a_lnotab == NULL) { |
| 6249 | goto error; |
| 6250 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6251 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6252 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6253 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6254 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6255 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6256 | error: |
| 6257 | Py_XDECREF(a->a_bytecode); |
| 6258 | Py_XDECREF(a->a_lnotab); |
| 6259 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
| 6262 | static void |
| 6263 | assemble_free(struct assembler *a) |
| 6264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6265 | Py_XDECREF(a->a_bytecode); |
| 6266 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6269 | static int |
| 6270 | blocksize(basicblock *b) |
| 6271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6272 | int i; |
| 6273 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6275 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6276 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6277 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6280 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6281 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6282 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6283 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6284 | if (a->a_lnotab_off + 2 >= len) { |
| 6285 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6286 | return 0; |
| 6287 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6288 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6289 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6291 | *lnotab++ = bdelta; |
| 6292 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6293 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6296 | /* Appends a range to the end of the line number table. See |
| 6297 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6298 | |
| 6299 | static int |
| 6300 | assemble_line_range(struct assembler *a) |
| 6301 | { |
| 6302 | int ldelta, bdelta; |
| 6303 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6304 | if (bdelta == 0) { |
| 6305 | return 1; |
| 6306 | } |
| 6307 | if (a->a_lineno < 0) { |
| 6308 | ldelta = -128; |
| 6309 | } |
| 6310 | else { |
| 6311 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6312 | a->a_prevlineno = a->a_lineno; |
| 6313 | while (ldelta > 127) { |
| 6314 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6315 | return 0; |
| 6316 | } |
| 6317 | ldelta -= 127; |
| 6318 | } |
| 6319 | while (ldelta < -127) { |
| 6320 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6321 | return 0; |
| 6322 | } |
| 6323 | ldelta += 127; |
| 6324 | } |
| 6325 | } |
| 6326 | assert(-128 <= ldelta && ldelta < 128); |
| 6327 | while (bdelta > 254) { |
| 6328 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6329 | return 0; |
| 6330 | } |
| 6331 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6332 | bdelta -= 254; |
| 6333 | } |
| 6334 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6335 | return 0; |
| 6336 | } |
| 6337 | a->a_lineno_start = a->a_offset; |
| 6338 | return 1; |
| 6339 | } |
| 6340 | |
| 6341 | static int |
| 6342 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6343 | { |
| 6344 | if (i->i_lineno == a->a_lineno) { |
| 6345 | return 1; |
| 6346 | } |
| 6347 | if (!assemble_line_range(a)) { |
| 6348 | return 0; |
| 6349 | } |
| 6350 | a->a_lineno = i->i_lineno; |
| 6351 | return 1; |
| 6352 | } |
| 6353 | |
| 6354 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6355 | /* assemble_emit() |
| 6356 | Extend the bytecode with a new instruction. |
| 6357 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6358 | */ |
| 6359 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6360 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6361 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6362 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6363 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6364 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6365 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6366 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6367 | arg = i->i_oparg; |
| 6368 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6370 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6371 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6372 | if (len > PY_SSIZE_T_MAX / 2) |
| 6373 | return 0; |
| 6374 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6375 | return 0; |
| 6376 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6377 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6378 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6379 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6383 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6384 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6387 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6388 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6390 | /* Compute the size of each block and fixup jump args. |
| 6391 | Replace block pointer with position in bytecode. */ |
| 6392 | do { |
| 6393 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6394 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6395 | bsize = blocksize(b); |
| 6396 | b->b_offset = totsize; |
| 6397 | totsize += bsize; |
| 6398 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6399 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6401 | bsize = b->b_offset; |
| 6402 | for (i = 0; i < b->b_iused; i++) { |
| 6403 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6404 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6405 | /* Relative jumps are computed relative to |
| 6406 | the instruction pointer after fetching |
| 6407 | the jump instruction. |
| 6408 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6409 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6410 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6411 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6412 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6413 | instr->i_oparg -= bsize; |
| 6414 | } |
| 6415 | if (instrsize(instr->i_oparg) != isize) { |
| 6416 | extended_arg_recompile = 1; |
| 6417 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6418 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6419 | } |
| 6420 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6422 | /* XXX: This is an awful hack that could hurt performance, but |
| 6423 | on the bright side it should work until we come up |
| 6424 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6426 | The issue is that in the first loop blocksize() is called |
| 6427 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6428 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6429 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6431 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6432 | The only EXTENDED_ARGs that could be popping up are |
| 6433 | ones in jump instructions. So this should converge |
| 6434 | fairly quickly. |
| 6435 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6436 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6439 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6440 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6442 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6443 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6445 | tuple = PyTuple_New(size); |
| 6446 | if (tuple == NULL) |
| 6447 | return NULL; |
| 6448 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6449 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6450 | Py_INCREF(k); |
| 6451 | assert((i - offset) < size); |
| 6452 | assert((i - offset) >= 0); |
| 6453 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6454 | } |
| 6455 | return tuple; |
| 6456 | } |
| 6457 | |
| 6458 | static PyObject * |
| 6459 | consts_dict_keys_inorder(PyObject *dict) |
| 6460 | { |
| 6461 | PyObject *consts, *k, *v; |
| 6462 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6463 | |
| 6464 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6465 | if (consts == NULL) |
| 6466 | return NULL; |
| 6467 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6468 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6469 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6470 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6471 | * the object we want is always second. */ |
| 6472 | if (PyTuple_CheckExact(k)) { |
| 6473 | k = PyTuple_GET_ITEM(k, 1); |
| 6474 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6475 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6476 | assert(i < size); |
| 6477 | assert(i >= 0); |
| 6478 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6479 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6480 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6481 | } |
| 6482 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6483 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6484 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6486 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6487 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6488 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6489 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6490 | if (ste->ste_nested) |
| 6491 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6492 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6494 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6495 | flags |= CO_COROUTINE; |
| 6496 | if (ste->ste_generator && ste->ste_coroutine) |
| 6497 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6498 | if (ste->ste_varargs) |
| 6499 | flags |= CO_VARARGS; |
| 6500 | if (ste->ste_varkeywords) |
| 6501 | flags |= CO_VARKEYWORDS; |
| 6502 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6504 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6505 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6506 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6507 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6508 | ste->ste_coroutine && |
| 6509 | !ste->ste_generator) { |
| 6510 | flags |= CO_COROUTINE; |
| 6511 | } |
| 6512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6513 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6514 | } |
| 6515 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6516 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6517 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6518 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6519 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6520 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6521 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6522 | if (key == NULL) { |
| 6523 | return 0; |
| 6524 | } |
| 6525 | |
| 6526 | // t is borrowed reference |
| 6527 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6528 | Py_DECREF(key); |
| 6529 | if (t == NULL) { |
| 6530 | return 0; |
| 6531 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6532 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6533 | return 1; |
| 6534 | } |
| 6535 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6536 | if (PyTuple_CheckExact(t)) { |
| 6537 | // t is still borrowed reference |
| 6538 | t = PyTuple_GET_ITEM(t, 1); |
| 6539 | } |
| 6540 | |
| 6541 | Py_INCREF(t); |
| 6542 | Py_DECREF(*obj); |
| 6543 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6544 | return 1; |
| 6545 | } |
| 6546 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6547 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6548 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | PyObject *names = NULL; |
| 6552 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6553 | PyObject *name = NULL; |
| 6554 | PyObject *freevars = NULL; |
| 6555 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6556 | Py_ssize_t nlocals; |
| 6557 | int nlocals_int; |
| 6558 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6559 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6562 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6563 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6564 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6565 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6566 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6567 | if (!cellvars) |
| 6568 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6569 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6570 | if (!freevars) |
| 6571 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6572 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6573 | if (!merge_const_one(c, &names) || |
| 6574 | !merge_const_one(c, &varnames) || |
| 6575 | !merge_const_one(c, &cellvars) || |
| 6576 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6577 | { |
| 6578 | goto error; |
| 6579 | } |
| 6580 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6581 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6582 | assert(nlocals < INT_MAX); |
| 6583 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6585 | flags = compute_code_flags(c); |
| 6586 | if (flags < 0) |
| 6587 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6588 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6589 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6590 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6591 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6592 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6593 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6594 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6595 | goto error; |
| 6596 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6597 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6598 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6599 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6600 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6601 | maxdepth = stackdepth(c); |
| 6602 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6603 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6604 | goto error; |
| 6605 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6606 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6607 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6608 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6609 | varnames, freevars, cellvars, c->c_filename, |
| 6610 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6611 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6612 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6613 | Py_XDECREF(names); |
| 6614 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6615 | Py_XDECREF(name); |
| 6616 | Py_XDECREF(freevars); |
| 6617 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6618 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6619 | } |
| 6620 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6621 | |
| 6622 | /* For debugging purposes only */ |
| 6623 | #if 0 |
| 6624 | static void |
| 6625 | dump_instr(const struct instr *i) |
| 6626 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6627 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 6628 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6629 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6631 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6632 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6633 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6634 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6635 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6636 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | static void |
| 6640 | dump_basicblock(const basicblock *b) |
| 6641 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6642 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6643 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6644 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6645 | if (b->b_instr) { |
| 6646 | int i; |
| 6647 | for (i = 0; i < b->b_iused; i++) { |
| 6648 | fprintf(stderr, " [%02d] ", i); |
| 6649 | dump_instr(b->b_instr + i); |
| 6650 | } |
| 6651 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6652 | } |
| 6653 | #endif |
| 6654 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6655 | |
| 6656 | static int |
| 6657 | normalize_basic_block(basicblock *bb); |
| 6658 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6659 | static int |
| 6660 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 6661 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6662 | static int |
| 6663 | ensure_exits_have_lineno(struct compiler *c); |
| 6664 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6665 | static int |
| 6666 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
| 6667 | |
| 6668 | int flags = compute_code_flags(c); |
| 6669 | if (flags < 0) { |
| 6670 | return -1; |
| 6671 | } |
| 6672 | int kind; |
| 6673 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 6674 | if (flags & CO_COROUTINE) { |
| 6675 | kind = 1; |
| 6676 | } |
| 6677 | else if (flags & CO_ASYNC_GENERATOR) { |
| 6678 | kind = 2; |
| 6679 | } |
| 6680 | else { |
| 6681 | kind = 0; |
| 6682 | } |
| 6683 | } |
| 6684 | else { |
| 6685 | return 0; |
| 6686 | } |
| 6687 | if (compiler_next_instr(entryblock) < 0) { |
| 6688 | return -1; |
| 6689 | } |
| 6690 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
| 6691 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
| 6692 | } |
| 6693 | entryblock->b_instr[0].i_opcode = GEN_START; |
| 6694 | entryblock->b_instr[0].i_oparg = kind; |
| 6695 | entryblock->b_instr[0].i_lineno = -1; |
| 6696 | entryblock->b_instr[0].i_target = NULL; |
| 6697 | return 0; |
| 6698 | } |
| 6699 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6700 | static PyCodeObject * |
| 6701 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | basicblock *b, *entryblock; |
| 6704 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6705 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6706 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6707 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6709 | /* Make sure every block that falls off the end returns None. |
| 6710 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6711 | block ends with a jump or return b_next shouldn't set. |
| 6712 | */ |
| 6713 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6714 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6715 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6716 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6717 | ADDOP(c, RETURN_VALUE); |
| 6718 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6719 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6720 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6721 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6722 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | if (ensure_exits_have_lineno(c)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6727 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6730 | nblocks = 0; |
| 6731 | entryblock = NULL; |
| 6732 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6733 | nblocks++; |
| 6734 | entryblock = b; |
| 6735 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6736 | assert(entryblock != NULL); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6737 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6738 | if (insert_generator_prefix(c, entryblock)) { |
| 6739 | goto error; |
| 6740 | } |
| 6741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6742 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6743 | if (!c->u->u_firstlineno) { |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame] | 6744 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6745 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6746 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6747 | c->u->u_firstlineno = 1; |
| 6748 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6750 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6751 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6752 | a.a_entry = entryblock; |
| 6753 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6754 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6755 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6756 | if (consts == NULL) { |
| 6757 | goto error; |
| 6758 | } |
| 6759 | if (optimize_cfg(&a, consts)) { |
| 6760 | goto error; |
| 6761 | } |
| 6762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6763 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6764 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6765 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6766 | /* Emit code. */ |
| 6767 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6768 | for (j = 0; j < b->b_iused; j++) |
| 6769 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6770 | goto error; |
| 6771 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6772 | if (!assemble_line_range(&a)) { |
| 6773 | return 0; |
| 6774 | } |
| 6775 | /* Emit sentinel at end of line number table */ |
| 6776 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6777 | goto error; |
| 6778 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6779 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6780 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6781 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6782 | } |
| 6783 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6784 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6785 | } |
| 6786 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 6787 | goto error; |
| 6788 | } |
| 6789 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 6790 | goto error; |
| 6791 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6792 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6793 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6794 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6795 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6796 | assemble_free(&a); |
| 6797 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6798 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6799 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6800 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6801 | with LOAD_CONST (c1, c2, ... cn). |
| 6802 | The consts table must still be in list form so that the |
| 6803 | new constant (c1, c2, ... cn) can be appended. |
| 6804 | Called with codestr pointing to the first LOAD_CONST. |
| 6805 | */ |
| 6806 | static int |
| 6807 | fold_tuple_on_constants(struct instr *inst, |
| 6808 | int n, PyObject *consts) |
| 6809 | { |
| 6810 | /* Pre-conditions */ |
| 6811 | assert(PyList_CheckExact(consts)); |
| 6812 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6813 | assert(inst[n].i_oparg == n); |
| 6814 | |
| 6815 | for (int i = 0; i < n; i++) { |
| 6816 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6817 | return 0; |
| 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | /* Buildup new tuple of constants */ |
| 6822 | PyObject *newconst = PyTuple_New(n); |
| 6823 | if (newconst == NULL) { |
| 6824 | return -1; |
| 6825 | } |
| 6826 | for (int i = 0; i < n; i++) { |
| 6827 | int arg = inst[i].i_oparg; |
| 6828 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6829 | Py_INCREF(constant); |
| 6830 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6831 | } |
| 6832 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6833 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6834 | Py_DECREF(newconst); |
| 6835 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6836 | return -1; |
| 6837 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6838 | if (PyList_Append(consts, newconst)) { |
| 6839 | Py_DECREF(newconst); |
| 6840 | return -1; |
| 6841 | } |
| 6842 | Py_DECREF(newconst); |
| 6843 | for (int i = 0; i < n; i++) { |
| 6844 | inst[i].i_opcode = NOP; |
| 6845 | } |
| 6846 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6847 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6848 | return 0; |
| 6849 | } |
| 6850 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6851 | |
| 6852 | static int |
| 6853 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 6854 | assert (bb->b_iused > 0); |
| 6855 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 6856 | assert (is_jump(inst)); |
| 6857 | assert (inst->i_target->b_iused > 0); |
| 6858 | struct instr *target = &inst->i_target->b_instr[0]; |
| 6859 | if (inst->i_target == target->i_target) { |
| 6860 | /* Nothing to do */ |
| 6861 | return 0; |
| 6862 | } |
| 6863 | int lineno = target->i_lineno; |
| 6864 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 6865 | return -1; |
| 6866 | } |
| 6867 | assert (bb->b_iused >= 2); |
| 6868 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 6869 | return 0; |
| 6870 | } |
| 6871 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6872 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6873 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6874 | |
| 6875 | /* Optimization */ |
| 6876 | static int |
| 6877 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6878 | { |
| 6879 | assert(PyList_CheckExact(consts)); |
| 6880 | struct instr nop; |
| 6881 | nop.i_opcode = NOP; |
| 6882 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6883 | for (int i = 0; i < bb->b_iused; i++) { |
| 6884 | struct instr *inst = &bb->b_instr[i]; |
| 6885 | int oparg = inst->i_oparg; |
| 6886 | 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] | 6887 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6888 | /* Skip over empty basic blocks. */ |
| 6889 | while (inst->i_target->b_iused == 0) { |
| 6890 | inst->i_target = inst->i_target->b_next; |
| 6891 | } |
| 6892 | target = &inst->i_target->b_instr[0]; |
| 6893 | } |
| 6894 | else { |
| 6895 | target = &nop; |
| 6896 | } |
| 6897 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6898 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6899 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6900 | { |
| 6901 | PyObject* cnt; |
| 6902 | int is_true; |
| 6903 | int jump_if_true; |
| 6904 | switch(nextop) { |
| 6905 | case POP_JUMP_IF_FALSE: |
| 6906 | case POP_JUMP_IF_TRUE: |
| 6907 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6908 | is_true = PyObject_IsTrue(cnt); |
| 6909 | if (is_true == -1) { |
| 6910 | goto error; |
| 6911 | } |
| 6912 | inst->i_opcode = NOP; |
| 6913 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6914 | if (is_true == jump_if_true) { |
| 6915 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6916 | bb->b_nofallthrough = 1; |
| 6917 | } |
| 6918 | else { |
| 6919 | bb->b_instr[i+1].i_opcode = NOP; |
| 6920 | } |
| 6921 | break; |
| 6922 | case JUMP_IF_FALSE_OR_POP: |
| 6923 | case JUMP_IF_TRUE_OR_POP: |
| 6924 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6925 | is_true = PyObject_IsTrue(cnt); |
| 6926 | if (is_true == -1) { |
| 6927 | goto error; |
| 6928 | } |
| 6929 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6930 | if (is_true == jump_if_true) { |
| 6931 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6932 | bb->b_nofallthrough = 1; |
| 6933 | } |
| 6934 | else { |
| 6935 | inst->i_opcode = NOP; |
| 6936 | bb->b_instr[i+1].i_opcode = NOP; |
| 6937 | } |
| 6938 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6939 | } |
| 6940 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6941 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6942 | |
| 6943 | /* Try to fold tuples of constants. |
| 6944 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6945 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6946 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6947 | case BUILD_TUPLE: |
| 6948 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6949 | switch(oparg) { |
| 6950 | case 1: |
| 6951 | inst->i_opcode = NOP; |
| 6952 | bb->b_instr[i+1].i_opcode = NOP; |
| 6953 | break; |
| 6954 | case 2: |
| 6955 | inst->i_opcode = ROT_TWO; |
| 6956 | bb->b_instr[i+1].i_opcode = NOP; |
| 6957 | break; |
| 6958 | case 3: |
| 6959 | inst->i_opcode = ROT_THREE; |
| 6960 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6961 | } |
| 6962 | break; |
| 6963 | } |
| 6964 | if (i >= oparg) { |
| 6965 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6966 | goto error; |
| 6967 | } |
| 6968 | } |
| 6969 | break; |
| 6970 | |
| 6971 | /* Simplify conditional jump to conditional jump where the |
| 6972 | result of the first test implies the success of a similar |
| 6973 | test or the failure of the opposite test. |
| 6974 | Arises in code like: |
| 6975 | "a and b or c" |
| 6976 | "(a and b) and c" |
| 6977 | "(a or b) or c" |
| 6978 | "(a or b) and c" |
| 6979 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6980 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6981 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6982 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6983 | where y+1 is the instruction following the second test. |
| 6984 | */ |
| 6985 | case JUMP_IF_FALSE_OR_POP: |
| 6986 | switch(target->i_opcode) { |
| 6987 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6988 | if (inst->i_lineno == target->i_lineno) { |
| 6989 | *inst = *target; |
| 6990 | i--; |
| 6991 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6992 | break; |
| 6993 | case JUMP_ABSOLUTE: |
| 6994 | case JUMP_FORWARD: |
| 6995 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6996 | if (inst->i_lineno == target->i_lineno && |
| 6997 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6998 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6999 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7000 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7001 | break; |
| 7002 | case JUMP_IF_TRUE_OR_POP: |
| 7003 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7004 | if (inst->i_lineno == target->i_lineno) { |
| 7005 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 7006 | inst->i_target = inst->i_target->b_next; |
| 7007 | --i; |
| 7008 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7009 | break; |
| 7010 | } |
| 7011 | break; |
| 7012 | |
| 7013 | case JUMP_IF_TRUE_OR_POP: |
| 7014 | switch(target->i_opcode) { |
| 7015 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7016 | if (inst->i_lineno == target->i_lineno) { |
| 7017 | *inst = *target; |
| 7018 | i--; |
| 7019 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7020 | break; |
| 7021 | case JUMP_ABSOLUTE: |
| 7022 | case JUMP_FORWARD: |
| 7023 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7024 | if (inst->i_lineno == target->i_lineno && |
| 7025 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7026 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7027 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7028 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7029 | break; |
| 7030 | case JUMP_IF_FALSE_OR_POP: |
| 7031 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7032 | if (inst->i_lineno == target->i_lineno) { |
| 7033 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 7034 | inst->i_target = inst->i_target->b_next; |
| 7035 | --i; |
| 7036 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7037 | break; |
| 7038 | } |
| 7039 | break; |
| 7040 | |
| 7041 | case POP_JUMP_IF_FALSE: |
| 7042 | switch(target->i_opcode) { |
| 7043 | case JUMP_ABSOLUTE: |
| 7044 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7045 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7046 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7047 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7048 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7049 | break; |
| 7050 | } |
| 7051 | break; |
| 7052 | |
| 7053 | case POP_JUMP_IF_TRUE: |
| 7054 | switch(target->i_opcode) { |
| 7055 | case JUMP_ABSOLUTE: |
| 7056 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7057 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7058 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7059 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7060 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7061 | break; |
| 7062 | } |
| 7063 | break; |
| 7064 | |
| 7065 | case JUMP_ABSOLUTE: |
| 7066 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7067 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7068 | switch(target->i_opcode) { |
| 7069 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7070 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7071 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7072 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7073 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7074 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7075 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7076 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7077 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7078 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7079 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7080 | default: |
| 7081 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7082 | basicblock *to_copy = inst->i_target; |
| 7083 | inst->i_opcode = NOP; |
| 7084 | for (i = 0; i < to_copy->b_iused; i++) { |
| 7085 | int index = compiler_next_instr(bb); |
| 7086 | if (index < 0) { |
| 7087 | return -1; |
| 7088 | } |
| 7089 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7090 | } |
| 7091 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7092 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7093 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7094 | } |
| 7095 | } |
| 7096 | return 0; |
| 7097 | error: |
| 7098 | return -1; |
| 7099 | } |
| 7100 | |
| 7101 | |
| 7102 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7103 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7104 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7105 | int dest = 0; |
| 7106 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7107 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7108 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7109 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7110 | if (lineno < 0) { |
| 7111 | continue; |
| 7112 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7113 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7114 | if (prev_lineno == lineno) { |
| 7115 | continue; |
| 7116 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7117 | /* 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] | 7118 | if (src < bb->b_iused - 1) { |
| 7119 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7120 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7121 | bb->b_instr[src+1].i_lineno = lineno; |
| 7122 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7123 | } |
| 7124 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7125 | else { |
| 7126 | basicblock* next = bb->b_next; |
| 7127 | while (next && next->b_iused == 0) { |
| 7128 | next = next->b_next; |
| 7129 | } |
| 7130 | /* or if last instruction in BB and next BB has same line number */ |
| 7131 | if (next) { |
| 7132 | if (lineno == next->b_instr[0].i_lineno) { |
| 7133 | continue; |
| 7134 | } |
| 7135 | } |
| 7136 | } |
| 7137 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7138 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7139 | if (dest != src) { |
| 7140 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7141 | } |
| 7142 | dest++; |
| 7143 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7144 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7145 | assert(dest <= bb->b_iused); |
| 7146 | bb->b_iused = dest; |
| 7147 | } |
| 7148 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7149 | static int |
| 7150 | normalize_basic_block(basicblock *bb) { |
| 7151 | /* Mark blocks as exit and/or nofallthrough. |
| 7152 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7153 | for (int i = 0; i < bb->b_iused; i++) { |
| 7154 | switch(bb->b_instr[i].i_opcode) { |
| 7155 | case RETURN_VALUE: |
| 7156 | case RAISE_VARARGS: |
| 7157 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7158 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7159 | bb->b_nofallthrough = 1; |
| 7160 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7161 | case JUMP_ABSOLUTE: |
| 7162 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7163 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7164 | /* fall through */ |
| 7165 | case POP_JUMP_IF_FALSE: |
| 7166 | case POP_JUMP_IF_TRUE: |
| 7167 | case JUMP_IF_FALSE_OR_POP: |
| 7168 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7169 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7170 | if (i != bb->b_iused-1) { |
| 7171 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7172 | return -1; |
| 7173 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7174 | /* Skip over empty basic blocks. */ |
| 7175 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7176 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7177 | } |
| 7178 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7179 | } |
| 7180 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7181 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7182 | } |
| 7183 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7184 | static int |
| 7185 | mark_reachable(struct assembler *a) { |
| 7186 | basicblock **stack, **sp; |
| 7187 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7188 | if (stack == NULL) { |
| 7189 | return -1; |
| 7190 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7191 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7192 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7193 | while (sp > stack) { |
| 7194 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7195 | if (b->b_next && !b->b_nofallthrough) { |
| 7196 | if (b->b_next->b_predecessors == 0) { |
| 7197 | *sp++ = b->b_next; |
| 7198 | } |
| 7199 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7200 | } |
| 7201 | for (int i = 0; i < b->b_iused; i++) { |
| 7202 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7203 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7204 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7205 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7206 | *sp++ = target; |
| 7207 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7208 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7209 | } |
| 7210 | } |
| 7211 | } |
| 7212 | PyObject_Free(stack); |
| 7213 | return 0; |
| 7214 | } |
| 7215 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7216 | static void |
| 7217 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7218 | /* Eliminate empty blocks */ |
| 7219 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7220 | basicblock *next = b->b_next; |
| 7221 | if (next) { |
| 7222 | while (next->b_iused == 0 && next->b_next) { |
| 7223 | next = next->b_next; |
| 7224 | } |
| 7225 | b->b_next = next; |
| 7226 | } |
| 7227 | } |
| 7228 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7229 | if (b->b_iused == 0) { |
| 7230 | continue; |
| 7231 | } |
| 7232 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7233 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7234 | while (target->b_iused == 0) { |
| 7235 | target = target->b_next; |
| 7236 | } |
| 7237 | b->b_instr[b->b_iused-1].i_target = target; |
| 7238 | } |
| 7239 | } |
| 7240 | } |
| 7241 | |
| 7242 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7243 | /* 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] | 7244 | * then copy the line number. If a successor block has no line number, and only |
| 7245 | * one predecessor, then inherit the line number. |
| 7246 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7247 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7248 | * but has no impact on the generated line number events. |
| 7249 | */ |
| 7250 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7251 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7252 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7253 | if (b->b_iused == 0) { |
| 7254 | continue; |
| 7255 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7256 | int prev_lineno = -1; |
| 7257 | for (int i = 0; i < b->b_iused; i++) { |
| 7258 | if (b->b_instr[i].i_lineno < 0) { |
| 7259 | b->b_instr[i].i_lineno = prev_lineno; |
| 7260 | } |
| 7261 | else { |
| 7262 | prev_lineno = b->b_instr[i].i_lineno; |
| 7263 | } |
| 7264 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7265 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7266 | assert(b->b_next->b_iused); |
| 7267 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7268 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7269 | } |
| 7270 | } |
| 7271 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7272 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7273 | /* Note: Only actual jumps, not exception handlers */ |
| 7274 | case SETUP_ASYNC_WITH: |
| 7275 | case SETUP_WITH: |
| 7276 | case SETUP_FINALLY: |
| 7277 | continue; |
| 7278 | } |
| 7279 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7280 | if (target->b_predecessors == 1) { |
| 7281 | if (target->b_instr[0].i_lineno < 0) { |
| 7282 | target->b_instr[0].i_lineno = prev_lineno; |
| 7283 | } |
| 7284 | } |
| 7285 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7286 | } |
| 7287 | } |
| 7288 | |
| 7289 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7290 | The consts object should still be in list form to allow new constants |
| 7291 | to be appended. |
| 7292 | |
| 7293 | All transformations keep the code size the same or smaller. |
| 7294 | For those that reduce size, the gaps are initially filled with |
| 7295 | NOPs. Later those NOPs are removed. |
| 7296 | */ |
| 7297 | |
| 7298 | static int |
| 7299 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 7300 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7301 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7302 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7303 | return -1; |
| 7304 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7305 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7306 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7307 | } |
| 7308 | if (mark_reachable(a)) { |
| 7309 | return -1; |
| 7310 | } |
| 7311 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7312 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7313 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7314 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7315 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7316 | } |
| 7317 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7318 | basicblock *pred = NULL; |
| 7319 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7320 | int prev_lineno = -1; |
| 7321 | if (pred && pred->b_iused) { |
| 7322 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7323 | } |
| 7324 | clean_basic_block(b, prev_lineno); |
| 7325 | pred = b->b_nofallthrough ? NULL : b; |
| 7326 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7327 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7328 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7329 | block ends with a jump instruction, check if the next non-empty block |
| 7330 | reached through normal flow control is the target of that jump. If it |
| 7331 | is, then the jump instruction is redundant and can be deleted. |
| 7332 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7333 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7334 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7335 | if (b->b_iused > 0) { |
| 7336 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7337 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7338 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7339 | if (b_last_instr->i_target == b->b_next) { |
| 7340 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7341 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7342 | b_last_instr->i_opcode = NOP; |
| 7343 | clean_basic_block(b, -1); |
| 7344 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7345 | } |
| 7346 | } |
| 7347 | } |
| 7348 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7349 | if (maybe_empty_blocks) { |
| 7350 | eliminate_empty_basic_blocks(a->a_entry); |
| 7351 | } |
| 7352 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7353 | return 0; |
| 7354 | } |
| 7355 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7356 | static inline int |
| 7357 | is_exit_without_lineno(basicblock *b) { |
| 7358 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7359 | } |
| 7360 | |
| 7361 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7362 | * after a frame terminates. It would be prohibitively expensive |
| 7363 | * to continuously update the f_lineno field at runtime, |
| 7364 | * so we make sure that all exiting instruction (raises and returns) |
| 7365 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7366 | * We can do this by duplicating the exit blocks without line number |
| 7367 | * so that none have more than one predecessor. We can then safely |
| 7368 | * copy the line number from the sole predecessor block. |
| 7369 | */ |
| 7370 | static int |
| 7371 | ensure_exits_have_lineno(struct compiler *c) |
| 7372 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7373 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7374 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7375 | */ |
| 7376 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7377 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7378 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7379 | /* Note: Only actual jumps, not exception handlers */ |
| 7380 | case SETUP_ASYNC_WITH: |
| 7381 | case SETUP_WITH: |
| 7382 | case SETUP_FINALLY: |
| 7383 | continue; |
| 7384 | } |
| 7385 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7386 | if (is_exit_without_lineno(target)) { |
| 7387 | basicblock *new_target = compiler_copy_block(c, target); |
| 7388 | if (new_target == NULL) { |
| 7389 | return -1; |
| 7390 | } |
| 7391 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7392 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 7393 | } |
| 7394 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7395 | entry = b; |
| 7396 | } |
| 7397 | assert(entry != NULL); |
| 7398 | if (is_exit_without_lineno(entry)) { |
| 7399 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7400 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7401 | /* Eliminate empty blocks */ |
| 7402 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7403 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7404 | b->b_next = b->b_next->b_next; |
| 7405 | } |
| 7406 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7407 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7408 | * fall through, and thus can only have a single predecessor */ |
| 7409 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7410 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7411 | if (is_exit_without_lineno(b->b_next)) { |
| 7412 | assert(b->b_next->b_iused > 0); |
| 7413 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7414 | } |
| 7415 | } |
| 7416 | } |
| 7417 | return 0; |
| 7418 | } |
| 7419 | |
| 7420 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7421 | /* Retained for API compatibility. |
| 7422 | * Optimization is now done in optimize_cfg */ |
| 7423 | |
| 7424 | PyObject * |
| 7425 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7426 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7427 | { |
| 7428 | Py_INCREF(code); |
| 7429 | return code; |
| 7430 | } |