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 | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame^] | 25 | #include "pycore_ast.h" // _PyAST_GetDocString() |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 26 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 27 | #include "pycore_long.h" // _PyLong_GetZero() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame^] | 29 | #include "symtable.h" // struct symtable |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 30 | #define NEED_OPCODE_JUMP_TABLES |
Victor Stinner | 526fdeb | 2021-03-17 23:50:50 +0100 | [diff] [blame^] | 31 | #include "opcode.h" // EXTENDED_ARG |
| 32 | #include "wordcode_helpers.h" // instrsize() |
| 33 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 34 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 35 | #define DEFAULT_BLOCK_SIZE 16 |
| 36 | #define DEFAULT_BLOCKS 8 |
| 37 | #define DEFAULT_CODE_SIZE 128 |
| 38 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 39 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 40 | #define COMP_GENEXP 0 |
| 41 | #define COMP_LISTCOMP 1 |
| 42 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 43 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 44 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 45 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 46 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 47 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 48 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 49 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | unsigned char i_opcode; |
| 51 | int i_oparg; |
| 52 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 53 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 56 | #define LOG_BITS_PER_INT 5 |
| 57 | #define MASK_LOW_LOG_BITS 31 |
| 58 | |
| 59 | static inline int |
| 60 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 61 | /* Is the relevant bit set in the relevant word? */ |
| 62 | /* 256 bits fit into 8 32-bits words. |
| 63 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 64 | * Bit within word is the low bits of bitindex. |
| 65 | */ |
| 66 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 67 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 68 | } |
| 69 | |
| 70 | static inline int |
| 71 | is_relative_jump(struct instr *i) |
| 72 | { |
| 73 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 74 | } |
| 75 | |
| 76 | static inline int |
| 77 | is_jump(struct instr *i) |
| 78 | { |
| 79 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 80 | } |
| 81 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 82 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 83 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 84 | reverse order that the block are allocated. b_list points to the next |
| 85 | 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] | 86 | struct basicblock_ *b_list; |
| 87 | /* number of instructions used */ |
| 88 | int b_iused; |
| 89 | /* length of instruction array (b_instr) */ |
| 90 | int b_ialloc; |
| 91 | /* pointer to an array of instructions, initially NULL */ |
| 92 | struct instr *b_instr; |
| 93 | /* If b_next is non-NULL, it is a pointer to the next |
| 94 | block reached by normal control flow. */ |
| 95 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 97 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 98 | /* Number of predecssors that a block has. */ |
| 99 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 100 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 101 | unsigned b_nofallthrough : 1; |
| 102 | /* Basic block exits scope (it ends with a return or raise) */ |
| 103 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 105 | int b_startdepth; |
| 106 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 107 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 108 | } basicblock; |
| 109 | |
| 110 | /* fblockinfo tracks the current frame block. |
| 111 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 112 | A frame block is used to handle loops, try/except, and try/finally. |
| 113 | It's called a frame block to distinguish it from a basic block in the |
| 114 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 115 | */ |
| 116 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 117 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
| 118 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 119 | |
| 120 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | enum fblocktype fb_type; |
| 122 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 123 | /* (optional) type-specific exit or cleanup block */ |
| 124 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 125 | /* (optional) additional information required for unwinding */ |
| 126 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 129 | enum { |
| 130 | COMPILER_SCOPE_MODULE, |
| 131 | COMPILER_SCOPE_CLASS, |
| 132 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 133 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 134 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 135 | COMPILER_SCOPE_COMPREHENSION, |
| 136 | }; |
| 137 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 138 | /* The following items change on entry and exit of code blocks. |
| 139 | They must be saved and restored when returning to a block. |
| 140 | */ |
| 141 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 145 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 146 | int u_scope_type; |
| 147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | /* The following fields are dicts that map objects to |
| 149 | the index of them in co_XXX. The index is used as |
| 150 | the argument for opcodes that refer to those collections. |
| 151 | */ |
| 152 | PyObject *u_consts; /* all constants */ |
| 153 | PyObject *u_names; /* all names */ |
| 154 | PyObject *u_varnames; /* local variables */ |
| 155 | PyObject *u_cellvars; /* cell variables */ |
| 156 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 159 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 160 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 161 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 162 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | /* Pointer to the most recently allocated block. By following b_list |
| 164 | members, you can reach all early allocated blocks. */ |
| 165 | basicblock *u_blocks; |
| 166 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | int u_nfblocks; |
| 169 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | int u_firstlineno; /* the first lineno of the block */ |
| 172 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 173 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 177 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 178 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | 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] | 180 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 181 | |
| 182 | Note that we don't track recursion levels during compilation - the |
| 183 | task of detecting and rejecting excessive levels of nesting is |
| 184 | handled by the symbol analysis pass. |
| 185 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 186 | */ |
| 187 | |
| 188 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 189 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | struct symtable *c_st; |
| 191 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 192 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 193 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 194 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | int c_interactive; /* true if in interactive mode */ |
| 196 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 197 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 198 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | struct compiler_unit *u; /* compiler state for current block */ |
| 200 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 201 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 204 | typedef struct { |
| 205 | PyObject *stores; |
| 206 | int allow_irrefutable; |
| 207 | } pattern_context; |
| 208 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 209 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 210 | static void compiler_free(struct compiler *); |
| 211 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 212 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 213 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 214 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 215 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 216 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 217 | static int compiler_error(struct compiler *, const char *, ...); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 218 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 219 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 220 | |
| 221 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 222 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 223 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 224 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 225 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 226 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 227 | static int compiler_subscript(struct compiler *, expr_ty); |
| 228 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 229 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 230 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 231 | 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] | 232 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 233 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 234 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 235 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 236 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 237 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 238 | asdl_expr_seq *args, |
| 239 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 240 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 241 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 242 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 243 | static int compiler_sync_comprehension_generator( |
| 244 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 245 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 246 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 247 | expr_ty elt, expr_ty val, int type); |
| 248 | |
| 249 | static int compiler_async_comprehension_generator( |
| 250 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 251 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 252 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 253 | expr_ty elt, expr_ty val, int type); |
| 254 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 255 | static int compiler_pattern(struct compiler *, expr_ty, pattern_context *); |
| 256 | static int compiler_match(struct compiler *, stmt_ty); |
| 257 | static int compiler_pattern_subpattern(struct compiler *, expr_ty, |
| 258 | pattern_context *); |
| 259 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 260 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 261 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 262 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 263 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 264 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 265 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 266 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 267 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | /* Name mangling: __private becomes _classname__private. |
| 269 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 270 | PyObject *result; |
| 271 | size_t nlen, plen, ipriv; |
| 272 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 274 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 275 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | Py_INCREF(ident); |
| 277 | return ident; |
| 278 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 279 | nlen = PyUnicode_GET_LENGTH(ident); |
| 280 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | The only time a name with a dot can occur is when |
| 284 | we are compiling an import statement that has a |
| 285 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | TODO(jhylton): Decide whether we want to support |
| 288 | mangling of the module name, e.g. __M.X. |
| 289 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 290 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 291 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 292 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | Py_INCREF(ident); |
| 294 | return ident; /* Don't mangle __whatever__ */ |
| 295 | } |
| 296 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 297 | ipriv = 0; |
| 298 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 299 | ipriv++; |
| 300 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | Py_INCREF(ident); |
| 302 | return ident; /* Don't mangle if class is just underscores */ |
| 303 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 304 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 306 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 307 | PyErr_SetString(PyExc_OverflowError, |
| 308 | "private identifier too large to be mangled"); |
| 309 | return NULL; |
| 310 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 311 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 312 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 313 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 314 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 315 | |
| 316 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 317 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 319 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 320 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 321 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 322 | Py_DECREF(result); |
| 323 | return NULL; |
| 324 | } |
| 325 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 326 | Py_DECREF(result); |
| 327 | return NULL; |
| 328 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 329 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 330 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 333 | static int |
| 334 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 337 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 338 | c->c_const_cache = PyDict_New(); |
| 339 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | c->c_stack = PyList_New(0); |
| 344 | if (!c->c_stack) { |
| 345 | Py_CLEAR(c->c_const_cache); |
| 346 | return 0; |
| 347 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 353 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 354 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | struct compiler c; |
| 357 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 358 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | if (!__doc__) { |
| 362 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 363 | if (!__doc__) |
| 364 | return NULL; |
| 365 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 366 | if (!__annotations__) { |
| 367 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 368 | if (!__annotations__) |
| 369 | return NULL; |
| 370 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | if (!compiler_init(&c)) |
| 372 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 373 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | c.c_filename = filename; |
| 375 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 376 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | if (c.c_future == NULL) |
| 378 | goto finally; |
| 379 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | flags = &local_flags; |
| 381 | } |
| 382 | merged = c.c_future->ff_features | flags->cf_flags; |
| 383 | c.c_future->ff_features = merged; |
| 384 | flags->cf_flags = merged; |
| 385 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 386 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 388 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 389 | _PyASTOptimizeState state; |
| 390 | state.optimize = c.c_optimize; |
| 391 | state.ff_features = merged; |
| 392 | |
| 393 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 394 | goto finally; |
| 395 | } |
| 396 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 397 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | if (c.c_st == NULL) { |
| 399 | if (!PyErr_Occurred()) |
| 400 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 401 | goto finally; |
| 402 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 405 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 406 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | compiler_free(&c); |
| 408 | assert(co || PyErr_Occurred()); |
| 409 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 413 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 414 | int optimize, PyArena *arena) |
| 415 | { |
| 416 | PyObject *filename; |
| 417 | PyCodeObject *co; |
| 418 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 419 | if (filename == NULL) |
| 420 | return NULL; |
| 421 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 422 | Py_DECREF(filename); |
| 423 | return co; |
| 424 | |
| 425 | } |
| 426 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 427 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 428 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | if (c->c_st) |
| 431 | PySymtable_Free(c->c_st); |
| 432 | if (c->c_future) |
| 433 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 434 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 435 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 439 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 440 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | Py_ssize_t i, n; |
| 443 | PyObject *v, *k; |
| 444 | PyObject *dict = PyDict_New(); |
| 445 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | n = PyList_Size(list); |
| 448 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 449 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | if (!v) { |
| 451 | Py_DECREF(dict); |
| 452 | return NULL; |
| 453 | } |
| 454 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 455 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | Py_DECREF(v); |
| 457 | Py_DECREF(dict); |
| 458 | return NULL; |
| 459 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | Py_DECREF(v); |
| 461 | } |
| 462 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Return new dict containing names from src that match scope(s). |
| 466 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 467 | 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] | 468 | 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] | 469 | values are integers, starting at offset and increasing by one for |
| 470 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 471 | */ |
| 472 | |
| 473 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 474 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 475 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 476 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 478 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | assert(offset >= 0); |
| 481 | if (dest == NULL) |
| 482 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 483 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 484 | /* Sort the keys so that we have a deterministic order on the indexes |
| 485 | saved in the returned dictionary. These indexes are used as indexes |
| 486 | into the free and cell var storage. Therefore if they aren't |
| 487 | deterministic, then the generated bytecode is not deterministic. |
| 488 | */ |
| 489 | sorted_keys = PyDict_Keys(src); |
| 490 | if (sorted_keys == NULL) |
| 491 | return NULL; |
| 492 | if (PyList_Sort(sorted_keys) != 0) { |
| 493 | Py_DECREF(sorted_keys); |
| 494 | return NULL; |
| 495 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 496 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 497 | |
| 498 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | /* XXX this should probably be a macro in symtable.h */ |
| 500 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 501 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 502 | v = PyDict_GetItemWithError(src, k); |
| 503 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | vi = PyLong_AS_LONG(v); |
| 505 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 508 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 510 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | Py_DECREF(dest); |
| 512 | return NULL; |
| 513 | } |
| 514 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 515 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 516 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | Py_DECREF(item); |
| 518 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | return NULL; |
| 520 | } |
| 521 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 524 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 528 | static void |
| 529 | compiler_unit_check(struct compiler_unit *u) |
| 530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | basicblock *block; |
| 532 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 533 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | if (block->b_instr != NULL) { |
| 535 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 536 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | assert(block->b_ialloc >= block->b_iused); |
| 538 | } |
| 539 | else { |
| 540 | assert (block->b_iused == 0); |
| 541 | assert (block->b_ialloc == 0); |
| 542 | } |
| 543 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static void |
| 547 | compiler_unit_free(struct compiler_unit *u) |
| 548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | compiler_unit_check(u); |
| 552 | b = u->u_blocks; |
| 553 | while (b != NULL) { |
| 554 | if (b->b_instr) |
| 555 | PyObject_Free((void *)b->b_instr); |
| 556 | next = b->b_list; |
| 557 | PyObject_Free((void *)b); |
| 558 | b = next; |
| 559 | } |
| 560 | Py_CLEAR(u->u_ste); |
| 561 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 562 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | Py_CLEAR(u->u_consts); |
| 564 | Py_CLEAR(u->u_names); |
| 565 | Py_CLEAR(u->u_varnames); |
| 566 | Py_CLEAR(u->u_freevars); |
| 567 | Py_CLEAR(u->u_cellvars); |
| 568 | Py_CLEAR(u->u_private); |
| 569 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 573 | compiler_enter_scope(struct compiler *c, identifier name, |
| 574 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 577 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 578 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 579 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | struct compiler_unit)); |
| 581 | if (!u) { |
| 582 | PyErr_NoMemory(); |
| 583 | return 0; |
| 584 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 585 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 587 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | u->u_kwonlyargcount = 0; |
| 589 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 590 | if (!u->u_ste) { |
| 591 | compiler_unit_free(u); |
| 592 | return 0; |
| 593 | } |
| 594 | Py_INCREF(name); |
| 595 | u->u_name = name; |
| 596 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 597 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 598 | if (!u->u_varnames || !u->u_cellvars) { |
| 599 | compiler_unit_free(u); |
| 600 | return 0; |
| 601 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 602 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 603 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 604 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 605 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 606 | int res; |
| 607 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 608 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 609 | name = _PyUnicode_FromId(&PyId___class__); |
| 610 | if (!name) { |
| 611 | compiler_unit_free(u); |
| 612 | return 0; |
| 613 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 614 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 615 | if (res < 0) { |
| 616 | compiler_unit_free(u); |
| 617 | return 0; |
| 618 | } |
| 619 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | 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] | 622 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | if (!u->u_freevars) { |
| 624 | compiler_unit_free(u); |
| 625 | return 0; |
| 626 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | u->u_blocks = NULL; |
| 629 | u->u_nfblocks = 0; |
| 630 | u->u_firstlineno = lineno; |
| 631 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 632 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | u->u_consts = PyDict_New(); |
| 634 | if (!u->u_consts) { |
| 635 | compiler_unit_free(u); |
| 636 | return 0; |
| 637 | } |
| 638 | u->u_names = PyDict_New(); |
| 639 | if (!u->u_names) { |
| 640 | compiler_unit_free(u); |
| 641 | return 0; |
| 642 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | /* Push the old compiler_unit on the stack. */ |
| 647 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 648 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 650 | Py_XDECREF(capsule); |
| 651 | compiler_unit_free(u); |
| 652 | return 0; |
| 653 | } |
| 654 | Py_DECREF(capsule); |
| 655 | u->u_private = c->u->u_private; |
| 656 | Py_XINCREF(u->u_private); |
| 657 | } |
| 658 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 661 | |
| 662 | block = compiler_new_block(c); |
| 663 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 665 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 666 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 667 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 668 | if (!compiler_set_qualname(c)) |
| 669 | return 0; |
| 670 | } |
| 671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 675 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 676 | compiler_exit_scope(struct compiler *c) |
| 677 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 678 | // Don't call PySequence_DelItem() with an exception raised |
| 679 | PyObject *exc_type, *exc_val, *exc_tb; |
| 680 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | c->c_nestlevel--; |
| 683 | compiler_unit_free(c->u); |
| 684 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 685 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 687 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 688 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | assert(c->u); |
| 690 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 691 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 692 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 693 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 694 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | compiler_unit_check(c->u); |
| 696 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 697 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 699 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 700 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 701 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 704 | static int |
| 705 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 706 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 707 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 708 | _Py_static_string(dot_locals, ".<locals>"); |
| 709 | Py_ssize_t stack_size; |
| 710 | struct compiler_unit *u = c->u; |
| 711 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 712 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 713 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 714 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 715 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 716 | if (stack_size > 1) { |
| 717 | int scope, force_global = 0; |
| 718 | struct compiler_unit *parent; |
| 719 | PyObject *mangled, *capsule; |
| 720 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 721 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 722 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 723 | assert(parent); |
| 724 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 725 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 726 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 727 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 728 | assert(u->u_name); |
| 729 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 730 | if (!mangled) |
| 731 | return 0; |
| 732 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 733 | Py_DECREF(mangled); |
| 734 | assert(scope != GLOBAL_IMPLICIT); |
| 735 | if (scope == GLOBAL_EXPLICIT) |
| 736 | force_global = 1; |
| 737 | } |
| 738 | |
| 739 | if (!force_global) { |
| 740 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 741 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 742 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 743 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 744 | if (dot_locals_str == NULL) |
| 745 | return 0; |
| 746 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 747 | if (base == NULL) |
| 748 | return 0; |
| 749 | } |
| 750 | else { |
| 751 | Py_INCREF(parent->u_qualname); |
| 752 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 753 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 754 | } |
| 755 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 756 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 757 | if (base != NULL) { |
| 758 | dot_str = _PyUnicode_FromId(&dot); |
| 759 | if (dot_str == NULL) { |
| 760 | Py_DECREF(base); |
| 761 | return 0; |
| 762 | } |
| 763 | name = PyUnicode_Concat(base, dot_str); |
| 764 | Py_DECREF(base); |
| 765 | if (name == NULL) |
| 766 | return 0; |
| 767 | PyUnicode_Append(&name, u->u_name); |
| 768 | if (name == NULL) |
| 769 | return 0; |
| 770 | } |
| 771 | else { |
| 772 | Py_INCREF(u->u_name); |
| 773 | name = u->u_name; |
| 774 | } |
| 775 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 776 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 777 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 780 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 781 | /* Allocate a new block and return a pointer to it. |
| 782 | Returns NULL on error. |
| 783 | */ |
| 784 | |
| 785 | static basicblock * |
| 786 | compiler_new_block(struct compiler *c) |
| 787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | basicblock *b; |
| 789 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 792 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | if (b == NULL) { |
| 794 | PyErr_NoMemory(); |
| 795 | return NULL; |
| 796 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | /* Extend the singly linked list of blocks with new block. */ |
| 798 | b->b_list = u->u_blocks; |
| 799 | u->u_blocks = b; |
| 800 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 804 | compiler_next_block(struct compiler *c) |
| 805 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | basicblock *block = compiler_new_block(c); |
| 807 | if (block == NULL) |
| 808 | return NULL; |
| 809 | c->u->u_curblock->b_next = block; |
| 810 | c->u->u_curblock = block; |
| 811 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static basicblock * |
| 815 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | assert(block != NULL); |
| 818 | c->u->u_curblock->b_next = block; |
| 819 | c->u->u_curblock = block; |
| 820 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 823 | static basicblock * |
| 824 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 825 | { |
| 826 | /* Cannot copy a block if it has a fallthrough, since |
| 827 | * a block can only have one fallthrough predecessor. |
| 828 | */ |
| 829 | assert(block->b_nofallthrough); |
| 830 | basicblock *result = compiler_next_block(c); |
| 831 | if (result == NULL) { |
| 832 | return NULL; |
| 833 | } |
| 834 | for (int i = 0; i < block->b_iused; i++) { |
| 835 | int n = compiler_next_instr(result); |
| 836 | if (n < 0) { |
| 837 | return NULL; |
| 838 | } |
| 839 | result->b_instr[n] = block->b_instr[i]; |
| 840 | } |
| 841 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 842 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 843 | return result; |
| 844 | } |
| 845 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 846 | /* Returns the offset of the next instruction in the current block's |
| 847 | b_instr array. Resizes the b_instr as necessary. |
| 848 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 849 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 850 | |
| 851 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 852 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | assert(b != NULL); |
| 855 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 856 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 857 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | if (b->b_instr == NULL) { |
| 859 | PyErr_NoMemory(); |
| 860 | return -1; |
| 861 | } |
| 862 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | } |
| 864 | else if (b->b_iused == b->b_ialloc) { |
| 865 | struct instr *tmp; |
| 866 | size_t oldsize, newsize; |
| 867 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 868 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 869 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 870 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | PyErr_NoMemory(); |
| 872 | return -1; |
| 873 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | if (newsize == 0) { |
| 876 | PyErr_NoMemory(); |
| 877 | return -1; |
| 878 | } |
| 879 | b->b_ialloc <<= 1; |
| 880 | tmp = (struct instr *)PyObject_Realloc( |
| 881 | (void *)b->b_instr, newsize); |
| 882 | if (tmp == NULL) { |
| 883 | PyErr_NoMemory(); |
| 884 | return -1; |
| 885 | } |
| 886 | b->b_instr = tmp; |
| 887 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 888 | } |
| 889 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 892 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 893 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 894 | The line number is reset in the following cases: |
| 895 | - when entering a new scope |
| 896 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 897 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 898 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 899 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 900 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 901 | #define SET_LOC(c, x) \ |
| 902 | (c)->u->u_lineno = (x)->lineno; \ |
| 903 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 904 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 905 | /* Return the stack effect of opcode with argument oparg. |
| 906 | |
| 907 | Some opcodes have different stack effect when jump to the target and |
| 908 | when not jump. The 'jump' parameter specifies the case: |
| 909 | |
| 910 | * 0 -- when not jump |
| 911 | * 1 -- when jump |
| 912 | * -1 -- maximal |
| 913 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 914 | static int |
| 915 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 918 | case NOP: |
| 919 | case EXTENDED_ARG: |
| 920 | return 0; |
| 921 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 922 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | case POP_TOP: |
| 924 | return -1; |
| 925 | case ROT_TWO: |
| 926 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 927 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | return 0; |
| 929 | case DUP_TOP: |
| 930 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 931 | case DUP_TOP_TWO: |
| 932 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 934 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | case UNARY_POSITIVE: |
| 936 | case UNARY_NEGATIVE: |
| 937 | case UNARY_NOT: |
| 938 | case UNARY_INVERT: |
| 939 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | case SET_ADD: |
| 942 | case LIST_APPEND: |
| 943 | return -1; |
| 944 | case MAP_ADD: |
| 945 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 946 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 947 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | case BINARY_POWER: |
| 949 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 950 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 951 | case BINARY_MODULO: |
| 952 | case BINARY_ADD: |
| 953 | case BINARY_SUBTRACT: |
| 954 | case BINARY_SUBSCR: |
| 955 | case BINARY_FLOOR_DIVIDE: |
| 956 | case BINARY_TRUE_DIVIDE: |
| 957 | return -1; |
| 958 | case INPLACE_FLOOR_DIVIDE: |
| 959 | case INPLACE_TRUE_DIVIDE: |
| 960 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | case INPLACE_ADD: |
| 963 | case INPLACE_SUBTRACT: |
| 964 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 965 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | case INPLACE_MODULO: |
| 967 | return -1; |
| 968 | case STORE_SUBSCR: |
| 969 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | case DELETE_SUBSCR: |
| 971 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | case BINARY_LSHIFT: |
| 974 | case BINARY_RSHIFT: |
| 975 | case BINARY_AND: |
| 976 | case BINARY_XOR: |
| 977 | case BINARY_OR: |
| 978 | return -1; |
| 979 | case INPLACE_POWER: |
| 980 | return -1; |
| 981 | case GET_ITER: |
| 982 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | case PRINT_EXPR: |
| 985 | return -1; |
| 986 | case LOAD_BUILD_CLASS: |
| 987 | return 1; |
| 988 | case INPLACE_LSHIFT: |
| 989 | case INPLACE_RSHIFT: |
| 990 | case INPLACE_AND: |
| 991 | case INPLACE_XOR: |
| 992 | case INPLACE_OR: |
| 993 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 996 | /* 1 in the normal flow. |
| 997 | * Restore the stack position and push 6 values before jumping to |
| 998 | * the handler if an exception be raised. */ |
| 999 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | case RETURN_VALUE: |
| 1001 | return -1; |
| 1002 | case IMPORT_STAR: |
| 1003 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1004 | case SETUP_ANNOTATIONS: |
| 1005 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | case YIELD_VALUE: |
| 1007 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1008 | case YIELD_FROM: |
| 1009 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | case POP_BLOCK: |
| 1011 | return 0; |
| 1012 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1013 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | case STORE_NAME: |
| 1016 | return -1; |
| 1017 | case DELETE_NAME: |
| 1018 | return 0; |
| 1019 | case UNPACK_SEQUENCE: |
| 1020 | return oparg-1; |
| 1021 | case UNPACK_EX: |
| 1022 | return (oparg&0xFF) + (oparg>>8); |
| 1023 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1024 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1025 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case STORE_ATTR: |
| 1028 | return -2; |
| 1029 | case DELETE_ATTR: |
| 1030 | return -1; |
| 1031 | case STORE_GLOBAL: |
| 1032 | return -1; |
| 1033 | case DELETE_GLOBAL: |
| 1034 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | case LOAD_CONST: |
| 1036 | return 1; |
| 1037 | case LOAD_NAME: |
| 1038 | return 1; |
| 1039 | case BUILD_TUPLE: |
| 1040 | case BUILD_LIST: |
| 1041 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1042 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | return 1-oparg; |
| 1044 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1045 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1046 | case BUILD_CONST_KEY_MAP: |
| 1047 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | case LOAD_ATTR: |
| 1049 | return 0; |
| 1050 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1051 | case IS_OP: |
| 1052 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1054 | case JUMP_IF_NOT_EXC_MATCH: |
| 1055 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | case IMPORT_NAME: |
| 1057 | return -1; |
| 1058 | case IMPORT_FROM: |
| 1059 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1060 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1061 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | case JUMP_ABSOLUTE: |
| 1064 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1065 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1066 | case JUMP_IF_TRUE_OR_POP: |
| 1067 | case JUMP_IF_FALSE_OR_POP: |
| 1068 | return jump ? 0 : -1; |
| 1069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | case POP_JUMP_IF_FALSE: |
| 1071 | case POP_JUMP_IF_TRUE: |
| 1072 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | case LOAD_GLOBAL: |
| 1075 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1076 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1077 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1079 | /* 0 in the normal flow. |
| 1080 | * Restore the stack position and push 6 values before jumping to |
| 1081 | * the handler if an exception be raised. */ |
| 1082 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1083 | case RERAISE: |
| 1084 | return -3; |
| 1085 | |
| 1086 | case WITH_EXCEPT_START: |
| 1087 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | case LOAD_FAST: |
| 1090 | return 1; |
| 1091 | case STORE_FAST: |
| 1092 | return -1; |
| 1093 | case DELETE_FAST: |
| 1094 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | case RAISE_VARARGS: |
| 1097 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1098 | |
| 1099 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1101 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1102 | case CALL_METHOD: |
| 1103 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1105 | return -oparg-1; |
| 1106 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1107 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1108 | case MAKE_FUNCTION: |
| 1109 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1110 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | case BUILD_SLICE: |
| 1112 | if (oparg == 3) |
| 1113 | return -2; |
| 1114 | else |
| 1115 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1116 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1117 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | case LOAD_CLOSURE: |
| 1119 | return 1; |
| 1120 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1121 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | return 1; |
| 1123 | case STORE_DEREF: |
| 1124 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1125 | case DELETE_DEREF: |
| 1126 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1127 | |
| 1128 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1129 | case GET_AWAITABLE: |
| 1130 | return 0; |
| 1131 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1132 | /* 0 in the normal flow. |
| 1133 | * Restore the stack position to the position before the result |
| 1134 | * of __aenter__ and push 6 values before jumping to the handler |
| 1135 | * if an exception be raised. */ |
| 1136 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1137 | case BEFORE_ASYNC_WITH: |
| 1138 | return 1; |
| 1139 | case GET_AITER: |
| 1140 | return 0; |
| 1141 | case GET_ANEXT: |
| 1142 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1143 | case GET_YIELD_FROM_ITER: |
| 1144 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1145 | case END_ASYNC_FOR: |
| 1146 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1147 | case FORMAT_VALUE: |
| 1148 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1149 | else 1->1. */ |
| 1150 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1151 | case LOAD_METHOD: |
| 1152 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1153 | case LOAD_ASSERTION_ERROR: |
| 1154 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1155 | case LIST_TO_TUPLE: |
| 1156 | return 0; |
| 1157 | case LIST_EXTEND: |
| 1158 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1159 | case DICT_MERGE: |
| 1160 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1161 | return -1; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1162 | case COPY_DICT_WITHOUT_KEYS: |
| 1163 | return 0; |
| 1164 | case MATCH_CLASS: |
| 1165 | return -1; |
| 1166 | case GET_LEN: |
| 1167 | case MATCH_MAPPING: |
| 1168 | case MATCH_SEQUENCE: |
| 1169 | return 1; |
| 1170 | case MATCH_KEYS: |
| 1171 | return 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1173 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1175 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1178 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1179 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1180 | { |
| 1181 | return stack_effect(opcode, oparg, jump); |
| 1182 | } |
| 1183 | |
| 1184 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1185 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1186 | { |
| 1187 | return stack_effect(opcode, oparg, -1); |
| 1188 | } |
| 1189 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1190 | /* Add an opcode with no argument. |
| 1191 | Returns 0 on failure, 1 on success. |
| 1192 | */ |
| 1193 | |
| 1194 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1195 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | basicblock *b; |
| 1198 | struct instr *i; |
| 1199 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1200 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1201 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | if (off < 0) |
| 1203 | return 0; |
| 1204 | b = c->u->u_curblock; |
| 1205 | i = &b->b_instr[off]; |
| 1206 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1207 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if (opcode == RETURN_VALUE) |
| 1209 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1210 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1214 | static int |
| 1215 | compiler_addop(struct compiler *c, int opcode) |
| 1216 | { |
| 1217 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1218 | } |
| 1219 | |
| 1220 | static int |
| 1221 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1222 | { |
| 1223 | return compiler_addop_line(c, opcode, -1); |
| 1224 | } |
| 1225 | |
| 1226 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1227 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1228 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1229 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1230 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1232 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1233 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1235 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1237 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1238 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1239 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | return -1; |
| 1242 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1243 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | Py_DECREF(v); |
| 1245 | return -1; |
| 1246 | } |
| 1247 | Py_DECREF(v); |
| 1248 | } |
| 1249 | else |
| 1250 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1251 | return arg; |
| 1252 | } |
| 1253 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1254 | // Merge const *o* recursively and return constant key object. |
| 1255 | static PyObject* |
| 1256 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1257 | { |
| 1258 | // None and Ellipsis are singleton, and key is the singleton. |
| 1259 | // No need to merge object and key. |
| 1260 | if (o == Py_None || o == Py_Ellipsis) { |
| 1261 | Py_INCREF(o); |
| 1262 | return o; |
| 1263 | } |
| 1264 | |
| 1265 | PyObject *key = _PyCode_ConstantKey(o); |
| 1266 | if (key == NULL) { |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | |
| 1270 | // t is borrowed reference |
| 1271 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1272 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1273 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1274 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1275 | Py_DECREF(key); |
| 1276 | return t; |
| 1277 | } |
| 1278 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1279 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1280 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1281 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1282 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1283 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1284 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1285 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1286 | PyObject *u = merge_consts_recursive(c, item); |
| 1287 | if (u == NULL) { |
| 1288 | Py_DECREF(key); |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | |
| 1292 | // See _PyCode_ConstantKey() |
| 1293 | PyObject *v; // borrowed |
| 1294 | if (PyTuple_CheckExact(u)) { |
| 1295 | v = PyTuple_GET_ITEM(u, 1); |
| 1296 | } |
| 1297 | else { |
| 1298 | v = u; |
| 1299 | } |
| 1300 | if (v != item) { |
| 1301 | Py_INCREF(v); |
| 1302 | PyTuple_SET_ITEM(o, i, v); |
| 1303 | Py_DECREF(item); |
| 1304 | } |
| 1305 | |
| 1306 | Py_DECREF(u); |
| 1307 | } |
| 1308 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1309 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1310 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1311 | // constant keys. |
| 1312 | // See _PyCode_ConstantKey() for detail. |
| 1313 | assert(PyTuple_CheckExact(key)); |
| 1314 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1315 | |
| 1316 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1317 | if (len == 0) { // empty frozenset should not be re-created. |
| 1318 | return key; |
| 1319 | } |
| 1320 | PyObject *tuple = PyTuple_New(len); |
| 1321 | if (tuple == NULL) { |
| 1322 | Py_DECREF(key); |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | Py_ssize_t i = 0, pos = 0; |
| 1326 | PyObject *item; |
| 1327 | Py_hash_t hash; |
| 1328 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1329 | PyObject *k = merge_consts_recursive(c, item); |
| 1330 | if (k == NULL) { |
| 1331 | Py_DECREF(tuple); |
| 1332 | Py_DECREF(key); |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | PyObject *u; |
| 1336 | if (PyTuple_CheckExact(k)) { |
| 1337 | u = PyTuple_GET_ITEM(k, 1); |
| 1338 | Py_INCREF(u); |
| 1339 | Py_DECREF(k); |
| 1340 | } |
| 1341 | else { |
| 1342 | u = k; |
| 1343 | } |
| 1344 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1345 | i++; |
| 1346 | } |
| 1347 | |
| 1348 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1349 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1350 | PyObject *new = PyFrozenSet_New(tuple); |
| 1351 | Py_DECREF(tuple); |
| 1352 | if (new == NULL) { |
| 1353 | Py_DECREF(key); |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1357 | Py_DECREF(o); |
| 1358 | PyTuple_SET_ITEM(key, 1, new); |
| 1359 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1360 | |
| 1361 | return key; |
| 1362 | } |
| 1363 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1364 | static Py_ssize_t |
| 1365 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1366 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1367 | PyObject *key = merge_consts_recursive(c, o); |
| 1368 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1369 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1370 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1371 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1372 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1373 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1378 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1379 | { |
| 1380 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1381 | if (arg < 0) |
| 1382 | return 0; |
| 1383 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1384 | } |
| 1385 | |
| 1386 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1387 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1389 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1390 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1391 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1392 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1393 | return compiler_addop_i(c, opcode, arg); |
| 1394 | } |
| 1395 | |
| 1396 | static int |
| 1397 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1399 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1400 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1401 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1402 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1403 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1404 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1405 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | Py_DECREF(mangled); |
| 1407 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1408 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1409 | return compiler_addop_i(c, opcode, arg); |
| 1410 | } |
| 1411 | |
| 1412 | /* Add an opcode with an integer argument. |
| 1413 | Returns 0 on failure, 1 on success. |
| 1414 | */ |
| 1415 | |
| 1416 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1417 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | struct instr *i; |
| 1420 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1421 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1422 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1423 | it in the C code (like Python/ceval.c). |
| 1424 | |
| 1425 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1426 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1427 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1428 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1429 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1430 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1431 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1432 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | if (off < 0) |
| 1434 | return 0; |
| 1435 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1436 | i->i_opcode = opcode; |
| 1437 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1438 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1442 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1443 | { |
| 1444 | assert(HAS_ARG(opcode)); |
| 1445 | assert(b != NULL); |
| 1446 | assert(target != NULL); |
| 1447 | |
| 1448 | int off = compiler_next_instr(b); |
| 1449 | struct instr *i = &b->b_instr[off]; |
| 1450 | if (off < 0) { |
| 1451 | return 0; |
| 1452 | } |
| 1453 | i->i_opcode = opcode; |
| 1454 | i->i_target = target; |
| 1455 | i->i_lineno = lineno; |
| 1456 | return 1; |
| 1457 | } |
| 1458 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1459 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1460 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1461 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1462 | 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] | 1463 | } |
| 1464 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1465 | static int |
| 1466 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1467 | { |
| 1468 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1469 | } |
| 1470 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1471 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1472 | to the new block. |
| 1473 | |
| 1474 | The returns inside this macro make it impossible to decref objects |
| 1475 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1476 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1477 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | if (compiler_next_block((C)) == NULL) \ |
| 1479 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | if (!compiler_addop((C), (OP))) \ |
| 1484 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1487 | #define ADDOP_NOLINE(C, OP) { \ |
| 1488 | if (!compiler_addop_noline((C), (OP))) \ |
| 1489 | return 0; \ |
| 1490 | } |
| 1491 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1492 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | if (!compiler_addop((C), (OP))) { \ |
| 1494 | compiler_exit_scope(c); \ |
| 1495 | return 0; \ |
| 1496 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1499 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1500 | if (!compiler_addop_load_const((C), (O))) \ |
| 1501 | return 0; \ |
| 1502 | } |
| 1503 | |
| 1504 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1505 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1506 | PyObject *__new_const = (O); \ |
| 1507 | if (__new_const == NULL) { \ |
| 1508 | return 0; \ |
| 1509 | } \ |
| 1510 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1511 | Py_DECREF(__new_const); \ |
| 1512 | return 0; \ |
| 1513 | } \ |
| 1514 | Py_DECREF(__new_const); \ |
| 1515 | } |
| 1516 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1517 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1519 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1522 | /* Same as ADDOP_O, but steals a reference. */ |
| 1523 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1524 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1525 | Py_DECREF((O)); \ |
| 1526 | return 0; \ |
| 1527 | } \ |
| 1528 | Py_DECREF((O)); \ |
| 1529 | } |
| 1530 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1531 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1533 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1538 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1541 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1542 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1546 | /* Add a jump with no line number. |
| 1547 | * Used for artificial jumps that have no corresponding |
| 1548 | * token in the source code. */ |
| 1549 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1550 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1551 | return 0; \ |
| 1552 | } |
| 1553 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1554 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1555 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1556 | return 0; \ |
| 1557 | } |
| 1558 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1559 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1560 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1561 | */ |
| 1562 | |
| 1563 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1565 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1568 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1570 | compiler_exit_scope(c); \ |
| 1571 | return 0; \ |
| 1572 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1575 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1577 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1582 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1584 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1585 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1586 | return 0; \ |
| 1587 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1590 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1592 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1594 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1595 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1596 | compiler_exit_scope(c); \ |
| 1597 | return 0; \ |
| 1598 | } \ |
| 1599 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1602 | #define RETURN_IF_FALSE(X) \ |
| 1603 | if (!(X)) { \ |
| 1604 | return 0; \ |
| 1605 | } |
| 1606 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1607 | /* Search if variable annotations are present statically in a block. */ |
| 1608 | |
| 1609 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1610 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1611 | { |
| 1612 | int i, j, res = 0; |
| 1613 | stmt_ty st; |
| 1614 | |
| 1615 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1616 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1617 | switch (st->kind) { |
| 1618 | case AnnAssign_kind: |
| 1619 | return 1; |
| 1620 | case For_kind: |
| 1621 | res = find_ann(st->v.For.body) || |
| 1622 | find_ann(st->v.For.orelse); |
| 1623 | break; |
| 1624 | case AsyncFor_kind: |
| 1625 | res = find_ann(st->v.AsyncFor.body) || |
| 1626 | find_ann(st->v.AsyncFor.orelse); |
| 1627 | break; |
| 1628 | case While_kind: |
| 1629 | res = find_ann(st->v.While.body) || |
| 1630 | find_ann(st->v.While.orelse); |
| 1631 | break; |
| 1632 | case If_kind: |
| 1633 | res = find_ann(st->v.If.body) || |
| 1634 | find_ann(st->v.If.orelse); |
| 1635 | break; |
| 1636 | case With_kind: |
| 1637 | res = find_ann(st->v.With.body); |
| 1638 | break; |
| 1639 | case AsyncWith_kind: |
| 1640 | res = find_ann(st->v.AsyncWith.body); |
| 1641 | break; |
| 1642 | case Try_kind: |
| 1643 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1644 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1645 | st->v.Try.handlers, j); |
| 1646 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1647 | return 1; |
| 1648 | } |
| 1649 | } |
| 1650 | res = find_ann(st->v.Try.body) || |
| 1651 | find_ann(st->v.Try.finalbody) || |
| 1652 | find_ann(st->v.Try.orelse); |
| 1653 | break; |
| 1654 | default: |
| 1655 | res = 0; |
| 1656 | } |
| 1657 | if (res) { |
| 1658 | break; |
| 1659 | } |
| 1660 | } |
| 1661 | return res; |
| 1662 | } |
| 1663 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1664 | /* |
| 1665 | * Frame block handling functions |
| 1666 | */ |
| 1667 | |
| 1668 | static int |
| 1669 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1670 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1671 | { |
| 1672 | struct fblockinfo *f; |
| 1673 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1674 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1675 | } |
| 1676 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1677 | f->fb_type = t; |
| 1678 | f->fb_block = b; |
| 1679 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1680 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1681 | return 1; |
| 1682 | } |
| 1683 | |
| 1684 | static void |
| 1685 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1686 | { |
| 1687 | struct compiler_unit *u = c->u; |
| 1688 | assert(u->u_nfblocks > 0); |
| 1689 | u->u_nfblocks--; |
| 1690 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1691 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1692 | } |
| 1693 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1694 | static int |
| 1695 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1696 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1697 | ADDOP(c, DUP_TOP); |
| 1698 | ADDOP(c, DUP_TOP); |
| 1699 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1700 | return 1; |
| 1701 | } |
| 1702 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1703 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1704 | * popping the blocks will be restored afterwards, unless another |
| 1705 | * return, break or continue is found. In which case, the TOS will |
| 1706 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1707 | */ |
| 1708 | static int |
| 1709 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1710 | int preserve_tos) |
| 1711 | { |
| 1712 | switch (info->fb_type) { |
| 1713 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1714 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1715 | return 1; |
| 1716 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1717 | case FOR_LOOP: |
| 1718 | /* Pop the iterator */ |
| 1719 | if (preserve_tos) { |
| 1720 | ADDOP(c, ROT_TWO); |
| 1721 | } |
| 1722 | ADDOP(c, POP_TOP); |
| 1723 | return 1; |
| 1724 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1725 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1726 | ADDOP(c, POP_BLOCK); |
| 1727 | return 1; |
| 1728 | |
| 1729 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1730 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1731 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1732 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1733 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1734 | return 0; |
| 1735 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1736 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1737 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1738 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1739 | if (preserve_tos) { |
| 1740 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1741 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1742 | /* The finally block should appear to execute after the |
| 1743 | * statement causing the unwinding, so make the unwinding |
| 1744 | * instruction artificial */ |
| 1745 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1746 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1747 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1748 | case FINALLY_END: |
| 1749 | if (preserve_tos) { |
| 1750 | ADDOP(c, ROT_FOUR); |
| 1751 | } |
| 1752 | ADDOP(c, POP_TOP); |
| 1753 | ADDOP(c, POP_TOP); |
| 1754 | ADDOP(c, POP_TOP); |
| 1755 | if (preserve_tos) { |
| 1756 | ADDOP(c, ROT_FOUR); |
| 1757 | } |
| 1758 | ADDOP(c, POP_EXCEPT); |
| 1759 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1760 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1761 | case WITH: |
| 1762 | case ASYNC_WITH: |
| 1763 | ADDOP(c, POP_BLOCK); |
| 1764 | if (preserve_tos) { |
| 1765 | ADDOP(c, ROT_TWO); |
| 1766 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1767 | if(!compiler_call_exit_with_nones(c)) { |
| 1768 | return 0; |
| 1769 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1770 | if (info->fb_type == ASYNC_WITH) { |
| 1771 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1772 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1773 | ADDOP(c, YIELD_FROM); |
| 1774 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1775 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1776 | return 1; |
| 1777 | |
| 1778 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1779 | if (info->fb_datum) { |
| 1780 | ADDOP(c, POP_BLOCK); |
| 1781 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1782 | if (preserve_tos) { |
| 1783 | ADDOP(c, ROT_FOUR); |
| 1784 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1785 | ADDOP(c, POP_EXCEPT); |
| 1786 | if (info->fb_datum) { |
| 1787 | ADDOP_LOAD_CONST(c, Py_None); |
| 1788 | compiler_nameop(c, info->fb_datum, Store); |
| 1789 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1790 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1791 | return 1; |
| 1792 | |
| 1793 | case POP_VALUE: |
| 1794 | if (preserve_tos) { |
| 1795 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1796 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1797 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1798 | return 1; |
| 1799 | } |
| 1800 | Py_UNREACHABLE(); |
| 1801 | } |
| 1802 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1803 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1804 | static int |
| 1805 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1806 | if (c->u->u_nfblocks == 0) { |
| 1807 | return 1; |
| 1808 | } |
| 1809 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1810 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1811 | *loop = top; |
| 1812 | return 1; |
| 1813 | } |
| 1814 | struct fblockinfo copy = *top; |
| 1815 | c->u->u_nfblocks--; |
| 1816 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1817 | return 0; |
| 1818 | } |
| 1819 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1820 | return 0; |
| 1821 | } |
| 1822 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1823 | c->u->u_nfblocks++; |
| 1824 | return 1; |
| 1825 | } |
| 1826 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1827 | /* Compile a sequence of statements, checking for a docstring |
| 1828 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1829 | |
| 1830 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1831 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1832 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1833 | int i = 0; |
| 1834 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1835 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1836 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1837 | /* Set current line number to the line number of first statement. |
| 1838 | This way line number for SETUP_ANNOTATIONS will always |
| 1839 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1840 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1841 | 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] | 1842 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1843 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1844 | } |
| 1845 | /* Every annotated class and module should have __annotations__. */ |
| 1846 | if (find_ann(stmts)) { |
| 1847 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1848 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1849 | if (!asdl_seq_LEN(stmts)) |
| 1850 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1851 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1852 | if (c->c_optimize < 2) { |
| 1853 | docstring = _PyAST_GetDocString(stmts); |
| 1854 | if (docstring) { |
| 1855 | i = 1; |
| 1856 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1857 | assert(st->kind == Expr_kind); |
| 1858 | VISIT(c, expr, st->v.Expr.value); |
| 1859 | if (!compiler_nameop(c, __doc__, Store)) |
| 1860 | return 0; |
| 1861 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1863 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1864 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | static PyCodeObject * |
| 1869 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | PyCodeObject *co; |
| 1872 | int addNone = 1; |
| 1873 | static PyObject *module; |
| 1874 | if (!module) { |
| 1875 | module = PyUnicode_InternFromString("<module>"); |
| 1876 | if (!module) |
| 1877 | return NULL; |
| 1878 | } |
| 1879 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1880 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | return NULL; |
| 1882 | switch (mod->kind) { |
| 1883 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1884 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | compiler_exit_scope(c); |
| 1886 | return 0; |
| 1887 | } |
| 1888 | break; |
| 1889 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1890 | if (find_ann(mod->v.Interactive.body)) { |
| 1891 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1892 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1894 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | break; |
| 1896 | case Expression_kind: |
| 1897 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1898 | addNone = 0; |
| 1899 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | default: |
| 1901 | PyErr_Format(PyExc_SystemError, |
| 1902 | "module kind %d should not be possible", |
| 1903 | mod->kind); |
| 1904 | return 0; |
| 1905 | } |
| 1906 | co = assemble(c, addNone); |
| 1907 | compiler_exit_scope(c); |
| 1908 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1911 | /* The test for LOCAL must come before the test for FREE in order to |
| 1912 | handle classes where name is both local and free. The local var is |
| 1913 | 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] | 1914 | */ |
| 1915 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1916 | static int |
| 1917 | get_ref_type(struct compiler *c, PyObject *name) |
| 1918 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1919 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1920 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1921 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1922 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1923 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1925 | PyErr_Format(PyExc_SystemError, |
| 1926 | "PyST_GetScope(name=%R) failed: " |
| 1927 | "unknown scope in unit %S (%R); " |
| 1928 | "symbols: %R; locals: %R; globals: %R", |
| 1929 | name, |
| 1930 | c->u->u_name, c->u->u_ste->ste_id, |
| 1931 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1932 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1933 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | static int |
| 1938 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1939 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1940 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1941 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1942 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1943 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1944 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1948 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1949 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1950 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1951 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1952 | if (qualname == NULL) |
| 1953 | qualname = co->co_name; |
| 1954 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1955 | if (free) { |
| 1956 | for (i = 0; i < free; ++i) { |
| 1957 | /* Bypass com_addop_varname because it will generate |
| 1958 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1959 | */ |
| 1960 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1961 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1962 | /* Special case: If a class contains a method with a |
| 1963 | free variable that has the same name as a method, |
| 1964 | the name will be considered free *and* local in the |
| 1965 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1966 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1967 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1968 | int reftype = get_ref_type(c, name); |
| 1969 | if (reftype == -1) { |
| 1970 | return 0; |
| 1971 | } |
| 1972 | int arg; |
| 1973 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1974 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1975 | } |
| 1976 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1977 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1978 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1979 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1980 | PyErr_Format(PyExc_SystemError, |
| 1981 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 1982 | "freevars of code %S: %R", |
| 1983 | name, |
| 1984 | reftype, |
| 1985 | c->u->u_name, |
| 1986 | co->co_name, |
| 1987 | co->co_freevars); |
| 1988 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1989 | } |
| 1990 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1992 | flags |= 0x08; |
| 1993 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1995 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1996 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1997 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2002 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | if (!decos) |
| 2007 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2010 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 2011 | } |
| 2012 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2016 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 2017 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2018 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2019 | /* Push a dict of keyword-only default values. |
| 2020 | |
| 2021 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 2022 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2023 | int i; |
| 2024 | PyObject *keys = NULL; |
| 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2027 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2028 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2029 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2030 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2031 | if (!mangled) { |
| 2032 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2034 | if (keys == NULL) { |
| 2035 | keys = PyList_New(1); |
| 2036 | if (keys == NULL) { |
| 2037 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2038 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2039 | } |
| 2040 | PyList_SET_ITEM(keys, 0, mangled); |
| 2041 | } |
| 2042 | else { |
| 2043 | int res = PyList_Append(keys, mangled); |
| 2044 | Py_DECREF(mangled); |
| 2045 | if (res == -1) { |
| 2046 | goto error; |
| 2047 | } |
| 2048 | } |
| 2049 | if (!compiler_visit_expr(c, default_)) { |
| 2050 | goto error; |
| 2051 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | } |
| 2053 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2054 | if (keys != NULL) { |
| 2055 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2056 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2057 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2058 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2059 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2060 | assert(default_count > 0); |
| 2061 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2062 | } |
| 2063 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2064 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | error: |
| 2068 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2069 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2073 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2074 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2075 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2076 | return 1; |
| 2077 | } |
| 2078 | |
| 2079 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2080 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2081 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2084 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2085 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2086 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2087 | |
| 2088 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2089 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2090 | VISIT(c, annexpr, annotation); |
| 2091 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2093 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2097 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2098 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2099 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2100 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2102 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2103 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | c, |
| 2105 | arg->arg, |
| 2106 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2107 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2108 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2110 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | static int |
| 2114 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2115 | expr_ty returns) |
| 2116 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2117 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2118 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2119 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2120 | 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] | 2121 | */ |
| 2122 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2123 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2124 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2125 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2126 | return 0; |
| 2127 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2128 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2129 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2130 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2131 | args->vararg->annotation, &annotations_len)) |
| 2132 | return 0; |
| 2133 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2134 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2135 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2136 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2137 | args->kwarg->annotation, &annotations_len)) |
| 2138 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | if (!return_str) { |
| 2141 | return_str = PyUnicode_InternFromString("return"); |
| 2142 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2143 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2145 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2146 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2149 | if (annotations_len) { |
| 2150 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2151 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2153 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2154 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2158 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2159 | { |
| 2160 | VISIT_SEQ(c, expr, args->defaults); |
| 2161 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2162 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2165 | static Py_ssize_t |
| 2166 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2167 | { |
| 2168 | Py_ssize_t funcflags = 0; |
| 2169 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2170 | if (!compiler_visit_defaults(c, args)) |
| 2171 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2172 | funcflags |= 0x01; |
| 2173 | } |
| 2174 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2175 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2176 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2177 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2178 | return -1; |
| 2179 | } |
| 2180 | else if (res > 0) { |
| 2181 | funcflags |= 0x02; |
| 2182 | } |
| 2183 | } |
| 2184 | return funcflags; |
| 2185 | } |
| 2186 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2187 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2188 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2189 | { |
| 2190 | |
| 2191 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2192 | compiler_error(c, "cannot assign to __debug__"); |
| 2193 | return 1; |
| 2194 | } |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
| 2198 | static int |
| 2199 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2200 | { |
| 2201 | if (arg != NULL) { |
| 2202 | if (forbidden_name(c, arg->arg, Store)) |
| 2203 | return 0; |
| 2204 | } |
| 2205 | return 1; |
| 2206 | } |
| 2207 | |
| 2208 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2209 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2210 | { |
| 2211 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2212 | 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] | 2213 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2214 | return 0; |
| 2215 | } |
| 2216 | } |
| 2217 | return 1; |
| 2218 | } |
| 2219 | |
| 2220 | static int |
| 2221 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2222 | { |
| 2223 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2224 | return 0; |
| 2225 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2226 | return 0; |
| 2227 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2228 | return 0; |
| 2229 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2230 | return 0; |
| 2231 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2232 | return 0; |
| 2233 | return 1; |
| 2234 | } |
| 2235 | |
| 2236 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2237 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2240 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2241 | arguments_ty args; |
| 2242 | expr_ty returns; |
| 2243 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2244 | asdl_expr_seq* decos; |
| 2245 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2246 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2247 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2248 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2249 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2250 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2251 | if (is_async) { |
| 2252 | assert(s->kind == AsyncFunctionDef_kind); |
| 2253 | |
| 2254 | args = s->v.AsyncFunctionDef.args; |
| 2255 | returns = s->v.AsyncFunctionDef.returns; |
| 2256 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2257 | name = s->v.AsyncFunctionDef.name; |
| 2258 | body = s->v.AsyncFunctionDef.body; |
| 2259 | |
| 2260 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2261 | } else { |
| 2262 | assert(s->kind == FunctionDef_kind); |
| 2263 | |
| 2264 | args = s->v.FunctionDef.args; |
| 2265 | returns = s->v.FunctionDef.returns; |
| 2266 | decos = s->v.FunctionDef.decorator_list; |
| 2267 | name = s->v.FunctionDef.name; |
| 2268 | body = s->v.FunctionDef.body; |
| 2269 | |
| 2270 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2271 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2272 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2273 | if (!compiler_check_debug_args(c, args)) |
| 2274 | return 0; |
| 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | if (!compiler_decorators(c, decos)) |
| 2277 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2278 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2279 | firstlineno = s->lineno; |
| 2280 | if (asdl_seq_LEN(decos)) { |
| 2281 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2282 | } |
| 2283 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2284 | funcflags = compiler_default_arguments(c, args); |
| 2285 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2287 | } |
| 2288 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2289 | annotations = compiler_visit_annotations(c, args, returns); |
| 2290 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2291 | return 0; |
| 2292 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2293 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2294 | funcflags |= 0x04; |
| 2295 | } |
| 2296 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2297 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2298 | return 0; |
| 2299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2301 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2302 | if (c->c_optimize < 2) { |
| 2303 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2304 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2305 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | compiler_exit_scope(c); |
| 2307 | return 0; |
| 2308 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2311 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2313 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2314 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2315 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2317 | qualname = c->u->u_qualname; |
| 2318 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2320 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2321 | Py_XDECREF(qualname); |
| 2322 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2323 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2324 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2325 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2326 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2327 | Py_DECREF(qualname); |
| 2328 | Py_DECREF(co); |
| 2329 | return 0; |
| 2330 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2331 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | /* decorators */ |
| 2335 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2336 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2337 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2338 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2339 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | static int |
| 2343 | compiler_class(struct compiler *c, stmt_ty s) |
| 2344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | PyCodeObject *co; |
| 2346 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2347 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2348 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | if (!compiler_decorators(c, decos)) |
| 2351 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2352 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2353 | firstlineno = s->lineno; |
| 2354 | if (asdl_seq_LEN(decos)) { |
| 2355 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2356 | } |
| 2357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | /* ultimately generate code for: |
| 2359 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2360 | where: |
| 2361 | <func> is a function/closure created from the class body; |
| 2362 | it has a single argument (__locals__) where the dict |
| 2363 | (or MutableSequence) representing the locals is passed |
| 2364 | <name> is the class name |
| 2365 | <bases> is the positional arguments and *varargs argument |
| 2366 | <keywords> is the keyword arguments and **kwds argument |
| 2367 | This borrows from compiler_call. |
| 2368 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2371 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2372 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | return 0; |
| 2374 | /* this block represents what we do in the new scope */ |
| 2375 | { |
| 2376 | /* use the class name for name mangling */ |
| 2377 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2378 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | /* load (global) __name__ ... */ |
| 2380 | str = PyUnicode_InternFromString("__name__"); |
| 2381 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2382 | Py_XDECREF(str); |
| 2383 | compiler_exit_scope(c); |
| 2384 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2385 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | Py_DECREF(str); |
| 2387 | /* ... and store it as __module__ */ |
| 2388 | str = PyUnicode_InternFromString("__module__"); |
| 2389 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2390 | Py_XDECREF(str); |
| 2391 | compiler_exit_scope(c); |
| 2392 | return 0; |
| 2393 | } |
| 2394 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2395 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2396 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2397 | str = PyUnicode_InternFromString("__qualname__"); |
| 2398 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2399 | Py_XDECREF(str); |
| 2400 | compiler_exit_scope(c); |
| 2401 | return 0; |
| 2402 | } |
| 2403 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2405 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | compiler_exit_scope(c); |
| 2407 | return 0; |
| 2408 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2409 | /* The following code is artificial */ |
| 2410 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2411 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2412 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2413 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2414 | str = PyUnicode_InternFromString("__class__"); |
| 2415 | if (str == NULL) { |
| 2416 | compiler_exit_scope(c); |
| 2417 | return 0; |
| 2418 | } |
| 2419 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2420 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2421 | if (i < 0) { |
| 2422 | compiler_exit_scope(c); |
| 2423 | return 0; |
| 2424 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2425 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2428 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2429 | str = PyUnicode_InternFromString("__classcell__"); |
| 2430 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2431 | Py_XDECREF(str); |
| 2432 | compiler_exit_scope(c); |
| 2433 | return 0; |
| 2434 | } |
| 2435 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2437 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2438 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2439 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2440 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2441 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2442 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2443 | /* create the code object */ |
| 2444 | co = assemble(c, 1); |
| 2445 | } |
| 2446 | /* leave the new scope */ |
| 2447 | compiler_exit_scope(c); |
| 2448 | if (co == NULL) |
| 2449 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | /* 2. load the 'build_class' function */ |
| 2452 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2453 | |
| 2454 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2455 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2456 | Py_DECREF(co); |
| 2457 | return 0; |
| 2458 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | Py_DECREF(co); |
| 2460 | |
| 2461 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2462 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | |
| 2464 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2465 | 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] | 2466 | return 0; |
| 2467 | |
| 2468 | /* 6. apply decorators */ |
| 2469 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2470 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2471 | } |
| 2472 | |
| 2473 | /* 7. store into <name> */ |
| 2474 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2475 | return 0; |
| 2476 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2479 | /* Return 0 if the expression is a constant value except named singletons. |
| 2480 | Return 1 otherwise. */ |
| 2481 | static int |
| 2482 | check_is_arg(expr_ty e) |
| 2483 | { |
| 2484 | if (e->kind != Constant_kind) { |
| 2485 | return 1; |
| 2486 | } |
| 2487 | PyObject *value = e->v.Constant.value; |
| 2488 | return (value == Py_None |
| 2489 | || value == Py_False |
| 2490 | || value == Py_True |
| 2491 | || value == Py_Ellipsis); |
| 2492 | } |
| 2493 | |
| 2494 | /* Check operands of identity chacks ("is" and "is not"). |
| 2495 | Emit a warning if any operand is a constant except named singletons. |
| 2496 | Return 0 on error. |
| 2497 | */ |
| 2498 | static int |
| 2499 | check_compare(struct compiler *c, expr_ty e) |
| 2500 | { |
| 2501 | Py_ssize_t i, n; |
| 2502 | int left = check_is_arg(e->v.Compare.left); |
| 2503 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2504 | for (i = 0; i < n; i++) { |
| 2505 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2506 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2507 | if (op == Is || op == IsNot) { |
| 2508 | if (!right || !left) { |
| 2509 | const char *msg = (op == Is) |
| 2510 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2511 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2512 | return compiler_warn(c, msg); |
| 2513 | } |
| 2514 | } |
| 2515 | left = right; |
| 2516 | } |
| 2517 | return 1; |
| 2518 | } |
| 2519 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2520 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2521 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2522 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2523 | switch (op) { |
| 2524 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2525 | cmp = Py_EQ; |
| 2526 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2527 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2528 | cmp = Py_NE; |
| 2529 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2530 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2531 | cmp = Py_LT; |
| 2532 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2533 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2534 | cmp = Py_LE; |
| 2535 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2536 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2537 | cmp = Py_GT; |
| 2538 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2539 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2540 | cmp = Py_GE; |
| 2541 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2542 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2543 | ADDOP_I(c, IS_OP, 0); |
| 2544 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2545 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2546 | ADDOP_I(c, IS_OP, 1); |
| 2547 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2548 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2549 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2550 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2551 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2552 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2553 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2554 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2555 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2556 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2557 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2558 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2559 | } |
| 2560 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2561 | |
| 2562 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2563 | static int |
| 2564 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2565 | { |
| 2566 | switch (e->kind) { |
| 2567 | case UnaryOp_kind: |
| 2568 | if (e->v.UnaryOp.op == Not) |
| 2569 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2570 | /* fallback to general implementation */ |
| 2571 | break; |
| 2572 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2573 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2574 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2575 | assert(n >= 0); |
| 2576 | int cond2 = e->v.BoolOp.op == Or; |
| 2577 | basicblock *next2 = next; |
| 2578 | if (!cond2 != !cond) { |
| 2579 | next2 = compiler_new_block(c); |
| 2580 | if (next2 == NULL) |
| 2581 | return 0; |
| 2582 | } |
| 2583 | for (i = 0; i < n; ++i) { |
| 2584 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2585 | return 0; |
| 2586 | } |
| 2587 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2588 | return 0; |
| 2589 | if (next2 != next) |
| 2590 | compiler_use_next_block(c, next2); |
| 2591 | return 1; |
| 2592 | } |
| 2593 | case IfExp_kind: { |
| 2594 | basicblock *end, *next2; |
| 2595 | end = compiler_new_block(c); |
| 2596 | if (end == NULL) |
| 2597 | return 0; |
| 2598 | next2 = compiler_new_block(c); |
| 2599 | if (next2 == NULL) |
| 2600 | return 0; |
| 2601 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2602 | return 0; |
| 2603 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2604 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2605 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2606 | compiler_use_next_block(c, next2); |
| 2607 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2608 | return 0; |
| 2609 | compiler_use_next_block(c, end); |
| 2610 | return 1; |
| 2611 | } |
| 2612 | case Compare_kind: { |
| 2613 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2614 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2615 | if (!check_compare(c, e)) { |
| 2616 | return 0; |
| 2617 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2618 | basicblock *cleanup = compiler_new_block(c); |
| 2619 | if (cleanup == NULL) |
| 2620 | return 0; |
| 2621 | VISIT(c, expr, e->v.Compare.left); |
| 2622 | for (i = 0; i < n; i++) { |
| 2623 | VISIT(c, expr, |
| 2624 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2625 | ADDOP(c, DUP_TOP); |
| 2626 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2627 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2628 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2629 | NEXT_BLOCK(c); |
| 2630 | } |
| 2631 | 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] | 2632 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2633 | 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] | 2634 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2635 | basicblock *end = compiler_new_block(c); |
| 2636 | if (end == NULL) |
| 2637 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2638 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2639 | compiler_use_next_block(c, cleanup); |
| 2640 | ADDOP(c, POP_TOP); |
| 2641 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2642 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2643 | } |
| 2644 | compiler_use_next_block(c, end); |
| 2645 | return 1; |
| 2646 | } |
| 2647 | /* fallback to general implementation */ |
| 2648 | break; |
| 2649 | } |
| 2650 | default: |
| 2651 | /* fallback to general implementation */ |
| 2652 | break; |
| 2653 | } |
| 2654 | |
| 2655 | /* general implementation */ |
| 2656 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2657 | 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] | 2658 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2659 | return 1; |
| 2660 | } |
| 2661 | |
| 2662 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2663 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2664 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | basicblock *end, *next; |
| 2666 | |
| 2667 | assert(e->kind == IfExp_kind); |
| 2668 | end = compiler_new_block(c); |
| 2669 | if (end == NULL) |
| 2670 | return 0; |
| 2671 | next = compiler_new_block(c); |
| 2672 | if (next == NULL) |
| 2673 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2674 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2675 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2677 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | compiler_use_next_block(c, next); |
| 2679 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2680 | compiler_use_next_block(c, end); |
| 2681 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2685 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2688 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2690 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | arguments_ty args = e->v.Lambda.args; |
| 2692 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2693 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2694 | if (!compiler_check_debug_args(c, args)) |
| 2695 | return 0; |
| 2696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | if (!name) { |
| 2698 | name = PyUnicode_InternFromString("<lambda>"); |
| 2699 | if (!name) |
| 2700 | return 0; |
| 2701 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2702 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2703 | funcflags = compiler_default_arguments(c, args); |
| 2704 | if (funcflags == -1) { |
| 2705 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2707 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2708 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2709 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2710 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2712 | /* Make None the first constant, so the lambda can't have a |
| 2713 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2714 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2718 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2720 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2721 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2722 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | } |
| 2724 | else { |
| 2725 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2726 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2728 | qualname = c->u->u_qualname; |
| 2729 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2731 | if (co == NULL) { |
| 2732 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2734 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2735 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2736 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2737 | Py_DECREF(qualname); |
| 2738 | Py_DECREF(co); |
| 2739 | return 0; |
| 2740 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2741 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | Py_DECREF(co); |
| 2743 | |
| 2744 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2748 | compiler_if(struct compiler *c, stmt_ty s) |
| 2749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | assert(s->kind == If_kind); |
| 2752 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2753 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2755 | } |
| 2756 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2757 | next = compiler_new_block(c); |
| 2758 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2759 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2760 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2761 | } |
| 2762 | else { |
| 2763 | next = end; |
| 2764 | } |
| 2765 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2766 | return 0; |
| 2767 | } |
| 2768 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2769 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2770 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2771 | compiler_use_next_block(c, next); |
| 2772 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | } |
| 2774 | compiler_use_next_block(c, end); |
| 2775 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | static int |
| 2779 | compiler_for(struct compiler *c, stmt_ty s) |
| 2780 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2781 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2784 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | cleanup = compiler_new_block(c); |
| 2786 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2787 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2789 | } |
| 2790 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2792 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | VISIT(c, expr, s->v.For.iter); |
| 2794 | ADDOP(c, GET_ITER); |
| 2795 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2796 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2797 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | VISIT(c, expr, s->v.For.target); |
| 2799 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2800 | /* Mark jump as artificial */ |
| 2801 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2802 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2804 | |
| 2805 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2808 | compiler_use_next_block(c, end); |
| 2809 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2812 | |
| 2813 | static int |
| 2814 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2815 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2816 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2817 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2818 | c->u->u_ste->ste_coroutine = 1; |
| 2819 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2820 | return compiler_error(c, "'async for' outside async function"); |
| 2821 | } |
| 2822 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2823 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2824 | except = compiler_new_block(c); |
| 2825 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2826 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2827 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2828 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2829 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2830 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2831 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2832 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2833 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2834 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2835 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2836 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2837 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2838 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2839 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2840 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2841 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2842 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2843 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2844 | /* Success block for __anext__ */ |
| 2845 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2846 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2847 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2848 | |
| 2849 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2850 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2851 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2852 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2853 | |
| 2854 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2855 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2856 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2857 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2858 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2859 | |
| 2860 | compiler_use_next_block(c, end); |
| 2861 | |
| 2862 | return 1; |
| 2863 | } |
| 2864 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2865 | static int |
| 2866 | compiler_while(struct compiler *c, stmt_ty s) |
| 2867 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2868 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2870 | body = compiler_new_block(c); |
| 2871 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2873 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2877 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2880 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2881 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2886 | SET_LOC(c, s); |
| 2887 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2888 | return 0; |
| 2889 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2891 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2892 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2893 | compiler_use_next_block(c, anchor); |
| 2894 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2896 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2903 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2904 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2905 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2906 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2907 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2908 | return compiler_error(c, "'return' outside function"); |
| 2909 | if (s->v.Return.value != NULL && |
| 2910 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2911 | { |
| 2912 | return compiler_error( |
| 2913 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2915 | if (preserve_tos) { |
| 2916 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2917 | } else { |
| 2918 | /* Emit instruction with line number for expression */ |
| 2919 | if (s->v.Return.value != NULL) { |
| 2920 | SET_LOC(c, s->v.Return.value); |
| 2921 | ADDOP(c, NOP); |
| 2922 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2923 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2924 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2925 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2926 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2927 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2928 | } |
| 2929 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2930 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2931 | } |
| 2932 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2933 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2938 | static int |
| 2939 | compiler_break(struct compiler *c) |
| 2940 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2941 | struct fblockinfo *loop = NULL; |
| 2942 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2943 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2944 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2945 | if (loop == NULL) { |
| 2946 | return compiler_error(c, "'break' outside loop"); |
| 2947 | } |
| 2948 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2949 | return 0; |
| 2950 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2951 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2952 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2953 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | static int |
| 2957 | compiler_continue(struct compiler *c) |
| 2958 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2959 | struct fblockinfo *loop = NULL; |
| 2960 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2961 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2962 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2963 | if (loop == NULL) { |
| 2964 | return compiler_error(c, "'continue' not properly in loop"); |
| 2965 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2966 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2967 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2968 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | |
| 2974 | SETUP_FINALLY L |
| 2975 | <code for body> |
| 2976 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2977 | <code for finalbody> |
| 2978 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2979 | L: |
| 2980 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2981 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | The special instructions use the block stack. Each block |
| 2984 | stack entry contains the instruction that created it (here |
| 2985 | SETUP_FINALLY), the level of the value stack at the time the |
| 2986 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2988 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2989 | Pushes the current value stack level and the label |
| 2990 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2991 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2992 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2994 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2995 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2996 | exceptions are pushed onto the value stack (and the exception |
| 2997 | condition is cleared), and the interpreter jumps to the label |
| 2998 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | */ |
| 3000 | |
| 3001 | static int |
| 3002 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 3003 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3004 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3006 | body = compiler_new_block(c); |
| 3007 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3008 | exit = compiler_new_block(c); |
| 3009 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3011 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3012 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3013 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3014 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3015 | 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] | 3016 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3017 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3018 | if (!compiler_try_except(c, s)) |
| 3019 | return 0; |
| 3020 | } |
| 3021 | else { |
| 3022 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3023 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3024 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3025 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3026 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3027 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3028 | /* `finally` block */ |
| 3029 | compiler_use_next_block(c, end); |
| 3030 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3031 | return 0; |
| 3032 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3033 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3034 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3035 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3040 | 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] | 3041 | (The contents of the value stack is shown in [], with the top |
| 3042 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3043 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3044 | |
| 3045 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3046 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | [] <code for S> |
| 3048 | [] POP_BLOCK |
| 3049 | [] JUMP_FORWARD L0 |
| 3050 | |
| 3051 | [tb, val, exc] L1: DUP ) |
| 3052 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3053 | [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] | 3054 | [tb, val, exc] POP |
| 3055 | [tb, val] <assign to V1> (or POP if no V1) |
| 3056 | [tb] POP |
| 3057 | [] <code for S1> |
| 3058 | JUMP_FORWARD L0 |
| 3059 | |
| 3060 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3061 | .............................etc....................... |
| 3062 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3063 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | |
| 3065 | [] L0: <next statement> |
| 3066 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3067 | Of course, parts are not generated if Vi or Ei is not present. |
| 3068 | */ |
| 3069 | static int |
| 3070 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3073 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | body = compiler_new_block(c); |
| 3076 | except = compiler_new_block(c); |
| 3077 | orelse = compiler_new_block(c); |
| 3078 | end = compiler_new_block(c); |
| 3079 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3080 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3081 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3083 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3085 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3086 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3087 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3088 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3089 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3091 | /* Runtime will push a block here, so we need to account for that */ |
| 3092 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3093 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | for (i = 0; i < n; i++) { |
| 3095 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3096 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3098 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3099 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | except = compiler_new_block(c); |
| 3101 | if (except == NULL) |
| 3102 | return 0; |
| 3103 | if (handler->v.ExceptHandler.type) { |
| 3104 | ADDOP(c, DUP_TOP); |
| 3105 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3106 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3107 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | } |
| 3109 | ADDOP(c, POP_TOP); |
| 3110 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3111 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3112 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3113 | cleanup_end = compiler_new_block(c); |
| 3114 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3115 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3116 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3117 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3118 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3119 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3120 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3122 | /* |
| 3123 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3124 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3125 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3126 | try: |
| 3127 | # body |
| 3128 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3129 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3130 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3131 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3133 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3134 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3135 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3136 | 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] | 3137 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3139 | /* second # body */ |
| 3140 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3141 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3142 | ADDOP(c, POP_BLOCK); |
| 3143 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3144 | /* name = None; del name; # Mark as artificial */ |
| 3145 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3146 | ADDOP_LOAD_CONST(c, Py_None); |
| 3147 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3148 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3149 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3151 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3152 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3154 | /* name = None; del name; # Mark as artificial */ |
| 3155 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3156 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3157 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3158 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3159 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3160 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | } |
| 3162 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3163 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3165 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3166 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3167 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3169 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3170 | ADDOP(c, POP_TOP); |
| 3171 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3172 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3173 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3175 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3176 | /* name = None; del name; # Mark as artificial */ |
| 3177 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3178 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3179 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | compiler_use_next_block(c, except); |
| 3182 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3183 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3184 | /* Mark as artificial */ |
| 3185 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3186 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3188 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | compiler_use_next_block(c, end); |
| 3190 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3194 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3195 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3196 | return compiler_try_finally(c, s); |
| 3197 | else |
| 3198 | return compiler_try_except(c, s); |
| 3199 | } |
| 3200 | |
| 3201 | |
| 3202 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3203 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | /* The IMPORT_NAME opcode was already generated. This function |
| 3206 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | 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] | 3209 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3211 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3212 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3213 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3214 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3215 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3217 | while (1) { |
| 3218 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3220 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3221 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3222 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3223 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3225 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3226 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3227 | if (dot == -1) { |
| 3228 | break; |
| 3229 | } |
| 3230 | ADDOP(c, ROT_TWO); |
| 3231 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3233 | if (!compiler_nameop(c, asname, Store)) { |
| 3234 | return 0; |
| 3235 | } |
| 3236 | ADDOP(c, POP_TOP); |
| 3237 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | } |
| 3239 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | static int |
| 3243 | compiler_import(struct compiler *c, stmt_ty s) |
| 3244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | /* The Import node stores a module name like a.b.c as a single |
| 3246 | string. This is convenient for all cases except |
| 3247 | import a.b.c as d |
| 3248 | where we need to parse that string to extract the individual |
| 3249 | module names. |
| 3250 | XXX Perhaps change the representation to make this case simpler? |
| 3251 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3252 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3253 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3254 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | for (i = 0; i < n; i++) { |
| 3256 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3257 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3258 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3259 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3260 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | if (alias->asname) { |
| 3264 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3265 | if (!r) |
| 3266 | return r; |
| 3267 | } |
| 3268 | else { |
| 3269 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3270 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3271 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3272 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3273 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3274 | if (tmp == NULL) |
| 3275 | return 0; |
| 3276 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3278 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3279 | Py_DECREF(tmp); |
| 3280 | } |
| 3281 | if (!r) |
| 3282 | return r; |
| 3283 | } |
| 3284 | } |
| 3285 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | static int |
| 3289 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3290 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3291 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3292 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | if (!empty_string) { |
| 3296 | empty_string = PyUnicode_FromString(""); |
| 3297 | if (!empty_string) |
| 3298 | return 0; |
| 3299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3300 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3301 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3302 | |
| 3303 | names = PyTuple_New(n); |
| 3304 | if (!names) |
| 3305 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3307 | /* build up the names */ |
| 3308 | for (i = 0; i < n; i++) { |
| 3309 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3310 | Py_INCREF(alias->name); |
| 3311 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3312 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3315 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | Py_DECREF(names); |
| 3317 | return compiler_error(c, "from __future__ imports must occur " |
| 3318 | "at the beginning of the file"); |
| 3319 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3320 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | if (s->v.ImportFrom.module) { |
| 3323 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3324 | } |
| 3325 | else { |
| 3326 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3327 | } |
| 3328 | for (i = 0; i < n; i++) { |
| 3329 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3330 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3331 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3332 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | assert(n == 1); |
| 3334 | ADDOP(c, IMPORT_STAR); |
| 3335 | return 1; |
| 3336 | } |
| 3337 | |
| 3338 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3339 | store_name = alias->name; |
| 3340 | if (alias->asname) |
| 3341 | store_name = alias->asname; |
| 3342 | |
| 3343 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | return 0; |
| 3345 | } |
| 3346 | } |
| 3347 | /* remove imported module */ |
| 3348 | ADDOP(c, POP_TOP); |
| 3349 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | static int |
| 3353 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3356 | |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3357 | /* Always emit a warning if the test is a non-zero length tuple */ |
| 3358 | if ((s->v.Assert.test->kind == Tuple_kind && |
| 3359 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || |
| 3360 | (s->v.Assert.test->kind == Constant_kind && |
| 3361 | PyTuple_Check(s->v.Assert.test->v.Constant.value) && |
| 3362 | PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3363 | { |
| 3364 | if (!compiler_warn(c, "assertion is always true, " |
| 3365 | "perhaps remove parentheses?")) |
| 3366 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3367 | return 0; |
| 3368 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | } |
tsukasa-au | a8ef457 | 2021-03-16 22:14:41 +1100 | [diff] [blame] | 3370 | if (c->c_optimize) |
| 3371 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | end = compiler_new_block(c); |
| 3373 | if (end == NULL) |
| 3374 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3375 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3376 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3377 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | if (s->v.Assert.msg) { |
| 3379 | VISIT(c, expr, s->v.Assert.msg); |
| 3380 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3381 | } |
| 3382 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3383 | compiler_use_next_block(c, end); |
| 3384 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3388 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3389 | { |
| 3390 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3391 | VISIT(c, expr, value); |
| 3392 | ADDOP(c, PRINT_EXPR); |
| 3393 | return 1; |
| 3394 | } |
| 3395 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3396 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3397 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3398 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3399 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3400 | } |
| 3401 | |
| 3402 | VISIT(c, expr, value); |
Mark Shannon | c544093 | 2021-03-15 14:24:25 +0000 | [diff] [blame] | 3403 | /* Mark POP_TOP as artificial */ |
| 3404 | c->u->u_lineno = -1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3405 | ADDOP(c, POP_TOP); |
| 3406 | return 1; |
| 3407 | } |
| 3408 | |
| 3409 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3410 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3411 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3412 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3415 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | switch (s->kind) { |
| 3418 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3419 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | case ClassDef_kind: |
| 3421 | return compiler_class(c, s); |
| 3422 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3423 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | case Delete_kind: |
| 3425 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3426 | break; |
| 3427 | case Assign_kind: |
| 3428 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3429 | VISIT(c, expr, s->v.Assign.value); |
| 3430 | for (i = 0; i < n; i++) { |
| 3431 | if (i < n - 1) |
| 3432 | ADDOP(c, DUP_TOP); |
| 3433 | VISIT(c, expr, |
| 3434 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3435 | } |
| 3436 | break; |
| 3437 | case AugAssign_kind: |
| 3438 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3439 | case AnnAssign_kind: |
| 3440 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | case For_kind: |
| 3442 | return compiler_for(c, s); |
| 3443 | case While_kind: |
| 3444 | return compiler_while(c, s); |
| 3445 | case If_kind: |
| 3446 | return compiler_if(c, s); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3447 | case Match_kind: |
| 3448 | return compiler_match(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | case Raise_kind: |
| 3450 | n = 0; |
| 3451 | if (s->v.Raise.exc) { |
| 3452 | VISIT(c, expr, s->v.Raise.exc); |
| 3453 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3454 | if (s->v.Raise.cause) { |
| 3455 | VISIT(c, expr, s->v.Raise.cause); |
| 3456 | n++; |
| 3457 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3459 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3460 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3462 | case Try_kind: |
| 3463 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | case Assert_kind: |
| 3465 | return compiler_assert(c, s); |
| 3466 | case Import_kind: |
| 3467 | return compiler_import(c, s); |
| 3468 | case ImportFrom_kind: |
| 3469 | return compiler_from_import(c, s); |
| 3470 | case Global_kind: |
| 3471 | case Nonlocal_kind: |
| 3472 | break; |
| 3473 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3474 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3475 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3476 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3477 | break; |
| 3478 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3479 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3480 | case Continue_kind: |
| 3481 | return compiler_continue(c); |
| 3482 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3483 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3484 | case AsyncFunctionDef_kind: |
| 3485 | return compiler_function(c, s, 1); |
| 3486 | case AsyncWith_kind: |
| 3487 | return compiler_async_with(c, s, 0); |
| 3488 | case AsyncFor_kind: |
| 3489 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3490 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | static int |
| 3496 | unaryop(unaryop_ty op) |
| 3497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | switch (op) { |
| 3499 | case Invert: |
| 3500 | return UNARY_INVERT; |
| 3501 | case Not: |
| 3502 | return UNARY_NOT; |
| 3503 | case UAdd: |
| 3504 | return UNARY_POSITIVE; |
| 3505 | case USub: |
| 3506 | return UNARY_NEGATIVE; |
| 3507 | default: |
| 3508 | PyErr_Format(PyExc_SystemError, |
| 3509 | "unary op %d should not be possible", op); |
| 3510 | return 0; |
| 3511 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3515 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | switch (op) { |
| 3518 | case Add: |
| 3519 | return BINARY_ADD; |
| 3520 | case Sub: |
| 3521 | return BINARY_SUBTRACT; |
| 3522 | case Mult: |
| 3523 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3524 | case MatMult: |
| 3525 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3526 | case Div: |
| 3527 | return BINARY_TRUE_DIVIDE; |
| 3528 | case Mod: |
| 3529 | return BINARY_MODULO; |
| 3530 | case Pow: |
| 3531 | return BINARY_POWER; |
| 3532 | case LShift: |
| 3533 | return BINARY_LSHIFT; |
| 3534 | case RShift: |
| 3535 | return BINARY_RSHIFT; |
| 3536 | case BitOr: |
| 3537 | return BINARY_OR; |
| 3538 | case BitXor: |
| 3539 | return BINARY_XOR; |
| 3540 | case BitAnd: |
| 3541 | return BINARY_AND; |
| 3542 | case FloorDiv: |
| 3543 | return BINARY_FLOOR_DIVIDE; |
| 3544 | default: |
| 3545 | PyErr_Format(PyExc_SystemError, |
| 3546 | "binary op %d should not be possible", op); |
| 3547 | return 0; |
| 3548 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3552 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3553 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | switch (op) { |
| 3555 | case Add: |
| 3556 | return INPLACE_ADD; |
| 3557 | case Sub: |
| 3558 | return INPLACE_SUBTRACT; |
| 3559 | case Mult: |
| 3560 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3561 | case MatMult: |
| 3562 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | case Div: |
| 3564 | return INPLACE_TRUE_DIVIDE; |
| 3565 | case Mod: |
| 3566 | return INPLACE_MODULO; |
| 3567 | case Pow: |
| 3568 | return INPLACE_POWER; |
| 3569 | case LShift: |
| 3570 | return INPLACE_LSHIFT; |
| 3571 | case RShift: |
| 3572 | return INPLACE_RSHIFT; |
| 3573 | case BitOr: |
| 3574 | return INPLACE_OR; |
| 3575 | case BitXor: |
| 3576 | return INPLACE_XOR; |
| 3577 | case BitAnd: |
| 3578 | return INPLACE_AND; |
| 3579 | case FloorDiv: |
| 3580 | return INPLACE_FLOOR_DIVIDE; |
| 3581 | default: |
| 3582 | PyErr_Format(PyExc_SystemError, |
| 3583 | "inplace binary op %d should not be possible", op); |
| 3584 | return 0; |
| 3585 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | static int |
| 3589 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3590 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3591 | int op, scope; |
| 3592 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3593 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 | PyObject *dict = c->u->u_names; |
| 3596 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3598 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3599 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3600 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3601 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3602 | if (forbidden_name(c, name, ctx)) |
| 3603 | return 0; |
| 3604 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3605 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3606 | if (!mangled) |
| 3607 | return 0; |
| 3608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | op = 0; |
| 3610 | optype = OP_NAME; |
| 3611 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3612 | switch (scope) { |
| 3613 | case FREE: |
| 3614 | dict = c->u->u_freevars; |
| 3615 | optype = OP_DEREF; |
| 3616 | break; |
| 3617 | case CELL: |
| 3618 | dict = c->u->u_cellvars; |
| 3619 | optype = OP_DEREF; |
| 3620 | break; |
| 3621 | case LOCAL: |
| 3622 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3623 | optype = OP_FAST; |
| 3624 | break; |
| 3625 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3626 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3627 | optype = OP_GLOBAL; |
| 3628 | break; |
| 3629 | case GLOBAL_EXPLICIT: |
| 3630 | optype = OP_GLOBAL; |
| 3631 | break; |
| 3632 | default: |
| 3633 | /* scope can be 0 */ |
| 3634 | break; |
| 3635 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | /* 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] | 3638 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | switch (optype) { |
| 3641 | case OP_DEREF: |
| 3642 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3643 | case Load: |
| 3644 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3645 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3646 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3647 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | } |
| 3649 | break; |
| 3650 | case OP_FAST: |
| 3651 | switch (ctx) { |
| 3652 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3653 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3655 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3656 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3657 | return 1; |
| 3658 | case OP_GLOBAL: |
| 3659 | switch (ctx) { |
| 3660 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3661 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3663 | } |
| 3664 | break; |
| 3665 | case OP_NAME: |
| 3666 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3667 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3668 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3670 | } |
| 3671 | break; |
| 3672 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3674 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3675 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | Py_DECREF(mangled); |
| 3677 | if (arg < 0) |
| 3678 | return 0; |
| 3679 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | static int |
| 3683 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3685 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3686 | int jumpi; |
| 3687 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3688 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | assert(e->kind == BoolOp_kind); |
| 3691 | if (e->v.BoolOp.op == And) |
| 3692 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3693 | else |
| 3694 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3695 | end = compiler_new_block(c); |
| 3696 | if (end == NULL) |
| 3697 | return 0; |
| 3698 | s = e->v.BoolOp.values; |
| 3699 | n = asdl_seq_LEN(s) - 1; |
| 3700 | assert(n >= 0); |
| 3701 | for (i = 0; i < n; ++i) { |
| 3702 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3703 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3704 | basicblock *next = compiler_new_block(c); |
| 3705 | if (next == NULL) { |
| 3706 | return 0; |
| 3707 | } |
| 3708 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3709 | } |
| 3710 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3711 | compiler_use_next_block(c, end); |
| 3712 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3716 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3717 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3718 | { |
| 3719 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3720 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3721 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3722 | PyObject *folded = PyTuple_New(n); |
| 3723 | if (folded == NULL) { |
| 3724 | return 0; |
| 3725 | } |
| 3726 | PyObject *val; |
| 3727 | for (i = 0; i < n; i++) { |
| 3728 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3729 | Py_INCREF(val); |
| 3730 | PyTuple_SET_ITEM(folded, i, val); |
| 3731 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3732 | if (tuple) { |
| 3733 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3734 | } else { |
| 3735 | if (add == SET_ADD) { |
| 3736 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3737 | if (folded == NULL) { |
| 3738 | return 0; |
| 3739 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3740 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3741 | ADDOP_I(c, build, pushed); |
| 3742 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3743 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3744 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3745 | return 1; |
| 3746 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3747 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3748 | for (i = 0; i < n; i++) { |
| 3749 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3750 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3751 | seen_star = 1; |
| 3752 | } |
| 3753 | } |
| 3754 | if (seen_star) { |
| 3755 | seen_star = 0; |
| 3756 | for (i = 0; i < n; i++) { |
| 3757 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3758 | if (elt->kind == Starred_kind) { |
| 3759 | if (seen_star == 0) { |
| 3760 | ADDOP_I(c, build, i+pushed); |
| 3761 | seen_star = 1; |
| 3762 | } |
| 3763 | VISIT(c, expr, elt->v.Starred.value); |
| 3764 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3765 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3766 | else { |
| 3767 | VISIT(c, expr, elt); |
| 3768 | if (seen_star) { |
| 3769 | ADDOP_I(c, add, 1); |
| 3770 | } |
| 3771 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3772 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3773 | assert(seen_star); |
| 3774 | if (tuple) { |
| 3775 | ADDOP(c, LIST_TO_TUPLE); |
| 3776 | } |
| 3777 | } |
| 3778 | else { |
| 3779 | for (i = 0; i < n; i++) { |
| 3780 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3781 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3782 | } |
| 3783 | if (tuple) { |
| 3784 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3785 | } else { |
| 3786 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3787 | } |
| 3788 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3789 | return 1; |
| 3790 | } |
| 3791 | |
| 3792 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3793 | unpack_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3794 | { |
| 3795 | Py_ssize_t n = asdl_seq_LEN(elts); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3796 | int seen_star = 0; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3797 | for (Py_ssize_t i = 0; i < n; i++) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3798 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3799 | if (elt->kind == Starred_kind && !seen_star) { |
| 3800 | if ((i >= (1 << 8)) || |
| 3801 | (n-i-1 >= (INT_MAX >> 8))) |
| 3802 | return compiler_error(c, |
| 3803 | "too many expressions in " |
| 3804 | "star-unpacking assignment"); |
| 3805 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3806 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3807 | } |
| 3808 | else if (elt->kind == Starred_kind) { |
| 3809 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3810 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | if (!seen_star) { |
| 3814 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3815 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3816 | return 1; |
| 3817 | } |
| 3818 | |
| 3819 | static int |
| 3820 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
| 3821 | { |
| 3822 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3823 | RETURN_IF_FALSE(unpack_helper(c, elts)); |
| 3824 | for (Py_ssize_t i = 0; i < n; i++) { |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3825 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3826 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3827 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3828 | return 1; |
| 3829 | } |
| 3830 | |
| 3831 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3832 | compiler_list(struct compiler *c, expr_ty e) |
| 3833 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3834 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3835 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3836 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3838 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3839 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3840 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3841 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3842 | else |
| 3843 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3844 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | static int |
| 3848 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3849 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3850 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3851 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3852 | return assignment_helper(c, elts); |
| 3853 | } |
| 3854 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3855 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3856 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3857 | } |
| 3858 | else |
| 3859 | VISIT_SEQ(c, expr, elts); |
| 3860 | return 1; |
| 3861 | } |
| 3862 | |
| 3863 | static int |
| 3864 | compiler_set(struct compiler *c, expr_ty e) |
| 3865 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3866 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3867 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3871 | 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] | 3872 | { |
| 3873 | Py_ssize_t i; |
| 3874 | for (i = begin; i < end; i++) { |
| 3875 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3876 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3877 | return 0; |
| 3878 | } |
| 3879 | return 1; |
| 3880 | } |
| 3881 | |
| 3882 | static int |
| 3883 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3884 | { |
| 3885 | Py_ssize_t i, n = end - begin; |
| 3886 | PyObject *keys, *key; |
| 3887 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3888 | for (i = begin; i < end; i++) { |
| 3889 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3890 | } |
| 3891 | keys = PyTuple_New(n); |
| 3892 | if (keys == NULL) { |
| 3893 | return 0; |
| 3894 | } |
| 3895 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3896 | 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] | 3897 | Py_INCREF(key); |
| 3898 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3899 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3900 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3901 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3902 | } |
| 3903 | else { |
| 3904 | for (i = begin; i < end; i++) { |
| 3905 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3906 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3907 | } |
| 3908 | ADDOP_I(c, BUILD_MAP, n); |
| 3909 | } |
| 3910 | return 1; |
| 3911 | } |
| 3912 | |
| 3913 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3914 | compiler_dict(struct compiler *c, expr_ty e) |
| 3915 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3916 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3917 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3918 | int is_unpacking = 0; |
| 3919 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3920 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3921 | elements = 0; |
| 3922 | for (i = 0; i < n; i++) { |
| 3923 | 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] | 3924 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3925 | if (elements) { |
| 3926 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3927 | return 0; |
| 3928 | } |
| 3929 | if (have_dict) { |
| 3930 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3931 | } |
| 3932 | have_dict = 1; |
| 3933 | elements = 0; |
| 3934 | } |
| 3935 | if (have_dict == 0) { |
| 3936 | ADDOP_I(c, BUILD_MAP, 0); |
| 3937 | have_dict = 1; |
| 3938 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3939 | 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] | 3940 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3941 | } |
| 3942 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3943 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3944 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3945 | return 0; |
| 3946 | } |
| 3947 | if (have_dict) { |
| 3948 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3949 | } |
| 3950 | have_dict = 1; |
| 3951 | elements = 0; |
| 3952 | } |
| 3953 | else { |
| 3954 | elements++; |
| 3955 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3956 | } |
| 3957 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3958 | if (elements) { |
| 3959 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3960 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3961 | } |
| 3962 | if (have_dict) { |
| 3963 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3964 | } |
| 3965 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3966 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3967 | if (!have_dict) { |
| 3968 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | } |
| 3970 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | static int |
| 3974 | compiler_compare(struct compiler *c, expr_ty e) |
| 3975 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3976 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3977 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3978 | if (!check_compare(c, e)) { |
| 3979 | return 0; |
| 3980 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3981 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3982 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3983 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3984 | if (n == 0) { |
| 3985 | 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] | 3986 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3987 | } |
| 3988 | else { |
| 3989 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3990 | if (cleanup == NULL) |
| 3991 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3992 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3993 | VISIT(c, expr, |
| 3994 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3995 | ADDOP(c, DUP_TOP); |
| 3996 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3997 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3998 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3999 | NEXT_BLOCK(c); |
| 4000 | } |
| 4001 | 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] | 4002 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4003 | basicblock *end = compiler_new_block(c); |
| 4004 | if (end == NULL) |
| 4005 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 4006 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4007 | compiler_use_next_block(c, cleanup); |
| 4008 | ADDOP(c, ROT_TWO); |
| 4009 | ADDOP(c, POP_TOP); |
| 4010 | compiler_use_next_block(c, end); |
| 4011 | } |
| 4012 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4015 | static PyTypeObject * |
| 4016 | infer_type(expr_ty e) |
| 4017 | { |
| 4018 | switch (e->kind) { |
| 4019 | case Tuple_kind: |
| 4020 | return &PyTuple_Type; |
| 4021 | case List_kind: |
| 4022 | case ListComp_kind: |
| 4023 | return &PyList_Type; |
| 4024 | case Dict_kind: |
| 4025 | case DictComp_kind: |
| 4026 | return &PyDict_Type; |
| 4027 | case Set_kind: |
| 4028 | case SetComp_kind: |
| 4029 | return &PySet_Type; |
| 4030 | case GeneratorExp_kind: |
| 4031 | return &PyGen_Type; |
| 4032 | case Lambda_kind: |
| 4033 | return &PyFunction_Type; |
| 4034 | case JoinedStr_kind: |
| 4035 | case FormattedValue_kind: |
| 4036 | return &PyUnicode_Type; |
| 4037 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4038 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4039 | default: |
| 4040 | return NULL; |
| 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | static int |
| 4045 | check_caller(struct compiler *c, expr_ty e) |
| 4046 | { |
| 4047 | switch (e->kind) { |
| 4048 | case Constant_kind: |
| 4049 | case Tuple_kind: |
| 4050 | case List_kind: |
| 4051 | case ListComp_kind: |
| 4052 | case Dict_kind: |
| 4053 | case DictComp_kind: |
| 4054 | case Set_kind: |
| 4055 | case SetComp_kind: |
| 4056 | case GeneratorExp_kind: |
| 4057 | case JoinedStr_kind: |
| 4058 | case FormattedValue_kind: |
| 4059 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4060 | "perhaps you missed a comma?", |
| 4061 | infer_type(e)->tp_name); |
| 4062 | default: |
| 4063 | return 1; |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | static int |
| 4068 | check_subscripter(struct compiler *c, expr_ty e) |
| 4069 | { |
| 4070 | PyObject *v; |
| 4071 | |
| 4072 | switch (e->kind) { |
| 4073 | case Constant_kind: |
| 4074 | v = e->v.Constant.value; |
| 4075 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4076 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4077 | PyAnySet_Check(v))) |
| 4078 | { |
| 4079 | return 1; |
| 4080 | } |
| 4081 | /* fall through */ |
| 4082 | case Set_kind: |
| 4083 | case SetComp_kind: |
| 4084 | case GeneratorExp_kind: |
| 4085 | case Lambda_kind: |
| 4086 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4087 | "perhaps you missed a comma?", |
| 4088 | infer_type(e)->tp_name); |
| 4089 | default: |
| 4090 | return 1; |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4095 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4096 | { |
| 4097 | PyObject *v; |
| 4098 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4099 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4100 | if (index_type == NULL |
| 4101 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4102 | || index_type == &PySlice_Type) { |
| 4103 | return 1; |
| 4104 | } |
| 4105 | |
| 4106 | switch (e->kind) { |
| 4107 | case Constant_kind: |
| 4108 | v = e->v.Constant.value; |
| 4109 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4110 | return 1; |
| 4111 | } |
| 4112 | /* fall through */ |
| 4113 | case Tuple_kind: |
| 4114 | case List_kind: |
| 4115 | case ListComp_kind: |
| 4116 | case JoinedStr_kind: |
| 4117 | case FormattedValue_kind: |
| 4118 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4119 | "not %.200s; " |
| 4120 | "perhaps you missed a comma?", |
| 4121 | infer_type(e)->tp_name, |
| 4122 | index_type->tp_name); |
| 4123 | default: |
| 4124 | return 1; |
| 4125 | } |
| 4126 | } |
| 4127 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4128 | // 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] | 4129 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4130 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4131 | { |
| 4132 | Py_ssize_t argsl, i; |
| 4133 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4134 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4135 | |
| 4136 | /* Check that the call node is an attribute access, and that |
| 4137 | the call doesn't have keyword parameters. */ |
| 4138 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4139 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4140 | return -1; |
| 4141 | |
| 4142 | /* Check that there are no *varargs types of arguments. */ |
| 4143 | argsl = asdl_seq_LEN(args); |
| 4144 | for (i = 0; i < argsl; i++) { |
| 4145 | expr_ty elt = asdl_seq_GET(args, i); |
| 4146 | if (elt->kind == Starred_kind) { |
| 4147 | return -1; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | /* Alright, we can optimize the code. */ |
| 4152 | VISIT(c, expr, meth->v.Attribute.value); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4153 | int old_lineno = c->u->u_lineno; |
| 4154 | c->u->u_lineno = meth->end_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4155 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4156 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4157 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 4158 | c->u->u_lineno = old_lineno; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4159 | return 1; |
| 4160 | } |
| 4161 | |
| 4162 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4163 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4164 | { |
| 4165 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4166 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4167 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4168 | if (key->arg == NULL) { |
| 4169 | continue; |
| 4170 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4171 | if (forbidden_name(c, key->arg, Store)) { |
| 4172 | return -1; |
| 4173 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4174 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4175 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4176 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4177 | c->u->u_col_offset = other->col_offset; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 4178 | compiler_error(c, "keyword argument repeated: %U", key->arg); |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4179 | return -1; |
| 4180 | } |
| 4181 | } |
| 4182 | } |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
| 4186 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4187 | compiler_call(struct compiler *c, expr_ty e) |
| 4188 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4189 | int ret = maybe_optimize_method_call(c, e); |
| 4190 | if (ret >= 0) { |
| 4191 | return ret; |
| 4192 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4193 | if (!check_caller(c, e->v.Call.func)) { |
| 4194 | return 0; |
| 4195 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | VISIT(c, expr, e->v.Call.func); |
| 4197 | return compiler_call_helper(c, 0, |
| 4198 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4199 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4202 | static int |
| 4203 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4204 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4205 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4206 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4207 | 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] | 4208 | return 1; |
| 4209 | } |
| 4210 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4211 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4212 | static int |
| 4213 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4214 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4215 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4216 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4217 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4218 | Convert the conversion char to 3 bits: |
| 4219 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4220 | !s : 001 0x1 FVC_STR |
| 4221 | !r : 010 0x2 FVC_REPR |
| 4222 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4223 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4224 | next bit is whether or not we have a format spec: |
| 4225 | yes : 100 0x4 |
| 4226 | no : 000 0x0 |
| 4227 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4228 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4229 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4230 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4231 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4232 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4233 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4234 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4235 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4236 | case 's': oparg = FVC_STR; break; |
| 4237 | case 'r': oparg = FVC_REPR; break; |
| 4238 | case 'a': oparg = FVC_ASCII; break; |
| 4239 | case -1: oparg = FVC_NONE; break; |
| 4240 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4241 | PyErr_Format(PyExc_SystemError, |
| 4242 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4243 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4244 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4245 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4246 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4247 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4248 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4249 | } |
| 4250 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4251 | /* And push our opcode and oparg */ |
| 4252 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4253 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4254 | return 1; |
| 4255 | } |
| 4256 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4257 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4258 | 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] | 4259 | { |
| 4260 | Py_ssize_t i, n = end - begin; |
| 4261 | keyword_ty kw; |
| 4262 | PyObject *keys, *key; |
| 4263 | assert(n > 0); |
| 4264 | if (n > 1) { |
| 4265 | for (i = begin; i < end; i++) { |
| 4266 | kw = asdl_seq_GET(keywords, i); |
| 4267 | VISIT(c, expr, kw->value); |
| 4268 | } |
| 4269 | keys = PyTuple_New(n); |
| 4270 | if (keys == NULL) { |
| 4271 | return 0; |
| 4272 | } |
| 4273 | for (i = begin; i < end; i++) { |
| 4274 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4275 | Py_INCREF(key); |
| 4276 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4277 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4278 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4279 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4280 | } |
| 4281 | else { |
| 4282 | /* a for loop only executes once */ |
| 4283 | for (i = begin; i < end; i++) { |
| 4284 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4285 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4286 | VISIT(c, expr, kw->value); |
| 4287 | } |
| 4288 | ADDOP_I(c, BUILD_MAP, n); |
| 4289 | } |
| 4290 | return 1; |
| 4291 | } |
| 4292 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4293 | /* shared code between compiler_call and compiler_class */ |
| 4294 | static int |
| 4295 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4296 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4297 | asdl_expr_seq *args, |
| 4298 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4299 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4300 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4301 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4302 | if (validate_keywords(c, keywords) == -1) { |
| 4303 | return 0; |
| 4304 | } |
| 4305 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4306 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4307 | nkwelts = asdl_seq_LEN(keywords); |
| 4308 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4309 | for (i = 0; i < nelts; i++) { |
| 4310 | expr_ty elt = asdl_seq_GET(args, i); |
| 4311 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4312 | goto ex_call; |
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 | } |
| 4315 | for (i = 0; i < nkwelts; i++) { |
| 4316 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4317 | if (kw->arg == NULL) { |
| 4318 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4319 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4321 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4322 | /* No * or ** args, so can use faster calling sequence */ |
| 4323 | for (i = 0; i < nelts; i++) { |
| 4324 | expr_ty elt = asdl_seq_GET(args, i); |
| 4325 | assert(elt->kind != Starred_kind); |
| 4326 | VISIT(c, expr, elt); |
| 4327 | } |
| 4328 | if (nkwelts) { |
| 4329 | PyObject *names; |
| 4330 | VISIT_SEQ(c, keyword, keywords); |
| 4331 | names = PyTuple_New(nkwelts); |
| 4332 | if (names == NULL) { |
| 4333 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4334 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4335 | for (i = 0; i < nkwelts; i++) { |
| 4336 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4337 | Py_INCREF(kw->arg); |
| 4338 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4339 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4340 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4341 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4342 | return 1; |
| 4343 | } |
| 4344 | else { |
| 4345 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4346 | return 1; |
| 4347 | } |
| 4348 | |
| 4349 | ex_call: |
| 4350 | |
| 4351 | /* Do positional arguments. */ |
| 4352 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4353 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4354 | } |
| 4355 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4356 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4357 | return 0; |
| 4358 | } |
| 4359 | /* Then keyword arguments */ |
| 4360 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4361 | /* Has a new dict been pushed */ |
| 4362 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4363 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4364 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4365 | for (i = 0; i < nkwelts; i++) { |
| 4366 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4367 | if (kw->arg == NULL) { |
| 4368 | /* A keyword argument unpacking. */ |
| 4369 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4370 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4371 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4372 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4373 | if (have_dict) { |
| 4374 | ADDOP_I(c, DICT_MERGE, 1); |
| 4375 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4376 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4377 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4378 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4379 | if (!have_dict) { |
| 4380 | ADDOP_I(c, BUILD_MAP, 0); |
| 4381 | have_dict = 1; |
| 4382 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4383 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4384 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4385 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4386 | else { |
| 4387 | nseen++; |
| 4388 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4389 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4390 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4391 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4392 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4393 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4394 | } |
| 4395 | if (have_dict) { |
| 4396 | ADDOP_I(c, DICT_MERGE, 1); |
| 4397 | } |
| 4398 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4399 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4400 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4402 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4403 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4406 | |
| 4407 | /* List and set comprehensions and generator expressions work by creating a |
| 4408 | nested function to perform the actual iteration. This means that the |
| 4409 | iteration variables don't leak into the current scope. |
| 4410 | The defined function is called immediately following its definition, with the |
| 4411 | result of that call being the result of the expression. |
| 4412 | The LC/SC version returns the populated container, while the GE version is |
| 4413 | flagged in symtable.c as a generator, so it returns the generator object |
| 4414 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4415 | |
| 4416 | Possible cleanups: |
| 4417 | - iterate over the generator sequence instead of using recursion |
| 4418 | */ |
| 4419 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4420 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4421 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4423 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4424 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4426 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4427 | comprehension_ty gen; |
| 4428 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4429 | if (gen->is_async) { |
| 4430 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4431 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4432 | } else { |
| 4433 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4434 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | static int |
| 4439 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4440 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4441 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4442 | expr_ty elt, expr_ty val, int type) |
| 4443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | /* generate code for the iterator, then each of the ifs, |
| 4445 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | comprehension_ty gen; |
| 4448 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4449 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | start = compiler_new_block(c); |
| 4452 | skip = compiler_new_block(c); |
| 4453 | if_cleanup = compiler_new_block(c); |
| 4454 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4456 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4457 | anchor == NULL) |
| 4458 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | if (gen_index == 0) { |
| 4463 | /* Receive outermost iter as an implicit argument */ |
| 4464 | c->u->u_argcount = 1; |
| 4465 | ADDOP_I(c, LOAD_FAST, 0); |
| 4466 | } |
| 4467 | else { |
| 4468 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4469 | /* Fast path for the temporary variable assignment idiom: |
| 4470 | for y in [f(x)] |
| 4471 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4472 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4473 | switch (gen->iter->kind) { |
| 4474 | case List_kind: |
| 4475 | elts = gen->iter->v.List.elts; |
| 4476 | break; |
| 4477 | case Tuple_kind: |
| 4478 | elts = gen->iter->v.Tuple.elts; |
| 4479 | break; |
| 4480 | default: |
| 4481 | elts = NULL; |
| 4482 | } |
| 4483 | if (asdl_seq_LEN(elts) == 1) { |
| 4484 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4485 | if (elt->kind != Starred_kind) { |
| 4486 | VISIT(c, expr, elt); |
| 4487 | start = NULL; |
| 4488 | } |
| 4489 | } |
| 4490 | if (start) { |
| 4491 | VISIT(c, expr, gen->iter); |
| 4492 | ADDOP(c, GET_ITER); |
| 4493 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4495 | if (start) { |
| 4496 | depth++; |
| 4497 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4498 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4499 | NEXT_BLOCK(c); |
| 4500 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | /* XXX this needs to be cleaned up...a lot! */ |
| 4504 | n = asdl_seq_LEN(gen->ifs); |
| 4505 | for (i = 0; i < n; i++) { |
| 4506 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4507 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4508 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | NEXT_BLOCK(c); |
| 4510 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4513 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4514 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4515 | elt, val, type)) |
| 4516 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | /* only append after the last for generator */ |
| 4519 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4520 | /* comprehension specific code */ |
| 4521 | switch (type) { |
| 4522 | case COMP_GENEXP: |
| 4523 | VISIT(c, expr, elt); |
| 4524 | ADDOP(c, YIELD_VALUE); |
| 4525 | ADDOP(c, POP_TOP); |
| 4526 | break; |
| 4527 | case COMP_LISTCOMP: |
| 4528 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4529 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | break; |
| 4531 | case COMP_SETCOMP: |
| 4532 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4533 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4534 | break; |
| 4535 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4536 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4537 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4539 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4540 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | break; |
| 4542 | default: |
| 4543 | return 0; |
| 4544 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | compiler_use_next_block(c, skip); |
| 4547 | } |
| 4548 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4549 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4550 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4551 | compiler_use_next_block(c, anchor); |
| 4552 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4553 | |
| 4554 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4558 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4559 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4560 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4561 | expr_ty elt, expr_ty val, int type) |
| 4562 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4563 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4564 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4565 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4566 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4567 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4568 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4569 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4570 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4571 | return 0; |
| 4572 | } |
| 4573 | |
| 4574 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4575 | |
| 4576 | if (gen_index == 0) { |
| 4577 | /* Receive outermost iter as an implicit argument */ |
| 4578 | c->u->u_argcount = 1; |
| 4579 | ADDOP_I(c, LOAD_FAST, 0); |
| 4580 | } |
| 4581 | else { |
| 4582 | /* Sub-iter - calculate on the fly */ |
| 4583 | VISIT(c, expr, gen->iter); |
| 4584 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4585 | } |
| 4586 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4587 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4588 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4589 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4590 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4591 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4592 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4593 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4594 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4595 | |
| 4596 | n = asdl_seq_LEN(gen->ifs); |
| 4597 | for (i = 0; i < n; i++) { |
| 4598 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4599 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4600 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4601 | NEXT_BLOCK(c); |
| 4602 | } |
| 4603 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4604 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4605 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4606 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4607 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4608 | elt, val, type)) |
| 4609 | return 0; |
| 4610 | |
| 4611 | /* only append after the last for generator */ |
| 4612 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4613 | /* comprehension specific code */ |
| 4614 | switch (type) { |
| 4615 | case COMP_GENEXP: |
| 4616 | VISIT(c, expr, elt); |
| 4617 | ADDOP(c, YIELD_VALUE); |
| 4618 | ADDOP(c, POP_TOP); |
| 4619 | break; |
| 4620 | case COMP_LISTCOMP: |
| 4621 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4622 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4623 | break; |
| 4624 | case COMP_SETCOMP: |
| 4625 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4626 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4627 | break; |
| 4628 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4629 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4630 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4631 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4632 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4633 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4634 | break; |
| 4635 | default: |
| 4636 | return 0; |
| 4637 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4638 | } |
| 4639 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4640 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4641 | |
| 4642 | compiler_use_next_block(c, except); |
| 4643 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4644 | |
| 4645 | return 1; |
| 4646 | } |
| 4647 | |
| 4648 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4649 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4650 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4651 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4654 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4655 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4656 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4657 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4658 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4659 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4660 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4661 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4662 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4663 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4664 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4670 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4671 | 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] | 4672 | compiler_error(c, "asynchronous comprehension outside of " |
| 4673 | "an asynchronous function"); |
| 4674 | goto error_in_scope; |
| 4675 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | if (type != COMP_GENEXP) { |
| 4678 | int op; |
| 4679 | switch (type) { |
| 4680 | case COMP_LISTCOMP: |
| 4681 | op = BUILD_LIST; |
| 4682 | break; |
| 4683 | case COMP_SETCOMP: |
| 4684 | op = BUILD_SET; |
| 4685 | break; |
| 4686 | case COMP_DICTCOMP: |
| 4687 | op = BUILD_MAP; |
| 4688 | break; |
| 4689 | default: |
| 4690 | PyErr_Format(PyExc_SystemError, |
| 4691 | "unknown comprehension type %d", type); |
| 4692 | goto error_in_scope; |
| 4693 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4695 | ADDOP_I(c, op, 0); |
| 4696 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4697 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4698 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | val, type)) |
| 4700 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4702 | if (type != COMP_GENEXP) { |
| 4703 | ADDOP(c, RETURN_VALUE); |
| 4704 | } |
| 4705 | |
| 4706 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4707 | qualname = c->u->u_qualname; |
| 4708 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4709 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4710 | if (top_level_await && is_async_generator){ |
| 4711 | c->u->u_ste->ste_coroutine = 1; |
| 4712 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4713 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | goto error; |
| 4715 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4716 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4718 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4719 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4720 | Py_DECREF(co); |
| 4721 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4722 | VISIT(c, expr, outermost->iter); |
| 4723 | |
| 4724 | if (outermost->is_async) { |
| 4725 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4726 | } else { |
| 4727 | ADDOP(c, GET_ITER); |
| 4728 | } |
| 4729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4731 | |
| 4732 | if (is_async_generator && type != COMP_GENEXP) { |
| 4733 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4734 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4735 | ADDOP(c, YIELD_FROM); |
| 4736 | } |
| 4737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4738 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4739 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4740 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4741 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4742 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4743 | Py_XDECREF(co); |
| 4744 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4748 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4750 | static identifier name; |
| 4751 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4752 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4753 | if (!name) |
| 4754 | return 0; |
| 4755 | } |
| 4756 | assert(e->kind == GeneratorExp_kind); |
| 4757 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4758 | e->v.GeneratorExp.generators, |
| 4759 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4763 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4765 | static identifier name; |
| 4766 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4767 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4768 | if (!name) |
| 4769 | return 0; |
| 4770 | } |
| 4771 | assert(e->kind == ListComp_kind); |
| 4772 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4773 | e->v.ListComp.generators, |
| 4774 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | static int |
| 4778 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | static identifier name; |
| 4781 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4782 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4783 | if (!name) |
| 4784 | return 0; |
| 4785 | } |
| 4786 | assert(e->kind == SetComp_kind); |
| 4787 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4788 | e->v.SetComp.generators, |
| 4789 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | |
| 4793 | static int |
| 4794 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | static identifier name; |
| 4797 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4798 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4799 | if (!name) |
| 4800 | return 0; |
| 4801 | } |
| 4802 | assert(e->kind == DictComp_kind); |
| 4803 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4804 | e->v.DictComp.generators, |
| 4805 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | |
| 4809 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4810 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4811 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4812 | VISIT(c, expr, k->value); |
| 4813 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4816 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4817 | whether they are true or false. |
| 4818 | |
| 4819 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4820 | */ |
| 4821 | |
| 4822 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4823 | compiler_with_except_finish(struct compiler *c) { |
| 4824 | basicblock *exit; |
| 4825 | exit = compiler_new_block(c); |
| 4826 | if (exit == NULL) |
| 4827 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4828 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4829 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4830 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4831 | compiler_use_next_block(c, exit); |
| 4832 | ADDOP(c, POP_TOP); |
| 4833 | ADDOP(c, POP_TOP); |
| 4834 | ADDOP(c, POP_TOP); |
| 4835 | ADDOP(c, POP_EXCEPT); |
| 4836 | ADDOP(c, POP_TOP); |
| 4837 | return 1; |
| 4838 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4839 | |
| 4840 | /* |
| 4841 | Implements the async with statement. |
| 4842 | |
| 4843 | The semantics outlined in that PEP are as follows: |
| 4844 | |
| 4845 | async with EXPR as VAR: |
| 4846 | BLOCK |
| 4847 | |
| 4848 | It is implemented roughly as: |
| 4849 | |
| 4850 | context = EXPR |
| 4851 | exit = context.__aexit__ # not calling it |
| 4852 | value = await context.__aenter__() |
| 4853 | try: |
| 4854 | VAR = value # if VAR present in the syntax |
| 4855 | BLOCK |
| 4856 | finally: |
| 4857 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4858 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4859 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4860 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4861 | if not (await exit(*exc)): |
| 4862 | raise |
| 4863 | */ |
| 4864 | static int |
| 4865 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4866 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4867 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4868 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4869 | |
| 4870 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4871 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4872 | c->u->u_ste->ste_coroutine = 1; |
| 4873 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4874 | return compiler_error(c, "'async with' outside async function"); |
| 4875 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4876 | |
| 4877 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4878 | final = compiler_new_block(c); |
| 4879 | exit = compiler_new_block(c); |
| 4880 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4881 | return 0; |
| 4882 | |
| 4883 | /* Evaluate EXPR */ |
| 4884 | VISIT(c, expr, item->context_expr); |
| 4885 | |
| 4886 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4887 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4888 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4889 | ADDOP(c, YIELD_FROM); |
| 4890 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4891 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4892 | |
| 4893 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4894 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4895 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4896 | return 0; |
| 4897 | } |
| 4898 | |
| 4899 | if (item->optional_vars) { |
| 4900 | VISIT(c, expr, item->optional_vars); |
| 4901 | } |
| 4902 | else { |
| 4903 | /* Discard result from context.__aenter__() */ |
| 4904 | ADDOP(c, POP_TOP); |
| 4905 | } |
| 4906 | |
| 4907 | pos++; |
| 4908 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4909 | /* BLOCK code */ |
| 4910 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4911 | else if (!compiler_async_with(c, s, pos)) |
| 4912 | return 0; |
| 4913 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4914 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4915 | ADDOP(c, POP_BLOCK); |
| 4916 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4917 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4918 | /* For successful outcome: |
| 4919 | * call __exit__(None, None, None) |
| 4920 | */ |
| 4921 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4922 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4923 | ADDOP(c, GET_AWAITABLE); |
| 4924 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4925 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4926 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4927 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4928 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4929 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4930 | |
| 4931 | /* For exceptional outcome: */ |
| 4932 | compiler_use_next_block(c, final); |
| 4933 | |
| 4934 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4935 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4936 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4937 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4938 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4939 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4940 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4941 | return 1; |
| 4942 | } |
| 4943 | |
| 4944 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4945 | /* |
| 4946 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4947 | with EXPR as VAR: |
| 4948 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4949 | is implemented as: |
| 4950 | <code for EXPR> |
| 4951 | SETUP_WITH E |
| 4952 | <code to store to VAR> or POP_TOP |
| 4953 | <code for BLOCK> |
| 4954 | LOAD_CONST (None, None, None) |
| 4955 | CALL_FUNCTION_EX 0 |
| 4956 | JUMP_FORWARD EXIT |
| 4957 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4958 | POP_JUMP_IF_TRUE T: |
| 4959 | RERAISE |
| 4960 | T: POP_TOP * 3 (remove exception from stack) |
| 4961 | POP_EXCEPT |
| 4962 | POP_TOP |
| 4963 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4964 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4965 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4966 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4967 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4968 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4969 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4970 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4971 | |
| 4972 | assert(s->kind == With_kind); |
| 4973 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4974 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4975 | final = compiler_new_block(c); |
| 4976 | exit = compiler_new_block(c); |
| 4977 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4978 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4979 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4980 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4981 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4982 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4983 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4984 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4985 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4986 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4987 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4988 | return 0; |
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 | if (item->optional_vars) { |
| 4992 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4993 | } |
| 4994 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4995 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4996 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4999 | pos++; |
| 5000 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 5001 | /* BLOCK code */ |
| 5002 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 5003 | else if (!compiler_with(c, s, pos)) |
| 5004 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5005 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 5006 | |
| 5007 | /* Mark all following code as artificial */ |
| 5008 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5009 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5010 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5011 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5012 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5013 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5014 | /* For successful outcome: |
| 5015 | * call __exit__(None, None, None) |
| 5016 | */ |
| 5017 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 5018 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5019 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5020 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5021 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5022 | /* For exceptional outcome: */ |
| 5023 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5024 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5025 | ADDOP(c, WITH_EXCEPT_START); |
| 5026 | compiler_with_except_finish(c); |
| 5027 | |
| 5028 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5029 | return 1; |
| 5030 | } |
| 5031 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5032 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5033 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5035 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5036 | case NamedExpr_kind: |
| 5037 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5038 | ADDOP(c, DUP_TOP); |
| 5039 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5040 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | case BoolOp_kind: |
| 5042 | return compiler_boolop(c, e); |
| 5043 | case BinOp_kind: |
| 5044 | VISIT(c, expr, e->v.BinOp.left); |
| 5045 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5046 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | break; |
| 5048 | case UnaryOp_kind: |
| 5049 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5050 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5051 | break; |
| 5052 | case Lambda_kind: |
| 5053 | return compiler_lambda(c, e); |
| 5054 | case IfExp_kind: |
| 5055 | return compiler_ifexp(c, e); |
| 5056 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5057 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5058 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5059 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | case GeneratorExp_kind: |
| 5061 | return compiler_genexp(c, e); |
| 5062 | case ListComp_kind: |
| 5063 | return compiler_listcomp(c, e); |
| 5064 | case SetComp_kind: |
| 5065 | return compiler_setcomp(c, e); |
| 5066 | case DictComp_kind: |
| 5067 | return compiler_dictcomp(c, e); |
| 5068 | case Yield_kind: |
| 5069 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5070 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5071 | if (e->v.Yield.value) { |
| 5072 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | } |
| 5074 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5075 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5076 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5077 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5078 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5079 | case YieldFrom_kind: |
| 5080 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5081 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5082 | |
| 5083 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5084 | return compiler_error(c, "'yield from' inside async function"); |
| 5085 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5086 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5087 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5088 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5089 | ADDOP(c, YIELD_FROM); |
| 5090 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5091 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5092 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5093 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5094 | return compiler_error(c, "'await' outside function"); |
| 5095 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5096 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5097 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5098 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5099 | return compiler_error(c, "'await' outside async function"); |
| 5100 | } |
| 5101 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5102 | |
| 5103 | VISIT(c, expr, e->v.Await.value); |
| 5104 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5105 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5106 | ADDOP(c, YIELD_FROM); |
| 5107 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | case Compare_kind: |
| 5109 | return compiler_compare(c, e); |
| 5110 | case Call_kind: |
| 5111 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5112 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5113 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5114 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5115 | case JoinedStr_kind: |
| 5116 | return compiler_joined_str(c, e); |
| 5117 | case FormattedValue_kind: |
| 5118 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5119 | /* The following exprs can be assignment targets. */ |
| 5120 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5121 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5123 | case Load: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5124 | { |
| 5125 | int old_lineno = c->u->u_lineno; |
| 5126 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5127 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5128 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5129 | break; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5130 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5131 | case Store: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5132 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5133 | return 0; |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5134 | } |
| 5135 | int old_lineno = c->u->u_lineno; |
| 5136 | c->u->u_lineno = e->end_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5137 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5138 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5139 | break; |
| 5140 | case Del: |
| 5141 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5142 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5143 | } |
| 5144 | break; |
| 5145 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5146 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5147 | case Starred_kind: |
| 5148 | switch (e->v.Starred.ctx) { |
| 5149 | case Store: |
| 5150 | /* In all legitimate cases, the Starred node was already replaced |
| 5151 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5152 | return compiler_error(c, |
| 5153 | "starred assignment target must be in a list or tuple"); |
| 5154 | default: |
| 5155 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5156 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5157 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5158 | break; |
| 5159 | case Slice_kind: |
| 5160 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5161 | case Name_kind: |
| 5162 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5163 | /* child nodes of List and Tuple will have expr_context set */ |
| 5164 | case List_kind: |
| 5165 | return compiler_list(c, e); |
| 5166 | case Tuple_kind: |
| 5167 | return compiler_tuple(c, e); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5168 | case MatchAs_kind: |
| 5169 | case MatchOr_kind: |
| 5170 | // Can only occur in patterns, which are handled elsewhere. |
| 5171 | Py_UNREACHABLE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5172 | } |
| 5173 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5177 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5178 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5179 | int old_lineno = c->u->u_lineno; |
| 5180 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5181 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5182 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5183 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5184 | c->u->u_col_offset = old_col_offset; |
| 5185 | return res; |
| 5186 | } |
| 5187 | |
| 5188 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5189 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5191 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5192 | expr_ty e = s->v.AugAssign.target; |
| 5193 | |
| 5194 | int old_lineno = c->u->u_lineno; |
| 5195 | int old_col_offset = c->u->u_col_offset; |
| 5196 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | switch (e->kind) { |
| 5199 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5200 | VISIT(c, expr, e->v.Attribute.value); |
| 5201 | ADDOP(c, DUP_TOP); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5202 | int old_lineno = c->u->u_lineno; |
| 5203 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5204 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5205 | c->u->u_lineno = old_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | break; |
| 5207 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5208 | VISIT(c, expr, e->v.Subscript.value); |
| 5209 | VISIT(c, expr, e->v.Subscript.slice); |
| 5210 | ADDOP(c, DUP_TOP_TWO); |
| 5211 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5212 | break; |
| 5213 | case Name_kind: |
| 5214 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5215 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5216 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | default: |
| 5218 | PyErr_Format(PyExc_SystemError, |
| 5219 | "invalid node type (%d) for augmented assignment", |
| 5220 | e->kind); |
| 5221 | return 0; |
| 5222 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5223 | |
| 5224 | c->u->u_lineno = old_lineno; |
| 5225 | c->u->u_col_offset = old_col_offset; |
| 5226 | |
| 5227 | VISIT(c, expr, s->v.AugAssign.value); |
| 5228 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5229 | |
| 5230 | SET_LOC(c, e); |
| 5231 | |
| 5232 | switch (e->kind) { |
| 5233 | case Attribute_kind: |
Mark Shannon | d48848c | 2021-03-14 18:01:30 +0000 | [diff] [blame] | 5234 | c->u->u_lineno = e->end_lineno; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5235 | ADDOP(c, ROT_TWO); |
| 5236 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5237 | break; |
| 5238 | case Subscript_kind: |
| 5239 | ADDOP(c, ROT_THREE); |
| 5240 | ADDOP(c, STORE_SUBSCR); |
| 5241 | break; |
| 5242 | case Name_kind: |
| 5243 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5244 | default: |
| 5245 | Py_UNREACHABLE(); |
| 5246 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5247 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5251 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5252 | { |
| 5253 | VISIT(c, expr, e); |
| 5254 | ADDOP(c, POP_TOP); |
| 5255 | return 1; |
| 5256 | } |
| 5257 | |
| 5258 | static int |
| 5259 | check_annotation(struct compiler *c, stmt_ty s) |
| 5260 | { |
| 5261 | /* Annotations are only evaluated in a module or class. */ |
| 5262 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5263 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5264 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5265 | } |
| 5266 | return 1; |
| 5267 | } |
| 5268 | |
| 5269 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5270 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5271 | { |
| 5272 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5273 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5274 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5275 | 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] | 5276 | return 0; |
| 5277 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5278 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5279 | return 0; |
| 5280 | } |
| 5281 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5282 | return 0; |
| 5283 | } |
| 5284 | return 1; |
| 5285 | case Tuple_kind: { |
| 5286 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5287 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5288 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5289 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5290 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5291 | return 0; |
| 5292 | } |
| 5293 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5294 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5295 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5296 | default: |
| 5297 | return check_ann_expr(c, e); |
| 5298 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | static int |
| 5302 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5303 | { |
| 5304 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5305 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5306 | |
| 5307 | assert(s->kind == AnnAssign_kind); |
| 5308 | |
| 5309 | /* We perform the actual assignment first. */ |
| 5310 | if (s->v.AnnAssign.value) { |
| 5311 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5312 | VISIT(c, expr, targ); |
| 5313 | } |
| 5314 | switch (targ->kind) { |
| 5315 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5316 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5317 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5318 | /* If we have a simple name in a module or class, store annotation. */ |
| 5319 | if (s->v.AnnAssign.simple && |
| 5320 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5321 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5322 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5323 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5324 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5325 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5326 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5327 | } |
| 5328 | break; |
| 5329 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5330 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5331 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5332 | if (!s->v.AnnAssign.value && |
| 5333 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5334 | return 0; |
| 5335 | } |
| 5336 | break; |
| 5337 | case Subscript_kind: |
| 5338 | if (!s->v.AnnAssign.value && |
| 5339 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5340 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5341 | return 0; |
| 5342 | } |
| 5343 | break; |
| 5344 | default: |
| 5345 | PyErr_Format(PyExc_SystemError, |
| 5346 | "invalid node type (%d) for annotated assignment", |
| 5347 | targ->kind); |
| 5348 | return 0; |
| 5349 | } |
| 5350 | /* Annotation is evaluated last. */ |
| 5351 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5352 | return 0; |
| 5353 | } |
| 5354 | return 1; |
| 5355 | } |
| 5356 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5357 | /* Raises a SyntaxError and returns 0. |
| 5358 | If something goes wrong, a different exception may be raised. |
| 5359 | */ |
| 5360 | |
| 5361 | static int |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5362 | compiler_error(struct compiler *c, const char *format, ...) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5363 | { |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5364 | va_list vargs; |
| 5365 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5366 | va_start(vargs, format); |
| 5367 | #else |
| 5368 | va_start(vargs); |
| 5369 | #endif |
| 5370 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5371 | va_end(vargs); |
| 5372 | if (msg == NULL) { |
| 5373 | return 0; |
| 5374 | } |
| 5375 | PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
| 5376 | if (loc == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5377 | Py_INCREF(Py_None); |
| 5378 | loc = Py_None; |
| 5379 | } |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5380 | PyObject *args = Py_BuildValue("O(OiiO)", msg, c->c_filename, |
| 5381 | c->u->u_lineno, c->u->u_col_offset + 1, loc); |
| 5382 | Py_DECREF(msg); |
| 5383 | if (args == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | goto exit; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5385 | } |
| 5386 | PyErr_SetObject(PyExc_SyntaxError, args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5387 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5388 | Py_DECREF(loc); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5389 | Py_XDECREF(args); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5393 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5394 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5395 | and returns 0. |
| 5396 | */ |
| 5397 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5398 | compiler_warn(struct compiler *c, const char *format, ...) |
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 | va_list vargs; |
| 5401 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5402 | va_start(vargs, format); |
| 5403 | #else |
| 5404 | va_start(vargs); |
| 5405 | #endif |
| 5406 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5407 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5408 | if (msg == NULL) { |
| 5409 | return 0; |
| 5410 | } |
| 5411 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5412 | c->u->u_lineno, NULL, NULL) < 0) |
| 5413 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5414 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5415 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5416 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5417 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5418 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5419 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5420 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5421 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5422 | return 0; |
| 5423 | } |
| 5424 | Py_DECREF(msg); |
| 5425 | return 1; |
| 5426 | } |
| 5427 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5428 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5429 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5430 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5431 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5432 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5433 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5434 | if (ctx == Load) { |
| 5435 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5436 | return 0; |
| 5437 | } |
| 5438 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5439 | return 0; |
| 5440 | } |
| 5441 | } |
| 5442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5443 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | case Store: op = STORE_SUBSCR; break; |
| 5446 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5447 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5448 | assert(op); |
| 5449 | VISIT(c, expr, e->v.Subscript.value); |
| 5450 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | ADDOP(c, op); |
| 5452 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5456 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5458 | int n = 2; |
| 5459 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5461 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5462 | if (s->v.Slice.lower) { |
| 5463 | VISIT(c, expr, s->v.Slice.lower); |
| 5464 | } |
| 5465 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5466 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5467 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | if (s->v.Slice.upper) { |
| 5470 | VISIT(c, expr, s->v.Slice.upper); |
| 5471 | } |
| 5472 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5473 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | if (s->v.Slice.step) { |
| 5477 | n++; |
| 5478 | VISIT(c, expr, s->v.Slice.step); |
| 5479 | } |
| 5480 | ADDOP_I(c, BUILD_SLICE, n); |
| 5481 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 5484 | |
| 5485 | // PEP 634: Structural Pattern Matching |
| 5486 | |
| 5487 | // To keep things simple, all compiler_pattern_* routines follow the convention |
| 5488 | // of replacing TOS (the subject for the given pattern) with either True (match) |
| 5489 | // or False (no match). We do this even for irrefutable patterns; the idea is |
| 5490 | // that it's much easier to smooth out any redundant pushing, popping, and |
| 5491 | // jumping in the peephole optimizer than to detect or predict it here. |
| 5492 | |
| 5493 | |
| 5494 | #define WILDCARD_CHECK(N) \ |
| 5495 | ((N)->kind == Name_kind && \ |
| 5496 | _PyUnicode_EqualToASCIIString((N)->v.Name.id, "_")) |
| 5497 | |
| 5498 | |
| 5499 | static int |
| 5500 | pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc) |
| 5501 | { |
| 5502 | assert(!_PyUnicode_EqualToASCIIString(n, "_")); |
| 5503 | // Can't assign to the same name twice: |
| 5504 | if (pc->stores == NULL) { |
| 5505 | RETURN_IF_FALSE(pc->stores = PySet_New(NULL)); |
| 5506 | } |
| 5507 | else { |
| 5508 | int duplicate = PySet_Contains(pc->stores, n); |
| 5509 | if (duplicate < 0) { |
| 5510 | return 0; |
| 5511 | } |
| 5512 | if (duplicate) { |
| 5513 | const char *e = "multiple assignments to name %R in pattern"; |
| 5514 | return compiler_error(c, e, n); |
| 5515 | } |
| 5516 | } |
| 5517 | RETURN_IF_FALSE(!PySet_Add(pc->stores, n)); |
| 5518 | RETURN_IF_FALSE(compiler_nameop(c, n, Store)); |
| 5519 | return 1; |
| 5520 | } |
| 5521 | |
| 5522 | |
| 5523 | static int |
| 5524 | pattern_helper_sequence_unpack(struct compiler *c, asdl_expr_seq *values, |
| 5525 | Py_ssize_t star, pattern_context *pc) |
| 5526 | { |
| 5527 | RETURN_IF_FALSE(unpack_helper(c, values)); |
| 5528 | // We've now got a bunch of new subjects on the stack. If any of them fail |
| 5529 | // to match, we need to pop everything else off, then finally push False. |
| 5530 | // fails is an array of blocks that correspond to the necessary amount of |
| 5531 | // popping for each element: |
| 5532 | basicblock **fails; |
| 5533 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5534 | fails = (basicblock **)PyObject_Malloc(sizeof(basicblock*) * size); |
| 5535 | if (fails == NULL) { |
| 5536 | PyErr_NoMemory(); |
| 5537 | return 0; |
| 5538 | } |
| 5539 | // NOTE: Can't use our nice returning macros anymore: they'll leak memory! |
| 5540 | // goto error on error. |
| 5541 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5542 | fails[i] = compiler_new_block(c); |
| 5543 | if (fails[i] == NULL) { |
| 5544 | goto error; |
| 5545 | } |
| 5546 | } |
| 5547 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5548 | expr_ty value = asdl_seq_GET(values, i); |
| 5549 | if (i == star) { |
| 5550 | assert(value->kind == Starred_kind); |
| 5551 | value = value->v.Starred.value; |
| 5552 | } |
| 5553 | if (!compiler_pattern_subpattern(c, value, pc) || |
| 5554 | !compiler_addop_j(c, POP_JUMP_IF_FALSE, fails[i]) || |
| 5555 | compiler_next_block(c) == NULL) |
| 5556 | { |
| 5557 | goto error; |
| 5558 | } |
| 5559 | } |
| 5560 | // Success! |
| 5561 | basicblock *end = compiler_new_block(c); |
| 5562 | if (end == NULL || |
| 5563 | !compiler_addop_load_const(c, Py_True) || |
| 5564 | !compiler_addop_j(c, JUMP_FORWARD, end)) |
| 5565 | { |
| 5566 | goto error; |
| 5567 | } |
| 5568 | // This is where we handle failed sub-patterns. For a sequence pattern like |
| 5569 | // [a, b, c, d], this will look like: |
| 5570 | // fails[0]: POP_TOP |
| 5571 | // fails[1]: POP_TOP |
| 5572 | // fails[2]: POP_TOP |
| 5573 | // fails[3]: LOAD_CONST False |
| 5574 | for (Py_ssize_t i = 0; i < size - 1; i++) { |
| 5575 | compiler_use_next_block(c, fails[i]); |
| 5576 | if (!compiler_addop(c, POP_TOP)) { |
| 5577 | goto error; |
| 5578 | } |
| 5579 | } |
| 5580 | compiler_use_next_block(c, fails[size - 1]); |
| 5581 | if (!compiler_addop_load_const(c, Py_False)) { |
| 5582 | goto error; |
| 5583 | } |
| 5584 | compiler_use_next_block(c, end); |
| 5585 | PyObject_Free(fails); |
| 5586 | return 1; |
| 5587 | error: |
| 5588 | PyObject_Free(fails); |
| 5589 | return 0; |
| 5590 | } |
| 5591 | |
| 5592 | // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of |
| 5593 | // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a |
| 5594 | // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc. |
| 5595 | static int |
| 5596 | pattern_helper_sequence_subscr(struct compiler *c, asdl_expr_seq *values, |
| 5597 | Py_ssize_t star, pattern_context *pc) |
| 5598 | { |
| 5599 | basicblock *end, *fail_pop_1; |
| 5600 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5601 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5602 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5603 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5604 | expr_ty value = asdl_seq_GET(values, i); |
| 5605 | if (WILDCARD_CHECK(value)) { |
| 5606 | continue; |
| 5607 | } |
| 5608 | if (i == star) { |
| 5609 | assert(value->kind == Starred_kind); |
| 5610 | assert(WILDCARD_CHECK(value->v.Starred.value)); |
| 5611 | continue; |
| 5612 | } |
| 5613 | ADDOP(c, DUP_TOP); |
| 5614 | if (i < star) { |
| 5615 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5616 | } |
| 5617 | else { |
| 5618 | // The subject may not support negative indexing! Compute a |
| 5619 | // nonnegative index: |
| 5620 | ADDOP(c, GET_LEN); |
| 5621 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i)); |
| 5622 | ADDOP(c, BINARY_SUBTRACT); |
| 5623 | } |
| 5624 | ADDOP(c, BINARY_SUBSCR); |
| 5625 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5626 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5627 | NEXT_BLOCK(c); |
| 5628 | } |
| 5629 | ADDOP(c, POP_TOP); |
| 5630 | ADDOP_LOAD_CONST(c, Py_True); |
| 5631 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5632 | compiler_use_next_block(c, fail_pop_1); |
| 5633 | ADDOP(c, POP_TOP); |
| 5634 | ADDOP_LOAD_CONST(c, Py_False); |
| 5635 | compiler_use_next_block(c, end); |
| 5636 | return 1; |
| 5637 | } |
| 5638 | |
| 5639 | |
| 5640 | // Like compiler_pattern, but turn off checks for irrefutability. |
| 5641 | static int |
| 5642 | compiler_pattern_subpattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5643 | { |
| 5644 | int allow_irrefutable = pc->allow_irrefutable; |
| 5645 | pc->allow_irrefutable = 1; |
| 5646 | RETURN_IF_FALSE(compiler_pattern(c, p, pc)); |
| 5647 | pc->allow_irrefutable = allow_irrefutable; |
| 5648 | return 1; |
| 5649 | } |
| 5650 | |
| 5651 | |
| 5652 | static int |
| 5653 | compiler_pattern_as(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5654 | { |
| 5655 | assert(p->kind == MatchAs_kind); |
| 5656 | basicblock *end, *fail_pop_1; |
| 5657 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5658 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5659 | // Need to make a copy for (possibly) storing later: |
| 5660 | ADDOP(c, DUP_TOP); |
| 5661 | RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); |
| 5662 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5663 | NEXT_BLOCK(c); |
| 5664 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc)); |
| 5665 | ADDOP_LOAD_CONST(c, Py_True); |
| 5666 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5667 | compiler_use_next_block(c, fail_pop_1); |
| 5668 | // Need to pop that unused copy from before: |
| 5669 | ADDOP(c, POP_TOP); |
| 5670 | ADDOP_LOAD_CONST(c, Py_False); |
| 5671 | compiler_use_next_block(c, end); |
| 5672 | return 1; |
| 5673 | } |
| 5674 | |
| 5675 | |
| 5676 | static int |
| 5677 | compiler_pattern_capture(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5678 | { |
| 5679 | assert(p->kind == Name_kind); |
| 5680 | assert(p->v.Name.ctx == Store); |
| 5681 | assert(!WILDCARD_CHECK(p)); |
| 5682 | if (!pc->allow_irrefutable) { |
| 5683 | // Whoops, can't have a name capture here! |
| 5684 | const char *e = "name capture %R makes remaining patterns unreachable"; |
| 5685 | return compiler_error(c, e, p->v.Name.id); |
| 5686 | } |
| 5687 | RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.Name.id, pc)); |
| 5688 | ADDOP_LOAD_CONST(c, Py_True); |
| 5689 | return 1; |
| 5690 | } |
| 5691 | |
| 5692 | |
| 5693 | static int |
| 5694 | compiler_pattern_class(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5695 | { |
| 5696 | asdl_expr_seq *args = p->v.Call.args; |
| 5697 | asdl_keyword_seq *kwargs = p->v.Call.keywords; |
| 5698 | Py_ssize_t nargs = asdl_seq_LEN(args); |
| 5699 | Py_ssize_t nkwargs = asdl_seq_LEN(kwargs); |
| 5700 | if (INT_MAX < nargs || INT_MAX < nargs + nkwargs - 1) { |
| 5701 | const char *e = "too many sub-patterns in class pattern %R"; |
| 5702 | return compiler_error(c, e, p->v.Call.func); |
| 5703 | } |
| 5704 | RETURN_IF_FALSE(!validate_keywords(c, kwargs)); |
| 5705 | basicblock *end, *fail_pop_1; |
| 5706 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5707 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5708 | VISIT(c, expr, p->v.Call.func); |
| 5709 | PyObject *kwnames; |
| 5710 | RETURN_IF_FALSE(kwnames = PyTuple_New(nkwargs)); |
| 5711 | Py_ssize_t i; |
| 5712 | for (i = 0; i < nkwargs; i++) { |
| 5713 | PyObject *name = ((keyword_ty) asdl_seq_GET(kwargs, i))->arg; |
| 5714 | Py_INCREF(name); |
| 5715 | PyTuple_SET_ITEM(kwnames, i, name); |
| 5716 | } |
| 5717 | ADDOP_LOAD_CONST_NEW(c, kwnames); |
| 5718 | ADDOP_I(c, MATCH_CLASS, nargs); |
| 5719 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5720 | NEXT_BLOCK(c); |
| 5721 | // TOS is now a tuple of (nargs + nkwargs) attributes. |
| 5722 | for (i = 0; i < nargs + nkwargs; i++) { |
| 5723 | expr_ty arg; |
| 5724 | if (i < nargs) { |
| 5725 | // Positional: |
| 5726 | arg = asdl_seq_GET(args, i); |
| 5727 | } |
| 5728 | else { |
| 5729 | // Keyword: |
| 5730 | arg = ((keyword_ty) asdl_seq_GET(kwargs, i - nargs))->value; |
| 5731 | } |
| 5732 | if (WILDCARD_CHECK(arg)) { |
| 5733 | continue; |
| 5734 | } |
| 5735 | // Get the i-th attribute, and match it against the i-th pattern: |
| 5736 | ADDOP(c, DUP_TOP); |
| 5737 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5738 | ADDOP(c, BINARY_SUBSCR); |
| 5739 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, arg, pc)); |
| 5740 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5741 | NEXT_BLOCK(c); |
| 5742 | } |
| 5743 | // Success! Pop the tuple of attributes: |
| 5744 | ADDOP(c, POP_TOP); |
| 5745 | ADDOP_LOAD_CONST(c, Py_True); |
| 5746 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5747 | compiler_use_next_block(c, fail_pop_1); |
| 5748 | ADDOP(c, POP_TOP); |
| 5749 | ADDOP_LOAD_CONST(c, Py_False); |
| 5750 | compiler_use_next_block(c, end); |
| 5751 | return 1; |
| 5752 | } |
| 5753 | |
| 5754 | |
| 5755 | static int |
| 5756 | compiler_pattern_literal(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5757 | { |
| 5758 | assert(p->kind == Constant_kind); |
| 5759 | PyObject *v = p->v.Constant.value; |
| 5760 | ADDOP_LOAD_CONST(c, v); |
| 5761 | // Literal True, False, and None are compared by identity. All others use |
| 5762 | // equality: |
| 5763 | ADDOP_COMPARE(c, (v == Py_None || PyBool_Check(v)) ? Is : Eq); |
| 5764 | return 1; |
| 5765 | } |
| 5766 | |
| 5767 | |
| 5768 | static int |
| 5769 | compiler_pattern_mapping(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5770 | { |
| 5771 | basicblock *end, *fail_pop_1, *fail_pop_3; |
| 5772 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5773 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5774 | RETURN_IF_FALSE(fail_pop_3 = compiler_new_block(c)); |
| 5775 | asdl_expr_seq *keys = p->v.Dict.keys; |
| 5776 | asdl_expr_seq *values = p->v.Dict.values; |
| 5777 | Py_ssize_t size = asdl_seq_LEN(values); |
Ikko Ashimine | 57827f8 | 2021-03-10 19:39:51 +0900 | [diff] [blame] | 5778 | // 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] | 5779 | int star = size ? !asdl_seq_GET(keys, size - 1) : 0; |
| 5780 | ADDOP(c, MATCH_MAPPING); |
| 5781 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5782 | NEXT_BLOCK(c); |
| 5783 | if (!size) { |
| 5784 | // If the pattern is just "{}", we're done! |
| 5785 | ADDOP(c, POP_TOP); |
| 5786 | ADDOP_LOAD_CONST(c, Py_True); |
| 5787 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5788 | compiler_use_next_block(c, fail_pop_1); |
| 5789 | ADDOP(c, POP_TOP); |
| 5790 | ADDOP_LOAD_CONST(c, Py_False); |
| 5791 | compiler_use_next_block(c, end); |
| 5792 | return 1; |
| 5793 | } |
| 5794 | if (size - star) { |
| 5795 | // If the pattern has any keys in it, perform a length check: |
| 5796 | ADDOP(c, GET_LEN); |
| 5797 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - star)); |
| 5798 | ADDOP_COMPARE(c, GtE); |
| 5799 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5800 | NEXT_BLOCK(c); |
| 5801 | } |
| 5802 | if (INT_MAX < size - star - 1) { |
| 5803 | return compiler_error(c, "too many sub-patterns in mapping pattern"); |
| 5804 | } |
| 5805 | // Collect all of the keys into a tuple for MATCH_KEYS and |
| 5806 | // COPY_DICT_WITHOUT_KEYS. They can either be dotted names or literals: |
| 5807 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5808 | expr_ty key = asdl_seq_GET(keys, i); |
| 5809 | if (key == NULL) { |
| 5810 | const char *e = "can't use starred name here " |
| 5811 | "(consider moving to end)"; |
| 5812 | return compiler_error(c, e); |
| 5813 | } |
| 5814 | VISIT(c, expr, key); |
| 5815 | } |
| 5816 | ADDOP_I(c, BUILD_TUPLE, size - star); |
| 5817 | ADDOP(c, MATCH_KEYS); |
| 5818 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5819 | NEXT_BLOCK(c); |
| 5820 | // So far so good. There's now a tuple of values on the stack to match |
| 5821 | // sub-patterns against: |
| 5822 | for (Py_ssize_t i = 0; i < size - star; i++) { |
| 5823 | expr_ty value = asdl_seq_GET(values, i); |
| 5824 | if (WILDCARD_CHECK(value)) { |
| 5825 | continue; |
| 5826 | } |
| 5827 | ADDOP(c, DUP_TOP); |
| 5828 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i)); |
| 5829 | ADDOP(c, BINARY_SUBSCR); |
| 5830 | RETURN_IF_FALSE(compiler_pattern_subpattern(c, value, pc)); |
| 5831 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_3); |
| 5832 | NEXT_BLOCK(c); |
| 5833 | } |
| 5834 | // If we get this far, it's a match! We're done with that tuple of values. |
| 5835 | ADDOP(c, POP_TOP); |
| 5836 | if (star) { |
| 5837 | // If we had a starred name, bind a dict of remaining items to it: |
| 5838 | ADDOP(c, COPY_DICT_WITHOUT_KEYS); |
| 5839 | PyObject *id = asdl_seq_GET(values, size - 1)->v.Name.id; |
| 5840 | RETURN_IF_FALSE(pattern_helper_store_name(c, id, pc)); |
| 5841 | } |
| 5842 | else { |
| 5843 | // Otherwise, we don't care about this tuple of keys anymore: |
| 5844 | ADDOP(c, POP_TOP); |
| 5845 | } |
| 5846 | // Pop the subject: |
| 5847 | ADDOP(c, POP_TOP); |
| 5848 | ADDOP_LOAD_CONST(c, Py_True); |
| 5849 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5850 | // The top two items are a tuple of values or None, followed by a tuple of |
| 5851 | // keys. Pop them both: |
| 5852 | compiler_use_next_block(c, fail_pop_3); |
| 5853 | ADDOP(c, POP_TOP); |
| 5854 | ADDOP(c, POP_TOP); |
| 5855 | compiler_use_next_block(c, fail_pop_1); |
| 5856 | // Pop the subject: |
| 5857 | ADDOP(c, POP_TOP); |
| 5858 | ADDOP_LOAD_CONST(c, Py_False); |
| 5859 | compiler_use_next_block(c, end); |
| 5860 | return 1; |
| 5861 | } |
| 5862 | |
| 5863 | |
| 5864 | static int |
| 5865 | compiler_pattern_or(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5866 | { |
| 5867 | assert(p->kind == MatchOr_kind); |
| 5868 | // control is the set of names bound by the first alternative. If all of the |
| 5869 | // others bind the same names (they should), then this becomes pc->stores. |
| 5870 | PyObject *control = NULL; |
| 5871 | basicblock *end, *pass_pop_1; |
| 5872 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5873 | RETURN_IF_FALSE(pass_pop_1 = compiler_new_block(c)); |
| 5874 | Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns); |
| 5875 | assert(size > 1); |
| 5876 | // We're going to be messing with pc. Keep the original info handy: |
| 5877 | PyObject *stores_init = pc->stores; |
| 5878 | int allow_irrefutable = pc->allow_irrefutable; |
| 5879 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5880 | // NOTE: Can't use our nice returning macros in here: they'll leak sets! |
| 5881 | expr_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i); |
| 5882 | pc->stores = PySet_New(stores_init); |
| 5883 | // An irrefutable sub-pattern must be last, if it is allowed at all: |
| 5884 | int is_last = i == size - 1; |
| 5885 | pc->allow_irrefutable = allow_irrefutable && is_last; |
| 5886 | SET_LOC(c, alt); |
| 5887 | if (pc->stores == NULL || |
| 5888 | // Only copy the subject if we're *not* on the last alternative: |
| 5889 | (!is_last && !compiler_addop(c, DUP_TOP)) || |
| 5890 | !compiler_pattern(c, alt, pc) || |
| 5891 | // Only jump if we're *not* on the last alternative: |
| 5892 | (!is_last && !compiler_addop_j(c, POP_JUMP_IF_TRUE, pass_pop_1)) || |
| 5893 | !compiler_next_block(c)) |
| 5894 | { |
| 5895 | goto fail; |
| 5896 | } |
| 5897 | if (!i) { |
| 5898 | // If this is the first alternative, save its stores as a "control" |
| 5899 | // for the others (they can't bind a different set of names): |
| 5900 | control = pc->stores; |
| 5901 | continue; |
| 5902 | } |
| 5903 | if (PySet_GET_SIZE(pc->stores) || PySet_GET_SIZE(control)) { |
| 5904 | // Otherwise, check to see if we differ from the control set: |
| 5905 | PyObject *diff = PyNumber_InPlaceXor(pc->stores, control); |
| 5906 | if (diff == NULL) { |
| 5907 | goto fail; |
| 5908 | } |
| 5909 | if (PySet_GET_SIZE(diff)) { |
| 5910 | // The names differ! Raise. |
| 5911 | Py_DECREF(diff); |
| 5912 | compiler_error(c, "alternative patterns bind different names"); |
| 5913 | goto fail; |
| 5914 | } |
| 5915 | Py_DECREF(diff); |
| 5916 | } |
| 5917 | Py_DECREF(pc->stores); |
| 5918 | } |
| 5919 | Py_XDECREF(stores_init); |
| 5920 | // Update pc->stores and restore pc->allow_irrefutable: |
| 5921 | pc->stores = control; |
| 5922 | pc->allow_irrefutable = allow_irrefutable; |
| 5923 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5924 | compiler_use_next_block(c, pass_pop_1); |
| 5925 | ADDOP(c, POP_TOP); |
| 5926 | ADDOP_LOAD_CONST(c, Py_True); |
| 5927 | compiler_use_next_block(c, end); |
| 5928 | return 1; |
| 5929 | fail: |
| 5930 | Py_XDECREF(stores_init); |
| 5931 | Py_XDECREF(control); |
| 5932 | return 0; |
| 5933 | } |
| 5934 | |
| 5935 | |
| 5936 | static int |
| 5937 | compiler_pattern_sequence(struct compiler *c, expr_ty p, pattern_context *pc) |
| 5938 | { |
| 5939 | assert(p->kind == List_kind || p->kind == Tuple_kind); |
| 5940 | asdl_expr_seq *values = (p->kind == Tuple_kind) ? p->v.Tuple.elts |
| 5941 | : p->v.List.elts; |
| 5942 | Py_ssize_t size = asdl_seq_LEN(values); |
| 5943 | Py_ssize_t star = -1; |
| 5944 | int only_wildcard = 1; |
| 5945 | int star_wildcard = 0; |
| 5946 | // Find a starred name, if it exists. There may be at most one: |
| 5947 | for (Py_ssize_t i = 0; i < size; i++) { |
| 5948 | expr_ty value = asdl_seq_GET(values, i); |
| 5949 | if (value->kind == Starred_kind) { |
| 5950 | value = value->v.Starred.value; |
| 5951 | if (star >= 0) { |
| 5952 | const char *e = "multiple starred names in sequence pattern"; |
| 5953 | return compiler_error(c, e); |
| 5954 | } |
| 5955 | star_wildcard = WILDCARD_CHECK(value); |
| 5956 | star = i; |
| 5957 | } |
| 5958 | only_wildcard &= WILDCARD_CHECK(value); |
| 5959 | } |
| 5960 | basicblock *end, *fail_pop_1; |
| 5961 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 5962 | RETURN_IF_FALSE(fail_pop_1 = compiler_new_block(c)); |
| 5963 | ADDOP(c, MATCH_SEQUENCE); |
| 5964 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5965 | NEXT_BLOCK(c); |
| 5966 | if (star < 0) { |
| 5967 | // No star: len(subject) == size |
| 5968 | ADDOP(c, GET_LEN); |
| 5969 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size)); |
| 5970 | ADDOP_COMPARE(c, Eq); |
| 5971 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5972 | NEXT_BLOCK(c); |
| 5973 | } |
| 5974 | else if (size > 1) { |
| 5975 | // Star: len(subject) >= size - 1 |
| 5976 | ADDOP(c, GET_LEN); |
| 5977 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1)); |
| 5978 | ADDOP_COMPARE(c, GtE); |
| 5979 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, fail_pop_1); |
| 5980 | NEXT_BLOCK(c); |
| 5981 | } |
| 5982 | if (only_wildcard) { |
| 5983 | // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. |
| 5984 | ADDOP(c, POP_TOP); |
| 5985 | ADDOP_LOAD_CONST(c, Py_True); |
| 5986 | } |
| 5987 | else if (star_wildcard) { |
| 5988 | RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, values, star, pc)); |
| 5989 | } |
| 5990 | else { |
| 5991 | RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, values, star, pc)); |
| 5992 | } |
| 5993 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 5994 | compiler_use_next_block(c, fail_pop_1); |
| 5995 | ADDOP(c, POP_TOP) |
| 5996 | ADDOP_LOAD_CONST(c, Py_False); |
| 5997 | compiler_use_next_block(c, end); |
| 5998 | return 1; |
| 5999 | } |
| 6000 | |
| 6001 | |
| 6002 | static int |
| 6003 | compiler_pattern_value(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6004 | { |
| 6005 | assert(p->kind == Attribute_kind); |
| 6006 | assert(p->v.Attribute.ctx == Load); |
| 6007 | VISIT(c, expr, p); |
| 6008 | ADDOP_COMPARE(c, Eq); |
| 6009 | return 1; |
| 6010 | } |
| 6011 | |
| 6012 | |
| 6013 | static int |
| 6014 | compiler_pattern_wildcard(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6015 | { |
| 6016 | assert(p->kind == Name_kind); |
| 6017 | assert(p->v.Name.ctx == Store); |
| 6018 | assert(WILDCARD_CHECK(p)); |
| 6019 | if (!pc->allow_irrefutable) { |
| 6020 | // Whoops, can't have a wildcard here! |
| 6021 | const char *e = "wildcard makes remaining patterns unreachable"; |
| 6022 | return compiler_error(c, e); |
| 6023 | } |
| 6024 | ADDOP(c, POP_TOP); |
| 6025 | ADDOP_LOAD_CONST(c, Py_True); |
| 6026 | return 1; |
| 6027 | } |
| 6028 | |
| 6029 | |
| 6030 | static int |
| 6031 | compiler_pattern(struct compiler *c, expr_ty p, pattern_context *pc) |
| 6032 | { |
| 6033 | SET_LOC(c, p); |
| 6034 | switch (p->kind) { |
| 6035 | case Attribute_kind: |
| 6036 | return compiler_pattern_value(c, p, pc); |
| 6037 | case BinOp_kind: |
| 6038 | // Because we allow "2+2j", things like "2+2" make it this far: |
| 6039 | return compiler_error(c, "patterns cannot include operators"); |
| 6040 | case Call_kind: |
| 6041 | return compiler_pattern_class(c, p, pc); |
| 6042 | case Constant_kind: |
| 6043 | return compiler_pattern_literal(c, p, pc); |
| 6044 | case Dict_kind: |
| 6045 | return compiler_pattern_mapping(c, p, pc); |
| 6046 | case JoinedStr_kind: |
| 6047 | // Because we allow strings, f-strings make it this far: |
| 6048 | return compiler_error(c, "patterns cannot include f-strings"); |
| 6049 | case List_kind: |
| 6050 | case Tuple_kind: |
| 6051 | return compiler_pattern_sequence(c, p, pc); |
| 6052 | case MatchAs_kind: |
| 6053 | return compiler_pattern_as(c, p, pc); |
| 6054 | case MatchOr_kind: |
| 6055 | return compiler_pattern_or(c, p, pc); |
| 6056 | case Name_kind: |
| 6057 | if (WILDCARD_CHECK(p)) { |
| 6058 | return compiler_pattern_wildcard(c, p, pc); |
| 6059 | } |
| 6060 | return compiler_pattern_capture(c, p, pc); |
| 6061 | default: |
| 6062 | Py_UNREACHABLE(); |
| 6063 | } |
| 6064 | } |
| 6065 | |
| 6066 | |
| 6067 | static int |
| 6068 | compiler_match(struct compiler *c, stmt_ty s) |
| 6069 | { |
| 6070 | VISIT(c, expr, s->v.Match.subject); |
| 6071 | basicblock *next, *end; |
| 6072 | RETURN_IF_FALSE(end = compiler_new_block(c)); |
| 6073 | Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); |
| 6074 | assert(cases); |
| 6075 | pattern_context pc; |
| 6076 | // We use pc.stores to track: |
| 6077 | // - Repeated name assignments in the same pattern. |
| 6078 | // - Different name assignments in alternatives. |
| 6079 | // It's a set of names, but we don't create it until it's needed: |
| 6080 | pc.stores = NULL; |
| 6081 | match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6082 | int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases; |
| 6083 | for (Py_ssize_t i = 0; i < cases - has_default; i++) { |
| 6084 | m = asdl_seq_GET(s->v.Match.cases, i); |
| 6085 | SET_LOC(c, m->pattern); |
| 6086 | RETURN_IF_FALSE(next = compiler_new_block(c)); |
| 6087 | // If pc.allow_irrefutable is 0, any name captures against our subject |
| 6088 | // will raise. Irrefutable cases must be either guarded, last, or both: |
| 6089 | pc.allow_irrefutable = m->guard != NULL || i == cases - 1; |
| 6090 | // Only copy the subject if we're *not* on the last case: |
| 6091 | if (i != cases - has_default - 1) { |
| 6092 | ADDOP(c, DUP_TOP); |
| 6093 | } |
| 6094 | int result = compiler_pattern(c, m->pattern, &pc); |
| 6095 | Py_CLEAR(pc.stores); |
| 6096 | RETURN_IF_FALSE(result); |
| 6097 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, next); |
| 6098 | NEXT_BLOCK(c); |
| 6099 | if (m->guard) { |
| 6100 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, next, 0)); |
| 6101 | } |
| 6102 | // Success! Pop the subject off, we're done with it: |
| 6103 | if (i != cases - has_default - 1) { |
| 6104 | ADDOP(c, POP_TOP); |
| 6105 | } |
| 6106 | VISIT_SEQ(c, stmt, m->body); |
| 6107 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 6108 | compiler_use_next_block(c, next); |
| 6109 | } |
| 6110 | if (has_default) { |
| 6111 | if (cases == 1) { |
| 6112 | // No matches. Done with the subject: |
| 6113 | ADDOP(c, POP_TOP); |
| 6114 | } |
| 6115 | // A trailing "case _" is common, and lets us save a bit of redundant |
| 6116 | // pushing and popping in the loop above: |
| 6117 | m = asdl_seq_GET(s->v.Match.cases, cases - 1); |
| 6118 | SET_LOC(c, m->pattern); |
| 6119 | if (m->guard) { |
| 6120 | RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0)); |
| 6121 | } |
| 6122 | VISIT_SEQ(c, stmt, m->body); |
| 6123 | } |
| 6124 | compiler_use_next_block(c, end); |
| 6125 | return 1; |
| 6126 | } |
| 6127 | |
| 6128 | |
| 6129 | #undef WILDCARD_CHECK |
| 6130 | |
| 6131 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6132 | /* End of the compiler section, beginning of the assembler section */ |
| 6133 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6134 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6135 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6136 | |
| 6137 | XXX must handle implicit jumps from one block to next |
| 6138 | */ |
| 6139 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6140 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6141 | PyObject *a_bytecode; /* string containing bytecode */ |
| 6142 | int a_offset; /* offset into bytecode */ |
| 6143 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6144 | PyObject *a_lnotab; /* string containing lnotab */ |
| 6145 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6146 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 6147 | int a_lineno; /* lineno of last emitted instruction */ |
| 6148 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6149 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6150 | }; |
| 6151 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6152 | Py_LOCAL_INLINE(void) |
| 6153 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6154 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 6155 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6156 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6157 | assert(b->b_startdepth < 0); |
| 6158 | b->b_startdepth = depth; |
| 6159 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 6160 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | /* Find the flow path that needs the largest stack. We assume that |
| 6164 | * cycles in the flow graph have no net effect on the stack depth. |
| 6165 | */ |
| 6166 | static int |
| 6167 | stackdepth(struct compiler *c) |
| 6168 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6169 | basicblock *b, *entryblock = NULL; |
| 6170 | basicblock **stack, **sp; |
| 6171 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6172 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6173 | b->b_startdepth = INT_MIN; |
| 6174 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6175 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6176 | } |
| 6177 | if (!entryblock) |
| 6178 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6179 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 6180 | if (!stack) { |
| 6181 | PyErr_NoMemory(); |
| 6182 | return -1; |
| 6183 | } |
| 6184 | |
| 6185 | sp = stack; |
| 6186 | stackdepth_push(&sp, entryblock, 0); |
| 6187 | while (sp != stack) { |
| 6188 | b = *--sp; |
| 6189 | int depth = b->b_startdepth; |
| 6190 | assert(depth >= 0); |
| 6191 | basicblock *next = b->b_next; |
| 6192 | for (int i = 0; i < b->b_iused; i++) { |
| 6193 | struct instr *instr = &b->b_instr[i]; |
| 6194 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 6195 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 6196 | PyErr_Format(PyExc_SystemError, |
| 6197 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 6198 | instr->i_opcode, instr->i_oparg); |
| 6199 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6200 | } |
| 6201 | int new_depth = depth + effect; |
| 6202 | if (new_depth > maxdepth) { |
| 6203 | maxdepth = new_depth; |
| 6204 | } |
| 6205 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6206 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6207 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 6208 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 6209 | int target_depth = depth + effect; |
| 6210 | if (target_depth > maxdepth) { |
| 6211 | maxdepth = target_depth; |
| 6212 | } |
| 6213 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6214 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 6215 | } |
| 6216 | depth = new_depth; |
| 6217 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 6218 | instr->i_opcode == JUMP_FORWARD || |
| 6219 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 6220 | instr->i_opcode == RAISE_VARARGS || |
| 6221 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6222 | { |
| 6223 | /* remaining code is dead */ |
| 6224 | next = NULL; |
| 6225 | break; |
| 6226 | } |
| 6227 | } |
| 6228 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6229 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6230 | stackdepth_push(&sp, next, depth); |
| 6231 | } |
| 6232 | } |
| 6233 | PyObject_Free(stack); |
| 6234 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | static int |
| 6238 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 6239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6240 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6241 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6242 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6243 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6244 | if (a->a_bytecode == NULL) { |
| 6245 | goto error; |
| 6246 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6247 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6248 | if (a->a_lnotab == NULL) { |
| 6249 | goto error; |
| 6250 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 6251 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6252 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6253 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6254 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6255 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 6256 | error: |
| 6257 | Py_XDECREF(a->a_bytecode); |
| 6258 | Py_XDECREF(a->a_lnotab); |
| 6259 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
| 6262 | static void |
| 6263 | assemble_free(struct assembler *a) |
| 6264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6265 | Py_XDECREF(a->a_bytecode); |
| 6266 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6269 | static int |
| 6270 | blocksize(basicblock *b) |
| 6271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6272 | int i; |
| 6273 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6275 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6276 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6277 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6278 | } |
| 6279 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 6280 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6281 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6282 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6283 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6284 | if (a->a_lnotab_off + 2 >= len) { |
| 6285 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 6286 | return 0; |
| 6287 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 6288 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 6289 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6290 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6291 | *lnotab++ = bdelta; |
| 6292 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6293 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6296 | /* Appends a range to the end of the line number table. See |
| 6297 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 6298 | |
| 6299 | static int |
| 6300 | assemble_line_range(struct assembler *a) |
| 6301 | { |
| 6302 | int ldelta, bdelta; |
| 6303 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 6304 | if (bdelta == 0) { |
| 6305 | return 1; |
| 6306 | } |
| 6307 | if (a->a_lineno < 0) { |
| 6308 | ldelta = -128; |
| 6309 | } |
| 6310 | else { |
| 6311 | ldelta = a->a_lineno - a->a_prevlineno; |
| 6312 | a->a_prevlineno = a->a_lineno; |
| 6313 | while (ldelta > 127) { |
| 6314 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 6315 | return 0; |
| 6316 | } |
| 6317 | ldelta -= 127; |
| 6318 | } |
| 6319 | while (ldelta < -127) { |
| 6320 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 6321 | return 0; |
| 6322 | } |
| 6323 | ldelta += 127; |
| 6324 | } |
| 6325 | } |
| 6326 | assert(-128 <= ldelta && ldelta < 128); |
| 6327 | while (bdelta > 254) { |
| 6328 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 6329 | return 0; |
| 6330 | } |
| 6331 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 6332 | bdelta -= 254; |
| 6333 | } |
| 6334 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 6335 | return 0; |
| 6336 | } |
| 6337 | a->a_lineno_start = a->a_offset; |
| 6338 | return 1; |
| 6339 | } |
| 6340 | |
| 6341 | static int |
| 6342 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 6343 | { |
| 6344 | if (i->i_lineno == a->a_lineno) { |
| 6345 | return 1; |
| 6346 | } |
| 6347 | if (!assemble_line_range(a)) { |
| 6348 | return 0; |
| 6349 | } |
| 6350 | a->a_lineno = i->i_lineno; |
| 6351 | return 1; |
| 6352 | } |
| 6353 | |
| 6354 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6355 | /* assemble_emit() |
| 6356 | Extend the bytecode with a new instruction. |
| 6357 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 6358 | */ |
| 6359 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6360 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6361 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 6362 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6363 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6364 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6365 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6366 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6367 | arg = i->i_oparg; |
| 6368 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6369 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 6370 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6371 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6372 | if (len > PY_SSIZE_T_MAX / 2) |
| 6373 | return 0; |
| 6374 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 6375 | return 0; |
| 6376 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6377 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6378 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6379 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6380 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 6383 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6384 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 6385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6386 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6387 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6388 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 6389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6390 | /* Compute the size of each block and fixup jump args. |
| 6391 | Replace block pointer with position in bytecode. */ |
| 6392 | do { |
| 6393 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6394 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6395 | bsize = blocksize(b); |
| 6396 | b->b_offset = totsize; |
| 6397 | totsize += bsize; |
| 6398 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6399 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6400 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6401 | bsize = b->b_offset; |
| 6402 | for (i = 0; i < b->b_iused; i++) { |
| 6403 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6404 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6405 | /* Relative jumps are computed relative to |
| 6406 | the instruction pointer after fetching |
| 6407 | the jump instruction. |
| 6408 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6409 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6410 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6411 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6412 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6413 | instr->i_oparg -= bsize; |
| 6414 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6415 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6416 | if (instrsize(instr->i_oparg) != isize) { |
| 6417 | extended_arg_recompile = 1; |
| 6418 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6419 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6420 | } |
| 6421 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6423 | /* XXX: This is an awful hack that could hurt performance, but |
| 6424 | on the bright side it should work until we come up |
| 6425 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6427 | The issue is that in the first loop blocksize() is called |
| 6428 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6429 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6430 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 6431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6432 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 6433 | The only EXTENDED_ARGs that could be popping up are |
| 6434 | ones in jump instructions. So this should converge |
| 6435 | fairly quickly. |
| 6436 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6437 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6440 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6441 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6443 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6444 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6446 | tuple = PyTuple_New(size); |
| 6447 | if (tuple == NULL) |
| 6448 | return NULL; |
| 6449 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6450 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6451 | Py_INCREF(k); |
| 6452 | assert((i - offset) < size); |
| 6453 | assert((i - offset) >= 0); |
| 6454 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 6455 | } |
| 6456 | return tuple; |
| 6457 | } |
| 6458 | |
| 6459 | static PyObject * |
| 6460 | consts_dict_keys_inorder(PyObject *dict) |
| 6461 | { |
| 6462 | PyObject *consts, *k, *v; |
| 6463 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 6464 | |
| 6465 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 6466 | if (consts == NULL) |
| 6467 | return NULL; |
| 6468 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 6469 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 6470 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 6471 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 6472 | * the object we want is always second. */ |
| 6473 | if (PyTuple_CheckExact(k)) { |
| 6474 | k = PyTuple_GET_ITEM(k, 1); |
| 6475 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6476 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6477 | assert(i < size); |
| 6478 | assert(i >= 0); |
| 6479 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6480 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6481 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6484 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6485 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6487 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6488 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6489 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 6490 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6491 | if (ste->ste_nested) |
| 6492 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6493 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6494 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 6495 | if (!ste->ste_generator && ste->ste_coroutine) |
| 6496 | flags |= CO_COROUTINE; |
| 6497 | if (ste->ste_generator && ste->ste_coroutine) |
| 6498 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6499 | if (ste->ste_varargs) |
| 6500 | flags |= CO_VARARGS; |
| 6501 | if (ste->ste_varkeywords) |
| 6502 | flags |= CO_VARKEYWORDS; |
| 6503 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6505 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 6506 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 6507 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 6508 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 6509 | ste->ste_coroutine && |
| 6510 | !ste->ste_generator) { |
| 6511 | flags |= CO_COROUTINE; |
| 6512 | } |
| 6513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6514 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6517 | // Merge *obj* with constant cache. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6518 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 6519 | static int |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6520 | merge_const_one(struct compiler *c, PyObject **obj) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6521 | { |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6522 | PyObject *key = _PyCode_ConstantKey(*obj); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6523 | if (key == NULL) { |
| 6524 | return 0; |
| 6525 | } |
| 6526 | |
| 6527 | // t is borrowed reference |
| 6528 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 6529 | Py_DECREF(key); |
| 6530 | if (t == NULL) { |
| 6531 | return 0; |
| 6532 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6533 | if (t == key) { // obj is new constant. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6534 | return 1; |
| 6535 | } |
| 6536 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6537 | if (PyTuple_CheckExact(t)) { |
| 6538 | // t is still borrowed reference |
| 6539 | t = PyTuple_GET_ITEM(t, 1); |
| 6540 | } |
| 6541 | |
| 6542 | Py_INCREF(t); |
| 6543 | Py_DECREF(*obj); |
| 6544 | *obj = t; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6545 | return 1; |
| 6546 | } |
| 6547 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6548 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6549 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6550 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6551 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6552 | PyObject *names = NULL; |
| 6553 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6554 | PyObject *name = NULL; |
| 6555 | PyObject *freevars = NULL; |
| 6556 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6557 | Py_ssize_t nlocals; |
| 6558 | int nlocals_int; |
| 6559 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6560 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6562 | names = dict_keys_inorder(c->u->u_names, 0); |
| 6563 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6564 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6565 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6566 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6567 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 6568 | if (!cellvars) |
| 6569 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6570 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6571 | if (!freevars) |
| 6572 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6573 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6574 | if (!merge_const_one(c, &names) || |
| 6575 | !merge_const_one(c, &varnames) || |
| 6576 | !merge_const_one(c, &cellvars) || |
| 6577 | !merge_const_one(c, &freevars)) |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6578 | { |
| 6579 | goto error; |
| 6580 | } |
| 6581 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6582 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 6583 | assert(nlocals < INT_MAX); |
| 6584 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 6585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6586 | flags = compute_code_flags(c); |
| 6587 | if (flags < 0) |
| 6588 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6589 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6590 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 6591 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6592 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6593 | } |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6594 | if (!merge_const_one(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6595 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 6596 | goto error; |
| 6597 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6598 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 6599 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 6600 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 6601 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6602 | maxdepth = stackdepth(c); |
| 6603 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6604 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6605 | goto error; |
| 6606 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6607 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 6608 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6609 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 6610 | varnames, freevars, cellvars, c->c_filename, |
| 6611 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6612 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6613 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6614 | Py_XDECREF(names); |
| 6615 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6616 | Py_XDECREF(name); |
| 6617 | Py_XDECREF(freevars); |
| 6618 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6619 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6622 | |
| 6623 | /* For debugging purposes only */ |
| 6624 | #if 0 |
| 6625 | static void |
| 6626 | dump_instr(const struct instr *i) |
| 6627 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6628 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 6629 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6630 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6632 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6633 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6634 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 6635 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6636 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 6637 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | static void |
| 6641 | dump_basicblock(const basicblock *b) |
| 6642 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6643 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6644 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 6645 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6646 | if (b->b_instr) { |
| 6647 | int i; |
| 6648 | for (i = 0; i < b->b_iused; i++) { |
| 6649 | fprintf(stderr, " [%02d] ", i); |
| 6650 | dump_instr(b->b_instr + i); |
| 6651 | } |
| 6652 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6653 | } |
| 6654 | #endif |
| 6655 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6656 | |
| 6657 | static int |
| 6658 | normalize_basic_block(basicblock *bb); |
| 6659 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6660 | static int |
| 6661 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 6662 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6663 | static int |
| 6664 | ensure_exits_have_lineno(struct compiler *c); |
| 6665 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6666 | static PyCodeObject * |
| 6667 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6669 | basicblock *b, *entryblock; |
| 6670 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6671 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6672 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6673 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6675 | /* Make sure every block that falls off the end returns None. |
| 6676 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6677 | block ends with a jump or return b_next shouldn't set. |
| 6678 | */ |
| 6679 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6680 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6681 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6682 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6683 | ADDOP(c, RETURN_VALUE); |
| 6684 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6685 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6686 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6687 | if (normalize_basic_block(b)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6688 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6689 | } |
| 6690 | } |
| 6691 | |
| 6692 | if (ensure_exits_have_lineno(c)) { |
Alex Henrie | 503627f | 2021-03-02 03:20:25 -0700 | [diff] [blame] | 6693 | return NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6696 | nblocks = 0; |
| 6697 | entryblock = NULL; |
| 6698 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6699 | nblocks++; |
| 6700 | entryblock = b; |
| 6701 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6703 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6704 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 6705 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6706 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6707 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6708 | c->u->u_firstlineno = 1; |
| 6709 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6711 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6712 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6713 | a.a_entry = entryblock; |
| 6714 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6715 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6716 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6717 | if (consts == NULL) { |
| 6718 | goto error; |
| 6719 | } |
| 6720 | if (optimize_cfg(&a, consts)) { |
| 6721 | goto error; |
| 6722 | } |
| 6723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6724 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6725 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6726 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6727 | /* Emit code. */ |
| 6728 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6729 | for (j = 0; j < b->b_iused; j++) |
| 6730 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6731 | goto error; |
| 6732 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6733 | if (!assemble_line_range(&a)) { |
| 6734 | return 0; |
| 6735 | } |
| 6736 | /* Emit sentinel at end of line number table */ |
| 6737 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6738 | goto error; |
| 6739 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6740 | |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6741 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6742 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6743 | } |
| 6744 | if (!merge_const_one(c, &a.a_lnotab)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6745 | goto error; |
Inada Naoki | bdb941b | 2021-02-10 09:20:42 +0900 | [diff] [blame] | 6746 | } |
| 6747 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) { |
| 6748 | goto error; |
| 6749 | } |
| 6750 | if (!merge_const_one(c, &a.a_bytecode)) { |
| 6751 | goto error; |
| 6752 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6753 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6754 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6755 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6756 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6757 | assemble_free(&a); |
| 6758 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6759 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6760 | |
| 6761 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6762 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6763 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6764 | PyArena *arena) |
| 6765 | { |
| 6766 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6767 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6768 | |
| 6769 | |
| 6770 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6771 | with LOAD_CONST (c1, c2, ... cn). |
| 6772 | The consts table must still be in list form so that the |
| 6773 | new constant (c1, c2, ... cn) can be appended. |
| 6774 | Called with codestr pointing to the first LOAD_CONST. |
| 6775 | */ |
| 6776 | static int |
| 6777 | fold_tuple_on_constants(struct instr *inst, |
| 6778 | int n, PyObject *consts) |
| 6779 | { |
| 6780 | /* Pre-conditions */ |
| 6781 | assert(PyList_CheckExact(consts)); |
| 6782 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6783 | assert(inst[n].i_oparg == n); |
| 6784 | |
| 6785 | for (int i = 0; i < n; i++) { |
| 6786 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6787 | return 0; |
| 6788 | } |
| 6789 | } |
| 6790 | |
| 6791 | /* Buildup new tuple of constants */ |
| 6792 | PyObject *newconst = PyTuple_New(n); |
| 6793 | if (newconst == NULL) { |
| 6794 | return -1; |
| 6795 | } |
| 6796 | for (int i = 0; i < n; i++) { |
| 6797 | int arg = inst[i].i_oparg; |
| 6798 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6799 | Py_INCREF(constant); |
| 6800 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6801 | } |
| 6802 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6803 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6804 | Py_DECREF(newconst); |
| 6805 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6806 | return -1; |
| 6807 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6808 | if (PyList_Append(consts, newconst)) { |
| 6809 | Py_DECREF(newconst); |
| 6810 | return -1; |
| 6811 | } |
| 6812 | Py_DECREF(newconst); |
| 6813 | for (int i = 0; i < n; i++) { |
| 6814 | inst[i].i_opcode = NOP; |
| 6815 | } |
| 6816 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6817 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6818 | return 0; |
| 6819 | } |
| 6820 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6821 | |
| 6822 | static int |
| 6823 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 6824 | assert (bb->b_iused > 0); |
| 6825 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 6826 | assert (is_jump(inst)); |
| 6827 | assert (inst->i_target->b_iused > 0); |
| 6828 | struct instr *target = &inst->i_target->b_instr[0]; |
| 6829 | if (inst->i_target == target->i_target) { |
| 6830 | /* Nothing to do */ |
| 6831 | return 0; |
| 6832 | } |
| 6833 | int lineno = target->i_lineno; |
| 6834 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 6835 | return -1; |
| 6836 | } |
| 6837 | assert (bb->b_iused >= 2); |
| 6838 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 6839 | return 0; |
| 6840 | } |
| 6841 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6842 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6843 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6844 | |
| 6845 | /* Optimization */ |
| 6846 | static int |
| 6847 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6848 | { |
| 6849 | assert(PyList_CheckExact(consts)); |
| 6850 | struct instr nop; |
| 6851 | nop.i_opcode = NOP; |
| 6852 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6853 | for (int i = 0; i < bb->b_iused; i++) { |
| 6854 | struct instr *inst = &bb->b_instr[i]; |
| 6855 | int oparg = inst->i_oparg; |
| 6856 | 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] | 6857 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6858 | /* Skip over empty basic blocks. */ |
| 6859 | while (inst->i_target->b_iused == 0) { |
| 6860 | inst->i_target = inst->i_target->b_next; |
| 6861 | } |
| 6862 | target = &inst->i_target->b_instr[0]; |
| 6863 | } |
| 6864 | else { |
| 6865 | target = &nop; |
| 6866 | } |
| 6867 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6868 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6869 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6870 | { |
| 6871 | PyObject* cnt; |
| 6872 | int is_true; |
| 6873 | int jump_if_true; |
| 6874 | switch(nextop) { |
| 6875 | case POP_JUMP_IF_FALSE: |
| 6876 | case POP_JUMP_IF_TRUE: |
| 6877 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6878 | is_true = PyObject_IsTrue(cnt); |
| 6879 | if (is_true == -1) { |
| 6880 | goto error; |
| 6881 | } |
| 6882 | inst->i_opcode = NOP; |
| 6883 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6884 | if (is_true == jump_if_true) { |
| 6885 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6886 | bb->b_nofallthrough = 1; |
| 6887 | } |
| 6888 | else { |
| 6889 | bb->b_instr[i+1].i_opcode = NOP; |
| 6890 | } |
| 6891 | break; |
| 6892 | case JUMP_IF_FALSE_OR_POP: |
| 6893 | case JUMP_IF_TRUE_OR_POP: |
| 6894 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6895 | is_true = PyObject_IsTrue(cnt); |
| 6896 | if (is_true == -1) { |
| 6897 | goto error; |
| 6898 | } |
| 6899 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6900 | if (is_true == jump_if_true) { |
| 6901 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6902 | bb->b_nofallthrough = 1; |
| 6903 | } |
| 6904 | else { |
| 6905 | inst->i_opcode = NOP; |
| 6906 | bb->b_instr[i+1].i_opcode = NOP; |
| 6907 | } |
| 6908 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6909 | } |
| 6910 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6911 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6912 | |
| 6913 | /* Try to fold tuples of constants. |
| 6914 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6915 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6916 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6917 | case BUILD_TUPLE: |
| 6918 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6919 | switch(oparg) { |
| 6920 | case 1: |
| 6921 | inst->i_opcode = NOP; |
| 6922 | bb->b_instr[i+1].i_opcode = NOP; |
| 6923 | break; |
| 6924 | case 2: |
| 6925 | inst->i_opcode = ROT_TWO; |
| 6926 | bb->b_instr[i+1].i_opcode = NOP; |
| 6927 | break; |
| 6928 | case 3: |
| 6929 | inst->i_opcode = ROT_THREE; |
| 6930 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6931 | } |
| 6932 | break; |
| 6933 | } |
| 6934 | if (i >= oparg) { |
| 6935 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6936 | goto error; |
| 6937 | } |
| 6938 | } |
| 6939 | break; |
| 6940 | |
| 6941 | /* Simplify conditional jump to conditional jump where the |
| 6942 | result of the first test implies the success of a similar |
| 6943 | test or the failure of the opposite test. |
| 6944 | Arises in code like: |
| 6945 | "a and b or c" |
| 6946 | "(a and b) and c" |
| 6947 | "(a or b) or c" |
| 6948 | "(a or b) and c" |
| 6949 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6950 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6951 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6952 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6953 | where y+1 is the instruction following the second test. |
| 6954 | */ |
| 6955 | case JUMP_IF_FALSE_OR_POP: |
| 6956 | switch(target->i_opcode) { |
| 6957 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6958 | if (inst->i_lineno == target->i_lineno) { |
| 6959 | *inst = *target; |
| 6960 | i--; |
| 6961 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6962 | break; |
| 6963 | case JUMP_ABSOLUTE: |
| 6964 | case JUMP_FORWARD: |
| 6965 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6966 | if (inst->i_lineno == target->i_lineno && |
| 6967 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6968 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6969 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6970 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6971 | break; |
| 6972 | case JUMP_IF_TRUE_OR_POP: |
| 6973 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6974 | if (inst->i_lineno == target->i_lineno) { |
| 6975 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6976 | inst->i_target = inst->i_target->b_next; |
| 6977 | --i; |
| 6978 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6979 | break; |
| 6980 | } |
| 6981 | break; |
| 6982 | |
| 6983 | case JUMP_IF_TRUE_OR_POP: |
| 6984 | switch(target->i_opcode) { |
| 6985 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6986 | if (inst->i_lineno == target->i_lineno) { |
| 6987 | *inst = *target; |
| 6988 | i--; |
| 6989 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6990 | break; |
| 6991 | case JUMP_ABSOLUTE: |
| 6992 | case JUMP_FORWARD: |
| 6993 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6994 | if (inst->i_lineno == target->i_lineno && |
| 6995 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6996 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6997 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6998 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6999 | break; |
| 7000 | case JUMP_IF_FALSE_OR_POP: |
| 7001 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7002 | if (inst->i_lineno == target->i_lineno) { |
| 7003 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 7004 | inst->i_target = inst->i_target->b_next; |
| 7005 | --i; |
| 7006 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7007 | break; |
| 7008 | } |
| 7009 | break; |
| 7010 | |
| 7011 | case POP_JUMP_IF_FALSE: |
| 7012 | switch(target->i_opcode) { |
| 7013 | case JUMP_ABSOLUTE: |
| 7014 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7015 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7016 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7017 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7018 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7019 | break; |
| 7020 | } |
| 7021 | break; |
| 7022 | |
| 7023 | case POP_JUMP_IF_TRUE: |
| 7024 | switch(target->i_opcode) { |
| 7025 | case JUMP_ABSOLUTE: |
| 7026 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7027 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7028 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7029 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7030 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7031 | break; |
| 7032 | } |
| 7033 | break; |
| 7034 | |
| 7035 | case JUMP_ABSOLUTE: |
| 7036 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7037 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7038 | switch(target->i_opcode) { |
| 7039 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7040 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 7041 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7042 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7043 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7044 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7045 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7046 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 7047 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7048 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7049 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 7050 | default: |
| 7051 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 7052 | basicblock *to_copy = inst->i_target; |
| 7053 | inst->i_opcode = NOP; |
| 7054 | for (i = 0; i < to_copy->b_iused; i++) { |
| 7055 | int index = compiler_next_instr(bb); |
| 7056 | if (index < 0) { |
| 7057 | return -1; |
| 7058 | } |
| 7059 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 7060 | } |
| 7061 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7062 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7063 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7064 | } |
| 7065 | } |
| 7066 | return 0; |
| 7067 | error: |
| 7068 | return -1; |
| 7069 | } |
| 7070 | |
| 7071 | |
| 7072 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7073 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 7074 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7075 | int dest = 0; |
| 7076 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7077 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7078 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7079 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7080 | if (lineno < 0) { |
| 7081 | continue; |
| 7082 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7083 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7084 | if (prev_lineno == lineno) { |
| 7085 | continue; |
| 7086 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7087 | /* 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] | 7088 | if (src < bb->b_iused - 1) { |
| 7089 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 7090 | if (next_lineno < 0 || next_lineno == lineno) { |
| 7091 | bb->b_instr[src+1].i_lineno = lineno; |
| 7092 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 7093 | } |
| 7094 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7095 | else { |
| 7096 | basicblock* next = bb->b_next; |
| 7097 | while (next && next->b_iused == 0) { |
| 7098 | next = next->b_next; |
| 7099 | } |
| 7100 | /* or if last instruction in BB and next BB has same line number */ |
| 7101 | if (next) { |
| 7102 | if (lineno == next->b_instr[0].i_lineno) { |
| 7103 | continue; |
| 7104 | } |
| 7105 | } |
| 7106 | } |
| 7107 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7108 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7109 | if (dest != src) { |
| 7110 | bb->b_instr[dest] = bb->b_instr[src]; |
| 7111 | } |
| 7112 | dest++; |
| 7113 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7114 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7115 | assert(dest <= bb->b_iused); |
| 7116 | bb->b_iused = dest; |
| 7117 | } |
| 7118 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7119 | static int |
| 7120 | normalize_basic_block(basicblock *bb) { |
| 7121 | /* Mark blocks as exit and/or nofallthrough. |
| 7122 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7123 | for (int i = 0; i < bb->b_iused; i++) { |
| 7124 | switch(bb->b_instr[i].i_opcode) { |
| 7125 | case RETURN_VALUE: |
| 7126 | case RAISE_VARARGS: |
| 7127 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7128 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7129 | bb->b_nofallthrough = 1; |
| 7130 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7131 | case JUMP_ABSOLUTE: |
| 7132 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7133 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7134 | /* fall through */ |
| 7135 | case POP_JUMP_IF_FALSE: |
| 7136 | case POP_JUMP_IF_TRUE: |
| 7137 | case JUMP_IF_FALSE_OR_POP: |
| 7138 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7139 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7140 | if (i != bb->b_iused-1) { |
| 7141 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 7142 | return -1; |
| 7143 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7144 | /* Skip over empty basic blocks. */ |
| 7145 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 7146 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 7147 | } |
| 7148 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7149 | } |
| 7150 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 7151 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7152 | } |
| 7153 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7154 | static int |
| 7155 | mark_reachable(struct assembler *a) { |
| 7156 | basicblock **stack, **sp; |
| 7157 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 7158 | if (stack == NULL) { |
| 7159 | return -1; |
| 7160 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7161 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7162 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7163 | while (sp > stack) { |
| 7164 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7165 | if (b->b_next && !b->b_nofallthrough) { |
| 7166 | if (b->b_next->b_predecessors == 0) { |
| 7167 | *sp++ = b->b_next; |
| 7168 | } |
| 7169 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7170 | } |
| 7171 | for (int i = 0; i < b->b_iused; i++) { |
| 7172 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 7173 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7174 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7175 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7176 | *sp++ = target; |
| 7177 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7178 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7179 | } |
| 7180 | } |
| 7181 | } |
| 7182 | PyObject_Free(stack); |
| 7183 | return 0; |
| 7184 | } |
| 7185 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7186 | static void |
| 7187 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 7188 | /* Eliminate empty blocks */ |
| 7189 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7190 | basicblock *next = b->b_next; |
| 7191 | if (next) { |
| 7192 | while (next->b_iused == 0 && next->b_next) { |
| 7193 | next = next->b_next; |
| 7194 | } |
| 7195 | b->b_next = next; |
| 7196 | } |
| 7197 | } |
| 7198 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 7199 | if (b->b_iused == 0) { |
| 7200 | continue; |
| 7201 | } |
| 7202 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7203 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7204 | while (target->b_iused == 0) { |
| 7205 | target = target->b_next; |
| 7206 | } |
| 7207 | b->b_instr[b->b_iused-1].i_target = target; |
| 7208 | } |
| 7209 | } |
| 7210 | } |
| 7211 | |
| 7212 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7213 | /* 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] | 7214 | * then copy the line number. If a successor block has no line number, and only |
| 7215 | * one predecessor, then inherit the line number. |
| 7216 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 7217 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7218 | * but has no impact on the generated line number events. |
| 7219 | */ |
| 7220 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7221 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7222 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7223 | if (b->b_iused == 0) { |
| 7224 | continue; |
| 7225 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7226 | int prev_lineno = -1; |
| 7227 | for (int i = 0; i < b->b_iused; i++) { |
| 7228 | if (b->b_instr[i].i_lineno < 0) { |
| 7229 | b->b_instr[i].i_lineno = prev_lineno; |
| 7230 | } |
| 7231 | else { |
| 7232 | prev_lineno = b->b_instr[i].i_lineno; |
| 7233 | } |
| 7234 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7235 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 7236 | assert(b->b_next->b_iused); |
| 7237 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 7238 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 7239 | } |
| 7240 | } |
| 7241 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 7242 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7243 | /* Note: Only actual jumps, not exception handlers */ |
| 7244 | case SETUP_ASYNC_WITH: |
| 7245 | case SETUP_WITH: |
| 7246 | case SETUP_FINALLY: |
| 7247 | continue; |
| 7248 | } |
| 7249 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7250 | if (target->b_predecessors == 1) { |
| 7251 | if (target->b_instr[0].i_lineno < 0) { |
| 7252 | target->b_instr[0].i_lineno = prev_lineno; |
| 7253 | } |
| 7254 | } |
| 7255 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7256 | } |
| 7257 | } |
| 7258 | |
| 7259 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7260 | The consts object should still be in list form to allow new constants |
| 7261 | to be appended. |
| 7262 | |
| 7263 | All transformations keep the code size the same or smaller. |
| 7264 | For those that reduce size, the gaps are initially filled with |
| 7265 | NOPs. Later those NOPs are removed. |
| 7266 | */ |
| 7267 | |
| 7268 | static int |
| 7269 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 7270 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7271 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7272 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7273 | return -1; |
| 7274 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7275 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7276 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7277 | } |
| 7278 | if (mark_reachable(a)) { |
| 7279 | return -1; |
| 7280 | } |
| 7281 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7282 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7283 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 7284 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7285 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7286 | } |
| 7287 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 7288 | basicblock *pred = NULL; |
| 7289 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7290 | int prev_lineno = -1; |
| 7291 | if (pred && pred->b_iused) { |
| 7292 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 7293 | } |
| 7294 | clean_basic_block(b, prev_lineno); |
| 7295 | pred = b->b_nofallthrough ? NULL : b; |
| 7296 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7297 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7298 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 7299 | block ends with a jump instruction, check if the next non-empty block |
| 7300 | reached through normal flow control is the target of that jump. If it |
| 7301 | is, then the jump instruction is redundant and can be deleted. |
| 7302 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7303 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7304 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 7305 | if (b->b_iused > 0) { |
| 7306 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7307 | if (b_last_instr->i_opcode == JUMP_ABSOLUTE || |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7308 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7309 | if (b_last_instr->i_target == b->b_next) { |
| 7310 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7311 | b->b_nofallthrough = 0; |
Mark Shannon | 802b645 | 2021-02-02 14:59:15 +0000 | [diff] [blame] | 7312 | b_last_instr->i_opcode = NOP; |
| 7313 | clean_basic_block(b, -1); |
| 7314 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 7315 | } |
| 7316 | } |
| 7317 | } |
| 7318 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 7319 | if (maybe_empty_blocks) { |
| 7320 | eliminate_empty_basic_blocks(a->a_entry); |
| 7321 | } |
| 7322 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7323 | return 0; |
| 7324 | } |
| 7325 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7326 | static inline int |
| 7327 | is_exit_without_lineno(basicblock *b) { |
| 7328 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 7329 | } |
| 7330 | |
| 7331 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 7332 | * after a frame terminates. It would be prohibitively expensive |
| 7333 | * to continuously update the f_lineno field at runtime, |
| 7334 | * so we make sure that all exiting instruction (raises and returns) |
| 7335 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 7336 | * We can do this by duplicating the exit blocks without line number |
| 7337 | * so that none have more than one predecessor. We can then safely |
| 7338 | * copy the line number from the sole predecessor block. |
| 7339 | */ |
| 7340 | static int |
| 7341 | ensure_exits_have_lineno(struct compiler *c) |
| 7342 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7343 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7344 | /* Copy all exit blocks without line number that are targets of a jump. |
| 7345 | */ |
| 7346 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7347 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 7348 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 7349 | /* Note: Only actual jumps, not exception handlers */ |
| 7350 | case SETUP_ASYNC_WITH: |
| 7351 | case SETUP_WITH: |
| 7352 | case SETUP_FINALLY: |
| 7353 | continue; |
| 7354 | } |
| 7355 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 7356 | if (is_exit_without_lineno(target)) { |
| 7357 | basicblock *new_target = compiler_copy_block(c, target); |
| 7358 | if (new_target == NULL) { |
| 7359 | return -1; |
| 7360 | } |
| 7361 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7362 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 7363 | } |
| 7364 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 7365 | entry = b; |
| 7366 | } |
| 7367 | assert(entry != NULL); |
| 7368 | if (is_exit_without_lineno(entry)) { |
| 7369 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7370 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 7371 | /* Eliminate empty blocks */ |
| 7372 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7373 | while (b->b_next && b->b_next->b_iused == 0) { |
| 7374 | b->b_next = b->b_next->b_next; |
| 7375 | } |
| 7376 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 7377 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 7378 | * fall through, and thus can only have a single predecessor */ |
| 7379 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 7380 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 7381 | if (is_exit_without_lineno(b->b_next)) { |
| 7382 | assert(b->b_next->b_iused > 0); |
| 7383 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 7384 | } |
| 7385 | } |
| 7386 | } |
| 7387 | return 0; |
| 7388 | } |
| 7389 | |
| 7390 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 7391 | /* Retained for API compatibility. |
| 7392 | * Optimization is now done in optimize_cfg */ |
| 7393 | |
| 7394 | PyObject * |
| 7395 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 7396 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 7397 | { |
| 7398 | Py_INCREF(code); |
| 7399 | return code; |
| 7400 | } |