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 | * |
| 4 | * The primary entry point is PyAST_Compile(), which returns a |
| 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 | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 25 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 26 | #include "pycore_long.h" // _PyLong_GetZero() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 28 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 29 | #include "ast.h" |
| 30 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 31 | #include "symtable.h" |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 32 | #define NEED_OPCODE_JUMP_TABLES |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 33 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 34 | #include "wordcode_helpers.h" |
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 | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 354 | PyAST_CompileObject(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 | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 377 | c.c_future = PyFuture_FromASTObject(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 | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 398 | c.c_st = PySymtable_BuildObject(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 | |
| 413 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 414 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 415 | int optimize, PyArena *arena) |
| 416 | { |
| 417 | PyObject *filename; |
| 418 | PyCodeObject *co; |
| 419 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 420 | if (filename == NULL) |
| 421 | return NULL; |
| 422 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 423 | Py_DECREF(filename); |
| 424 | return co; |
| 425 | |
| 426 | } |
| 427 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 428 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 429 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | if (c->c_st) |
| 432 | PySymtable_Free(c->c_st); |
| 433 | if (c->c_future) |
| 434 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 435 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 436 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 440 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 441 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | Py_ssize_t i, n; |
| 444 | PyObject *v, *k; |
| 445 | PyObject *dict = PyDict_New(); |
| 446 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | n = PyList_Size(list); |
| 449 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 450 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | if (!v) { |
| 452 | Py_DECREF(dict); |
| 453 | return NULL; |
| 454 | } |
| 455 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 456 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | Py_DECREF(v); |
| 458 | Py_DECREF(dict); |
| 459 | return NULL; |
| 460 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | Py_DECREF(v); |
| 462 | } |
| 463 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Return new dict containing names from src that match scope(s). |
| 467 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 468 | 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] | 469 | 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] | 470 | values are integers, starting at offset and increasing by one for |
| 471 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 472 | */ |
| 473 | |
| 474 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 475 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 476 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 477 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 479 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | assert(offset >= 0); |
| 482 | if (dest == NULL) |
| 483 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 484 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 485 | /* Sort the keys so that we have a deterministic order on the indexes |
| 486 | saved in the returned dictionary. These indexes are used as indexes |
| 487 | into the free and cell var storage. Therefore if they aren't |
| 488 | deterministic, then the generated bytecode is not deterministic. |
| 489 | */ |
| 490 | sorted_keys = PyDict_Keys(src); |
| 491 | if (sorted_keys == NULL) |
| 492 | return NULL; |
| 493 | if (PyList_Sort(sorted_keys) != 0) { |
| 494 | Py_DECREF(sorted_keys); |
| 495 | return NULL; |
| 496 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 497 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 498 | |
| 499 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | /* XXX this should probably be a macro in symtable.h */ |
| 501 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 502 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 503 | v = PyDict_GetItemWithError(src, k); |
| 504 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | vi = PyLong_AS_LONG(v); |
| 506 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 509 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 511 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | Py_DECREF(dest); |
| 513 | return NULL; |
| 514 | } |
| 515 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 516 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 517 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | Py_DECREF(item); |
| 519 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | return NULL; |
| 521 | } |
| 522 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 525 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 529 | static void |
| 530 | compiler_unit_check(struct compiler_unit *u) |
| 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | basicblock *block; |
| 533 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 534 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | if (block->b_instr != NULL) { |
| 536 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 537 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | assert(block->b_ialloc >= block->b_iused); |
| 539 | } |
| 540 | else { |
| 541 | assert (block->b_iused == 0); |
| 542 | assert (block->b_ialloc == 0); |
| 543 | } |
| 544 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void |
| 548 | compiler_unit_free(struct compiler_unit *u) |
| 549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | compiler_unit_check(u); |
| 553 | b = u->u_blocks; |
| 554 | while (b != NULL) { |
| 555 | if (b->b_instr) |
| 556 | PyObject_Free((void *)b->b_instr); |
| 557 | next = b->b_list; |
| 558 | PyObject_Free((void *)b); |
| 559 | b = next; |
| 560 | } |
| 561 | Py_CLEAR(u->u_ste); |
| 562 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 563 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | Py_CLEAR(u->u_consts); |
| 565 | Py_CLEAR(u->u_names); |
| 566 | Py_CLEAR(u->u_varnames); |
| 567 | Py_CLEAR(u->u_freevars); |
| 568 | Py_CLEAR(u->u_cellvars); |
| 569 | Py_CLEAR(u->u_private); |
| 570 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 574 | compiler_enter_scope(struct compiler *c, identifier name, |
| 575 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 578 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 579 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 580 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | struct compiler_unit)); |
| 582 | if (!u) { |
| 583 | PyErr_NoMemory(); |
| 584 | return 0; |
| 585 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 586 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 588 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | u->u_kwonlyargcount = 0; |
| 590 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 591 | if (!u->u_ste) { |
| 592 | compiler_unit_free(u); |
| 593 | return 0; |
| 594 | } |
| 595 | Py_INCREF(name); |
| 596 | u->u_name = name; |
| 597 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 598 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 599 | if (!u->u_varnames || !u->u_cellvars) { |
| 600 | compiler_unit_free(u); |
| 601 | return 0; |
| 602 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 603 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 604 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 605 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 606 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 607 | int res; |
| 608 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 609 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 610 | name = _PyUnicode_FromId(&PyId___class__); |
| 611 | if (!name) { |
| 612 | compiler_unit_free(u); |
| 613 | return 0; |
| 614 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 615 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 616 | if (res < 0) { |
| 617 | compiler_unit_free(u); |
| 618 | return 0; |
| 619 | } |
| 620 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | 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] | 623 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | if (!u->u_freevars) { |
| 625 | compiler_unit_free(u); |
| 626 | return 0; |
| 627 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | u->u_blocks = NULL; |
| 630 | u->u_nfblocks = 0; |
| 631 | u->u_firstlineno = lineno; |
| 632 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 633 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | u->u_consts = PyDict_New(); |
| 635 | if (!u->u_consts) { |
| 636 | compiler_unit_free(u); |
| 637 | return 0; |
| 638 | } |
| 639 | u->u_names = PyDict_New(); |
| 640 | if (!u->u_names) { |
| 641 | compiler_unit_free(u); |
| 642 | return 0; |
| 643 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | /* Push the old compiler_unit on the stack. */ |
| 648 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 649 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 651 | Py_XDECREF(capsule); |
| 652 | compiler_unit_free(u); |
| 653 | return 0; |
| 654 | } |
| 655 | Py_DECREF(capsule); |
| 656 | u->u_private = c->u->u_private; |
| 657 | Py_XINCREF(u->u_private); |
| 658 | } |
| 659 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 662 | |
| 663 | block = compiler_new_block(c); |
| 664 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 666 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 667 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 668 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 669 | if (!compiler_set_qualname(c)) |
| 670 | return 0; |
| 671 | } |
| 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 676 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 677 | compiler_exit_scope(struct compiler *c) |
| 678 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 679 | // Don't call PySequence_DelItem() with an exception raised |
| 680 | PyObject *exc_type, *exc_val, *exc_tb; |
| 681 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | c->c_nestlevel--; |
| 684 | compiler_unit_free(c->u); |
| 685 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 686 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 688 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 689 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | assert(c->u); |
| 691 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 692 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 693 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 694 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 695 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | compiler_unit_check(c->u); |
| 697 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 698 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 700 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 701 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 702 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 705 | static int |
| 706 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 707 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 708 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 709 | _Py_static_string(dot_locals, ".<locals>"); |
| 710 | Py_ssize_t stack_size; |
| 711 | struct compiler_unit *u = c->u; |
| 712 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 713 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 714 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 715 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 716 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 717 | if (stack_size > 1) { |
| 718 | int scope, force_global = 0; |
| 719 | struct compiler_unit *parent; |
| 720 | PyObject *mangled, *capsule; |
| 721 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 722 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 723 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 724 | assert(parent); |
| 725 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 726 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 727 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 728 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 729 | assert(u->u_name); |
| 730 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 731 | if (!mangled) |
| 732 | return 0; |
| 733 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 734 | Py_DECREF(mangled); |
| 735 | assert(scope != GLOBAL_IMPLICIT); |
| 736 | if (scope == GLOBAL_EXPLICIT) |
| 737 | force_global = 1; |
| 738 | } |
| 739 | |
| 740 | if (!force_global) { |
| 741 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 742 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 743 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 744 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 745 | if (dot_locals_str == NULL) |
| 746 | return 0; |
| 747 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 748 | if (base == NULL) |
| 749 | return 0; |
| 750 | } |
| 751 | else { |
| 752 | Py_INCREF(parent->u_qualname); |
| 753 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 754 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 755 | } |
| 756 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 757 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 758 | if (base != NULL) { |
| 759 | dot_str = _PyUnicode_FromId(&dot); |
| 760 | if (dot_str == NULL) { |
| 761 | Py_DECREF(base); |
| 762 | return 0; |
| 763 | } |
| 764 | name = PyUnicode_Concat(base, dot_str); |
| 765 | Py_DECREF(base); |
| 766 | if (name == NULL) |
| 767 | return 0; |
| 768 | PyUnicode_Append(&name, u->u_name); |
| 769 | if (name == NULL) |
| 770 | return 0; |
| 771 | } |
| 772 | else { |
| 773 | Py_INCREF(u->u_name); |
| 774 | name = u->u_name; |
| 775 | } |
| 776 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 777 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 778 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 781 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 782 | /* Allocate a new block and return a pointer to it. |
| 783 | Returns NULL on error. |
| 784 | */ |
| 785 | |
| 786 | static basicblock * |
| 787 | compiler_new_block(struct compiler *c) |
| 788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | basicblock *b; |
| 790 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 793 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | if (b == NULL) { |
| 795 | PyErr_NoMemory(); |
| 796 | return NULL; |
| 797 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | /* Extend the singly linked list of blocks with new block. */ |
| 799 | b->b_list = u->u_blocks; |
| 800 | u->u_blocks = b; |
| 801 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 804 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | compiler_next_block(struct compiler *c) |
| 806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | basicblock *block = compiler_new_block(c); |
| 808 | if (block == NULL) |
| 809 | return NULL; |
| 810 | c->u->u_curblock->b_next = block; |
| 811 | c->u->u_curblock = block; |
| 812 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | static basicblock * |
| 816 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 817 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | assert(block != NULL); |
| 819 | c->u->u_curblock->b_next = block; |
| 820 | c->u->u_curblock = block; |
| 821 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 824 | static basicblock * |
| 825 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 826 | { |
| 827 | /* Cannot copy a block if it has a fallthrough, since |
| 828 | * a block can only have one fallthrough predecessor. |
| 829 | */ |
| 830 | assert(block->b_nofallthrough); |
| 831 | basicblock *result = compiler_next_block(c); |
| 832 | if (result == NULL) { |
| 833 | return NULL; |
| 834 | } |
| 835 | for (int i = 0; i < block->b_iused; i++) { |
| 836 | int n = compiler_next_instr(result); |
| 837 | if (n < 0) { |
| 838 | return NULL; |
| 839 | } |
| 840 | result->b_instr[n] = block->b_instr[i]; |
| 841 | } |
| 842 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 843 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 844 | return result; |
| 845 | } |
| 846 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 847 | /* Returns the offset of the next instruction in the current block's |
| 848 | b_instr array. Resizes the b_instr as necessary. |
| 849 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 850 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 851 | |
| 852 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 853 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 854 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | assert(b != NULL); |
| 856 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 857 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 858 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | if (b->b_instr == NULL) { |
| 860 | PyErr_NoMemory(); |
| 861 | return -1; |
| 862 | } |
| 863 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | } |
| 865 | else if (b->b_iused == b->b_ialloc) { |
| 866 | struct instr *tmp; |
| 867 | size_t oldsize, newsize; |
| 868 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 869 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 870 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 871 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | PyErr_NoMemory(); |
| 873 | return -1; |
| 874 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | if (newsize == 0) { |
| 877 | PyErr_NoMemory(); |
| 878 | return -1; |
| 879 | } |
| 880 | b->b_ialloc <<= 1; |
| 881 | tmp = (struct instr *)PyObject_Realloc( |
| 882 | (void *)b->b_instr, newsize); |
| 883 | if (tmp == NULL) { |
| 884 | PyErr_NoMemory(); |
| 885 | return -1; |
| 886 | } |
| 887 | b->b_instr = tmp; |
| 888 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 889 | } |
| 890 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 893 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 894 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 895 | The line number is reset in the following cases: |
| 896 | - when entering a new scope |
| 897 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 898 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 899 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 900 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 901 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 902 | #define SET_LOC(c, x) \ |
| 903 | (c)->u->u_lineno = (x)->lineno; \ |
| 904 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 905 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 906 | /* Return the stack effect of opcode with argument oparg. |
| 907 | |
| 908 | Some opcodes have different stack effect when jump to the target and |
| 909 | when not jump. The 'jump' parameter specifies the case: |
| 910 | |
| 911 | * 0 -- when not jump |
| 912 | * 1 -- when jump |
| 913 | * -1 -- maximal |
| 914 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 915 | static int |
| 916 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 919 | case NOP: |
| 920 | case EXTENDED_ARG: |
| 921 | return 0; |
| 922 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 923 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | case POP_TOP: |
| 925 | return -1; |
| 926 | case ROT_TWO: |
| 927 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 928 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | return 0; |
| 930 | case DUP_TOP: |
| 931 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 932 | case DUP_TOP_TWO: |
| 933 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 934 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 935 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | case UNARY_POSITIVE: |
| 937 | case UNARY_NEGATIVE: |
| 938 | case UNARY_NOT: |
| 939 | case UNARY_INVERT: |
| 940 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | case SET_ADD: |
| 943 | case LIST_APPEND: |
| 944 | return -1; |
| 945 | case MAP_ADD: |
| 946 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 947 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 948 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | case BINARY_POWER: |
| 950 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 951 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case BINARY_MODULO: |
| 953 | case BINARY_ADD: |
| 954 | case BINARY_SUBTRACT: |
| 955 | case BINARY_SUBSCR: |
| 956 | case BINARY_FLOOR_DIVIDE: |
| 957 | case BINARY_TRUE_DIVIDE: |
| 958 | return -1; |
| 959 | case INPLACE_FLOOR_DIVIDE: |
| 960 | case INPLACE_TRUE_DIVIDE: |
| 961 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | case INPLACE_ADD: |
| 964 | case INPLACE_SUBTRACT: |
| 965 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 966 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | case INPLACE_MODULO: |
| 968 | return -1; |
| 969 | case STORE_SUBSCR: |
| 970 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | case DELETE_SUBSCR: |
| 972 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | case BINARY_LSHIFT: |
| 975 | case BINARY_RSHIFT: |
| 976 | case BINARY_AND: |
| 977 | case BINARY_XOR: |
| 978 | case BINARY_OR: |
| 979 | return -1; |
| 980 | case INPLACE_POWER: |
| 981 | return -1; |
| 982 | case GET_ITER: |
| 983 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | case PRINT_EXPR: |
| 986 | return -1; |
| 987 | case LOAD_BUILD_CLASS: |
| 988 | return 1; |
| 989 | case INPLACE_LSHIFT: |
| 990 | case INPLACE_RSHIFT: |
| 991 | case INPLACE_AND: |
| 992 | case INPLACE_XOR: |
| 993 | case INPLACE_OR: |
| 994 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 997 | /* 1 in the normal flow. |
| 998 | * Restore the stack position and push 6 values before jumping to |
| 999 | * the handler if an exception be raised. */ |
| 1000 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case RETURN_VALUE: |
| 1002 | return -1; |
| 1003 | case IMPORT_STAR: |
| 1004 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1005 | case SETUP_ANNOTATIONS: |
| 1006 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | case YIELD_VALUE: |
| 1008 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1009 | case YIELD_FROM: |
| 1010 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | case POP_BLOCK: |
| 1012 | return 0; |
| 1013 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1014 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | case STORE_NAME: |
| 1017 | return -1; |
| 1018 | case DELETE_NAME: |
| 1019 | return 0; |
| 1020 | case UNPACK_SEQUENCE: |
| 1021 | return oparg-1; |
| 1022 | case UNPACK_EX: |
| 1023 | return (oparg&0xFF) + (oparg>>8); |
| 1024 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1025 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1026 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | case STORE_ATTR: |
| 1029 | return -2; |
| 1030 | case DELETE_ATTR: |
| 1031 | return -1; |
| 1032 | case STORE_GLOBAL: |
| 1033 | return -1; |
| 1034 | case DELETE_GLOBAL: |
| 1035 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | case LOAD_CONST: |
| 1037 | return 1; |
| 1038 | case LOAD_NAME: |
| 1039 | return 1; |
| 1040 | case BUILD_TUPLE: |
| 1041 | case BUILD_LIST: |
| 1042 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1043 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | return 1-oparg; |
| 1045 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1046 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1047 | case BUILD_CONST_KEY_MAP: |
| 1048 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case LOAD_ATTR: |
| 1050 | return 0; |
| 1051 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1052 | case IS_OP: |
| 1053 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1055 | case JUMP_IF_NOT_EXC_MATCH: |
| 1056 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | case IMPORT_NAME: |
| 1058 | return -1; |
| 1059 | case IMPORT_FROM: |
| 1060 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1061 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1062 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | case JUMP_ABSOLUTE: |
| 1065 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1066 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1067 | case JUMP_IF_TRUE_OR_POP: |
| 1068 | case JUMP_IF_FALSE_OR_POP: |
| 1069 | return jump ? 0 : -1; |
| 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case POP_JUMP_IF_FALSE: |
| 1072 | case POP_JUMP_IF_TRUE: |
| 1073 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case LOAD_GLOBAL: |
| 1076 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1077 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1078 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1080 | /* 0 in the normal flow. |
| 1081 | * Restore the stack position and push 6 values before jumping to |
| 1082 | * the handler if an exception be raised. */ |
| 1083 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1084 | case RERAISE: |
| 1085 | return -3; |
| 1086 | |
| 1087 | case WITH_EXCEPT_START: |
| 1088 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | case LOAD_FAST: |
| 1091 | return 1; |
| 1092 | case STORE_FAST: |
| 1093 | return -1; |
| 1094 | case DELETE_FAST: |
| 1095 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | case RAISE_VARARGS: |
| 1098 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1099 | |
| 1100 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1102 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1103 | case CALL_METHOD: |
| 1104 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1106 | return -oparg-1; |
| 1107 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1108 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1109 | case MAKE_FUNCTION: |
| 1110 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1111 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | case BUILD_SLICE: |
| 1113 | if (oparg == 3) |
| 1114 | return -2; |
| 1115 | else |
| 1116 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1117 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1118 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | case LOAD_CLOSURE: |
| 1120 | return 1; |
| 1121 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1122 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | return 1; |
| 1124 | case STORE_DEREF: |
| 1125 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1126 | case DELETE_DEREF: |
| 1127 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1128 | |
| 1129 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1130 | case GET_AWAITABLE: |
| 1131 | return 0; |
| 1132 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1133 | /* 0 in the normal flow. |
| 1134 | * Restore the stack position to the position before the result |
| 1135 | * of __aenter__ and push 6 values before jumping to the handler |
| 1136 | * if an exception be raised. */ |
| 1137 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1138 | case BEFORE_ASYNC_WITH: |
| 1139 | return 1; |
| 1140 | case GET_AITER: |
| 1141 | return 0; |
| 1142 | case GET_ANEXT: |
| 1143 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1144 | case GET_YIELD_FROM_ITER: |
| 1145 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1146 | case END_ASYNC_FOR: |
| 1147 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1148 | case FORMAT_VALUE: |
| 1149 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1150 | else 1->1. */ |
| 1151 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1152 | case LOAD_METHOD: |
| 1153 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1154 | case LOAD_ASSERTION_ERROR: |
| 1155 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1156 | case LIST_TO_TUPLE: |
| 1157 | return 0; |
| 1158 | case LIST_EXTEND: |
| 1159 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1160 | case DICT_MERGE: |
| 1161 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1162 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1163 | case COPY_DICT_WITHOUT_KEYS: |
| 1164 | return 0; |
| 1165 | case MATCH_CLASS: |
| 1166 | return -1; |
| 1167 | case GET_LEN: |
| 1168 | case MATCH_MAPPING: |
| 1169 | case MATCH_SEQUENCE: |
| 1170 | return 1; |
| 1171 | case MATCH_KEYS: |
| 1172 | return 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1174 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1176 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1179 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1180 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1181 | { |
| 1182 | return stack_effect(opcode, oparg, jump); |
| 1183 | } |
| 1184 | |
| 1185 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1186 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1187 | { |
| 1188 | return stack_effect(opcode, oparg, -1); |
| 1189 | } |
| 1190 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1191 | /* Add an opcode with no argument. |
| 1192 | Returns 0 on failure, 1 on success. |
| 1193 | */ |
| 1194 | |
| 1195 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1196 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | basicblock *b; |
| 1199 | struct instr *i; |
| 1200 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1201 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1202 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | if (off < 0) |
| 1204 | return 0; |
| 1205 | b = c->u->u_curblock; |
| 1206 | i = &b->b_instr[off]; |
| 1207 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1208 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | if (opcode == RETURN_VALUE) |
| 1210 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1211 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1215 | static int |
| 1216 | compiler_addop(struct compiler *c, int opcode) |
| 1217 | { |
| 1218 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1219 | } |
| 1220 | |
| 1221 | static int |
| 1222 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1223 | { |
| 1224 | return compiler_addop_line(c, opcode, -1); |
| 1225 | } |
| 1226 | |
| 1227 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1228 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1229 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1230 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1231 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1233 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1234 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1236 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1238 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1239 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1240 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | return -1; |
| 1243 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1244 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | Py_DECREF(v); |
| 1246 | return -1; |
| 1247 | } |
| 1248 | Py_DECREF(v); |
| 1249 | } |
| 1250 | else |
| 1251 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1252 | return arg; |
| 1253 | } |
| 1254 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1255 | // Merge const *o* recursively and return constant key object. |
| 1256 | static PyObject* |
| 1257 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1258 | { |
| 1259 | // None and Ellipsis are singleton, and key is the singleton. |
| 1260 | // No need to merge object and key. |
| 1261 | if (o == Py_None || o == Py_Ellipsis) { |
| 1262 | Py_INCREF(o); |
| 1263 | return o; |
| 1264 | } |
| 1265 | |
| 1266 | PyObject *key = _PyCode_ConstantKey(o); |
| 1267 | if (key == NULL) { |
| 1268 | return NULL; |
| 1269 | } |
| 1270 | |
| 1271 | // t is borrowed reference |
| 1272 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1273 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1274 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1275 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1276 | Py_DECREF(key); |
| 1277 | return t; |
| 1278 | } |
| 1279 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1280 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1281 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1282 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1283 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1284 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1285 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1286 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1287 | PyObject *u = merge_consts_recursive(c, item); |
| 1288 | if (u == NULL) { |
| 1289 | Py_DECREF(key); |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | |
| 1293 | // See _PyCode_ConstantKey() |
| 1294 | PyObject *v; // borrowed |
| 1295 | if (PyTuple_CheckExact(u)) { |
| 1296 | v = PyTuple_GET_ITEM(u, 1); |
| 1297 | } |
| 1298 | else { |
| 1299 | v = u; |
| 1300 | } |
| 1301 | if (v != item) { |
| 1302 | Py_INCREF(v); |
| 1303 | PyTuple_SET_ITEM(o, i, v); |
| 1304 | Py_DECREF(item); |
| 1305 | } |
| 1306 | |
| 1307 | Py_DECREF(u); |
| 1308 | } |
| 1309 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1310 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1311 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1312 | // constant keys. |
| 1313 | // See _PyCode_ConstantKey() for detail. |
| 1314 | assert(PyTuple_CheckExact(key)); |
| 1315 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1316 | |
| 1317 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1318 | if (len == 0) { // empty frozenset should not be re-created. |
| 1319 | return key; |
| 1320 | } |
| 1321 | PyObject *tuple = PyTuple_New(len); |
| 1322 | if (tuple == NULL) { |
| 1323 | Py_DECREF(key); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | Py_ssize_t i = 0, pos = 0; |
| 1327 | PyObject *item; |
| 1328 | Py_hash_t hash; |
| 1329 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1330 | PyObject *k = merge_consts_recursive(c, item); |
| 1331 | if (k == NULL) { |
| 1332 | Py_DECREF(tuple); |
| 1333 | Py_DECREF(key); |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | PyObject *u; |
| 1337 | if (PyTuple_CheckExact(k)) { |
| 1338 | u = PyTuple_GET_ITEM(k, 1); |
| 1339 | Py_INCREF(u); |
| 1340 | Py_DECREF(k); |
| 1341 | } |
| 1342 | else { |
| 1343 | u = k; |
| 1344 | } |
| 1345 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1346 | i++; |
| 1347 | } |
| 1348 | |
| 1349 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1350 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1351 | PyObject *new = PyFrozenSet_New(tuple); |
| 1352 | Py_DECREF(tuple); |
| 1353 | if (new == NULL) { |
| 1354 | Py_DECREF(key); |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1358 | Py_DECREF(o); |
| 1359 | PyTuple_SET_ITEM(key, 1, new); |
| 1360 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1361 | |
| 1362 | return key; |
| 1363 | } |
| 1364 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1365 | static Py_ssize_t |
| 1366 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1367 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1368 | PyObject *key = merge_consts_recursive(c, o); |
| 1369 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1370 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1371 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1372 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1373 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1374 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1379 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1380 | { |
| 1381 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1382 | if (arg < 0) |
| 1383 | return 0; |
| 1384 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1385 | } |
| 1386 | |
| 1387 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1388 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1390 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1391 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1392 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1393 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1394 | return compiler_addop_i(c, opcode, arg); |
| 1395 | } |
| 1396 | |
| 1397 | static int |
| 1398 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1400 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1401 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1402 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1403 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1404 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1405 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1406 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1407 | Py_DECREF(mangled); |
| 1408 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1409 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | return compiler_addop_i(c, opcode, arg); |
| 1411 | } |
| 1412 | |
| 1413 | /* Add an opcode with an integer argument. |
| 1414 | Returns 0 on failure, 1 on success. |
| 1415 | */ |
| 1416 | |
| 1417 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1418 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | struct instr *i; |
| 1421 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1422 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1423 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1424 | it in the C code (like Python/ceval.c). |
| 1425 | |
| 1426 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1427 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1428 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1429 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1430 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1431 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1432 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1433 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | if (off < 0) |
| 1435 | return 0; |
| 1436 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1437 | i->i_opcode = opcode; |
| 1438 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1439 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1443 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1444 | { |
| 1445 | assert(HAS_ARG(opcode)); |
| 1446 | assert(b != NULL); |
| 1447 | assert(target != NULL); |
| 1448 | |
| 1449 | int off = compiler_next_instr(b); |
| 1450 | struct instr *i = &b->b_instr[off]; |
| 1451 | if (off < 0) { |
| 1452 | return 0; |
| 1453 | } |
| 1454 | i->i_opcode = opcode; |
| 1455 | i->i_target = target; |
| 1456 | i->i_lineno = lineno; |
| 1457 | return 1; |
| 1458 | } |
| 1459 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1460 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1461 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1462 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1463 | 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] | 1464 | } |
| 1465 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1466 | static int |
| 1467 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1468 | { |
| 1469 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1470 | } |
| 1471 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1472 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1473 | to the new block. |
| 1474 | |
| 1475 | The returns inside this macro make it impossible to decref objects |
| 1476 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1477 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1478 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | if (compiler_next_block((C)) == NULL) \ |
| 1480 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | if (!compiler_addop((C), (OP))) \ |
| 1485 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1488 | #define ADDOP_NOLINE(C, OP) { \ |
| 1489 | if (!compiler_addop_noline((C), (OP))) \ |
| 1490 | return 0; \ |
| 1491 | } |
| 1492 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1493 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | if (!compiler_addop((C), (OP))) { \ |
| 1495 | compiler_exit_scope(c); \ |
| 1496 | return 0; \ |
| 1497 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1500 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1501 | if (!compiler_addop_load_const((C), (O))) \ |
| 1502 | return 0; \ |
| 1503 | } |
| 1504 | |
| 1505 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1506 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1507 | PyObject *__new_const = (O); \ |
| 1508 | if (__new_const == NULL) { \ |
| 1509 | return 0; \ |
| 1510 | } \ |
| 1511 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1512 | Py_DECREF(__new_const); \ |
| 1513 | return 0; \ |
| 1514 | } \ |
| 1515 | Py_DECREF(__new_const); \ |
| 1516 | } |
| 1517 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1518 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1520 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1523 | /* Same as ADDOP_O, but steals a reference. */ |
| 1524 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1525 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1526 | Py_DECREF((O)); \ |
| 1527 | return 0; \ |
| 1528 | } \ |
| 1529 | Py_DECREF((O)); \ |
| 1530 | } |
| 1531 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1534 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1539 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1542 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1543 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1547 | /* Add a jump with no line number. |
| 1548 | * Used for artificial jumps that have no corresponding |
| 1549 | * token in the source code. */ |
| 1550 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1551 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1552 | return 0; \ |
| 1553 | } |
| 1554 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1555 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1556 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1557 | return 0; \ |
| 1558 | } |
| 1559 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1560 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1561 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1562 | */ |
| 1563 | |
| 1564 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1566 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1569 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1571 | compiler_exit_scope(c); \ |
| 1572 | return 0; \ |
| 1573 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1576 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1578 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1583 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1585 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1586 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1587 | return 0; \ |
| 1588 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1591 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1593 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1595 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1596 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1597 | compiler_exit_scope(c); \ |
| 1598 | return 0; \ |
| 1599 | } \ |
| 1600 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1603 | #define RETURN_IF_FALSE(X) \ |
| 1604 | if (!(X)) { \ |
| 1605 | return 0; \ |
| 1606 | } |
| 1607 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1608 | /* Search if variable annotations are present statically in a block. */ |
| 1609 | |
| 1610 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1611 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1612 | { |
| 1613 | int i, j, res = 0; |
| 1614 | stmt_ty st; |
| 1615 | |
| 1616 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1617 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1618 | switch (st->kind) { |
| 1619 | case AnnAssign_kind: |
| 1620 | return 1; |
| 1621 | case For_kind: |
| 1622 | res = find_ann(st->v.For.body) || |
| 1623 | find_ann(st->v.For.orelse); |
| 1624 | break; |
| 1625 | case AsyncFor_kind: |
| 1626 | res = find_ann(st->v.AsyncFor.body) || |
| 1627 | find_ann(st->v.AsyncFor.orelse); |
| 1628 | break; |
| 1629 | case While_kind: |
| 1630 | res = find_ann(st->v.While.body) || |
| 1631 | find_ann(st->v.While.orelse); |
| 1632 | break; |
| 1633 | case If_kind: |
| 1634 | res = find_ann(st->v.If.body) || |
| 1635 | find_ann(st->v.If.orelse); |
| 1636 | break; |
| 1637 | case With_kind: |
| 1638 | res = find_ann(st->v.With.body); |
| 1639 | break; |
| 1640 | case AsyncWith_kind: |
| 1641 | res = find_ann(st->v.AsyncWith.body); |
| 1642 | break; |
| 1643 | case Try_kind: |
| 1644 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1645 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1646 | st->v.Try.handlers, j); |
| 1647 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1648 | return 1; |
| 1649 | } |
| 1650 | } |
| 1651 | res = find_ann(st->v.Try.body) || |
| 1652 | find_ann(st->v.Try.finalbody) || |
| 1653 | find_ann(st->v.Try.orelse); |
| 1654 | break; |
| 1655 | default: |
| 1656 | res = 0; |
| 1657 | } |
| 1658 | if (res) { |
| 1659 | break; |
| 1660 | } |
| 1661 | } |
| 1662 | return res; |
| 1663 | } |
| 1664 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1665 | /* |
| 1666 | * Frame block handling functions |
| 1667 | */ |
| 1668 | |
| 1669 | static int |
| 1670 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1671 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1672 | { |
| 1673 | struct fblockinfo *f; |
| 1674 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1675 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1676 | } |
| 1677 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1678 | f->fb_type = t; |
| 1679 | f->fb_block = b; |
| 1680 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1681 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1682 | return 1; |
| 1683 | } |
| 1684 | |
| 1685 | static void |
| 1686 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1687 | { |
| 1688 | struct compiler_unit *u = c->u; |
| 1689 | assert(u->u_nfblocks > 0); |
| 1690 | u->u_nfblocks--; |
| 1691 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1692 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1693 | } |
| 1694 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1695 | static int |
| 1696 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1697 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1698 | ADDOP(c, DUP_TOP); |
| 1699 | ADDOP(c, DUP_TOP); |
| 1700 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1701 | return 1; |
| 1702 | } |
| 1703 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1704 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1705 | * popping the blocks will be restored afterwards, unless another |
| 1706 | * return, break or continue is found. In which case, the TOS will |
| 1707 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1708 | */ |
| 1709 | static int |
| 1710 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1711 | int preserve_tos) |
| 1712 | { |
| 1713 | switch (info->fb_type) { |
| 1714 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1715 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1716 | return 1; |
| 1717 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1718 | case FOR_LOOP: |
| 1719 | /* Pop the iterator */ |
| 1720 | if (preserve_tos) { |
| 1721 | ADDOP(c, ROT_TWO); |
| 1722 | } |
| 1723 | ADDOP(c, POP_TOP); |
| 1724 | return 1; |
| 1725 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1726 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1727 | ADDOP(c, POP_BLOCK); |
| 1728 | return 1; |
| 1729 | |
| 1730 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1731 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1732 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1733 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1734 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1735 | return 0; |
| 1736 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1737 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1738 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1739 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1740 | if (preserve_tos) { |
| 1741 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1742 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1743 | /* The finally block should appear to execute after the |
| 1744 | * statement causing the unwinding, so make the unwinding |
| 1745 | * instruction artificial */ |
| 1746 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1747 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1748 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1749 | case FINALLY_END: |
| 1750 | if (preserve_tos) { |
| 1751 | ADDOP(c, ROT_FOUR); |
| 1752 | } |
| 1753 | ADDOP(c, POP_TOP); |
| 1754 | ADDOP(c, POP_TOP); |
| 1755 | ADDOP(c, POP_TOP); |
| 1756 | if (preserve_tos) { |
| 1757 | ADDOP(c, ROT_FOUR); |
| 1758 | } |
| 1759 | ADDOP(c, POP_EXCEPT); |
| 1760 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1761 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1762 | case WITH: |
| 1763 | case ASYNC_WITH: |
| 1764 | ADDOP(c, POP_BLOCK); |
| 1765 | if (preserve_tos) { |
| 1766 | ADDOP(c, ROT_TWO); |
| 1767 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1768 | if(!compiler_call_exit_with_nones(c)) { |
| 1769 | return 0; |
| 1770 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1771 | if (info->fb_type == ASYNC_WITH) { |
| 1772 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1773 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1774 | ADDOP(c, YIELD_FROM); |
| 1775 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1776 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1777 | return 1; |
| 1778 | |
| 1779 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1780 | if (info->fb_datum) { |
| 1781 | ADDOP(c, POP_BLOCK); |
| 1782 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1783 | if (preserve_tos) { |
| 1784 | ADDOP(c, ROT_FOUR); |
| 1785 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1786 | ADDOP(c, POP_EXCEPT); |
| 1787 | if (info->fb_datum) { |
| 1788 | ADDOP_LOAD_CONST(c, Py_None); |
| 1789 | compiler_nameop(c, info->fb_datum, Store); |
| 1790 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1791 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1792 | return 1; |
| 1793 | |
| 1794 | case POP_VALUE: |
| 1795 | if (preserve_tos) { |
| 1796 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1797 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1798 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1799 | return 1; |
| 1800 | } |
| 1801 | Py_UNREACHABLE(); |
| 1802 | } |
| 1803 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1804 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1805 | static int |
| 1806 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1807 | if (c->u->u_nfblocks == 0) { |
| 1808 | return 1; |
| 1809 | } |
| 1810 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1811 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1812 | *loop = top; |
| 1813 | return 1; |
| 1814 | } |
| 1815 | struct fblockinfo copy = *top; |
| 1816 | c->u->u_nfblocks--; |
| 1817 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1818 | return 0; |
| 1819 | } |
| 1820 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1821 | return 0; |
| 1822 | } |
| 1823 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1824 | c->u->u_nfblocks++; |
| 1825 | return 1; |
| 1826 | } |
| 1827 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1828 | /* Compile a sequence of statements, checking for a docstring |
| 1829 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1830 | |
| 1831 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1832 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1834 | int i = 0; |
| 1835 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1836 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1837 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1838 | /* Set current line number to the line number of first statement. |
| 1839 | This way line number for SETUP_ANNOTATIONS will always |
| 1840 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1841 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1842 | 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] | 1843 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1844 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1845 | } |
| 1846 | /* Every annotated class and module should have __annotations__. */ |
| 1847 | if (find_ann(stmts)) { |
| 1848 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1849 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1850 | if (!asdl_seq_LEN(stmts)) |
| 1851 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1852 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1853 | if (c->c_optimize < 2) { |
| 1854 | docstring = _PyAST_GetDocString(stmts); |
| 1855 | if (docstring) { |
| 1856 | i = 1; |
| 1857 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1858 | assert(st->kind == Expr_kind); |
| 1859 | VISIT(c, expr, st->v.Expr.value); |
| 1860 | if (!compiler_nameop(c, __doc__, Store)) |
| 1861 | return 0; |
| 1862 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1864 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1865 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static PyCodeObject * |
| 1870 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | PyCodeObject *co; |
| 1873 | int addNone = 1; |
| 1874 | static PyObject *module; |
| 1875 | if (!module) { |
| 1876 | module = PyUnicode_InternFromString("<module>"); |
| 1877 | if (!module) |
| 1878 | return NULL; |
| 1879 | } |
| 1880 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1881 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | return NULL; |
| 1883 | switch (mod->kind) { |
| 1884 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1885 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | compiler_exit_scope(c); |
| 1887 | return 0; |
| 1888 | } |
| 1889 | break; |
| 1890 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1891 | if (find_ann(mod->v.Interactive.body)) { |
| 1892 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1893 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1895 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | break; |
| 1897 | case Expression_kind: |
| 1898 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1899 | addNone = 0; |
| 1900 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | default: |
| 1902 | PyErr_Format(PyExc_SystemError, |
| 1903 | "module kind %d should not be possible", |
| 1904 | mod->kind); |
| 1905 | return 0; |
| 1906 | } |
| 1907 | co = assemble(c, addNone); |
| 1908 | compiler_exit_scope(c); |
| 1909 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1912 | /* The test for LOCAL must come before the test for FREE in order to |
| 1913 | handle classes where name is both local and free. The local var is |
| 1914 | 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] | 1915 | */ |
| 1916 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1917 | static int |
| 1918 | get_ref_type(struct compiler *c, PyObject *name) |
| 1919 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1920 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1921 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1922 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1923 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1924 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1926 | PyErr_Format(PyExc_SystemError, |
| 1927 | "PyST_GetScope(name=%R) failed: " |
| 1928 | "unknown scope in unit %S (%R); " |
| 1929 | "symbols: %R; locals: %R; globals: %R", |
| 1930 | name, |
| 1931 | c->u->u_name, c->u->u_ste->ste_id, |
| 1932 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1933 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | static int |
| 1939 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1940 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1941 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1942 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1943 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1944 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1945 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1949 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1950 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1951 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1952 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1953 | if (qualname == NULL) |
| 1954 | qualname = co->co_name; |
| 1955 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1956 | if (free) { |
| 1957 | for (i = 0; i < free; ++i) { |
| 1958 | /* Bypass com_addop_varname because it will generate |
| 1959 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1960 | */ |
| 1961 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1962 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1963 | /* Special case: If a class contains a method with a |
| 1964 | free variable that has the same name as a method, |
| 1965 | the name will be considered free *and* local in the |
| 1966 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1967 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1968 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1969 | int reftype = get_ref_type(c, name); |
| 1970 | if (reftype == -1) { |
| 1971 | return 0; |
| 1972 | } |
| 1973 | int arg; |
| 1974 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1975 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1976 | } |
| 1977 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1978 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1979 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1980 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1981 | PyErr_Format(PyExc_SystemError, |
| 1982 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 1983 | "freevars of code %S: %R", |
| 1984 | name, |
| 1985 | reftype, |
| 1986 | c->u->u_name, |
| 1987 | co->co_name, |
| 1988 | co->co_freevars); |
| 1989 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1990 | } |
| 1991 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1993 | flags |= 0x08; |
| 1994 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1996 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1997 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1998 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2003 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | if (!decos) |
| 2008 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2011 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2012 | } |
| 2013 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2017 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2018 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2019 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2020 | /* Push a dict of keyword-only default values. |
| 2021 | |
| 2022 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2023 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2024 | int i; |
| 2025 | PyObject *keys = NULL; |
| 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2028 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2029 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2030 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2031 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2032 | if (!mangled) { |
| 2033 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2035 | if (keys == NULL) { |
| 2036 | keys = PyList_New(1); |
| 2037 | if (keys == NULL) { |
| 2038 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2039 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2040 | } |
| 2041 | PyList_SET_ITEM(keys, 0, mangled); |
| 2042 | } |
| 2043 | else { |
| 2044 | int res = PyList_Append(keys, mangled); |
| 2045 | Py_DECREF(mangled); |
| 2046 | if (res == -1) { |
| 2047 | goto error; |
| 2048 | } |
| 2049 | } |
| 2050 | if (!compiler_visit_expr(c, default_)) { |
| 2051 | goto error; |
| 2052 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | } |
| 2054 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2055 | if (keys != NULL) { |
| 2056 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2057 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2058 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2059 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2060 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2061 | assert(default_count > 0); |
| 2062 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2063 | } |
| 2064 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2065 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | error: |
| 2069 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2070 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2074 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2075 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2076 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2077 | return 1; |
| 2078 | } |
| 2079 | |
| 2080 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2081 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2082 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2083 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2085 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2086 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2087 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2088 | |
| 2089 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2090 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2091 | VISIT(c, annexpr, annotation); |
| 2092 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2094 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2098 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2099 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2100 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2101 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2103 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2104 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | c, |
| 2106 | arg->arg, |
| 2107 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2108 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2109 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2111 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | static int |
| 2115 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2116 | expr_ty returns) |
| 2117 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2118 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2119 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2120 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2121 | 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] | 2122 | */ |
| 2123 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2124 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2125 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2126 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2127 | return 0; |
| 2128 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2129 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2130 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2131 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2132 | args->vararg->annotation, &annotations_len)) |
| 2133 | return 0; |
| 2134 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2135 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2136 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2137 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2138 | args->kwarg->annotation, &annotations_len)) |
| 2139 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | if (!return_str) { |
| 2142 | return_str = PyUnicode_InternFromString("return"); |
| 2143 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2144 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2146 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2147 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2150 | if (annotations_len) { |
| 2151 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2152 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2154 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2155 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2159 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2160 | { |
| 2161 | VISIT_SEQ(c, expr, args->defaults); |
| 2162 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2163 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2166 | static Py_ssize_t |
| 2167 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2168 | { |
| 2169 | Py_ssize_t funcflags = 0; |
| 2170 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2171 | if (!compiler_visit_defaults(c, args)) |
| 2172 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2173 | funcflags |= 0x01; |
| 2174 | } |
| 2175 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2176 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2177 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2178 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2179 | return -1; |
| 2180 | } |
| 2181 | else if (res > 0) { |
| 2182 | funcflags |= 0x02; |
| 2183 | } |
| 2184 | } |
| 2185 | return funcflags; |
| 2186 | } |
| 2187 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2188 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2189 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2190 | { |
| 2191 | |
| 2192 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2193 | compiler_error(c, "cannot assign to __debug__"); |
| 2194 | return 1; |
| 2195 | } |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
| 2199 | static int |
| 2200 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2201 | { |
| 2202 | if (arg != NULL) { |
| 2203 | if (forbidden_name(c, arg->arg, Store)) |
| 2204 | return 0; |
| 2205 | } |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2210 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2211 | { |
| 2212 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2213 | 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] | 2214 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2215 | return 0; |
| 2216 | } |
| 2217 | } |
| 2218 | return 1; |
| 2219 | } |
| 2220 | |
| 2221 | static int |
| 2222 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2223 | { |
| 2224 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2225 | return 0; |
| 2226 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2227 | return 0; |
| 2228 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2229 | return 0; |
| 2230 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2231 | return 0; |
| 2232 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2233 | return 0; |
| 2234 | return 1; |
| 2235 | } |
| 2236 | |
| 2237 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2238 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2241 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2242 | arguments_ty args; |
| 2243 | expr_ty returns; |
| 2244 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2245 | asdl_expr_seq* decos; |
| 2246 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2247 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2248 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2249 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2250 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2251 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2252 | if (is_async) { |
| 2253 | assert(s->kind == AsyncFunctionDef_kind); |
| 2254 | |
| 2255 | args = s->v.AsyncFunctionDef.args; |
| 2256 | returns = s->v.AsyncFunctionDef.returns; |
| 2257 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2258 | name = s->v.AsyncFunctionDef.name; |
| 2259 | body = s->v.AsyncFunctionDef.body; |
| 2260 | |
| 2261 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2262 | } else { |
| 2263 | assert(s->kind == FunctionDef_kind); |
| 2264 | |
| 2265 | args = s->v.FunctionDef.args; |
| 2266 | returns = s->v.FunctionDef.returns; |
| 2267 | decos = s->v.FunctionDef.decorator_list; |
| 2268 | name = s->v.FunctionDef.name; |
| 2269 | body = s->v.FunctionDef.body; |
| 2270 | |
| 2271 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2272 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2273 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2274 | if (!compiler_check_debug_args(c, args)) |
| 2275 | return 0; |
| 2276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | if (!compiler_decorators(c, decos)) |
| 2278 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2279 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2280 | firstlineno = s->lineno; |
| 2281 | if (asdl_seq_LEN(decos)) { |
| 2282 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2283 | } |
| 2284 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2285 | funcflags = compiler_default_arguments(c, args); |
| 2286 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2288 | } |
| 2289 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2290 | annotations = compiler_visit_annotations(c, args, returns); |
| 2291 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2292 | return 0; |
| 2293 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2294 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2295 | funcflags |= 0x04; |
| 2296 | } |
| 2297 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2298 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2299 | return 0; |
| 2300 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2301 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2302 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2303 | if (c->c_optimize < 2) { |
| 2304 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2305 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2306 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | compiler_exit_scope(c); |
| 2308 | return 0; |
| 2309 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2312 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2314 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2315 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2316 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2318 | qualname = c->u->u_qualname; |
| 2319 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2321 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2322 | Py_XDECREF(qualname); |
| 2323 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2325 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2326 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2327 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2328 | Py_DECREF(qualname); |
| 2329 | Py_DECREF(co); |
| 2330 | return 0; |
| 2331 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2332 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | /* decorators */ |
| 2336 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2337 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2338 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2339 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2340 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | static int |
| 2344 | compiler_class(struct compiler *c, stmt_ty s) |
| 2345 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | PyCodeObject *co; |
| 2347 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2348 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2349 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | if (!compiler_decorators(c, decos)) |
| 2352 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2353 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2354 | firstlineno = s->lineno; |
| 2355 | if (asdl_seq_LEN(decos)) { |
| 2356 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2357 | } |
| 2358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | /* ultimately generate code for: |
| 2360 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2361 | where: |
| 2362 | <func> is a function/closure created from the class body; |
| 2363 | it has a single argument (__locals__) where the dict |
| 2364 | (or MutableSequence) representing the locals is passed |
| 2365 | <name> is the class name |
| 2366 | <bases> is the positional arguments and *varargs argument |
| 2367 | <keywords> is the keyword arguments and **kwds argument |
| 2368 | This borrows from compiler_call. |
| 2369 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2372 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2373 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | return 0; |
| 2375 | /* this block represents what we do in the new scope */ |
| 2376 | { |
| 2377 | /* use the class name for name mangling */ |
| 2378 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2379 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | /* load (global) __name__ ... */ |
| 2381 | str = PyUnicode_InternFromString("__name__"); |
| 2382 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2383 | Py_XDECREF(str); |
| 2384 | compiler_exit_scope(c); |
| 2385 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2386 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | Py_DECREF(str); |
| 2388 | /* ... and store it as __module__ */ |
| 2389 | str = PyUnicode_InternFromString("__module__"); |
| 2390 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2391 | Py_XDECREF(str); |
| 2392 | compiler_exit_scope(c); |
| 2393 | return 0; |
| 2394 | } |
| 2395 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2396 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2397 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2398 | str = PyUnicode_InternFromString("__qualname__"); |
| 2399 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2400 | Py_XDECREF(str); |
| 2401 | compiler_exit_scope(c); |
| 2402 | return 0; |
| 2403 | } |
| 2404 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2406 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | compiler_exit_scope(c); |
| 2408 | return 0; |
| 2409 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2410 | /* The following code is artificial */ |
| 2411 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2412 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2413 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2414 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2415 | str = PyUnicode_InternFromString("__class__"); |
| 2416 | if (str == NULL) { |
| 2417 | compiler_exit_scope(c); |
| 2418 | return 0; |
| 2419 | } |
| 2420 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2421 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2422 | if (i < 0) { |
| 2423 | compiler_exit_scope(c); |
| 2424 | return 0; |
| 2425 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2426 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2428 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2429 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2430 | str = PyUnicode_InternFromString("__classcell__"); |
| 2431 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2432 | Py_XDECREF(str); |
| 2433 | compiler_exit_scope(c); |
| 2434 | return 0; |
| 2435 | } |
| 2436 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2438 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2439 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2440 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2441 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2442 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2443 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | /* create the code object */ |
| 2445 | co = assemble(c, 1); |
| 2446 | } |
| 2447 | /* leave the new scope */ |
| 2448 | compiler_exit_scope(c); |
| 2449 | if (co == NULL) |
| 2450 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | /* 2. load the 'build_class' function */ |
| 2453 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2454 | |
| 2455 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2456 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2457 | Py_DECREF(co); |
| 2458 | return 0; |
| 2459 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | Py_DECREF(co); |
| 2461 | |
| 2462 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2463 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | |
| 2465 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2466 | 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] | 2467 | return 0; |
| 2468 | |
| 2469 | /* 6. apply decorators */ |
| 2470 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2471 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2472 | } |
| 2473 | |
| 2474 | /* 7. store into <name> */ |
| 2475 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2476 | return 0; |
| 2477 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2480 | /* Return 0 if the expression is a constant value except named singletons. |
| 2481 | Return 1 otherwise. */ |
| 2482 | static int |
| 2483 | check_is_arg(expr_ty e) |
| 2484 | { |
| 2485 | if (e->kind != Constant_kind) { |
| 2486 | return 1; |
| 2487 | } |
| 2488 | PyObject *value = e->v.Constant.value; |
| 2489 | return (value == Py_None |
| 2490 | || value == Py_False |
| 2491 | || value == Py_True |
| 2492 | || value == Py_Ellipsis); |
| 2493 | } |
| 2494 | |
| 2495 | /* Check operands of identity chacks ("is" and "is not"). |
| 2496 | Emit a warning if any operand is a constant except named singletons. |
| 2497 | Return 0 on error. |
| 2498 | */ |
| 2499 | static int |
| 2500 | check_compare(struct compiler *c, expr_ty e) |
| 2501 | { |
| 2502 | Py_ssize_t i, n; |
| 2503 | int left = check_is_arg(e->v.Compare.left); |
| 2504 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2505 | for (i = 0; i < n; i++) { |
| 2506 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2507 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2508 | if (op == Is || op == IsNot) { |
| 2509 | if (!right || !left) { |
| 2510 | const char *msg = (op == Is) |
| 2511 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2512 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2513 | return compiler_warn(c, msg); |
| 2514 | } |
| 2515 | } |
| 2516 | left = right; |
| 2517 | } |
| 2518 | return 1; |
| 2519 | } |
| 2520 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2521 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2522 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2523 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2524 | switch (op) { |
| 2525 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2526 | cmp = Py_EQ; |
| 2527 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2528 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2529 | cmp = Py_NE; |
| 2530 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2531 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2532 | cmp = Py_LT; |
| 2533 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2534 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2535 | cmp = Py_LE; |
| 2536 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2537 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2538 | cmp = Py_GT; |
| 2539 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2540 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2541 | cmp = Py_GE; |
| 2542 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2543 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2544 | ADDOP_I(c, IS_OP, 0); |
| 2545 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2546 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2547 | ADDOP_I(c, IS_OP, 1); |
| 2548 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2549 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2550 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2551 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2552 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2553 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2554 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2555 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2556 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2557 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2558 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2559 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2560 | } |
| 2561 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2562 | |
| 2563 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2564 | static int |
| 2565 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2566 | { |
| 2567 | switch (e->kind) { |
| 2568 | case UnaryOp_kind: |
| 2569 | if (e->v.UnaryOp.op == Not) |
| 2570 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2571 | /* fallback to general implementation */ |
| 2572 | break; |
| 2573 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2574 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2575 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2576 | assert(n >= 0); |
| 2577 | int cond2 = e->v.BoolOp.op == Or; |
| 2578 | basicblock *next2 = next; |
| 2579 | if (!cond2 != !cond) { |
| 2580 | next2 = compiler_new_block(c); |
| 2581 | if (next2 == NULL) |
| 2582 | return 0; |
| 2583 | } |
| 2584 | for (i = 0; i < n; ++i) { |
| 2585 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2586 | return 0; |
| 2587 | } |
| 2588 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2589 | return 0; |
| 2590 | if (next2 != next) |
| 2591 | compiler_use_next_block(c, next2); |
| 2592 | return 1; |
| 2593 | } |
| 2594 | case IfExp_kind: { |
| 2595 | basicblock *end, *next2; |
| 2596 | end = compiler_new_block(c); |
| 2597 | if (end == NULL) |
| 2598 | return 0; |
| 2599 | next2 = compiler_new_block(c); |
| 2600 | if (next2 == NULL) |
| 2601 | return 0; |
| 2602 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2603 | return 0; |
| 2604 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2605 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2606 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2607 | compiler_use_next_block(c, next2); |
| 2608 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2609 | return 0; |
| 2610 | compiler_use_next_block(c, end); |
| 2611 | return 1; |
| 2612 | } |
| 2613 | case Compare_kind: { |
| 2614 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2615 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2616 | if (!check_compare(c, e)) { |
| 2617 | return 0; |
| 2618 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2619 | basicblock *cleanup = compiler_new_block(c); |
| 2620 | if (cleanup == NULL) |
| 2621 | return 0; |
| 2622 | VISIT(c, expr, e->v.Compare.left); |
| 2623 | for (i = 0; i < n; i++) { |
| 2624 | VISIT(c, expr, |
| 2625 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2626 | ADDOP(c, DUP_TOP); |
| 2627 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2628 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2629 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2630 | NEXT_BLOCK(c); |
| 2631 | } |
| 2632 | 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] | 2633 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2634 | 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] | 2635 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2636 | basicblock *end = compiler_new_block(c); |
| 2637 | if (end == NULL) |
| 2638 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2639 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2640 | compiler_use_next_block(c, cleanup); |
| 2641 | ADDOP(c, POP_TOP); |
| 2642 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2643 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2644 | } |
| 2645 | compiler_use_next_block(c, end); |
| 2646 | return 1; |
| 2647 | } |
| 2648 | /* fallback to general implementation */ |
| 2649 | break; |
| 2650 | } |
| 2651 | default: |
| 2652 | /* fallback to general implementation */ |
| 2653 | break; |
| 2654 | } |
| 2655 | |
| 2656 | /* general implementation */ |
| 2657 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2658 | 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] | 2659 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2660 | return 1; |
| 2661 | } |
| 2662 | |
| 2663 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2664 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | basicblock *end, *next; |
| 2667 | |
| 2668 | assert(e->kind == IfExp_kind); |
| 2669 | end = compiler_new_block(c); |
| 2670 | if (end == NULL) |
| 2671 | return 0; |
| 2672 | next = compiler_new_block(c); |
| 2673 | if (next == NULL) |
| 2674 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2675 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2676 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2678 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | compiler_use_next_block(c, next); |
| 2680 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2681 | compiler_use_next_block(c, end); |
| 2682 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2686 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2689 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2691 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | arguments_ty args = e->v.Lambda.args; |
| 2693 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2694 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2695 | if (!compiler_check_debug_args(c, args)) |
| 2696 | return 0; |
| 2697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | if (!name) { |
| 2699 | name = PyUnicode_InternFromString("<lambda>"); |
| 2700 | if (!name) |
| 2701 | return 0; |
| 2702 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2703 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2704 | funcflags = compiler_default_arguments(c, args); |
| 2705 | if (funcflags == -1) { |
| 2706 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2708 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2709 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2710 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2713 | /* Make None the first constant, so the lambda can't have a |
| 2714 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2715 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2719 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2721 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2722 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2723 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2724 | } |
| 2725 | else { |
| 2726 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2727 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2729 | qualname = c->u->u_qualname; |
| 2730 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2732 | if (co == NULL) { |
| 2733 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2735 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2736 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2737 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2738 | Py_DECREF(qualname); |
| 2739 | Py_DECREF(co); |
| 2740 | return 0; |
| 2741 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2742 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | Py_DECREF(co); |
| 2744 | |
| 2745 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2749 | compiler_if(struct compiler *c, stmt_ty s) |
| 2750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | assert(s->kind == If_kind); |
| 2753 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2754 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2756 | } |
| 2757 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2758 | next = compiler_new_block(c); |
| 2759 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2760 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2761 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2762 | } |
| 2763 | else { |
| 2764 | next = end; |
| 2765 | } |
| 2766 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2767 | return 0; |
| 2768 | } |
| 2769 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2770 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2771 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2772 | compiler_use_next_block(c, next); |
| 2773 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | } |
| 2775 | compiler_use_next_block(c, end); |
| 2776 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | static int |
| 2780 | compiler_for(struct compiler *c, stmt_ty s) |
| 2781 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2782 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2785 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | cleanup = compiler_new_block(c); |
| 2787 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2788 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2790 | } |
| 2791 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2793 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | VISIT(c, expr, s->v.For.iter); |
| 2795 | ADDOP(c, GET_ITER); |
| 2796 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2797 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2798 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | VISIT(c, expr, s->v.For.target); |
| 2800 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2801 | /* Mark jump as artificial */ |
| 2802 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2803 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2805 | |
| 2806 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2809 | compiler_use_next_block(c, end); |
| 2810 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2813 | |
| 2814 | static int |
| 2815 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2816 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2817 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2818 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2819 | c->u->u_ste->ste_coroutine = 1; |
| 2820 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2821 | return compiler_error(c, "'async for' outside async function"); |
| 2822 | } |
| 2823 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2824 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2825 | except = compiler_new_block(c); |
| 2826 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2827 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2828 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2829 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2830 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2831 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2832 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2833 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2834 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2835 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2836 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2837 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2838 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2839 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2840 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2841 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2842 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2843 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2844 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2845 | /* Success block for __anext__ */ |
| 2846 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2847 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2848 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2849 | |
| 2850 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2851 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2852 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2853 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2854 | |
| 2855 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2856 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2857 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2858 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2859 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2860 | |
| 2861 | compiler_use_next_block(c, end); |
| 2862 | |
| 2863 | return 1; |
| 2864 | } |
| 2865 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2866 | static int |
| 2867 | compiler_while(struct compiler *c, stmt_ty s) |
| 2868 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2869 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2871 | body = compiler_new_block(c); |
| 2872 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2874 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2878 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2881 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2882 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2887 | SET_LOC(c, s); |
| 2888 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2889 | return 0; |
| 2890 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2892 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2893 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2894 | compiler_use_next_block(c, anchor); |
| 2895 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2897 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2904 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2905 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2906 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2907 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2908 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2909 | return compiler_error(c, "'return' outside function"); |
| 2910 | if (s->v.Return.value != NULL && |
| 2911 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2912 | { |
| 2913 | return compiler_error( |
| 2914 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2915 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2916 | if (preserve_tos) { |
| 2917 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2918 | } else { |
| 2919 | /* Emit instruction with line number for expression */ |
| 2920 | if (s->v.Return.value != NULL) { |
| 2921 | SET_LOC(c, s->v.Return.value); |
| 2922 | ADDOP(c, NOP); |
| 2923 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2924 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2925 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2926 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2927 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2928 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2929 | } |
| 2930 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2931 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2932 | } |
| 2933 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2934 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2939 | static int |
| 2940 | compiler_break(struct compiler *c) |
| 2941 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2942 | struct fblockinfo *loop = NULL; |
| 2943 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2944 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2945 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2946 | if (loop == NULL) { |
| 2947 | return compiler_error(c, "'break' outside loop"); |
| 2948 | } |
| 2949 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2950 | return 0; |
| 2951 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2952 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2953 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2954 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | static int |
| 2958 | compiler_continue(struct compiler *c) |
| 2959 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2960 | struct fblockinfo *loop = NULL; |
| 2961 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2962 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2963 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2964 | if (loop == NULL) { |
| 2965 | return compiler_error(c, "'continue' not properly in loop"); |
| 2966 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2967 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2968 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2969 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2973 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | |
| 2975 | SETUP_FINALLY L |
| 2976 | <code for body> |
| 2977 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2978 | <code for finalbody> |
| 2979 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2980 | L: |
| 2981 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2982 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2984 | The special instructions use the block stack. Each block |
| 2985 | stack entry contains the instruction that created it (here |
| 2986 | SETUP_FINALLY), the level of the value stack at the time the |
| 2987 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2989 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | Pushes the current value stack level and the label |
| 2991 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2992 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2993 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2995 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2996 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2997 | exceptions are pushed onto the value stack (and the exception |
| 2998 | condition is cleared), and the interpreter jumps to the label |
| 2999 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 | */ |
| 3001 | |
| 3002 | static int |
| 3003 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 3004 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3005 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | body = compiler_new_block(c); |
| 3008 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3009 | exit = compiler_new_block(c); |
| 3010 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3012 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3013 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3014 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3016 | 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] | 3017 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3018 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3019 | if (!compiler_try_except(c, s)) |
| 3020 | return 0; |
| 3021 | } |
| 3022 | else { |
| 3023 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3024 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3025 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3026 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3027 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3028 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3029 | /* `finally` block */ |
| 3030 | compiler_use_next_block(c, end); |
| 3031 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3032 | return 0; |
| 3033 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3034 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3035 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3036 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3041 | 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] | 3042 | (The contents of the value stack is shown in [], with the top |
| 3043 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3044 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | |
| 3046 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3047 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | [] <code for S> |
| 3049 | [] POP_BLOCK |
| 3050 | [] JUMP_FORWARD L0 |
| 3051 | |
| 3052 | [tb, val, exc] L1: DUP ) |
| 3053 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3054 | [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] | 3055 | [tb, val, exc] POP |
| 3056 | [tb, val] <assign to V1> (or POP if no V1) |
| 3057 | [tb] POP |
| 3058 | [] <code for S1> |
| 3059 | JUMP_FORWARD L0 |
| 3060 | |
| 3061 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3062 | .............................etc....................... |
| 3063 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3064 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | |
| 3066 | [] L0: <next statement> |
| 3067 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3068 | Of course, parts are not generated if Vi or Ei is not present. |
| 3069 | */ |
| 3070 | static int |
| 3071 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3074 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | body = compiler_new_block(c); |
| 3077 | except = compiler_new_block(c); |
| 3078 | orelse = compiler_new_block(c); |
| 3079 | end = compiler_new_block(c); |
| 3080 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3081 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3082 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3084 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3086 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3087 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3088 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3089 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3090 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3092 | /* Runtime will push a block here, so we need to account for that */ |
| 3093 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3094 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | for (i = 0; i < n; i++) { |
| 3096 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3097 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3099 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3100 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | except = compiler_new_block(c); |
| 3102 | if (except == NULL) |
| 3103 | return 0; |
| 3104 | if (handler->v.ExceptHandler.type) { |
| 3105 | ADDOP(c, DUP_TOP); |
| 3106 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3107 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3108 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | } |
| 3110 | ADDOP(c, POP_TOP); |
| 3111 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3112 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3113 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3114 | cleanup_end = compiler_new_block(c); |
| 3115 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3116 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3117 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3118 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3119 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3120 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3121 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3123 | /* |
| 3124 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3125 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3126 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3127 | try: |
| 3128 | # body |
| 3129 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3130 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3131 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3132 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3134 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3135 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3136 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3137 | 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] | 3138 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3140 | /* second # body */ |
| 3141 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3142 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3143 | ADDOP(c, POP_BLOCK); |
| 3144 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3145 | /* name = None; del name; # Mark as artificial */ |
| 3146 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3147 | ADDOP_LOAD_CONST(c, Py_None); |
| 3148 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3149 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3150 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3152 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3153 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3155 | /* name = None; del name; # Mark as artificial */ |
| 3156 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3157 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3158 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3159 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3161 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | } |
| 3163 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3164 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3166 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3167 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3168 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3170 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3171 | ADDOP(c, POP_TOP); |
| 3172 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3173 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3174 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3176 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3177 | /* name = None; del name; # Mark as artificial */ |
| 3178 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3179 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3180 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | compiler_use_next_block(c, except); |
| 3183 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3184 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3185 | /* Mark as artificial */ |
| 3186 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3187 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3189 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | compiler_use_next_block(c, end); |
| 3191 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3195 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3196 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3197 | return compiler_try_finally(c, s); |
| 3198 | else |
| 3199 | return compiler_try_except(c, s); |
| 3200 | } |
| 3201 | |
| 3202 | |
| 3203 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3204 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | /* The IMPORT_NAME opcode was already generated. This function |
| 3207 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | 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] | 3210 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3212 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3213 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3214 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3215 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3216 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3218 | while (1) { |
| 3219 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3221 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3222 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3223 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3224 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3226 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3227 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3228 | if (dot == -1) { |
| 3229 | break; |
| 3230 | } |
| 3231 | ADDOP(c, ROT_TWO); |
| 3232 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3234 | if (!compiler_nameop(c, asname, Store)) { |
| 3235 | return 0; |
| 3236 | } |
| 3237 | ADDOP(c, POP_TOP); |
| 3238 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | } |
| 3240 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | static int |
| 3244 | compiler_import(struct compiler *c, stmt_ty s) |
| 3245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | /* The Import node stores a module name like a.b.c as a single |
| 3247 | string. This is convenient for all cases except |
| 3248 | import a.b.c as d |
| 3249 | where we need to parse that string to extract the individual |
| 3250 | module names. |
| 3251 | XXX Perhaps change the representation to make this case simpler? |
| 3252 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3253 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3254 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3255 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | for (i = 0; i < n; i++) { |
| 3257 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3258 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3259 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3260 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3261 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3262 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | if (alias->asname) { |
| 3265 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3266 | if (!r) |
| 3267 | return r; |
| 3268 | } |
| 3269 | else { |
| 3270 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3271 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3272 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3273 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3274 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3275 | if (tmp == NULL) |
| 3276 | return 0; |
| 3277 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3279 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | Py_DECREF(tmp); |
| 3281 | } |
| 3282 | if (!r) |
| 3283 | return r; |
| 3284 | } |
| 3285 | } |
| 3286 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | static int |
| 3290 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3291 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3292 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3293 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | if (!empty_string) { |
| 3297 | empty_string = PyUnicode_FromString(""); |
| 3298 | if (!empty_string) |
| 3299 | return 0; |
| 3300 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3301 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3302 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3303 | |
| 3304 | names = PyTuple_New(n); |
| 3305 | if (!names) |
| 3306 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3308 | /* build up the names */ |
| 3309 | for (i = 0; i < n; i++) { |
| 3310 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3311 | Py_INCREF(alias->name); |
| 3312 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3313 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3316 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | Py_DECREF(names); |
| 3318 | return compiler_error(c, "from __future__ imports must occur " |
| 3319 | "at the beginning of the file"); |
| 3320 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3321 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | if (s->v.ImportFrom.module) { |
| 3324 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3325 | } |
| 3326 | else { |
| 3327 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3328 | } |
| 3329 | for (i = 0; i < n; i++) { |
| 3330 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3331 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3333 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | assert(n == 1); |
| 3335 | ADDOP(c, IMPORT_STAR); |
| 3336 | return 1; |
| 3337 | } |
| 3338 | |
| 3339 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3340 | store_name = alias->name; |
| 3341 | if (alias->asname) |
| 3342 | store_name = alias->asname; |
| 3343 | |
| 3344 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | return 0; |
| 3346 | } |
| 3347 | } |
| 3348 | /* remove imported module */ |
| 3349 | ADDOP(c, POP_TOP); |
| 3350 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | static int |
| 3354 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3357 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3358 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3359 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3361 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3362 | { |
| 3363 | if (!compiler_warn(c, "assertion is always true, " |
| 3364 | "perhaps remove parentheses?")) |
| 3365 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3366 | return 0; |
| 3367 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | end = compiler_new_block(c); |
| 3370 | if (end == NULL) |
| 3371 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3372 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3373 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3374 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3375 | if (s->v.Assert.msg) { |
| 3376 | VISIT(c, expr, s->v.Assert.msg); |
| 3377 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3378 | } |
| 3379 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3380 | compiler_use_next_block(c, end); |
| 3381 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3385 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3386 | { |
| 3387 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3388 | VISIT(c, expr, value); |
| 3389 | ADDOP(c, PRINT_EXPR); |
| 3390 | return 1; |
| 3391 | } |
| 3392 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3393 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3394 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3395 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3396 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | VISIT(c, expr, value); |
| 3400 | ADDOP(c, POP_TOP); |
| 3401 | return 1; |
| 3402 | } |
| 3403 | |
| 3404 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3405 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3406 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3407 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3409 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3410 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3412 | switch (s->kind) { |
| 3413 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3414 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3415 | case ClassDef_kind: |
| 3416 | return compiler_class(c, s); |
| 3417 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3418 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | case Delete_kind: |
| 3420 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3421 | break; |
| 3422 | case Assign_kind: |
| 3423 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3424 | VISIT(c, expr, s->v.Assign.value); |
| 3425 | for (i = 0; i < n; i++) { |
| 3426 | if (i < n - 1) |
| 3427 | ADDOP(c, DUP_TOP); |
| 3428 | VISIT(c, expr, |
| 3429 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3430 | } |
| 3431 | break; |
| 3432 | case AugAssign_kind: |
| 3433 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3434 | case AnnAssign_kind: |
| 3435 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | case For_kind: |
| 3437 | return compiler_for(c, s); |
| 3438 | case While_kind: |
| 3439 | return compiler_while(c, s); |
| 3440 | case If_kind: |
| 3441 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3442 | case Match_kind: |
| 3443 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | case Raise_kind: |
| 3445 | n = 0; |
| 3446 | if (s->v.Raise.exc) { |
| 3447 | VISIT(c, expr, s->v.Raise.exc); |
| 3448 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3449 | if (s->v.Raise.cause) { |
| 3450 | VISIT(c, expr, s->v.Raise.cause); |
| 3451 | n++; |
| 3452 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3454 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3455 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3456 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3457 | case Try_kind: |
| 3458 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | case Assert_kind: |
| 3460 | return compiler_assert(c, s); |
| 3461 | case Import_kind: |
| 3462 | return compiler_import(c, s); |
| 3463 | case ImportFrom_kind: |
| 3464 | return compiler_from_import(c, s); |
| 3465 | case Global_kind: |
| 3466 | case Nonlocal_kind: |
| 3467 | break; |
| 3468 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3469 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3471 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | break; |
| 3473 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3474 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3475 | case Continue_kind: |
| 3476 | return compiler_continue(c); |
| 3477 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3478 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3479 | case AsyncFunctionDef_kind: |
| 3480 | return compiler_function(c, s, 1); |
| 3481 | case AsyncWith_kind: |
| 3482 | return compiler_async_with(c, s, 0); |
| 3483 | case AsyncFor_kind: |
| 3484 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | static int |
| 3491 | unaryop(unaryop_ty op) |
| 3492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 | switch (op) { |
| 3494 | case Invert: |
| 3495 | return UNARY_INVERT; |
| 3496 | case Not: |
| 3497 | return UNARY_NOT; |
| 3498 | case UAdd: |
| 3499 | return UNARY_POSITIVE; |
| 3500 | case USub: |
| 3501 | return UNARY_NEGATIVE; |
| 3502 | default: |
| 3503 | PyErr_Format(PyExc_SystemError, |
| 3504 | "unary op %d should not be possible", op); |
| 3505 | return 0; |
| 3506 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3510 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3512 | switch (op) { |
| 3513 | case Add: |
| 3514 | return BINARY_ADD; |
| 3515 | case Sub: |
| 3516 | return BINARY_SUBTRACT; |
| 3517 | case Mult: |
| 3518 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3519 | case MatMult: |
| 3520 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | case Div: |
| 3522 | return BINARY_TRUE_DIVIDE; |
| 3523 | case Mod: |
| 3524 | return BINARY_MODULO; |
| 3525 | case Pow: |
| 3526 | return BINARY_POWER; |
| 3527 | case LShift: |
| 3528 | return BINARY_LSHIFT; |
| 3529 | case RShift: |
| 3530 | return BINARY_RSHIFT; |
| 3531 | case BitOr: |
| 3532 | return BINARY_OR; |
| 3533 | case BitXor: |
| 3534 | return BINARY_XOR; |
| 3535 | case BitAnd: |
| 3536 | return BINARY_AND; |
| 3537 | case FloorDiv: |
| 3538 | return BINARY_FLOOR_DIVIDE; |
| 3539 | default: |
| 3540 | PyErr_Format(PyExc_SystemError, |
| 3541 | "binary op %d should not be possible", op); |
| 3542 | return 0; |
| 3543 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3547 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3549 | switch (op) { |
| 3550 | case Add: |
| 3551 | return INPLACE_ADD; |
| 3552 | case Sub: |
| 3553 | return INPLACE_SUBTRACT; |
| 3554 | case Mult: |
| 3555 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3556 | case MatMult: |
| 3557 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3558 | case Div: |
| 3559 | return INPLACE_TRUE_DIVIDE; |
| 3560 | case Mod: |
| 3561 | return INPLACE_MODULO; |
| 3562 | case Pow: |
| 3563 | return INPLACE_POWER; |
| 3564 | case LShift: |
| 3565 | return INPLACE_LSHIFT; |
| 3566 | case RShift: |
| 3567 | return INPLACE_RSHIFT; |
| 3568 | case BitOr: |
| 3569 | return INPLACE_OR; |
| 3570 | case BitXor: |
| 3571 | return INPLACE_XOR; |
| 3572 | case BitAnd: |
| 3573 | return INPLACE_AND; |
| 3574 | case FloorDiv: |
| 3575 | return INPLACE_FLOOR_DIVIDE; |
| 3576 | default: |
| 3577 | PyErr_Format(PyExc_SystemError, |
| 3578 | "inplace binary op %d should not be possible", op); |
| 3579 | return 0; |
| 3580 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | static int |
| 3584 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3585 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3586 | int op, scope; |
| 3587 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3588 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 | PyObject *dict = c->u->u_names; |
| 3591 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3592 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3593 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3594 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3595 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3596 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3597 | if (forbidden_name(c, name, ctx)) |
| 3598 | return 0; |
| 3599 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3600 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3601 | if (!mangled) |
| 3602 | return 0; |
| 3603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3604 | op = 0; |
| 3605 | optype = OP_NAME; |
| 3606 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3607 | switch (scope) { |
| 3608 | case FREE: |
| 3609 | dict = c->u->u_freevars; |
| 3610 | optype = OP_DEREF; |
| 3611 | break; |
| 3612 | case CELL: |
| 3613 | dict = c->u->u_cellvars; |
| 3614 | optype = OP_DEREF; |
| 3615 | break; |
| 3616 | case LOCAL: |
| 3617 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3618 | optype = OP_FAST; |
| 3619 | break; |
| 3620 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3621 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | optype = OP_GLOBAL; |
| 3623 | break; |
| 3624 | case GLOBAL_EXPLICIT: |
| 3625 | optype = OP_GLOBAL; |
| 3626 | break; |
| 3627 | default: |
| 3628 | /* scope can be 0 */ |
| 3629 | break; |
| 3630 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | /* 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] | 3633 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3635 | switch (optype) { |
| 3636 | case OP_DEREF: |
| 3637 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3638 | case Load: |
| 3639 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3640 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3641 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3642 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | } |
| 3644 | break; |
| 3645 | case OP_FAST: |
| 3646 | switch (ctx) { |
| 3647 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3648 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3650 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3651 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3652 | return 1; |
| 3653 | case OP_GLOBAL: |
| 3654 | switch (ctx) { |
| 3655 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3656 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3657 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | } |
| 3659 | break; |
| 3660 | case OP_NAME: |
| 3661 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3662 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3663 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3665 | } |
| 3666 | break; |
| 3667 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3670 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3671 | Py_DECREF(mangled); |
| 3672 | if (arg < 0) |
| 3673 | return 0; |
| 3674 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | static int |
| 3678 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3680 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3681 | int jumpi; |
| 3682 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3683 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3685 | assert(e->kind == BoolOp_kind); |
| 3686 | if (e->v.BoolOp.op == And) |
| 3687 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3688 | else |
| 3689 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3690 | end = compiler_new_block(c); |
| 3691 | if (end == NULL) |
| 3692 | return 0; |
| 3693 | s = e->v.BoolOp.values; |
| 3694 | n = asdl_seq_LEN(s) - 1; |
| 3695 | assert(n >= 0); |
| 3696 | for (i = 0; i < n; ++i) { |
| 3697 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3698 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3699 | basicblock *next = compiler_new_block(c); |
| 3700 | if (next == NULL) { |
| 3701 | return 0; |
| 3702 | } |
| 3703 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3704 | } |
| 3705 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3706 | compiler_use_next_block(c, end); |
| 3707 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3711 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3712 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3713 | { |
| 3714 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3715 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3716 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3717 | PyObject *folded = PyTuple_New(n); |
| 3718 | if (folded == NULL) { |
| 3719 | return 0; |
| 3720 | } |
| 3721 | PyObject *val; |
| 3722 | for (i = 0; i < n; i++) { |
| 3723 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3724 | Py_INCREF(val); |
| 3725 | PyTuple_SET_ITEM(folded, i, val); |
| 3726 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3727 | if (tuple) { |
| 3728 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3729 | } else { |
| 3730 | if (add == SET_ADD) { |
| 3731 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3732 | if (folded == NULL) { |
| 3733 | return 0; |
| 3734 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3735 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3736 | ADDOP_I(c, build, pushed); |
| 3737 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3738 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3739 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3740 | return 1; |
| 3741 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3742 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3743 | for (i = 0; i < n; i++) { |
| 3744 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3745 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3746 | seen_star = 1; |
| 3747 | } |
| 3748 | } |
| 3749 | if (seen_star) { |
| 3750 | seen_star = 0; |
| 3751 | for (i = 0; i < n; i++) { |
| 3752 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3753 | if (elt->kind == Starred_kind) { |
| 3754 | if (seen_star == 0) { |
| 3755 | ADDOP_I(c, build, i+pushed); |
| 3756 | seen_star = 1; |
| 3757 | } |
| 3758 | VISIT(c, expr, elt->v.Starred.value); |
| 3759 | ADDOP_I(c, extend, 1); |
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 | else { |
| 3762 | VISIT(c, expr, elt); |
| 3763 | if (seen_star) { |
| 3764 | ADDOP_I(c, add, 1); |
| 3765 | } |
| 3766 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3767 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3768 | assert(seen_star); |
| 3769 | if (tuple) { |
| 3770 | ADDOP(c, LIST_TO_TUPLE); |
| 3771 | } |
| 3772 | } |
| 3773 | else { |
| 3774 | for (i = 0; i < n; i++) { |
| 3775 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3776 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3777 | } |
| 3778 | if (tuple) { |
| 3779 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3780 | } else { |
| 3781 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3782 | } |
| 3783 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3784 | return 1; |
| 3785 | } |
| 3786 | |
| 3787 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3788 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3789 | { |
| 3790 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3791 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3792 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3793 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3794 | if (elt->kind == Starred_kind && !seen_star) { |
| 3795 | if ((i >= (1 << 8)) || |
| 3796 | (n-i-1 >= (INT_MAX >> 8))) |
| 3797 | return compiler_error(c, |
| 3798 | "too many expressions in " |
| 3799 | "star-unpacking assignment"); |
| 3800 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3801 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3802 | } |
| 3803 | else if (elt->kind == Starred_kind) { |
| 3804 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3805 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | if (!seen_star) { |
| 3809 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3810 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3811 | return 1; |
| 3812 | } |
| 3813 | |
| 3814 | static int |
| 3815 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3816 | { |
| 3817 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3818 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3819 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3820 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3821 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3822 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3823 | return 1; |
| 3824 | } |
| 3825 | |
| 3826 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3827 | compiler_list(struct compiler *c, expr_ty e) |
| 3828 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3829 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3830 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3831 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3832 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3833 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3834 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3835 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3836 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3837 | else |
| 3838 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | static int |
| 3843 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3844 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3845 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3846 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3847 | return assignment_helper(c, elts); |
| 3848 | } |
| 3849 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3850 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3851 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3852 | } |
| 3853 | else |
| 3854 | VISIT_SEQ(c, expr, elts); |
| 3855 | return 1; |
| 3856 | } |
| 3857 | |
| 3858 | static int |
| 3859 | compiler_set(struct compiler *c, expr_ty e) |
| 3860 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3861 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3862 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3866 | 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] | 3867 | { |
| 3868 | Py_ssize_t i; |
| 3869 | for (i = begin; i < end; i++) { |
| 3870 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3871 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3872 | return 0; |
| 3873 | } |
| 3874 | return 1; |
| 3875 | } |
| 3876 | |
| 3877 | static int |
| 3878 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3879 | { |
| 3880 | Py_ssize_t i, n = end - begin; |
| 3881 | PyObject *keys, *key; |
| 3882 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3883 | for (i = begin; i < end; i++) { |
| 3884 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3885 | } |
| 3886 | keys = PyTuple_New(n); |
| 3887 | if (keys == NULL) { |
| 3888 | return 0; |
| 3889 | } |
| 3890 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3891 | 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] | 3892 | Py_INCREF(key); |
| 3893 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3894 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3895 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3896 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3897 | } |
| 3898 | else { |
| 3899 | for (i = begin; i < end; i++) { |
| 3900 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3901 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3902 | } |
| 3903 | ADDOP_I(c, BUILD_MAP, n); |
| 3904 | } |
| 3905 | return 1; |
| 3906 | } |
| 3907 | |
| 3908 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3909 | compiler_dict(struct compiler *c, expr_ty e) |
| 3910 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3911 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3912 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3913 | int is_unpacking = 0; |
| 3914 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3915 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3916 | elements = 0; |
| 3917 | for (i = 0; i < n; i++) { |
| 3918 | 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] | 3919 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3920 | if (elements) { |
| 3921 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3922 | return 0; |
| 3923 | } |
| 3924 | if (have_dict) { |
| 3925 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3926 | } |
| 3927 | have_dict = 1; |
| 3928 | elements = 0; |
| 3929 | } |
| 3930 | if (have_dict == 0) { |
| 3931 | ADDOP_I(c, BUILD_MAP, 0); |
| 3932 | have_dict = 1; |
| 3933 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3934 | 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] | 3935 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3936 | } |
| 3937 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3938 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3939 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3940 | return 0; |
| 3941 | } |
| 3942 | if (have_dict) { |
| 3943 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3944 | } |
| 3945 | have_dict = 1; |
| 3946 | elements = 0; |
| 3947 | } |
| 3948 | else { |
| 3949 | elements++; |
| 3950 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3951 | } |
| 3952 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3953 | if (elements) { |
| 3954 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3955 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3956 | } |
| 3957 | if (have_dict) { |
| 3958 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3959 | } |
| 3960 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3961 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3962 | if (!have_dict) { |
| 3963 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | } |
| 3965 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | static int |
| 3969 | compiler_compare(struct compiler *c, expr_ty e) |
| 3970 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3971 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3972 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3973 | if (!check_compare(c, e)) { |
| 3974 | return 0; |
| 3975 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3977 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3978 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3979 | if (n == 0) { |
| 3980 | 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] | 3981 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3982 | } |
| 3983 | else { |
| 3984 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3985 | if (cleanup == NULL) |
| 3986 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3987 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3988 | VISIT(c, expr, |
| 3989 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3990 | ADDOP(c, DUP_TOP); |
| 3991 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3992 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3993 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3994 | NEXT_BLOCK(c); |
| 3995 | } |
| 3996 | 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] | 3997 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3998 | basicblock *end = compiler_new_block(c); |
| 3999 | if (end == NULL) |
| 4000 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 4001 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4002 | compiler_use_next_block(c, cleanup); |
| 4003 | ADDOP(c, ROT_TWO); |
| 4004 | ADDOP(c, POP_TOP); |
| 4005 | compiler_use_next_block(c, end); |
| 4006 | } |
| 4007 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4010 | static PyTypeObject * |
| 4011 | infer_type(expr_ty e) |
| 4012 | { |
| 4013 | switch (e->kind) { |
| 4014 | case Tuple_kind: |
| 4015 | return &PyTuple_Type; |
| 4016 | case List_kind: |
| 4017 | case ListComp_kind: |
| 4018 | return &PyList_Type; |
| 4019 | case Dict_kind: |
| 4020 | case DictComp_kind: |
| 4021 | return &PyDict_Type; |
| 4022 | case Set_kind: |
| 4023 | case SetComp_kind: |
| 4024 | return &PySet_Type; |
| 4025 | case GeneratorExp_kind: |
| 4026 | return &PyGen_Type; |
| 4027 | case Lambda_kind: |
| 4028 | return &PyFunction_Type; |
| 4029 | case JoinedStr_kind: |
| 4030 | case FormattedValue_kind: |
| 4031 | return &PyUnicode_Type; |
| 4032 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4033 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4034 | default: |
| 4035 | return NULL; |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | static int |
| 4040 | check_caller(struct compiler *c, expr_ty e) |
| 4041 | { |
| 4042 | switch (e->kind) { |
| 4043 | case Constant_kind: |
| 4044 | case Tuple_kind: |
| 4045 | case List_kind: |
| 4046 | case ListComp_kind: |
| 4047 | case Dict_kind: |
| 4048 | case DictComp_kind: |
| 4049 | case Set_kind: |
| 4050 | case SetComp_kind: |
| 4051 | case GeneratorExp_kind: |
| 4052 | case JoinedStr_kind: |
| 4053 | case FormattedValue_kind: |
| 4054 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4055 | "perhaps you missed a comma?", |
| 4056 | infer_type(e)->tp_name); |
| 4057 | default: |
| 4058 | return 1; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | static int |
| 4063 | check_subscripter(struct compiler *c, expr_ty e) |
| 4064 | { |
| 4065 | PyObject *v; |
| 4066 | |
| 4067 | switch (e->kind) { |
| 4068 | case Constant_kind: |
| 4069 | v = e->v.Constant.value; |
| 4070 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4071 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4072 | PyAnySet_Check(v))) |
| 4073 | { |
| 4074 | return 1; |
| 4075 | } |
| 4076 | /* fall through */ |
| 4077 | case Set_kind: |
| 4078 | case SetComp_kind: |
| 4079 | case GeneratorExp_kind: |
| 4080 | case Lambda_kind: |
| 4081 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4082 | "perhaps you missed a comma?", |
| 4083 | infer_type(e)->tp_name); |
| 4084 | default: |
| 4085 | return 1; |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4090 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4091 | { |
| 4092 | PyObject *v; |
| 4093 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4094 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4095 | if (index_type == NULL |
| 4096 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4097 | || index_type == &PySlice_Type) { |
| 4098 | return 1; |
| 4099 | } |
| 4100 | |
| 4101 | switch (e->kind) { |
| 4102 | case Constant_kind: |
| 4103 | v = e->v.Constant.value; |
| 4104 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4105 | return 1; |
| 4106 | } |
| 4107 | /* fall through */ |
| 4108 | case Tuple_kind: |
| 4109 | case List_kind: |
| 4110 | case ListComp_kind: |
| 4111 | case JoinedStr_kind: |
| 4112 | case FormattedValue_kind: |
| 4113 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4114 | "not %.200s; " |
| 4115 | "perhaps you missed a comma?", |
| 4116 | infer_type(e)->tp_name, |
| 4117 | index_type->tp_name); |
| 4118 | default: |
| 4119 | return 1; |
| 4120 | } |
| 4121 | } |
| 4122 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4123 | // 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] | 4124 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4125 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4126 | { |
| 4127 | Py_ssize_t argsl, i; |
| 4128 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4129 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4130 | |
| 4131 | /* Check that the call node is an attribute access, and that |
| 4132 | the call doesn't have keyword parameters. */ |
| 4133 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4134 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4135 | return -1; |
| 4136 | |
| 4137 | /* Check that there are no *varargs types of arguments. */ |
| 4138 | argsl = asdl_seq_LEN(args); |
| 4139 | for (i = 0; i < argsl; i++) { |
| 4140 | expr_ty elt = asdl_seq_GET(args, i); |
| 4141 | if (elt->kind == Starred_kind) { |
| 4142 | return -1; |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | /* Alright, we can optimize the code. */ |
| 4147 | VISIT(c, expr, meth->v.Attribute.value); |
| 4148 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4149 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4150 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4151 | return 1; |
| 4152 | } |
| 4153 | |
| 4154 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4155 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4156 | { |
| 4157 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4158 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4159 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4160 | if (key->arg == NULL) { |
| 4161 | continue; |
| 4162 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4163 | if (forbidden_name(c, key->arg, Store)) { |
| 4164 | return -1; |
| 4165 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4166 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4167 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4168 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4169 | c->u->u_col_offset = other->col_offset; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4170 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4171 | return -1; |
| 4172 | } |
| 4173 | } |
| 4174 | } |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
| 4178 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4179 | compiler_call(struct compiler *c, expr_ty e) |
| 4180 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4181 | int ret = maybe_optimize_method_call(c, e); |
| 4182 | if (ret >= 0) { |
| 4183 | return ret; |
| 4184 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4185 | if (!check_caller(c, e->v.Call.func)) { |
| 4186 | return 0; |
| 4187 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 | VISIT(c, expr, e->v.Call.func); |
| 4189 | return compiler_call_helper(c, 0, |
| 4190 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4191 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4194 | static int |
| 4195 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4196 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4197 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4198 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4199 | 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] | 4200 | return 1; |
| 4201 | } |
| 4202 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4203 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4204 | static int |
| 4205 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4206 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4207 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4208 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4209 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4210 | Convert the conversion char to 3 bits: |
| 4211 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4212 | !s : 001 0x1 FVC_STR |
| 4213 | !r : 010 0x2 FVC_REPR |
| 4214 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4215 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4216 | next bit is whether or not we have a format spec: |
| 4217 | yes : 100 0x4 |
| 4218 | no : 000 0x0 |
| 4219 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4220 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4221 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4222 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4223 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4224 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4225 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4226 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4227 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4228 | case 's': oparg = FVC_STR; break; |
| 4229 | case 'r': oparg = FVC_REPR; break; |
| 4230 | case 'a': oparg = FVC_ASCII; break; |
| 4231 | case -1: oparg = FVC_NONE; break; |
| 4232 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4233 | PyErr_Format(PyExc_SystemError, |
| 4234 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4235 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4236 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4237 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4238 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4239 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4240 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4241 | } |
| 4242 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4243 | /* And push our opcode and oparg */ |
| 4244 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4245 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4246 | return 1; |
| 4247 | } |
| 4248 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4249 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4250 | 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] | 4251 | { |
| 4252 | Py_ssize_t i, n = end - begin; |
| 4253 | keyword_ty kw; |
| 4254 | PyObject *keys, *key; |
| 4255 | assert(n > 0); |
| 4256 | if (n > 1) { |
| 4257 | for (i = begin; i < end; i++) { |
| 4258 | kw = asdl_seq_GET(keywords, i); |
| 4259 | VISIT(c, expr, kw->value); |
| 4260 | } |
| 4261 | keys = PyTuple_New(n); |
| 4262 | if (keys == NULL) { |
| 4263 | return 0; |
| 4264 | } |
| 4265 | for (i = begin; i < end; i++) { |
| 4266 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4267 | Py_INCREF(key); |
| 4268 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4269 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4270 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4271 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4272 | } |
| 4273 | else { |
| 4274 | /* a for loop only executes once */ |
| 4275 | for (i = begin; i < end; i++) { |
| 4276 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4277 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4278 | VISIT(c, expr, kw->value); |
| 4279 | } |
| 4280 | ADDOP_I(c, BUILD_MAP, n); |
| 4281 | } |
| 4282 | return 1; |
| 4283 | } |
| 4284 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4285 | /* shared code between compiler_call and compiler_class */ |
| 4286 | static int |
| 4287 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4288 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4289 | asdl_expr_seq *args, |
| 4290 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4291 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4292 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4293 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4294 | if (validate_keywords(c, keywords) == -1) { |
| 4295 | return 0; |
| 4296 | } |
| 4297 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4298 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4299 | nkwelts = asdl_seq_LEN(keywords); |
| 4300 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4301 | for (i = 0; i < nelts; i++) { |
| 4302 | expr_ty elt = asdl_seq_GET(args, i); |
| 4303 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4304 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4305 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4306 | } |
| 4307 | for (i = 0; i < nkwelts; i++) { |
| 4308 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4309 | if (kw->arg == NULL) { |
| 4310 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4311 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4312 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4313 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4314 | /* No * or ** args, so can use faster calling sequence */ |
| 4315 | for (i = 0; i < nelts; i++) { |
| 4316 | expr_ty elt = asdl_seq_GET(args, i); |
| 4317 | assert(elt->kind != Starred_kind); |
| 4318 | VISIT(c, expr, elt); |
| 4319 | } |
| 4320 | if (nkwelts) { |
| 4321 | PyObject *names; |
| 4322 | VISIT_SEQ(c, keyword, keywords); |
| 4323 | names = PyTuple_New(nkwelts); |
| 4324 | if (names == NULL) { |
| 4325 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4326 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4327 | for (i = 0; i < nkwelts; i++) { |
| 4328 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4329 | Py_INCREF(kw->arg); |
| 4330 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4331 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4332 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4333 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4334 | return 1; |
| 4335 | } |
| 4336 | else { |
| 4337 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4338 | return 1; |
| 4339 | } |
| 4340 | |
| 4341 | ex_call: |
| 4342 | |
| 4343 | /* Do positional arguments. */ |
| 4344 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4345 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4346 | } |
| 4347 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4348 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4349 | return 0; |
| 4350 | } |
| 4351 | /* Then keyword arguments */ |
| 4352 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4353 | /* Has a new dict been pushed */ |
| 4354 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4355 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4356 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4357 | for (i = 0; i < nkwelts; i++) { |
| 4358 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4359 | if (kw->arg == NULL) { |
| 4360 | /* A keyword argument unpacking. */ |
| 4361 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4362 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4363 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4364 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4365 | if (have_dict) { |
| 4366 | ADDOP_I(c, DICT_MERGE, 1); |
| 4367 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4368 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4369 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4370 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4371 | if (!have_dict) { |
| 4372 | ADDOP_I(c, BUILD_MAP, 0); |
| 4373 | have_dict = 1; |
| 4374 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4375 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4376 | ADDOP_I(c, DICT_MERGE, 1); |
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 | else { |
| 4379 | nseen++; |
| 4380 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4381 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4382 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4383 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4384 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4385 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4386 | } |
| 4387 | if (have_dict) { |
| 4388 | ADDOP_I(c, DICT_MERGE, 1); |
| 4389 | } |
| 4390 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4391 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4392 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4394 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4395 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4398 | |
| 4399 | /* List and set comprehensions and generator expressions work by creating a |
| 4400 | nested function to perform the actual iteration. This means that the |
| 4401 | iteration variables don't leak into the current scope. |
| 4402 | The defined function is called immediately following its definition, with the |
| 4403 | result of that call being the result of the expression. |
| 4404 | The LC/SC version returns the populated container, while the GE version is |
| 4405 | flagged in symtable.c as a generator, so it returns the generator object |
| 4406 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4407 | |
| 4408 | Possible cleanups: |
| 4409 | - iterate over the generator sequence instead of using recursion |
| 4410 | */ |
| 4411 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4412 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4413 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4415 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4416 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4417 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4418 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4419 | comprehension_ty gen; |
| 4420 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4421 | if (gen->is_async) { |
| 4422 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4423 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4424 | } else { |
| 4425 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4426 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | static int |
| 4431 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4432 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4433 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4434 | expr_ty elt, expr_ty val, int type) |
| 4435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | /* generate code for the iterator, then each of the ifs, |
| 4437 | and then write to the element */ |
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 | comprehension_ty gen; |
| 4440 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4441 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4443 | start = compiler_new_block(c); |
| 4444 | skip = compiler_new_block(c); |
| 4445 | if_cleanup = compiler_new_block(c); |
| 4446 | anchor = compiler_new_block(c); |
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 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4449 | anchor == NULL) |
| 4450 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | if (gen_index == 0) { |
| 4455 | /* Receive outermost iter as an implicit argument */ |
| 4456 | c->u->u_argcount = 1; |
| 4457 | ADDOP_I(c, LOAD_FAST, 0); |
| 4458 | } |
| 4459 | else { |
| 4460 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4461 | /* Fast path for the temporary variable assignment idiom: |
| 4462 | for y in [f(x)] |
| 4463 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4464 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4465 | switch (gen->iter->kind) { |
| 4466 | case List_kind: |
| 4467 | elts = gen->iter->v.List.elts; |
| 4468 | break; |
| 4469 | case Tuple_kind: |
| 4470 | elts = gen->iter->v.Tuple.elts; |
| 4471 | break; |
| 4472 | default: |
| 4473 | elts = NULL; |
| 4474 | } |
| 4475 | if (asdl_seq_LEN(elts) == 1) { |
| 4476 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4477 | if (elt->kind != Starred_kind) { |
| 4478 | VISIT(c, expr, elt); |
| 4479 | start = NULL; |
| 4480 | } |
| 4481 | } |
| 4482 | if (start) { |
| 4483 | VISIT(c, expr, gen->iter); |
| 4484 | ADDOP(c, GET_ITER); |
| 4485 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4486 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4487 | if (start) { |
| 4488 | depth++; |
| 4489 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4490 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4491 | NEXT_BLOCK(c); |
| 4492 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4495 | /* XXX this needs to be cleaned up...a lot! */ |
| 4496 | n = asdl_seq_LEN(gen->ifs); |
| 4497 | for (i = 0; i < n; i++) { |
| 4498 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4499 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4500 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | NEXT_BLOCK(c); |
| 4502 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4505 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4506 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | elt, val, type)) |
| 4508 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | /* only append after the last for generator */ |
| 4511 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4512 | /* comprehension specific code */ |
| 4513 | switch (type) { |
| 4514 | case COMP_GENEXP: |
| 4515 | VISIT(c, expr, elt); |
| 4516 | ADDOP(c, YIELD_VALUE); |
| 4517 | ADDOP(c, POP_TOP); |
| 4518 | break; |
| 4519 | case COMP_LISTCOMP: |
| 4520 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4521 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | break; |
| 4523 | case COMP_SETCOMP: |
| 4524 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4525 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | break; |
| 4527 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4528 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4529 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4531 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4532 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | break; |
| 4534 | default: |
| 4535 | return 0; |
| 4536 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | compiler_use_next_block(c, skip); |
| 4539 | } |
| 4540 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4541 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4542 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4543 | compiler_use_next_block(c, anchor); |
| 4544 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4545 | |
| 4546 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4550 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4551 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4552 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4553 | expr_ty elt, expr_ty val, int type) |
| 4554 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4555 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4556 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4557 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4558 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4560 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4561 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4562 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4563 | return 0; |
| 4564 | } |
| 4565 | |
| 4566 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4567 | |
| 4568 | if (gen_index == 0) { |
| 4569 | /* Receive outermost iter as an implicit argument */ |
| 4570 | c->u->u_argcount = 1; |
| 4571 | ADDOP_I(c, LOAD_FAST, 0); |
| 4572 | } |
| 4573 | else { |
| 4574 | /* Sub-iter - calculate on the fly */ |
| 4575 | VISIT(c, expr, gen->iter); |
| 4576 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4577 | } |
| 4578 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4579 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4580 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4581 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4582 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4583 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4584 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4585 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4586 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4587 | |
| 4588 | n = asdl_seq_LEN(gen->ifs); |
| 4589 | for (i = 0; i < n; i++) { |
| 4590 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4591 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4592 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4593 | NEXT_BLOCK(c); |
| 4594 | } |
| 4595 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4596 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4597 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4598 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4599 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4600 | elt, val, type)) |
| 4601 | return 0; |
| 4602 | |
| 4603 | /* only append after the last for generator */ |
| 4604 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4605 | /* comprehension specific code */ |
| 4606 | switch (type) { |
| 4607 | case COMP_GENEXP: |
| 4608 | VISIT(c, expr, elt); |
| 4609 | ADDOP(c, YIELD_VALUE); |
| 4610 | ADDOP(c, POP_TOP); |
| 4611 | break; |
| 4612 | case COMP_LISTCOMP: |
| 4613 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4614 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4615 | break; |
| 4616 | case COMP_SETCOMP: |
| 4617 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4618 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4619 | break; |
| 4620 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4621 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4622 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4623 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4624 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4625 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4626 | break; |
| 4627 | default: |
| 4628 | return 0; |
| 4629 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4630 | } |
| 4631 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4632 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4633 | |
| 4634 | compiler_use_next_block(c, except); |
| 4635 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4636 | |
| 4637 | return 1; |
| 4638 | } |
| 4639 | |
| 4640 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4641 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4642 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4643 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4646 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4647 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4648 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4649 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4650 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4651 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4652 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4653 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4654 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4655 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4656 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4658 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4662 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4663 | 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] | 4664 | compiler_error(c, "asynchronous comprehension outside of " |
| 4665 | "an asynchronous function"); |
| 4666 | goto error_in_scope; |
| 4667 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | if (type != COMP_GENEXP) { |
| 4670 | int op; |
| 4671 | switch (type) { |
| 4672 | case COMP_LISTCOMP: |
| 4673 | op = BUILD_LIST; |
| 4674 | break; |
| 4675 | case COMP_SETCOMP: |
| 4676 | op = BUILD_SET; |
| 4677 | break; |
| 4678 | case COMP_DICTCOMP: |
| 4679 | op = BUILD_MAP; |
| 4680 | break; |
| 4681 | default: |
| 4682 | PyErr_Format(PyExc_SystemError, |
| 4683 | "unknown comprehension type %d", type); |
| 4684 | goto error_in_scope; |
| 4685 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4687 | ADDOP_I(c, op, 0); |
| 4688 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4689 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4690 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | val, type)) |
| 4692 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4694 | if (type != COMP_GENEXP) { |
| 4695 | ADDOP(c, RETURN_VALUE); |
| 4696 | } |
| 4697 | |
| 4698 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4699 | qualname = c->u->u_qualname; |
| 4700 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4701 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4702 | if (top_level_await && is_async_generator){ |
| 4703 | c->u->u_ste->ste_coroutine = 1; |
| 4704 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4705 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | goto error; |
| 4707 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4708 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4710 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4711 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | Py_DECREF(co); |
| 4713 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4714 | VISIT(c, expr, outermost->iter); |
| 4715 | |
| 4716 | if (outermost->is_async) { |
| 4717 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4718 | } else { |
| 4719 | ADDOP(c, GET_ITER); |
| 4720 | } |
| 4721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4722 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4723 | |
| 4724 | if (is_async_generator && type != COMP_GENEXP) { |
| 4725 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4726 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4727 | ADDOP(c, YIELD_FROM); |
| 4728 | } |
| 4729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4731 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4732 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4733 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4734 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | Py_XDECREF(co); |
| 4736 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4740 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4742 | static identifier name; |
| 4743 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4744 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | if (!name) |
| 4746 | return 0; |
| 4747 | } |
| 4748 | assert(e->kind == GeneratorExp_kind); |
| 4749 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4750 | e->v.GeneratorExp.generators, |
| 4751 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4755 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | static identifier name; |
| 4758 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4759 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | if (!name) |
| 4761 | return 0; |
| 4762 | } |
| 4763 | assert(e->kind == ListComp_kind); |
| 4764 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4765 | e->v.ListComp.generators, |
| 4766 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | static int |
| 4770 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4772 | static identifier name; |
| 4773 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4774 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | if (!name) |
| 4776 | return 0; |
| 4777 | } |
| 4778 | assert(e->kind == SetComp_kind); |
| 4779 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4780 | e->v.SetComp.generators, |
| 4781 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | |
| 4785 | static int |
| 4786 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4788 | static identifier name; |
| 4789 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4790 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4791 | if (!name) |
| 4792 | return 0; |
| 4793 | } |
| 4794 | assert(e->kind == DictComp_kind); |
| 4795 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4796 | e->v.DictComp.generators, |
| 4797 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | |
| 4801 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4802 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | VISIT(c, expr, k->value); |
| 4805 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4808 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4809 | whether they are true or false. |
| 4810 | |
| 4811 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4812 | */ |
| 4813 | |
| 4814 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4815 | compiler_with_except_finish(struct compiler *c) { |
| 4816 | basicblock *exit; |
| 4817 | exit = compiler_new_block(c); |
| 4818 | if (exit == NULL) |
| 4819 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4820 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4821 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4822 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4823 | compiler_use_next_block(c, exit); |
| 4824 | ADDOP(c, POP_TOP); |
| 4825 | ADDOP(c, POP_TOP); |
| 4826 | ADDOP(c, POP_TOP); |
| 4827 | ADDOP(c, POP_EXCEPT); |
| 4828 | ADDOP(c, POP_TOP); |
| 4829 | return 1; |
| 4830 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4831 | |
| 4832 | /* |
| 4833 | Implements the async with statement. |
| 4834 | |
| 4835 | The semantics outlined in that PEP are as follows: |
| 4836 | |
| 4837 | async with EXPR as VAR: |
| 4838 | BLOCK |
| 4839 | |
| 4840 | It is implemented roughly as: |
| 4841 | |
| 4842 | context = EXPR |
| 4843 | exit = context.__aexit__ # not calling it |
| 4844 | value = await context.__aenter__() |
| 4845 | try: |
| 4846 | VAR = value # if VAR present in the syntax |
| 4847 | BLOCK |
| 4848 | finally: |
| 4849 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4850 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4851 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4852 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4853 | if not (await exit(*exc)): |
| 4854 | raise |
| 4855 | */ |
| 4856 | static int |
| 4857 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4858 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4859 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4860 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4861 | |
| 4862 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4863 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4864 | c->u->u_ste->ste_coroutine = 1; |
| 4865 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4866 | return compiler_error(c, "'async with' outside async function"); |
| 4867 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4868 | |
| 4869 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4870 | final = compiler_new_block(c); |
| 4871 | exit = compiler_new_block(c); |
| 4872 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4873 | return 0; |
| 4874 | |
| 4875 | /* Evaluate EXPR */ |
| 4876 | VISIT(c, expr, item->context_expr); |
| 4877 | |
| 4878 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4879 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4880 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4881 | ADDOP(c, YIELD_FROM); |
| 4882 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4883 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4884 | |
| 4885 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4886 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4887 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | if (item->optional_vars) { |
| 4892 | VISIT(c, expr, item->optional_vars); |
| 4893 | } |
| 4894 | else { |
| 4895 | /* Discard result from context.__aenter__() */ |
| 4896 | ADDOP(c, POP_TOP); |
| 4897 | } |
| 4898 | |
| 4899 | pos++; |
| 4900 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4901 | /* BLOCK code */ |
| 4902 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4903 | else if (!compiler_async_with(c, s, pos)) |
| 4904 | return 0; |
| 4905 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4906 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4907 | ADDOP(c, POP_BLOCK); |
| 4908 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4909 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4910 | /* For successful outcome: |
| 4911 | * call __exit__(None, None, None) |
| 4912 | */ |
| 4913 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4914 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4915 | ADDOP(c, GET_AWAITABLE); |
| 4916 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4917 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4918 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4919 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4920 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4921 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4922 | |
| 4923 | /* For exceptional outcome: */ |
| 4924 | compiler_use_next_block(c, final); |
| 4925 | |
| 4926 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4927 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4928 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4929 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4930 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4931 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4932 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4933 | return 1; |
| 4934 | } |
| 4935 | |
| 4936 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4937 | /* |
| 4938 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4939 | with EXPR as VAR: |
| 4940 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4941 | is implemented as: |
| 4942 | <code for EXPR> |
| 4943 | SETUP_WITH E |
| 4944 | <code to store to VAR> or POP_TOP |
| 4945 | <code for BLOCK> |
| 4946 | LOAD_CONST (None, None, None) |
| 4947 | CALL_FUNCTION_EX 0 |
| 4948 | JUMP_FORWARD EXIT |
| 4949 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4950 | POP_JUMP_IF_TRUE T: |
| 4951 | RERAISE |
| 4952 | T: POP_TOP * 3 (remove exception from stack) |
| 4953 | POP_EXCEPT |
| 4954 | POP_TOP |
| 4955 | EXIT: |
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 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4958 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4959 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4960 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4961 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4962 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4963 | |
| 4964 | assert(s->kind == With_kind); |
| 4965 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4966 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4967 | final = compiler_new_block(c); |
| 4968 | exit = compiler_new_block(c); |
| 4969 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4970 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4971 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4972 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4973 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4974 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4975 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4976 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4977 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4978 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4979 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4980 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4983 | if (item->optional_vars) { |
| 4984 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4985 | } |
| 4986 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4987 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4988 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4991 | pos++; |
| 4992 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4993 | /* BLOCK code */ |
| 4994 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4995 | else if (!compiler_with(c, s, pos)) |
| 4996 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4997 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 4998 | |
| 4999 | /* Mark all following code as artificial */ |
| 5000 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5001 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5002 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5003 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5004 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5005 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5006 | /* For successful outcome: |
| 5007 | * call __exit__(None, None, None) |
| 5008 | */ |
| 5009 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5010 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5011 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5012 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5013 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5014 | /* For exceptional outcome: */ |
| 5015 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5016 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5017 | ADDOP(c, WITH_EXCEPT_START); |
| 5018 | compiler_with_except_finish(c); |
| 5019 | |
| 5020 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5021 | return 1; |
| 5022 | } |
| 5023 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5024 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5025 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5027 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5028 | case NamedExpr_kind: |
| 5029 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5030 | ADDOP(c, DUP_TOP); |
| 5031 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5032 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5033 | case BoolOp_kind: |
| 5034 | return compiler_boolop(c, e); |
| 5035 | case BinOp_kind: |
| 5036 | VISIT(c, expr, e->v.BinOp.left); |
| 5037 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5038 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | break; |
| 5040 | case UnaryOp_kind: |
| 5041 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5042 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5043 | break; |
| 5044 | case Lambda_kind: |
| 5045 | return compiler_lambda(c, e); |
| 5046 | case IfExp_kind: |
| 5047 | return compiler_ifexp(c, e); |
| 5048 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5049 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5051 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5052 | case GeneratorExp_kind: |
| 5053 | return compiler_genexp(c, e); |
| 5054 | case ListComp_kind: |
| 5055 | return compiler_listcomp(c, e); |
| 5056 | case SetComp_kind: |
| 5057 | return compiler_setcomp(c, e); |
| 5058 | case DictComp_kind: |
| 5059 | return compiler_dictcomp(c, e); |
| 5060 | case Yield_kind: |
| 5061 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5062 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5063 | if (e->v.Yield.value) { |
| 5064 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | } |
| 5066 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5067 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5069 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5071 | case YieldFrom_kind: |
| 5072 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5073 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5074 | |
| 5075 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5076 | return compiler_error(c, "'yield from' inside async function"); |
| 5077 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5078 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5079 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5080 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5081 | ADDOP(c, YIELD_FROM); |
| 5082 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5083 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5084 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5085 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5086 | return compiler_error(c, "'await' outside function"); |
| 5087 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5088 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5089 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5090 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5091 | return compiler_error(c, "'await' outside async function"); |
| 5092 | } |
| 5093 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5094 | |
| 5095 | VISIT(c, expr, e->v.Await.value); |
| 5096 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5097 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5098 | ADDOP(c, YIELD_FROM); |
| 5099 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5100 | case Compare_kind: |
| 5101 | return compiler_compare(c, e); |
| 5102 | case Call_kind: |
| 5103 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5104 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5105 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5106 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5107 | case JoinedStr_kind: |
| 5108 | return compiler_joined_str(c, e); |
| 5109 | case FormattedValue_kind: |
| 5110 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5111 | /* The following exprs can be assignment targets. */ |
| 5112 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5113 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5114 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5115 | case Load: |
| 5116 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5117 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5118 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5119 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5120 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5122 | break; |
| 5123 | case Del: |
| 5124 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5125 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | } |
| 5127 | break; |
| 5128 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5129 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5130 | case Starred_kind: |
| 5131 | switch (e->v.Starred.ctx) { |
| 5132 | case Store: |
| 5133 | /* In all legitimate cases, the Starred node was already replaced |
| 5134 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5135 | return compiler_error(c, |
| 5136 | "starred assignment target must be in a list or tuple"); |
| 5137 | default: |
| 5138 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5139 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5140 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5141 | break; |
| 5142 | case Slice_kind: |
| 5143 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5144 | case Name_kind: |
| 5145 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5146 | /* child nodes of List and Tuple will have expr_context set */ |
| 5147 | case List_kind: |
| 5148 | return compiler_list(c, e); |
| 5149 | case Tuple_kind: |
| 5150 | return compiler_tuple(c, e); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5151 | case MatchAs_kind: |
| 5152 | case MatchOr_kind: |
| 5153 | // Can only occur in patterns, which are handled elsewhere. |
| 5154 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5155 | } |
| 5156 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5160 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5161 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5162 | int old_lineno = c->u->u_lineno; |
| 5163 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5164 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5165 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5166 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5167 | c->u->u_col_offset = old_col_offset; |
| 5168 | return res; |
| 5169 | } |
| 5170 | |
| 5171 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5172 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5174 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5175 | expr_ty e = s->v.AugAssign.target; |
| 5176 | |
| 5177 | int old_lineno = c->u->u_lineno; |
| 5178 | int old_col_offset = c->u->u_col_offset; |
| 5179 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5181 | switch (e->kind) { |
| 5182 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5183 | VISIT(c, expr, e->v.Attribute.value); |
| 5184 | ADDOP(c, DUP_TOP); |
| 5185 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5186 | break; |
| 5187 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5188 | VISIT(c, expr, e->v.Subscript.value); |
| 5189 | VISIT(c, expr, e->v.Subscript.slice); |
| 5190 | ADDOP(c, DUP_TOP_TWO); |
| 5191 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5192 | break; |
| 5193 | case Name_kind: |
| 5194 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5195 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5196 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5197 | default: |
| 5198 | PyErr_Format(PyExc_SystemError, |
| 5199 | "invalid node type (%d) for augmented assignment", |
| 5200 | e->kind); |
| 5201 | return 0; |
| 5202 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5203 | |
| 5204 | c->u->u_lineno = old_lineno; |
| 5205 | c->u->u_col_offset = old_col_offset; |
| 5206 | |
| 5207 | VISIT(c, expr, s->v.AugAssign.value); |
| 5208 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5209 | |
| 5210 | SET_LOC(c, e); |
| 5211 | |
| 5212 | switch (e->kind) { |
| 5213 | case Attribute_kind: |
| 5214 | ADDOP(c, ROT_TWO); |
| 5215 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5216 | break; |
| 5217 | case Subscript_kind: |
| 5218 | ADDOP(c, ROT_THREE); |
| 5219 | ADDOP(c, STORE_SUBSCR); |
| 5220 | break; |
| 5221 | case Name_kind: |
| 5222 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5223 | default: |
| 5224 | Py_UNREACHABLE(); |
| 5225 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5226 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5230 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5231 | { |
| 5232 | VISIT(c, expr, e); |
| 5233 | ADDOP(c, POP_TOP); |
| 5234 | return 1; |
| 5235 | } |
| 5236 | |
| 5237 | static int |
| 5238 | check_annotation(struct compiler *c, stmt_ty s) |
| 5239 | { |
| 5240 | /* Annotations are only evaluated in a module or class. */ |
| 5241 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5242 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5243 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5244 | } |
| 5245 | return 1; |
| 5246 | } |
| 5247 | |
| 5248 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5249 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5250 | { |
| 5251 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5252 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5253 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5254 | 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] | 5255 | return 0; |
| 5256 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5257 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5258 | return 0; |
| 5259 | } |
| 5260 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5261 | return 0; |
| 5262 | } |
| 5263 | return 1; |
| 5264 | case Tuple_kind: { |
| 5265 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5266 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5267 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5268 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5269 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5270 | return 0; |
| 5271 | } |
| 5272 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5273 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5274 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5275 | default: |
| 5276 | return check_ann_expr(c, e); |
| 5277 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | static int |
| 5281 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5282 | { |
| 5283 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5284 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5285 | |
| 5286 | assert(s->kind == AnnAssign_kind); |
| 5287 | |
| 5288 | /* We perform the actual assignment first. */ |
| 5289 | if (s->v.AnnAssign.value) { |
| 5290 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5291 | VISIT(c, expr, targ); |
| 5292 | } |
| 5293 | switch (targ->kind) { |
| 5294 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5295 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5296 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5297 | /* If we have a simple name in a module or class, store annotation. */ |
| 5298 | if (s->v.AnnAssign.simple && |
| 5299 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5300 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5301 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5302 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5303 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5304 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5305 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5306 | } |
| 5307 | break; |
| 5308 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5309 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5310 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5311 | if (!s->v.AnnAssign.value && |
| 5312 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5313 | return 0; |
| 5314 | } |
| 5315 | break; |
| 5316 | case Subscript_kind: |
| 5317 | if (!s->v.AnnAssign.value && |
| 5318 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5319 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5320 | return 0; |
| 5321 | } |
| 5322 | break; |
| 5323 | default: |
| 5324 | PyErr_Format(PyExc_SystemError, |
| 5325 | "invalid node type (%d) for annotated assignment", |
| 5326 | targ->kind); |
| 5327 | return 0; |
| 5328 | } |
| 5329 | /* Annotation is evaluated last. */ |
| 5330 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5331 | return 0; |
| 5332 | } |
| 5333 | return 1; |
| 5334 | } |
| 5335 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5336 | /* Raises a SyntaxError and returns 0. |
| 5337 | If something goes wrong, a different exception may be raised. |
| 5338 | */ |
| 5339 | |
| 5340 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5341 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5342 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5343 | va_list vargs; |
| 5344 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5345 | va_start(vargs, format); |
| 5346 | #else |
| 5347 | va_start(vargs); |
| 5348 | #endif |
| 5349 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5350 | va_end(vargs); |
| 5351 | if (msg == NULL) { |
| 5352 | return 0; |
| 5353 | } |
| 5354 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5355 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5356 | Py_INCREF(Py_None); |
| 5357 | loc = Py_None; |
| 5358 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5359 | PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename, |
| 5360 | c->u->u_lineno, c->u->u_col_offset + 1, loc); |
| 5361 | Py_DECREF(msg); |
| 5362 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5364 | } |
| 5365 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5366 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5367 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5368 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5369 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5372 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5373 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5374 | and returns 0. |
| 5375 | */ |
| 5376 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5377 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5378 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5379 | va_list vargs; |
| 5380 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5381 | va_start(vargs, format); |
| 5382 | #else |
| 5383 | va_start(vargs); |
| 5384 | #endif |
| 5385 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5386 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5387 | if (msg == NULL) { |
| 5388 | return 0; |
| 5389 | } |
| 5390 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5391 | c->u->u_lineno, NULL, NULL) < 0) |
| 5392 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5393 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5394 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5395 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5396 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5397 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5398 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5399 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5400 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5401 | return 0; |
| 5402 | } |
| 5403 | Py_DECREF(msg); |
| 5404 | return 1; |
| 5405 | } |
| 5406 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5407 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5408 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5409 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5410 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5412 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5413 | if (ctx == Load) { |
| 5414 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5415 | return 0; |
| 5416 | } |
| 5417 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5418 | return 0; |
| 5419 | } |
| 5420 | } |
| 5421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5423 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5424 | case Store: op = STORE_SUBSCR; break; |
| 5425 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5426 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5427 | assert(op); |
| 5428 | VISIT(c, expr, e->v.Subscript.value); |
| 5429 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | ADDOP(c, op); |
| 5431 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5435 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5437 | int n = 2; |
| 5438 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5441 | if (s->v.Slice.lower) { |
| 5442 | VISIT(c, expr, s->v.Slice.lower); |
| 5443 | } |
| 5444 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5445 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5446 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5448 | if (s->v.Slice.upper) { |
| 5449 | VISIT(c, expr, s->v.Slice.upper); |
| 5450 | } |
| 5451 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5452 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | if (s->v.Slice.step) { |
| 5456 | n++; |
| 5457 | VISIT(c, expr, s->v.Slice.step); |
| 5458 | } |
| 5459 | ADDOP_I(c, BUILD_SLICE, n); |
| 5460 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5463 | |
| 5464 | // PEP 634: Structural Pattern Matching |
| 5465 | |
| 5466 | // To keep things simple, all compiler_pattern_* routines follow the convention |
| 5467 | // of replacing TOS (the subject for the given pattern) with either True (match) |
| 5468 | // or False (no match). We do this even for irrefutable patterns; the idea is |
| 5469 | // that it's much easier to smooth out any redundant pushing, popping, and |
| 5470 | // jumping in the peephole optimizer than to detect or predict it here. |
| 5471 | |
| 5472 | |
| 5473 | #define WILDCARD_CHECK(N) \ |
| 5474 | ((N)->kind == Name_kind && \ |
| 5475 | _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_")) |
| 5476 | |
| 5477 | |
| 5478 | static int |
| 5479 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5480 | { |
| 5481 | assert(!_PyUnicode_EqualToASCIIString(n, "_")); |
| 5482 | // Can't assign to the same name twice: |
| 5483 | if (pc->stores == NULL) { |
| 5484 | RETURN_IF_FALSE(pc->stores = PySet_New(NULL)); |
| 5485 | } |
| 5486 | else { |
| 5487 | int duplicate = PySet_Contains(pc->stores, n); |
| 5488 | if (duplicate < 0) { |
| 5489 | return 0; |
| 5490 | } |
| 5491 | if (duplicate) { |
| 5492 | const char *e = "multiple assignments to name %R in pattern"; |
| 5493 | return compiler_error(c, e, n); |
| 5494 | } |
| 5495 | } |
| 5496 | RETURN_IF_FALSE(!PySet_Add(pc->stores, n)); |
| 5497 | RETURN_IF_FALSE(compiler_nameop(c, n, Store)); |
| 5498 | return 1; |
| 5499 | } |
| 5500 | |
| 5501 | |
| 5502 | static int |
| 5503 | pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values, |
| 5504 | Py_ssize_t star, pattern_context *pc) |
| 5505 | { |
| 5506 | RETURN_IF_FALSE(unpack_helper(c, values)); |
| 5507 | // We've now got a bunch of new subjects on the stack. If any of them fail |
| 5508 | // to match, we need to pop everything else off, then finally push False. |
| 5509 | // fails is an array of blocks that correspond to the necessary amount of |
| 5510 | // popping for each element: |
| 5511 | basicblock **fails; |
| 5512 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5513 | fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size); |
| 5514 | if (fails == NULL) { |
| 5515 | PyErr_NoMemory(); |
| 5516 | return 0; |
| 5517 | } |
| 5518 | // NOTE: Can't use our nice returning macros anymore: they'll leak memory! |
| 5519 | // goto error on error. |
| 5520 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5521 | fails[i] = compiler_new_block(c); |
| 5522 | if (fails[i] == NULL) { |
| 5523 | goto error; |
| 5524 | } |
| 5525 | } |
| 5526 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5527 | expr_ty value = asdl_seq_GET(values, i); |
| 5528 | if (i == star) { |
| 5529 | assert(value->kind == Starred_kind); |
| 5530 | value = value->v.Starred.value; |
| 5531 | } |
| 5532 | if (!compiler_pattern_subpattern(c, value, pc) || |
| 5533 | !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) || |
| 5534 | compiler_next_block(c) == NULL) |
| 5535 | { |
| 5536 | goto error; |
| 5537 | } |
| 5538 | } |
| 5539 | // Success! |
| 5540 | basicblock *end = compiler_new_block(c); |
| 5541 | if (end == NULL || |
| 5542 | !compiler_addop_load_const(c, Py_True) || |
| 5543 | !compiler_addop_j(c, JUMP_FORWARD, end)) |
| 5544 | { |
| 5545 | goto error; |
| 5546 | } |
| 5547 | // This is where we handle failed sub-patterns. For a sequence pattern like |
| 5548 | // [a, b, c, d], this will look like: |
| 5549 | // fails[0]: POP_TOP |
| 5550 | // fails[1]: POP_TOP |
| 5551 | // fails[2]: POP_TOP |
| 5552 | // fails[3]: LOAD_CONST False |
| 5553 | for (Py_ssize_t i = 0; i < size - 1; i++) { |
| 5554 | compiler_use_next_block(c, fails[i]); |
| 5555 | if (!compiler_addop(c, POP_TOP)) { |
| 5556 | goto error; |
| 5557 | } |
| 5558 | } |
| 5559 | compiler_use_next_block(c, fails[size - 1]); |
| 5560 | if (!compiler_addop_load_const(c, Py_False)) { |
| 5561 | goto error; |
| 5562 | } |
| 5563 | compiler_use_next_block(c, end); |
| 5564 | PyObject_Free(fails); |
| 5565 | return 1; |
| 5566 | error: |
| 5567 | PyObject_Free(fails); |
| 5568 | return 0; |
| 5569 | } |
| 5570 | |
| 5571 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5572 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5573 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5574 | static int |
| 5575 | pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values, |
| 5576 | Py_ssize_t star, pattern_context *pc) |
| 5577 | { |
| 5578 | basicblock *end, *fail_pop_1; |
| 5579 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5580 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5581 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5582 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5583 | expr_ty value = asdl_seq_GET(values, i); |
| 5584 | if (WILDCARD_CHECK(value)) { |
| 5585 | continue; |
| 5586 | } |
| 5587 | if (i == star) { |
| 5588 | assert(value->kind == Starred_kind); |
| 5589 | assert(WILDCARD_CHECK(value->v.Starred.value)); |
| 5590 | continue; |
| 5591 | } |
| 5592 | ADDOP(c, DUP_TOP); |
| 5593 | if (i < star) { |
| 5594 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5595 | } |
| 5596 | else { |
| 5597 | // The subject may not support negative indexing! Compute a |
| 5598 | // nonnegative index: |
| 5599 | ADDOP(c, GET_LEN); |
| 5600 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5601 | ADDOP(c, BINARY_SUBTRACT); |
| 5602 | } |
| 5603 | ADDOP(c, BINARY_SUBSCR); |
| 5604 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5605 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5606 | NEXT_BLOCK(c); |
| 5607 | } |
| 5608 | ADDOP(c, POP_TOP); |
| 5609 | ADDOP_LOAD_CONST(c, Py_True); |
| 5610 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5611 | compiler_use_next_block(c, fail_pop_1); |
| 5612 | ADDOP(c, POP_TOP); |
| 5613 | ADDOP_LOAD_CONST(c, Py_False); |
| 5614 | compiler_use_next_block(c, end); |
| 5615 | return 1; |
| 5616 | } |
| 5617 | |
| 5618 | |
| 5619 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5620 | static int |
| 5621 | compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5622 | { |
| 5623 | int allow_irrefutable = pc->allow_irrefutable; |
| 5624 | pc->allow_irrefutable = 1; |
| 5625 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5626 | pc->allow_irrefutable = allow_irrefutable; |
| 5627 | return 1; |
| 5628 | } |
| 5629 | |
| 5630 | |
| 5631 | static int |
| 5632 | compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5633 | { |
| 5634 | assert(p->kind == MatchAs_kind); |
| 5635 | basicblock *end, *fail_pop_1; |
| 5636 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5637 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5638 | // Need to make a copy for (possibly) storing later: |
| 5639 | ADDOP(c, DUP_TOP); |
| 5640 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
| 5641 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5642 | NEXT_BLOCK(c); |
| 5643 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5644 | ADDOP_LOAD_CONST(c, Py_True); |
| 5645 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5646 | compiler_use_next_block(c, fail_pop_1); |
| 5647 | // Need to pop that unused copy from before: |
| 5648 | ADDOP(c, POP_TOP); |
| 5649 | ADDOP_LOAD_CONST(c, Py_False); |
| 5650 | compiler_use_next_block(c, end); |
| 5651 | return 1; |
| 5652 | } |
| 5653 | |
| 5654 | |
| 5655 | static int |
| 5656 | compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5657 | { |
| 5658 | assert(p->kind == Name_kind); |
| 5659 | assert(p->v.Name.ctx == Store); |
| 5660 | assert(!WILDCARD_CHECK(p)); |
| 5661 | if (!pc->allow_irrefutable) { |
| 5662 | // Whoops, can't have a name capture here! |
| 5663 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5664 | return compiler_error(c, e, p->v.Name.id); |
| 5665 | } |
| 5666 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc)); |
| 5667 | ADDOP_LOAD_CONST(c, Py_True); |
| 5668 | return 1; |
| 5669 | } |
| 5670 | |
| 5671 | |
| 5672 | static int |
| 5673 | compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5674 | { |
| 5675 | asdl_expr_seq *args = p->v.Call.args; |
| 5676 | asdl_keyword_seq *kwargs = p->v.Call.keywords; |
| 5677 | Py_ssize_t nargs = asdl_seq_LEN(args); |
| 5678 | Py_ssize_t nkwargs = asdl_seq_LEN(kwargs); |
| 5679 | if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) { |
| 5680 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5681 | return compiler_error(c, e, p->v.Call.func); |
| 5682 | } |
| 5683 | RETURN_IF_FALSE(!validate_keywords(c, kwargs)); |
| 5684 | basicblock *end, *fail_pop_1; |
| 5685 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5686 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5687 | VISIT(c, expr, p->v.Call.func); |
| 5688 | PyObject *kwnames; |
| 5689 | RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs)); |
| 5690 | Py_ssize_t i; |
| 5691 | for (i = 0; i < nkwargs; i++) { |
| 5692 | PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg; |
| 5693 | Py_INCREF(name); |
| 5694 | PyTuple_SET_ITEM(kwnames, i, name); |
| 5695 | } |
| 5696 | ADDOP_LOAD_CONST_NEW(c, kwnames); |
| 5697 | ADDOP_I(c, MATCH_CLASS, nargs); |
| 5698 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5699 | NEXT_BLOCK(c); |
| 5700 | // TOS is now a tuple of (nargs + nkwargs) attributes. |
| 5701 | for (i = 0; i < nargs + nkwargs; i++) { |
| 5702 | expr_ty arg; |
| 5703 | if (i < nargs) { |
| 5704 | // Positional: |
| 5705 | arg = asdl_seq_GET(args, i); |
| 5706 | } |
| 5707 | else { |
| 5708 | // Keyword: |
| 5709 | arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value; |
| 5710 | } |
| 5711 | if (WILDCARD_CHECK(arg)) { |
| 5712 | continue; |
| 5713 | } |
| 5714 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5715 | ADDOP(c, DUP_TOP); |
| 5716 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5717 | ADDOP(c, BINARY_SUBSCR); |
| 5718 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc)); |
| 5719 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5720 | NEXT_BLOCK(c); |
| 5721 | } |
| 5722 | // Success! Pop the tuple of attributes: |
| 5723 | ADDOP(c, POP_TOP); |
| 5724 | ADDOP_LOAD_CONST(c, Py_True); |
| 5725 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5726 | compiler_use_next_block(c, fail_pop_1); |
| 5727 | ADDOP(c, POP_TOP); |
| 5728 | ADDOP_LOAD_CONST(c, Py_False); |
| 5729 | compiler_use_next_block(c, end); |
| 5730 | return 1; |
| 5731 | } |
| 5732 | |
| 5733 | |
| 5734 | static int |
| 5735 | compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5736 | { |
| 5737 | assert(p->kind == Constant_kind); |
| 5738 | PyObject *v = p->v.Constant.value; |
| 5739 | ADDOP_LOAD_CONST(c, v); |
| 5740 | // Literal True, False, and None are compared by identity. All others use |
| 5741 | // equality: |
| 5742 | ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq); |
| 5743 | return 1; |
| 5744 | } |
| 5745 | |
| 5746 | |
| 5747 | static int |
| 5748 | compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5749 | { |
| 5750 | basicblock *end, *fail_pop_1, *fail_pop_3; |
| 5751 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5752 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5753 | RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c)); |
| 5754 | asdl_expr_seq *keys = p->v.Dict.keys; |
| 5755 | asdl_expr_seq *values = p->v.Dict.values; |
| 5756 | Py_ssize_t size = asdl_seq_LEN(values); |
Ikko Ashimine | 57827f8 | 2021-03-10 19:39:51 +0900 | [diff] [blame] | 5757 | // 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] | 5758 | int star = size ? !asdl_seq_GET(keys, size - 1) : 0; |
| 5759 | ADDOP(c, MATCH_MAPPING); |
| 5760 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5761 | NEXT_BLOCK(c); |
| 5762 | if (!size) { |
| 5763 | // If the pattern is just "{}", we're done! |
| 5764 | ADDOP(c, POP_TOP); |
| 5765 | ADDOP_LOAD_CONST(c, Py_True); |
| 5766 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5767 | compiler_use_next_block(c, fail_pop_1); |
| 5768 | ADDOP(c, POP_TOP); |
| 5769 | ADDOP_LOAD_CONST(c, Py_False); |
| 5770 | compiler_use_next_block(c, end); |
| 5771 | return 1; |
| 5772 | } |
| 5773 | if (size - star) { |
| 5774 | // If the pattern has any keys in it, perform a length check: |
| 5775 | ADDOP(c, GET_LEN); |
| 5776 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star)); |
| 5777 | ADDOP_COMPARE(c, GtE); |
| 5778 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5779 | NEXT_BLOCK(c); |
| 5780 | } |
| 5781 | if (INT_MAX < size - star - 1) { |
| 5782 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 5783 | } |
| 5784 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 5785 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
| 5786 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5787 | expr_ty key = asdl_seq_GET(keys, i); |
| 5788 | if (key == NULL) { |
| 5789 | const char *e = "can't use starred name here " |
| 5790 | "(consider moving to end)"; |
| 5791 | return compiler_error(c, e); |
| 5792 | } |
| 5793 | VISIT(c, expr, key); |
| 5794 | } |
| 5795 | ADDOP_I(c, BUILD_TUPLE, size - star); |
| 5796 | ADDOP(c, MATCH_KEYS); |
| 5797 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5798 | NEXT_BLOCK(c); |
| 5799 | // So far so good. There's now a tuple of values on the stack to match |
| 5800 | // sub-patterns against: |
| 5801 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5802 | expr_ty value = asdl_seq_GET(values, i); |
| 5803 | if (WILDCARD_CHECK(value)) { |
| 5804 | continue; |
| 5805 | } |
| 5806 | ADDOP(c, DUP_TOP); |
| 5807 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5808 | ADDOP(c, BINARY_SUBSCR); |
| 5809 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5810 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5811 | NEXT_BLOCK(c); |
| 5812 | } |
| 5813 | // If we get this far, it's a match! We're done with that tuple of values. |
| 5814 | ADDOP(c, POP_TOP); |
| 5815 | if (star) { |
| 5816 | // If we had a starred name, bind a dict of remaining items to it: |
| 5817 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
| 5818 | PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id; |
| 5819 | RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc)); |
| 5820 | } |
| 5821 | else { |
| 5822 | // Otherwise, we don't care about this tuple of keys anymore: |
| 5823 | ADDOP(c, POP_TOP); |
| 5824 | } |
| 5825 | // Pop the subject: |
| 5826 | ADDOP(c, POP_TOP); |
| 5827 | ADDOP_LOAD_CONST(c, Py_True); |
| 5828 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5829 | // The top two items are a tuple of values or None, followed by a tuple of |
| 5830 | // keys. Pop them both: |
| 5831 | compiler_use_next_block(c, fail_pop_3); |
| 5832 | ADDOP(c, POP_TOP); |
| 5833 | ADDOP(c, POP_TOP); |
| 5834 | compiler_use_next_block(c, fail_pop_1); |
| 5835 | // Pop the subject: |
| 5836 | ADDOP(c, POP_TOP); |
| 5837 | ADDOP_LOAD_CONST(c, Py_False); |
| 5838 | compiler_use_next_block(c, end); |
| 5839 | return 1; |
| 5840 | } |
| 5841 | |
| 5842 | |
| 5843 | static int |
| 5844 | compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5845 | { |
| 5846 | assert(p->kind == MatchOr_kind); |
| 5847 | // control is the set of names bound by the first alternative. If all of the |
| 5848 | // others bind the same names (they should), then this becomes pc->stores. |
| 5849 | PyObject *control = NULL; |
| 5850 | basicblock *end, *pass_pop_1; |
| 5851 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5852 | RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c)); |
| 5853 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 5854 | assert(size > 1); |
| 5855 | // We're going to be messing with pc. Keep the original info handy: |
| 5856 | PyObject *stores_init = pc->stores; |
| 5857 | int allow_irrefutable = pc->allow_irrefutable; |
| 5858 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5859 | // NOTE: Can't use our nice returning macros in here: they'll leak sets! |
| 5860 | expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
| 5861 | pc->stores = PySet_New(stores_init); |
| 5862 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 5863 | int is_last = i == size - 1; |
| 5864 | pc->allow_irrefutable = allow_irrefutable && is_last; |
| 5865 | SET_LOC(c, alt); |
| 5866 | if (pc->stores == NULL || |
| 5867 | // Only copy the subject if we're *not* on the last alternative: |
| 5868 | (!is_last && !compiler_addop(c, DUP_TOP)) || |
| 5869 | !compiler_pattern(c, alt, pc) || |
| 5870 | // Only jump if we're *not* on the last alternative: |
| 5871 | (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) || |
| 5872 | !compiler_next_block(c)) |
| 5873 | { |
| 5874 | goto fail; |
| 5875 | } |
| 5876 | if (!i) { |
| 5877 | // If this is the first alternative, save its stores as a "control" |
| 5878 | // for the others (they can't bind a different set of names): |
| 5879 | control = pc->stores; |
| 5880 | continue; |
| 5881 | } |
| 5882 | if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) { |
| 5883 | // Otherwise, check to see if we differ from the control set: |
| 5884 | PyObject *diff = PyNumber_InPlaceXor(pc->stores, control); |
| 5885 | if (diff == NULL) { |
| 5886 | goto fail; |
| 5887 | } |
| 5888 | if (PySet_GET_SIZE(diff)) { |
| 5889 | // The names differ! Raise. |
| 5890 | Py_DECREF(diff); |
| 5891 | compiler_error(c, "alternative patterns bind different names"); |
| 5892 | goto fail; |
| 5893 | } |
| 5894 | Py_DECREF(diff); |
| 5895 | } |
| 5896 | Py_DECREF(pc->stores); |
| 5897 | } |
| 5898 | Py_XDECREF(stores_init); |
| 5899 | // Update pc->stores and restore pc->allow_irrefutable: |
| 5900 | pc->stores = control; |
| 5901 | pc->allow_irrefutable = allow_irrefutable; |
| 5902 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5903 | compiler_use_next_block(c, pass_pop_1); |
| 5904 | ADDOP(c, POP_TOP); |
| 5905 | ADDOP_LOAD_CONST(c, Py_True); |
| 5906 | compiler_use_next_block(c, end); |
| 5907 | return 1; |
| 5908 | fail: |
| 5909 | Py_XDECREF(stores_init); |
| 5910 | Py_XDECREF(control); |
| 5911 | return 0; |
| 5912 | } |
| 5913 | |
| 5914 | |
| 5915 | static int |
| 5916 | compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5917 | { |
| 5918 | assert(p->kind == List_kind || p->kind == Tuple_kind); |
| 5919 | asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts |
| 5920 | : p->v.List.elts; |
| 5921 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5922 | Py_ssize_t star = -1; |
| 5923 | int only_wildcard = 1; |
| 5924 | int star_wildcard = 0; |
| 5925 | // Find a starred name, if it exists. There may be at most one: |
| 5926 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5927 | expr_ty value = asdl_seq_GET(values, i); |
| 5928 | if (value->kind == Starred_kind) { |
| 5929 | value = value->v.Starred.value; |
| 5930 | if (star >= 0) { |
| 5931 | const char *e = "multiple starred names in sequence pattern"; |
| 5932 | return compiler_error(c, e); |
| 5933 | } |
| 5934 | star_wildcard = WILDCARD_CHECK(value); |
| 5935 | star = i; |
| 5936 | } |
| 5937 | only_wildcard &= WILDCARD_CHECK(value); |
| 5938 | } |
| 5939 | basicblock *end, *fail_pop_1; |
| 5940 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5941 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5942 | ADDOP(c, MATCH_SEQUENCE); |
| 5943 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5944 | NEXT_BLOCK(c); |
| 5945 | if (star < 0) { |
| 5946 | // No star: len(subject) == size |
| 5947 | ADDOP(c, GET_LEN); |
| 5948 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 5949 | ADDOP_COMPARE(c, Eq); |
| 5950 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5951 | NEXT_BLOCK(c); |
| 5952 | } |
| 5953 | else if (size > 1) { |
| 5954 | // Star: len(subject) >= size - 1 |
| 5955 | ADDOP(c, GET_LEN); |
| 5956 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 5957 | ADDOP_COMPARE(c, GtE); |
| 5958 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5959 | NEXT_BLOCK(c); |
| 5960 | } |
| 5961 | if (only_wildcard) { |
| 5962 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 5963 | ADDOP(c, POP_TOP); |
| 5964 | ADDOP_LOAD_CONST(c, Py_True); |
| 5965 | } |
| 5966 | else if (star_wildcard) { |
| 5967 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc)); |
| 5968 | } |
| 5969 | else { |
| 5970 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc)); |
| 5971 | } |
| 5972 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5973 | compiler_use_next_block(c, fail_pop_1); |
| 5974 | ADDOP(c, POP_TOP) |
| 5975 | ADDOP_LOAD_CONST(c, Py_False); |
| 5976 | compiler_use_next_block(c, end); |
| 5977 | return 1; |
| 5978 | } |
| 5979 | |
| 5980 | |
| 5981 | static int |
| 5982 | compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5983 | { |
| 5984 | assert(p->kind == Attribute_kind); |
| 5985 | assert(p->v.Attribute.ctx == Load); |
| 5986 | VISIT(c, expr, p); |
| 5987 | ADDOP_COMPARE(c, Eq); |
| 5988 | return 1; |
| 5989 | } |
| 5990 | |
| 5991 | |
| 5992 | static int |
| 5993 | compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5994 | { |
| 5995 | assert(p->kind == Name_kind); |
| 5996 | assert(p->v.Name.ctx == Store); |
| 5997 | assert(WILDCARD_CHECK(p)); |
| 5998 | if (!pc->allow_irrefutable) { |
| 5999 | // Whoops, can't have a wildcard here! |
| 6000 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 6001 | return compiler_error(c, e); |
| 6002 | } |
| 6003 | ADDOP(c, POP_TOP); |
| 6004 | ADDOP_LOAD_CONST(c, Py_True); |
| 6005 | return 1; |
| 6006 | } |
| 6007 | |
| 6008 | |
| 6009 | static int |
| 6010 | compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6011 | { |
| 6012 | SET_LOC(c, p); |
| 6013 | switch (p->kind) { |
| 6014 | case Attribute_kind: |
| 6015 | return compiler_pattern_value(c, p, pc); |
| 6016 | case BinOp_kind: |
| 6017 | // Because we allow "2+2j", things like "2+2" make it this far: |
| 6018 | return compiler_error(c, "patterns cannot include operators"); |
| 6019 | case Call_kind: |
| 6020 | return compiler_pattern_class(c, p, pc); |
| 6021 | case Constant_kind: |
| 6022 | return compiler_pattern_literal(c, p, pc); |
| 6023 | case Dict_kind: |
| 6024 | return compiler_pattern_mapping(c, p, pc); |
| 6025 | case JoinedStr_kind: |
| 6026 | // Because we allow strings, f-strings make it this far: |
| 6027 | return compiler_error(c, "patterns cannot include f-strings"); |
| 6028 | case List_kind: |
| 6029 | case Tuple_kind: |
| 6030 | return compiler_pattern_sequence(c, p, pc); |
| 6031 | case MatchAs_kind: |
| 6032 | return compiler_pattern_as(c, p, pc); |
| 6033 | case MatchOr_kind: |
| 6034 | return compiler_pattern_or(c, p, pc); |
| 6035 | case Name_kind: |
| 6036 | if (WILDCARD_CHECK(p)) { |
| 6037 | return compiler_pattern_wildcard(c, p, pc); |
| 6038 | } |
| 6039 | return compiler_pattern_capture(c, p, pc); |
| 6040 | default: |
| 6041 | Py_UNREACHABLE(); |
| 6042 | } |
| 6043 | } |
| 6044 | |
| 6045 | |
| 6046 | static int |
| 6047 | compiler_match(struct compiler *c, stmt_ty s) |
| 6048 | { |
| 6049 | VISIT(c, expr, s->v.Match.subject); |
| 6050 | basicblock *next, *end; |
| 6051 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6052 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
| 6053 | assert(cases); |
| 6054 | pattern_context pc; |
| 6055 | // We use pc.stores to track: |
| 6056 | // - Repeated name assignments in the same pattern. |
| 6057 | // - Different name assignments in alternatives. |
| 6058 | // It's a set of names, but we don't create it until it's needed: |
| 6059 | pc.stores = NULL; |
| 6060 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6061 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6062 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6063 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6064 | SET_LOC(c, m->pattern); |
| 6065 | RETURN_IF_FALSE(next = compiler_new_block(c)); |
| 6066 | // If pc.allow_irrefutable is 0, any name captures against our subject |
| 6067 | // will raise. Irrefutable cases must be either guarded, last, or both: |
| 6068 | pc.allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6069 | // Only copy the subject if we're *not* on the last case: |
| 6070 | if (i != cases - has_default - 1) { |
| 6071 | ADDOP(c, DUP_TOP); |
| 6072 | } |
| 6073 | int result = compiler_pattern(c, m->pattern, &pc); |
| 6074 | Py_CLEAR(pc.stores); |
| 6075 | RETURN_IF_FALSE(result); |
| 6076 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next); |
| 6077 | NEXT_BLOCK(c); |
| 6078 | if (m->guard) { |
| 6079 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0)); |
| 6080 | } |
| 6081 | // Success! Pop the subject off, we're done with it: |
| 6082 | if (i != cases - has_default - 1) { |
| 6083 | ADDOP(c, POP_TOP); |
| 6084 | } |
| 6085 | VISIT_SEQ(c, stmt, m->body); |
| 6086 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6087 | compiler_use_next_block(c, next); |
| 6088 | } |
| 6089 | if (has_default) { |
| 6090 | if (cases == 1) { |
| 6091 | // No matches. Done with the subject: |
| 6092 | ADDOP(c, POP_TOP); |
| 6093 | } |
| 6094 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6095 | // pushing and popping in the loop above: |
| 6096 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6097 | SET_LOC(c, m->pattern); |
| 6098 | if (m->guard) { |
| 6099 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6100 | } |
| 6101 | VISIT_SEQ(c, stmt, m->body); |
| 6102 | } |
| 6103 | compiler_use_next_block(c, end); |
| 6104 | return 1; |
| 6105 | } |
| 6106 | |
| 6107 | |
| 6108 | #undef WILDCARD_CHECK |
| 6109 | |
| 6110 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6111 | /* End of the compiler section, beginning of the assembler section */ |
| 6112 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6113 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6114 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6115 | |
| 6116 | XXX must handle implicit jumps from one block to next |
| 6117 | */ |
| 6118 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6119 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6120 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6121 | int a_offset; /* offset into bytecode */ |
| 6122 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6123 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6124 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6125 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6126 | int a_lineno; /* lineno of last emitted instruction */ |
| 6127 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6128 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6129 | }; |
| 6130 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6131 | Py_LOCAL_INLINE(void) |
| 6132 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6133 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6134 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6135 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6136 | assert(b->b_startdepth < 0); |
| 6137 | b->b_startdepth = depth; |
| 6138 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6139 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | /* Find the flow path that needs the largest stack. We assume that |
| 6143 | * cycles in the flow graph have no net effect on the stack depth. |
| 6144 | */ |
| 6145 | static int |
| 6146 | stackdepth(struct compiler *c) |
| 6147 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6148 | basicblock *b, *entryblock = NULL; |
| 6149 | basicblock **stack, **sp; |
| 6150 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6151 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6152 | b->b_startdepth = INT_MIN; |
| 6153 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6154 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6155 | } |
| 6156 | if (!entryblock) |
| 6157 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6158 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6159 | if (!stack) { |
| 6160 | PyErr_NoMemory(); |
| 6161 | return -1; |
| 6162 | } |
| 6163 | |
| 6164 | sp = stack; |
| 6165 | stackdepth_push(&sp, entryblock, 0); |
| 6166 | while (sp != stack) { |
| 6167 | b = *--sp; |
| 6168 | int depth = b->b_startdepth; |
| 6169 | assert(depth >= 0); |
| 6170 | basicblock *next = b->b_next; |
| 6171 | for (int i = 0; i < b->b_iused; i++) { |
| 6172 | struct instr *instr = &b->b_instr[i]; |
| 6173 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6174 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6175 | PyErr_Format(PyExc_SystemError, |
| 6176 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6177 | instr->i_opcode, instr->i_oparg); |
| 6178 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6179 | } |
| 6180 | int new_depth = depth + effect; |
| 6181 | if (new_depth > maxdepth) { |
| 6182 | maxdepth = new_depth; |
| 6183 | } |
| 6184 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6185 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6186 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6187 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6188 | int target_depth = depth + effect; |
| 6189 | if (target_depth > maxdepth) { |
| 6190 | maxdepth = target_depth; |
| 6191 | } |
| 6192 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6193 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6194 | } |
| 6195 | depth = new_depth; |
| 6196 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6197 | instr->i_opcode == JUMP_FORWARD || |
| 6198 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6199 | instr->i_opcode == RAISE_VARARGS || |
| 6200 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6201 | { |
| 6202 | /* remaining code is dead */ |
| 6203 | next = NULL; |
| 6204 | break; |
| 6205 | } |
| 6206 | } |
| 6207 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6208 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6209 | stackdepth_push(&sp, next, depth); |
| 6210 | } |
| 6211 | } |
| 6212 | PyObject_Free(stack); |
| 6213 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6214 | } |
| 6215 | |
| 6216 | static int |
| 6217 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6219 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6220 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6221 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6222 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6223 | if (a->a_bytecode == NULL) { |
| 6224 | goto error; |
| 6225 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6226 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6227 | if (a->a_lnotab == NULL) { |
| 6228 | goto error; |
| 6229 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6230 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6231 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6232 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6233 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6234 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6235 | error: |
| 6236 | Py_XDECREF(a->a_bytecode); |
| 6237 | Py_XDECREF(a->a_lnotab); |
| 6238 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | static void |
| 6242 | assemble_free(struct assembler *a) |
| 6243 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6244 | Py_XDECREF(a->a_bytecode); |
| 6245 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6248 | static int |
| 6249 | blocksize(basicblock *b) |
| 6250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6251 | int i; |
| 6252 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6254 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6255 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6256 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6259 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6260 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6261 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6262 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6263 | if (a->a_lnotab_off + 2 >= len) { |
| 6264 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6265 | return 0; |
| 6266 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6267 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6268 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6269 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6270 | *lnotab++ = bdelta; |
| 6271 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6272 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6273 | } |
| 6274 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6275 | /* Appends a range to the end of the line number table. See |
| 6276 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6277 | |
| 6278 | static int |
| 6279 | assemble_line_range(struct assembler *a) |
| 6280 | { |
| 6281 | int ldelta, bdelta; |
| 6282 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6283 | if (bdelta == 0) { |
| 6284 | return 1; |
| 6285 | } |
| 6286 | if (a->a_lineno < 0) { |
| 6287 | ldelta = -128; |
| 6288 | } |
| 6289 | else { |
| 6290 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6291 | a->a_prevlineno = a->a_lineno; |
| 6292 | while (ldelta > 127) { |
| 6293 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6294 | return 0; |
| 6295 | } |
| 6296 | ldelta -= 127; |
| 6297 | } |
| 6298 | while (ldelta < -127) { |
| 6299 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6300 | return 0; |
| 6301 | } |
| 6302 | ldelta += 127; |
| 6303 | } |
| 6304 | } |
| 6305 | assert(-128 <= ldelta && ldelta < 128); |
| 6306 | while (bdelta > 254) { |
| 6307 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6308 | return 0; |
| 6309 | } |
| 6310 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6311 | bdelta -= 254; |
| 6312 | } |
| 6313 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6314 | return 0; |
| 6315 | } |
| 6316 | a->a_lineno_start = a->a_offset; |
| 6317 | return 1; |
| 6318 | } |
| 6319 | |
| 6320 | static int |
| 6321 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6322 | { |
| 6323 | if (i->i_lineno == a->a_lineno) { |
| 6324 | return 1; |
| 6325 | } |
| 6326 | if (!assemble_line_range(a)) { |
| 6327 | return 0; |
| 6328 | } |
| 6329 | a->a_lineno = i->i_lineno; |
| 6330 | return 1; |
| 6331 | } |
| 6332 | |
| 6333 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6334 | /* assemble_emit() |
| 6335 | Extend the bytecode with a new instruction. |
| 6336 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6337 | */ |
| 6338 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6339 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6340 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6341 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6342 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6343 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6344 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6345 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6346 | arg = i->i_oparg; |
| 6347 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6348 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6349 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6350 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6351 | if (len > PY_SSIZE_T_MAX / 2) |
| 6352 | return 0; |
| 6353 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6354 | return 0; |
| 6355 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6356 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6357 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6358 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6359 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6362 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6363 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6365 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6366 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6367 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | /* Compute the size of each block and fixup jump args. |
| 6370 | Replace block pointer with position in bytecode. */ |
| 6371 | do { |
| 6372 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6373 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6374 | bsize = blocksize(b); |
| 6375 | b->b_offset = totsize; |
| 6376 | totsize += bsize; |
| 6377 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6378 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6379 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6380 | bsize = b->b_offset; |
| 6381 | for (i = 0; i < b->b_iused; i++) { |
| 6382 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6383 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6384 | /* Relative jumps are computed relative to |
| 6385 | the instruction pointer after fetching |
| 6386 | the jump instruction. |
| 6387 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6388 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6389 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6390 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6391 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6392 | instr->i_oparg -= bsize; |
| 6393 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6394 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6395 | if (instrsize(instr->i_oparg) != isize) { |
| 6396 | extended_arg_recompile = 1; |
| 6397 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6398 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6399 | } |
| 6400 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6402 | /* XXX: This is an awful hack that could hurt performance, but |
| 6403 | on the bright side it should work until we come up |
| 6404 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6406 | The issue is that in the first loop blocksize() is called |
| 6407 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6408 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6409 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6411 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6412 | The only EXTENDED_ARGs that could be popping up are |
| 6413 | ones in jump instructions. So this should converge |
| 6414 | fairly quickly. |
| 6415 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6416 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6419 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6420 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6422 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6423 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6425 | tuple = PyTuple_New(size); |
| 6426 | if (tuple == NULL) |
| 6427 | return NULL; |
| 6428 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6429 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6430 | Py_INCREF(k); |
| 6431 | assert((i - offset) < size); |
| 6432 | assert((i - offset) >= 0); |
| 6433 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6434 | } |
| 6435 | return tuple; |
| 6436 | } |
| 6437 | |
| 6438 | static PyObject * |
| 6439 | consts_dict_keys_inorder(PyObject *dict) |
| 6440 | { |
| 6441 | PyObject *consts, *k, *v; |
| 6442 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6443 | |
| 6444 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6445 | if (consts == NULL) |
| 6446 | return NULL; |
| 6447 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6448 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6449 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6450 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6451 | * the object we want is always second. */ |
| 6452 | if (PyTuple_CheckExact(k)) { |
| 6453 | k = PyTuple_GET_ITEM(k, 1); |
| 6454 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6455 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6456 | assert(i < size); |
| 6457 | assert(i >= 0); |
| 6458 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6459 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6460 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6463 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6464 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6466 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6467 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6468 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6469 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6470 | if (ste->ste_nested) |
| 6471 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6472 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6473 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6474 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6475 | flags |= CO_COROUTINE; |
| 6476 | if (ste->ste_generator && ste->ste_coroutine) |
| 6477 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6478 | if (ste->ste_varargs) |
| 6479 | flags |= CO_VARARGS; |
| 6480 | if (ste->ste_varkeywords) |
| 6481 | flags |= CO_VARKEYWORDS; |
| 6482 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6483 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6484 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6485 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6486 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6487 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6488 | ste->ste_coroutine && |
| 6489 | !ste->ste_generator) { |
| 6490 | flags |= CO_COROUTINE; |
| 6491 | } |
| 6492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6493 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6494 | } |
| 6495 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6496 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6497 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6498 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6499 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6500 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6501 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6502 | if (key == NULL) { |
| 6503 | return 0; |
| 6504 | } |
| 6505 | |
| 6506 | // t is borrowed reference |
| 6507 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6508 | Py_DECREF(key); |
| 6509 | if (t == NULL) { |
| 6510 | return 0; |
| 6511 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6512 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6513 | return 1; |
| 6514 | } |
| 6515 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6516 | if (PyTuple_CheckExact(t)) { |
| 6517 | // t is still borrowed reference |
| 6518 | t = PyTuple_GET_ITEM(t, 1); |
| 6519 | } |
| 6520 | |
| 6521 | Py_INCREF(t); |
| 6522 | Py_DECREF(*obj); |
| 6523 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6524 | return 1; |
| 6525 | } |
| 6526 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6527 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6528 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6530 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6531 | PyObject *names = NULL; |
| 6532 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6533 | PyObject *name = NULL; |
| 6534 | PyObject *freevars = NULL; |
| 6535 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6536 | Py_ssize_t nlocals; |
| 6537 | int nlocals_int; |
| 6538 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6539 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
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 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6542 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6543 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6544 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6545 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6546 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6547 | if (!cellvars) |
| 6548 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6549 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6550 | if (!freevars) |
| 6551 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6552 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6553 | if (!merge_const_one(c, &names) || |
| 6554 | !merge_const_one(c, &varnames) || |
| 6555 | !merge_const_one(c, &cellvars) || |
| 6556 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6557 | { |
| 6558 | goto error; |
| 6559 | } |
| 6560 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6561 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6562 | assert(nlocals < INT_MAX); |
| 6563 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6565 | flags = compute_code_flags(c); |
| 6566 | if (flags < 0) |
| 6567 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6568 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6569 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6570 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6572 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6573 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6574 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6575 | goto error; |
| 6576 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6577 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6578 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6579 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6580 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6581 | maxdepth = stackdepth(c); |
| 6582 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6583 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6584 | goto error; |
| 6585 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6586 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6587 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6588 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6589 | varnames, freevars, cellvars, c->c_filename, |
| 6590 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6591 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6592 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6593 | Py_XDECREF(names); |
| 6594 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6595 | Py_XDECREF(name); |
| 6596 | Py_XDECREF(freevars); |
| 6597 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6601 | |
| 6602 | /* For debugging purposes only */ |
| 6603 | #if 0 |
| 6604 | static void |
| 6605 | dump_instr(const struct instr *i) |
| 6606 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6607 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 6608 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6609 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6612 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6613 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6614 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6615 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6616 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6617 | } |
| 6618 | |
| 6619 | static void |
| 6620 | dump_basicblock(const basicblock *b) |
| 6621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6622 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6623 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6624 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6625 | if (b->b_instr) { |
| 6626 | int i; |
| 6627 | for (i = 0; i < b->b_iused; i++) { |
| 6628 | fprintf(stderr, " [%02d] ", i); |
| 6629 | dump_instr(b->b_instr + i); |
| 6630 | } |
| 6631 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6632 | } |
| 6633 | #endif |
| 6634 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6635 | |
| 6636 | static int |
| 6637 | normalize_basic_block(basicblock *bb); |
| 6638 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6639 | static int |
| 6640 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 6641 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6642 | static int |
| 6643 | ensure_exits_have_lineno(struct compiler *c); |
| 6644 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6645 | static PyCodeObject * |
| 6646 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6647 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6648 | basicblock *b, *entryblock; |
| 6649 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6650 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6651 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6652 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6654 | /* Make sure every block that falls off the end returns None. |
| 6655 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6656 | block ends with a jump or return b_next shouldn't set. |
| 6657 | */ |
| 6658 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6659 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6660 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6661 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6662 | ADDOP(c, RETURN_VALUE); |
| 6663 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6664 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6665 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6666 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6667 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6668 | } |
| 6669 | } |
| 6670 | |
| 6671 | if (ensure_exits_have_lineno(c)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6672 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6675 | nblocks = 0; |
| 6676 | entryblock = NULL; |
| 6677 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6678 | nblocks++; |
| 6679 | entryblock = b; |
| 6680 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6682 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6683 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 6684 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6685 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6686 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6687 | c->u->u_firstlineno = 1; |
| 6688 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6690 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6691 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6692 | a.a_entry = entryblock; |
| 6693 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6694 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6695 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6696 | if (consts == NULL) { |
| 6697 | goto error; |
| 6698 | } |
| 6699 | if (optimize_cfg(&a, consts)) { |
| 6700 | goto error; |
| 6701 | } |
| 6702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6704 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6705 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6706 | /* Emit code. */ |
| 6707 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6708 | for (j = 0; j < b->b_iused; j++) |
| 6709 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6710 | goto error; |
| 6711 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6712 | if (!assemble_line_range(&a)) { |
| 6713 | return 0; |
| 6714 | } |
| 6715 | /* Emit sentinel at end of line number table */ |
| 6716 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6717 | goto error; |
| 6718 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6719 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6720 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6721 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6722 | } |
| 6723 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6724 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6725 | } |
| 6726 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 6727 | goto error; |
| 6728 | } |
| 6729 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 6730 | goto error; |
| 6731 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6732 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6733 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6734 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6735 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6736 | assemble_free(&a); |
| 6737 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6738 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6739 | |
| 6740 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6741 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6742 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6743 | PyArena *arena) |
| 6744 | { |
| 6745 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6746 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6747 | |
| 6748 | |
| 6749 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6750 | with LOAD_CONST (c1, c2, ... cn). |
| 6751 | The consts table must still be in list form so that the |
| 6752 | new constant (c1, c2, ... cn) can be appended. |
| 6753 | Called with codestr pointing to the first LOAD_CONST. |
| 6754 | */ |
| 6755 | static int |
| 6756 | fold_tuple_on_constants(struct instr *inst, |
| 6757 | int n, PyObject *consts) |
| 6758 | { |
| 6759 | /* Pre-conditions */ |
| 6760 | assert(PyList_CheckExact(consts)); |
| 6761 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6762 | assert(inst[n].i_oparg == n); |
| 6763 | |
| 6764 | for (int i = 0; i < n; i++) { |
| 6765 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6766 | return 0; |
| 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | /* Buildup new tuple of constants */ |
| 6771 | PyObject *newconst = PyTuple_New(n); |
| 6772 | if (newconst == NULL) { |
| 6773 | return -1; |
| 6774 | } |
| 6775 | for (int i = 0; i < n; i++) { |
| 6776 | int arg = inst[i].i_oparg; |
| 6777 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6778 | Py_INCREF(constant); |
| 6779 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6780 | } |
| 6781 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6782 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6783 | Py_DECREF(newconst); |
| 6784 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6785 | return -1; |
| 6786 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6787 | if (PyList_Append(consts, newconst)) { |
| 6788 | Py_DECREF(newconst); |
| 6789 | return -1; |
| 6790 | } |
| 6791 | Py_DECREF(newconst); |
| 6792 | for (int i = 0; i < n; i++) { |
| 6793 | inst[i].i_opcode = NOP; |
| 6794 | } |
| 6795 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6796 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6797 | return 0; |
| 6798 | } |
| 6799 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6800 | |
| 6801 | static int |
| 6802 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 6803 | assert (bb->b_iused > 0); |
| 6804 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 6805 | assert (is_jump(inst)); |
| 6806 | assert (inst->i_target->b_iused > 0); |
| 6807 | struct instr *target = &inst->i_target->b_instr[0]; |
| 6808 | if (inst->i_target == target->i_target) { |
| 6809 | /* Nothing to do */ |
| 6810 | return 0; |
| 6811 | } |
| 6812 | int lineno = target->i_lineno; |
| 6813 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 6814 | return -1; |
| 6815 | } |
| 6816 | assert (bb->b_iused >= 2); |
| 6817 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 6818 | return 0; |
| 6819 | } |
| 6820 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6821 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6822 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6823 | |
| 6824 | /* Optimization */ |
| 6825 | static int |
| 6826 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6827 | { |
| 6828 | assert(PyList_CheckExact(consts)); |
| 6829 | struct instr nop; |
| 6830 | nop.i_opcode = NOP; |
| 6831 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6832 | for (int i = 0; i < bb->b_iused; i++) { |
| 6833 | struct instr *inst = &bb->b_instr[i]; |
| 6834 | int oparg = inst->i_oparg; |
| 6835 | 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] | 6836 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6837 | /* Skip over empty basic blocks. */ |
| 6838 | while (inst->i_target->b_iused == 0) { |
| 6839 | inst->i_target = inst->i_target->b_next; |
| 6840 | } |
| 6841 | target = &inst->i_target->b_instr[0]; |
| 6842 | } |
| 6843 | else { |
| 6844 | target = &nop; |
| 6845 | } |
| 6846 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6847 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6848 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6849 | { |
| 6850 | PyObject* cnt; |
| 6851 | int is_true; |
| 6852 | int jump_if_true; |
| 6853 | switch(nextop) { |
| 6854 | case POP_JUMP_IF_FALSE: |
| 6855 | case POP_JUMP_IF_TRUE: |
| 6856 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6857 | is_true = PyObject_IsTrue(cnt); |
| 6858 | if (is_true == -1) { |
| 6859 | goto error; |
| 6860 | } |
| 6861 | inst->i_opcode = NOP; |
| 6862 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6863 | if (is_true == jump_if_true) { |
| 6864 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6865 | bb->b_nofallthrough = 1; |
| 6866 | } |
| 6867 | else { |
| 6868 | bb->b_instr[i+1].i_opcode = NOP; |
| 6869 | } |
| 6870 | break; |
| 6871 | case JUMP_IF_FALSE_OR_POP: |
| 6872 | case JUMP_IF_TRUE_OR_POP: |
| 6873 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6874 | is_true = PyObject_IsTrue(cnt); |
| 6875 | if (is_true == -1) { |
| 6876 | goto error; |
| 6877 | } |
| 6878 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6879 | if (is_true == jump_if_true) { |
| 6880 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6881 | bb->b_nofallthrough = 1; |
| 6882 | } |
| 6883 | else { |
| 6884 | inst->i_opcode = NOP; |
| 6885 | bb->b_instr[i+1].i_opcode = NOP; |
| 6886 | } |
| 6887 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6888 | } |
| 6889 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6890 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6891 | |
| 6892 | /* Try to fold tuples of constants. |
| 6893 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6894 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6895 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6896 | case BUILD_TUPLE: |
| 6897 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6898 | switch(oparg) { |
| 6899 | case 1: |
| 6900 | inst->i_opcode = NOP; |
| 6901 | bb->b_instr[i+1].i_opcode = NOP; |
| 6902 | break; |
| 6903 | case 2: |
| 6904 | inst->i_opcode = ROT_TWO; |
| 6905 | bb->b_instr[i+1].i_opcode = NOP; |
| 6906 | break; |
| 6907 | case 3: |
| 6908 | inst->i_opcode = ROT_THREE; |
| 6909 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6910 | } |
| 6911 | break; |
| 6912 | } |
| 6913 | if (i >= oparg) { |
| 6914 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6915 | goto error; |
| 6916 | } |
| 6917 | } |
| 6918 | break; |
| 6919 | |
| 6920 | /* Simplify conditional jump to conditional jump where the |
| 6921 | result of the first test implies the success of a similar |
| 6922 | test or the failure of the opposite test. |
| 6923 | Arises in code like: |
| 6924 | "a and b or c" |
| 6925 | "(a and b) and c" |
| 6926 | "(a or b) or c" |
| 6927 | "(a or b) and c" |
| 6928 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6929 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6930 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6931 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6932 | where y+1 is the instruction following the second test. |
| 6933 | */ |
| 6934 | case JUMP_IF_FALSE_OR_POP: |
| 6935 | switch(target->i_opcode) { |
| 6936 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6937 | if (inst->i_lineno == target->i_lineno) { |
| 6938 | *inst = *target; |
| 6939 | i--; |
| 6940 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6941 | break; |
| 6942 | case JUMP_ABSOLUTE: |
| 6943 | case JUMP_FORWARD: |
| 6944 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6945 | if (inst->i_lineno == target->i_lineno && |
| 6946 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6947 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6948 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6949 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6950 | break; |
| 6951 | case JUMP_IF_TRUE_OR_POP: |
| 6952 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6953 | if (inst->i_lineno == target->i_lineno) { |
| 6954 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6955 | inst->i_target = inst->i_target->b_next; |
| 6956 | --i; |
| 6957 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6958 | break; |
| 6959 | } |
| 6960 | break; |
| 6961 | |
| 6962 | case JUMP_IF_TRUE_OR_POP: |
| 6963 | switch(target->i_opcode) { |
| 6964 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6965 | if (inst->i_lineno == target->i_lineno) { |
| 6966 | *inst = *target; |
| 6967 | i--; |
| 6968 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6969 | break; |
| 6970 | case JUMP_ABSOLUTE: |
| 6971 | case JUMP_FORWARD: |
| 6972 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6973 | if (inst->i_lineno == target->i_lineno && |
| 6974 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6975 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6976 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6977 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6978 | break; |
| 6979 | case JUMP_IF_FALSE_OR_POP: |
| 6980 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6981 | if (inst->i_lineno == target->i_lineno) { |
| 6982 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 6983 | inst->i_target = inst->i_target->b_next; |
| 6984 | --i; |
| 6985 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6986 | break; |
| 6987 | } |
| 6988 | break; |
| 6989 | |
| 6990 | case POP_JUMP_IF_FALSE: |
| 6991 | switch(target->i_opcode) { |
| 6992 | case JUMP_ABSOLUTE: |
| 6993 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6994 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6995 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6996 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6997 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6998 | break; |
| 6999 | } |
| 7000 | break; |
| 7001 | |
| 7002 | case POP_JUMP_IF_TRUE: |
| 7003 | switch(target->i_opcode) { |
| 7004 | case JUMP_ABSOLUTE: |
| 7005 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7006 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7007 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7008 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7009 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7010 | break; |
| 7011 | } |
| 7012 | break; |
| 7013 | |
| 7014 | case JUMP_ABSOLUTE: |
| 7015 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7016 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7017 | switch(target->i_opcode) { |
| 7018 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7019 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7020 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7021 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7022 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7023 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7024 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7025 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7026 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7027 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7028 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7029 | default: |
| 7030 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7031 | basicblock *to_copy = inst->i_target; |
| 7032 | inst->i_opcode = NOP; |
| 7033 | for (i = 0; i < to_copy->b_iused; i++) { |
| 7034 | int index = compiler_next_instr(bb); |
| 7035 | if (index < 0) { |
| 7036 | return -1; |
| 7037 | } |
| 7038 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7039 | } |
| 7040 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7041 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7042 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7043 | } |
| 7044 | } |
| 7045 | return 0; |
| 7046 | error: |
| 7047 | return -1; |
| 7048 | } |
| 7049 | |
| 7050 | |
| 7051 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7052 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7053 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7054 | int dest = 0; |
| 7055 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7056 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7057 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7058 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7059 | if (lineno < 0) { |
| 7060 | continue; |
| 7061 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7062 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7063 | if (prev_lineno == lineno) { |
| 7064 | continue; |
| 7065 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7066 | /* 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] | 7067 | if (src < bb->b_iused - 1) { |
| 7068 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7069 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7070 | bb->b_instr[src+1].i_lineno = lineno; |
| 7071 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7072 | } |
| 7073 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7074 | else { |
| 7075 | basicblock* next = bb->b_next; |
| 7076 | while (next && next->b_iused == 0) { |
| 7077 | next = next->b_next; |
| 7078 | } |
| 7079 | /* or if last instruction in BB and next BB has same line number */ |
| 7080 | if (next) { |
| 7081 | if (lineno == next->b_instr[0].i_lineno) { |
| 7082 | continue; |
| 7083 | } |
| 7084 | } |
| 7085 | } |
| 7086 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7087 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7088 | if (dest != src) { |
| 7089 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7090 | } |
| 7091 | dest++; |
| 7092 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7093 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7094 | assert(dest <= bb->b_iused); |
| 7095 | bb->b_iused = dest; |
| 7096 | } |
| 7097 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7098 | static int |
| 7099 | normalize_basic_block(basicblock *bb) { |
| 7100 | /* Mark blocks as exit and/or nofallthrough. |
| 7101 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7102 | for (int i = 0; i < bb->b_iused; i++) { |
| 7103 | switch(bb->b_instr[i].i_opcode) { |
| 7104 | case RETURN_VALUE: |
| 7105 | case RAISE_VARARGS: |
| 7106 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7107 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7108 | bb->b_nofallthrough = 1; |
| 7109 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7110 | case JUMP_ABSOLUTE: |
| 7111 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7112 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7113 | /* fall through */ |
| 7114 | case POP_JUMP_IF_FALSE: |
| 7115 | case POP_JUMP_IF_TRUE: |
| 7116 | case JUMP_IF_FALSE_OR_POP: |
| 7117 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7118 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7119 | if (i != bb->b_iused-1) { |
| 7120 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7121 | return -1; |
| 7122 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7123 | /* Skip over empty basic blocks. */ |
| 7124 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7125 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7126 | } |
| 7127 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7128 | } |
| 7129 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7130 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7131 | } |
| 7132 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7133 | static int |
| 7134 | mark_reachable(struct assembler *a) { |
| 7135 | basicblock **stack, **sp; |
| 7136 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7137 | if (stack == NULL) { |
| 7138 | return -1; |
| 7139 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7140 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7141 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7142 | while (sp > stack) { |
| 7143 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7144 | if (b->b_next && !b->b_nofallthrough) { |
| 7145 | if (b->b_next->b_predecessors == 0) { |
| 7146 | *sp++ = b->b_next; |
| 7147 | } |
| 7148 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7149 | } |
| 7150 | for (int i = 0; i < b->b_iused; i++) { |
| 7151 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7152 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7153 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7154 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7155 | *sp++ = target; |
| 7156 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7157 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7158 | } |
| 7159 | } |
| 7160 | } |
| 7161 | PyObject_Free(stack); |
| 7162 | return 0; |
| 7163 | } |
| 7164 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7165 | static void |
| 7166 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7167 | /* Eliminate empty blocks */ |
| 7168 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7169 | basicblock *next = b->b_next; |
| 7170 | if (next) { |
| 7171 | while (next->b_iused == 0 && next->b_next) { |
| 7172 | next = next->b_next; |
| 7173 | } |
| 7174 | b->b_next = next; |
| 7175 | } |
| 7176 | } |
| 7177 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7178 | if (b->b_iused == 0) { |
| 7179 | continue; |
| 7180 | } |
| 7181 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7182 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7183 | while (target->b_iused == 0) { |
| 7184 | target = target->b_next; |
| 7185 | } |
| 7186 | b->b_instr[b->b_iused-1].i_target = target; |
| 7187 | } |
| 7188 | } |
| 7189 | } |
| 7190 | |
| 7191 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7192 | /* 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] | 7193 | * then copy the line number. If a successor block has no line number, and only |
| 7194 | * one predecessor, then inherit the line number. |
| 7195 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7196 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7197 | * but has no impact on the generated line number events. |
| 7198 | */ |
| 7199 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7200 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7201 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7202 | if (b->b_iused == 0) { |
| 7203 | continue; |
| 7204 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7205 | int prev_lineno = -1; |
| 7206 | for (int i = 0; i < b->b_iused; i++) { |
| 7207 | if (b->b_instr[i].i_lineno < 0) { |
| 7208 | b->b_instr[i].i_lineno = prev_lineno; |
| 7209 | } |
| 7210 | else { |
| 7211 | prev_lineno = b->b_instr[i].i_lineno; |
| 7212 | } |
| 7213 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7214 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7215 | assert(b->b_next->b_iused); |
| 7216 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7217 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7218 | } |
| 7219 | } |
| 7220 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7221 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7222 | /* Note: Only actual jumps, not exception handlers */ |
| 7223 | case SETUP_ASYNC_WITH: |
| 7224 | case SETUP_WITH: |
| 7225 | case SETUP_FINALLY: |
| 7226 | continue; |
| 7227 | } |
| 7228 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7229 | if (target->b_predecessors == 1) { |
| 7230 | if (target->b_instr[0].i_lineno < 0) { |
| 7231 | target->b_instr[0].i_lineno = prev_lineno; |
| 7232 | } |
| 7233 | } |
| 7234 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7235 | } |
| 7236 | } |
| 7237 | |
| 7238 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7239 | The consts object should still be in list form to allow new constants |
| 7240 | to be appended. |
| 7241 | |
| 7242 | All transformations keep the code size the same or smaller. |
| 7243 | For those that reduce size, the gaps are initially filled with |
| 7244 | NOPs. Later those NOPs are removed. |
| 7245 | */ |
| 7246 | |
| 7247 | static int |
| 7248 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 7249 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7250 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7251 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7252 | return -1; |
| 7253 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7254 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7255 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7256 | } |
| 7257 | if (mark_reachable(a)) { |
| 7258 | return -1; |
| 7259 | } |
| 7260 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7261 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7262 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7263 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7264 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7265 | } |
| 7266 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7267 | basicblock *pred = NULL; |
| 7268 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7269 | int prev_lineno = -1; |
| 7270 | if (pred && pred->b_iused) { |
| 7271 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7272 | } |
| 7273 | clean_basic_block(b, prev_lineno); |
| 7274 | pred = b->b_nofallthrough ? NULL : b; |
| 7275 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7276 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7277 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7278 | block ends with a jump instruction, check if the next non-empty block |
| 7279 | reached through normal flow control is the target of that jump. If it |
| 7280 | is, then the jump instruction is redundant and can be deleted. |
| 7281 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7282 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7283 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7284 | if (b->b_iused > 0) { |
| 7285 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7286 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7287 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7288 | if (b_last_instr->i_target == b->b_next) { |
| 7289 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7290 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7291 | b_last_instr->i_opcode = NOP; |
| 7292 | clean_basic_block(b, -1); |
| 7293 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7298 | if (maybe_empty_blocks) { |
| 7299 | eliminate_empty_basic_blocks(a->a_entry); |
| 7300 | } |
| 7301 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7302 | return 0; |
| 7303 | } |
| 7304 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7305 | static inline int |
| 7306 | is_exit_without_lineno(basicblock *b) { |
| 7307 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7308 | } |
| 7309 | |
| 7310 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7311 | * after a frame terminates. It would be prohibitively expensive |
| 7312 | * to continuously update the f_lineno field at runtime, |
| 7313 | * so we make sure that all exiting instruction (raises and returns) |
| 7314 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7315 | * We can do this by duplicating the exit blocks without line number |
| 7316 | * so that none have more than one predecessor. We can then safely |
| 7317 | * copy the line number from the sole predecessor block. |
| 7318 | */ |
| 7319 | static int |
| 7320 | ensure_exits_have_lineno(struct compiler *c) |
| 7321 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7322 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7323 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7324 | */ |
| 7325 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7326 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7327 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7328 | /* Note: Only actual jumps, not exception handlers */ |
| 7329 | case SETUP_ASYNC_WITH: |
| 7330 | case SETUP_WITH: |
| 7331 | case SETUP_FINALLY: |
| 7332 | continue; |
| 7333 | } |
| 7334 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7335 | if (is_exit_without_lineno(target)) { |
| 7336 | basicblock *new_target = compiler_copy_block(c, target); |
| 7337 | if (new_target == NULL) { |
| 7338 | return -1; |
| 7339 | } |
| 7340 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7341 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 7342 | } |
| 7343 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7344 | entry = b; |
| 7345 | } |
| 7346 | assert(entry != NULL); |
| 7347 | if (is_exit_without_lineno(entry)) { |
| 7348 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7349 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7350 | /* Eliminate empty blocks */ |
| 7351 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7352 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7353 | b->b_next = b->b_next->b_next; |
| 7354 | } |
| 7355 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7356 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7357 | * fall through, and thus can only have a single predecessor */ |
| 7358 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7359 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7360 | if (is_exit_without_lineno(b->b_next)) { |
| 7361 | assert(b->b_next->b_iused > 0); |
| 7362 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7363 | } |
| 7364 | } |
| 7365 | } |
| 7366 | return 0; |
| 7367 | } |
| 7368 | |
| 7369 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7370 | /* Retained for API compatibility. |
| 7371 | * Optimization is now done in optimize_cfg */ |
| 7372 | |
| 7373 | PyObject * |
| 7374 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7375 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7376 | { |
| 7377 | Py_INCREF(code); |
| 7378 | return code; |
| 7379 | } |