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, |
| 119 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 120 | |
| 121 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | enum fblocktype fb_type; |
| 123 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 124 | /* (optional) type-specific exit or cleanup block */ |
| 125 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 126 | /* (optional) additional information required for unwinding */ |
| 127 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 130 | enum { |
| 131 | COMPILER_SCOPE_MODULE, |
| 132 | COMPILER_SCOPE_CLASS, |
| 133 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 134 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 135 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 136 | COMPILER_SCOPE_COMPREHENSION, |
| 137 | }; |
| 138 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 139 | /* The following items change on entry and exit of code blocks. |
| 140 | They must be saved and restored when returning to a block. |
| 141 | */ |
| 142 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 146 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 147 | int u_scope_type; |
| 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | /* The following fields are dicts that map objects to |
| 150 | the index of them in co_XXX. The index is used as |
| 151 | the argument for opcodes that refer to those collections. |
| 152 | */ |
| 153 | PyObject *u_consts; /* all constants */ |
| 154 | PyObject *u_names; /* all names */ |
| 155 | PyObject *u_varnames; /* local variables */ |
| 156 | PyObject *u_cellvars; /* cell variables */ |
| 157 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 160 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 161 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 162 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 163 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | /* Pointer to the most recently allocated block. By following b_list |
| 165 | members, you can reach all early allocated blocks. */ |
| 166 | basicblock *u_blocks; |
| 167 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | int u_nfblocks; |
| 170 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | int u_firstlineno; /* the first lineno of the block */ |
| 173 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 174 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 178 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 179 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | 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] | 181 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 182 | |
| 183 | Note that we don't track recursion levels during compilation - the |
| 184 | task of detecting and rejecting excessive levels of nesting is |
| 185 | handled by the symbol analysis pass. |
| 186 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | */ |
| 188 | |
| 189 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 190 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | struct symtable *c_st; |
| 192 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 193 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 194 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 195 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | int c_interactive; /* true if in interactive mode */ |
| 197 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 198 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 199 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | struct compiler_unit *u; /* compiler state for current block */ |
| 201 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 202 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 205 | typedef struct { |
| 206 | PyObject *stores; |
| 207 | int allow_irrefutable; |
| 208 | } pattern_context; |
| 209 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 210 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 211 | static void compiler_free(struct compiler *); |
| 212 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 213 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 215 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 216 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 217 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 218 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 219 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 220 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 221 | |
| 222 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 223 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 224 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 225 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 226 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 227 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 228 | static int compiler_subscript(struct compiler *, expr_ty); |
| 229 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 230 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 231 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 232 | 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] | 233 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 234 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 235 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 236 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 237 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 238 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 239 | asdl_expr_seq *args, |
| 240 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 241 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 242 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 243 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 244 | static int compiler_sync_comprehension_generator( |
| 245 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 246 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 247 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 248 | expr_ty elt, expr_ty val, int type); |
| 249 | |
| 250 | static int compiler_async_comprehension_generator( |
| 251 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 252 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 253 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 254 | expr_ty elt, expr_ty val, int type); |
| 255 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 256 | static int compiler_pattern(struct compiler *, expr_ty, pattern_context *); |
| 257 | static int compiler_match(struct compiler *, stmt_ty); |
| 258 | static int compiler_pattern_subpattern(struct compiler *, expr_ty, |
| 259 | pattern_context *); |
| 260 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 261 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 262 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 263 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 264 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 265 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 266 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 267 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | /* Name mangling: __private becomes _classname__private. |
| 270 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 271 | PyObject *result; |
| 272 | size_t nlen, plen, ipriv; |
| 273 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 275 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 276 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | Py_INCREF(ident); |
| 278 | return ident; |
| 279 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 280 | nlen = PyUnicode_GET_LENGTH(ident); |
| 281 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | The only time a name with a dot can occur is when |
| 285 | we are compiling an import statement that has a |
| 286 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | TODO(jhylton): Decide whether we want to support |
| 289 | mangling of the module name, e.g. __M.X. |
| 290 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 291 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 292 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 293 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | Py_INCREF(ident); |
| 295 | return ident; /* Don't mangle __whatever__ */ |
| 296 | } |
| 297 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 298 | ipriv = 0; |
| 299 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 300 | ipriv++; |
| 301 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | Py_INCREF(ident); |
| 303 | return ident; /* Don't mangle if class is just underscores */ |
| 304 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 305 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 307 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 308 | PyErr_SetString(PyExc_OverflowError, |
| 309 | "private identifier too large to be mangled"); |
| 310 | return NULL; |
| 311 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 312 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 313 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 314 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 315 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 316 | |
| 317 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 318 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 320 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 321 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 322 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 323 | Py_DECREF(result); |
| 324 | return NULL; |
| 325 | } |
| 326 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 327 | Py_DECREF(result); |
| 328 | return NULL; |
| 329 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 330 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 331 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 334 | static int |
| 335 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 338 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 339 | c->c_const_cache = PyDict_New(); |
| 340 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | c->c_stack = PyList_New(0); |
| 345 | if (!c->c_stack) { |
| 346 | Py_CLEAR(c->c_const_cache); |
| 347 | return 0; |
| 348 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | PyCodeObject * |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 354 | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 355 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | struct compiler c; |
| 358 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 359 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (!__doc__) { |
| 363 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 364 | if (!__doc__) |
| 365 | return NULL; |
| 366 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 367 | if (!__annotations__) { |
| 368 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 369 | if (!__annotations__) |
| 370 | return NULL; |
| 371 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | if (!compiler_init(&c)) |
| 373 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 374 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | c.c_filename = filename; |
| 376 | c.c_arena = arena; |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 377 | c.c_future = _PyFuture_FromAST(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | if (c.c_future == NULL) |
| 379 | goto finally; |
| 380 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | flags = &local_flags; |
| 382 | } |
| 383 | merged = c.c_future->ff_features | flags->cf_flags; |
| 384 | c.c_future->ff_features = merged; |
| 385 | flags->cf_flags = merged; |
| 386 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 387 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 389 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 390 | _PyASTOptimizeState state; |
| 391 | state.optimize = c.c_optimize; |
| 392 | state.ff_features = merged; |
| 393 | |
| 394 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 395 | goto finally; |
| 396 | } |
| 397 | |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 398 | c.c_st = _PySymtable_Build(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | if (c.c_st == NULL) { |
| 400 | if (!PyErr_Occurred()) |
| 401 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 402 | goto finally; |
| 403 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 406 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 407 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | compiler_free(&c); |
| 409 | assert(co || PyErr_Occurred()); |
| 410 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 413 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 414 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | if (c->c_st) |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 417 | _PySymtable_Free(c->c_st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | if (c->c_future) |
| 419 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 420 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 421 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 425 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 426 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | Py_ssize_t i, n; |
| 429 | PyObject *v, *k; |
| 430 | PyObject *dict = PyDict_New(); |
| 431 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | n = PyList_Size(list); |
| 434 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 435 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | if (!v) { |
| 437 | Py_DECREF(dict); |
| 438 | return NULL; |
| 439 | } |
| 440 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 441 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | Py_DECREF(v); |
| 443 | Py_DECREF(dict); |
| 444 | return NULL; |
| 445 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | Py_DECREF(v); |
| 447 | } |
| 448 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Return new dict containing names from src that match scope(s). |
| 452 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 453 | 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] | 454 | 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] | 455 | values are integers, starting at offset and increasing by one for |
| 456 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 457 | */ |
| 458 | |
| 459 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 460 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 461 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 462 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 464 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | assert(offset >= 0); |
| 467 | if (dest == NULL) |
| 468 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 469 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 470 | /* Sort the keys so that we have a deterministic order on the indexes |
| 471 | saved in the returned dictionary. These indexes are used as indexes |
| 472 | into the free and cell var storage. Therefore if they aren't |
| 473 | deterministic, then the generated bytecode is not deterministic. |
| 474 | */ |
| 475 | sorted_keys = PyDict_Keys(src); |
| 476 | if (sorted_keys == NULL) |
| 477 | return NULL; |
| 478 | if (PyList_Sort(sorted_keys) != 0) { |
| 479 | Py_DECREF(sorted_keys); |
| 480 | return NULL; |
| 481 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 482 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 483 | |
| 484 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | /* XXX this should probably be a macro in symtable.h */ |
| 486 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 487 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 488 | v = PyDict_GetItemWithError(src, k); |
| 489 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | vi = PyLong_AS_LONG(v); |
| 491 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 494 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 496 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | Py_DECREF(dest); |
| 498 | return NULL; |
| 499 | } |
| 500 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 501 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 502 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | Py_DECREF(item); |
| 504 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | return NULL; |
| 506 | } |
| 507 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 510 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 514 | static void |
| 515 | compiler_unit_check(struct compiler_unit *u) |
| 516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | basicblock *block; |
| 518 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 519 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (block->b_instr != NULL) { |
| 521 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 522 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | assert(block->b_ialloc >= block->b_iused); |
| 524 | } |
| 525 | else { |
| 526 | assert (block->b_iused == 0); |
| 527 | assert (block->b_ialloc == 0); |
| 528 | } |
| 529 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static void |
| 533 | compiler_unit_free(struct compiler_unit *u) |
| 534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | compiler_unit_check(u); |
| 538 | b = u->u_blocks; |
| 539 | while (b != NULL) { |
| 540 | if (b->b_instr) |
| 541 | PyObject_Free((void *)b->b_instr); |
| 542 | next = b->b_list; |
| 543 | PyObject_Free((void *)b); |
| 544 | b = next; |
| 545 | } |
| 546 | Py_CLEAR(u->u_ste); |
| 547 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 548 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | Py_CLEAR(u->u_consts); |
| 550 | Py_CLEAR(u->u_names); |
| 551 | Py_CLEAR(u->u_varnames); |
| 552 | Py_CLEAR(u->u_freevars); |
| 553 | Py_CLEAR(u->u_cellvars); |
| 554 | Py_CLEAR(u->u_private); |
| 555 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 559 | compiler_enter_scope(struct compiler *c, identifier name, |
| 560 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 563 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 564 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 565 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | struct compiler_unit)); |
| 567 | if (!u) { |
| 568 | PyErr_NoMemory(); |
| 569 | return 0; |
| 570 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 571 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 573 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | u->u_kwonlyargcount = 0; |
| 575 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 576 | if (!u->u_ste) { |
| 577 | compiler_unit_free(u); |
| 578 | return 0; |
| 579 | } |
| 580 | Py_INCREF(name); |
| 581 | u->u_name = name; |
| 582 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 583 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 584 | if (!u->u_varnames || !u->u_cellvars) { |
| 585 | compiler_unit_free(u); |
| 586 | return 0; |
| 587 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 588 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 589 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 590 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 591 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 592 | int res; |
| 593 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 594 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 595 | name = _PyUnicode_FromId(&PyId___class__); |
| 596 | if (!name) { |
| 597 | compiler_unit_free(u); |
| 598 | return 0; |
| 599 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 600 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 601 | if (res < 0) { |
| 602 | compiler_unit_free(u); |
| 603 | return 0; |
| 604 | } |
| 605 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | 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] | 608 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | if (!u->u_freevars) { |
| 610 | compiler_unit_free(u); |
| 611 | return 0; |
| 612 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | u->u_blocks = NULL; |
| 615 | u->u_nfblocks = 0; |
| 616 | u->u_firstlineno = lineno; |
| 617 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 618 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | u->u_consts = PyDict_New(); |
| 620 | if (!u->u_consts) { |
| 621 | compiler_unit_free(u); |
| 622 | return 0; |
| 623 | } |
| 624 | u->u_names = PyDict_New(); |
| 625 | if (!u->u_names) { |
| 626 | compiler_unit_free(u); |
| 627 | return 0; |
| 628 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | /* Push the old compiler_unit on the stack. */ |
| 633 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 634 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 636 | Py_XDECREF(capsule); |
| 637 | compiler_unit_free(u); |
| 638 | return 0; |
| 639 | } |
| 640 | Py_DECREF(capsule); |
| 641 | u->u_private = c->u->u_private; |
| 642 | Py_XINCREF(u->u_private); |
| 643 | } |
| 644 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 647 | |
| 648 | block = compiler_new_block(c); |
| 649 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 651 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 653 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 654 | if (!compiler_set_qualname(c)) |
| 655 | return 0; |
| 656 | } |
| 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 661 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 662 | compiler_exit_scope(struct compiler *c) |
| 663 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 664 | // Don't call PySequence_DelItem() with an exception raised |
| 665 | PyObject *exc_type, *exc_val, *exc_tb; |
| 666 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | c->c_nestlevel--; |
| 669 | compiler_unit_free(c->u); |
| 670 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 671 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 673 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 674 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | assert(c->u); |
| 676 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 677 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 678 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 679 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 680 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | compiler_unit_check(c->u); |
| 682 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 683 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 685 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 686 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 687 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 690 | static int |
| 691 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 692 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 693 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 694 | _Py_static_string(dot_locals, ".<locals>"); |
| 695 | Py_ssize_t stack_size; |
| 696 | struct compiler_unit *u = c->u; |
| 697 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 698 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 699 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 700 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 701 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 702 | if (stack_size > 1) { |
| 703 | int scope, force_global = 0; |
| 704 | struct compiler_unit *parent; |
| 705 | PyObject *mangled, *capsule; |
| 706 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 707 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 708 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 709 | assert(parent); |
| 710 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 711 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 712 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 713 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 714 | assert(u->u_name); |
| 715 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 716 | if (!mangled) |
| 717 | return 0; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 718 | scope = _PyST_GetScope(parent->u_ste, mangled); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 719 | Py_DECREF(mangled); |
| 720 | assert(scope != GLOBAL_IMPLICIT); |
| 721 | if (scope == GLOBAL_EXPLICIT) |
| 722 | force_global = 1; |
| 723 | } |
| 724 | |
| 725 | if (!force_global) { |
| 726 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 727 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 728 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 729 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 730 | if (dot_locals_str == NULL) |
| 731 | return 0; |
| 732 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 733 | if (base == NULL) |
| 734 | return 0; |
| 735 | } |
| 736 | else { |
| 737 | Py_INCREF(parent->u_qualname); |
| 738 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 739 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 740 | } |
| 741 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 742 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 743 | if (base != NULL) { |
| 744 | dot_str = _PyUnicode_FromId(&dot); |
| 745 | if (dot_str == NULL) { |
| 746 | Py_DECREF(base); |
| 747 | return 0; |
| 748 | } |
| 749 | name = PyUnicode_Concat(base, dot_str); |
| 750 | Py_DECREF(base); |
| 751 | if (name == NULL) |
| 752 | return 0; |
| 753 | PyUnicode_Append(&name, u->u_name); |
| 754 | if (name == NULL) |
| 755 | return 0; |
| 756 | } |
| 757 | else { |
| 758 | Py_INCREF(u->u_name); |
| 759 | name = u->u_name; |
| 760 | } |
| 761 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 762 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 763 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 766 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | /* Allocate a new block and return a pointer to it. |
| 768 | Returns NULL on error. |
| 769 | */ |
| 770 | |
| 771 | static basicblock * |
| 772 | compiler_new_block(struct compiler *c) |
| 773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | basicblock *b; |
| 775 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 778 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | if (b == NULL) { |
| 780 | PyErr_NoMemory(); |
| 781 | return NULL; |
| 782 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | /* Extend the singly linked list of blocks with new block. */ |
| 784 | b->b_list = u->u_blocks; |
| 785 | u->u_blocks = b; |
| 786 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | compiler_next_block(struct compiler *c) |
| 791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | basicblock *block = compiler_new_block(c); |
| 793 | if (block == NULL) |
| 794 | return NULL; |
| 795 | c->u->u_curblock->b_next = block; |
| 796 | c->u->u_curblock = block; |
| 797 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static basicblock * |
| 801 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | assert(block != NULL); |
| 804 | c->u->u_curblock->b_next = block; |
| 805 | c->u->u_curblock = block; |
| 806 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 809 | static basicblock * |
| 810 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 811 | { |
| 812 | /* Cannot copy a block if it has a fallthrough, since |
| 813 | * a block can only have one fallthrough predecessor. |
| 814 | */ |
| 815 | assert(block->b_nofallthrough); |
| 816 | basicblock *result = compiler_next_block(c); |
| 817 | if (result == NULL) { |
| 818 | return NULL; |
| 819 | } |
| 820 | for (int i = 0; i < block->b_iused; i++) { |
| 821 | int n = compiler_next_instr(result); |
| 822 | if (n < 0) { |
| 823 | return NULL; |
| 824 | } |
| 825 | result->b_instr[n] = block->b_instr[i]; |
| 826 | } |
| 827 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 828 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 829 | return result; |
| 830 | } |
| 831 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 832 | /* Returns the offset of the next instruction in the current block's |
| 833 | b_instr array. Resizes the b_instr as necessary. |
| 834 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 835 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | |
| 837 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 838 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 839 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | assert(b != NULL); |
| 841 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 842 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 843 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | if (b->b_instr == NULL) { |
| 845 | PyErr_NoMemory(); |
| 846 | return -1; |
| 847 | } |
| 848 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | } |
| 850 | else if (b->b_iused == b->b_ialloc) { |
| 851 | struct instr *tmp; |
| 852 | size_t oldsize, newsize; |
| 853 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 854 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 855 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 856 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | PyErr_NoMemory(); |
| 858 | return -1; |
| 859 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | if (newsize == 0) { |
| 862 | PyErr_NoMemory(); |
| 863 | return -1; |
| 864 | } |
| 865 | b->b_ialloc <<= 1; |
| 866 | tmp = (struct instr *)PyObject_Realloc( |
| 867 | (void *)b->b_instr, newsize); |
| 868 | if (tmp == NULL) { |
| 869 | PyErr_NoMemory(); |
| 870 | return -1; |
| 871 | } |
| 872 | b->b_instr = tmp; |
| 873 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 874 | } |
| 875 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 878 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 879 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 880 | The line number is reset in the following cases: |
| 881 | - when entering a new scope |
| 882 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 883 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 884 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 885 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 886 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 887 | #define SET_LOC(c, x) \ |
| 888 | (c)->u->u_lineno = (x)->lineno; \ |
| 889 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 890 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 891 | /* Return the stack effect of opcode with argument oparg. |
| 892 | |
| 893 | Some opcodes have different stack effect when jump to the target and |
| 894 | when not jump. The 'jump' parameter specifies the case: |
| 895 | |
| 896 | * 0 -- when not jump |
| 897 | * 1 -- when jump |
| 898 | * -1 -- maximal |
| 899 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 900 | static int |
| 901 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 904 | case NOP: |
| 905 | case EXTENDED_ARG: |
| 906 | return 0; |
| 907 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 908 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | case POP_TOP: |
| 910 | return -1; |
| 911 | case ROT_TWO: |
| 912 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 913 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | return 0; |
| 915 | case DUP_TOP: |
| 916 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 917 | case DUP_TOP_TWO: |
| 918 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 919 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 920 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | case UNARY_POSITIVE: |
| 922 | case UNARY_NEGATIVE: |
| 923 | case UNARY_NOT: |
| 924 | case UNARY_INVERT: |
| 925 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | case SET_ADD: |
| 928 | case LIST_APPEND: |
| 929 | return -1; |
| 930 | case MAP_ADD: |
| 931 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 932 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 933 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | case BINARY_POWER: |
| 935 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 936 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | case BINARY_MODULO: |
| 938 | case BINARY_ADD: |
| 939 | case BINARY_SUBTRACT: |
| 940 | case BINARY_SUBSCR: |
| 941 | case BINARY_FLOOR_DIVIDE: |
| 942 | case BINARY_TRUE_DIVIDE: |
| 943 | return -1; |
| 944 | case INPLACE_FLOOR_DIVIDE: |
| 945 | case INPLACE_TRUE_DIVIDE: |
| 946 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | case INPLACE_ADD: |
| 949 | case INPLACE_SUBTRACT: |
| 950 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 951 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case INPLACE_MODULO: |
| 953 | return -1; |
| 954 | case STORE_SUBSCR: |
| 955 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | case DELETE_SUBSCR: |
| 957 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | case BINARY_LSHIFT: |
| 960 | case BINARY_RSHIFT: |
| 961 | case BINARY_AND: |
| 962 | case BINARY_XOR: |
| 963 | case BINARY_OR: |
| 964 | return -1; |
| 965 | case INPLACE_POWER: |
| 966 | return -1; |
| 967 | case GET_ITER: |
| 968 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | case PRINT_EXPR: |
| 971 | return -1; |
| 972 | case LOAD_BUILD_CLASS: |
| 973 | return 1; |
| 974 | case INPLACE_LSHIFT: |
| 975 | case INPLACE_RSHIFT: |
| 976 | case INPLACE_AND: |
| 977 | case INPLACE_XOR: |
| 978 | case INPLACE_OR: |
| 979 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 982 | /* 1 in the normal flow. |
| 983 | * Restore the stack position and push 6 values before jumping to |
| 984 | * the handler if an exception be raised. */ |
| 985 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | case RETURN_VALUE: |
| 987 | return -1; |
| 988 | case IMPORT_STAR: |
| 989 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 990 | case SETUP_ANNOTATIONS: |
| 991 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | case YIELD_VALUE: |
| 993 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 994 | case YIELD_FROM: |
| 995 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | case POP_BLOCK: |
| 997 | return 0; |
| 998 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 999 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case STORE_NAME: |
| 1002 | return -1; |
| 1003 | case DELETE_NAME: |
| 1004 | return 0; |
| 1005 | case UNPACK_SEQUENCE: |
| 1006 | return oparg-1; |
| 1007 | case UNPACK_EX: |
| 1008 | return (oparg&0xFF) + (oparg>>8); |
| 1009 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1010 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1011 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | case STORE_ATTR: |
| 1014 | return -2; |
| 1015 | case DELETE_ATTR: |
| 1016 | return -1; |
| 1017 | case STORE_GLOBAL: |
| 1018 | return -1; |
| 1019 | case DELETE_GLOBAL: |
| 1020 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | case LOAD_CONST: |
| 1022 | return 1; |
| 1023 | case LOAD_NAME: |
| 1024 | return 1; |
| 1025 | case BUILD_TUPLE: |
| 1026 | case BUILD_LIST: |
| 1027 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1028 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | return 1-oparg; |
| 1030 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1031 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1032 | case BUILD_CONST_KEY_MAP: |
| 1033 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | case LOAD_ATTR: |
| 1035 | return 0; |
| 1036 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1037 | case IS_OP: |
| 1038 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1040 | case JUMP_IF_NOT_EXC_MATCH: |
| 1041 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | case IMPORT_NAME: |
| 1043 | return -1; |
| 1044 | case IMPORT_FROM: |
| 1045 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1046 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1047 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case JUMP_ABSOLUTE: |
| 1050 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1051 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1052 | case JUMP_IF_TRUE_OR_POP: |
| 1053 | case JUMP_IF_FALSE_OR_POP: |
| 1054 | return jump ? 0 : -1; |
| 1055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | case POP_JUMP_IF_FALSE: |
| 1057 | case POP_JUMP_IF_TRUE: |
| 1058 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | case LOAD_GLOBAL: |
| 1061 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1062 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1063 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1065 | /* 0 in the normal flow. |
| 1066 | * Restore the stack position and push 6 values before jumping to |
| 1067 | * the handler if an exception be raised. */ |
| 1068 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1069 | case RERAISE: |
| 1070 | return -3; |
| 1071 | |
| 1072 | case WITH_EXCEPT_START: |
| 1073 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case LOAD_FAST: |
| 1076 | return 1; |
| 1077 | case STORE_FAST: |
| 1078 | return -1; |
| 1079 | case DELETE_FAST: |
| 1080 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | case RAISE_VARARGS: |
| 1083 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1087 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1088 | case CALL_METHOD: |
| 1089 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1091 | return -oparg-1; |
| 1092 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1093 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1094 | case MAKE_FUNCTION: |
| 1095 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1096 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | case BUILD_SLICE: |
| 1098 | if (oparg == 3) |
| 1099 | return -2; |
| 1100 | else |
| 1101 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1102 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1103 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | case LOAD_CLOSURE: |
| 1105 | return 1; |
| 1106 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1107 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | return 1; |
| 1109 | case STORE_DEREF: |
| 1110 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1111 | case DELETE_DEREF: |
| 1112 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1113 | |
| 1114 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1115 | case GET_AWAITABLE: |
| 1116 | return 0; |
| 1117 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1118 | /* 0 in the normal flow. |
| 1119 | * Restore the stack position to the position before the result |
| 1120 | * of __aenter__ and push 6 values before jumping to the handler |
| 1121 | * if an exception be raised. */ |
| 1122 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1123 | case BEFORE_ASYNC_WITH: |
| 1124 | return 1; |
| 1125 | case GET_AITER: |
| 1126 | return 0; |
| 1127 | case GET_ANEXT: |
| 1128 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1129 | case GET_YIELD_FROM_ITER: |
| 1130 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1131 | case END_ASYNC_FOR: |
| 1132 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1133 | case FORMAT_VALUE: |
| 1134 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1135 | else 1->1. */ |
| 1136 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1137 | case LOAD_METHOD: |
| 1138 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1139 | case LOAD_ASSERTION_ERROR: |
| 1140 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1141 | case LIST_TO_TUPLE: |
| 1142 | return 0; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 1143 | case GEN_START: |
| 1144 | return -1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1145 | case LIST_EXTEND: |
| 1146 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1147 | case DICT_MERGE: |
| 1148 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1149 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1150 | case COPY_DICT_WITHOUT_KEYS: |
| 1151 | return 0; |
| 1152 | case MATCH_CLASS: |
| 1153 | return -1; |
| 1154 | case GET_LEN: |
| 1155 | case MATCH_MAPPING: |
| 1156 | case MATCH_SEQUENCE: |
| 1157 | return 1; |
| 1158 | case MATCH_KEYS: |
| 1159 | return 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1161 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1163 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1166 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1167 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1168 | { |
| 1169 | return stack_effect(opcode, oparg, jump); |
| 1170 | } |
| 1171 | |
| 1172 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1173 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1174 | { |
| 1175 | return stack_effect(opcode, oparg, -1); |
| 1176 | } |
| 1177 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | /* Add an opcode with no argument. |
| 1179 | Returns 0 on failure, 1 on success. |
| 1180 | */ |
| 1181 | |
| 1182 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1183 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | basicblock *b; |
| 1186 | struct instr *i; |
| 1187 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1188 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1189 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | if (off < 0) |
| 1191 | return 0; |
| 1192 | b = c->u->u_curblock; |
| 1193 | i = &b->b_instr[off]; |
| 1194 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1195 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | if (opcode == RETURN_VALUE) |
| 1197 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1198 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1202 | static int |
| 1203 | compiler_addop(struct compiler *c, int opcode) |
| 1204 | { |
| 1205 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1206 | } |
| 1207 | |
| 1208 | static int |
| 1209 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1210 | { |
| 1211 | return compiler_addop_line(c, opcode, -1); |
| 1212 | } |
| 1213 | |
| 1214 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1215 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1216 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1217 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1218 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1220 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1221 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1223 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1225 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1226 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1227 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | return -1; |
| 1230 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1231 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | Py_DECREF(v); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | Py_DECREF(v); |
| 1236 | } |
| 1237 | else |
| 1238 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1239 | return arg; |
| 1240 | } |
| 1241 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1242 | // Merge const *o* recursively and return constant key object. |
| 1243 | static PyObject* |
| 1244 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1245 | { |
| 1246 | // None and Ellipsis are singleton, and key is the singleton. |
| 1247 | // No need to merge object and key. |
| 1248 | if (o == Py_None || o == Py_Ellipsis) { |
| 1249 | Py_INCREF(o); |
| 1250 | return o; |
| 1251 | } |
| 1252 | |
| 1253 | PyObject *key = _PyCode_ConstantKey(o); |
| 1254 | if (key == NULL) { |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | |
| 1258 | // t is borrowed reference |
| 1259 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1260 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1261 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1262 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1263 | Py_DECREF(key); |
| 1264 | return t; |
| 1265 | } |
| 1266 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1267 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1268 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1269 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1270 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1271 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1272 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1273 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1274 | PyObject *u = merge_consts_recursive(c, item); |
| 1275 | if (u == NULL) { |
| 1276 | Py_DECREF(key); |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | |
| 1280 | // See _PyCode_ConstantKey() |
| 1281 | PyObject *v; // borrowed |
| 1282 | if (PyTuple_CheckExact(u)) { |
| 1283 | v = PyTuple_GET_ITEM(u, 1); |
| 1284 | } |
| 1285 | else { |
| 1286 | v = u; |
| 1287 | } |
| 1288 | if (v != item) { |
| 1289 | Py_INCREF(v); |
| 1290 | PyTuple_SET_ITEM(o, i, v); |
| 1291 | Py_DECREF(item); |
| 1292 | } |
| 1293 | |
| 1294 | Py_DECREF(u); |
| 1295 | } |
| 1296 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1297 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1298 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1299 | // constant keys. |
| 1300 | // See _PyCode_ConstantKey() for detail. |
| 1301 | assert(PyTuple_CheckExact(key)); |
| 1302 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1303 | |
| 1304 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1305 | if (len == 0) { // empty frozenset should not be re-created. |
| 1306 | return key; |
| 1307 | } |
| 1308 | PyObject *tuple = PyTuple_New(len); |
| 1309 | if (tuple == NULL) { |
| 1310 | Py_DECREF(key); |
| 1311 | return NULL; |
| 1312 | } |
| 1313 | Py_ssize_t i = 0, pos = 0; |
| 1314 | PyObject *item; |
| 1315 | Py_hash_t hash; |
| 1316 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1317 | PyObject *k = merge_consts_recursive(c, item); |
| 1318 | if (k == NULL) { |
| 1319 | Py_DECREF(tuple); |
| 1320 | Py_DECREF(key); |
| 1321 | return NULL; |
| 1322 | } |
| 1323 | PyObject *u; |
| 1324 | if (PyTuple_CheckExact(k)) { |
| 1325 | u = PyTuple_GET_ITEM(k, 1); |
| 1326 | Py_INCREF(u); |
| 1327 | Py_DECREF(k); |
| 1328 | } |
| 1329 | else { |
| 1330 | u = k; |
| 1331 | } |
| 1332 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1333 | i++; |
| 1334 | } |
| 1335 | |
| 1336 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1337 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1338 | PyObject *new = PyFrozenSet_New(tuple); |
| 1339 | Py_DECREF(tuple); |
| 1340 | if (new == NULL) { |
| 1341 | Py_DECREF(key); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1345 | Py_DECREF(o); |
| 1346 | PyTuple_SET_ITEM(key, 1, new); |
| 1347 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1348 | |
| 1349 | return key; |
| 1350 | } |
| 1351 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1352 | static Py_ssize_t |
| 1353 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1354 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1355 | PyObject *key = merge_consts_recursive(c, o); |
| 1356 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1357 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1358 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1359 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1360 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1361 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1366 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1367 | { |
| 1368 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1369 | if (arg < 0) |
| 1370 | return 0; |
| 1371 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1372 | } |
| 1373 | |
| 1374 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1375 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1377 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1378 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1379 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1380 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1381 | return compiler_addop_i(c, opcode, arg); |
| 1382 | } |
| 1383 | |
| 1384 | static int |
| 1385 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1387 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1388 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1389 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1390 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1391 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1392 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1393 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1394 | Py_DECREF(mangled); |
| 1395 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1396 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1397 | return compiler_addop_i(c, opcode, arg); |
| 1398 | } |
| 1399 | |
| 1400 | /* Add an opcode with an integer argument. |
| 1401 | Returns 0 on failure, 1 on success. |
| 1402 | */ |
| 1403 | |
| 1404 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1405 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | struct instr *i; |
| 1408 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1409 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1410 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1411 | it in the C code (like Python/ceval.c). |
| 1412 | |
| 1413 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1414 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1415 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1416 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1417 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1418 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1419 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1420 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | if (off < 0) |
| 1422 | return 0; |
| 1423 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1424 | i->i_opcode = opcode; |
| 1425 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1426 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1430 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1431 | { |
| 1432 | assert(HAS_ARG(opcode)); |
| 1433 | assert(b != NULL); |
| 1434 | assert(target != NULL); |
| 1435 | |
| 1436 | int off = compiler_next_instr(b); |
| 1437 | struct instr *i = &b->b_instr[off]; |
| 1438 | if (off < 0) { |
| 1439 | return 0; |
| 1440 | } |
| 1441 | i->i_opcode = opcode; |
| 1442 | i->i_target = target; |
| 1443 | i->i_lineno = lineno; |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1447 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1448 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1449 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1450 | 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] | 1451 | } |
| 1452 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1453 | static int |
| 1454 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1455 | { |
| 1456 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1457 | } |
| 1458 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1459 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1460 | to the new block. |
| 1461 | |
| 1462 | The returns inside this macro make it impossible to decref objects |
| 1463 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1464 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1465 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | if (compiler_next_block((C)) == NULL) \ |
| 1467 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | if (!compiler_addop((C), (OP))) \ |
| 1472 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1475 | #define ADDOP_NOLINE(C, OP) { \ |
| 1476 | if (!compiler_addop_noline((C), (OP))) \ |
| 1477 | return 0; \ |
| 1478 | } |
| 1479 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1480 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | if (!compiler_addop((C), (OP))) { \ |
| 1482 | compiler_exit_scope(c); \ |
| 1483 | return 0; \ |
| 1484 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1487 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1488 | if (!compiler_addop_load_const((C), (O))) \ |
| 1489 | return 0; \ |
| 1490 | } |
| 1491 | |
| 1492 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1493 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1494 | PyObject *__new_const = (O); \ |
| 1495 | if (__new_const == NULL) { \ |
| 1496 | return 0; \ |
| 1497 | } \ |
| 1498 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1499 | Py_DECREF(__new_const); \ |
| 1500 | return 0; \ |
| 1501 | } \ |
| 1502 | Py_DECREF(__new_const); \ |
| 1503 | } |
| 1504 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1505 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1507 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1510 | /* Same as ADDOP_O, but steals a reference. */ |
| 1511 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1512 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1513 | Py_DECREF((O)); \ |
| 1514 | return 0; \ |
| 1515 | } \ |
| 1516 | Py_DECREF((O)); \ |
| 1517 | } |
| 1518 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1519 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1521 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1526 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1529 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1530 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1534 | /* Add a jump with no line number. |
| 1535 | * Used for artificial jumps that have no corresponding |
| 1536 | * token in the source code. */ |
| 1537 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1538 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1539 | return 0; \ |
| 1540 | } |
| 1541 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1542 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1543 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1544 | return 0; \ |
| 1545 | } |
| 1546 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1547 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1548 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1549 | */ |
| 1550 | |
| 1551 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1553 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1556 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1558 | compiler_exit_scope(c); \ |
| 1559 | return 0; \ |
| 1560 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1563 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1565 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1570 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1572 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1573 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1574 | return 0; \ |
| 1575 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1578 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1580 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1582 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1583 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1584 | compiler_exit_scope(c); \ |
| 1585 | return 0; \ |
| 1586 | } \ |
| 1587 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1590 | #define RETURN_IF_FALSE(X) \ |
| 1591 | if (!(X)) { \ |
| 1592 | return 0; \ |
| 1593 | } |
| 1594 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1595 | /* Search if variable annotations are present statically in a block. */ |
| 1596 | |
| 1597 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1598 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1599 | { |
| 1600 | int i, j, res = 0; |
| 1601 | stmt_ty st; |
| 1602 | |
| 1603 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1604 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1605 | switch (st->kind) { |
| 1606 | case AnnAssign_kind: |
| 1607 | return 1; |
| 1608 | case For_kind: |
| 1609 | res = find_ann(st->v.For.body) || |
| 1610 | find_ann(st->v.For.orelse); |
| 1611 | break; |
| 1612 | case AsyncFor_kind: |
| 1613 | res = find_ann(st->v.AsyncFor.body) || |
| 1614 | find_ann(st->v.AsyncFor.orelse); |
| 1615 | break; |
| 1616 | case While_kind: |
| 1617 | res = find_ann(st->v.While.body) || |
| 1618 | find_ann(st->v.While.orelse); |
| 1619 | break; |
| 1620 | case If_kind: |
| 1621 | res = find_ann(st->v.If.body) || |
| 1622 | find_ann(st->v.If.orelse); |
| 1623 | break; |
| 1624 | case With_kind: |
| 1625 | res = find_ann(st->v.With.body); |
| 1626 | break; |
| 1627 | case AsyncWith_kind: |
| 1628 | res = find_ann(st->v.AsyncWith.body); |
| 1629 | break; |
| 1630 | case Try_kind: |
| 1631 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1632 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1633 | st->v.Try.handlers, j); |
| 1634 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1635 | return 1; |
| 1636 | } |
| 1637 | } |
| 1638 | res = find_ann(st->v.Try.body) || |
| 1639 | find_ann(st->v.Try.finalbody) || |
| 1640 | find_ann(st->v.Try.orelse); |
| 1641 | break; |
| 1642 | default: |
| 1643 | res = 0; |
| 1644 | } |
| 1645 | if (res) { |
| 1646 | break; |
| 1647 | } |
| 1648 | } |
| 1649 | return res; |
| 1650 | } |
| 1651 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1652 | /* |
| 1653 | * Frame block handling functions |
| 1654 | */ |
| 1655 | |
| 1656 | static int |
| 1657 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1658 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1659 | { |
| 1660 | struct fblockinfo *f; |
| 1661 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1662 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1663 | } |
| 1664 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1665 | f->fb_type = t; |
| 1666 | f->fb_block = b; |
| 1667 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1668 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1669 | return 1; |
| 1670 | } |
| 1671 | |
| 1672 | static void |
| 1673 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1674 | { |
| 1675 | struct compiler_unit *u = c->u; |
| 1676 | assert(u->u_nfblocks > 0); |
| 1677 | u->u_nfblocks--; |
| 1678 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1679 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1680 | } |
| 1681 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1682 | static int |
| 1683 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1684 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1685 | ADDOP(c, DUP_TOP); |
| 1686 | ADDOP(c, DUP_TOP); |
| 1687 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1688 | return 1; |
| 1689 | } |
| 1690 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1691 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1692 | * popping the blocks will be restored afterwards, unless another |
| 1693 | * return, break or continue is found. In which case, the TOS will |
| 1694 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1695 | */ |
| 1696 | static int |
| 1697 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1698 | int preserve_tos) |
| 1699 | { |
| 1700 | switch (info->fb_type) { |
| 1701 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1702 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1703 | return 1; |
| 1704 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1705 | case FOR_LOOP: |
| 1706 | /* Pop the iterator */ |
| 1707 | if (preserve_tos) { |
| 1708 | ADDOP(c, ROT_TWO); |
| 1709 | } |
| 1710 | ADDOP(c, POP_TOP); |
| 1711 | return 1; |
| 1712 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1713 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1714 | ADDOP(c, POP_BLOCK); |
| 1715 | return 1; |
| 1716 | |
| 1717 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1718 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1719 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1720 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1721 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1722 | return 0; |
| 1723 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1724 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1725 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1726 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1727 | if (preserve_tos) { |
| 1728 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1729 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1730 | /* The finally block should appear to execute after the |
| 1731 | * statement causing the unwinding, so make the unwinding |
| 1732 | * instruction artificial */ |
| 1733 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1734 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1735 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1736 | case FINALLY_END: |
| 1737 | if (preserve_tos) { |
| 1738 | ADDOP(c, ROT_FOUR); |
| 1739 | } |
| 1740 | ADDOP(c, POP_TOP); |
| 1741 | ADDOP(c, POP_TOP); |
| 1742 | ADDOP(c, POP_TOP); |
| 1743 | if (preserve_tos) { |
| 1744 | ADDOP(c, ROT_FOUR); |
| 1745 | } |
| 1746 | ADDOP(c, POP_EXCEPT); |
| 1747 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1748 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1749 | case WITH: |
| 1750 | case ASYNC_WITH: |
| 1751 | ADDOP(c, POP_BLOCK); |
| 1752 | if (preserve_tos) { |
| 1753 | ADDOP(c, ROT_TWO); |
| 1754 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1755 | if(!compiler_call_exit_with_nones(c)) { |
| 1756 | return 0; |
| 1757 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1758 | if (info->fb_type == ASYNC_WITH) { |
| 1759 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1760 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1761 | ADDOP(c, YIELD_FROM); |
| 1762 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1763 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1764 | return 1; |
| 1765 | |
| 1766 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1767 | if (info->fb_datum) { |
| 1768 | ADDOP(c, POP_BLOCK); |
| 1769 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1770 | if (preserve_tos) { |
| 1771 | ADDOP(c, ROT_FOUR); |
| 1772 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1773 | ADDOP(c, POP_EXCEPT); |
| 1774 | if (info->fb_datum) { |
| 1775 | ADDOP_LOAD_CONST(c, Py_None); |
| 1776 | compiler_nameop(c, info->fb_datum, Store); |
| 1777 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1778 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1779 | return 1; |
| 1780 | |
| 1781 | case POP_VALUE: |
| 1782 | if (preserve_tos) { |
| 1783 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1784 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1785 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1786 | return 1; |
| 1787 | } |
| 1788 | Py_UNREACHABLE(); |
| 1789 | } |
| 1790 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1791 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1792 | static int |
| 1793 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1794 | if (c->u->u_nfblocks == 0) { |
| 1795 | return 1; |
| 1796 | } |
| 1797 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1798 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1799 | *loop = top; |
| 1800 | return 1; |
| 1801 | } |
| 1802 | struct fblockinfo copy = *top; |
| 1803 | c->u->u_nfblocks--; |
| 1804 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1805 | return 0; |
| 1806 | } |
| 1807 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1808 | return 0; |
| 1809 | } |
| 1810 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1811 | c->u->u_nfblocks++; |
| 1812 | return 1; |
| 1813 | } |
| 1814 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1815 | /* Compile a sequence of statements, checking for a docstring |
| 1816 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1817 | |
| 1818 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1819 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1820 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1821 | int i = 0; |
| 1822 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1823 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1824 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1825 | /* Set current line number to the line number of first statement. |
| 1826 | This way line number for SETUP_ANNOTATIONS will always |
| 1827 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1828 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1829 | 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] | 1830 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1831 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1832 | } |
| 1833 | /* Every annotated class and module should have __annotations__. */ |
| 1834 | if (find_ann(stmts)) { |
| 1835 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1836 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1837 | if (!asdl_seq_LEN(stmts)) |
| 1838 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1839 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1840 | if (c->c_optimize < 2) { |
| 1841 | docstring = _PyAST_GetDocString(stmts); |
| 1842 | if (docstring) { |
| 1843 | i = 1; |
| 1844 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1845 | assert(st->kind == Expr_kind); |
| 1846 | VISIT(c, expr, st->v.Expr.value); |
| 1847 | if (!compiler_nameop(c, __doc__, Store)) |
| 1848 | return 0; |
| 1849 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1851 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1852 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | static PyCodeObject * |
| 1857 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | PyCodeObject *co; |
| 1860 | int addNone = 1; |
| 1861 | static PyObject *module; |
| 1862 | if (!module) { |
| 1863 | module = PyUnicode_InternFromString("<module>"); |
| 1864 | if (!module) |
| 1865 | return NULL; |
| 1866 | } |
| 1867 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1868 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | return NULL; |
| 1870 | switch (mod->kind) { |
| 1871 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1872 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | compiler_exit_scope(c); |
| 1874 | return 0; |
| 1875 | } |
| 1876 | break; |
| 1877 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1878 | if (find_ann(mod->v.Interactive.body)) { |
| 1879 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1880 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1882 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | break; |
| 1884 | case Expression_kind: |
| 1885 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1886 | addNone = 0; |
| 1887 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | default: |
| 1889 | PyErr_Format(PyExc_SystemError, |
| 1890 | "module kind %d should not be possible", |
| 1891 | mod->kind); |
| 1892 | return 0; |
| 1893 | } |
| 1894 | co = assemble(c, addNone); |
| 1895 | compiler_exit_scope(c); |
| 1896 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1899 | /* The test for LOCAL must come before the test for FREE in order to |
| 1900 | handle classes where name is both local and free. The local var is |
| 1901 | 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] | 1902 | */ |
| 1903 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1904 | static int |
| 1905 | get_ref_type(struct compiler *c, PyObject *name) |
| 1906 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1907 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1908 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1909 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1910 | return CELL; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1911 | scope = _PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1913 | PyErr_Format(PyExc_SystemError, |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 1914 | "_PyST_GetScope(name=%R) failed: " |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1915 | "unknown scope in unit %S (%R); " |
| 1916 | "symbols: %R; locals: %R; globals: %R", |
| 1917 | name, |
| 1918 | c->u->u_name, c->u->u_ste->ste_id, |
| 1919 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1920 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1921 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | static int |
| 1926 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1927 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1928 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1929 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1931 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1932 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1936 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1937 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1938 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1939 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1940 | if (qualname == NULL) |
| 1941 | qualname = co->co_name; |
| 1942 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1943 | if (free) { |
| 1944 | for (i = 0; i < free; ++i) { |
| 1945 | /* Bypass com_addop_varname because it will generate |
| 1946 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1947 | */ |
| 1948 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1949 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1950 | /* Special case: If a class contains a method with a |
| 1951 | free variable that has the same name as a method, |
| 1952 | the name will be considered free *and* local in the |
| 1953 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1954 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1955 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1956 | int reftype = get_ref_type(c, name); |
| 1957 | if (reftype == -1) { |
| 1958 | return 0; |
| 1959 | } |
| 1960 | int arg; |
| 1961 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1962 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1963 | } |
| 1964 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1965 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1966 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1967 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1968 | PyErr_Format(PyExc_SystemError, |
| 1969 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 1970 | "freevars of code %S: %R", |
| 1971 | name, |
| 1972 | reftype, |
| 1973 | c->u->u_name, |
| 1974 | co->co_name, |
| 1975 | co->co_freevars); |
| 1976 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1977 | } |
| 1978 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1980 | flags |= 0x08; |
| 1981 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1983 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1984 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1985 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1990 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1991 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | int i; |
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 | if (!decos) |
| 1995 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1998 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1999 | } |
| 2000 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2004 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2005 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2006 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2007 | /* Push a dict of keyword-only default values. |
| 2008 | |
| 2009 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2010 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2011 | int i; |
| 2012 | PyObject *keys = NULL; |
| 2013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2014 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2015 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2016 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2017 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2018 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2019 | if (!mangled) { |
| 2020 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2022 | if (keys == NULL) { |
| 2023 | keys = PyList_New(1); |
| 2024 | if (keys == NULL) { |
| 2025 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2026 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2027 | } |
| 2028 | PyList_SET_ITEM(keys, 0, mangled); |
| 2029 | } |
| 2030 | else { |
| 2031 | int res = PyList_Append(keys, mangled); |
| 2032 | Py_DECREF(mangled); |
| 2033 | if (res == -1) { |
| 2034 | goto error; |
| 2035 | } |
| 2036 | } |
| 2037 | if (!compiler_visit_expr(c, default_)) { |
| 2038 | goto error; |
| 2039 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | } |
| 2041 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2042 | if (keys != NULL) { |
| 2043 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2044 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2045 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2046 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2047 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2048 | assert(default_count > 0); |
| 2049 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2050 | } |
| 2051 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2052 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | error: |
| 2056 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2057 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2061 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2062 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2063 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2064 | return 1; |
| 2065 | } |
| 2066 | |
| 2067 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2068 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2069 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2072 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2073 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2074 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2075 | |
| 2076 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2077 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2078 | VISIT(c, annexpr, annotation); |
| 2079 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2081 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2085 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2086 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2087 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2088 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2090 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2091 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | c, |
| 2093 | arg->arg, |
| 2094 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2095 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2096 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2097 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2098 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | static int |
| 2102 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2103 | expr_ty returns) |
| 2104 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2105 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2106 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2107 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2108 | 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] | 2109 | */ |
| 2110 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2111 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2112 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2113 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2114 | return 0; |
| 2115 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2116 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2117 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2118 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2119 | args->vararg->annotation, &annotations_len)) |
| 2120 | return 0; |
| 2121 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2122 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2123 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2124 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2125 | args->kwarg->annotation, &annotations_len)) |
| 2126 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | if (!return_str) { |
| 2129 | return_str = PyUnicode_InternFromString("return"); |
| 2130 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2131 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2133 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2134 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2137 | if (annotations_len) { |
| 2138 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2139 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2141 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2142 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2146 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2147 | { |
| 2148 | VISIT_SEQ(c, expr, args->defaults); |
| 2149 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2150 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2153 | static Py_ssize_t |
| 2154 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2155 | { |
| 2156 | Py_ssize_t funcflags = 0; |
| 2157 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2158 | if (!compiler_visit_defaults(c, args)) |
| 2159 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2160 | funcflags |= 0x01; |
| 2161 | } |
| 2162 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2163 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2164 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2165 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2166 | return -1; |
| 2167 | } |
| 2168 | else if (res > 0) { |
| 2169 | funcflags |= 0x02; |
| 2170 | } |
| 2171 | } |
| 2172 | return funcflags; |
| 2173 | } |
| 2174 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2175 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2176 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2177 | { |
| 2178 | |
| 2179 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2180 | compiler_error(c, "cannot assign to __debug__"); |
| 2181 | return 1; |
| 2182 | } |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | static int |
| 2187 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2188 | { |
| 2189 | if (arg != NULL) { |
| 2190 | if (forbidden_name(c, arg->arg, Store)) |
| 2191 | return 0; |
| 2192 | } |
| 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2197 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2198 | { |
| 2199 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2200 | 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] | 2201 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2202 | return 0; |
| 2203 | } |
| 2204 | } |
| 2205 | return 1; |
| 2206 | } |
| 2207 | |
| 2208 | static int |
| 2209 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2210 | { |
| 2211 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2212 | return 0; |
| 2213 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2214 | return 0; |
| 2215 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2216 | return 0; |
| 2217 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2218 | return 0; |
| 2219 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2220 | return 0; |
| 2221 | return 1; |
| 2222 | } |
| 2223 | |
| 2224 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2225 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2228 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2229 | arguments_ty args; |
| 2230 | expr_ty returns; |
| 2231 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2232 | asdl_expr_seq* decos; |
| 2233 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2234 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2235 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2236 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2237 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2238 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2239 | if (is_async) { |
| 2240 | assert(s->kind == AsyncFunctionDef_kind); |
| 2241 | |
| 2242 | args = s->v.AsyncFunctionDef.args; |
| 2243 | returns = s->v.AsyncFunctionDef.returns; |
| 2244 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2245 | name = s->v.AsyncFunctionDef.name; |
| 2246 | body = s->v.AsyncFunctionDef.body; |
| 2247 | |
| 2248 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2249 | } else { |
| 2250 | assert(s->kind == FunctionDef_kind); |
| 2251 | |
| 2252 | args = s->v.FunctionDef.args; |
| 2253 | returns = s->v.FunctionDef.returns; |
| 2254 | decos = s->v.FunctionDef.decorator_list; |
| 2255 | name = s->v.FunctionDef.name; |
| 2256 | body = s->v.FunctionDef.body; |
| 2257 | |
| 2258 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2259 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2260 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2261 | if (!compiler_check_debug_args(c, args)) |
| 2262 | return 0; |
| 2263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | if (!compiler_decorators(c, decos)) |
| 2265 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2266 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2267 | firstlineno = s->lineno; |
| 2268 | if (asdl_seq_LEN(decos)) { |
| 2269 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2270 | } |
| 2271 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2272 | funcflags = compiler_default_arguments(c, args); |
| 2273 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2275 | } |
| 2276 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2277 | annotations = compiler_visit_annotations(c, args, returns); |
| 2278 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2279 | return 0; |
| 2280 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2281 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2282 | funcflags |= 0x04; |
| 2283 | } |
| 2284 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2285 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2286 | return 0; |
| 2287 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2288 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2289 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2290 | if (c->c_optimize < 2) { |
| 2291 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2292 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2293 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | compiler_exit_scope(c); |
| 2295 | return 0; |
| 2296 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2299 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2301 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2302 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2303 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2305 | qualname = c->u->u_qualname; |
| 2306 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2308 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2309 | Py_XDECREF(qualname); |
| 2310 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2312 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2313 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2314 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2315 | Py_DECREF(qualname); |
| 2316 | Py_DECREF(co); |
| 2317 | return 0; |
| 2318 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2319 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | /* decorators */ |
| 2323 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2324 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2325 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2326 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2327 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | static int |
| 2331 | compiler_class(struct compiler *c, stmt_ty s) |
| 2332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | PyCodeObject *co; |
| 2334 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2335 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2336 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | if (!compiler_decorators(c, decos)) |
| 2339 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2340 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2341 | firstlineno = s->lineno; |
| 2342 | if (asdl_seq_LEN(decos)) { |
| 2343 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2344 | } |
| 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | /* ultimately generate code for: |
| 2347 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2348 | where: |
| 2349 | <func> is a function/closure created from the class body; |
| 2350 | it has a single argument (__locals__) where the dict |
| 2351 | (or MutableSequence) representing the locals is passed |
| 2352 | <name> is the class name |
| 2353 | <bases> is the positional arguments and *varargs argument |
| 2354 | <keywords> is the keyword arguments and **kwds argument |
| 2355 | This borrows from compiler_call. |
| 2356 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2359 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2360 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | return 0; |
| 2362 | /* this block represents what we do in the new scope */ |
| 2363 | { |
| 2364 | /* use the class name for name mangling */ |
| 2365 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2366 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | /* load (global) __name__ ... */ |
| 2368 | str = PyUnicode_InternFromString("__name__"); |
| 2369 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2370 | Py_XDECREF(str); |
| 2371 | compiler_exit_scope(c); |
| 2372 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2373 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | Py_DECREF(str); |
| 2375 | /* ... and store it as __module__ */ |
| 2376 | str = PyUnicode_InternFromString("__module__"); |
| 2377 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2378 | Py_XDECREF(str); |
| 2379 | compiler_exit_scope(c); |
| 2380 | return 0; |
| 2381 | } |
| 2382 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2383 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2384 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2385 | str = PyUnicode_InternFromString("__qualname__"); |
| 2386 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2387 | Py_XDECREF(str); |
| 2388 | compiler_exit_scope(c); |
| 2389 | return 0; |
| 2390 | } |
| 2391 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2393 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | compiler_exit_scope(c); |
| 2395 | return 0; |
| 2396 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2397 | /* The following code is artificial */ |
| 2398 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2399 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2400 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2401 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2402 | str = PyUnicode_InternFromString("__class__"); |
| 2403 | if (str == NULL) { |
| 2404 | compiler_exit_scope(c); |
| 2405 | return 0; |
| 2406 | } |
| 2407 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2408 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2409 | if (i < 0) { |
| 2410 | compiler_exit_scope(c); |
| 2411 | return 0; |
| 2412 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2413 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2416 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2417 | str = PyUnicode_InternFromString("__classcell__"); |
| 2418 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2419 | Py_XDECREF(str); |
| 2420 | compiler_exit_scope(c); |
| 2421 | return 0; |
| 2422 | } |
| 2423 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2425 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2426 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2427 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2428 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2429 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2430 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | /* create the code object */ |
| 2432 | co = assemble(c, 1); |
| 2433 | } |
| 2434 | /* leave the new scope */ |
| 2435 | compiler_exit_scope(c); |
| 2436 | if (co == NULL) |
| 2437 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | /* 2. load the 'build_class' function */ |
| 2440 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2441 | |
| 2442 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2443 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2444 | Py_DECREF(co); |
| 2445 | return 0; |
| 2446 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | Py_DECREF(co); |
| 2448 | |
| 2449 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2450 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | |
| 2452 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2453 | 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] | 2454 | return 0; |
| 2455 | |
| 2456 | /* 6. apply decorators */ |
| 2457 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2458 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2459 | } |
| 2460 | |
| 2461 | /* 7. store into <name> */ |
| 2462 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2463 | return 0; |
| 2464 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2467 | /* Return 0 if the expression is a constant value except named singletons. |
| 2468 | Return 1 otherwise. */ |
| 2469 | static int |
| 2470 | check_is_arg(expr_ty e) |
| 2471 | { |
| 2472 | if (e->kind != Constant_kind) { |
| 2473 | return 1; |
| 2474 | } |
| 2475 | PyObject *value = e->v.Constant.value; |
| 2476 | return (value == Py_None |
| 2477 | || value == Py_False |
| 2478 | || value == Py_True |
| 2479 | || value == Py_Ellipsis); |
| 2480 | } |
| 2481 | |
| 2482 | /* Check operands of identity chacks ("is" and "is not"). |
| 2483 | Emit a warning if any operand is a constant except named singletons. |
| 2484 | Return 0 on error. |
| 2485 | */ |
| 2486 | static int |
| 2487 | check_compare(struct compiler *c, expr_ty e) |
| 2488 | { |
| 2489 | Py_ssize_t i, n; |
| 2490 | int left = check_is_arg(e->v.Compare.left); |
| 2491 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2492 | for (i = 0; i < n; i++) { |
| 2493 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2494 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2495 | if (op == Is || op == IsNot) { |
| 2496 | if (!right || !left) { |
| 2497 | const char *msg = (op == Is) |
| 2498 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2499 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2500 | return compiler_warn(c, msg); |
| 2501 | } |
| 2502 | } |
| 2503 | left = right; |
| 2504 | } |
| 2505 | return 1; |
| 2506 | } |
| 2507 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2508 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2509 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2510 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2511 | switch (op) { |
| 2512 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2513 | cmp = Py_EQ; |
| 2514 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2515 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2516 | cmp = Py_NE; |
| 2517 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2518 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2519 | cmp = Py_LT; |
| 2520 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2521 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2522 | cmp = Py_LE; |
| 2523 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2524 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2525 | cmp = Py_GT; |
| 2526 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2527 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2528 | cmp = Py_GE; |
| 2529 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2530 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2531 | ADDOP_I(c, IS_OP, 0); |
| 2532 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2533 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2534 | ADDOP_I(c, IS_OP, 1); |
| 2535 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2536 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2537 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2538 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2539 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2540 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2541 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2542 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2543 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2544 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2545 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2546 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2547 | } |
| 2548 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2549 | |
| 2550 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2551 | static int |
| 2552 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2553 | { |
| 2554 | switch (e->kind) { |
| 2555 | case UnaryOp_kind: |
| 2556 | if (e->v.UnaryOp.op == Not) |
| 2557 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2558 | /* fallback to general implementation */ |
| 2559 | break; |
| 2560 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2561 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2562 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2563 | assert(n >= 0); |
| 2564 | int cond2 = e->v.BoolOp.op == Or; |
| 2565 | basicblock *next2 = next; |
| 2566 | if (!cond2 != !cond) { |
| 2567 | next2 = compiler_new_block(c); |
| 2568 | if (next2 == NULL) |
| 2569 | return 0; |
| 2570 | } |
| 2571 | for (i = 0; i < n; ++i) { |
| 2572 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2573 | return 0; |
| 2574 | } |
| 2575 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2576 | return 0; |
| 2577 | if (next2 != next) |
| 2578 | compiler_use_next_block(c, next2); |
| 2579 | return 1; |
| 2580 | } |
| 2581 | case IfExp_kind: { |
| 2582 | basicblock *end, *next2; |
| 2583 | end = compiler_new_block(c); |
| 2584 | if (end == NULL) |
| 2585 | return 0; |
| 2586 | next2 = compiler_new_block(c); |
| 2587 | if (next2 == NULL) |
| 2588 | return 0; |
| 2589 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2590 | return 0; |
| 2591 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2592 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2593 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2594 | compiler_use_next_block(c, next2); |
| 2595 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2596 | return 0; |
| 2597 | compiler_use_next_block(c, end); |
| 2598 | return 1; |
| 2599 | } |
| 2600 | case Compare_kind: { |
| 2601 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2602 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2603 | if (!check_compare(c, e)) { |
| 2604 | return 0; |
| 2605 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2606 | basicblock *cleanup = compiler_new_block(c); |
| 2607 | if (cleanup == NULL) |
| 2608 | return 0; |
| 2609 | VISIT(c, expr, e->v.Compare.left); |
| 2610 | for (i = 0; i < n; i++) { |
| 2611 | VISIT(c, expr, |
| 2612 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2613 | ADDOP(c, DUP_TOP); |
| 2614 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2615 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2616 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2617 | NEXT_BLOCK(c); |
| 2618 | } |
| 2619 | 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] | 2620 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2621 | 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] | 2622 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2623 | basicblock *end = compiler_new_block(c); |
| 2624 | if (end == NULL) |
| 2625 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2626 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2627 | compiler_use_next_block(c, cleanup); |
| 2628 | ADDOP(c, POP_TOP); |
| 2629 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2630 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2631 | } |
| 2632 | compiler_use_next_block(c, end); |
| 2633 | return 1; |
| 2634 | } |
| 2635 | /* fallback to general implementation */ |
| 2636 | break; |
| 2637 | } |
| 2638 | default: |
| 2639 | /* fallback to general implementation */ |
| 2640 | break; |
| 2641 | } |
| 2642 | |
| 2643 | /* general implementation */ |
| 2644 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2645 | 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] | 2646 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2647 | return 1; |
| 2648 | } |
| 2649 | |
| 2650 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2651 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | basicblock *end, *next; |
| 2654 | |
| 2655 | assert(e->kind == IfExp_kind); |
| 2656 | end = compiler_new_block(c); |
| 2657 | if (end == NULL) |
| 2658 | return 0; |
| 2659 | next = compiler_new_block(c); |
| 2660 | if (next == NULL) |
| 2661 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2662 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2663 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2665 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | compiler_use_next_block(c, next); |
| 2667 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2668 | compiler_use_next_block(c, end); |
| 2669 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2673 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2675 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2676 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2678 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | arguments_ty args = e->v.Lambda.args; |
| 2680 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2681 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2682 | if (!compiler_check_debug_args(c, args)) |
| 2683 | return 0; |
| 2684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2685 | if (!name) { |
| 2686 | name = PyUnicode_InternFromString("<lambda>"); |
| 2687 | if (!name) |
| 2688 | return 0; |
| 2689 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2690 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2691 | funcflags = compiler_default_arguments(c, args); |
| 2692 | if (funcflags == -1) { |
| 2693 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2695 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2696 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2697 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | /* Make None the first constant, so the lambda can't have a |
| 2701 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2702 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2706 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2708 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2709 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2710 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | } |
| 2712 | else { |
| 2713 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2714 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2716 | qualname = c->u->u_qualname; |
| 2717 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2719 | if (co == NULL) { |
| 2720 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2722 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2723 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2724 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2725 | Py_DECREF(qualname); |
| 2726 | Py_DECREF(co); |
| 2727 | return 0; |
| 2728 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2729 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | Py_DECREF(co); |
| 2731 | |
| 2732 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2736 | compiler_if(struct compiler *c, stmt_ty s) |
| 2737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | assert(s->kind == If_kind); |
| 2740 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2741 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2743 | } |
| 2744 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2745 | next = compiler_new_block(c); |
| 2746 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2747 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2748 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2749 | } |
| 2750 | else { |
| 2751 | next = end; |
| 2752 | } |
| 2753 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2754 | return 0; |
| 2755 | } |
| 2756 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2757 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2758 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2759 | compiler_use_next_block(c, next); |
| 2760 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | } |
| 2762 | compiler_use_next_block(c, end); |
| 2763 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | static int |
| 2767 | compiler_for(struct compiler *c, stmt_ty s) |
| 2768 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2769 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2772 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | cleanup = compiler_new_block(c); |
| 2774 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2775 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2777 | } |
| 2778 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2780 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | VISIT(c, expr, s->v.For.iter); |
| 2782 | ADDOP(c, GET_ITER); |
| 2783 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2784 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2785 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | VISIT(c, expr, s->v.For.target); |
| 2787 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2788 | /* Mark jump as artificial */ |
| 2789 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2790 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2792 | |
| 2793 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2796 | compiler_use_next_block(c, end); |
| 2797 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2800 | |
| 2801 | static int |
| 2802 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2803 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2804 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2805 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2806 | c->u->u_ste->ste_coroutine = 1; |
| 2807 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2808 | return compiler_error(c, "'async for' outside async function"); |
| 2809 | } |
| 2810 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2811 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2812 | except = compiler_new_block(c); |
| 2813 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2814 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2815 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2816 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2817 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2818 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2819 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2820 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2821 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2822 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2823 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2824 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2825 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2826 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2827 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2828 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2829 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2830 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2831 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2832 | /* Success block for __anext__ */ |
| 2833 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2834 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2835 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2836 | |
| 2837 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2838 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2839 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2840 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2841 | |
| 2842 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2843 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2844 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2845 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2846 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2847 | |
| 2848 | compiler_use_next_block(c, end); |
| 2849 | |
| 2850 | return 1; |
| 2851 | } |
| 2852 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2853 | static int |
| 2854 | compiler_while(struct compiler *c, stmt_ty s) |
| 2855 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2856 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2858 | body = compiler_new_block(c); |
| 2859 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2861 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2865 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2868 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2869 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2874 | SET_LOC(c, s); |
| 2875 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2876 | return 0; |
| 2877 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2878 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2879 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2880 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2881 | compiler_use_next_block(c, anchor); |
| 2882 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2884 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2891 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2892 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2893 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2894 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2895 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2896 | return compiler_error(c, "'return' outside function"); |
| 2897 | if (s->v.Return.value != NULL && |
| 2898 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2899 | { |
| 2900 | return compiler_error( |
| 2901 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2903 | if (preserve_tos) { |
| 2904 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2905 | } else { |
| 2906 | /* Emit instruction with line number for expression */ |
| 2907 | if (s->v.Return.value != NULL) { |
| 2908 | SET_LOC(c, s->v.Return.value); |
| 2909 | ADDOP(c, NOP); |
| 2910 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2911 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2912 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2913 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2914 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2915 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2916 | } |
| 2917 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2918 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2919 | } |
| 2920 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2921 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2926 | static int |
| 2927 | compiler_break(struct compiler *c) |
| 2928 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2929 | struct fblockinfo *loop = NULL; |
| 2930 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2931 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2932 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2933 | if (loop == NULL) { |
| 2934 | return compiler_error(c, "'break' outside loop"); |
| 2935 | } |
| 2936 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2937 | return 0; |
| 2938 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2939 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2940 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2941 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | static int |
| 2945 | compiler_continue(struct compiler *c) |
| 2946 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2947 | struct fblockinfo *loop = NULL; |
| 2948 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2949 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2950 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2951 | if (loop == NULL) { |
| 2952 | return compiler_error(c, "'continue' not properly in loop"); |
| 2953 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2954 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2955 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2956 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2960 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | |
| 2962 | SETUP_FINALLY L |
| 2963 | <code for body> |
| 2964 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2965 | <code for finalbody> |
| 2966 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2967 | L: |
| 2968 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2969 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2971 | The special instructions use the block stack. Each block |
| 2972 | stack entry contains the instruction that created it (here |
| 2973 | SETUP_FINALLY), the level of the value stack at the time the |
| 2974 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2976 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | Pushes the current value stack level and the label |
| 2978 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2979 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2980 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2981 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2983 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2984 | exceptions are pushed onto the value stack (and the exception |
| 2985 | condition is cleared), and the interpreter jumps to the label |
| 2986 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2987 | */ |
| 2988 | |
| 2989 | static int |
| 2990 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2991 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2992 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | body = compiler_new_block(c); |
| 2995 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2996 | exit = compiler_new_block(c); |
| 2997 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3000 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3001 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3003 | 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] | 3004 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3005 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3006 | if (!compiler_try_except(c, s)) |
| 3007 | return 0; |
| 3008 | } |
| 3009 | else { |
| 3010 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3011 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3012 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3013 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3014 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3015 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3016 | /* `finally` block */ |
| 3017 | compiler_use_next_block(c, end); |
| 3018 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3019 | return 0; |
| 3020 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3021 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3022 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3023 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3028 | 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] | 3029 | (The contents of the value stack is shown in [], with the top |
| 3030 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3031 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | |
| 3033 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3034 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | [] <code for S> |
| 3036 | [] POP_BLOCK |
| 3037 | [] JUMP_FORWARD L0 |
| 3038 | |
| 3039 | [tb, val, exc] L1: DUP ) |
| 3040 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3041 | [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] | 3042 | [tb, val, exc] POP |
| 3043 | [tb, val] <assign to V1> (or POP if no V1) |
| 3044 | [tb] POP |
| 3045 | [] <code for S1> |
| 3046 | JUMP_FORWARD L0 |
| 3047 | |
| 3048 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3049 | .............................etc....................... |
| 3050 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3051 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | |
| 3053 | [] L0: <next statement> |
| 3054 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3055 | Of course, parts are not generated if Vi or Ei is not present. |
| 3056 | */ |
| 3057 | static int |
| 3058 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3059 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3061 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | body = compiler_new_block(c); |
| 3064 | except = compiler_new_block(c); |
| 3065 | orelse = compiler_new_block(c); |
| 3066 | end = compiler_new_block(c); |
| 3067 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3068 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3069 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3071 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3073 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3074 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3075 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3076 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3077 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3079 | /* Runtime will push a block here, so we need to account for that */ |
| 3080 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3081 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | for (i = 0; i < n; i++) { |
| 3083 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3084 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3086 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3087 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | except = compiler_new_block(c); |
| 3089 | if (except == NULL) |
| 3090 | return 0; |
| 3091 | if (handler->v.ExceptHandler.type) { |
| 3092 | ADDOP(c, DUP_TOP); |
| 3093 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3094 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3095 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | } |
| 3097 | ADDOP(c, POP_TOP); |
| 3098 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3099 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3100 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3101 | cleanup_end = compiler_new_block(c); |
| 3102 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3103 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3104 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3105 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3106 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3107 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3108 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3110 | /* |
| 3111 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3112 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3113 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3114 | try: |
| 3115 | # body |
| 3116 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3117 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3118 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3119 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3121 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3122 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3123 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3124 | 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] | 3125 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3127 | /* second # body */ |
| 3128 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3129 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3130 | ADDOP(c, POP_BLOCK); |
| 3131 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3132 | /* name = None; del name; # Mark as artificial */ |
| 3133 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3134 | ADDOP_LOAD_CONST(c, Py_None); |
| 3135 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3136 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3137 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3139 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3140 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3142 | /* name = None; del name; # Mark as artificial */ |
| 3143 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3144 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3145 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3146 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3148 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | } |
| 3150 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3151 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3153 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3154 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3155 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3157 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3158 | ADDOP(c, POP_TOP); |
| 3159 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3160 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3161 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3163 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3164 | /* name = None; del name; # Mark as artificial */ |
| 3165 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3166 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3167 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | compiler_use_next_block(c, except); |
| 3170 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3171 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3172 | /* Mark as artificial */ |
| 3173 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3174 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3176 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | compiler_use_next_block(c, end); |
| 3178 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3182 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3183 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3184 | return compiler_try_finally(c, s); |
| 3185 | else |
| 3186 | return compiler_try_except(c, s); |
| 3187 | } |
| 3188 | |
| 3189 | |
| 3190 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3191 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | /* The IMPORT_NAME opcode was already generated. This function |
| 3194 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3196 | 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] | 3197 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3199 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3200 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3201 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3202 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3203 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3205 | while (1) { |
| 3206 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3208 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3209 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3210 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3211 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3213 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3214 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3215 | if (dot == -1) { |
| 3216 | break; |
| 3217 | } |
| 3218 | ADDOP(c, ROT_TWO); |
| 3219 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3221 | if (!compiler_nameop(c, asname, Store)) { |
| 3222 | return 0; |
| 3223 | } |
| 3224 | ADDOP(c, POP_TOP); |
| 3225 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | } |
| 3227 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | static int |
| 3231 | compiler_import(struct compiler *c, stmt_ty s) |
| 3232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | /* The Import node stores a module name like a.b.c as a single |
| 3234 | string. This is convenient for all cases except |
| 3235 | import a.b.c as d |
| 3236 | where we need to parse that string to extract the individual |
| 3237 | module names. |
| 3238 | XXX Perhaps change the representation to make this case simpler? |
| 3239 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3240 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3241 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3242 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | for (i = 0; i < n; i++) { |
| 3244 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3245 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3246 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3247 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3248 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | if (alias->asname) { |
| 3252 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3253 | if (!r) |
| 3254 | return r; |
| 3255 | } |
| 3256 | else { |
| 3257 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3258 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3259 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3260 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3261 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3262 | if (tmp == NULL) |
| 3263 | return 0; |
| 3264 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3266 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | Py_DECREF(tmp); |
| 3268 | } |
| 3269 | if (!r) |
| 3270 | return r; |
| 3271 | } |
| 3272 | } |
| 3273 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | static int |
| 3277 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3278 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3279 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3280 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | if (!empty_string) { |
| 3284 | empty_string = PyUnicode_FromString(""); |
| 3285 | if (!empty_string) |
| 3286 | return 0; |
| 3287 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3289 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3290 | |
| 3291 | names = PyTuple_New(n); |
| 3292 | if (!names) |
| 3293 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | /* build up the names */ |
| 3296 | for (i = 0; i < n; i++) { |
| 3297 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3298 | Py_INCREF(alias->name); |
| 3299 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3300 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3302 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3303 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | Py_DECREF(names); |
| 3305 | return compiler_error(c, "from __future__ imports must occur " |
| 3306 | "at the beginning of the file"); |
| 3307 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3308 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | if (s->v.ImportFrom.module) { |
| 3311 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3312 | } |
| 3313 | else { |
| 3314 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3315 | } |
| 3316 | for (i = 0; i < n; i++) { |
| 3317 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3318 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3319 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3320 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | assert(n == 1); |
| 3322 | ADDOP(c, IMPORT_STAR); |
| 3323 | return 1; |
| 3324 | } |
| 3325 | |
| 3326 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3327 | store_name = alias->name; |
| 3328 | if (alias->asname) |
| 3329 | store_name = alias->asname; |
| 3330 | |
| 3331 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | return 0; |
| 3333 | } |
| 3334 | } |
| 3335 | /* remove imported module */ |
| 3336 | ADDOP(c, POP_TOP); |
| 3337 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | static int |
| 3341 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3344 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3345 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3346 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3347 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3348 | (s->v.Assert.test->kind == Constant_kind && |
| 3349 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3350 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3351 | { |
| 3352 | if (!compiler_warn(c, "assertion is always true, " |
| 3353 | "perhaps remove parentheses?")) |
| 3354 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3355 | return 0; |
| 3356 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3357 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3358 | if (c->c_optimize) |
| 3359 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | end = compiler_new_block(c); |
| 3361 | if (end == NULL) |
| 3362 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3363 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3364 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3365 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3366 | if (s->v.Assert.msg) { |
| 3367 | VISIT(c, expr, s->v.Assert.msg); |
| 3368 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3369 | } |
| 3370 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3371 | compiler_use_next_block(c, end); |
| 3372 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3376 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3377 | { |
| 3378 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3379 | VISIT(c, expr, value); |
| 3380 | ADDOP(c, PRINT_EXPR); |
| 3381 | return 1; |
| 3382 | } |
| 3383 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3384 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3385 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3386 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3387 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3391 | /* Mark POP_TOP as artificial */ |
| 3392 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3393 | ADDOP(c, POP_TOP); |
| 3394 | return 1; |
| 3395 | } |
| 3396 | |
| 3397 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3398 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3399 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3400 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3403 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | switch (s->kind) { |
| 3406 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3407 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | case ClassDef_kind: |
| 3409 | return compiler_class(c, s); |
| 3410 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3411 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3412 | case Delete_kind: |
| 3413 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3414 | break; |
| 3415 | case Assign_kind: |
| 3416 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3417 | VISIT(c, expr, s->v.Assign.value); |
| 3418 | for (i = 0; i < n; i++) { |
| 3419 | if (i < n - 1) |
| 3420 | ADDOP(c, DUP_TOP); |
| 3421 | VISIT(c, expr, |
| 3422 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3423 | } |
| 3424 | break; |
| 3425 | case AugAssign_kind: |
| 3426 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3427 | case AnnAssign_kind: |
| 3428 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | case For_kind: |
| 3430 | return compiler_for(c, s); |
| 3431 | case While_kind: |
| 3432 | return compiler_while(c, s); |
| 3433 | case If_kind: |
| 3434 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3435 | case Match_kind: |
| 3436 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | case Raise_kind: |
| 3438 | n = 0; |
| 3439 | if (s->v.Raise.exc) { |
| 3440 | VISIT(c, expr, s->v.Raise.exc); |
| 3441 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3442 | if (s->v.Raise.cause) { |
| 3443 | VISIT(c, expr, s->v.Raise.cause); |
| 3444 | n++; |
| 3445 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3447 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3448 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3450 | case Try_kind: |
| 3451 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | case Assert_kind: |
| 3453 | return compiler_assert(c, s); |
| 3454 | case Import_kind: |
| 3455 | return compiler_import(c, s); |
| 3456 | case ImportFrom_kind: |
| 3457 | return compiler_from_import(c, s); |
| 3458 | case Global_kind: |
| 3459 | case Nonlocal_kind: |
| 3460 | break; |
| 3461 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3462 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3464 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3465 | break; |
| 3466 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3467 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | case Continue_kind: |
| 3469 | return compiler_continue(c); |
| 3470 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3471 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3472 | case AsyncFunctionDef_kind: |
| 3473 | return compiler_function(c, s, 1); |
| 3474 | case AsyncWith_kind: |
| 3475 | return compiler_async_with(c, s, 0); |
| 3476 | case AsyncFor_kind: |
| 3477 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3478 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | static int |
| 3484 | unaryop(unaryop_ty op) |
| 3485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | switch (op) { |
| 3487 | case Invert: |
| 3488 | return UNARY_INVERT; |
| 3489 | case Not: |
| 3490 | return UNARY_NOT; |
| 3491 | case UAdd: |
| 3492 | return UNARY_POSITIVE; |
| 3493 | case USub: |
| 3494 | return UNARY_NEGATIVE; |
| 3495 | default: |
| 3496 | PyErr_Format(PyExc_SystemError, |
| 3497 | "unary op %d should not be possible", op); |
| 3498 | return 0; |
| 3499 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3503 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | switch (op) { |
| 3506 | case Add: |
| 3507 | return BINARY_ADD; |
| 3508 | case Sub: |
| 3509 | return BINARY_SUBTRACT; |
| 3510 | case Mult: |
| 3511 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3512 | case MatMult: |
| 3513 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | case Div: |
| 3515 | return BINARY_TRUE_DIVIDE; |
| 3516 | case Mod: |
| 3517 | return BINARY_MODULO; |
| 3518 | case Pow: |
| 3519 | return BINARY_POWER; |
| 3520 | case LShift: |
| 3521 | return BINARY_LSHIFT; |
| 3522 | case RShift: |
| 3523 | return BINARY_RSHIFT; |
| 3524 | case BitOr: |
| 3525 | return BINARY_OR; |
| 3526 | case BitXor: |
| 3527 | return BINARY_XOR; |
| 3528 | case BitAnd: |
| 3529 | return BINARY_AND; |
| 3530 | case FloorDiv: |
| 3531 | return BINARY_FLOOR_DIVIDE; |
| 3532 | default: |
| 3533 | PyErr_Format(PyExc_SystemError, |
| 3534 | "binary op %d should not be possible", op); |
| 3535 | return 0; |
| 3536 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3540 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3541 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3542 | switch (op) { |
| 3543 | case Add: |
| 3544 | return INPLACE_ADD; |
| 3545 | case Sub: |
| 3546 | return INPLACE_SUBTRACT; |
| 3547 | case Mult: |
| 3548 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3549 | case MatMult: |
| 3550 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3551 | case Div: |
| 3552 | return INPLACE_TRUE_DIVIDE; |
| 3553 | case Mod: |
| 3554 | return INPLACE_MODULO; |
| 3555 | case Pow: |
| 3556 | return INPLACE_POWER; |
| 3557 | case LShift: |
| 3558 | return INPLACE_LSHIFT; |
| 3559 | case RShift: |
| 3560 | return INPLACE_RSHIFT; |
| 3561 | case BitOr: |
| 3562 | return INPLACE_OR; |
| 3563 | case BitXor: |
| 3564 | return INPLACE_XOR; |
| 3565 | case BitAnd: |
| 3566 | return INPLACE_AND; |
| 3567 | case FloorDiv: |
| 3568 | return INPLACE_FLOOR_DIVIDE; |
| 3569 | default: |
| 3570 | PyErr_Format(PyExc_SystemError, |
| 3571 | "inplace binary op %d should not be possible", op); |
| 3572 | return 0; |
| 3573 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | static int |
| 3577 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3578 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3579 | int op, scope; |
| 3580 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | PyObject *dict = c->u->u_names; |
| 3584 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3585 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3586 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3587 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3588 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3589 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3590 | if (forbidden_name(c, name, ctx)) |
| 3591 | return 0; |
| 3592 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3593 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3594 | if (!mangled) |
| 3595 | return 0; |
| 3596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3597 | op = 0; |
| 3598 | optype = OP_NAME; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 3599 | scope = _PyST_GetScope(c->u->u_ste, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3600 | switch (scope) { |
| 3601 | case FREE: |
| 3602 | dict = c->u->u_freevars; |
| 3603 | optype = OP_DEREF; |
| 3604 | break; |
| 3605 | case CELL: |
| 3606 | dict = c->u->u_cellvars; |
| 3607 | optype = OP_DEREF; |
| 3608 | break; |
| 3609 | case LOCAL: |
| 3610 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3611 | optype = OP_FAST; |
| 3612 | break; |
| 3613 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3614 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | optype = OP_GLOBAL; |
| 3616 | break; |
| 3617 | case GLOBAL_EXPLICIT: |
| 3618 | optype = OP_GLOBAL; |
| 3619 | break; |
| 3620 | default: |
| 3621 | /* scope can be 0 */ |
| 3622 | break; |
| 3623 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | /* 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] | 3626 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | switch (optype) { |
| 3629 | case OP_DEREF: |
| 3630 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3631 | case Load: |
| 3632 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3633 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3634 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3635 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3636 | } |
| 3637 | break; |
| 3638 | case OP_FAST: |
| 3639 | switch (ctx) { |
| 3640 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3641 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3644 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | return 1; |
| 3646 | case OP_GLOBAL: |
| 3647 | switch (ctx) { |
| 3648 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3649 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3650 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | } |
| 3652 | break; |
| 3653 | case OP_NAME: |
| 3654 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3655 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3656 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3657 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | } |
| 3659 | break; |
| 3660 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3663 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | Py_DECREF(mangled); |
| 3665 | if (arg < 0) |
| 3666 | return 0; |
| 3667 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | static int |
| 3671 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3673 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3674 | int jumpi; |
| 3675 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3676 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | assert(e->kind == BoolOp_kind); |
| 3679 | if (e->v.BoolOp.op == And) |
| 3680 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3681 | else |
| 3682 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3683 | end = compiler_new_block(c); |
| 3684 | if (end == NULL) |
| 3685 | return 0; |
| 3686 | s = e->v.BoolOp.values; |
| 3687 | n = asdl_seq_LEN(s) - 1; |
| 3688 | assert(n >= 0); |
| 3689 | for (i = 0; i < n; ++i) { |
| 3690 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3691 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3692 | basicblock *next = compiler_new_block(c); |
| 3693 | if (next == NULL) { |
| 3694 | return 0; |
| 3695 | } |
| 3696 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3697 | } |
| 3698 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3699 | compiler_use_next_block(c, end); |
| 3700 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3704 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3705 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3706 | { |
| 3707 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3708 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3709 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3710 | PyObject *folded = PyTuple_New(n); |
| 3711 | if (folded == NULL) { |
| 3712 | return 0; |
| 3713 | } |
| 3714 | PyObject *val; |
| 3715 | for (i = 0; i < n; i++) { |
| 3716 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3717 | Py_INCREF(val); |
| 3718 | PyTuple_SET_ITEM(folded, i, val); |
| 3719 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3720 | if (tuple) { |
| 3721 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3722 | } else { |
| 3723 | if (add == SET_ADD) { |
| 3724 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3725 | if (folded == NULL) { |
| 3726 | return 0; |
| 3727 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3728 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3729 | ADDOP_I(c, build, pushed); |
| 3730 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3731 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3732 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3733 | return 1; |
| 3734 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3735 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3736 | for (i = 0; i < n; i++) { |
| 3737 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3738 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3739 | seen_star = 1; |
| 3740 | } |
| 3741 | } |
| 3742 | if (seen_star) { |
| 3743 | seen_star = 0; |
| 3744 | for (i = 0; i < n; i++) { |
| 3745 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3746 | if (elt->kind == Starred_kind) { |
| 3747 | if (seen_star == 0) { |
| 3748 | ADDOP_I(c, build, i+pushed); |
| 3749 | seen_star = 1; |
| 3750 | } |
| 3751 | VISIT(c, expr, elt->v.Starred.value); |
| 3752 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3753 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3754 | else { |
| 3755 | VISIT(c, expr, elt); |
| 3756 | if (seen_star) { |
| 3757 | ADDOP_I(c, add, 1); |
| 3758 | } |
| 3759 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3760 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3761 | assert(seen_star); |
| 3762 | if (tuple) { |
| 3763 | ADDOP(c, LIST_TO_TUPLE); |
| 3764 | } |
| 3765 | } |
| 3766 | else { |
| 3767 | for (i = 0; i < n; i++) { |
| 3768 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3769 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3770 | } |
| 3771 | if (tuple) { |
| 3772 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3773 | } else { |
| 3774 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3775 | } |
| 3776 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3777 | return 1; |
| 3778 | } |
| 3779 | |
| 3780 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3781 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3782 | { |
| 3783 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3784 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3785 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3786 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3787 | if (elt->kind == Starred_kind && !seen_star) { |
| 3788 | if ((i >= (1 << 8)) || |
| 3789 | (n-i-1 >= (INT_MAX >> 8))) |
| 3790 | return compiler_error(c, |
| 3791 | "too many expressions in " |
| 3792 | "star-unpacking assignment"); |
| 3793 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3794 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3795 | } |
| 3796 | else if (elt->kind == Starred_kind) { |
| 3797 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3798 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3799 | } |
| 3800 | } |
| 3801 | if (!seen_star) { |
| 3802 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3803 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3804 | return 1; |
| 3805 | } |
| 3806 | |
| 3807 | static int |
| 3808 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3809 | { |
| 3810 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3811 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3812 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3813 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3814 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3815 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3816 | return 1; |
| 3817 | } |
| 3818 | |
| 3819 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3820 | compiler_list(struct compiler *c, expr_ty e) |
| 3821 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3822 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3823 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3824 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3826 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3827 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3828 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3830 | else |
| 3831 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3832 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | static int |
| 3836 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3837 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3838 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3839 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3840 | return assignment_helper(c, elts); |
| 3841 | } |
| 3842 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3843 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3844 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3845 | } |
| 3846 | else |
| 3847 | VISIT_SEQ(c, expr, elts); |
| 3848 | return 1; |
| 3849 | } |
| 3850 | |
| 3851 | static int |
| 3852 | compiler_set(struct compiler *c, expr_ty e) |
| 3853 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3854 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3855 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3859 | 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] | 3860 | { |
| 3861 | Py_ssize_t i; |
| 3862 | for (i = begin; i < end; i++) { |
| 3863 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3864 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3865 | return 0; |
| 3866 | } |
| 3867 | return 1; |
| 3868 | } |
| 3869 | |
| 3870 | static int |
| 3871 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3872 | { |
| 3873 | Py_ssize_t i, n = end - begin; |
| 3874 | PyObject *keys, *key; |
| 3875 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3876 | for (i = begin; i < end; i++) { |
| 3877 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3878 | } |
| 3879 | keys = PyTuple_New(n); |
| 3880 | if (keys == NULL) { |
| 3881 | return 0; |
| 3882 | } |
| 3883 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3884 | 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] | 3885 | Py_INCREF(key); |
| 3886 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3887 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3888 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3889 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3890 | } |
| 3891 | else { |
| 3892 | for (i = begin; i < end; i++) { |
| 3893 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3894 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3895 | } |
| 3896 | ADDOP_I(c, BUILD_MAP, n); |
| 3897 | } |
| 3898 | return 1; |
| 3899 | } |
| 3900 | |
| 3901 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3902 | compiler_dict(struct compiler *c, expr_ty e) |
| 3903 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3904 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3905 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3906 | int is_unpacking = 0; |
| 3907 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3908 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3909 | elements = 0; |
| 3910 | for (i = 0; i < n; i++) { |
| 3911 | 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] | 3912 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3913 | if (elements) { |
| 3914 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3915 | return 0; |
| 3916 | } |
| 3917 | if (have_dict) { |
| 3918 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3919 | } |
| 3920 | have_dict = 1; |
| 3921 | elements = 0; |
| 3922 | } |
| 3923 | if (have_dict == 0) { |
| 3924 | ADDOP_I(c, BUILD_MAP, 0); |
| 3925 | have_dict = 1; |
| 3926 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3927 | 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] | 3928 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3929 | } |
| 3930 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3931 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3932 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3933 | return 0; |
| 3934 | } |
| 3935 | if (have_dict) { |
| 3936 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3937 | } |
| 3938 | have_dict = 1; |
| 3939 | elements = 0; |
| 3940 | } |
| 3941 | else { |
| 3942 | elements++; |
| 3943 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3944 | } |
| 3945 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3946 | if (elements) { |
| 3947 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3948 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3949 | } |
| 3950 | if (have_dict) { |
| 3951 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3952 | } |
| 3953 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3954 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3955 | if (!have_dict) { |
| 3956 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | } |
| 3958 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | static int |
| 3962 | compiler_compare(struct compiler *c, expr_ty e) |
| 3963 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3964 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3965 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3966 | if (!check_compare(c, e)) { |
| 3967 | return 0; |
| 3968 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3970 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3971 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3972 | if (n == 0) { |
| 3973 | 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] | 3974 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3975 | } |
| 3976 | else { |
| 3977 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3978 | if (cleanup == NULL) |
| 3979 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3980 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3981 | VISIT(c, expr, |
| 3982 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3983 | ADDOP(c, DUP_TOP); |
| 3984 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3985 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3986 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3987 | NEXT_BLOCK(c); |
| 3988 | } |
| 3989 | 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] | 3990 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | basicblock *end = compiler_new_block(c); |
| 3992 | if (end == NULL) |
| 3993 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3994 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | compiler_use_next_block(c, cleanup); |
| 3996 | ADDOP(c, ROT_TWO); |
| 3997 | ADDOP(c, POP_TOP); |
| 3998 | compiler_use_next_block(c, end); |
| 3999 | } |
| 4000 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4003 | static PyTypeObject * |
| 4004 | infer_type(expr_ty e) |
| 4005 | { |
| 4006 | switch (e->kind) { |
| 4007 | case Tuple_kind: |
| 4008 | return &PyTuple_Type; |
| 4009 | case List_kind: |
| 4010 | case ListComp_kind: |
| 4011 | return &PyList_Type; |
| 4012 | case Dict_kind: |
| 4013 | case DictComp_kind: |
| 4014 | return &PyDict_Type; |
| 4015 | case Set_kind: |
| 4016 | case SetComp_kind: |
| 4017 | return &PySet_Type; |
| 4018 | case GeneratorExp_kind: |
| 4019 | return &PyGen_Type; |
| 4020 | case Lambda_kind: |
| 4021 | return &PyFunction_Type; |
| 4022 | case JoinedStr_kind: |
| 4023 | case FormattedValue_kind: |
| 4024 | return &PyUnicode_Type; |
| 4025 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4026 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4027 | default: |
| 4028 | return NULL; |
| 4029 | } |
| 4030 | } |
| 4031 | |
| 4032 | static int |
| 4033 | check_caller(struct compiler *c, expr_ty e) |
| 4034 | { |
| 4035 | switch (e->kind) { |
| 4036 | case Constant_kind: |
| 4037 | case Tuple_kind: |
| 4038 | case List_kind: |
| 4039 | case ListComp_kind: |
| 4040 | case Dict_kind: |
| 4041 | case DictComp_kind: |
| 4042 | case Set_kind: |
| 4043 | case SetComp_kind: |
| 4044 | case GeneratorExp_kind: |
| 4045 | case JoinedStr_kind: |
| 4046 | case FormattedValue_kind: |
| 4047 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4048 | "perhaps you missed a comma?", |
| 4049 | infer_type(e)->tp_name); |
| 4050 | default: |
| 4051 | return 1; |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | static int |
| 4056 | check_subscripter(struct compiler *c, expr_ty e) |
| 4057 | { |
| 4058 | PyObject *v; |
| 4059 | |
| 4060 | switch (e->kind) { |
| 4061 | case Constant_kind: |
| 4062 | v = e->v.Constant.value; |
| 4063 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4064 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4065 | PyAnySet_Check(v))) |
| 4066 | { |
| 4067 | return 1; |
| 4068 | } |
| 4069 | /* fall through */ |
| 4070 | case Set_kind: |
| 4071 | case SetComp_kind: |
| 4072 | case GeneratorExp_kind: |
| 4073 | case Lambda_kind: |
| 4074 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4075 | "perhaps you missed a comma?", |
| 4076 | infer_type(e)->tp_name); |
| 4077 | default: |
| 4078 | return 1; |
| 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4083 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4084 | { |
| 4085 | PyObject *v; |
| 4086 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4087 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4088 | if (index_type == NULL |
| 4089 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4090 | || index_type == &PySlice_Type) { |
| 4091 | return 1; |
| 4092 | } |
| 4093 | |
| 4094 | switch (e->kind) { |
| 4095 | case Constant_kind: |
| 4096 | v = e->v.Constant.value; |
| 4097 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4098 | return 1; |
| 4099 | } |
| 4100 | /* fall through */ |
| 4101 | case Tuple_kind: |
| 4102 | case List_kind: |
| 4103 | case ListComp_kind: |
| 4104 | case JoinedStr_kind: |
| 4105 | case FormattedValue_kind: |
| 4106 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4107 | "not %.200s; " |
| 4108 | "perhaps you missed a comma?", |
| 4109 | infer_type(e)->tp_name, |
| 4110 | index_type->tp_name); |
| 4111 | default: |
| 4112 | return 1; |
| 4113 | } |
| 4114 | } |
| 4115 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4116 | // 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] | 4117 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4118 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4119 | { |
| 4120 | Py_ssize_t argsl, i; |
| 4121 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4122 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4123 | |
| 4124 | /* Check that the call node is an attribute access, and that |
| 4125 | the call doesn't have keyword parameters. */ |
| 4126 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4127 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4128 | return -1; |
| 4129 | |
| 4130 | /* Check that there are no *varargs types of arguments. */ |
| 4131 | argsl = asdl_seq_LEN(args); |
| 4132 | for (i = 0; i < argsl; i++) { |
| 4133 | expr_ty elt = asdl_seq_GET(args, i); |
| 4134 | if (elt->kind == Starred_kind) { |
| 4135 | return -1; |
| 4136 | } |
| 4137 | } |
| 4138 | |
| 4139 | /* Alright, we can optimize the code. */ |
| 4140 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4141 | int old_lineno = c->u->u_lineno; |
| 4142 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4143 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4144 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4145 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4146 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4147 | return 1; |
| 4148 | } |
| 4149 | |
| 4150 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4151 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4152 | { |
| 4153 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4154 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4155 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4156 | if (key->arg == NULL) { |
| 4157 | continue; |
| 4158 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4159 | if (forbidden_name(c, key->arg, Store)) { |
| 4160 | return -1; |
| 4161 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4162 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4163 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4164 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4165 | c->u->u_col_offset = other->col_offset; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4166 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4167 | return -1; |
| 4168 | } |
| 4169 | } |
| 4170 | } |
| 4171 | return 0; |
| 4172 | } |
| 4173 | |
| 4174 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4175 | compiler_call(struct compiler *c, expr_ty e) |
| 4176 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4177 | int ret = maybe_optimize_method_call(c, e); |
| 4178 | if (ret >= 0) { |
| 4179 | return ret; |
| 4180 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4181 | if (!check_caller(c, e->v.Call.func)) { |
| 4182 | return 0; |
| 4183 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | VISIT(c, expr, e->v.Call.func); |
| 4185 | return compiler_call_helper(c, 0, |
| 4186 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4187 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4190 | static int |
| 4191 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4192 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4193 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4194 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4195 | 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] | 4196 | return 1; |
| 4197 | } |
| 4198 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4199 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4200 | static int |
| 4201 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4202 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4203 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4204 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4205 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4206 | Convert the conversion char to 3 bits: |
| 4207 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4208 | !s : 001 0x1 FVC_STR |
| 4209 | !r : 010 0x2 FVC_REPR |
| 4210 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4211 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4212 | next bit is whether or not we have a format spec: |
| 4213 | yes : 100 0x4 |
| 4214 | no : 000 0x0 |
| 4215 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4216 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4217 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4218 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4219 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4220 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4221 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4222 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4223 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4224 | case 's': oparg = FVC_STR; break; |
| 4225 | case 'r': oparg = FVC_REPR; break; |
| 4226 | case 'a': oparg = FVC_ASCII; break; |
| 4227 | case -1: oparg = FVC_NONE; break; |
| 4228 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4229 | PyErr_Format(PyExc_SystemError, |
| 4230 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4231 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4232 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4233 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4234 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4235 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4236 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4237 | } |
| 4238 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4239 | /* And push our opcode and oparg */ |
| 4240 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4241 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4242 | return 1; |
| 4243 | } |
| 4244 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4245 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4246 | 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] | 4247 | { |
| 4248 | Py_ssize_t i, n = end - begin; |
| 4249 | keyword_ty kw; |
| 4250 | PyObject *keys, *key; |
| 4251 | assert(n > 0); |
| 4252 | if (n > 1) { |
| 4253 | for (i = begin; i < end; i++) { |
| 4254 | kw = asdl_seq_GET(keywords, i); |
| 4255 | VISIT(c, expr, kw->value); |
| 4256 | } |
| 4257 | keys = PyTuple_New(n); |
| 4258 | if (keys == NULL) { |
| 4259 | return 0; |
| 4260 | } |
| 4261 | for (i = begin; i < end; i++) { |
| 4262 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4263 | Py_INCREF(key); |
| 4264 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4265 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4266 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4267 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4268 | } |
| 4269 | else { |
| 4270 | /* a for loop only executes once */ |
| 4271 | for (i = begin; i < end; i++) { |
| 4272 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4273 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4274 | VISIT(c, expr, kw->value); |
| 4275 | } |
| 4276 | ADDOP_I(c, BUILD_MAP, n); |
| 4277 | } |
| 4278 | return 1; |
| 4279 | } |
| 4280 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4281 | /* shared code between compiler_call and compiler_class */ |
| 4282 | static int |
| 4283 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4284 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4285 | asdl_expr_seq *args, |
| 4286 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4287 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4288 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4289 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4290 | if (validate_keywords(c, keywords) == -1) { |
| 4291 | return 0; |
| 4292 | } |
| 4293 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4294 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4295 | nkwelts = asdl_seq_LEN(keywords); |
| 4296 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4297 | for (i = 0; i < nelts; i++) { |
| 4298 | expr_ty elt = asdl_seq_GET(args, i); |
| 4299 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4300 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4301 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4302 | } |
| 4303 | for (i = 0; i < nkwelts; i++) { |
| 4304 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4305 | if (kw->arg == NULL) { |
| 4306 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4307 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4308 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4309 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4310 | /* No * or ** args, so can use faster calling sequence */ |
| 4311 | for (i = 0; i < nelts; i++) { |
| 4312 | expr_ty elt = asdl_seq_GET(args, i); |
| 4313 | assert(elt->kind != Starred_kind); |
| 4314 | VISIT(c, expr, elt); |
| 4315 | } |
| 4316 | if (nkwelts) { |
| 4317 | PyObject *names; |
| 4318 | VISIT_SEQ(c, keyword, keywords); |
| 4319 | names = PyTuple_New(nkwelts); |
| 4320 | if (names == NULL) { |
| 4321 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4322 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4323 | for (i = 0; i < nkwelts; i++) { |
| 4324 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4325 | Py_INCREF(kw->arg); |
| 4326 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4327 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4328 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4329 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4330 | return 1; |
| 4331 | } |
| 4332 | else { |
| 4333 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4334 | return 1; |
| 4335 | } |
| 4336 | |
| 4337 | ex_call: |
| 4338 | |
| 4339 | /* Do positional arguments. */ |
| 4340 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4341 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4342 | } |
| 4343 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4344 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4345 | return 0; |
| 4346 | } |
| 4347 | /* Then keyword arguments */ |
| 4348 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4349 | /* Has a new dict been pushed */ |
| 4350 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4351 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4352 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4353 | for (i = 0; i < nkwelts; i++) { |
| 4354 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4355 | if (kw->arg == NULL) { |
| 4356 | /* A keyword argument unpacking. */ |
| 4357 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4358 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4359 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4360 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4361 | if (have_dict) { |
| 4362 | ADDOP_I(c, DICT_MERGE, 1); |
| 4363 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4364 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4365 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4366 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4367 | if (!have_dict) { |
| 4368 | ADDOP_I(c, BUILD_MAP, 0); |
| 4369 | have_dict = 1; |
| 4370 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4371 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4372 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4373 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4374 | else { |
| 4375 | nseen++; |
| 4376 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4377 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4378 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4379 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4380 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4381 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4382 | } |
| 4383 | if (have_dict) { |
| 4384 | ADDOP_I(c, DICT_MERGE, 1); |
| 4385 | } |
| 4386 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4387 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4388 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4390 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4391 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4394 | |
| 4395 | /* List and set comprehensions and generator expressions work by creating a |
| 4396 | nested function to perform the actual iteration. This means that the |
| 4397 | iteration variables don't leak into the current scope. |
| 4398 | The defined function is called immediately following its definition, with the |
| 4399 | result of that call being the result of the expression. |
| 4400 | The LC/SC version returns the populated container, while the GE version is |
| 4401 | flagged in symtable.c as a generator, so it returns the generator object |
| 4402 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4403 | |
| 4404 | Possible cleanups: |
| 4405 | - iterate over the generator sequence instead of using recursion |
| 4406 | */ |
| 4407 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4408 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4409 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4411 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4412 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4414 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4415 | comprehension_ty gen; |
| 4416 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4417 | if (gen->is_async) { |
| 4418 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4419 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4420 | } else { |
| 4421 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4422 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | static int |
| 4427 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4428 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4429 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4430 | expr_ty elt, expr_ty val, int type) |
| 4431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4432 | /* generate code for the iterator, then each of the ifs, |
| 4433 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | comprehension_ty gen; |
| 4436 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4437 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | start = compiler_new_block(c); |
| 4440 | skip = compiler_new_block(c); |
| 4441 | if_cleanup = compiler_new_block(c); |
| 4442 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4445 | anchor == NULL) |
| 4446 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4448 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
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 | if (gen_index == 0) { |
| 4451 | /* Receive outermost iter as an implicit argument */ |
| 4452 | c->u->u_argcount = 1; |
| 4453 | ADDOP_I(c, LOAD_FAST, 0); |
| 4454 | } |
| 4455 | else { |
| 4456 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4457 | /* Fast path for the temporary variable assignment idiom: |
| 4458 | for y in [f(x)] |
| 4459 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4460 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4461 | switch (gen->iter->kind) { |
| 4462 | case List_kind: |
| 4463 | elts = gen->iter->v.List.elts; |
| 4464 | break; |
| 4465 | case Tuple_kind: |
| 4466 | elts = gen->iter->v.Tuple.elts; |
| 4467 | break; |
| 4468 | default: |
| 4469 | elts = NULL; |
| 4470 | } |
| 4471 | if (asdl_seq_LEN(elts) == 1) { |
| 4472 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4473 | if (elt->kind != Starred_kind) { |
| 4474 | VISIT(c, expr, elt); |
| 4475 | start = NULL; |
| 4476 | } |
| 4477 | } |
| 4478 | if (start) { |
| 4479 | VISIT(c, expr, gen->iter); |
| 4480 | ADDOP(c, GET_ITER); |
| 4481 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4483 | if (start) { |
| 4484 | depth++; |
| 4485 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4486 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4487 | NEXT_BLOCK(c); |
| 4488 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | /* XXX this needs to be cleaned up...a lot! */ |
| 4492 | n = asdl_seq_LEN(gen->ifs); |
| 4493 | for (i = 0; i < n; i++) { |
| 4494 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4495 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4496 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | NEXT_BLOCK(c); |
| 4498 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4501 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4502 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | elt, val, type)) |
| 4504 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | /* only append after the last for generator */ |
| 4507 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4508 | /* comprehension specific code */ |
| 4509 | switch (type) { |
| 4510 | case COMP_GENEXP: |
| 4511 | VISIT(c, expr, elt); |
| 4512 | ADDOP(c, YIELD_VALUE); |
| 4513 | ADDOP(c, POP_TOP); |
| 4514 | break; |
| 4515 | case COMP_LISTCOMP: |
| 4516 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4517 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | break; |
| 4519 | case COMP_SETCOMP: |
| 4520 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4521 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | break; |
| 4523 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4524 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4525 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4527 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4528 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4529 | break; |
| 4530 | default: |
| 4531 | return 0; |
| 4532 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4534 | compiler_use_next_block(c, skip); |
| 4535 | } |
| 4536 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4537 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4538 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4539 | compiler_use_next_block(c, anchor); |
| 4540 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | |
| 4542 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4546 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4547 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4548 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4549 | expr_ty elt, expr_ty val, int type) |
| 4550 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4551 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4552 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4553 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4554 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4555 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4556 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4557 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4558 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | return 0; |
| 4560 | } |
| 4561 | |
| 4562 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4563 | |
| 4564 | if (gen_index == 0) { |
| 4565 | /* Receive outermost iter as an implicit argument */ |
| 4566 | c->u->u_argcount = 1; |
| 4567 | ADDOP_I(c, LOAD_FAST, 0); |
| 4568 | } |
| 4569 | else { |
| 4570 | /* Sub-iter - calculate on the fly */ |
| 4571 | VISIT(c, expr, gen->iter); |
| 4572 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4573 | } |
| 4574 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4575 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4576 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4577 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4578 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4579 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4580 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4581 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4582 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4583 | |
| 4584 | n = asdl_seq_LEN(gen->ifs); |
| 4585 | for (i = 0; i < n; i++) { |
| 4586 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4587 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4588 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4589 | NEXT_BLOCK(c); |
| 4590 | } |
| 4591 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4592 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4593 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4594 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4595 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4596 | elt, val, type)) |
| 4597 | return 0; |
| 4598 | |
| 4599 | /* only append after the last for generator */ |
| 4600 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4601 | /* comprehension specific code */ |
| 4602 | switch (type) { |
| 4603 | case COMP_GENEXP: |
| 4604 | VISIT(c, expr, elt); |
| 4605 | ADDOP(c, YIELD_VALUE); |
| 4606 | ADDOP(c, POP_TOP); |
| 4607 | break; |
| 4608 | case COMP_LISTCOMP: |
| 4609 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4610 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4611 | break; |
| 4612 | case COMP_SETCOMP: |
| 4613 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4614 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4615 | break; |
| 4616 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4617 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4618 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4619 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4620 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4621 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4622 | break; |
| 4623 | default: |
| 4624 | return 0; |
| 4625 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4626 | } |
| 4627 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4628 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4629 | |
| 4630 | compiler_use_next_block(c, except); |
| 4631 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4632 | |
| 4633 | return 1; |
| 4634 | } |
| 4635 | |
| 4636 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4637 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4638 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4639 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4642 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4643 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4644 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4645 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4646 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4647 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4648 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4649 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4650 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4651 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4652 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4654 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4658 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4659 | 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] | 4660 | compiler_error(c, "asynchronous comprehension outside of " |
| 4661 | "an asynchronous function"); |
| 4662 | goto error_in_scope; |
| 4663 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | if (type != COMP_GENEXP) { |
| 4666 | int op; |
| 4667 | switch (type) { |
| 4668 | case COMP_LISTCOMP: |
| 4669 | op = BUILD_LIST; |
| 4670 | break; |
| 4671 | case COMP_SETCOMP: |
| 4672 | op = BUILD_SET; |
| 4673 | break; |
| 4674 | case COMP_DICTCOMP: |
| 4675 | op = BUILD_MAP; |
| 4676 | break; |
| 4677 | default: |
| 4678 | PyErr_Format(PyExc_SystemError, |
| 4679 | "unknown comprehension type %d", type); |
| 4680 | goto error_in_scope; |
| 4681 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | ADDOP_I(c, op, 0); |
| 4684 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4685 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4686 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4687 | val, type)) |
| 4688 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | if (type != COMP_GENEXP) { |
| 4691 | ADDOP(c, RETURN_VALUE); |
| 4692 | } |
| 4693 | |
| 4694 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4695 | qualname = c->u->u_qualname; |
| 4696 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4697 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4698 | if (top_level_await && is_async_generator){ |
| 4699 | c->u->u_ste->ste_coroutine = 1; |
| 4700 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4701 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4702 | goto error; |
| 4703 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4704 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4706 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4707 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4708 | Py_DECREF(co); |
| 4709 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4710 | VISIT(c, expr, outermost->iter); |
| 4711 | |
| 4712 | if (outermost->is_async) { |
| 4713 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4714 | } else { |
| 4715 | ADDOP(c, GET_ITER); |
| 4716 | } |
| 4717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4719 | |
| 4720 | if (is_async_generator && type != COMP_GENEXP) { |
| 4721 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4722 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4723 | ADDOP(c, YIELD_FROM); |
| 4724 | } |
| 4725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4726 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4727 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4729 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4730 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4731 | Py_XDECREF(co); |
| 4732 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4736 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4738 | static identifier name; |
| 4739 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4740 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | if (!name) |
| 4742 | return 0; |
| 4743 | } |
| 4744 | assert(e->kind == GeneratorExp_kind); |
| 4745 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4746 | e->v.GeneratorExp.generators, |
| 4747 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4751 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4753 | static identifier name; |
| 4754 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4755 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4756 | if (!name) |
| 4757 | return 0; |
| 4758 | } |
| 4759 | assert(e->kind == ListComp_kind); |
| 4760 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4761 | e->v.ListComp.generators, |
| 4762 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | static int |
| 4766 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4768 | static identifier name; |
| 4769 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4770 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | if (!name) |
| 4772 | return 0; |
| 4773 | } |
| 4774 | assert(e->kind == SetComp_kind); |
| 4775 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4776 | e->v.SetComp.generators, |
| 4777 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | |
| 4781 | static int |
| 4782 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | static identifier name; |
| 4785 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4786 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4787 | if (!name) |
| 4788 | return 0; |
| 4789 | } |
| 4790 | assert(e->kind == DictComp_kind); |
| 4791 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4792 | e->v.DictComp.generators, |
| 4793 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | |
| 4797 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4798 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | VISIT(c, expr, k->value); |
| 4801 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4805 | whether they are true or false. |
| 4806 | |
| 4807 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4808 | */ |
| 4809 | |
| 4810 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4811 | compiler_with_except_finish(struct compiler *c) { |
| 4812 | basicblock *exit; |
| 4813 | exit = compiler_new_block(c); |
| 4814 | if (exit == NULL) |
| 4815 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4816 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4817 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4818 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4819 | compiler_use_next_block(c, exit); |
| 4820 | ADDOP(c, POP_TOP); |
| 4821 | ADDOP(c, POP_TOP); |
| 4822 | ADDOP(c, POP_TOP); |
| 4823 | ADDOP(c, POP_EXCEPT); |
| 4824 | ADDOP(c, POP_TOP); |
| 4825 | return 1; |
| 4826 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4827 | |
| 4828 | /* |
| 4829 | Implements the async with statement. |
| 4830 | |
| 4831 | The semantics outlined in that PEP are as follows: |
| 4832 | |
| 4833 | async with EXPR as VAR: |
| 4834 | BLOCK |
| 4835 | |
| 4836 | It is implemented roughly as: |
| 4837 | |
| 4838 | context = EXPR |
| 4839 | exit = context.__aexit__ # not calling it |
| 4840 | value = await context.__aenter__() |
| 4841 | try: |
| 4842 | VAR = value # if VAR present in the syntax |
| 4843 | BLOCK |
| 4844 | finally: |
| 4845 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4846 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4847 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4848 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4849 | if not (await exit(*exc)): |
| 4850 | raise |
| 4851 | */ |
| 4852 | static int |
| 4853 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4854 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4855 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4856 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4857 | |
| 4858 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4859 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4860 | c->u->u_ste->ste_coroutine = 1; |
| 4861 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4862 | return compiler_error(c, "'async with' outside async function"); |
| 4863 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4864 | |
| 4865 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4866 | final = compiler_new_block(c); |
| 4867 | exit = compiler_new_block(c); |
| 4868 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4869 | return 0; |
| 4870 | |
| 4871 | /* Evaluate EXPR */ |
| 4872 | VISIT(c, expr, item->context_expr); |
| 4873 | |
| 4874 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4875 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4876 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4877 | ADDOP(c, YIELD_FROM); |
| 4878 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4879 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4880 | |
| 4881 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4882 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4883 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4884 | return 0; |
| 4885 | } |
| 4886 | |
| 4887 | if (item->optional_vars) { |
| 4888 | VISIT(c, expr, item->optional_vars); |
| 4889 | } |
| 4890 | else { |
| 4891 | /* Discard result from context.__aenter__() */ |
| 4892 | ADDOP(c, POP_TOP); |
| 4893 | } |
| 4894 | |
| 4895 | pos++; |
| 4896 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4897 | /* BLOCK code */ |
| 4898 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4899 | else if (!compiler_async_with(c, s, pos)) |
| 4900 | return 0; |
| 4901 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4902 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4903 | ADDOP(c, POP_BLOCK); |
| 4904 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4905 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4906 | /* For successful outcome: |
| 4907 | * call __exit__(None, None, None) |
| 4908 | */ |
| 4909 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4910 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4911 | ADDOP(c, GET_AWAITABLE); |
| 4912 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4913 | ADDOP(c, YIELD_FROM); |
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 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4916 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4917 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4918 | |
| 4919 | /* For exceptional outcome: */ |
| 4920 | compiler_use_next_block(c, final); |
| 4921 | |
| 4922 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4923 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4924 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4925 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4926 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4927 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4928 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4929 | return 1; |
| 4930 | } |
| 4931 | |
| 4932 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4933 | /* |
| 4934 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4935 | with EXPR as VAR: |
| 4936 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4937 | is implemented as: |
| 4938 | <code for EXPR> |
| 4939 | SETUP_WITH E |
| 4940 | <code to store to VAR> or POP_TOP |
| 4941 | <code for BLOCK> |
| 4942 | LOAD_CONST (None, None, None) |
| 4943 | CALL_FUNCTION_EX 0 |
| 4944 | JUMP_FORWARD EXIT |
| 4945 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4946 | POP_JUMP_IF_TRUE T: |
| 4947 | RERAISE |
| 4948 | T: POP_TOP * 3 (remove exception from stack) |
| 4949 | POP_EXCEPT |
| 4950 | POP_TOP |
| 4951 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4952 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4953 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4954 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4955 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4956 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4957 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4958 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4959 | |
| 4960 | assert(s->kind == With_kind); |
| 4961 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4962 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4963 | final = compiler_new_block(c); |
| 4964 | exit = compiler_new_block(c); |
| 4965 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4966 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4967 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4968 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4969 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4970 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4971 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4972 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4973 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4974 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4975 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4976 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4979 | if (item->optional_vars) { |
| 4980 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4981 | } |
| 4982 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4983 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4984 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4987 | pos++; |
| 4988 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4989 | /* BLOCK code */ |
| 4990 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4991 | else if (!compiler_with(c, s, pos)) |
| 4992 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4993 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 4994 | |
| 4995 | /* Mark all following code as artificial */ |
| 4996 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4997 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4998 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4999 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5000 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5001 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5002 | /* For successful outcome: |
| 5003 | * call __exit__(None, None, None) |
| 5004 | */ |
| 5005 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5006 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5007 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5008 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5009 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5010 | /* For exceptional outcome: */ |
| 5011 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5012 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5013 | ADDOP(c, WITH_EXCEPT_START); |
| 5014 | compiler_with_except_finish(c); |
| 5015 | |
| 5016 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5017 | return 1; |
| 5018 | } |
| 5019 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5020 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5021 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5024 | case NamedExpr_kind: |
| 5025 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5026 | ADDOP(c, DUP_TOP); |
| 5027 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5028 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5029 | case BoolOp_kind: |
| 5030 | return compiler_boolop(c, e); |
| 5031 | case BinOp_kind: |
| 5032 | VISIT(c, expr, e->v.BinOp.left); |
| 5033 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5034 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5035 | break; |
| 5036 | case UnaryOp_kind: |
| 5037 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5038 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5039 | break; |
| 5040 | case Lambda_kind: |
| 5041 | return compiler_lambda(c, e); |
| 5042 | case IfExp_kind: |
| 5043 | return compiler_ifexp(c, e); |
| 5044 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5045 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5046 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5047 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | case GeneratorExp_kind: |
| 5049 | return compiler_genexp(c, e); |
| 5050 | case ListComp_kind: |
| 5051 | return compiler_listcomp(c, e); |
| 5052 | case SetComp_kind: |
| 5053 | return compiler_setcomp(c, e); |
| 5054 | case DictComp_kind: |
| 5055 | return compiler_dictcomp(c, e); |
| 5056 | case Yield_kind: |
| 5057 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5058 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5059 | if (e->v.Yield.value) { |
| 5060 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5061 | } |
| 5062 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5063 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5064 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5065 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5066 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5067 | case YieldFrom_kind: |
| 5068 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5069 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5070 | |
| 5071 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5072 | return compiler_error(c, "'yield from' inside async function"); |
| 5073 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5074 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5075 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5076 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5077 | ADDOP(c, YIELD_FROM); |
| 5078 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5079 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5080 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5081 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5082 | return compiler_error(c, "'await' outside function"); |
| 5083 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5084 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5085 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5086 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5087 | return compiler_error(c, "'await' outside async function"); |
| 5088 | } |
| 5089 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5090 | |
| 5091 | VISIT(c, expr, e->v.Await.value); |
| 5092 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5093 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5094 | ADDOP(c, YIELD_FROM); |
| 5095 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | case Compare_kind: |
| 5097 | return compiler_compare(c, e); |
| 5098 | case Call_kind: |
| 5099 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5100 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5101 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5102 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5103 | case JoinedStr_kind: |
| 5104 | return compiler_joined_str(c, e); |
| 5105 | case FormattedValue_kind: |
| 5106 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5107 | /* The following exprs can be assignment targets. */ |
| 5108 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5109 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5110 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5111 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5112 | { |
| 5113 | int old_lineno = c->u->u_lineno; |
| 5114 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5115 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5116 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5118 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5120 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5121 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5122 | } |
| 5123 | int old_lineno = c->u->u_lineno; |
| 5124 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5125 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5126 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5127 | break; |
| 5128 | case Del: |
| 5129 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5130 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5131 | } |
| 5132 | break; |
| 5133 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5134 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5135 | case Starred_kind: |
| 5136 | switch (e->v.Starred.ctx) { |
| 5137 | case Store: |
| 5138 | /* In all legitimate cases, the Starred node was already replaced |
| 5139 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5140 | return compiler_error(c, |
| 5141 | "starred assignment target must be in a list or tuple"); |
| 5142 | default: |
| 5143 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5144 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5146 | break; |
| 5147 | case Slice_kind: |
| 5148 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5149 | case Name_kind: |
| 5150 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5151 | /* child nodes of List and Tuple will have expr_context set */ |
| 5152 | case List_kind: |
| 5153 | return compiler_list(c, e); |
| 5154 | case Tuple_kind: |
| 5155 | return compiler_tuple(c, e); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5156 | case MatchAs_kind: |
| 5157 | case MatchOr_kind: |
| 5158 | // Can only occur in patterns, which are handled elsewhere. |
| 5159 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 | } |
| 5161 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5165 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5166 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5167 | int old_lineno = c->u->u_lineno; |
| 5168 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5169 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5170 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5171 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5172 | c->u->u_col_offset = old_col_offset; |
| 5173 | return res; |
| 5174 | } |
| 5175 | |
| 5176 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5177 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5180 | expr_ty e = s->v.AugAssign.target; |
| 5181 | |
| 5182 | int old_lineno = c->u->u_lineno; |
| 5183 | int old_col_offset = c->u->u_col_offset; |
| 5184 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5186 | switch (e->kind) { |
| 5187 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5188 | VISIT(c, expr, e->v.Attribute.value); |
| 5189 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5190 | int old_lineno = c->u->u_lineno; |
| 5191 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5192 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5193 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5194 | break; |
| 5195 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5196 | VISIT(c, expr, e->v.Subscript.value); |
| 5197 | VISIT(c, expr, e->v.Subscript.slice); |
| 5198 | ADDOP(c, DUP_TOP_TWO); |
| 5199 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5200 | break; |
| 5201 | case Name_kind: |
| 5202 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5203 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5204 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5205 | default: |
| 5206 | PyErr_Format(PyExc_SystemError, |
| 5207 | "invalid node type (%d) for augmented assignment", |
| 5208 | e->kind); |
| 5209 | return 0; |
| 5210 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5211 | |
| 5212 | c->u->u_lineno = old_lineno; |
| 5213 | c->u->u_col_offset = old_col_offset; |
| 5214 | |
| 5215 | VISIT(c, expr, s->v.AugAssign.value); |
| 5216 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5217 | |
| 5218 | SET_LOC(c, e); |
| 5219 | |
| 5220 | switch (e->kind) { |
| 5221 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5222 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5223 | ADDOP(c, ROT_TWO); |
| 5224 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5225 | break; |
| 5226 | case Subscript_kind: |
| 5227 | ADDOP(c, ROT_THREE); |
| 5228 | ADDOP(c, STORE_SUBSCR); |
| 5229 | break; |
| 5230 | case Name_kind: |
| 5231 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5232 | default: |
| 5233 | Py_UNREACHABLE(); |
| 5234 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5235 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5239 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5240 | { |
| 5241 | VISIT(c, expr, e); |
| 5242 | ADDOP(c, POP_TOP); |
| 5243 | return 1; |
| 5244 | } |
| 5245 | |
| 5246 | static int |
| 5247 | check_annotation(struct compiler *c, stmt_ty s) |
| 5248 | { |
| 5249 | /* Annotations are only evaluated in a module or class. */ |
| 5250 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5251 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5252 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5253 | } |
| 5254 | return 1; |
| 5255 | } |
| 5256 | |
| 5257 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5258 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5259 | { |
| 5260 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5261 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5262 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5263 | 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] | 5264 | return 0; |
| 5265 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5266 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5267 | return 0; |
| 5268 | } |
| 5269 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5270 | return 0; |
| 5271 | } |
| 5272 | return 1; |
| 5273 | case Tuple_kind: { |
| 5274 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5275 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5276 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5277 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5278 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5279 | return 0; |
| 5280 | } |
| 5281 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5282 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5283 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5284 | default: |
| 5285 | return check_ann_expr(c, e); |
| 5286 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | static int |
| 5290 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5291 | { |
| 5292 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5293 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5294 | |
| 5295 | assert(s->kind == AnnAssign_kind); |
| 5296 | |
| 5297 | /* We perform the actual assignment first. */ |
| 5298 | if (s->v.AnnAssign.value) { |
| 5299 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5300 | VISIT(c, expr, targ); |
| 5301 | } |
| 5302 | switch (targ->kind) { |
| 5303 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5304 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5305 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5306 | /* If we have a simple name in a module or class, store annotation. */ |
| 5307 | if (s->v.AnnAssign.simple && |
| 5308 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5309 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5310 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5311 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5312 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5313 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5314 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5315 | } |
| 5316 | break; |
| 5317 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5318 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5319 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5320 | if (!s->v.AnnAssign.value && |
| 5321 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5322 | return 0; |
| 5323 | } |
| 5324 | break; |
| 5325 | case Subscript_kind: |
| 5326 | if (!s->v.AnnAssign.value && |
| 5327 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5328 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5329 | return 0; |
| 5330 | } |
| 5331 | break; |
| 5332 | default: |
| 5333 | PyErr_Format(PyExc_SystemError, |
| 5334 | "invalid node type (%d) for annotated assignment", |
| 5335 | targ->kind); |
| 5336 | return 0; |
| 5337 | } |
| 5338 | /* Annotation is evaluated last. */ |
| 5339 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5340 | return 0; |
| 5341 | } |
| 5342 | return 1; |
| 5343 | } |
| 5344 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5345 | /* Raises a SyntaxError and returns 0. |
| 5346 | If something goes wrong, a different exception may be raised. |
| 5347 | */ |
| 5348 | |
| 5349 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5350 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5351 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5352 | va_list vargs; |
| 5353 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5354 | va_start(vargs, format); |
| 5355 | #else |
| 5356 | va_start(vargs); |
| 5357 | #endif |
| 5358 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5359 | va_end(vargs); |
| 5360 | if (msg == NULL) { |
| 5361 | return 0; |
| 5362 | } |
| 5363 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5364 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5365 | Py_INCREF(Py_None); |
| 5366 | loc = Py_None; |
| 5367 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5368 | PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename, |
| 5369 | c->u->u_lineno, c->u->u_col_offset + 1, loc); |
| 5370 | Py_DECREF(msg); |
| 5371 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5372 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5373 | } |
| 5374 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5375 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5376 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5377 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5381 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5382 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5383 | and returns 0. |
| 5384 | */ |
| 5385 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5386 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5387 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5388 | va_list vargs; |
| 5389 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5390 | va_start(vargs, format); |
| 5391 | #else |
| 5392 | va_start(vargs); |
| 5393 | #endif |
| 5394 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5395 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5396 | if (msg == NULL) { |
| 5397 | return 0; |
| 5398 | } |
| 5399 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5400 | c->u->u_lineno, NULL, NULL) < 0) |
| 5401 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5402 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5403 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5404 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5405 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5406 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5407 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5408 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5409 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5410 | return 0; |
| 5411 | } |
| 5412 | Py_DECREF(msg); |
| 5413 | return 1; |
| 5414 | } |
| 5415 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5416 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5417 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5418 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5419 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5421 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5422 | if (ctx == Load) { |
| 5423 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5424 | return 0; |
| 5425 | } |
| 5426 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5427 | return 0; |
| 5428 | } |
| 5429 | } |
| 5430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5432 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5433 | case Store: op = STORE_SUBSCR; break; |
| 5434 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5435 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5436 | assert(op); |
| 5437 | VISIT(c, expr, e->v.Subscript.value); |
| 5438 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5439 | ADDOP(c, op); |
| 5440 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
| 5443 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5444 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5446 | int n = 2; |
| 5447 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5449 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5450 | if (s->v.Slice.lower) { |
| 5451 | VISIT(c, expr, s->v.Slice.lower); |
| 5452 | } |
| 5453 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5454 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5455 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5457 | if (s->v.Slice.upper) { |
| 5458 | VISIT(c, expr, s->v.Slice.upper); |
| 5459 | } |
| 5460 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5461 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | if (s->v.Slice.step) { |
| 5465 | n++; |
| 5466 | VISIT(c, expr, s->v.Slice.step); |
| 5467 | } |
| 5468 | ADDOP_I(c, BUILD_SLICE, n); |
| 5469 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5472 | |
| 5473 | // PEP 634: Structural Pattern Matching |
| 5474 | |
| 5475 | // To keep things simple, all compiler_pattern_* routines follow the convention |
| 5476 | // of replacing TOS (the subject for the given pattern) with either True (match) |
| 5477 | // or False (no match). We do this even for irrefutable patterns; the idea is |
| 5478 | // that it's much easier to smooth out any redundant pushing, popping, and |
| 5479 | // jumping in the peephole optimizer than to detect or predict it here. |
| 5480 | |
| 5481 | |
| 5482 | #define WILDCARD_CHECK(N) \ |
| 5483 | ((N)->kind == Name_kind && \ |
| 5484 | _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_")) |
| 5485 | |
| 5486 | |
| 5487 | static int |
| 5488 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5489 | { |
| 5490 | assert(!_PyUnicode_EqualToASCIIString(n, "_")); |
| 5491 | // Can't assign to the same name twice: |
| 5492 | if (pc->stores == NULL) { |
| 5493 | RETURN_IF_FALSE(pc->stores = PySet_New(NULL)); |
| 5494 | } |
| 5495 | else { |
| 5496 | int duplicate = PySet_Contains(pc->stores, n); |
| 5497 | if (duplicate < 0) { |
| 5498 | return 0; |
| 5499 | } |
| 5500 | if (duplicate) { |
| 5501 | const char *e = "multiple assignments to name %R in pattern"; |
| 5502 | return compiler_error(c, e, n); |
| 5503 | } |
| 5504 | } |
| 5505 | RETURN_IF_FALSE(!PySet_Add(pc->stores, n)); |
| 5506 | RETURN_IF_FALSE(compiler_nameop(c, n, Store)); |
| 5507 | return 1; |
| 5508 | } |
| 5509 | |
| 5510 | |
| 5511 | static int |
| 5512 | pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values, |
| 5513 | Py_ssize_t star, pattern_context *pc) |
| 5514 | { |
| 5515 | RETURN_IF_FALSE(unpack_helper(c, values)); |
| 5516 | // We've now got a bunch of new subjects on the stack. If any of them fail |
| 5517 | // to match, we need to pop everything else off, then finally push False. |
| 5518 | // fails is an array of blocks that correspond to the necessary amount of |
| 5519 | // popping for each element: |
| 5520 | basicblock **fails; |
| 5521 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5522 | fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size); |
| 5523 | if (fails == NULL) { |
| 5524 | PyErr_NoMemory(); |
| 5525 | return 0; |
| 5526 | } |
| 5527 | // NOTE: Can't use our nice returning macros anymore: they'll leak memory! |
| 5528 | // goto error on error. |
| 5529 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5530 | fails[i] = compiler_new_block(c); |
| 5531 | if (fails[i] == NULL) { |
| 5532 | goto error; |
| 5533 | } |
| 5534 | } |
| 5535 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5536 | expr_ty value = asdl_seq_GET(values, i); |
| 5537 | if (i == star) { |
| 5538 | assert(value->kind == Starred_kind); |
| 5539 | value = value->v.Starred.value; |
| 5540 | } |
| 5541 | if (!compiler_pattern_subpattern(c, value, pc) || |
| 5542 | !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) || |
| 5543 | compiler_next_block(c) == NULL) |
| 5544 | { |
| 5545 | goto error; |
| 5546 | } |
| 5547 | } |
| 5548 | // Success! |
| 5549 | basicblock *end = compiler_new_block(c); |
| 5550 | if (end == NULL || |
| 5551 | !compiler_addop_load_const(c, Py_True) || |
| 5552 | !compiler_addop_j(c, JUMP_FORWARD, end)) |
| 5553 | { |
| 5554 | goto error; |
| 5555 | } |
| 5556 | // This is where we handle failed sub-patterns. For a sequence pattern like |
| 5557 | // [a, b, c, d], this will look like: |
| 5558 | // fails[0]: POP_TOP |
| 5559 | // fails[1]: POP_TOP |
| 5560 | // fails[2]: POP_TOP |
| 5561 | // fails[3]: LOAD_CONST False |
| 5562 | for (Py_ssize_t i = 0; i < size - 1; i++) { |
| 5563 | compiler_use_next_block(c, fails[i]); |
| 5564 | if (!compiler_addop(c, POP_TOP)) { |
| 5565 | goto error; |
| 5566 | } |
| 5567 | } |
| 5568 | compiler_use_next_block(c, fails[size - 1]); |
| 5569 | if (!compiler_addop_load_const(c, Py_False)) { |
| 5570 | goto error; |
| 5571 | } |
| 5572 | compiler_use_next_block(c, end); |
| 5573 | PyObject_Free(fails); |
| 5574 | return 1; |
| 5575 | error: |
| 5576 | PyObject_Free(fails); |
| 5577 | return 0; |
| 5578 | } |
| 5579 | |
| 5580 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5581 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5582 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5583 | static int |
| 5584 | pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values, |
| 5585 | Py_ssize_t star, pattern_context *pc) |
| 5586 | { |
| 5587 | basicblock *end, *fail_pop_1; |
| 5588 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5589 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5590 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5591 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5592 | expr_ty value = asdl_seq_GET(values, i); |
| 5593 | if (WILDCARD_CHECK(value)) { |
| 5594 | continue; |
| 5595 | } |
| 5596 | if (i == star) { |
| 5597 | assert(value->kind == Starred_kind); |
| 5598 | assert(WILDCARD_CHECK(value->v.Starred.value)); |
| 5599 | continue; |
| 5600 | } |
| 5601 | ADDOP(c, DUP_TOP); |
| 5602 | if (i < star) { |
| 5603 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5604 | } |
| 5605 | else { |
| 5606 | // The subject may not support negative indexing! Compute a |
| 5607 | // nonnegative index: |
| 5608 | ADDOP(c, GET_LEN); |
| 5609 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5610 | ADDOP(c, BINARY_SUBTRACT); |
| 5611 | } |
| 5612 | ADDOP(c, BINARY_SUBSCR); |
| 5613 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5614 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5615 | NEXT_BLOCK(c); |
| 5616 | } |
| 5617 | ADDOP(c, POP_TOP); |
| 5618 | ADDOP_LOAD_CONST(c, Py_True); |
| 5619 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5620 | compiler_use_next_block(c, fail_pop_1); |
| 5621 | ADDOP(c, POP_TOP); |
| 5622 | ADDOP_LOAD_CONST(c, Py_False); |
| 5623 | compiler_use_next_block(c, end); |
| 5624 | return 1; |
| 5625 | } |
| 5626 | |
| 5627 | |
| 5628 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5629 | static int |
| 5630 | compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5631 | { |
| 5632 | int allow_irrefutable = pc->allow_irrefutable; |
| 5633 | pc->allow_irrefutable = 1; |
| 5634 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5635 | pc->allow_irrefutable = allow_irrefutable; |
| 5636 | return 1; |
| 5637 | } |
| 5638 | |
| 5639 | |
| 5640 | static int |
| 5641 | compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5642 | { |
| 5643 | assert(p->kind == MatchAs_kind); |
| 5644 | basicblock *end, *fail_pop_1; |
| 5645 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5646 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5647 | // Need to make a copy for (possibly) storing later: |
| 5648 | ADDOP(c, DUP_TOP); |
| 5649 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
| 5650 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5651 | NEXT_BLOCK(c); |
| 5652 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5653 | ADDOP_LOAD_CONST(c, Py_True); |
| 5654 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5655 | compiler_use_next_block(c, fail_pop_1); |
| 5656 | // Need to pop that unused copy from before: |
| 5657 | ADDOP(c, POP_TOP); |
| 5658 | ADDOP_LOAD_CONST(c, Py_False); |
| 5659 | compiler_use_next_block(c, end); |
| 5660 | return 1; |
| 5661 | } |
| 5662 | |
| 5663 | |
| 5664 | static int |
| 5665 | compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5666 | { |
| 5667 | assert(p->kind == Name_kind); |
| 5668 | assert(p->v.Name.ctx == Store); |
| 5669 | assert(!WILDCARD_CHECK(p)); |
| 5670 | if (!pc->allow_irrefutable) { |
| 5671 | // Whoops, can't have a name capture here! |
| 5672 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5673 | return compiler_error(c, e, p->v.Name.id); |
| 5674 | } |
| 5675 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc)); |
| 5676 | ADDOP_LOAD_CONST(c, Py_True); |
| 5677 | return 1; |
| 5678 | } |
| 5679 | |
| 5680 | |
| 5681 | static int |
| 5682 | compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5683 | { |
| 5684 | asdl_expr_seq *args = p->v.Call.args; |
| 5685 | asdl_keyword_seq *kwargs = p->v.Call.keywords; |
| 5686 | Py_ssize_t nargs = asdl_seq_LEN(args); |
| 5687 | Py_ssize_t nkwargs = asdl_seq_LEN(kwargs); |
| 5688 | if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) { |
| 5689 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5690 | return compiler_error(c, e, p->v.Call.func); |
| 5691 | } |
| 5692 | RETURN_IF_FALSE(!validate_keywords(c, kwargs)); |
| 5693 | basicblock *end, *fail_pop_1; |
| 5694 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5695 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5696 | VISIT(c, expr, p->v.Call.func); |
| 5697 | PyObject *kwnames; |
| 5698 | RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs)); |
| 5699 | Py_ssize_t i; |
| 5700 | for (i = 0; i < nkwargs; i++) { |
| 5701 | PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg; |
| 5702 | Py_INCREF(name); |
| 5703 | PyTuple_SET_ITEM(kwnames, i, name); |
| 5704 | } |
| 5705 | ADDOP_LOAD_CONST_NEW(c, kwnames); |
| 5706 | ADDOP_I(c, MATCH_CLASS, nargs); |
| 5707 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5708 | NEXT_BLOCK(c); |
| 5709 | // TOS is now a tuple of (nargs + nkwargs) attributes. |
| 5710 | for (i = 0; i < nargs + nkwargs; i++) { |
| 5711 | expr_ty arg; |
| 5712 | if (i < nargs) { |
| 5713 | // Positional: |
| 5714 | arg = asdl_seq_GET(args, i); |
| 5715 | } |
| 5716 | else { |
| 5717 | // Keyword: |
| 5718 | arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value; |
| 5719 | } |
| 5720 | if (WILDCARD_CHECK(arg)) { |
| 5721 | continue; |
| 5722 | } |
| 5723 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5724 | ADDOP(c, DUP_TOP); |
| 5725 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5726 | ADDOP(c, BINARY_SUBSCR); |
| 5727 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc)); |
| 5728 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5729 | NEXT_BLOCK(c); |
| 5730 | } |
| 5731 | // Success! Pop the tuple of attributes: |
| 5732 | ADDOP(c, POP_TOP); |
| 5733 | ADDOP_LOAD_CONST(c, Py_True); |
| 5734 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5735 | compiler_use_next_block(c, fail_pop_1); |
| 5736 | ADDOP(c, POP_TOP); |
| 5737 | ADDOP_LOAD_CONST(c, Py_False); |
| 5738 | compiler_use_next_block(c, end); |
| 5739 | return 1; |
| 5740 | } |
| 5741 | |
| 5742 | |
| 5743 | static int |
| 5744 | compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5745 | { |
| 5746 | assert(p->kind == Constant_kind); |
| 5747 | PyObject *v = p->v.Constant.value; |
| 5748 | ADDOP_LOAD_CONST(c, v); |
| 5749 | // Literal True, False, and None are compared by identity. All others use |
| 5750 | // equality: |
| 5751 | ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq); |
| 5752 | return 1; |
| 5753 | } |
| 5754 | |
| 5755 | |
| 5756 | static int |
| 5757 | compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5758 | { |
| 5759 | basicblock *end, *fail_pop_1, *fail_pop_3; |
| 5760 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5761 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5762 | RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c)); |
| 5763 | asdl_expr_seq *keys = p->v.Dict.keys; |
| 5764 | asdl_expr_seq *values = p->v.Dict.values; |
| 5765 | Py_ssize_t size = asdl_seq_LEN(values); |
Ikko Ashimine | 57827f8 | 2021-03-10 19:39:51 +0900 | [diff] [blame] | 5766 | // 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] | 5767 | int star = size ? !asdl_seq_GET(keys, size - 1) : 0; |
| 5768 | ADDOP(c, MATCH_MAPPING); |
| 5769 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5770 | NEXT_BLOCK(c); |
| 5771 | if (!size) { |
| 5772 | // If the pattern is just "{}", we're done! |
| 5773 | ADDOP(c, POP_TOP); |
| 5774 | ADDOP_LOAD_CONST(c, Py_True); |
| 5775 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5776 | compiler_use_next_block(c, fail_pop_1); |
| 5777 | ADDOP(c, POP_TOP); |
| 5778 | ADDOP_LOAD_CONST(c, Py_False); |
| 5779 | compiler_use_next_block(c, end); |
| 5780 | return 1; |
| 5781 | } |
| 5782 | if (size - star) { |
| 5783 | // If the pattern has any keys in it, perform a length check: |
| 5784 | ADDOP(c, GET_LEN); |
| 5785 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star)); |
| 5786 | ADDOP_COMPARE(c, GtE); |
| 5787 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5788 | NEXT_BLOCK(c); |
| 5789 | } |
| 5790 | if (INT_MAX < size - star - 1) { |
| 5791 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 5792 | } |
| 5793 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 5794 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
| 5795 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5796 | expr_ty key = asdl_seq_GET(keys, i); |
| 5797 | if (key == NULL) { |
| 5798 | const char *e = "can't use starred name here " |
| 5799 | "(consider moving to end)"; |
| 5800 | return compiler_error(c, e); |
| 5801 | } |
| 5802 | VISIT(c, expr, key); |
| 5803 | } |
| 5804 | ADDOP_I(c, BUILD_TUPLE, size - star); |
| 5805 | ADDOP(c, MATCH_KEYS); |
| 5806 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5807 | NEXT_BLOCK(c); |
| 5808 | // So far so good. There's now a tuple of values on the stack to match |
| 5809 | // sub-patterns against: |
| 5810 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5811 | expr_ty value = asdl_seq_GET(values, i); |
| 5812 | if (WILDCARD_CHECK(value)) { |
| 5813 | continue; |
| 5814 | } |
| 5815 | ADDOP(c, DUP_TOP); |
| 5816 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5817 | ADDOP(c, BINARY_SUBSCR); |
| 5818 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5819 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5820 | NEXT_BLOCK(c); |
| 5821 | } |
| 5822 | // If we get this far, it's a match! We're done with that tuple of values. |
| 5823 | ADDOP(c, POP_TOP); |
| 5824 | if (star) { |
| 5825 | // If we had a starred name, bind a dict of remaining items to it: |
| 5826 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
| 5827 | PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id; |
| 5828 | RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc)); |
| 5829 | } |
| 5830 | else { |
| 5831 | // Otherwise, we don't care about this tuple of keys anymore: |
| 5832 | ADDOP(c, POP_TOP); |
| 5833 | } |
| 5834 | // Pop the subject: |
| 5835 | ADDOP(c, POP_TOP); |
| 5836 | ADDOP_LOAD_CONST(c, Py_True); |
| 5837 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5838 | // The top two items are a tuple of values or None, followed by a tuple of |
| 5839 | // keys. Pop them both: |
| 5840 | compiler_use_next_block(c, fail_pop_3); |
| 5841 | ADDOP(c, POP_TOP); |
| 5842 | ADDOP(c, POP_TOP); |
| 5843 | compiler_use_next_block(c, fail_pop_1); |
| 5844 | // Pop the subject: |
| 5845 | ADDOP(c, POP_TOP); |
| 5846 | ADDOP_LOAD_CONST(c, Py_False); |
| 5847 | compiler_use_next_block(c, end); |
| 5848 | return 1; |
| 5849 | } |
| 5850 | |
| 5851 | |
| 5852 | static int |
| 5853 | compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5854 | { |
| 5855 | assert(p->kind == MatchOr_kind); |
| 5856 | // control is the set of names bound by the first alternative. If all of the |
| 5857 | // others bind the same names (they should), then this becomes pc->stores. |
| 5858 | PyObject *control = NULL; |
| 5859 | basicblock *end, *pass_pop_1; |
| 5860 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5861 | RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c)); |
| 5862 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 5863 | assert(size > 1); |
| 5864 | // We're going to be messing with pc. Keep the original info handy: |
| 5865 | PyObject *stores_init = pc->stores; |
| 5866 | int allow_irrefutable = pc->allow_irrefutable; |
| 5867 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5868 | // NOTE: Can't use our nice returning macros in here: they'll leak sets! |
| 5869 | expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
| 5870 | pc->stores = PySet_New(stores_init); |
| 5871 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 5872 | int is_last = i == size - 1; |
| 5873 | pc->allow_irrefutable = allow_irrefutable && is_last; |
| 5874 | SET_LOC(c, alt); |
| 5875 | if (pc->stores == NULL || |
| 5876 | // Only copy the subject if we're *not* on the last alternative: |
| 5877 | (!is_last && !compiler_addop(c, DUP_TOP)) || |
| 5878 | !compiler_pattern(c, alt, pc) || |
| 5879 | // Only jump if we're *not* on the last alternative: |
| 5880 | (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) || |
| 5881 | !compiler_next_block(c)) |
| 5882 | { |
| 5883 | goto fail; |
| 5884 | } |
| 5885 | if (!i) { |
| 5886 | // If this is the first alternative, save its stores as a "control" |
| 5887 | // for the others (they can't bind a different set of names): |
| 5888 | control = pc->stores; |
| 5889 | continue; |
| 5890 | } |
| 5891 | if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) { |
| 5892 | // Otherwise, check to see if we differ from the control set: |
| 5893 | PyObject *diff = PyNumber_InPlaceXor(pc->stores, control); |
| 5894 | if (diff == NULL) { |
| 5895 | goto fail; |
| 5896 | } |
| 5897 | if (PySet_GET_SIZE(diff)) { |
| 5898 | // The names differ! Raise. |
| 5899 | Py_DECREF(diff); |
| 5900 | compiler_error(c, "alternative patterns bind different names"); |
| 5901 | goto fail; |
| 5902 | } |
| 5903 | Py_DECREF(diff); |
| 5904 | } |
| 5905 | Py_DECREF(pc->stores); |
| 5906 | } |
| 5907 | Py_XDECREF(stores_init); |
| 5908 | // Update pc->stores and restore pc->allow_irrefutable: |
| 5909 | pc->stores = control; |
| 5910 | pc->allow_irrefutable = allow_irrefutable; |
| 5911 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5912 | compiler_use_next_block(c, pass_pop_1); |
| 5913 | ADDOP(c, POP_TOP); |
| 5914 | ADDOP_LOAD_CONST(c, Py_True); |
| 5915 | compiler_use_next_block(c, end); |
| 5916 | return 1; |
| 5917 | fail: |
| 5918 | Py_XDECREF(stores_init); |
| 5919 | Py_XDECREF(control); |
| 5920 | return 0; |
| 5921 | } |
| 5922 | |
| 5923 | |
| 5924 | static int |
| 5925 | compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5926 | { |
| 5927 | assert(p->kind == List_kind || p->kind == Tuple_kind); |
| 5928 | asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts |
| 5929 | : p->v.List.elts; |
| 5930 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5931 | Py_ssize_t star = -1; |
| 5932 | int only_wildcard = 1; |
| 5933 | int star_wildcard = 0; |
| 5934 | // Find a starred name, if it exists. There may be at most one: |
| 5935 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5936 | expr_ty value = asdl_seq_GET(values, i); |
| 5937 | if (value->kind == Starred_kind) { |
| 5938 | value = value->v.Starred.value; |
| 5939 | if (star >= 0) { |
| 5940 | const char *e = "multiple starred names in sequence pattern"; |
| 5941 | return compiler_error(c, e); |
| 5942 | } |
| 5943 | star_wildcard = WILDCARD_CHECK(value); |
| 5944 | star = i; |
| 5945 | } |
| 5946 | only_wildcard &= WILDCARD_CHECK(value); |
| 5947 | } |
| 5948 | basicblock *end, *fail_pop_1; |
| 5949 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5950 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5951 | ADDOP(c, MATCH_SEQUENCE); |
| 5952 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5953 | NEXT_BLOCK(c); |
| 5954 | if (star < 0) { |
| 5955 | // No star: len(subject) == size |
| 5956 | ADDOP(c, GET_LEN); |
| 5957 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 5958 | ADDOP_COMPARE(c, Eq); |
| 5959 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5960 | NEXT_BLOCK(c); |
| 5961 | } |
| 5962 | else if (size > 1) { |
| 5963 | // Star: len(subject) >= size - 1 |
| 5964 | ADDOP(c, GET_LEN); |
| 5965 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 5966 | ADDOP_COMPARE(c, GtE); |
| 5967 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5968 | NEXT_BLOCK(c); |
| 5969 | } |
| 5970 | if (only_wildcard) { |
| 5971 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 5972 | ADDOP(c, POP_TOP); |
| 5973 | ADDOP_LOAD_CONST(c, Py_True); |
| 5974 | } |
| 5975 | else if (star_wildcard) { |
| 5976 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc)); |
| 5977 | } |
| 5978 | else { |
| 5979 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc)); |
| 5980 | } |
| 5981 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5982 | compiler_use_next_block(c, fail_pop_1); |
| 5983 | ADDOP(c, POP_TOP) |
| 5984 | ADDOP_LOAD_CONST(c, Py_False); |
| 5985 | compiler_use_next_block(c, end); |
| 5986 | return 1; |
| 5987 | } |
| 5988 | |
| 5989 | |
| 5990 | static int |
| 5991 | compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5992 | { |
| 5993 | assert(p->kind == Attribute_kind); |
| 5994 | assert(p->v.Attribute.ctx == Load); |
| 5995 | VISIT(c, expr, p); |
| 5996 | ADDOP_COMPARE(c, Eq); |
| 5997 | return 1; |
| 5998 | } |
| 5999 | |
| 6000 | |
| 6001 | static int |
| 6002 | compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6003 | { |
| 6004 | assert(p->kind == Name_kind); |
| 6005 | assert(p->v.Name.ctx == Store); |
| 6006 | assert(WILDCARD_CHECK(p)); |
| 6007 | if (!pc->allow_irrefutable) { |
| 6008 | // Whoops, can't have a wildcard here! |
| 6009 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 6010 | return compiler_error(c, e); |
| 6011 | } |
| 6012 | ADDOP(c, POP_TOP); |
| 6013 | ADDOP_LOAD_CONST(c, Py_True); |
| 6014 | return 1; |
| 6015 | } |
| 6016 | |
| 6017 | |
| 6018 | static int |
| 6019 | compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6020 | { |
| 6021 | SET_LOC(c, p); |
| 6022 | switch (p->kind) { |
| 6023 | case Attribute_kind: |
| 6024 | return compiler_pattern_value(c, p, pc); |
| 6025 | case BinOp_kind: |
| 6026 | // Because we allow "2+2j", things like "2+2" make it this far: |
| 6027 | return compiler_error(c, "patterns cannot include operators"); |
| 6028 | case Call_kind: |
| 6029 | return compiler_pattern_class(c, p, pc); |
| 6030 | case Constant_kind: |
| 6031 | return compiler_pattern_literal(c, p, pc); |
| 6032 | case Dict_kind: |
| 6033 | return compiler_pattern_mapping(c, p, pc); |
| 6034 | case JoinedStr_kind: |
| 6035 | // Because we allow strings, f-strings make it this far: |
| 6036 | return compiler_error(c, "patterns cannot include f-strings"); |
| 6037 | case List_kind: |
| 6038 | case Tuple_kind: |
| 6039 | return compiler_pattern_sequence(c, p, pc); |
| 6040 | case MatchAs_kind: |
| 6041 | return compiler_pattern_as(c, p, pc); |
| 6042 | case MatchOr_kind: |
| 6043 | return compiler_pattern_or(c, p, pc); |
| 6044 | case Name_kind: |
| 6045 | if (WILDCARD_CHECK(p)) { |
| 6046 | return compiler_pattern_wildcard(c, p, pc); |
| 6047 | } |
| 6048 | return compiler_pattern_capture(c, p, pc); |
| 6049 | default: |
| 6050 | Py_UNREACHABLE(); |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | |
| 6055 | static int |
| 6056 | compiler_match(struct compiler *c, stmt_ty s) |
| 6057 | { |
| 6058 | VISIT(c, expr, s->v.Match.subject); |
| 6059 | basicblock *next, *end; |
| 6060 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6061 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
| 6062 | assert(cases); |
| 6063 | pattern_context pc; |
| 6064 | // We use pc.stores to track: |
| 6065 | // - Repeated name assignments in the same pattern. |
| 6066 | // - Different name assignments in alternatives. |
| 6067 | // It's a set of names, but we don't create it until it's needed: |
| 6068 | pc.stores = NULL; |
| 6069 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6070 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6071 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6072 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6073 | SET_LOC(c, m->pattern); |
| 6074 | RETURN_IF_FALSE(next = compiler_new_block(c)); |
| 6075 | // If pc.allow_irrefutable is 0, any name captures against our subject |
| 6076 | // will raise. Irrefutable cases must be either guarded, last, or both: |
| 6077 | pc.allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6078 | // Only copy the subject if we're *not* on the last case: |
| 6079 | if (i != cases - has_default - 1) { |
| 6080 | ADDOP(c, DUP_TOP); |
| 6081 | } |
| 6082 | int result = compiler_pattern(c, m->pattern, &pc); |
| 6083 | Py_CLEAR(pc.stores); |
| 6084 | RETURN_IF_FALSE(result); |
| 6085 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next); |
| 6086 | NEXT_BLOCK(c); |
| 6087 | if (m->guard) { |
| 6088 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0)); |
| 6089 | } |
| 6090 | // Success! Pop the subject off, we're done with it: |
| 6091 | if (i != cases - has_default - 1) { |
| 6092 | ADDOP(c, POP_TOP); |
| 6093 | } |
| 6094 | VISIT_SEQ(c, stmt, m->body); |
| 6095 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6096 | compiler_use_next_block(c, next); |
| 6097 | } |
| 6098 | if (has_default) { |
| 6099 | if (cases == 1) { |
| 6100 | // No matches. Done with the subject: |
| 6101 | ADDOP(c, POP_TOP); |
| 6102 | } |
| 6103 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6104 | // pushing and popping in the loop above: |
| 6105 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6106 | SET_LOC(c, m->pattern); |
| 6107 | if (m->guard) { |
| 6108 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6109 | } |
| 6110 | VISIT_SEQ(c, stmt, m->body); |
| 6111 | } |
| 6112 | compiler_use_next_block(c, end); |
| 6113 | return 1; |
| 6114 | } |
| 6115 | |
| 6116 | |
| 6117 | #undef WILDCARD_CHECK |
| 6118 | |
| 6119 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6120 | /* End of the compiler section, beginning of the assembler section */ |
| 6121 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6122 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6123 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6124 | |
| 6125 | XXX must handle implicit jumps from one block to next |
| 6126 | */ |
| 6127 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6128 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6129 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6130 | int a_offset; /* offset into bytecode */ |
| 6131 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6132 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6133 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6134 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6135 | int a_lineno; /* lineno of last emitted instruction */ |
| 6136 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6137 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6138 | }; |
| 6139 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6140 | Py_LOCAL_INLINE(void) |
| 6141 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6142 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6143 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6144 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6145 | assert(b->b_startdepth < 0); |
| 6146 | b->b_startdepth = depth; |
| 6147 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6148 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | /* Find the flow path that needs the largest stack. We assume that |
| 6152 | * cycles in the flow graph have no net effect on the stack depth. |
| 6153 | */ |
| 6154 | static int |
| 6155 | stackdepth(struct compiler *c) |
| 6156 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6157 | basicblock *b, *entryblock = NULL; |
| 6158 | basicblock **stack, **sp; |
| 6159 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6160 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6161 | b->b_startdepth = INT_MIN; |
| 6162 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6163 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6164 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame^] | 6165 | assert(entryblock!= NULL); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6166 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6167 | if (!stack) { |
| 6168 | PyErr_NoMemory(); |
| 6169 | return -1; |
| 6170 | } |
| 6171 | |
| 6172 | sp = stack; |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6173 | if (c->u->u_ste->ste_generator || c->u->u_ste->ste_coroutine) { |
| 6174 | stackdepth_push(&sp, entryblock, 1); |
| 6175 | } else { |
| 6176 | stackdepth_push(&sp, entryblock, 0); |
| 6177 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6178 | while (sp != stack) { |
| 6179 | b = *--sp; |
| 6180 | int depth = b->b_startdepth; |
| 6181 | assert(depth >= 0); |
| 6182 | basicblock *next = b->b_next; |
| 6183 | for (int i = 0; i < b->b_iused; i++) { |
| 6184 | struct instr *instr = &b->b_instr[i]; |
| 6185 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6186 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6187 | PyErr_Format(PyExc_SystemError, |
| 6188 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6189 | instr->i_opcode, instr->i_oparg); |
| 6190 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6191 | } |
| 6192 | int new_depth = depth + effect; |
| 6193 | if (new_depth > maxdepth) { |
| 6194 | maxdepth = new_depth; |
| 6195 | } |
| 6196 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6197 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6198 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6199 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6200 | int target_depth = depth + effect; |
| 6201 | if (target_depth > maxdepth) { |
| 6202 | maxdepth = target_depth; |
| 6203 | } |
| 6204 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6205 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6206 | } |
| 6207 | depth = new_depth; |
| 6208 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6209 | instr->i_opcode == JUMP_FORWARD || |
| 6210 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6211 | instr->i_opcode == RAISE_VARARGS || |
| 6212 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6213 | { |
| 6214 | /* remaining code is dead */ |
| 6215 | next = NULL; |
| 6216 | break; |
| 6217 | } |
| 6218 | } |
| 6219 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6220 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6221 | stackdepth_push(&sp, next, depth); |
| 6222 | } |
| 6223 | } |
| 6224 | PyObject_Free(stack); |
| 6225 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | static int |
| 6229 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6231 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6232 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6233 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6234 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6235 | if (a->a_bytecode == NULL) { |
| 6236 | goto error; |
| 6237 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6238 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6239 | if (a->a_lnotab == NULL) { |
| 6240 | goto error; |
| 6241 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6242 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6243 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6244 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6245 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6246 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6247 | error: |
| 6248 | Py_XDECREF(a->a_bytecode); |
| 6249 | Py_XDECREF(a->a_lnotab); |
| 6250 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | static void |
| 6254 | assemble_free(struct assembler *a) |
| 6255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6256 | Py_XDECREF(a->a_bytecode); |
| 6257 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6260 | static int |
| 6261 | blocksize(basicblock *b) |
| 6262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6263 | int i; |
| 6264 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6266 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6267 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6268 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6271 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6272 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6273 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6274 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6275 | if (a->a_lnotab_off + 2 >= len) { |
| 6276 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6277 | return 0; |
| 6278 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6279 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6280 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6281 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6282 | *lnotab++ = bdelta; |
| 6283 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6284 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6287 | /* Appends a range to the end of the line number table. See |
| 6288 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6289 | |
| 6290 | static int |
| 6291 | assemble_line_range(struct assembler *a) |
| 6292 | { |
| 6293 | int ldelta, bdelta; |
| 6294 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6295 | if (bdelta == 0) { |
| 6296 | return 1; |
| 6297 | } |
| 6298 | if (a->a_lineno < 0) { |
| 6299 | ldelta = -128; |
| 6300 | } |
| 6301 | else { |
| 6302 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6303 | a->a_prevlineno = a->a_lineno; |
| 6304 | while (ldelta > 127) { |
| 6305 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6306 | return 0; |
| 6307 | } |
| 6308 | ldelta -= 127; |
| 6309 | } |
| 6310 | while (ldelta < -127) { |
| 6311 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6312 | return 0; |
| 6313 | } |
| 6314 | ldelta += 127; |
| 6315 | } |
| 6316 | } |
| 6317 | assert(-128 <= ldelta && ldelta < 128); |
| 6318 | while (bdelta > 254) { |
| 6319 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6320 | return 0; |
| 6321 | } |
| 6322 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6323 | bdelta -= 254; |
| 6324 | } |
| 6325 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6326 | return 0; |
| 6327 | } |
| 6328 | a->a_lineno_start = a->a_offset; |
| 6329 | return 1; |
| 6330 | } |
| 6331 | |
| 6332 | static int |
| 6333 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6334 | { |
| 6335 | if (i->i_lineno == a->a_lineno) { |
| 6336 | return 1; |
| 6337 | } |
| 6338 | if (!assemble_line_range(a)) { |
| 6339 | return 0; |
| 6340 | } |
| 6341 | a->a_lineno = i->i_lineno; |
| 6342 | return 1; |
| 6343 | } |
| 6344 | |
| 6345 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6346 | /* assemble_emit() |
| 6347 | Extend the bytecode with a new instruction. |
| 6348 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6349 | */ |
| 6350 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6351 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6352 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6353 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6354 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6355 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6356 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6357 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6358 | arg = i->i_oparg; |
| 6359 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6360 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6361 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6362 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6363 | if (len > PY_SSIZE_T_MAX / 2) |
| 6364 | return 0; |
| 6365 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6366 | return 0; |
| 6367 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6368 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6370 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6371 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6374 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6375 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6377 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6378 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6379 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6381 | /* Compute the size of each block and fixup jump args. |
| 6382 | Replace block pointer with position in bytecode. */ |
| 6383 | do { |
| 6384 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6385 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | bsize = blocksize(b); |
| 6387 | b->b_offset = totsize; |
| 6388 | totsize += bsize; |
| 6389 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6390 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6391 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6392 | bsize = b->b_offset; |
| 6393 | for (i = 0; i < b->b_iused; i++) { |
| 6394 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6395 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6396 | /* Relative jumps are computed relative to |
| 6397 | the instruction pointer after fetching |
| 6398 | the jump instruction. |
| 6399 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6400 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6401 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6402 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6403 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6404 | instr->i_oparg -= bsize; |
| 6405 | } |
| 6406 | if (instrsize(instr->i_oparg) != isize) { |
| 6407 | extended_arg_recompile = 1; |
| 6408 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6409 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6410 | } |
| 6411 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6413 | /* XXX: This is an awful hack that could hurt performance, but |
| 6414 | on the bright side it should work until we come up |
| 6415 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6417 | The issue is that in the first loop blocksize() is called |
| 6418 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6419 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6420 | i_oparg is calculated in the second loop above. |
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 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6423 | The only EXTENDED_ARGs that could be popping up are |
| 6424 | ones in jump instructions. So this should converge |
| 6425 | fairly quickly. |
| 6426 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6427 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6430 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6431 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6433 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6434 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6436 | tuple = PyTuple_New(size); |
| 6437 | if (tuple == NULL) |
| 6438 | return NULL; |
| 6439 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6440 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6441 | Py_INCREF(k); |
| 6442 | assert((i - offset) < size); |
| 6443 | assert((i - offset) >= 0); |
| 6444 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6445 | } |
| 6446 | return tuple; |
| 6447 | } |
| 6448 | |
| 6449 | static PyObject * |
| 6450 | consts_dict_keys_inorder(PyObject *dict) |
| 6451 | { |
| 6452 | PyObject *consts, *k, *v; |
| 6453 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6454 | |
| 6455 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6456 | if (consts == NULL) |
| 6457 | return NULL; |
| 6458 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6459 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6460 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6461 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6462 | * the object we want is always second. */ |
| 6463 | if (PyTuple_CheckExact(k)) { |
| 6464 | k = PyTuple_GET_ITEM(k, 1); |
| 6465 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6466 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6467 | assert(i < size); |
| 6468 | assert(i >= 0); |
| 6469 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6470 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6471 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6474 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6475 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6477 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6478 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6479 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6480 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6481 | if (ste->ste_nested) |
| 6482 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6483 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6484 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6485 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6486 | flags |= CO_COROUTINE; |
| 6487 | if (ste->ste_generator && ste->ste_coroutine) |
| 6488 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6489 | if (ste->ste_varargs) |
| 6490 | flags |= CO_VARARGS; |
| 6491 | if (ste->ste_varkeywords) |
| 6492 | flags |= CO_VARKEYWORDS; |
| 6493 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6495 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6496 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6497 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6498 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6499 | ste->ste_coroutine && |
| 6500 | !ste->ste_generator) { |
| 6501 | flags |= CO_COROUTINE; |
| 6502 | } |
| 6503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6504 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6505 | } |
| 6506 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6507 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6508 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6509 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6510 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6511 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6512 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6513 | if (key == NULL) { |
| 6514 | return 0; |
| 6515 | } |
| 6516 | |
| 6517 | // t is borrowed reference |
| 6518 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6519 | Py_DECREF(key); |
| 6520 | if (t == NULL) { |
| 6521 | return 0; |
| 6522 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6523 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6524 | return 1; |
| 6525 | } |
| 6526 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6527 | if (PyTuple_CheckExact(t)) { |
| 6528 | // t is still borrowed reference |
| 6529 | t = PyTuple_GET_ITEM(t, 1); |
| 6530 | } |
| 6531 | |
| 6532 | Py_INCREF(t); |
| 6533 | Py_DECREF(*obj); |
| 6534 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6535 | return 1; |
| 6536 | } |
| 6537 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6538 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6539 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6541 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6542 | PyObject *names = NULL; |
| 6543 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6544 | PyObject *name = NULL; |
| 6545 | PyObject *freevars = NULL; |
| 6546 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6547 | Py_ssize_t nlocals; |
| 6548 | int nlocals_int; |
| 6549 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6550 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6553 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6554 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6555 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6556 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6557 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6558 | if (!cellvars) |
| 6559 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6560 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6561 | if (!freevars) |
| 6562 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6563 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6564 | if (!merge_const_one(c, &names) || |
| 6565 | !merge_const_one(c, &varnames) || |
| 6566 | !merge_const_one(c, &cellvars) || |
| 6567 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6568 | { |
| 6569 | goto error; |
| 6570 | } |
| 6571 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6572 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6573 | assert(nlocals < INT_MAX); |
| 6574 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6576 | flags = compute_code_flags(c); |
| 6577 | if (flags < 0) |
| 6578 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6579 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6580 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6581 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6582 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6583 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6584 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6585 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6586 | goto error; |
| 6587 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6588 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6589 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6590 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6591 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6592 | maxdepth = stackdepth(c); |
| 6593 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6594 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6595 | goto error; |
| 6596 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6597 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6598 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6599 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6600 | varnames, freevars, cellvars, c->c_filename, |
| 6601 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6602 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6603 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6604 | Py_XDECREF(names); |
| 6605 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6606 | Py_XDECREF(name); |
| 6607 | Py_XDECREF(freevars); |
| 6608 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6609 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6610 | } |
| 6611 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6612 | |
| 6613 | /* For debugging purposes only */ |
| 6614 | #if 0 |
| 6615 | static void |
| 6616 | dump_instr(const struct instr *i) |
| 6617 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6618 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 6619 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6620 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6622 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6623 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6624 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6625 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6626 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6627 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
| 6630 | static void |
| 6631 | dump_basicblock(const basicblock *b) |
| 6632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6633 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6634 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6635 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6636 | if (b->b_instr) { |
| 6637 | int i; |
| 6638 | for (i = 0; i < b->b_iused; i++) { |
| 6639 | fprintf(stderr, " [%02d] ", i); |
| 6640 | dump_instr(b->b_instr + i); |
| 6641 | } |
| 6642 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6643 | } |
| 6644 | #endif |
| 6645 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6646 | |
| 6647 | static int |
| 6648 | normalize_basic_block(basicblock *bb); |
| 6649 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6650 | static int |
| 6651 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 6652 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6653 | static int |
| 6654 | ensure_exits_have_lineno(struct compiler *c); |
| 6655 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6656 | static int |
| 6657 | insert_generator_prefix(struct compiler *c, basicblock *entryblock) { |
| 6658 | |
| 6659 | int flags = compute_code_flags(c); |
| 6660 | if (flags < 0) { |
| 6661 | return -1; |
| 6662 | } |
| 6663 | int kind; |
| 6664 | if (flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { |
| 6665 | if (flags & CO_COROUTINE) { |
| 6666 | kind = 1; |
| 6667 | } |
| 6668 | else if (flags & CO_ASYNC_GENERATOR) { |
| 6669 | kind = 2; |
| 6670 | } |
| 6671 | else { |
| 6672 | kind = 0; |
| 6673 | } |
| 6674 | } |
| 6675 | else { |
| 6676 | return 0; |
| 6677 | } |
| 6678 | if (compiler_next_instr(entryblock) < 0) { |
| 6679 | return -1; |
| 6680 | } |
| 6681 | for (int i = entryblock->b_iused-1; i > 0; i--) { |
| 6682 | entryblock->b_instr[i] = entryblock->b_instr[i-1]; |
| 6683 | } |
| 6684 | entryblock->b_instr[0].i_opcode = GEN_START; |
| 6685 | entryblock->b_instr[0].i_oparg = kind; |
| 6686 | entryblock->b_instr[0].i_lineno = -1; |
| 6687 | entryblock->b_instr[0].i_target = NULL; |
| 6688 | return 0; |
| 6689 | } |
| 6690 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6691 | static PyCodeObject * |
| 6692 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6694 | basicblock *b, *entryblock; |
| 6695 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6696 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6697 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6698 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6700 | /* Make sure every block that falls off the end returns None. |
| 6701 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6702 | block ends with a jump or return b_next shouldn't set. |
| 6703 | */ |
| 6704 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6705 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6706 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6707 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6708 | ADDOP(c, RETURN_VALUE); |
| 6709 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6710 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6711 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6712 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6713 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6714 | } |
| 6715 | } |
| 6716 | |
| 6717 | if (ensure_exits_have_lineno(c)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6718 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6721 | nblocks = 0; |
| 6722 | entryblock = NULL; |
| 6723 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6724 | nblocks++; |
| 6725 | entryblock = b; |
| 6726 | } |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame^] | 6727 | assert(entryblock != NULL); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6728 | |
Mark Shannon | b37181e | 2021-04-06 11:48:59 +0100 | [diff] [blame] | 6729 | if (insert_generator_prefix(c, entryblock)) { |
| 6730 | goto error; |
| 6731 | } |
| 6732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6733 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6734 | if (!c->u->u_firstlineno) { |
Mark Shannon | 67969f5 | 2021-04-07 10:52:07 +0100 | [diff] [blame^] | 6735 | if (entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6736 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6737 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6738 | c->u->u_firstlineno = 1; |
| 6739 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6741 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6742 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6743 | a.a_entry = entryblock; |
| 6744 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6745 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6746 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6747 | if (consts == NULL) { |
| 6748 | goto error; |
| 6749 | } |
| 6750 | if (optimize_cfg(&a, consts)) { |
| 6751 | goto error; |
| 6752 | } |
| 6753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6754 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6755 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6756 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6757 | /* Emit code. */ |
| 6758 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6759 | for (j = 0; j < b->b_iused; j++) |
| 6760 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6761 | goto error; |
| 6762 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6763 | if (!assemble_line_range(&a)) { |
| 6764 | return 0; |
| 6765 | } |
| 6766 | /* Emit sentinel at end of line number table */ |
| 6767 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6768 | goto error; |
| 6769 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6770 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6771 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6772 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6773 | } |
| 6774 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6775 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6776 | } |
| 6777 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 6778 | goto error; |
| 6779 | } |
| 6780 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 6781 | goto error; |
| 6782 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6783 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6784 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6785 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6786 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6787 | assemble_free(&a); |
| 6788 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6789 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6790 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6791 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6792 | with LOAD_CONST (c1, c2, ... cn). |
| 6793 | The consts table must still be in list form so that the |
| 6794 | new constant (c1, c2, ... cn) can be appended. |
| 6795 | Called with codestr pointing to the first LOAD_CONST. |
| 6796 | */ |
| 6797 | static int |
| 6798 | fold_tuple_on_constants(struct instr *inst, |
| 6799 | int n, PyObject *consts) |
| 6800 | { |
| 6801 | /* Pre-conditions */ |
| 6802 | assert(PyList_CheckExact(consts)); |
| 6803 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6804 | assert(inst[n].i_oparg == n); |
| 6805 | |
| 6806 | for (int i = 0; i < n; i++) { |
| 6807 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6808 | return 0; |
| 6809 | } |
| 6810 | } |
| 6811 | |
| 6812 | /* Buildup new tuple of constants */ |
| 6813 | PyObject *newconst = PyTuple_New(n); |
| 6814 | if (newconst == NULL) { |
| 6815 | return -1; |
| 6816 | } |
| 6817 | for (int i = 0; i < n; i++) { |
| 6818 | int arg = inst[i].i_oparg; |
| 6819 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6820 | Py_INCREF(constant); |
| 6821 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6822 | } |
| 6823 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6824 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6825 | Py_DECREF(newconst); |
| 6826 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6827 | return -1; |
| 6828 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6829 | if (PyList_Append(consts, newconst)) { |
| 6830 | Py_DECREF(newconst); |
| 6831 | return -1; |
| 6832 | } |
| 6833 | Py_DECREF(newconst); |
| 6834 | for (int i = 0; i < n; i++) { |
| 6835 | inst[i].i_opcode = NOP; |
| 6836 | } |
| 6837 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6838 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6839 | return 0; |
| 6840 | } |
| 6841 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6842 | |
| 6843 | static int |
| 6844 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 6845 | assert (bb->b_iused > 0); |
| 6846 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 6847 | assert (is_jump(inst)); |
| 6848 | assert (inst->i_target->b_iused > 0); |
| 6849 | struct instr *target = &inst->i_target->b_instr[0]; |
| 6850 | if (inst->i_target == target->i_target) { |
| 6851 | /* Nothing to do */ |
| 6852 | return 0; |
| 6853 | } |
| 6854 | int lineno = target->i_lineno; |
| 6855 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 6856 | return -1; |
| 6857 | } |
| 6858 | assert (bb->b_iused >= 2); |
| 6859 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 6860 | return 0; |
| 6861 | } |
| 6862 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6863 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6864 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6865 | |
| 6866 | /* Optimization */ |
| 6867 | static int |
| 6868 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6869 | { |
| 6870 | assert(PyList_CheckExact(consts)); |
| 6871 | struct instr nop; |
| 6872 | nop.i_opcode = NOP; |
| 6873 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6874 | for (int i = 0; i < bb->b_iused; i++) { |
| 6875 | struct instr *inst = &bb->b_instr[i]; |
| 6876 | int oparg = inst->i_oparg; |
| 6877 | 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] | 6878 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6879 | /* Skip over empty basic blocks. */ |
| 6880 | while (inst->i_target->b_iused == 0) { |
| 6881 | inst->i_target = inst->i_target->b_next; |
| 6882 | } |
| 6883 | target = &inst->i_target->b_instr[0]; |
| 6884 | } |
| 6885 | else { |
| 6886 | target = &nop; |
| 6887 | } |
| 6888 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6889 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6890 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6891 | { |
| 6892 | PyObject* cnt; |
| 6893 | int is_true; |
| 6894 | int jump_if_true; |
| 6895 | switch(nextop) { |
| 6896 | case POP_JUMP_IF_FALSE: |
| 6897 | case POP_JUMP_IF_TRUE: |
| 6898 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6899 | is_true = PyObject_IsTrue(cnt); |
| 6900 | if (is_true == -1) { |
| 6901 | goto error; |
| 6902 | } |
| 6903 | inst->i_opcode = NOP; |
| 6904 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6905 | if (is_true == jump_if_true) { |
| 6906 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6907 | bb->b_nofallthrough = 1; |
| 6908 | } |
| 6909 | else { |
| 6910 | bb->b_instr[i+1].i_opcode = NOP; |
| 6911 | } |
| 6912 | break; |
| 6913 | case JUMP_IF_FALSE_OR_POP: |
| 6914 | case JUMP_IF_TRUE_OR_POP: |
| 6915 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6916 | is_true = PyObject_IsTrue(cnt); |
| 6917 | if (is_true == -1) { |
| 6918 | goto error; |
| 6919 | } |
| 6920 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6921 | if (is_true == jump_if_true) { |
| 6922 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6923 | bb->b_nofallthrough = 1; |
| 6924 | } |
| 6925 | else { |
| 6926 | inst->i_opcode = NOP; |
| 6927 | bb->b_instr[i+1].i_opcode = NOP; |
| 6928 | } |
| 6929 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6930 | } |
| 6931 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6932 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6933 | |
| 6934 | /* Try to fold tuples of constants. |
| 6935 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6936 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6937 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6938 | case BUILD_TUPLE: |
| 6939 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6940 | switch(oparg) { |
| 6941 | case 1: |
| 6942 | inst->i_opcode = NOP; |
| 6943 | bb->b_instr[i+1].i_opcode = NOP; |
| 6944 | break; |
| 6945 | case 2: |
| 6946 | inst->i_opcode = ROT_TWO; |
| 6947 | bb->b_instr[i+1].i_opcode = NOP; |
| 6948 | break; |
| 6949 | case 3: |
| 6950 | inst->i_opcode = ROT_THREE; |
| 6951 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6952 | } |
| 6953 | break; |
| 6954 | } |
| 6955 | if (i >= oparg) { |
| 6956 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6957 | goto error; |
| 6958 | } |
| 6959 | } |
| 6960 | break; |
| 6961 | |
| 6962 | /* Simplify conditional jump to conditional jump where the |
| 6963 | result of the first test implies the success of a similar |
| 6964 | test or the failure of the opposite test. |
| 6965 | Arises in code like: |
| 6966 | "a and b or c" |
| 6967 | "(a and b) and c" |
| 6968 | "(a or b) or c" |
| 6969 | "(a or b) and c" |
| 6970 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6971 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6972 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6973 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6974 | where y+1 is the instruction following the second test. |
| 6975 | */ |
| 6976 | case JUMP_IF_FALSE_OR_POP: |
| 6977 | switch(target->i_opcode) { |
| 6978 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6979 | if (inst->i_lineno == target->i_lineno) { |
| 6980 | *inst = *target; |
| 6981 | i--; |
| 6982 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6983 | break; |
| 6984 | case JUMP_ABSOLUTE: |
| 6985 | case JUMP_FORWARD: |
| 6986 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6987 | if (inst->i_lineno == target->i_lineno && |
| 6988 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6989 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6990 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6991 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6992 | break; |
| 6993 | case JUMP_IF_TRUE_OR_POP: |
| 6994 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6995 | if (inst->i_lineno == target->i_lineno) { |
| 6996 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6997 | inst->i_target = inst->i_target->b_next; |
| 6998 | --i; |
| 6999 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7000 | break; |
| 7001 | } |
| 7002 | break; |
| 7003 | |
| 7004 | case JUMP_IF_TRUE_OR_POP: |
| 7005 | switch(target->i_opcode) { |
| 7006 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7007 | if (inst->i_lineno == target->i_lineno) { |
| 7008 | *inst = *target; |
| 7009 | i--; |
| 7010 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7011 | break; |
| 7012 | case JUMP_ABSOLUTE: |
| 7013 | case JUMP_FORWARD: |
| 7014 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7015 | if (inst->i_lineno == target->i_lineno && |
| 7016 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7017 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7018 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7019 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7020 | break; |
| 7021 | case JUMP_IF_FALSE_OR_POP: |
| 7022 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7023 | if (inst->i_lineno == target->i_lineno) { |
| 7024 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 7025 | inst->i_target = inst->i_target->b_next; |
| 7026 | --i; |
| 7027 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7028 | break; |
| 7029 | } |
| 7030 | break; |
| 7031 | |
| 7032 | case POP_JUMP_IF_FALSE: |
| 7033 | switch(target->i_opcode) { |
| 7034 | case JUMP_ABSOLUTE: |
| 7035 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7036 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7037 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7038 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7039 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7040 | break; |
| 7041 | } |
| 7042 | break; |
| 7043 | |
| 7044 | case POP_JUMP_IF_TRUE: |
| 7045 | switch(target->i_opcode) { |
| 7046 | case JUMP_ABSOLUTE: |
| 7047 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7048 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7049 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7050 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7051 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7052 | break; |
| 7053 | } |
| 7054 | break; |
| 7055 | |
| 7056 | case JUMP_ABSOLUTE: |
| 7057 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7058 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7059 | switch(target->i_opcode) { |
| 7060 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7061 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7062 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7063 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7064 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7065 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7066 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7067 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7068 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7069 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7070 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7071 | default: |
| 7072 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7073 | basicblock *to_copy = inst->i_target; |
| 7074 | inst->i_opcode = NOP; |
| 7075 | for (i = 0; i < to_copy->b_iused; i++) { |
| 7076 | int index = compiler_next_instr(bb); |
| 7077 | if (index < 0) { |
| 7078 | return -1; |
| 7079 | } |
| 7080 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7081 | } |
| 7082 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7083 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7084 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7085 | } |
| 7086 | } |
| 7087 | return 0; |
| 7088 | error: |
| 7089 | return -1; |
| 7090 | } |
| 7091 | |
| 7092 | |
| 7093 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7094 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7095 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7096 | int dest = 0; |
| 7097 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7098 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7099 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7100 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7101 | if (lineno < 0) { |
| 7102 | continue; |
| 7103 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7104 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7105 | if (prev_lineno == lineno) { |
| 7106 | continue; |
| 7107 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7108 | /* 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] | 7109 | if (src < bb->b_iused - 1) { |
| 7110 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7111 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7112 | bb->b_instr[src+1].i_lineno = lineno; |
| 7113 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7114 | } |
| 7115 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7116 | else { |
| 7117 | basicblock* next = bb->b_next; |
| 7118 | while (next && next->b_iused == 0) { |
| 7119 | next = next->b_next; |
| 7120 | } |
| 7121 | /* or if last instruction in BB and next BB has same line number */ |
| 7122 | if (next) { |
| 7123 | if (lineno == next->b_instr[0].i_lineno) { |
| 7124 | continue; |
| 7125 | } |
| 7126 | } |
| 7127 | } |
| 7128 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7129 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7130 | if (dest != src) { |
| 7131 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7132 | } |
| 7133 | dest++; |
| 7134 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7135 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7136 | assert(dest <= bb->b_iused); |
| 7137 | bb->b_iused = dest; |
| 7138 | } |
| 7139 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7140 | static int |
| 7141 | normalize_basic_block(basicblock *bb) { |
| 7142 | /* Mark blocks as exit and/or nofallthrough. |
| 7143 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7144 | for (int i = 0; i < bb->b_iused; i++) { |
| 7145 | switch(bb->b_instr[i].i_opcode) { |
| 7146 | case RETURN_VALUE: |
| 7147 | case RAISE_VARARGS: |
| 7148 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7149 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7150 | bb->b_nofallthrough = 1; |
| 7151 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7152 | case JUMP_ABSOLUTE: |
| 7153 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7154 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7155 | /* fall through */ |
| 7156 | case POP_JUMP_IF_FALSE: |
| 7157 | case POP_JUMP_IF_TRUE: |
| 7158 | case JUMP_IF_FALSE_OR_POP: |
| 7159 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7160 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7161 | if (i != bb->b_iused-1) { |
| 7162 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7163 | return -1; |
| 7164 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7165 | /* Skip over empty basic blocks. */ |
| 7166 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7167 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7168 | } |
| 7169 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7170 | } |
| 7171 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7172 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7173 | } |
| 7174 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7175 | static int |
| 7176 | mark_reachable(struct assembler *a) { |
| 7177 | basicblock **stack, **sp; |
| 7178 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7179 | if (stack == NULL) { |
| 7180 | return -1; |
| 7181 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7182 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7183 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7184 | while (sp > stack) { |
| 7185 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7186 | if (b->b_next && !b->b_nofallthrough) { |
| 7187 | if (b->b_next->b_predecessors == 0) { |
| 7188 | *sp++ = b->b_next; |
| 7189 | } |
| 7190 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7191 | } |
| 7192 | for (int i = 0; i < b->b_iused; i++) { |
| 7193 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7194 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7195 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7196 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7197 | *sp++ = target; |
| 7198 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7199 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7200 | } |
| 7201 | } |
| 7202 | } |
| 7203 | PyObject_Free(stack); |
| 7204 | return 0; |
| 7205 | } |
| 7206 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7207 | static void |
| 7208 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7209 | /* Eliminate empty blocks */ |
| 7210 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7211 | basicblock *next = b->b_next; |
| 7212 | if (next) { |
| 7213 | while (next->b_iused == 0 && next->b_next) { |
| 7214 | next = next->b_next; |
| 7215 | } |
| 7216 | b->b_next = next; |
| 7217 | } |
| 7218 | } |
| 7219 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7220 | if (b->b_iused == 0) { |
| 7221 | continue; |
| 7222 | } |
| 7223 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7224 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7225 | while (target->b_iused == 0) { |
| 7226 | target = target->b_next; |
| 7227 | } |
| 7228 | b->b_instr[b->b_iused-1].i_target = target; |
| 7229 | } |
| 7230 | } |
| 7231 | } |
| 7232 | |
| 7233 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7234 | /* 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] | 7235 | * then copy the line number. If a successor block has no line number, and only |
| 7236 | * one predecessor, then inherit the line number. |
| 7237 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7238 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7239 | * but has no impact on the generated line number events. |
| 7240 | */ |
| 7241 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7242 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7243 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7244 | if (b->b_iused == 0) { |
| 7245 | continue; |
| 7246 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7247 | int prev_lineno = -1; |
| 7248 | for (int i = 0; i < b->b_iused; i++) { |
| 7249 | if (b->b_instr[i].i_lineno < 0) { |
| 7250 | b->b_instr[i].i_lineno = prev_lineno; |
| 7251 | } |
| 7252 | else { |
| 7253 | prev_lineno = b->b_instr[i].i_lineno; |
| 7254 | } |
| 7255 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7256 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7257 | assert(b->b_next->b_iused); |
| 7258 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7259 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7260 | } |
| 7261 | } |
| 7262 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7263 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7264 | /* Note: Only actual jumps, not exception handlers */ |
| 7265 | case SETUP_ASYNC_WITH: |
| 7266 | case SETUP_WITH: |
| 7267 | case SETUP_FINALLY: |
| 7268 | continue; |
| 7269 | } |
| 7270 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7271 | if (target->b_predecessors == 1) { |
| 7272 | if (target->b_instr[0].i_lineno < 0) { |
| 7273 | target->b_instr[0].i_lineno = prev_lineno; |
| 7274 | } |
| 7275 | } |
| 7276 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7277 | } |
| 7278 | } |
| 7279 | |
| 7280 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7281 | The consts object should still be in list form to allow new constants |
| 7282 | to be appended. |
| 7283 | |
| 7284 | All transformations keep the code size the same or smaller. |
| 7285 | For those that reduce size, the gaps are initially filled with |
| 7286 | NOPs. Later those NOPs are removed. |
| 7287 | */ |
| 7288 | |
| 7289 | static int |
| 7290 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 7291 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7292 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7293 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7294 | return -1; |
| 7295 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7296 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7297 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7298 | } |
| 7299 | if (mark_reachable(a)) { |
| 7300 | return -1; |
| 7301 | } |
| 7302 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7303 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7304 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7305 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7306 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7307 | } |
| 7308 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7309 | basicblock *pred = NULL; |
| 7310 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7311 | int prev_lineno = -1; |
| 7312 | if (pred && pred->b_iused) { |
| 7313 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7314 | } |
| 7315 | clean_basic_block(b, prev_lineno); |
| 7316 | pred = b->b_nofallthrough ? NULL : b; |
| 7317 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7318 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7319 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7320 | block ends with a jump instruction, check if the next non-empty block |
| 7321 | reached through normal flow control is the target of that jump. If it |
| 7322 | is, then the jump instruction is redundant and can be deleted. |
| 7323 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7324 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7325 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7326 | if (b->b_iused > 0) { |
| 7327 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7328 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7329 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7330 | if (b_last_instr->i_target == b->b_next) { |
| 7331 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7332 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7333 | b_last_instr->i_opcode = NOP; |
| 7334 | clean_basic_block(b, -1); |
| 7335 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7336 | } |
| 7337 | } |
| 7338 | } |
| 7339 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7340 | if (maybe_empty_blocks) { |
| 7341 | eliminate_empty_basic_blocks(a->a_entry); |
| 7342 | } |
| 7343 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7344 | return 0; |
| 7345 | } |
| 7346 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7347 | static inline int |
| 7348 | is_exit_without_lineno(basicblock *b) { |
| 7349 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7350 | } |
| 7351 | |
| 7352 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7353 | * after a frame terminates. It would be prohibitively expensive |
| 7354 | * to continuously update the f_lineno field at runtime, |
| 7355 | * so we make sure that all exiting instruction (raises and returns) |
| 7356 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7357 | * We can do this by duplicating the exit blocks without line number |
| 7358 | * so that none have more than one predecessor. We can then safely |
| 7359 | * copy the line number from the sole predecessor block. |
| 7360 | */ |
| 7361 | static int |
| 7362 | ensure_exits_have_lineno(struct compiler *c) |
| 7363 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7364 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7365 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7366 | */ |
| 7367 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7368 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7369 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7370 | /* Note: Only actual jumps, not exception handlers */ |
| 7371 | case SETUP_ASYNC_WITH: |
| 7372 | case SETUP_WITH: |
| 7373 | case SETUP_FINALLY: |
| 7374 | continue; |
| 7375 | } |
| 7376 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7377 | if (is_exit_without_lineno(target)) { |
| 7378 | basicblock *new_target = compiler_copy_block(c, target); |
| 7379 | if (new_target == NULL) { |
| 7380 | return -1; |
| 7381 | } |
| 7382 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7383 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 7384 | } |
| 7385 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7386 | entry = b; |
| 7387 | } |
| 7388 | assert(entry != NULL); |
| 7389 | if (is_exit_without_lineno(entry)) { |
| 7390 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7391 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7392 | /* Eliminate empty blocks */ |
| 7393 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7394 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7395 | b->b_next = b->b_next->b_next; |
| 7396 | } |
| 7397 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7398 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7399 | * fall through, and thus can only have a single predecessor */ |
| 7400 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7401 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7402 | if (is_exit_without_lineno(b->b_next)) { |
| 7403 | assert(b->b_next->b_iused > 0); |
| 7404 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7405 | } |
| 7406 | } |
| 7407 | } |
| 7408 | return 0; |
| 7409 | } |
| 7410 | |
| 7411 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7412 | /* Retained for API compatibility. |
| 7413 | * Optimization is now done in optimize_cfg */ |
| 7414 | |
| 7415 | PyObject * |
| 7416 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7417 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7418 | { |
| 7419 | Py_INCREF(code); |
| 7420 | return code; |
| 7421 | } |