Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
| 3 | * |
| 4 | * The primary entry point is PyAST_Compile(), which returns a |
| 5 | * PyCodeObject. The compiler makes several passes to build the code |
| 6 | * object: |
| 7 | * 1. Checks for future statements. See future.c |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | * 2. Builds a symbol table. See symtable.c. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | * 3. Generate code for basic blocks. See compiler_mod() in this file. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | * 4. Assemble the basic blocks into final code. See assemble() in |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | * this file. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 12 | * 5. Optimize the byte code (peephole optimizations). |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | * |
| 14 | * Note that compiler_mod() suggests module, but the module ast type |
| 15 | * (mod_ty) has cases for expressions and interactive statements. |
Nick Coghlan | 944d3eb | 2005-11-16 12:46:55 +0000 | [diff] [blame] | 16 | * |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 17 | * CAUTION: The VISIT_* macros abort the current function when they |
| 18 | * encounter a problem. So don't invoke them when there is memory |
| 19 | * which needs to be released. Code blocks are OK, as the compiler |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | * structure takes care of releasing those. Use the arena to manage |
| 21 | * objects. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 24 | #include "Python.h" |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 25 | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 26 | #include "pycore_long.h" // _PyLong_GetZero() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 28 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 29 | #include "ast.h" |
| 30 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 31 | #include "symtable.h" |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 32 | #define NEED_OPCODE_JUMP_TABLES |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 33 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 34 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 35 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 36 | #define DEFAULT_BLOCK_SIZE 16 |
| 37 | #define DEFAULT_BLOCKS 8 |
| 38 | #define DEFAULT_CODE_SIZE 128 |
| 39 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 40 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 41 | #define COMP_GENEXP 0 |
| 42 | #define COMP_LISTCOMP 1 |
| 43 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 44 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 45 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 46 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 47 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 48 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 49 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 50 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | unsigned char i_opcode; |
| 52 | int i_oparg; |
| 53 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 54 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 57 | #define LOG_BITS_PER_INT 5 |
| 58 | #define MASK_LOW_LOG_BITS 31 |
| 59 | |
| 60 | static inline int |
| 61 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 62 | /* Is the relevant bit set in the relevant word? */ |
| 63 | /* 256 bits fit into 8 32-bits words. |
| 64 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 65 | * Bit within word is the low bits of bitindex. |
| 66 | */ |
| 67 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 68 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 69 | } |
| 70 | |
| 71 | static inline int |
| 72 | is_relative_jump(struct instr *i) |
| 73 | { |
| 74 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 75 | } |
| 76 | |
| 77 | static inline int |
| 78 | is_jump(struct instr *i) |
| 79 | { |
| 80 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 81 | } |
| 82 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 83 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 85 | reverse order that the block are allocated. b_list points to the next |
| 86 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | struct basicblock_ *b_list; |
| 88 | /* number of instructions used */ |
| 89 | int b_iused; |
| 90 | /* length of instruction array (b_instr) */ |
| 91 | int b_ialloc; |
| 92 | /* pointer to an array of instructions, initially NULL */ |
| 93 | struct instr *b_instr; |
| 94 | /* If b_next is non-NULL, it is a pointer to the next |
| 95 | block reached by normal control flow. */ |
| 96 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 98 | unsigned b_return : 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 99 | /* Number of predecssors that a block has. */ |
| 100 | int b_predecessors; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 101 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 102 | unsigned b_nofallthrough : 1; |
| 103 | /* Basic block exits scope (it ends with a return or raise) */ |
| 104 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 106 | int b_startdepth; |
| 107 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 108 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | } basicblock; |
| 110 | |
| 111 | /* fblockinfo tracks the current frame block. |
| 112 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 113 | A frame block is used to handle loops, try/except, and try/finally. |
| 114 | It's called a frame block to distinguish it from a basic block in the |
| 115 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 116 | */ |
| 117 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 118 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
| 119 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 120 | |
| 121 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | enum fblocktype fb_type; |
| 123 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 124 | /* (optional) type-specific exit or cleanup block */ |
| 125 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 126 | /* (optional) additional information required for unwinding */ |
| 127 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 130 | enum { |
| 131 | COMPILER_SCOPE_MODULE, |
| 132 | COMPILER_SCOPE_CLASS, |
| 133 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 134 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 135 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 136 | COMPILER_SCOPE_COMPREHENSION, |
| 137 | }; |
| 138 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 139 | /* The following items change on entry and exit of code blocks. |
| 140 | They must be saved and restored when returning to a block. |
| 141 | */ |
| 142 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 146 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 147 | int u_scope_type; |
| 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | /* The following fields are dicts that map objects to |
| 150 | the index of them in co_XXX. The index is used as |
| 151 | the argument for opcodes that refer to those collections. |
| 152 | */ |
| 153 | PyObject *u_consts; /* all constants */ |
| 154 | PyObject *u_names; /* all names */ |
| 155 | PyObject *u_varnames; /* local variables */ |
| 156 | PyObject *u_cellvars; /* cell variables */ |
| 157 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 160 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 161 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 162 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 163 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | /* Pointer to the most recently allocated block. By following b_list |
| 165 | members, you can reach all early allocated blocks. */ |
| 166 | basicblock *u_blocks; |
| 167 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | int u_nfblocks; |
| 170 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | int u_firstlineno; /* the first lineno of the block */ |
| 173 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 174 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 178 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 179 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | for enclosing blocks are stored in c_stack. The u and c_stack are |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 181 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 182 | |
| 183 | Note that we don't track recursion levels during compilation - the |
| 184 | task of detecting and rejecting excessive levels of nesting is |
| 185 | handled by the symbol analysis pass. |
| 186 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | */ |
| 188 | |
| 189 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 190 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | struct symtable *c_st; |
| 192 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 193 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 194 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 195 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | int c_interactive; /* true if in interactive mode */ |
| 197 | int c_nestlevel; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 198 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 199 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | struct compiler_unit *u; /* compiler state for current block */ |
| 201 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 202 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 205 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 206 | static void compiler_free(struct compiler *); |
| 207 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 208 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 209 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 210 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 211 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 212 | static int compiler_addop_j_noline(struct compiler *, int, basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 213 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 214 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 215 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 216 | |
| 217 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 218 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 219 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 220 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 221 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 222 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 223 | static int compiler_subscript(struct compiler *, expr_ty); |
| 224 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 225 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 226 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 227 | 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] | 228 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 229 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 230 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 231 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 232 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 233 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 234 | asdl_expr_seq *args, |
| 235 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 236 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 237 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 238 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 239 | static int compiler_sync_comprehension_generator( |
| 240 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 241 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 242 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 243 | expr_ty elt, expr_ty val, int type); |
| 244 | |
| 245 | static int compiler_async_comprehension_generator( |
| 246 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 247 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 248 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 249 | expr_ty elt, expr_ty val, int type); |
| 250 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 251 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 252 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 253 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 254 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 255 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 256 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 257 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | /* Name mangling: __private becomes _classname__private. |
| 260 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 261 | PyObject *result; |
| 262 | size_t nlen, plen, ipriv; |
| 263 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 265 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 266 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | Py_INCREF(ident); |
| 268 | return ident; |
| 269 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 270 | nlen = PyUnicode_GET_LENGTH(ident); |
| 271 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | The only time a name with a dot can occur is when |
| 275 | we are compiling an import statement that has a |
| 276 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | TODO(jhylton): Decide whether we want to support |
| 279 | mangling of the module name, e.g. __M.X. |
| 280 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 281 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 282 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 283 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | Py_INCREF(ident); |
| 285 | return ident; /* Don't mangle __whatever__ */ |
| 286 | } |
| 287 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 288 | ipriv = 0; |
| 289 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 290 | ipriv++; |
| 291 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | Py_INCREF(ident); |
| 293 | return ident; /* Don't mangle if class is just underscores */ |
| 294 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 295 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 296 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 297 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 298 | PyErr_SetString(PyExc_OverflowError, |
| 299 | "private identifier too large to be mangled"); |
| 300 | return NULL; |
| 301 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 302 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 303 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 304 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 305 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 306 | |
| 307 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 308 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 310 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 311 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 312 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 313 | Py_DECREF(result); |
| 314 | return NULL; |
| 315 | } |
| 316 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 317 | Py_DECREF(result); |
| 318 | return NULL; |
| 319 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 320 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 321 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 324 | static int |
| 325 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 328 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 329 | c->c_const_cache = PyDict_New(); |
| 330 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | c->c_stack = PyList_New(0); |
| 335 | if (!c->c_stack) { |
| 336 | Py_CLEAR(c->c_const_cache); |
| 337 | return 0; |
| 338 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 344 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 345 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | struct compiler c; |
| 348 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 349 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (!__doc__) { |
| 353 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 354 | if (!__doc__) |
| 355 | return NULL; |
| 356 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 357 | if (!__annotations__) { |
| 358 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 359 | if (!__annotations__) |
| 360 | return NULL; |
| 361 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (!compiler_init(&c)) |
| 363 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 364 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | c.c_filename = filename; |
| 366 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 367 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | if (c.c_future == NULL) |
| 369 | goto finally; |
| 370 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | flags = &local_flags; |
| 372 | } |
| 373 | merged = c.c_future->ff_features | flags->cf_flags; |
| 374 | c.c_future->ff_features = merged; |
| 375 | flags->cf_flags = merged; |
| 376 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 377 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 379 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 380 | _PyASTOptimizeState state; |
| 381 | state.optimize = c.c_optimize; |
| 382 | state.ff_features = merged; |
| 383 | |
| 384 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 385 | goto finally; |
| 386 | } |
| 387 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 388 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (c.c_st == NULL) { |
| 390 | if (!PyErr_Occurred()) |
| 391 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 392 | goto finally; |
| 393 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 396 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 397 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | compiler_free(&c); |
| 399 | assert(co || PyErr_Occurred()); |
| 400 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 404 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 405 | int optimize, PyArena *arena) |
| 406 | { |
| 407 | PyObject *filename; |
| 408 | PyCodeObject *co; |
| 409 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 410 | if (filename == NULL) |
| 411 | return NULL; |
| 412 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 413 | Py_DECREF(filename); |
| 414 | return co; |
| 415 | |
| 416 | } |
| 417 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 418 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 419 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | if (c->c_st) |
| 422 | PySymtable_Free(c->c_st); |
| 423 | if (c->c_future) |
| 424 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 425 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 426 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 430 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 431 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | Py_ssize_t i, n; |
| 434 | PyObject *v, *k; |
| 435 | PyObject *dict = PyDict_New(); |
| 436 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | n = PyList_Size(list); |
| 439 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 440 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | if (!v) { |
| 442 | Py_DECREF(dict); |
| 443 | return NULL; |
| 444 | } |
| 445 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 446 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | Py_DECREF(v); |
| 448 | Py_DECREF(dict); |
| 449 | return NULL; |
| 450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | Py_DECREF(v); |
| 452 | } |
| 453 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Return new dict containing names from src that match scope(s). |
| 457 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 458 | 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] | 459 | 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] | 460 | values are integers, starting at offset and increasing by one for |
| 461 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 462 | */ |
| 463 | |
| 464 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 465 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 466 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 467 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 469 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | assert(offset >= 0); |
| 472 | if (dest == NULL) |
| 473 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 474 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 475 | /* Sort the keys so that we have a deterministic order on the indexes |
| 476 | saved in the returned dictionary. These indexes are used as indexes |
| 477 | into the free and cell var storage. Therefore if they aren't |
| 478 | deterministic, then the generated bytecode is not deterministic. |
| 479 | */ |
| 480 | sorted_keys = PyDict_Keys(src); |
| 481 | if (sorted_keys == NULL) |
| 482 | return NULL; |
| 483 | if (PyList_Sort(sorted_keys) != 0) { |
| 484 | Py_DECREF(sorted_keys); |
| 485 | return NULL; |
| 486 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 487 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 488 | |
| 489 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | /* XXX this should probably be a macro in symtable.h */ |
| 491 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 492 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 493 | v = PyDict_GetItemWithError(src, k); |
| 494 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | vi = PyLong_AS_LONG(v); |
| 496 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 499 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 501 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | Py_DECREF(dest); |
| 503 | return NULL; |
| 504 | } |
| 505 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 506 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 507 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | Py_DECREF(item); |
| 509 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | return NULL; |
| 511 | } |
| 512 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 515 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 519 | static void |
| 520 | compiler_unit_check(struct compiler_unit *u) |
| 521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | basicblock *block; |
| 523 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 524 | assert(!_PyMem_IsPtrFreed(block)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | if (block->b_instr != NULL) { |
| 526 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 527 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | assert(block->b_ialloc >= block->b_iused); |
| 529 | } |
| 530 | else { |
| 531 | assert (block->b_iused == 0); |
| 532 | assert (block->b_ialloc == 0); |
| 533 | } |
| 534 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static void |
| 538 | compiler_unit_free(struct compiler_unit *u) |
| 539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | compiler_unit_check(u); |
| 543 | b = u->u_blocks; |
| 544 | while (b != NULL) { |
| 545 | if (b->b_instr) |
| 546 | PyObject_Free((void *)b->b_instr); |
| 547 | next = b->b_list; |
| 548 | PyObject_Free((void *)b); |
| 549 | b = next; |
| 550 | } |
| 551 | Py_CLEAR(u->u_ste); |
| 552 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 553 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | Py_CLEAR(u->u_consts); |
| 555 | Py_CLEAR(u->u_names); |
| 556 | Py_CLEAR(u->u_varnames); |
| 557 | Py_CLEAR(u->u_freevars); |
| 558 | Py_CLEAR(u->u_cellvars); |
| 559 | Py_CLEAR(u->u_private); |
| 560 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 564 | compiler_enter_scope(struct compiler *c, identifier name, |
| 565 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 568 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 569 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 570 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | struct compiler_unit)); |
| 572 | if (!u) { |
| 573 | PyErr_NoMemory(); |
| 574 | return 0; |
| 575 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 576 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 578 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | u->u_kwonlyargcount = 0; |
| 580 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 581 | if (!u->u_ste) { |
| 582 | compiler_unit_free(u); |
| 583 | return 0; |
| 584 | } |
| 585 | Py_INCREF(name); |
| 586 | u->u_name = name; |
| 587 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 588 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 589 | if (!u->u_varnames || !u->u_cellvars) { |
| 590 | compiler_unit_free(u); |
| 591 | return 0; |
| 592 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 593 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 594 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 595 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 596 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 597 | int res; |
| 598 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 599 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 600 | name = _PyUnicode_FromId(&PyId___class__); |
| 601 | if (!name) { |
| 602 | compiler_unit_free(u); |
| 603 | return 0; |
| 604 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 605 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 606 | if (res < 0) { |
| 607 | compiler_unit_free(u); |
| 608 | return 0; |
| 609 | } |
| 610 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | 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] | 613 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | if (!u->u_freevars) { |
| 615 | compiler_unit_free(u); |
| 616 | return 0; |
| 617 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | u->u_blocks = NULL; |
| 620 | u->u_nfblocks = 0; |
| 621 | u->u_firstlineno = lineno; |
| 622 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 623 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | u->u_consts = PyDict_New(); |
| 625 | if (!u->u_consts) { |
| 626 | compiler_unit_free(u); |
| 627 | return 0; |
| 628 | } |
| 629 | u->u_names = PyDict_New(); |
| 630 | if (!u->u_names) { |
| 631 | compiler_unit_free(u); |
| 632 | return 0; |
| 633 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | /* Push the old compiler_unit on the stack. */ |
| 638 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 639 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 641 | Py_XDECREF(capsule); |
| 642 | compiler_unit_free(u); |
| 643 | return 0; |
| 644 | } |
| 645 | Py_DECREF(capsule); |
| 646 | u->u_private = c->u->u_private; |
| 647 | Py_XINCREF(u->u_private); |
| 648 | } |
| 649 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 652 | |
| 653 | block = compiler_new_block(c); |
| 654 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 656 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 657 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 658 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 659 | if (!compiler_set_qualname(c)) |
| 660 | return 0; |
| 661 | } |
| 662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 666 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 667 | compiler_exit_scope(struct compiler *c) |
| 668 | { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 669 | // Don't call PySequence_DelItem() with an exception raised |
| 670 | PyObject *exc_type, *exc_val, *exc_tb; |
| 671 | PyErr_Fetch(&exc_type, &exc_val, &exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | c->c_nestlevel--; |
| 674 | compiler_unit_free(c->u); |
| 675 | /* Restore c->u to the parent unit. */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 676 | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | if (n >= 0) { |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 678 | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 679 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | assert(c->u); |
| 681 | /* we are deleting from a list so this really shouldn't fail */ |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 682 | if (PySequence_DelItem(c->c_stack, n) < 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 683 | _PyErr_WriteUnraisableMsg("on removing the last compiler " |
| 684 | "stack item", NULL); |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 685 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | compiler_unit_check(c->u); |
| 687 | } |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 688 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | c->u = NULL; |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 690 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 691 | |
Victor Stinner | a619263 | 2021-01-29 16:53:03 +0100 | [diff] [blame] | 692 | PyErr_Restore(exc_type, exc_val, exc_tb); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 695 | static int |
| 696 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 697 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 698 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 699 | _Py_static_string(dot_locals, ".<locals>"); |
| 700 | Py_ssize_t stack_size; |
| 701 | struct compiler_unit *u = c->u; |
| 702 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 703 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 704 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 705 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 706 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 707 | if (stack_size > 1) { |
| 708 | int scope, force_global = 0; |
| 709 | struct compiler_unit *parent; |
| 710 | PyObject *mangled, *capsule; |
| 711 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 712 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 713 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 714 | assert(parent); |
| 715 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 716 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 717 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 718 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 719 | assert(u->u_name); |
| 720 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 721 | if (!mangled) |
| 722 | return 0; |
| 723 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 724 | Py_DECREF(mangled); |
| 725 | assert(scope != GLOBAL_IMPLICIT); |
| 726 | if (scope == GLOBAL_EXPLICIT) |
| 727 | force_global = 1; |
| 728 | } |
| 729 | |
| 730 | if (!force_global) { |
| 731 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 732 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 733 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 734 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 735 | if (dot_locals_str == NULL) |
| 736 | return 0; |
| 737 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 738 | if (base == NULL) |
| 739 | return 0; |
| 740 | } |
| 741 | else { |
| 742 | Py_INCREF(parent->u_qualname); |
| 743 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 744 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 745 | } |
| 746 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 747 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 748 | if (base != NULL) { |
| 749 | dot_str = _PyUnicode_FromId(&dot); |
| 750 | if (dot_str == NULL) { |
| 751 | Py_DECREF(base); |
| 752 | return 0; |
| 753 | } |
| 754 | name = PyUnicode_Concat(base, dot_str); |
| 755 | Py_DECREF(base); |
| 756 | if (name == NULL) |
| 757 | return 0; |
| 758 | PyUnicode_Append(&name, u->u_name); |
| 759 | if (name == NULL) |
| 760 | return 0; |
| 761 | } |
| 762 | else { |
| 763 | Py_INCREF(u->u_name); |
| 764 | name = u->u_name; |
| 765 | } |
| 766 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 767 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 768 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 771 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 772 | /* Allocate a new block and return a pointer to it. |
| 773 | Returns NULL on error. |
| 774 | */ |
| 775 | |
| 776 | static basicblock * |
| 777 | compiler_new_block(struct compiler *c) |
| 778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | basicblock *b; |
| 780 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 783 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | if (b == NULL) { |
| 785 | PyErr_NoMemory(); |
| 786 | return NULL; |
| 787 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | /* Extend the singly linked list of blocks with new block. */ |
| 789 | b->b_list = u->u_blocks; |
| 790 | u->u_blocks = b; |
| 791 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | compiler_next_block(struct compiler *c) |
| 796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | basicblock *block = compiler_new_block(c); |
| 798 | if (block == NULL) |
| 799 | return NULL; |
| 800 | c->u->u_curblock->b_next = block; |
| 801 | c->u->u_curblock = block; |
| 802 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static basicblock * |
| 806 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | assert(block != 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 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 814 | static basicblock * |
| 815 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 816 | { |
| 817 | /* Cannot copy a block if it has a fallthrough, since |
| 818 | * a block can only have one fallthrough predecessor. |
| 819 | */ |
| 820 | assert(block->b_nofallthrough); |
| 821 | basicblock *result = compiler_next_block(c); |
| 822 | if (result == NULL) { |
| 823 | return NULL; |
| 824 | } |
| 825 | for (int i = 0; i < block->b_iused; i++) { |
| 826 | int n = compiler_next_instr(result); |
| 827 | if (n < 0) { |
| 828 | return NULL; |
| 829 | } |
| 830 | result->b_instr[n] = block->b_instr[i]; |
| 831 | } |
| 832 | result->b_exit = block->b_exit; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 833 | result->b_nofallthrough = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 834 | return result; |
| 835 | } |
| 836 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | /* Returns the offset of the next instruction in the current block's |
| 838 | b_instr array. Resizes the b_instr as necessary. |
| 839 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 840 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 841 | |
| 842 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 843 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 844 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | assert(b != NULL); |
| 846 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 847 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 848 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | if (b->b_instr == NULL) { |
| 850 | PyErr_NoMemory(); |
| 851 | return -1; |
| 852 | } |
| 853 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | } |
| 855 | else if (b->b_iused == b->b_ialloc) { |
| 856 | struct instr *tmp; |
| 857 | size_t oldsize, newsize; |
| 858 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 859 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 860 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 861 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | PyErr_NoMemory(); |
| 863 | return -1; |
| 864 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | if (newsize == 0) { |
| 867 | PyErr_NoMemory(); |
| 868 | return -1; |
| 869 | } |
| 870 | b->b_ialloc <<= 1; |
| 871 | tmp = (struct instr *)PyObject_Realloc( |
| 872 | (void *)b->b_instr, newsize); |
| 873 | if (tmp == NULL) { |
| 874 | PyErr_NoMemory(); |
| 875 | return -1; |
| 876 | } |
| 877 | b->b_instr = tmp; |
| 878 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 879 | } |
| 880 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 883 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 884 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 885 | The line number is reset in the following cases: |
| 886 | - when entering a new scope |
| 887 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 888 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 889 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 890 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 891 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 892 | #define SET_LOC(c, x) \ |
| 893 | (c)->u->u_lineno = (x)->lineno; \ |
| 894 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 895 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 896 | /* Return the stack effect of opcode with argument oparg. |
| 897 | |
| 898 | Some opcodes have different stack effect when jump to the target and |
| 899 | when not jump. The 'jump' parameter specifies the case: |
| 900 | |
| 901 | * 0 -- when not jump |
| 902 | * 1 -- when jump |
| 903 | * -1 -- maximal |
| 904 | */ |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 905 | static int |
| 906 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 909 | case NOP: |
| 910 | case EXTENDED_ARG: |
| 911 | return 0; |
| 912 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 913 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | case POP_TOP: |
| 915 | return -1; |
| 916 | case ROT_TWO: |
| 917 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 918 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | return 0; |
| 920 | case DUP_TOP: |
| 921 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 922 | case DUP_TOP_TWO: |
| 923 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 924 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 925 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | case UNARY_POSITIVE: |
| 927 | case UNARY_NEGATIVE: |
| 928 | case UNARY_NOT: |
| 929 | case UNARY_INVERT: |
| 930 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | case SET_ADD: |
| 933 | case LIST_APPEND: |
| 934 | return -1; |
| 935 | case MAP_ADD: |
| 936 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 937 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 938 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | case BINARY_POWER: |
| 940 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 941 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | case BINARY_MODULO: |
| 943 | case BINARY_ADD: |
| 944 | case BINARY_SUBTRACT: |
| 945 | case BINARY_SUBSCR: |
| 946 | case BINARY_FLOOR_DIVIDE: |
| 947 | case BINARY_TRUE_DIVIDE: |
| 948 | return -1; |
| 949 | case INPLACE_FLOOR_DIVIDE: |
| 950 | case INPLACE_TRUE_DIVIDE: |
| 951 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | case INPLACE_ADD: |
| 954 | case INPLACE_SUBTRACT: |
| 955 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 956 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | case INPLACE_MODULO: |
| 958 | return -1; |
| 959 | case STORE_SUBSCR: |
| 960 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | case DELETE_SUBSCR: |
| 962 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 963 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | case BINARY_LSHIFT: |
| 965 | case BINARY_RSHIFT: |
| 966 | case BINARY_AND: |
| 967 | case BINARY_XOR: |
| 968 | case BINARY_OR: |
| 969 | return -1; |
| 970 | case INPLACE_POWER: |
| 971 | return -1; |
| 972 | case GET_ITER: |
| 973 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | case PRINT_EXPR: |
| 976 | return -1; |
| 977 | case LOAD_BUILD_CLASS: |
| 978 | return 1; |
| 979 | case INPLACE_LSHIFT: |
| 980 | case INPLACE_RSHIFT: |
| 981 | case INPLACE_AND: |
| 982 | case INPLACE_XOR: |
| 983 | case INPLACE_OR: |
| 984 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 987 | /* 1 in the normal flow. |
| 988 | * Restore the stack position and push 6 values before jumping to |
| 989 | * the handler if an exception be raised. */ |
| 990 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | case RETURN_VALUE: |
| 992 | return -1; |
| 993 | case IMPORT_STAR: |
| 994 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 995 | case SETUP_ANNOTATIONS: |
| 996 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | case YIELD_VALUE: |
| 998 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 999 | case YIELD_FROM: |
| 1000 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case POP_BLOCK: |
| 1002 | return 0; |
| 1003 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1004 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | case STORE_NAME: |
| 1007 | return -1; |
| 1008 | case DELETE_NAME: |
| 1009 | return 0; |
| 1010 | case UNPACK_SEQUENCE: |
| 1011 | return oparg-1; |
| 1012 | case UNPACK_EX: |
| 1013 | return (oparg&0xFF) + (oparg>>8); |
| 1014 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1015 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1016 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | case STORE_ATTR: |
| 1019 | return -2; |
| 1020 | case DELETE_ATTR: |
| 1021 | return -1; |
| 1022 | case STORE_GLOBAL: |
| 1023 | return -1; |
| 1024 | case DELETE_GLOBAL: |
| 1025 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | case LOAD_CONST: |
| 1027 | return 1; |
| 1028 | case LOAD_NAME: |
| 1029 | return 1; |
| 1030 | case BUILD_TUPLE: |
| 1031 | case BUILD_LIST: |
| 1032 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1033 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | return 1-oparg; |
| 1035 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1036 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1037 | case BUILD_CONST_KEY_MAP: |
| 1038 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | case LOAD_ATTR: |
| 1040 | return 0; |
| 1041 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1042 | case IS_OP: |
| 1043 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1045 | case JUMP_IF_NOT_EXC_MATCH: |
| 1046 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | case IMPORT_NAME: |
| 1048 | return -1; |
| 1049 | case IMPORT_FROM: |
| 1050 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1051 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1052 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | case JUMP_ABSOLUTE: |
| 1055 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1056 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1057 | case JUMP_IF_TRUE_OR_POP: |
| 1058 | case JUMP_IF_FALSE_OR_POP: |
| 1059 | return jump ? 0 : -1; |
| 1060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | case POP_JUMP_IF_FALSE: |
| 1062 | case POP_JUMP_IF_TRUE: |
| 1063 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1064 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | case LOAD_GLOBAL: |
| 1066 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1067 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1068 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1070 | /* 0 in the normal flow. |
| 1071 | * Restore the stack position and push 6 values before jumping to |
| 1072 | * the handler if an exception be raised. */ |
| 1073 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1074 | case RERAISE: |
| 1075 | return -3; |
| 1076 | |
| 1077 | case WITH_EXCEPT_START: |
| 1078 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | case LOAD_FAST: |
| 1081 | return 1; |
| 1082 | case STORE_FAST: |
| 1083 | return -1; |
| 1084 | case DELETE_FAST: |
| 1085 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | case RAISE_VARARGS: |
| 1088 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1089 | |
| 1090 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1092 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1093 | case CALL_METHOD: |
| 1094 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1096 | return -oparg-1; |
| 1097 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1098 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1099 | case MAKE_FUNCTION: |
| 1100 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1101 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | case BUILD_SLICE: |
| 1103 | if (oparg == 3) |
| 1104 | return -2; |
| 1105 | else |
| 1106 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1107 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1108 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | case LOAD_CLOSURE: |
| 1110 | return 1; |
| 1111 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1112 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | return 1; |
| 1114 | case STORE_DEREF: |
| 1115 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1116 | case DELETE_DEREF: |
| 1117 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1118 | |
| 1119 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1120 | case GET_AWAITABLE: |
| 1121 | return 0; |
| 1122 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1123 | /* 0 in the normal flow. |
| 1124 | * Restore the stack position to the position before the result |
| 1125 | * of __aenter__ and push 6 values before jumping to the handler |
| 1126 | * if an exception be raised. */ |
| 1127 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1128 | case BEFORE_ASYNC_WITH: |
| 1129 | return 1; |
| 1130 | case GET_AITER: |
| 1131 | return 0; |
| 1132 | case GET_ANEXT: |
| 1133 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1134 | case GET_YIELD_FROM_ITER: |
| 1135 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1136 | case END_ASYNC_FOR: |
| 1137 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1138 | case FORMAT_VALUE: |
| 1139 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1140 | else 1->1. */ |
| 1141 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1142 | case LOAD_METHOD: |
| 1143 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1144 | case LOAD_ASSERTION_ERROR: |
| 1145 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1146 | case LIST_TO_TUPLE: |
| 1147 | return 0; |
| 1148 | case LIST_EXTEND: |
| 1149 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1150 | case DICT_MERGE: |
| 1151 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1152 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1154 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1156 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1159 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1160 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1161 | { |
| 1162 | return stack_effect(opcode, oparg, jump); |
| 1163 | } |
| 1164 | |
| 1165 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1166 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1167 | { |
| 1168 | return stack_effect(opcode, oparg, -1); |
| 1169 | } |
| 1170 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | /* Add an opcode with no argument. |
| 1172 | Returns 0 on failure, 1 on success. |
| 1173 | */ |
| 1174 | |
| 1175 | static int |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1176 | compiler_addop_line(struct compiler *c, int opcode, int line) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | basicblock *b; |
| 1179 | struct instr *i; |
| 1180 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1181 | assert(!HAS_ARG(opcode)); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1182 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | if (off < 0) |
| 1184 | return 0; |
| 1185 | b = c->u->u_curblock; |
| 1186 | i = &b->b_instr[off]; |
| 1187 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1188 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | if (opcode == RETURN_VALUE) |
| 1190 | b->b_return = 1; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1191 | i->i_lineno = line; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1195 | static int |
| 1196 | compiler_addop(struct compiler *c, int opcode) |
| 1197 | { |
| 1198 | return compiler_addop_line(c, opcode, c->u->u_lineno); |
| 1199 | } |
| 1200 | |
| 1201 | static int |
| 1202 | compiler_addop_noline(struct compiler *c, int opcode) |
| 1203 | { |
| 1204 | return compiler_addop_line(c, opcode, -1); |
| 1205 | } |
| 1206 | |
| 1207 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1208 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1209 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1210 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1211 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1213 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1214 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1216 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1218 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1219 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1220 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | return -1; |
| 1223 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1224 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | Py_DECREF(v); |
| 1226 | return -1; |
| 1227 | } |
| 1228 | Py_DECREF(v); |
| 1229 | } |
| 1230 | else |
| 1231 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1232 | return arg; |
| 1233 | } |
| 1234 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1235 | // Merge const *o* recursively and return constant key object. |
| 1236 | static PyObject* |
| 1237 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1238 | { |
| 1239 | // None and Ellipsis are singleton, and key is the singleton. |
| 1240 | // No need to merge object and key. |
| 1241 | if (o == Py_None || o == Py_Ellipsis) { |
| 1242 | Py_INCREF(o); |
| 1243 | return o; |
| 1244 | } |
| 1245 | |
| 1246 | PyObject *key = _PyCode_ConstantKey(o); |
| 1247 | if (key == NULL) { |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | |
| 1251 | // t is borrowed reference |
| 1252 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1253 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1254 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1255 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1256 | Py_DECREF(key); |
| 1257 | return t; |
| 1258 | } |
| 1259 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1260 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1261 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1262 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1263 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1264 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1265 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1266 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1267 | PyObject *u = merge_consts_recursive(c, item); |
| 1268 | if (u == NULL) { |
| 1269 | Py_DECREF(key); |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | |
| 1273 | // See _PyCode_ConstantKey() |
| 1274 | PyObject *v; // borrowed |
| 1275 | if (PyTuple_CheckExact(u)) { |
| 1276 | v = PyTuple_GET_ITEM(u, 1); |
| 1277 | } |
| 1278 | else { |
| 1279 | v = u; |
| 1280 | } |
| 1281 | if (v != item) { |
| 1282 | Py_INCREF(v); |
| 1283 | PyTuple_SET_ITEM(o, i, v); |
| 1284 | Py_DECREF(item); |
| 1285 | } |
| 1286 | |
| 1287 | Py_DECREF(u); |
| 1288 | } |
| 1289 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1290 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1291 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1292 | // constant keys. |
| 1293 | // See _PyCode_ConstantKey() for detail. |
| 1294 | assert(PyTuple_CheckExact(key)); |
| 1295 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1296 | |
| 1297 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1298 | if (len == 0) { // empty frozenset should not be re-created. |
| 1299 | return key; |
| 1300 | } |
| 1301 | PyObject *tuple = PyTuple_New(len); |
| 1302 | if (tuple == NULL) { |
| 1303 | Py_DECREF(key); |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | Py_ssize_t i = 0, pos = 0; |
| 1307 | PyObject *item; |
| 1308 | Py_hash_t hash; |
| 1309 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1310 | PyObject *k = merge_consts_recursive(c, item); |
| 1311 | if (k == NULL) { |
| 1312 | Py_DECREF(tuple); |
| 1313 | Py_DECREF(key); |
| 1314 | return NULL; |
| 1315 | } |
| 1316 | PyObject *u; |
| 1317 | if (PyTuple_CheckExact(k)) { |
| 1318 | u = PyTuple_GET_ITEM(k, 1); |
| 1319 | Py_INCREF(u); |
| 1320 | Py_DECREF(k); |
| 1321 | } |
| 1322 | else { |
| 1323 | u = k; |
| 1324 | } |
| 1325 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1326 | i++; |
| 1327 | } |
| 1328 | |
| 1329 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1330 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1331 | PyObject *new = PyFrozenSet_New(tuple); |
| 1332 | Py_DECREF(tuple); |
| 1333 | if (new == NULL) { |
| 1334 | Py_DECREF(key); |
| 1335 | return NULL; |
| 1336 | } |
| 1337 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1338 | Py_DECREF(o); |
| 1339 | PyTuple_SET_ITEM(key, 1, new); |
| 1340 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1341 | |
| 1342 | return key; |
| 1343 | } |
| 1344 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1345 | static Py_ssize_t |
| 1346 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1347 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1348 | PyObject *key = merge_consts_recursive(c, o); |
| 1349 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1350 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1351 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1352 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1353 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1354 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1359 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1360 | { |
| 1361 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1362 | if (arg < 0) |
| 1363 | return 0; |
| 1364 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1365 | } |
| 1366 | |
| 1367 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1368 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1370 | { |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1371 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1372 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1373 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1374 | return compiler_addop_i(c, opcode, arg); |
| 1375 | } |
| 1376 | |
| 1377 | static int |
| 1378 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1380 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1381 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1382 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1383 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1384 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1385 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1386 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1387 | Py_DECREF(mangled); |
| 1388 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1389 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1390 | return compiler_addop_i(c, opcode, arg); |
| 1391 | } |
| 1392 | |
| 1393 | /* Add an opcode with an integer argument. |
| 1394 | Returns 0 on failure, 1 on success. |
| 1395 | */ |
| 1396 | |
| 1397 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1398 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | struct instr *i; |
| 1401 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1402 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1403 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1404 | it in the C code (like Python/ceval.c). |
| 1405 | |
| 1406 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1407 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1408 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1409 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1410 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1411 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1412 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1413 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | if (off < 0) |
| 1415 | return 0; |
| 1416 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1417 | i->i_opcode = opcode; |
| 1418 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1419 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1423 | static int add_jump_to_block(basicblock *b, int opcode, int lineno, basicblock *target) |
| 1424 | { |
| 1425 | assert(HAS_ARG(opcode)); |
| 1426 | assert(b != NULL); |
| 1427 | assert(target != NULL); |
| 1428 | |
| 1429 | int off = compiler_next_instr(b); |
| 1430 | struct instr *i = &b->b_instr[off]; |
| 1431 | if (off < 0) { |
| 1432 | return 0; |
| 1433 | } |
| 1434 | i->i_opcode = opcode; |
| 1435 | i->i_target = target; |
| 1436 | i->i_lineno = lineno; |
| 1437 | return 1; |
| 1438 | } |
| 1439 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1441 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1442 | { |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 1443 | 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] | 1444 | } |
| 1445 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1446 | static int |
| 1447 | compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b) |
| 1448 | { |
| 1449 | return add_jump_to_block(c->u->u_curblock, opcode, -1, b); |
| 1450 | } |
| 1451 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1452 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1453 | to the new block. |
| 1454 | |
| 1455 | The returns inside this macro make it impossible to decref objects |
| 1456 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1457 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1458 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | if (compiler_next_block((C)) == NULL) \ |
| 1460 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | if (!compiler_addop((C), (OP))) \ |
| 1465 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 1468 | #define ADDOP_NOLINE(C, OP) { \ |
| 1469 | if (!compiler_addop_noline((C), (OP))) \ |
| 1470 | return 0; \ |
| 1471 | } |
| 1472 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1473 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | if (!compiler_addop((C), (OP))) { \ |
| 1475 | compiler_exit_scope(c); \ |
| 1476 | return 0; \ |
| 1477 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1480 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1481 | if (!compiler_addop_load_const((C), (O))) \ |
| 1482 | return 0; \ |
| 1483 | } |
| 1484 | |
| 1485 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1486 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1487 | PyObject *__new_const = (O); \ |
| 1488 | if (__new_const == NULL) { \ |
| 1489 | return 0; \ |
| 1490 | } \ |
| 1491 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1492 | Py_DECREF(__new_const); \ |
| 1493 | return 0; \ |
| 1494 | } \ |
| 1495 | Py_DECREF(__new_const); \ |
| 1496 | } |
| 1497 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1500 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1503 | /* Same as ADDOP_O, but steals a reference. */ |
| 1504 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1505 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1506 | Py_DECREF((O)); \ |
| 1507 | return 0; \ |
| 1508 | } \ |
| 1509 | Py_DECREF((O)); \ |
| 1510 | } |
| 1511 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1512 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1514 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1519 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1522 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1523 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 1527 | /* Add a jump with no line number. |
| 1528 | * Used for artificial jumps that have no corresponding |
| 1529 | * token in the source code. */ |
| 1530 | #define ADDOP_JUMP_NOLINE(C, OP, O) { \ |
| 1531 | if (!compiler_addop_j_noline((C), (OP), (O))) \ |
| 1532 | return 0; \ |
| 1533 | } |
| 1534 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1535 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1536 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1537 | return 0; \ |
| 1538 | } |
| 1539 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1540 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1541 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1542 | */ |
| 1543 | |
| 1544 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1546 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1549 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1551 | compiler_exit_scope(c); \ |
| 1552 | return 0; \ |
| 1553 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1556 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1558 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1563 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1565 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1566 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1567 | return 0; \ |
| 1568 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1571 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1573 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1575 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1576 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1577 | compiler_exit_scope(c); \ |
| 1578 | return 0; \ |
| 1579 | } \ |
| 1580 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1583 | /* Search if variable annotations are present statically in a block. */ |
| 1584 | |
| 1585 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1586 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1587 | { |
| 1588 | int i, j, res = 0; |
| 1589 | stmt_ty st; |
| 1590 | |
| 1591 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1592 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1593 | switch (st->kind) { |
| 1594 | case AnnAssign_kind: |
| 1595 | return 1; |
| 1596 | case For_kind: |
| 1597 | res = find_ann(st->v.For.body) || |
| 1598 | find_ann(st->v.For.orelse); |
| 1599 | break; |
| 1600 | case AsyncFor_kind: |
| 1601 | res = find_ann(st->v.AsyncFor.body) || |
| 1602 | find_ann(st->v.AsyncFor.orelse); |
| 1603 | break; |
| 1604 | case While_kind: |
| 1605 | res = find_ann(st->v.While.body) || |
| 1606 | find_ann(st->v.While.orelse); |
| 1607 | break; |
| 1608 | case If_kind: |
| 1609 | res = find_ann(st->v.If.body) || |
| 1610 | find_ann(st->v.If.orelse); |
| 1611 | break; |
| 1612 | case With_kind: |
| 1613 | res = find_ann(st->v.With.body); |
| 1614 | break; |
| 1615 | case AsyncWith_kind: |
| 1616 | res = find_ann(st->v.AsyncWith.body); |
| 1617 | break; |
| 1618 | case Try_kind: |
| 1619 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1620 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1621 | st->v.Try.handlers, j); |
| 1622 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1623 | return 1; |
| 1624 | } |
| 1625 | } |
| 1626 | res = find_ann(st->v.Try.body) || |
| 1627 | find_ann(st->v.Try.finalbody) || |
| 1628 | find_ann(st->v.Try.orelse); |
| 1629 | break; |
| 1630 | default: |
| 1631 | res = 0; |
| 1632 | } |
| 1633 | if (res) { |
| 1634 | break; |
| 1635 | } |
| 1636 | } |
| 1637 | return res; |
| 1638 | } |
| 1639 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1640 | /* |
| 1641 | * Frame block handling functions |
| 1642 | */ |
| 1643 | |
| 1644 | static int |
| 1645 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1646 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1647 | { |
| 1648 | struct fblockinfo *f; |
| 1649 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1650 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1651 | } |
| 1652 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1653 | f->fb_type = t; |
| 1654 | f->fb_block = b; |
| 1655 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1656 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1657 | return 1; |
| 1658 | } |
| 1659 | |
| 1660 | static void |
| 1661 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1662 | { |
| 1663 | struct compiler_unit *u = c->u; |
| 1664 | assert(u->u_nfblocks > 0); |
| 1665 | u->u_nfblocks--; |
| 1666 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1667 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1668 | } |
| 1669 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1670 | static int |
| 1671 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1672 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1673 | ADDOP(c, DUP_TOP); |
| 1674 | ADDOP(c, DUP_TOP); |
| 1675 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1679 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1680 | * popping the blocks will be restored afterwards, unless another |
| 1681 | * return, break or continue is found. In which case, the TOS will |
| 1682 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1683 | */ |
| 1684 | static int |
| 1685 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1686 | int preserve_tos) |
| 1687 | { |
| 1688 | switch (info->fb_type) { |
| 1689 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1690 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1691 | return 1; |
| 1692 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1693 | case FOR_LOOP: |
| 1694 | /* Pop the iterator */ |
| 1695 | if (preserve_tos) { |
| 1696 | ADDOP(c, ROT_TWO); |
| 1697 | } |
| 1698 | ADDOP(c, POP_TOP); |
| 1699 | return 1; |
| 1700 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1701 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1702 | ADDOP(c, POP_BLOCK); |
| 1703 | return 1; |
| 1704 | |
| 1705 | case FINALLY_TRY: |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1706 | /* This POP_BLOCK gets the line number of the unwinding statement */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1707 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1708 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1709 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1710 | return 0; |
| 1711 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1712 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1713 | /* Emit the finally block */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1714 | VISIT_SEQ(c, stmt, info->fb_datum); |
| 1715 | if (preserve_tos) { |
| 1716 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1717 | } |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 1718 | /* The finally block should appear to execute after the |
| 1719 | * statement causing the unwinding, so make the unwinding |
| 1720 | * instruction artificial */ |
| 1721 | c->u->u_lineno = -1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1722 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1723 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1724 | case FINALLY_END: |
| 1725 | if (preserve_tos) { |
| 1726 | ADDOP(c, ROT_FOUR); |
| 1727 | } |
| 1728 | ADDOP(c, POP_TOP); |
| 1729 | ADDOP(c, POP_TOP); |
| 1730 | ADDOP(c, POP_TOP); |
| 1731 | if (preserve_tos) { |
| 1732 | ADDOP(c, ROT_FOUR); |
| 1733 | } |
| 1734 | ADDOP(c, POP_EXCEPT); |
| 1735 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1736 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1737 | case WITH: |
| 1738 | case ASYNC_WITH: |
| 1739 | ADDOP(c, POP_BLOCK); |
| 1740 | if (preserve_tos) { |
| 1741 | ADDOP(c, ROT_TWO); |
| 1742 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1743 | if(!compiler_call_exit_with_nones(c)) { |
| 1744 | return 0; |
| 1745 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1746 | if (info->fb_type == ASYNC_WITH) { |
| 1747 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1748 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1749 | ADDOP(c, YIELD_FROM); |
| 1750 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1751 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1752 | return 1; |
| 1753 | |
| 1754 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1755 | if (info->fb_datum) { |
| 1756 | ADDOP(c, POP_BLOCK); |
| 1757 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1758 | if (preserve_tos) { |
| 1759 | ADDOP(c, ROT_FOUR); |
| 1760 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1761 | ADDOP(c, POP_EXCEPT); |
| 1762 | if (info->fb_datum) { |
| 1763 | ADDOP_LOAD_CONST(c, Py_None); |
| 1764 | compiler_nameop(c, info->fb_datum, Store); |
| 1765 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1766 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1767 | return 1; |
| 1768 | |
| 1769 | case POP_VALUE: |
| 1770 | if (preserve_tos) { |
| 1771 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1772 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1773 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1774 | return 1; |
| 1775 | } |
| 1776 | Py_UNREACHABLE(); |
| 1777 | } |
| 1778 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1779 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1780 | static int |
| 1781 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1782 | if (c->u->u_nfblocks == 0) { |
| 1783 | return 1; |
| 1784 | } |
| 1785 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1786 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1787 | *loop = top; |
| 1788 | return 1; |
| 1789 | } |
| 1790 | struct fblockinfo copy = *top; |
| 1791 | c->u->u_nfblocks--; |
| 1792 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1793 | return 0; |
| 1794 | } |
| 1795 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1796 | return 0; |
| 1797 | } |
| 1798 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1799 | c->u->u_nfblocks++; |
| 1800 | return 1; |
| 1801 | } |
| 1802 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1803 | /* Compile a sequence of statements, checking for a docstring |
| 1804 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1805 | |
| 1806 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1807 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1808 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1809 | int i = 0; |
| 1810 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1811 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1812 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1813 | /* Set current line number to the line number of first statement. |
| 1814 | This way line number for SETUP_ANNOTATIONS will always |
| 1815 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1816 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1817 | 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] | 1818 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1819 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1820 | } |
| 1821 | /* Every annotated class and module should have __annotations__. */ |
| 1822 | if (find_ann(stmts)) { |
| 1823 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1824 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1825 | if (!asdl_seq_LEN(stmts)) |
| 1826 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1827 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1828 | if (c->c_optimize < 2) { |
| 1829 | docstring = _PyAST_GetDocString(stmts); |
| 1830 | if (docstring) { |
| 1831 | i = 1; |
| 1832 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1833 | assert(st->kind == Expr_kind); |
| 1834 | VISIT(c, expr, st->v.Expr.value); |
| 1835 | if (!compiler_nameop(c, __doc__, Store)) |
| 1836 | return 0; |
| 1837 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1839 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1840 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static PyCodeObject * |
| 1845 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1846 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | PyCodeObject *co; |
| 1848 | int addNone = 1; |
| 1849 | static PyObject *module; |
| 1850 | if (!module) { |
| 1851 | module = PyUnicode_InternFromString("<module>"); |
| 1852 | if (!module) |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1856 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | return NULL; |
| 1858 | switch (mod->kind) { |
| 1859 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1860 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | compiler_exit_scope(c); |
| 1862 | return 0; |
| 1863 | } |
| 1864 | break; |
| 1865 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1866 | if (find_ann(mod->v.Interactive.body)) { |
| 1867 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1868 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1870 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | case Expression_kind: |
| 1873 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1874 | addNone = 0; |
| 1875 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | default: |
| 1877 | PyErr_Format(PyExc_SystemError, |
| 1878 | "module kind %d should not be possible", |
| 1879 | mod->kind); |
| 1880 | return 0; |
| 1881 | } |
| 1882 | co = assemble(c, addNone); |
| 1883 | compiler_exit_scope(c); |
| 1884 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1887 | /* The test for LOCAL must come before the test for FREE in order to |
| 1888 | handle classes where name is both local and free. The local var is |
| 1889 | 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] | 1890 | */ |
| 1891 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1892 | static int |
| 1893 | get_ref_type(struct compiler *c, PyObject *name) |
| 1894 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1895 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1896 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1897 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1898 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1899 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | if (scope == 0) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1901 | PyErr_Format(PyExc_SystemError, |
| 1902 | "PyST_GetScope(name=%R) failed: " |
| 1903 | "unknown scope in unit %S (%R); " |
| 1904 | "symbols: %R; locals: %R; globals: %R", |
| 1905 | name, |
| 1906 | c->u->u_name, c->u->u_ste->ste_id, |
| 1907 | c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names); |
| 1908 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | static int |
| 1914 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1915 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1916 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1917 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1918 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1919 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1920 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | static int |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1924 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, |
| 1925 | PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1926 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1927 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1928 | if (qualname == NULL) |
| 1929 | qualname = co->co_name; |
| 1930 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1931 | if (free) { |
| 1932 | for (i = 0; i < free; ++i) { |
| 1933 | /* Bypass com_addop_varname because it will generate |
| 1934 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1935 | */ |
| 1936 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1937 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1938 | /* Special case: If a class contains a method with a |
| 1939 | free variable that has the same name as a method, |
| 1940 | the name will be considered free *and* local in the |
| 1941 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1942 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1943 | */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1944 | int reftype = get_ref_type(c, name); |
| 1945 | if (reftype == -1) { |
| 1946 | return 0; |
| 1947 | } |
| 1948 | int arg; |
| 1949 | if (reftype == CELL) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1950 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1951 | } |
| 1952 | else { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1953 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1954 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1955 | if (arg == -1) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 1956 | PyErr_Format(PyExc_SystemError, |
| 1957 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
| 1958 | "freevars of code %S: %R", |
| 1959 | name, |
| 1960 | reftype, |
| 1961 | c->u->u_name, |
| 1962 | co->co_name, |
| 1963 | co->co_freevars); |
| 1964 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1965 | } |
| 1966 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1968 | flags |= 0x08; |
| 1969 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1971 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1972 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1973 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1978 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | if (!decos) |
| 1983 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1986 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1987 | } |
| 1988 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1992 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 1993 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1994 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1995 | /* Push a dict of keyword-only default values. |
| 1996 | |
| 1997 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1998 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1999 | int i; |
| 2000 | PyObject *keys = NULL; |
| 2001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 2003 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 2004 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2005 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2006 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2007 | if (!mangled) { |
| 2008 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2010 | if (keys == NULL) { |
| 2011 | keys = PyList_New(1); |
| 2012 | if (keys == NULL) { |
| 2013 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2014 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2015 | } |
| 2016 | PyList_SET_ITEM(keys, 0, mangled); |
| 2017 | } |
| 2018 | else { |
| 2019 | int res = PyList_Append(keys, mangled); |
| 2020 | Py_DECREF(mangled); |
| 2021 | if (res == -1) { |
| 2022 | goto error; |
| 2023 | } |
| 2024 | } |
| 2025 | if (!compiler_visit_expr(c, default_)) { |
| 2026 | goto error; |
| 2027 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | } |
| 2029 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2030 | if (keys != NULL) { |
| 2031 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2032 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2033 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2034 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2035 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2036 | assert(default_count > 0); |
| 2037 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2038 | } |
| 2039 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2040 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | error: |
| 2044 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2045 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2049 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2050 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2051 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2052 | return 1; |
| 2053 | } |
| 2054 | |
| 2055 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2056 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2057 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2058 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2059 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2060 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2061 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2062 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2063 | |
| 2064 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2065 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2066 | VISIT(c, annexpr, annotation); |
| 2067 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2069 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2073 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2074 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2075 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2076 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2077 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2078 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2079 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | c, |
| 2081 | arg->arg, |
| 2082 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2083 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2084 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2086 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | static int |
| 2090 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2091 | expr_ty returns) |
| 2092 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2093 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2094 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2095 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2096 | 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] | 2097 | */ |
| 2098 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2099 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2100 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2101 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2102 | return 0; |
| 2103 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2104 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2105 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2106 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2107 | args->vararg->annotation, &annotations_len)) |
| 2108 | return 0; |
| 2109 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2110 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2111 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2112 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2113 | args->kwarg->annotation, &annotations_len)) |
| 2114 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | if (!return_str) { |
| 2117 | return_str = PyUnicode_InternFromString("return"); |
| 2118 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2119 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2121 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2122 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2125 | if (annotations_len) { |
| 2126 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2127 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2129 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2130 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2134 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2135 | { |
| 2136 | VISIT_SEQ(c, expr, args->defaults); |
| 2137 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2138 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2141 | static Py_ssize_t |
| 2142 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2143 | { |
| 2144 | Py_ssize_t funcflags = 0; |
| 2145 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2146 | if (!compiler_visit_defaults(c, args)) |
| 2147 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2148 | funcflags |= 0x01; |
| 2149 | } |
| 2150 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2151 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2152 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2153 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2154 | return -1; |
| 2155 | } |
| 2156 | else if (res > 0) { |
| 2157 | funcflags |= 0x02; |
| 2158 | } |
| 2159 | } |
| 2160 | return funcflags; |
| 2161 | } |
| 2162 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2163 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2164 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2165 | { |
| 2166 | |
| 2167 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2168 | compiler_error(c, "cannot assign to __debug__"); |
| 2169 | return 1; |
| 2170 | } |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | static int |
| 2175 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2176 | { |
| 2177 | if (arg != NULL) { |
| 2178 | if (forbidden_name(c, arg->arg, Store)) |
| 2179 | return 0; |
| 2180 | } |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
| 2184 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2185 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2186 | { |
| 2187 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2188 | 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] | 2189 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2190 | return 0; |
| 2191 | } |
| 2192 | } |
| 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | static int |
| 2197 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2198 | { |
| 2199 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2200 | return 0; |
| 2201 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2202 | return 0; |
| 2203 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2204 | return 0; |
| 2205 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2206 | return 0; |
| 2207 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2208 | return 0; |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
| 2212 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2213 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2215 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2216 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2217 | arguments_ty args; |
| 2218 | expr_ty returns; |
| 2219 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2220 | asdl_expr_seq* decos; |
| 2221 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2222 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2223 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2224 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2225 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2226 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2227 | if (is_async) { |
| 2228 | assert(s->kind == AsyncFunctionDef_kind); |
| 2229 | |
| 2230 | args = s->v.AsyncFunctionDef.args; |
| 2231 | returns = s->v.AsyncFunctionDef.returns; |
| 2232 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2233 | name = s->v.AsyncFunctionDef.name; |
| 2234 | body = s->v.AsyncFunctionDef.body; |
| 2235 | |
| 2236 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2237 | } else { |
| 2238 | assert(s->kind == FunctionDef_kind); |
| 2239 | |
| 2240 | args = s->v.FunctionDef.args; |
| 2241 | returns = s->v.FunctionDef.returns; |
| 2242 | decos = s->v.FunctionDef.decorator_list; |
| 2243 | name = s->v.FunctionDef.name; |
| 2244 | body = s->v.FunctionDef.body; |
| 2245 | |
| 2246 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2247 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2248 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2249 | if (!compiler_check_debug_args(c, args)) |
| 2250 | return 0; |
| 2251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | if (!compiler_decorators(c, decos)) |
| 2253 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2254 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2255 | firstlineno = s->lineno; |
| 2256 | if (asdl_seq_LEN(decos)) { |
| 2257 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2258 | } |
| 2259 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2260 | funcflags = compiler_default_arguments(c, args); |
| 2261 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2263 | } |
| 2264 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2265 | annotations = compiler_visit_annotations(c, args, returns); |
| 2266 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2267 | return 0; |
| 2268 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2269 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2270 | funcflags |= 0x04; |
| 2271 | } |
| 2272 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2273 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2274 | return 0; |
| 2275 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2276 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2277 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2278 | if (c->c_optimize < 2) { |
| 2279 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2280 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2281 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | compiler_exit_scope(c); |
| 2283 | return 0; |
| 2284 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2287 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2289 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2290 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2291 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2293 | qualname = c->u->u_qualname; |
| 2294 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2296 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2297 | Py_XDECREF(qualname); |
| 2298 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2300 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2301 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2302 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2303 | Py_DECREF(qualname); |
| 2304 | Py_DECREF(co); |
| 2305 | return 0; |
| 2306 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2307 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | Py_DECREF(co); |
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 | /* decorators */ |
| 2311 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2312 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2313 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2314 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2315 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | static int |
| 2319 | compiler_class(struct compiler *c, stmt_ty s) |
| 2320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | PyCodeObject *co; |
| 2322 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2323 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2324 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | if (!compiler_decorators(c, decos)) |
| 2327 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2328 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2329 | firstlineno = s->lineno; |
| 2330 | if (asdl_seq_LEN(decos)) { |
| 2331 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2332 | } |
| 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | /* ultimately generate code for: |
| 2335 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2336 | where: |
| 2337 | <func> is a function/closure created from the class body; |
| 2338 | it has a single argument (__locals__) where the dict |
| 2339 | (or MutableSequence) representing the locals is passed |
| 2340 | <name> is the class name |
| 2341 | <bases> is the positional arguments and *varargs argument |
| 2342 | <keywords> is the keyword arguments and **kwds argument |
| 2343 | This borrows from compiler_call. |
| 2344 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2347 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2348 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | return 0; |
| 2350 | /* this block represents what we do in the new scope */ |
| 2351 | { |
| 2352 | /* use the class name for name mangling */ |
| 2353 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2354 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | /* load (global) __name__ ... */ |
| 2356 | str = PyUnicode_InternFromString("__name__"); |
| 2357 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2358 | Py_XDECREF(str); |
| 2359 | compiler_exit_scope(c); |
| 2360 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2361 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | Py_DECREF(str); |
| 2363 | /* ... and store it as __module__ */ |
| 2364 | str = PyUnicode_InternFromString("__module__"); |
| 2365 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2366 | Py_XDECREF(str); |
| 2367 | compiler_exit_scope(c); |
| 2368 | return 0; |
| 2369 | } |
| 2370 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2371 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2372 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2373 | str = PyUnicode_InternFromString("__qualname__"); |
| 2374 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2375 | Py_XDECREF(str); |
| 2376 | compiler_exit_scope(c); |
| 2377 | return 0; |
| 2378 | } |
| 2379 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2381 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | compiler_exit_scope(c); |
| 2383 | return 0; |
| 2384 | } |
Mark Shannon | e56d54e | 2021-01-15 13:52:00 +0000 | [diff] [blame] | 2385 | /* The following code is artificial */ |
| 2386 | c->u->u_lineno = -1; |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2387 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2388 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2389 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2390 | str = PyUnicode_InternFromString("__class__"); |
| 2391 | if (str == NULL) { |
| 2392 | compiler_exit_scope(c); |
| 2393 | return 0; |
| 2394 | } |
| 2395 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2396 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2397 | if (i < 0) { |
| 2398 | compiler_exit_scope(c); |
| 2399 | return 0; |
| 2400 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2401 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2404 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2405 | str = PyUnicode_InternFromString("__classcell__"); |
| 2406 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2407 | Py_XDECREF(str); |
| 2408 | compiler_exit_scope(c); |
| 2409 | return 0; |
| 2410 | } |
| 2411 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2413 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2414 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2415 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2416 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2417 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2418 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | /* create the code object */ |
| 2420 | co = assemble(c, 1); |
| 2421 | } |
| 2422 | /* leave the new scope */ |
| 2423 | compiler_exit_scope(c); |
| 2424 | if (co == NULL) |
| 2425 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | /* 2. load the 'build_class' function */ |
| 2428 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2429 | |
| 2430 | /* 3. load a function (or closure) made from the code object */ |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2431 | if (!compiler_make_closure(c, co, 0, NULL)) { |
| 2432 | Py_DECREF(co); |
| 2433 | return 0; |
| 2434 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | Py_DECREF(co); |
| 2436 | |
| 2437 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2438 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | |
| 2440 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2441 | 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] | 2442 | return 0; |
| 2443 | |
| 2444 | /* 6. apply decorators */ |
| 2445 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2446 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2447 | } |
| 2448 | |
| 2449 | /* 7. store into <name> */ |
| 2450 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2451 | return 0; |
| 2452 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2455 | /* Return 0 if the expression is a constant value except named singletons. |
| 2456 | Return 1 otherwise. */ |
| 2457 | static int |
| 2458 | check_is_arg(expr_ty e) |
| 2459 | { |
| 2460 | if (e->kind != Constant_kind) { |
| 2461 | return 1; |
| 2462 | } |
| 2463 | PyObject *value = e->v.Constant.value; |
| 2464 | return (value == Py_None |
| 2465 | || value == Py_False |
| 2466 | || value == Py_True |
| 2467 | || value == Py_Ellipsis); |
| 2468 | } |
| 2469 | |
| 2470 | /* Check operands of identity chacks ("is" and "is not"). |
| 2471 | Emit a warning if any operand is a constant except named singletons. |
| 2472 | Return 0 on error. |
| 2473 | */ |
| 2474 | static int |
| 2475 | check_compare(struct compiler *c, expr_ty e) |
| 2476 | { |
| 2477 | Py_ssize_t i, n; |
| 2478 | int left = check_is_arg(e->v.Compare.left); |
| 2479 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2480 | for (i = 0; i < n; i++) { |
| 2481 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2482 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2483 | if (op == Is || op == IsNot) { |
| 2484 | if (!right || !left) { |
| 2485 | const char *msg = (op == Is) |
| 2486 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2487 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2488 | return compiler_warn(c, msg); |
| 2489 | } |
| 2490 | } |
| 2491 | left = right; |
| 2492 | } |
| 2493 | return 1; |
| 2494 | } |
| 2495 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2496 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2497 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2498 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2499 | switch (op) { |
| 2500 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2501 | cmp = Py_EQ; |
| 2502 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2503 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2504 | cmp = Py_NE; |
| 2505 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2506 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2507 | cmp = Py_LT; |
| 2508 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2509 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2510 | cmp = Py_LE; |
| 2511 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2512 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2513 | cmp = Py_GT; |
| 2514 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2515 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2516 | cmp = Py_GE; |
| 2517 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2518 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2519 | ADDOP_I(c, IS_OP, 0); |
| 2520 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2521 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2522 | ADDOP_I(c, IS_OP, 1); |
| 2523 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2524 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2525 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2526 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2527 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2528 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2529 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2530 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2531 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2532 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2533 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2534 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2535 | } |
| 2536 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2537 | |
| 2538 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2539 | static int |
| 2540 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2541 | { |
| 2542 | switch (e->kind) { |
| 2543 | case UnaryOp_kind: |
| 2544 | if (e->v.UnaryOp.op == Not) |
| 2545 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2546 | /* fallback to general implementation */ |
| 2547 | break; |
| 2548 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2549 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2550 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2551 | assert(n >= 0); |
| 2552 | int cond2 = e->v.BoolOp.op == Or; |
| 2553 | basicblock *next2 = next; |
| 2554 | if (!cond2 != !cond) { |
| 2555 | next2 = compiler_new_block(c); |
| 2556 | if (next2 == NULL) |
| 2557 | return 0; |
| 2558 | } |
| 2559 | for (i = 0; i < n; ++i) { |
| 2560 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2561 | return 0; |
| 2562 | } |
| 2563 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2564 | return 0; |
| 2565 | if (next2 != next) |
| 2566 | compiler_use_next_block(c, next2); |
| 2567 | return 1; |
| 2568 | } |
| 2569 | case IfExp_kind: { |
| 2570 | basicblock *end, *next2; |
| 2571 | end = compiler_new_block(c); |
| 2572 | if (end == NULL) |
| 2573 | return 0; |
| 2574 | next2 = compiler_new_block(c); |
| 2575 | if (next2 == NULL) |
| 2576 | return 0; |
| 2577 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2578 | return 0; |
| 2579 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2580 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2581 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2582 | compiler_use_next_block(c, next2); |
| 2583 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2584 | return 0; |
| 2585 | compiler_use_next_block(c, end); |
| 2586 | return 1; |
| 2587 | } |
| 2588 | case Compare_kind: { |
| 2589 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2590 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2591 | if (!check_compare(c, e)) { |
| 2592 | return 0; |
| 2593 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2594 | basicblock *cleanup = compiler_new_block(c); |
| 2595 | if (cleanup == NULL) |
| 2596 | return 0; |
| 2597 | VISIT(c, expr, e->v.Compare.left); |
| 2598 | for (i = 0; i < n; i++) { |
| 2599 | VISIT(c, expr, |
| 2600 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2601 | ADDOP(c, DUP_TOP); |
| 2602 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2603 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2604 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2605 | NEXT_BLOCK(c); |
| 2606 | } |
| 2607 | 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] | 2608 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2609 | 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] | 2610 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2611 | basicblock *end = compiler_new_block(c); |
| 2612 | if (end == NULL) |
| 2613 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2614 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2615 | compiler_use_next_block(c, cleanup); |
| 2616 | ADDOP(c, POP_TOP); |
| 2617 | if (!cond) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2618 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2619 | } |
| 2620 | compiler_use_next_block(c, end); |
| 2621 | return 1; |
| 2622 | } |
| 2623 | /* fallback to general implementation */ |
| 2624 | break; |
| 2625 | } |
| 2626 | default: |
| 2627 | /* fallback to general implementation */ |
| 2628 | break; |
| 2629 | } |
| 2630 | |
| 2631 | /* general implementation */ |
| 2632 | VISIT(c, expr, e); |
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 | return 1; |
| 2636 | } |
| 2637 | |
| 2638 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2639 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | basicblock *end, *next; |
| 2642 | |
| 2643 | assert(e->kind == IfExp_kind); |
| 2644 | end = compiler_new_block(c); |
| 2645 | if (end == NULL) |
| 2646 | return 0; |
| 2647 | next = compiler_new_block(c); |
| 2648 | if (next == NULL) |
| 2649 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2650 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2651 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2653 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | compiler_use_next_block(c, next); |
| 2655 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2656 | compiler_use_next_block(c, end); |
| 2657 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2661 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2664 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2666 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | arguments_ty args = e->v.Lambda.args; |
| 2668 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2669 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2670 | if (!compiler_check_debug_args(c, args)) |
| 2671 | return 0; |
| 2672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | if (!name) { |
| 2674 | name = PyUnicode_InternFromString("<lambda>"); |
| 2675 | if (!name) |
| 2676 | return 0; |
| 2677 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2678 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2679 | funcflags = compiler_default_arguments(c, args); |
| 2680 | if (funcflags == -1) { |
| 2681 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2683 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2684 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2685 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | /* Make None the first constant, so the lambda can't have a |
| 2689 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2690 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2694 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2696 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2697 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2698 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | } |
| 2700 | else { |
| 2701 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2702 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2704 | qualname = c->u->u_qualname; |
| 2705 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | compiler_exit_scope(c); |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2707 | if (co == NULL) { |
| 2708 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | return 0; |
Pablo Galindo | 7fdab83 | 2021-01-29 22:40:59 +0000 | [diff] [blame] | 2710 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2711 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 2712 | if (!compiler_make_closure(c, co, funcflags, qualname)) { |
| 2713 | Py_DECREF(qualname); |
| 2714 | Py_DECREF(co); |
| 2715 | return 0; |
| 2716 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2717 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | Py_DECREF(co); |
| 2719 | |
| 2720 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2724 | compiler_if(struct compiler *c, stmt_ty s) |
| 2725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | assert(s->kind == If_kind); |
| 2728 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2729 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2731 | } |
| 2732 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2733 | next = compiler_new_block(c); |
| 2734 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2735 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2736 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2737 | } |
| 2738 | else { |
| 2739 | next = end; |
| 2740 | } |
| 2741 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2742 | return 0; |
| 2743 | } |
| 2744 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2745 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 2746 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2747 | compiler_use_next_block(c, next); |
| 2748 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2749 | } |
| 2750 | compiler_use_next_block(c, end); |
| 2751 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | static int |
| 2755 | compiler_for(struct compiler *c, stmt_ty s) |
| 2756 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2757 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2759 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2760 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | cleanup = compiler_new_block(c); |
| 2762 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2763 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2765 | } |
| 2766 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2768 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | VISIT(c, expr, s->v.For.iter); |
| 2770 | ADDOP(c, GET_ITER); |
| 2771 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2772 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2773 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | VISIT(c, expr, s->v.For.target); |
| 2775 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2776 | /* Mark jump as artificial */ |
| 2777 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2778 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2780 | |
| 2781 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2784 | compiler_use_next_block(c, end); |
| 2785 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2788 | |
| 2789 | static int |
| 2790 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2791 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2792 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2793 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2794 | c->u->u_ste->ste_coroutine = 1; |
| 2795 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2796 | return compiler_error(c, "'async for' outside async function"); |
| 2797 | } |
| 2798 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2799 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2800 | except = compiler_new_block(c); |
| 2801 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2802 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2803 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2804 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2805 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2806 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2807 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2808 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2809 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2810 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2811 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2812 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2813 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2814 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2815 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2816 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2817 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2818 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2819 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2820 | /* Success block for __anext__ */ |
| 2821 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2822 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2823 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2824 | |
| 2825 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2826 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2827 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2828 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2829 | |
| 2830 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2831 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2832 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2833 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2834 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2835 | |
| 2836 | compiler_use_next_block(c, end); |
| 2837 | |
| 2838 | return 1; |
| 2839 | } |
| 2840 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2841 | static int |
| 2842 | compiler_while(struct compiler *c, stmt_ty s) |
| 2843 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2844 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2846 | body = compiler_new_block(c); |
| 2847 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2849 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2853 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2856 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2857 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame] | 2862 | SET_LOC(c, s); |
| 2863 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2864 | return 0; |
| 2865 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2866 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2867 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2868 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2869 | compiler_use_next_block(c, anchor); |
| 2870 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2872 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2879 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2881 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2882 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2883 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2884 | return compiler_error(c, "'return' outside function"); |
| 2885 | if (s->v.Return.value != NULL && |
| 2886 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2887 | { |
| 2888 | return compiler_error( |
| 2889 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2891 | if (preserve_tos) { |
| 2892 | VISIT(c, expr, s->v.Return.value); |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2893 | } else { |
| 2894 | /* Emit instruction with line number for expression */ |
| 2895 | if (s->v.Return.value != NULL) { |
| 2896 | SET_LOC(c, s->v.Return.value); |
| 2897 | ADDOP(c, NOP); |
| 2898 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2899 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2900 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2901 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2902 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2903 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2904 | } |
| 2905 | else if (!preserve_tos) { |
Mark Shannon | 5274b68 | 2020-12-16 13:07:01 +0000 | [diff] [blame] | 2906 | ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2907 | } |
| 2908 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2909 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2914 | static int |
| 2915 | compiler_break(struct compiler *c) |
| 2916 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2917 | struct fblockinfo *loop = NULL; |
| 2918 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2919 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2920 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2921 | if (loop == NULL) { |
| 2922 | return compiler_error(c, "'break' outside loop"); |
| 2923 | } |
| 2924 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2925 | return 0; |
| 2926 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2927 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2928 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2929 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static int |
| 2933 | compiler_continue(struct compiler *c) |
| 2934 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2935 | struct fblockinfo *loop = NULL; |
| 2936 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2937 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2938 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2939 | if (loop == NULL) { |
| 2940 | return compiler_error(c, "'continue' not properly in loop"); |
| 2941 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2942 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2943 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2944 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2948 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | |
| 2950 | SETUP_FINALLY L |
| 2951 | <code for body> |
| 2952 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2953 | <code for finalbody> |
| 2954 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2955 | L: |
| 2956 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2957 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2959 | The special instructions use the block stack. Each block |
| 2960 | stack entry contains the instruction that created it (here |
| 2961 | SETUP_FINALLY), the level of the value stack at the time the |
| 2962 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2964 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | Pushes the current value stack level and the label |
| 2966 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2967 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2968 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2970 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2971 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2972 | exceptions are pushed onto the value stack (and the exception |
| 2973 | condition is cleared), and the interpreter jumps to the label |
| 2974 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2975 | */ |
| 2976 | |
| 2977 | static int |
| 2978 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2979 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2980 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | body = compiler_new_block(c); |
| 2983 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2984 | exit = compiler_new_block(c); |
| 2985 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2986 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2987 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2988 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2989 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2991 | 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] | 2992 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2993 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2994 | if (!compiler_try_except(c, s)) |
| 2995 | return 0; |
| 2996 | } |
| 2997 | else { |
| 2998 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2999 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3000 | ADDOP_NOLINE(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3001 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3002 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3003 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3004 | /* `finally` block */ |
| 3005 | compiler_use_next_block(c, end); |
| 3006 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3007 | return 0; |
| 3008 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3009 | compiler_pop_fblock(c, FINALLY_END, end); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3010 | ADDOP_I(c, RERAISE, 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3011 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3012 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3016 | 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] | 3017 | (The contents of the value stack is shown in [], with the top |
| 3018 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3019 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3020 | |
| 3021 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3022 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | [] <code for S> |
| 3024 | [] POP_BLOCK |
| 3025 | [] JUMP_FORWARD L0 |
| 3026 | |
| 3027 | [tb, val, exc] L1: DUP ) |
| 3028 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3029 | [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] | 3030 | [tb, val, exc] POP |
| 3031 | [tb, val] <assign to V1> (or POP if no V1) |
| 3032 | [tb] POP |
| 3033 | [] <code for S1> |
| 3034 | JUMP_FORWARD L0 |
| 3035 | |
| 3036 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3037 | .............................etc....................... |
| 3038 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3039 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | |
| 3041 | [] L0: <next statement> |
| 3042 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3043 | Of course, parts are not generated if Vi or Ei is not present. |
| 3044 | */ |
| 3045 | static int |
| 3046 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3047 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3049 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | body = compiler_new_block(c); |
| 3052 | except = compiler_new_block(c); |
| 3053 | orelse = compiler_new_block(c); |
| 3054 | end = compiler_new_block(c); |
| 3055 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3056 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3057 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3059 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3061 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3062 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 3063 | ADDOP_NOLINE(c, POP_BLOCK); |
| 3064 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3065 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3067 | /* Runtime will push a block here, so we need to account for that */ |
| 3068 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3069 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | for (i = 0; i < n; i++) { |
| 3071 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3072 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3074 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3075 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | except = compiler_new_block(c); |
| 3077 | if (except == NULL) |
| 3078 | return 0; |
| 3079 | if (handler->v.ExceptHandler.type) { |
| 3080 | ADDOP(c, DUP_TOP); |
| 3081 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3082 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3083 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | } |
| 3085 | ADDOP(c, POP_TOP); |
| 3086 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3087 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3088 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3089 | cleanup_end = compiler_new_block(c); |
| 3090 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3091 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3092 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3093 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3094 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3095 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3096 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3098 | /* |
| 3099 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3100 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3101 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3102 | try: |
| 3103 | # body |
| 3104 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3105 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3106 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3107 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3109 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3110 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3111 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3112 | 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] | 3113 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3115 | /* second # body */ |
| 3116 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3117 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3118 | ADDOP(c, POP_BLOCK); |
| 3119 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3120 | /* name = None; del name; # Mark as artificial */ |
| 3121 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3122 | ADDOP_LOAD_CONST(c, Py_None); |
| 3123 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3124 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3125 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3127 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3128 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3130 | /* name = None; del name; # Mark as artificial */ |
| 3131 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3132 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3133 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3134 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3136 | ADDOP_I(c, RERAISE, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | } |
| 3138 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3139 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3141 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3142 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3143 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3145 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3146 | ADDOP(c, POP_TOP); |
| 3147 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3148 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3149 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3151 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3152 | /* name = None; del name; # Mark as artificial */ |
| 3153 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3154 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3155 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | compiler_use_next_block(c, except); |
| 3158 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3159 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | f2dbfd7 | 2020-12-21 13:53:50 +0000 | [diff] [blame] | 3160 | /* Mark as artificial */ |
| 3161 | c->u->u_lineno = -1; |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 3162 | ADDOP_I(c, RERAISE, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3164 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | compiler_use_next_block(c, end); |
| 3166 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3170 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3171 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3172 | return compiler_try_finally(c, s); |
| 3173 | else |
| 3174 | return compiler_try_except(c, s); |
| 3175 | } |
| 3176 | |
| 3177 | |
| 3178 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3179 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | /* The IMPORT_NAME opcode was already generated. This function |
| 3182 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3184 | 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] | 3185 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3187 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3188 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3189 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3190 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3191 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3193 | while (1) { |
| 3194 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3196 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3197 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3198 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3199 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3201 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3202 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3203 | if (dot == -1) { |
| 3204 | break; |
| 3205 | } |
| 3206 | ADDOP(c, ROT_TWO); |
| 3207 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3209 | if (!compiler_nameop(c, asname, Store)) { |
| 3210 | return 0; |
| 3211 | } |
| 3212 | ADDOP(c, POP_TOP); |
| 3213 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | } |
| 3215 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | static int |
| 3219 | compiler_import(struct compiler *c, stmt_ty s) |
| 3220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | /* The Import node stores a module name like a.b.c as a single |
| 3222 | string. This is convenient for all cases except |
| 3223 | import a.b.c as d |
| 3224 | where we need to parse that string to extract the individual |
| 3225 | module names. |
| 3226 | XXX Perhaps change the representation to make this case simpler? |
| 3227 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3228 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3229 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3230 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | for (i = 0; i < n; i++) { |
| 3232 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3233 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3234 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3235 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3236 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | if (alias->asname) { |
| 3240 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3241 | if (!r) |
| 3242 | return r; |
| 3243 | } |
| 3244 | else { |
| 3245 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3246 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3247 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3248 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3249 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3250 | if (tmp == NULL) |
| 3251 | return 0; |
| 3252 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3254 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | Py_DECREF(tmp); |
| 3256 | } |
| 3257 | if (!r) |
| 3258 | return r; |
| 3259 | } |
| 3260 | } |
| 3261 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | static int |
| 3265 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3266 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3267 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3268 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | if (!empty_string) { |
| 3272 | empty_string = PyUnicode_FromString(""); |
| 3273 | if (!empty_string) |
| 3274 | return 0; |
| 3275 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3276 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3277 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3278 | |
| 3279 | names = PyTuple_New(n); |
| 3280 | if (!names) |
| 3281 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | /* build up the names */ |
| 3284 | for (i = 0; i < n; i++) { |
| 3285 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3286 | Py_INCREF(alias->name); |
| 3287 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3288 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3291 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3292 | Py_DECREF(names); |
| 3293 | return compiler_error(c, "from __future__ imports must occur " |
| 3294 | "at the beginning of the file"); |
| 3295 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3296 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | if (s->v.ImportFrom.module) { |
| 3299 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3300 | } |
| 3301 | else { |
| 3302 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3303 | } |
| 3304 | for (i = 0; i < n; i++) { |
| 3305 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3306 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3308 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | assert(n == 1); |
| 3310 | ADDOP(c, IMPORT_STAR); |
| 3311 | return 1; |
| 3312 | } |
| 3313 | |
| 3314 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3315 | store_name = alias->name; |
| 3316 | if (alias->asname) |
| 3317 | store_name = alias->asname; |
| 3318 | |
| 3319 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | return 0; |
| 3321 | } |
| 3322 | } |
| 3323 | /* remove imported module */ |
| 3324 | ADDOP(c, POP_TOP); |
| 3325 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | static int |
| 3329 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3333 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3336 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3337 | { |
| 3338 | if (!compiler_warn(c, "assertion is always true, " |
| 3339 | "perhaps remove parentheses?")) |
| 3340 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3341 | return 0; |
| 3342 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | end = compiler_new_block(c); |
| 3345 | if (end == NULL) |
| 3346 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3347 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3348 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3349 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | if (s->v.Assert.msg) { |
| 3351 | VISIT(c, expr, s->v.Assert.msg); |
| 3352 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3353 | } |
| 3354 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3355 | compiler_use_next_block(c, end); |
| 3356 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3360 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3361 | { |
| 3362 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3363 | VISIT(c, expr, value); |
| 3364 | ADDOP(c, PRINT_EXPR); |
| 3365 | return 1; |
| 3366 | } |
| 3367 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3368 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3369 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3370 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3371 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | VISIT(c, expr, value); |
| 3375 | ADDOP(c, POP_TOP); |
| 3376 | return 1; |
| 3377 | } |
| 3378 | |
| 3379 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3380 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3381 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3382 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3384 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3385 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | switch (s->kind) { |
| 3388 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3389 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | case ClassDef_kind: |
| 3391 | return compiler_class(c, s); |
| 3392 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3393 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3394 | case Delete_kind: |
| 3395 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3396 | break; |
| 3397 | case Assign_kind: |
| 3398 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3399 | VISIT(c, expr, s->v.Assign.value); |
| 3400 | for (i = 0; i < n; i++) { |
| 3401 | if (i < n - 1) |
| 3402 | ADDOP(c, DUP_TOP); |
| 3403 | VISIT(c, expr, |
| 3404 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3405 | } |
| 3406 | break; |
| 3407 | case AugAssign_kind: |
| 3408 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3409 | case AnnAssign_kind: |
| 3410 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3411 | case For_kind: |
| 3412 | return compiler_for(c, s); |
| 3413 | case While_kind: |
| 3414 | return compiler_while(c, s); |
| 3415 | case If_kind: |
| 3416 | return compiler_if(c, s); |
| 3417 | case Raise_kind: |
| 3418 | n = 0; |
| 3419 | if (s->v.Raise.exc) { |
| 3420 | VISIT(c, expr, s->v.Raise.exc); |
| 3421 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3422 | if (s->v.Raise.cause) { |
| 3423 | VISIT(c, expr, s->v.Raise.cause); |
| 3424 | n++; |
| 3425 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3427 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3428 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3430 | case Try_kind: |
| 3431 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | case Assert_kind: |
| 3433 | return compiler_assert(c, s); |
| 3434 | case Import_kind: |
| 3435 | return compiler_import(c, s); |
| 3436 | case ImportFrom_kind: |
| 3437 | return compiler_from_import(c, s); |
| 3438 | case Global_kind: |
| 3439 | case Nonlocal_kind: |
| 3440 | break; |
| 3441 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3442 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3443 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3444 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | break; |
| 3446 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3447 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | case Continue_kind: |
| 3449 | return compiler_continue(c); |
| 3450 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3451 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3452 | case AsyncFunctionDef_kind: |
| 3453 | return compiler_function(c, s, 1); |
| 3454 | case AsyncWith_kind: |
| 3455 | return compiler_async_with(c, s, 0); |
| 3456 | case AsyncFor_kind: |
| 3457 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | static int |
| 3464 | unaryop(unaryop_ty op) |
| 3465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | switch (op) { |
| 3467 | case Invert: |
| 3468 | return UNARY_INVERT; |
| 3469 | case Not: |
| 3470 | return UNARY_NOT; |
| 3471 | case UAdd: |
| 3472 | return UNARY_POSITIVE; |
| 3473 | case USub: |
| 3474 | return UNARY_NEGATIVE; |
| 3475 | default: |
| 3476 | PyErr_Format(PyExc_SystemError, |
| 3477 | "unary op %d should not be possible", op); |
| 3478 | return 0; |
| 3479 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3483 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | switch (op) { |
| 3486 | case Add: |
| 3487 | return BINARY_ADD; |
| 3488 | case Sub: |
| 3489 | return BINARY_SUBTRACT; |
| 3490 | case Mult: |
| 3491 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3492 | case MatMult: |
| 3493 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3494 | case Div: |
| 3495 | return BINARY_TRUE_DIVIDE; |
| 3496 | case Mod: |
| 3497 | return BINARY_MODULO; |
| 3498 | case Pow: |
| 3499 | return BINARY_POWER; |
| 3500 | case LShift: |
| 3501 | return BINARY_LSHIFT; |
| 3502 | case RShift: |
| 3503 | return BINARY_RSHIFT; |
| 3504 | case BitOr: |
| 3505 | return BINARY_OR; |
| 3506 | case BitXor: |
| 3507 | return BINARY_XOR; |
| 3508 | case BitAnd: |
| 3509 | return BINARY_AND; |
| 3510 | case FloorDiv: |
| 3511 | return BINARY_FLOOR_DIVIDE; |
| 3512 | default: |
| 3513 | PyErr_Format(PyExc_SystemError, |
| 3514 | "binary op %d should not be possible", op); |
| 3515 | return 0; |
| 3516 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3520 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | switch (op) { |
| 3523 | case Add: |
| 3524 | return INPLACE_ADD; |
| 3525 | case Sub: |
| 3526 | return INPLACE_SUBTRACT; |
| 3527 | case Mult: |
| 3528 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3529 | case MatMult: |
| 3530 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | case Div: |
| 3532 | return INPLACE_TRUE_DIVIDE; |
| 3533 | case Mod: |
| 3534 | return INPLACE_MODULO; |
| 3535 | case Pow: |
| 3536 | return INPLACE_POWER; |
| 3537 | case LShift: |
| 3538 | return INPLACE_LSHIFT; |
| 3539 | case RShift: |
| 3540 | return INPLACE_RSHIFT; |
| 3541 | case BitOr: |
| 3542 | return INPLACE_OR; |
| 3543 | case BitXor: |
| 3544 | return INPLACE_XOR; |
| 3545 | case BitAnd: |
| 3546 | return INPLACE_AND; |
| 3547 | case FloorDiv: |
| 3548 | return INPLACE_FLOOR_DIVIDE; |
| 3549 | default: |
| 3550 | PyErr_Format(PyExc_SystemError, |
| 3551 | "inplace binary op %d should not be possible", op); |
| 3552 | return 0; |
| 3553 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | static int |
| 3557 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3558 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3559 | int op, scope; |
| 3560 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | PyObject *dict = c->u->u_names; |
| 3564 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3565 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3566 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3567 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3568 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3569 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3570 | if (forbidden_name(c, name, ctx)) |
| 3571 | return 0; |
| 3572 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3573 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3574 | if (!mangled) |
| 3575 | return 0; |
| 3576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3577 | op = 0; |
| 3578 | optype = OP_NAME; |
| 3579 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3580 | switch (scope) { |
| 3581 | case FREE: |
| 3582 | dict = c->u->u_freevars; |
| 3583 | optype = OP_DEREF; |
| 3584 | break; |
| 3585 | case CELL: |
| 3586 | dict = c->u->u_cellvars; |
| 3587 | optype = OP_DEREF; |
| 3588 | break; |
| 3589 | case LOCAL: |
| 3590 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3591 | optype = OP_FAST; |
| 3592 | break; |
| 3593 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3594 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 | optype = OP_GLOBAL; |
| 3596 | break; |
| 3597 | case GLOBAL_EXPLICIT: |
| 3598 | optype = OP_GLOBAL; |
| 3599 | break; |
| 3600 | default: |
| 3601 | /* scope can be 0 */ |
| 3602 | break; |
| 3603 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3605 | /* 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] | 3606 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | switch (optype) { |
| 3609 | case OP_DEREF: |
| 3610 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3611 | case Load: |
| 3612 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3613 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3614 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3615 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | } |
| 3617 | break; |
| 3618 | case OP_FAST: |
| 3619 | switch (ctx) { |
| 3620 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3621 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3623 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3624 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | return 1; |
| 3626 | case OP_GLOBAL: |
| 3627 | switch (ctx) { |
| 3628 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3629 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | } |
| 3632 | break; |
| 3633 | case OP_NAME: |
| 3634 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3635 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3636 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | } |
| 3639 | break; |
| 3640 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3642 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3643 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | Py_DECREF(mangled); |
| 3645 | if (arg < 0) |
| 3646 | return 0; |
| 3647 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | static int |
| 3651 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3654 | int jumpi; |
| 3655 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3656 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3658 | assert(e->kind == BoolOp_kind); |
| 3659 | if (e->v.BoolOp.op == And) |
| 3660 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3661 | else |
| 3662 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3663 | end = compiler_new_block(c); |
| 3664 | if (end == NULL) |
| 3665 | return 0; |
| 3666 | s = e->v.BoolOp.values; |
| 3667 | n = asdl_seq_LEN(s) - 1; |
| 3668 | assert(n >= 0); |
| 3669 | for (i = 0; i < n; ++i) { |
| 3670 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3671 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3672 | basicblock *next = compiler_new_block(c); |
| 3673 | if (next == NULL) { |
| 3674 | return 0; |
| 3675 | } |
| 3676 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3677 | } |
| 3678 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3679 | compiler_use_next_block(c, end); |
| 3680 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3684 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3685 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3686 | { |
| 3687 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3688 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3689 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3690 | PyObject *folded = PyTuple_New(n); |
| 3691 | if (folded == NULL) { |
| 3692 | return 0; |
| 3693 | } |
| 3694 | PyObject *val; |
| 3695 | for (i = 0; i < n; i++) { |
| 3696 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3697 | Py_INCREF(val); |
| 3698 | PyTuple_SET_ITEM(folded, i, val); |
| 3699 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3700 | if (tuple) { |
| 3701 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3702 | } else { |
| 3703 | if (add == SET_ADD) { |
| 3704 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3705 | if (folded == NULL) { |
| 3706 | return 0; |
| 3707 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3708 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3709 | ADDOP_I(c, build, pushed); |
| 3710 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3711 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3712 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3713 | return 1; |
| 3714 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3715 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3716 | for (i = 0; i < n; i++) { |
| 3717 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3718 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3719 | seen_star = 1; |
| 3720 | } |
| 3721 | } |
| 3722 | if (seen_star) { |
| 3723 | seen_star = 0; |
| 3724 | for (i = 0; i < n; i++) { |
| 3725 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3726 | if (elt->kind == Starred_kind) { |
| 3727 | if (seen_star == 0) { |
| 3728 | ADDOP_I(c, build, i+pushed); |
| 3729 | seen_star = 1; |
| 3730 | } |
| 3731 | VISIT(c, expr, elt->v.Starred.value); |
| 3732 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3733 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3734 | else { |
| 3735 | VISIT(c, expr, elt); |
| 3736 | if (seen_star) { |
| 3737 | ADDOP_I(c, add, 1); |
| 3738 | } |
| 3739 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3740 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3741 | assert(seen_star); |
| 3742 | if (tuple) { |
| 3743 | ADDOP(c, LIST_TO_TUPLE); |
| 3744 | } |
| 3745 | } |
| 3746 | else { |
| 3747 | for (i = 0; i < n; i++) { |
| 3748 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3749 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3750 | } |
| 3751 | if (tuple) { |
| 3752 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3753 | } else { |
| 3754 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3755 | } |
| 3756 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3757 | return 1; |
| 3758 | } |
| 3759 | |
| 3760 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3761 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3762 | { |
| 3763 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3764 | Py_ssize_t i; |
| 3765 | int seen_star = 0; |
| 3766 | for (i = 0; i < n; i++) { |
| 3767 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3768 | if (elt->kind == Starred_kind && !seen_star) { |
| 3769 | if ((i >= (1 << 8)) || |
| 3770 | (n-i-1 >= (INT_MAX >> 8))) |
| 3771 | return compiler_error(c, |
| 3772 | "too many expressions in " |
| 3773 | "star-unpacking assignment"); |
| 3774 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3775 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3776 | } |
| 3777 | else if (elt->kind == Starred_kind) { |
| 3778 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3779 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3780 | } |
| 3781 | } |
| 3782 | if (!seen_star) { |
| 3783 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3784 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3785 | for (i = 0; i < n; i++) { |
| 3786 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3787 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3788 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3789 | return 1; |
| 3790 | } |
| 3791 | |
| 3792 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3793 | compiler_list(struct compiler *c, expr_ty e) |
| 3794 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3795 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3796 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3797 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3798 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3799 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3800 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3801 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3802 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3803 | else |
| 3804 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3805 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | static int |
| 3809 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3810 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3811 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3812 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3813 | return assignment_helper(c, elts); |
| 3814 | } |
| 3815 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3816 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3817 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3818 | } |
| 3819 | else |
| 3820 | VISIT_SEQ(c, expr, elts); |
| 3821 | return 1; |
| 3822 | } |
| 3823 | |
| 3824 | static int |
| 3825 | compiler_set(struct compiler *c, expr_ty e) |
| 3826 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3827 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3828 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3832 | 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] | 3833 | { |
| 3834 | Py_ssize_t i; |
| 3835 | for (i = begin; i < end; i++) { |
| 3836 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3837 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3838 | return 0; |
| 3839 | } |
| 3840 | return 1; |
| 3841 | } |
| 3842 | |
| 3843 | static int |
| 3844 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3845 | { |
| 3846 | Py_ssize_t i, n = end - begin; |
| 3847 | PyObject *keys, *key; |
| 3848 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3849 | for (i = begin; i < end; i++) { |
| 3850 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3851 | } |
| 3852 | keys = PyTuple_New(n); |
| 3853 | if (keys == NULL) { |
| 3854 | return 0; |
| 3855 | } |
| 3856 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3857 | 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] | 3858 | Py_INCREF(key); |
| 3859 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3860 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3861 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3862 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3863 | } |
| 3864 | else { |
| 3865 | for (i = begin; i < end; i++) { |
| 3866 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3867 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3868 | } |
| 3869 | ADDOP_I(c, BUILD_MAP, n); |
| 3870 | } |
| 3871 | return 1; |
| 3872 | } |
| 3873 | |
| 3874 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3875 | compiler_dict(struct compiler *c, expr_ty e) |
| 3876 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3877 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3878 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3879 | int is_unpacking = 0; |
| 3880 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3881 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3882 | elements = 0; |
| 3883 | for (i = 0; i < n; i++) { |
| 3884 | 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] | 3885 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3886 | if (elements) { |
| 3887 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3888 | return 0; |
| 3889 | } |
| 3890 | if (have_dict) { |
| 3891 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3892 | } |
| 3893 | have_dict = 1; |
| 3894 | elements = 0; |
| 3895 | } |
| 3896 | if (have_dict == 0) { |
| 3897 | ADDOP_I(c, BUILD_MAP, 0); |
| 3898 | have_dict = 1; |
| 3899 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3900 | 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] | 3901 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3902 | } |
| 3903 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3904 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3905 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3906 | return 0; |
| 3907 | } |
| 3908 | if (have_dict) { |
| 3909 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3910 | } |
| 3911 | have_dict = 1; |
| 3912 | elements = 0; |
| 3913 | } |
| 3914 | else { |
| 3915 | elements++; |
| 3916 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3917 | } |
| 3918 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3919 | if (elements) { |
| 3920 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3921 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3922 | } |
| 3923 | if (have_dict) { |
| 3924 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3925 | } |
| 3926 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3927 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3928 | if (!have_dict) { |
| 3929 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | } |
| 3931 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | static int |
| 3935 | compiler_compare(struct compiler *c, expr_ty e) |
| 3936 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3937 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3938 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3939 | if (!check_compare(c, e)) { |
| 3940 | return 0; |
| 3941 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3942 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3943 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3944 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3945 | if (n == 0) { |
| 3946 | 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] | 3947 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3948 | } |
| 3949 | else { |
| 3950 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3951 | if (cleanup == NULL) |
| 3952 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3953 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3954 | VISIT(c, expr, |
| 3955 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3956 | ADDOP(c, DUP_TOP); |
| 3957 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3958 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3959 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3960 | NEXT_BLOCK(c); |
| 3961 | } |
| 3962 | 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] | 3963 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3964 | basicblock *end = compiler_new_block(c); |
| 3965 | if (end == NULL) |
| 3966 | return 0; |
Mark Shannon | 127dde5 | 2021-01-04 18:06:55 +0000 | [diff] [blame] | 3967 | ADDOP_JUMP_NOLINE(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3968 | compiler_use_next_block(c, cleanup); |
| 3969 | ADDOP(c, ROT_TWO); |
| 3970 | ADDOP(c, POP_TOP); |
| 3971 | compiler_use_next_block(c, end); |
| 3972 | } |
| 3973 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3976 | static PyTypeObject * |
| 3977 | infer_type(expr_ty e) |
| 3978 | { |
| 3979 | switch (e->kind) { |
| 3980 | case Tuple_kind: |
| 3981 | return &PyTuple_Type; |
| 3982 | case List_kind: |
| 3983 | case ListComp_kind: |
| 3984 | return &PyList_Type; |
| 3985 | case Dict_kind: |
| 3986 | case DictComp_kind: |
| 3987 | return &PyDict_Type; |
| 3988 | case Set_kind: |
| 3989 | case SetComp_kind: |
| 3990 | return &PySet_Type; |
| 3991 | case GeneratorExp_kind: |
| 3992 | return &PyGen_Type; |
| 3993 | case Lambda_kind: |
| 3994 | return &PyFunction_Type; |
| 3995 | case JoinedStr_kind: |
| 3996 | case FormattedValue_kind: |
| 3997 | return &PyUnicode_Type; |
| 3998 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3999 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4000 | default: |
| 4001 | return NULL; |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | static int |
| 4006 | check_caller(struct compiler *c, expr_ty e) |
| 4007 | { |
| 4008 | switch (e->kind) { |
| 4009 | case Constant_kind: |
| 4010 | case Tuple_kind: |
| 4011 | case List_kind: |
| 4012 | case ListComp_kind: |
| 4013 | case Dict_kind: |
| 4014 | case DictComp_kind: |
| 4015 | case Set_kind: |
| 4016 | case SetComp_kind: |
| 4017 | case GeneratorExp_kind: |
| 4018 | case JoinedStr_kind: |
| 4019 | case FormattedValue_kind: |
| 4020 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4021 | "perhaps you missed a comma?", |
| 4022 | infer_type(e)->tp_name); |
| 4023 | default: |
| 4024 | return 1; |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | static int |
| 4029 | check_subscripter(struct compiler *c, expr_ty e) |
| 4030 | { |
| 4031 | PyObject *v; |
| 4032 | |
| 4033 | switch (e->kind) { |
| 4034 | case Constant_kind: |
| 4035 | v = e->v.Constant.value; |
| 4036 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4037 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4038 | PyAnySet_Check(v))) |
| 4039 | { |
| 4040 | return 1; |
| 4041 | } |
| 4042 | /* fall through */ |
| 4043 | case Set_kind: |
| 4044 | case SetComp_kind: |
| 4045 | case GeneratorExp_kind: |
| 4046 | case Lambda_kind: |
| 4047 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4048 | "perhaps you missed a comma?", |
| 4049 | infer_type(e)->tp_name); |
| 4050 | default: |
| 4051 | return 1; |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4056 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4057 | { |
| 4058 | PyObject *v; |
| 4059 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4060 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4061 | if (index_type == NULL |
| 4062 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4063 | || index_type == &PySlice_Type) { |
| 4064 | return 1; |
| 4065 | } |
| 4066 | |
| 4067 | switch (e->kind) { |
| 4068 | case Constant_kind: |
| 4069 | v = e->v.Constant.value; |
| 4070 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4071 | return 1; |
| 4072 | } |
| 4073 | /* fall through */ |
| 4074 | case Tuple_kind: |
| 4075 | case List_kind: |
| 4076 | case ListComp_kind: |
| 4077 | case JoinedStr_kind: |
| 4078 | case FormattedValue_kind: |
| 4079 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4080 | "not %.200s; " |
| 4081 | "perhaps you missed a comma?", |
| 4082 | infer_type(e)->tp_name, |
| 4083 | index_type->tp_name); |
| 4084 | default: |
| 4085 | return 1; |
| 4086 | } |
| 4087 | } |
| 4088 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4089 | // 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] | 4090 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4091 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4092 | { |
| 4093 | Py_ssize_t argsl, i; |
| 4094 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4095 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4096 | |
| 4097 | /* Check that the call node is an attribute access, and that |
| 4098 | the call doesn't have keyword parameters. */ |
| 4099 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4100 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4101 | return -1; |
| 4102 | |
| 4103 | /* Check that there are no *varargs types of arguments. */ |
| 4104 | argsl = asdl_seq_LEN(args); |
| 4105 | for (i = 0; i < argsl; i++) { |
| 4106 | expr_ty elt = asdl_seq_GET(args, i); |
| 4107 | if (elt->kind == Starred_kind) { |
| 4108 | return -1; |
| 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | /* Alright, we can optimize the code. */ |
| 4113 | VISIT(c, expr, meth->v.Attribute.value); |
| 4114 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4115 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4116 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4117 | return 1; |
| 4118 | } |
| 4119 | |
| 4120 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4121 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4122 | { |
| 4123 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4124 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4125 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4126 | if (key->arg == NULL) { |
| 4127 | continue; |
| 4128 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4129 | if (forbidden_name(c, key->arg, Store)) { |
| 4130 | return -1; |
| 4131 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4132 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4133 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4134 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
| 4135 | PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg); |
| 4136 | if (msg == NULL) { |
| 4137 | return -1; |
| 4138 | } |
| 4139 | c->u->u_col_offset = other->col_offset; |
| 4140 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
| 4141 | Py_DECREF(msg); |
| 4142 | return -1; |
| 4143 | } |
| 4144 | } |
| 4145 | } |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
| 4149 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4150 | compiler_call(struct compiler *c, expr_ty e) |
| 4151 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4152 | int ret = maybe_optimize_method_call(c, e); |
| 4153 | if (ret >= 0) { |
| 4154 | return ret; |
| 4155 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4156 | if (!check_caller(c, e->v.Call.func)) { |
| 4157 | return 0; |
| 4158 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4159 | VISIT(c, expr, e->v.Call.func); |
| 4160 | return compiler_call_helper(c, 0, |
| 4161 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4162 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4165 | static int |
| 4166 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4167 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4168 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4169 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4170 | 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] | 4171 | return 1; |
| 4172 | } |
| 4173 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4174 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4175 | static int |
| 4176 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4177 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4178 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4179 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4180 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4181 | Convert the conversion char to 3 bits: |
| 4182 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4183 | !s : 001 0x1 FVC_STR |
| 4184 | !r : 010 0x2 FVC_REPR |
| 4185 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4186 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4187 | next bit is whether or not we have a format spec: |
| 4188 | yes : 100 0x4 |
| 4189 | no : 000 0x0 |
| 4190 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4191 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4192 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4193 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4194 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4195 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4196 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4197 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4198 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4199 | case 's': oparg = FVC_STR; break; |
| 4200 | case 'r': oparg = FVC_REPR; break; |
| 4201 | case 'a': oparg = FVC_ASCII; break; |
| 4202 | case -1: oparg = FVC_NONE; break; |
| 4203 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4204 | PyErr_Format(PyExc_SystemError, |
| 4205 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4206 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4207 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4208 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4209 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4210 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4211 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4212 | } |
| 4213 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4214 | /* And push our opcode and oparg */ |
| 4215 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4216 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4217 | return 1; |
| 4218 | } |
| 4219 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4220 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4221 | 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] | 4222 | { |
| 4223 | Py_ssize_t i, n = end - begin; |
| 4224 | keyword_ty kw; |
| 4225 | PyObject *keys, *key; |
| 4226 | assert(n > 0); |
| 4227 | if (n > 1) { |
| 4228 | for (i = begin; i < end; i++) { |
| 4229 | kw = asdl_seq_GET(keywords, i); |
| 4230 | VISIT(c, expr, kw->value); |
| 4231 | } |
| 4232 | keys = PyTuple_New(n); |
| 4233 | if (keys == NULL) { |
| 4234 | return 0; |
| 4235 | } |
| 4236 | for (i = begin; i < end; i++) { |
| 4237 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4238 | Py_INCREF(key); |
| 4239 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4240 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4241 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4242 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4243 | } |
| 4244 | else { |
| 4245 | /* a for loop only executes once */ |
| 4246 | for (i = begin; i < end; i++) { |
| 4247 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4248 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4249 | VISIT(c, expr, kw->value); |
| 4250 | } |
| 4251 | ADDOP_I(c, BUILD_MAP, n); |
| 4252 | } |
| 4253 | return 1; |
| 4254 | } |
| 4255 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4256 | /* shared code between compiler_call and compiler_class */ |
| 4257 | static int |
| 4258 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4259 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4260 | asdl_expr_seq *args, |
| 4261 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4262 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4263 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4264 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4265 | if (validate_keywords(c, keywords) == -1) { |
| 4266 | return 0; |
| 4267 | } |
| 4268 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4269 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4270 | nkwelts = asdl_seq_LEN(keywords); |
| 4271 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4272 | for (i = 0; i < nelts; i++) { |
| 4273 | expr_ty elt = asdl_seq_GET(args, i); |
| 4274 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4275 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4276 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4277 | } |
| 4278 | for (i = 0; i < nkwelts; i++) { |
| 4279 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4280 | if (kw->arg == NULL) { |
| 4281 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4282 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4284 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4285 | /* No * or ** args, so can use faster calling sequence */ |
| 4286 | for (i = 0; i < nelts; i++) { |
| 4287 | expr_ty elt = asdl_seq_GET(args, i); |
| 4288 | assert(elt->kind != Starred_kind); |
| 4289 | VISIT(c, expr, elt); |
| 4290 | } |
| 4291 | if (nkwelts) { |
| 4292 | PyObject *names; |
| 4293 | VISIT_SEQ(c, keyword, keywords); |
| 4294 | names = PyTuple_New(nkwelts); |
| 4295 | if (names == NULL) { |
| 4296 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4297 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4298 | for (i = 0; i < nkwelts; i++) { |
| 4299 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4300 | Py_INCREF(kw->arg); |
| 4301 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4302 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4303 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4304 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4305 | return 1; |
| 4306 | } |
| 4307 | else { |
| 4308 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
| 4312 | ex_call: |
| 4313 | |
| 4314 | /* Do positional arguments. */ |
| 4315 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4316 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4317 | } |
| 4318 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4319 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4320 | return 0; |
| 4321 | } |
| 4322 | /* Then keyword arguments */ |
| 4323 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4324 | /* Has a new dict been pushed */ |
| 4325 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4326 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4327 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4328 | for (i = 0; i < nkwelts; i++) { |
| 4329 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4330 | if (kw->arg == NULL) { |
| 4331 | /* A keyword argument unpacking. */ |
| 4332 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4333 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4334 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4335 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4336 | if (have_dict) { |
| 4337 | ADDOP_I(c, DICT_MERGE, 1); |
| 4338 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4339 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4340 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4341 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4342 | if (!have_dict) { |
| 4343 | ADDOP_I(c, BUILD_MAP, 0); |
| 4344 | have_dict = 1; |
| 4345 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4346 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4347 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4348 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4349 | else { |
| 4350 | nseen++; |
| 4351 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4352 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4353 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4354 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4355 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4356 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4357 | } |
| 4358 | if (have_dict) { |
| 4359 | ADDOP_I(c, DICT_MERGE, 1); |
| 4360 | } |
| 4361 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4362 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4363 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4365 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4366 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4369 | |
| 4370 | /* List and set comprehensions and generator expressions work by creating a |
| 4371 | nested function to perform the actual iteration. This means that the |
| 4372 | iteration variables don't leak into the current scope. |
| 4373 | The defined function is called immediately following its definition, with the |
| 4374 | result of that call being the result of the expression. |
| 4375 | The LC/SC version returns the populated container, while the GE version is |
| 4376 | flagged in symtable.c as a generator, so it returns the generator object |
| 4377 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4378 | |
| 4379 | Possible cleanups: |
| 4380 | - iterate over the generator sequence instead of using recursion |
| 4381 | */ |
| 4382 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4383 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4384 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4386 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4387 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4389 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4390 | comprehension_ty gen; |
| 4391 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4392 | if (gen->is_async) { |
| 4393 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4394 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4395 | } else { |
| 4396 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4397 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | static int |
| 4402 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4403 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4404 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4405 | expr_ty elt, expr_ty val, int type) |
| 4406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | /* generate code for the iterator, then each of the ifs, |
| 4408 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | comprehension_ty gen; |
| 4411 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4412 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4414 | start = compiler_new_block(c); |
| 4415 | skip = compiler_new_block(c); |
| 4416 | if_cleanup = compiler_new_block(c); |
| 4417 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4420 | anchor == NULL) |
| 4421 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | if (gen_index == 0) { |
| 4426 | /* Receive outermost iter as an implicit argument */ |
| 4427 | c->u->u_argcount = 1; |
| 4428 | ADDOP_I(c, LOAD_FAST, 0); |
| 4429 | } |
| 4430 | else { |
| 4431 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4432 | /* Fast path for the temporary variable assignment idiom: |
| 4433 | for y in [f(x)] |
| 4434 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4435 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4436 | switch (gen->iter->kind) { |
| 4437 | case List_kind: |
| 4438 | elts = gen->iter->v.List.elts; |
| 4439 | break; |
| 4440 | case Tuple_kind: |
| 4441 | elts = gen->iter->v.Tuple.elts; |
| 4442 | break; |
| 4443 | default: |
| 4444 | elts = NULL; |
| 4445 | } |
| 4446 | if (asdl_seq_LEN(elts) == 1) { |
| 4447 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4448 | if (elt->kind != Starred_kind) { |
| 4449 | VISIT(c, expr, elt); |
| 4450 | start = NULL; |
| 4451 | } |
| 4452 | } |
| 4453 | if (start) { |
| 4454 | VISIT(c, expr, gen->iter); |
| 4455 | ADDOP(c, GET_ITER); |
| 4456 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4458 | if (start) { |
| 4459 | depth++; |
| 4460 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4461 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4462 | NEXT_BLOCK(c); |
| 4463 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4466 | /* XXX this needs to be cleaned up...a lot! */ |
| 4467 | n = asdl_seq_LEN(gen->ifs); |
| 4468 | for (i = 0; i < n; i++) { |
| 4469 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4470 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4471 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | NEXT_BLOCK(c); |
| 4473 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4476 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4477 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | elt, val, type)) |
| 4479 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | /* only append after the last for generator */ |
| 4482 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4483 | /* comprehension specific code */ |
| 4484 | switch (type) { |
| 4485 | case COMP_GENEXP: |
| 4486 | VISIT(c, expr, elt); |
| 4487 | ADDOP(c, YIELD_VALUE); |
| 4488 | ADDOP(c, POP_TOP); |
| 4489 | break; |
| 4490 | case COMP_LISTCOMP: |
| 4491 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4492 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | break; |
| 4494 | case COMP_SETCOMP: |
| 4495 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4496 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | break; |
| 4498 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4499 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4502 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4503 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | break; |
| 4505 | default: |
| 4506 | return 0; |
| 4507 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | compiler_use_next_block(c, skip); |
| 4510 | } |
| 4511 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4512 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4513 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4514 | compiler_use_next_block(c, anchor); |
| 4515 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | |
| 4517 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4521 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4522 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4523 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4524 | expr_ty elt, expr_ty val, int type) |
| 4525 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4526 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4527 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4528 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4529 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4530 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4531 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4532 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4533 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4538 | |
| 4539 | if (gen_index == 0) { |
| 4540 | /* Receive outermost iter as an implicit argument */ |
| 4541 | c->u->u_argcount = 1; |
| 4542 | ADDOP_I(c, LOAD_FAST, 0); |
| 4543 | } |
| 4544 | else { |
| 4545 | /* Sub-iter - calculate on the fly */ |
| 4546 | VISIT(c, expr, gen->iter); |
| 4547 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4550 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4551 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4552 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4553 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4554 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4555 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4556 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4557 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4558 | |
| 4559 | n = asdl_seq_LEN(gen->ifs); |
| 4560 | for (i = 0; i < n; i++) { |
| 4561 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4562 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4563 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4564 | NEXT_BLOCK(c); |
| 4565 | } |
| 4566 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4567 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4568 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4569 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4570 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4571 | elt, val, type)) |
| 4572 | return 0; |
| 4573 | |
| 4574 | /* only append after the last for generator */ |
| 4575 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4576 | /* comprehension specific code */ |
| 4577 | switch (type) { |
| 4578 | case COMP_GENEXP: |
| 4579 | VISIT(c, expr, elt); |
| 4580 | ADDOP(c, YIELD_VALUE); |
| 4581 | ADDOP(c, POP_TOP); |
| 4582 | break; |
| 4583 | case COMP_LISTCOMP: |
| 4584 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4585 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4586 | break; |
| 4587 | case COMP_SETCOMP: |
| 4588 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4589 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4590 | break; |
| 4591 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4592 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4593 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4594 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4595 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4596 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4597 | break; |
| 4598 | default: |
| 4599 | return 0; |
| 4600 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4601 | } |
| 4602 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4603 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4604 | |
| 4605 | compiler_use_next_block(c, except); |
| 4606 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4607 | |
| 4608 | return 1; |
| 4609 | } |
| 4610 | |
| 4611 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4612 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4613 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4614 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4617 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4618 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4619 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4620 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4621 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4622 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4623 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4624 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4625 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4626 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4627 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4633 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4634 | 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] | 4635 | compiler_error(c, "asynchronous comprehension outside of " |
| 4636 | "an asynchronous function"); |
| 4637 | goto error_in_scope; |
| 4638 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | if (type != COMP_GENEXP) { |
| 4641 | int op; |
| 4642 | switch (type) { |
| 4643 | case COMP_LISTCOMP: |
| 4644 | op = BUILD_LIST; |
| 4645 | break; |
| 4646 | case COMP_SETCOMP: |
| 4647 | op = BUILD_SET; |
| 4648 | break; |
| 4649 | case COMP_DICTCOMP: |
| 4650 | op = BUILD_MAP; |
| 4651 | break; |
| 4652 | default: |
| 4653 | PyErr_Format(PyExc_SystemError, |
| 4654 | "unknown comprehension type %d", type); |
| 4655 | goto error_in_scope; |
| 4656 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4658 | ADDOP_I(c, op, 0); |
| 4659 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4660 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4661 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4662 | val, type)) |
| 4663 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4664 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | if (type != COMP_GENEXP) { |
| 4666 | ADDOP(c, RETURN_VALUE); |
| 4667 | } |
| 4668 | |
| 4669 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4670 | qualname = c->u->u_qualname; |
| 4671 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4673 | if (top_level_await && is_async_generator){ |
| 4674 | c->u->u_ste->ste_coroutine = 1; |
| 4675 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4676 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | goto error; |
| 4678 | |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4679 | if (!compiler_make_closure(c, co, 0, qualname)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4680 | goto error; |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 4681 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4682 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | Py_DECREF(co); |
| 4684 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4685 | VISIT(c, expr, outermost->iter); |
| 4686 | |
| 4687 | if (outermost->is_async) { |
| 4688 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4689 | } else { |
| 4690 | ADDOP(c, GET_ITER); |
| 4691 | } |
| 4692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4694 | |
| 4695 | if (is_async_generator && type != COMP_GENEXP) { |
| 4696 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4697 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4698 | ADDOP(c, YIELD_FROM); |
| 4699 | } |
| 4700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4701 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4702 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4704 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4705 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | Py_XDECREF(co); |
| 4707 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4711 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | static identifier name; |
| 4714 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4715 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4716 | if (!name) |
| 4717 | return 0; |
| 4718 | } |
| 4719 | assert(e->kind == GeneratorExp_kind); |
| 4720 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4721 | e->v.GeneratorExp.generators, |
| 4722 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4726 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | static identifier name; |
| 4729 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4730 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4731 | if (!name) |
| 4732 | return 0; |
| 4733 | } |
| 4734 | assert(e->kind == ListComp_kind); |
| 4735 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4736 | e->v.ListComp.generators, |
| 4737 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | static int |
| 4741 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4742 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4743 | static identifier name; |
| 4744 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4745 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | if (!name) |
| 4747 | return 0; |
| 4748 | } |
| 4749 | assert(e->kind == SetComp_kind); |
| 4750 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4751 | e->v.SetComp.generators, |
| 4752 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | |
| 4756 | static int |
| 4757 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | static identifier name; |
| 4760 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4761 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | if (!name) |
| 4763 | return 0; |
| 4764 | } |
| 4765 | assert(e->kind == DictComp_kind); |
| 4766 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4767 | e->v.DictComp.generators, |
| 4768 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4773 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | VISIT(c, expr, k->value); |
| 4776 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4780 | whether they are true or false. |
| 4781 | |
| 4782 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4783 | */ |
| 4784 | |
| 4785 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4786 | compiler_with_except_finish(struct compiler *c) { |
| 4787 | basicblock *exit; |
| 4788 | exit = compiler_new_block(c); |
| 4789 | if (exit == NULL) |
| 4790 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4791 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4792 | NEXT_BLOCK(c); |
Mark Shannon | bf353f3 | 2020-12-17 13:55:28 +0000 | [diff] [blame] | 4793 | ADDOP_I(c, RERAISE, 1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4794 | compiler_use_next_block(c, exit); |
| 4795 | ADDOP(c, POP_TOP); |
| 4796 | ADDOP(c, POP_TOP); |
| 4797 | ADDOP(c, POP_TOP); |
| 4798 | ADDOP(c, POP_EXCEPT); |
| 4799 | ADDOP(c, POP_TOP); |
| 4800 | return 1; |
| 4801 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4802 | |
| 4803 | /* |
| 4804 | Implements the async with statement. |
| 4805 | |
| 4806 | The semantics outlined in that PEP are as follows: |
| 4807 | |
| 4808 | async with EXPR as VAR: |
| 4809 | BLOCK |
| 4810 | |
| 4811 | It is implemented roughly as: |
| 4812 | |
| 4813 | context = EXPR |
| 4814 | exit = context.__aexit__ # not calling it |
| 4815 | value = await context.__aenter__() |
| 4816 | try: |
| 4817 | VAR = value # if VAR present in the syntax |
| 4818 | BLOCK |
| 4819 | finally: |
| 4820 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4821 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4822 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4823 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4824 | if not (await exit(*exc)): |
| 4825 | raise |
| 4826 | */ |
| 4827 | static int |
| 4828 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4829 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4830 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4831 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4832 | |
| 4833 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4834 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4835 | c->u->u_ste->ste_coroutine = 1; |
| 4836 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4837 | return compiler_error(c, "'async with' outside async function"); |
| 4838 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4839 | |
| 4840 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4841 | final = compiler_new_block(c); |
| 4842 | exit = compiler_new_block(c); |
| 4843 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4844 | return 0; |
| 4845 | |
| 4846 | /* Evaluate EXPR */ |
| 4847 | VISIT(c, expr, item->context_expr); |
| 4848 | |
| 4849 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4850 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4851 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4852 | ADDOP(c, YIELD_FROM); |
| 4853 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4854 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4855 | |
| 4856 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4857 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4858 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4859 | return 0; |
| 4860 | } |
| 4861 | |
| 4862 | if (item->optional_vars) { |
| 4863 | VISIT(c, expr, item->optional_vars); |
| 4864 | } |
| 4865 | else { |
| 4866 | /* Discard result from context.__aenter__() */ |
| 4867 | ADDOP(c, POP_TOP); |
| 4868 | } |
| 4869 | |
| 4870 | pos++; |
| 4871 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4872 | /* BLOCK code */ |
| 4873 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4874 | else if (!compiler_async_with(c, s, pos)) |
| 4875 | return 0; |
| 4876 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4877 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4878 | ADDOP(c, POP_BLOCK); |
| 4879 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4880 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4881 | /* For successful outcome: |
| 4882 | * call __exit__(None, None, None) |
| 4883 | */ |
| 4884 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4885 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4886 | ADDOP(c, GET_AWAITABLE); |
| 4887 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4888 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4889 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4890 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4891 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4892 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4893 | |
| 4894 | /* For exceptional outcome: */ |
| 4895 | compiler_use_next_block(c, final); |
| 4896 | |
| 4897 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4898 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4899 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4900 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4901 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4902 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4903 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4904 | return 1; |
| 4905 | } |
| 4906 | |
| 4907 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4908 | /* |
| 4909 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4910 | with EXPR as VAR: |
| 4911 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4912 | is implemented as: |
| 4913 | <code for EXPR> |
| 4914 | SETUP_WITH E |
| 4915 | <code to store to VAR> or POP_TOP |
| 4916 | <code for BLOCK> |
| 4917 | LOAD_CONST (None, None, None) |
| 4918 | CALL_FUNCTION_EX 0 |
| 4919 | JUMP_FORWARD EXIT |
| 4920 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4921 | POP_JUMP_IF_TRUE T: |
| 4922 | RERAISE |
| 4923 | T: POP_TOP * 3 (remove exception from stack) |
| 4924 | POP_EXCEPT |
| 4925 | POP_TOP |
| 4926 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4927 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4928 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4929 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4930 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4931 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4932 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4933 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4934 | |
| 4935 | assert(s->kind == With_kind); |
| 4936 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4937 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4938 | final = compiler_new_block(c); |
| 4939 | exit = compiler_new_block(c); |
| 4940 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4941 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4942 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4943 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4944 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4945 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4946 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4947 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4948 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4949 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4950 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4951 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4954 | if (item->optional_vars) { |
| 4955 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4956 | } |
| 4957 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4958 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4959 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4962 | pos++; |
| 4963 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4964 | /* BLOCK code */ |
| 4965 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4966 | else if (!compiler_with(c, s, pos)) |
| 4967 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4968 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 4969 | |
| 4970 | /* Mark all following code as artificial */ |
| 4971 | c->u->u_lineno = -1; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4972 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4973 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4974 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4975 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4976 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4977 | /* For successful outcome: |
| 4978 | * call __exit__(None, None, None) |
| 4979 | */ |
| 4980 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4981 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4982 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4983 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4984 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4985 | /* For exceptional outcome: */ |
| 4986 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4987 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4988 | ADDOP(c, WITH_EXCEPT_START); |
| 4989 | compiler_with_except_finish(c); |
| 4990 | |
| 4991 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4992 | return 1; |
| 4993 | } |
| 4994 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4995 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4996 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4997 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4999 | case NamedExpr_kind: |
| 5000 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5001 | ADDOP(c, DUP_TOP); |
| 5002 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5003 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5004 | case BoolOp_kind: |
| 5005 | return compiler_boolop(c, e); |
| 5006 | case BinOp_kind: |
| 5007 | VISIT(c, expr, e->v.BinOp.left); |
| 5008 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5009 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | break; |
| 5011 | case UnaryOp_kind: |
| 5012 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5013 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5014 | break; |
| 5015 | case Lambda_kind: |
| 5016 | return compiler_lambda(c, e); |
| 5017 | case IfExp_kind: |
| 5018 | return compiler_ifexp(c, e); |
| 5019 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5020 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5022 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | case GeneratorExp_kind: |
| 5024 | return compiler_genexp(c, e); |
| 5025 | case ListComp_kind: |
| 5026 | return compiler_listcomp(c, e); |
| 5027 | case SetComp_kind: |
| 5028 | return compiler_setcomp(c, e); |
| 5029 | case DictComp_kind: |
| 5030 | return compiler_dictcomp(c, e); |
| 5031 | case Yield_kind: |
| 5032 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5033 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5034 | if (e->v.Yield.value) { |
| 5035 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5036 | } |
| 5037 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5038 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5040 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5042 | case YieldFrom_kind: |
| 5043 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5044 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5045 | |
| 5046 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5047 | return compiler_error(c, "'yield from' inside async function"); |
| 5048 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5049 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5050 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5051 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5052 | ADDOP(c, YIELD_FROM); |
| 5053 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5054 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5055 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5056 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5057 | return compiler_error(c, "'await' outside function"); |
| 5058 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5059 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5060 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5061 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5062 | return compiler_error(c, "'await' outside async function"); |
| 5063 | } |
| 5064 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5065 | |
| 5066 | VISIT(c, expr, e->v.Await.value); |
| 5067 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5068 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5069 | ADDOP(c, YIELD_FROM); |
| 5070 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5071 | case Compare_kind: |
| 5072 | return compiler_compare(c, e); |
| 5073 | case Call_kind: |
| 5074 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5075 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5076 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5077 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5078 | case JoinedStr_kind: |
| 5079 | return compiler_joined_str(c, e); |
| 5080 | case FormattedValue_kind: |
| 5081 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | /* The following exprs can be assignment targets. */ |
| 5083 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5084 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5086 | case Load: |
| 5087 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5088 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5090 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5091 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5093 | break; |
| 5094 | case Del: |
| 5095 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5096 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5097 | } |
| 5098 | break; |
| 5099 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5100 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | case Starred_kind: |
| 5102 | switch (e->v.Starred.ctx) { |
| 5103 | case Store: |
| 5104 | /* In all legitimate cases, the Starred node was already replaced |
| 5105 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5106 | return compiler_error(c, |
| 5107 | "starred assignment target must be in a list or tuple"); |
| 5108 | default: |
| 5109 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5110 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5111 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5112 | break; |
| 5113 | case Slice_kind: |
| 5114 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5115 | case Name_kind: |
| 5116 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5117 | /* child nodes of List and Tuple will have expr_context set */ |
| 5118 | case List_kind: |
| 5119 | return compiler_list(c, e); |
| 5120 | case Tuple_kind: |
| 5121 | return compiler_tuple(c, e); |
| 5122 | } |
| 5123 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5127 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5128 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5129 | int old_lineno = c->u->u_lineno; |
| 5130 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5131 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5132 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5133 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5134 | c->u->u_col_offset = old_col_offset; |
| 5135 | return res; |
| 5136 | } |
| 5137 | |
| 5138 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5139 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5140 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5141 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5142 | expr_ty e = s->v.AugAssign.target; |
| 5143 | |
| 5144 | int old_lineno = c->u->u_lineno; |
| 5145 | int old_col_offset = c->u->u_col_offset; |
| 5146 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5148 | switch (e->kind) { |
| 5149 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5150 | VISIT(c, expr, e->v.Attribute.value); |
| 5151 | ADDOP(c, DUP_TOP); |
| 5152 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5153 | break; |
| 5154 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5155 | VISIT(c, expr, e->v.Subscript.value); |
| 5156 | VISIT(c, expr, e->v.Subscript.slice); |
| 5157 | ADDOP(c, DUP_TOP_TWO); |
| 5158 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5159 | break; |
| 5160 | case Name_kind: |
| 5161 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5162 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5163 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | default: |
| 5165 | PyErr_Format(PyExc_SystemError, |
| 5166 | "invalid node type (%d) for augmented assignment", |
| 5167 | e->kind); |
| 5168 | return 0; |
| 5169 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5170 | |
| 5171 | c->u->u_lineno = old_lineno; |
| 5172 | c->u->u_col_offset = old_col_offset; |
| 5173 | |
| 5174 | VISIT(c, expr, s->v.AugAssign.value); |
| 5175 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5176 | |
| 5177 | SET_LOC(c, e); |
| 5178 | |
| 5179 | switch (e->kind) { |
| 5180 | case Attribute_kind: |
| 5181 | ADDOP(c, ROT_TWO); |
| 5182 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5183 | break; |
| 5184 | case Subscript_kind: |
| 5185 | ADDOP(c, ROT_THREE); |
| 5186 | ADDOP(c, STORE_SUBSCR); |
| 5187 | break; |
| 5188 | case Name_kind: |
| 5189 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5190 | default: |
| 5191 | Py_UNREACHABLE(); |
| 5192 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5197 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5198 | { |
| 5199 | VISIT(c, expr, e); |
| 5200 | ADDOP(c, POP_TOP); |
| 5201 | return 1; |
| 5202 | } |
| 5203 | |
| 5204 | static int |
| 5205 | check_annotation(struct compiler *c, stmt_ty s) |
| 5206 | { |
| 5207 | /* Annotations are only evaluated in a module or class. */ |
| 5208 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5209 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5210 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5211 | } |
| 5212 | return 1; |
| 5213 | } |
| 5214 | |
| 5215 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5216 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5217 | { |
| 5218 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5219 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5220 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5221 | 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] | 5222 | return 0; |
| 5223 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5224 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5225 | return 0; |
| 5226 | } |
| 5227 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5228 | return 0; |
| 5229 | } |
| 5230 | return 1; |
| 5231 | case Tuple_kind: { |
| 5232 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5233 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5234 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5235 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5236 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5237 | return 0; |
| 5238 | } |
| 5239 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5240 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5241 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5242 | default: |
| 5243 | return check_ann_expr(c, e); |
| 5244 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | static int |
| 5248 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5249 | { |
| 5250 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5251 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5252 | |
| 5253 | assert(s->kind == AnnAssign_kind); |
| 5254 | |
| 5255 | /* We perform the actual assignment first. */ |
| 5256 | if (s->v.AnnAssign.value) { |
| 5257 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5258 | VISIT(c, expr, targ); |
| 5259 | } |
| 5260 | switch (targ->kind) { |
| 5261 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5262 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5263 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5264 | /* If we have a simple name in a module or class, store annotation. */ |
| 5265 | if (s->v.AnnAssign.simple && |
| 5266 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5267 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5268 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5269 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5270 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5271 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5272 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5273 | } |
| 5274 | break; |
| 5275 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5276 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5277 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5278 | if (!s->v.AnnAssign.value && |
| 5279 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5280 | return 0; |
| 5281 | } |
| 5282 | break; |
| 5283 | case Subscript_kind: |
| 5284 | if (!s->v.AnnAssign.value && |
| 5285 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5286 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5287 | return 0; |
| 5288 | } |
| 5289 | break; |
| 5290 | default: |
| 5291 | PyErr_Format(PyExc_SystemError, |
| 5292 | "invalid node type (%d) for annotated assignment", |
| 5293 | targ->kind); |
| 5294 | return 0; |
| 5295 | } |
| 5296 | /* Annotation is evaluated last. */ |
| 5297 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5298 | return 0; |
| 5299 | } |
| 5300 | return 1; |
| 5301 | } |
| 5302 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5303 | /* Raises a SyntaxError and returns 0. |
| 5304 | If something goes wrong, a different exception may be raised. |
| 5305 | */ |
| 5306 | |
| 5307 | static int |
| 5308 | compiler_error(struct compiler *c, const char *errstr) |
| 5309 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5310 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5312 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5313 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5314 | if (!loc) { |
| 5315 | Py_INCREF(Py_None); |
| 5316 | loc = Py_None; |
| 5317 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5318 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5319 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5320 | if (!u) |
| 5321 | goto exit; |
| 5322 | v = Py_BuildValue("(zO)", errstr, u); |
| 5323 | if (!v) |
| 5324 | goto exit; |
| 5325 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5326 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5327 | Py_DECREF(loc); |
| 5328 | Py_XDECREF(u); |
| 5329 | Py_XDECREF(v); |
| 5330 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5333 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5334 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5335 | and returns 0. |
| 5336 | */ |
| 5337 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5338 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5339 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5340 | va_list vargs; |
| 5341 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5342 | va_start(vargs, format); |
| 5343 | #else |
| 5344 | va_start(vargs); |
| 5345 | #endif |
| 5346 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5347 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5348 | if (msg == NULL) { |
| 5349 | return 0; |
| 5350 | } |
| 5351 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5352 | c->u->u_lineno, NULL, NULL) < 0) |
| 5353 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5354 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5355 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5356 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5357 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5358 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5359 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5360 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5361 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5362 | return 0; |
| 5363 | } |
| 5364 | Py_DECREF(msg); |
| 5365 | return 1; |
| 5366 | } |
| 5367 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5368 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5369 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5370 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5371 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5372 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5373 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5374 | if (ctx == Load) { |
| 5375 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5376 | return 0; |
| 5377 | } |
| 5378 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5379 | return 0; |
| 5380 | } |
| 5381 | } |
| 5382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5385 | case Store: op = STORE_SUBSCR; break; |
| 5386 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5388 | assert(op); |
| 5389 | VISIT(c, expr, e->v.Subscript.value); |
| 5390 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5391 | ADDOP(c, op); |
| 5392 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5396 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | int n = 2; |
| 5399 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5401 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5402 | if (s->v.Slice.lower) { |
| 5403 | VISIT(c, expr, s->v.Slice.lower); |
| 5404 | } |
| 5405 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5406 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5407 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | if (s->v.Slice.upper) { |
| 5410 | VISIT(c, expr, s->v.Slice.upper); |
| 5411 | } |
| 5412 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5413 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | if (s->v.Slice.step) { |
| 5417 | n++; |
| 5418 | VISIT(c, expr, s->v.Slice.step); |
| 5419 | } |
| 5420 | ADDOP_I(c, BUILD_SLICE, n); |
| 5421 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5424 | /* End of the compiler section, beginning of the assembler section */ |
| 5425 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5426 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5427 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5428 | |
| 5429 | XXX must handle implicit jumps from one block to next |
| 5430 | */ |
| 5431 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5432 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5433 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5434 | int a_offset; /* offset into bytecode */ |
| 5435 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5436 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5437 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5438 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 5439 | int a_lineno; /* lineno of last emitted instruction */ |
| 5440 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5441 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5442 | }; |
| 5443 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5444 | Py_LOCAL_INLINE(void) |
| 5445 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5446 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5447 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5448 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5449 | assert(b->b_startdepth < 0); |
| 5450 | b->b_startdepth = depth; |
| 5451 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5452 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* Find the flow path that needs the largest stack. We assume that |
| 5456 | * cycles in the flow graph have no net effect on the stack depth. |
| 5457 | */ |
| 5458 | static int |
| 5459 | stackdepth(struct compiler *c) |
| 5460 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5461 | basicblock *b, *entryblock = NULL; |
| 5462 | basicblock **stack, **sp; |
| 5463 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5464 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | b->b_startdepth = INT_MIN; |
| 5466 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5467 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5468 | } |
| 5469 | if (!entryblock) |
| 5470 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5471 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5472 | if (!stack) { |
| 5473 | PyErr_NoMemory(); |
| 5474 | return -1; |
| 5475 | } |
| 5476 | |
| 5477 | sp = stack; |
| 5478 | stackdepth_push(&sp, entryblock, 0); |
| 5479 | while (sp != stack) { |
| 5480 | b = *--sp; |
| 5481 | int depth = b->b_startdepth; |
| 5482 | assert(depth >= 0); |
| 5483 | basicblock *next = b->b_next; |
| 5484 | for (int i = 0; i < b->b_iused; i++) { |
| 5485 | struct instr *instr = &b->b_instr[i]; |
| 5486 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5487 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | ba7a99d | 2021-01-30 01:46:44 +0100 | [diff] [blame] | 5488 | PyErr_Format(PyExc_SystemError, |
| 5489 | "compiler stack_effect(opcode=%d, arg=%i) failed", |
| 5490 | instr->i_opcode, instr->i_oparg); |
| 5491 | return -1; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5492 | } |
| 5493 | int new_depth = depth + effect; |
| 5494 | if (new_depth > maxdepth) { |
| 5495 | maxdepth = new_depth; |
| 5496 | } |
| 5497 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5498 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5499 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5500 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5501 | int target_depth = depth + effect; |
| 5502 | if (target_depth > maxdepth) { |
| 5503 | maxdepth = target_depth; |
| 5504 | } |
| 5505 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5506 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5507 | } |
| 5508 | depth = new_depth; |
| 5509 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5510 | instr->i_opcode == JUMP_FORWARD || |
| 5511 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5512 | instr->i_opcode == RAISE_VARARGS || |
| 5513 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5514 | { |
| 5515 | /* remaining code is dead */ |
| 5516 | next = NULL; |
| 5517 | break; |
| 5518 | } |
| 5519 | } |
| 5520 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 5521 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5522 | stackdepth_push(&sp, next, depth); |
| 5523 | } |
| 5524 | } |
| 5525 | PyObject_Free(stack); |
| 5526 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | static int |
| 5530 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5532 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5533 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5534 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5536 | if (a->a_bytecode == NULL) { |
| 5537 | goto error; |
| 5538 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5540 | if (a->a_lnotab == NULL) { |
| 5541 | goto error; |
| 5542 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5543 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5545 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5546 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5548 | error: |
| 5549 | Py_XDECREF(a->a_bytecode); |
| 5550 | Py_XDECREF(a->a_lnotab); |
| 5551 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | static void |
| 5555 | assemble_free(struct assembler *a) |
| 5556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5557 | Py_XDECREF(a->a_bytecode); |
| 5558 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5561 | static int |
| 5562 | blocksize(basicblock *b) |
| 5563 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | int i; |
| 5565 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5567 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5568 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5572 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5573 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5574 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5575 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | if (a->a_lnotab_off + 2 >= len) { |
| 5577 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5578 | return 0; |
| 5579 | } |
Pablo Galindo | 86e322f | 2021-01-30 13:54:22 +0000 | [diff] [blame] | 5580 | unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(a->a_lnotab); |
| 5581 | lnotab += a->a_lnotab_off; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5582 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5583 | *lnotab++ = bdelta; |
| 5584 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5588 | /* Appends a range to the end of the line number table. See |
| 5589 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 5590 | |
| 5591 | static int |
| 5592 | assemble_line_range(struct assembler *a) |
| 5593 | { |
| 5594 | int ldelta, bdelta; |
| 5595 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 5596 | if (bdelta == 0) { |
| 5597 | return 1; |
| 5598 | } |
| 5599 | if (a->a_lineno < 0) { |
| 5600 | ldelta = -128; |
| 5601 | } |
| 5602 | else { |
| 5603 | ldelta = a->a_lineno - a->a_prevlineno; |
| 5604 | a->a_prevlineno = a->a_lineno; |
| 5605 | while (ldelta > 127) { |
| 5606 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 5607 | return 0; |
| 5608 | } |
| 5609 | ldelta -= 127; |
| 5610 | } |
| 5611 | while (ldelta < -127) { |
| 5612 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 5613 | return 0; |
| 5614 | } |
| 5615 | ldelta += 127; |
| 5616 | } |
| 5617 | } |
| 5618 | assert(-128 <= ldelta && ldelta < 128); |
| 5619 | while (bdelta > 254) { |
| 5620 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 5621 | return 0; |
| 5622 | } |
| 5623 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 5624 | bdelta -= 254; |
| 5625 | } |
| 5626 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 5627 | return 0; |
| 5628 | } |
| 5629 | a->a_lineno_start = a->a_offset; |
| 5630 | return 1; |
| 5631 | } |
| 5632 | |
| 5633 | static int |
| 5634 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 5635 | { |
| 5636 | if (i->i_lineno == a->a_lineno) { |
| 5637 | return 1; |
| 5638 | } |
| 5639 | if (!assemble_line_range(a)) { |
| 5640 | return 0; |
| 5641 | } |
| 5642 | a->a_lineno = i->i_lineno; |
| 5643 | return 1; |
| 5644 | } |
| 5645 | |
| 5646 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5647 | /* assemble_emit() |
| 5648 | Extend the bytecode with a new instruction. |
| 5649 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5650 | */ |
| 5651 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5652 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5653 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5654 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5655 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5656 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5657 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5658 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5659 | arg = i->i_oparg; |
| 5660 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5661 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5662 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5663 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5664 | if (len > PY_SSIZE_T_MAX / 2) |
| 5665 | return 0; |
| 5666 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5667 | return 0; |
| 5668 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5669 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5670 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5671 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5672 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5675 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5676 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5679 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5682 | /* Compute the size of each block and fixup jump args. |
| 5683 | Replace block pointer with position in bytecode. */ |
| 5684 | do { |
| 5685 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5686 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5687 | bsize = blocksize(b); |
| 5688 | b->b_offset = totsize; |
| 5689 | totsize += bsize; |
| 5690 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5691 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5693 | bsize = b->b_offset; |
| 5694 | for (i = 0; i < b->b_iused; i++) { |
| 5695 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5696 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 | /* Relative jumps are computed relative to |
| 5698 | the instruction pointer after fetching |
| 5699 | the jump instruction. |
| 5700 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5701 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5702 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5703 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5704 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5705 | instr->i_oparg -= bsize; |
| 5706 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5707 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5708 | if (instrsize(instr->i_oparg) != isize) { |
| 5709 | extended_arg_recompile = 1; |
| 5710 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5711 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | } |
| 5713 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | /* XXX: This is an awful hack that could hurt performance, but |
| 5716 | on the bright side it should work until we come up |
| 5717 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5719 | The issue is that in the first loop blocksize() is called |
| 5720 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5721 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5722 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5724 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5725 | The only EXTENDED_ARGs that could be popping up are |
| 5726 | ones in jump instructions. So this should converge |
| 5727 | fairly quickly. |
| 5728 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5729 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5732 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5733 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5735 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5736 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5738 | tuple = PyTuple_New(size); |
| 5739 | if (tuple == NULL) |
| 5740 | return NULL; |
| 5741 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5742 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5743 | Py_INCREF(k); |
| 5744 | assert((i - offset) < size); |
| 5745 | assert((i - offset) >= 0); |
| 5746 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5747 | } |
| 5748 | return tuple; |
| 5749 | } |
| 5750 | |
| 5751 | static PyObject * |
| 5752 | consts_dict_keys_inorder(PyObject *dict) |
| 5753 | { |
| 5754 | PyObject *consts, *k, *v; |
| 5755 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5756 | |
| 5757 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5758 | if (consts == NULL) |
| 5759 | return NULL; |
| 5760 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5761 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5762 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5763 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5764 | * the object we want is always second. */ |
| 5765 | if (PyTuple_CheckExact(k)) { |
| 5766 | k = PyTuple_GET_ITEM(k, 1); |
| 5767 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5768 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5769 | assert(i < size); |
| 5770 | assert(i >= 0); |
| 5771 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5772 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5773 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5776 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5777 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5779 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5780 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5781 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5782 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5783 | if (ste->ste_nested) |
| 5784 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5785 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5786 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5787 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5788 | flags |= CO_COROUTINE; |
| 5789 | if (ste->ste_generator && ste->ste_coroutine) |
| 5790 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5791 | if (ste->ste_varargs) |
| 5792 | flags |= CO_VARARGS; |
| 5793 | if (ste->ste_varkeywords) |
| 5794 | flags |= CO_VARKEYWORDS; |
| 5795 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5798 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5799 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5800 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5801 | ste->ste_coroutine && |
| 5802 | !ste->ste_generator) { |
| 5803 | flags |= CO_COROUTINE; |
| 5804 | } |
| 5805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5806 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5809 | // Merge *tuple* with constant cache. |
| 5810 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5811 | static int |
| 5812 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5813 | { |
| 5814 | assert(PyTuple_CheckExact(*tuple)); |
| 5815 | |
| 5816 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5817 | if (key == NULL) { |
| 5818 | return 0; |
| 5819 | } |
| 5820 | |
| 5821 | // t is borrowed reference |
| 5822 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5823 | Py_DECREF(key); |
| 5824 | if (t == NULL) { |
| 5825 | return 0; |
| 5826 | } |
| 5827 | if (t == key) { // tuple is new constant. |
| 5828 | return 1; |
| 5829 | } |
| 5830 | |
| 5831 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5832 | Py_INCREF(u); |
| 5833 | Py_DECREF(*tuple); |
| 5834 | *tuple = u; |
| 5835 | return 1; |
| 5836 | } |
| 5837 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5838 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5839 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5842 | PyObject *names = NULL; |
| 5843 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | PyObject *name = NULL; |
| 5845 | PyObject *freevars = NULL; |
| 5846 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5847 | Py_ssize_t nlocals; |
| 5848 | int nlocals_int; |
| 5849 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5850 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5853 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5854 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5855 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5856 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5857 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5858 | if (!cellvars) |
| 5859 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5860 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5861 | if (!freevars) |
| 5862 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5863 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5864 | if (!merge_const_tuple(c, &names) || |
| 5865 | !merge_const_tuple(c, &varnames) || |
| 5866 | !merge_const_tuple(c, &cellvars) || |
| 5867 | !merge_const_tuple(c, &freevars)) |
| 5868 | { |
| 5869 | goto error; |
| 5870 | } |
| 5871 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5872 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5873 | assert(nlocals < INT_MAX); |
| 5874 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5876 | flags = compute_code_flags(c); |
| 5877 | if (flags < 0) |
| 5878 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5879 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5880 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5881 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5883 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5884 | if (!merge_const_tuple(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5885 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5886 | goto error; |
| 5887 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5888 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5889 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5890 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5891 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5892 | maxdepth = stackdepth(c); |
| 5893 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5894 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5895 | goto error; |
| 5896 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5897 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5898 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5899 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5900 | varnames, freevars, cellvars, c->c_filename, |
| 5901 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5902 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5903 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5904 | Py_XDECREF(names); |
| 5905 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5906 | Py_XDECREF(name); |
| 5907 | Py_XDECREF(freevars); |
| 5908 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5909 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5912 | |
| 5913 | /* For debugging purposes only */ |
| 5914 | #if 0 |
| 5915 | static void |
| 5916 | dump_instr(const struct instr *i) |
| 5917 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5918 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 5919 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5920 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5922 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5923 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5924 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5925 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5926 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5927 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | static void |
| 5931 | dump_basicblock(const basicblock *b) |
| 5932 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5933 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5934 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 5935 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | if (b->b_instr) { |
| 5937 | int i; |
| 5938 | for (i = 0; i < b->b_iused; i++) { |
| 5939 | fprintf(stderr, " [%02d] ", i); |
| 5940 | dump_instr(b->b_instr + i); |
| 5941 | } |
| 5942 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5943 | } |
| 5944 | #endif |
| 5945 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5946 | |
| 5947 | static int |
| 5948 | normalize_basic_block(basicblock *bb); |
| 5949 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5950 | static int |
| 5951 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 5952 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5953 | static int |
| 5954 | ensure_exits_have_lineno(struct compiler *c); |
| 5955 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5956 | static PyCodeObject * |
| 5957 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5958 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5959 | basicblock *b, *entryblock; |
| 5960 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5961 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5962 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5963 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5965 | /* Make sure every block that falls off the end returns None. |
| 5966 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5967 | block ends with a jump or return b_next shouldn't set. |
| 5968 | */ |
| 5969 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5970 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5971 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5972 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5973 | ADDOP(c, RETURN_VALUE); |
| 5974 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5975 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5976 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5977 | if (normalize_basic_block(b)) { |
| 5978 | goto error; |
| 5979 | } |
| 5980 | } |
| 5981 | |
| 5982 | if (ensure_exits_have_lineno(c)) { |
| 5983 | goto error; |
| 5984 | } |
| 5985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | nblocks = 0; |
| 5987 | entryblock = NULL; |
| 5988 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5989 | nblocks++; |
| 5990 | entryblock = b; |
| 5991 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5993 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5994 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5995 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5996 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5997 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5998 | c->u->u_firstlineno = 1; |
| 5999 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6001 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6002 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6003 | a.a_entry = entryblock; |
| 6004 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6005 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6006 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6007 | if (consts == NULL) { |
| 6008 | goto error; |
| 6009 | } |
| 6010 | if (optimize_cfg(&a, consts)) { |
| 6011 | goto error; |
| 6012 | } |
| 6013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6014 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6015 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6016 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6017 | /* Emit code. */ |
| 6018 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6019 | for (j = 0; j < b->b_iused; j++) |
| 6020 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6021 | goto error; |
| 6022 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6023 | if (!assemble_line_range(&a)) { |
| 6024 | return 0; |
| 6025 | } |
| 6026 | /* Emit sentinel at end of line number table */ |
| 6027 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6028 | goto error; |
| 6029 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6031 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6032 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6033 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6034 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6035 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6036 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6037 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6038 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6039 | assemble_free(&a); |
| 6040 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6041 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6042 | |
| 6043 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6044 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6045 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6046 | PyArena *arena) |
| 6047 | { |
| 6048 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6049 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6050 | |
| 6051 | |
| 6052 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6053 | with LOAD_CONST (c1, c2, ... cn). |
| 6054 | The consts table must still be in list form so that the |
| 6055 | new constant (c1, c2, ... cn) can be appended. |
| 6056 | Called with codestr pointing to the first LOAD_CONST. |
| 6057 | */ |
| 6058 | static int |
| 6059 | fold_tuple_on_constants(struct instr *inst, |
| 6060 | int n, PyObject *consts) |
| 6061 | { |
| 6062 | /* Pre-conditions */ |
| 6063 | assert(PyList_CheckExact(consts)); |
| 6064 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6065 | assert(inst[n].i_oparg == n); |
| 6066 | |
| 6067 | for (int i = 0; i < n; i++) { |
| 6068 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6069 | return 0; |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | /* Buildup new tuple of constants */ |
| 6074 | PyObject *newconst = PyTuple_New(n); |
| 6075 | if (newconst == NULL) { |
| 6076 | return -1; |
| 6077 | } |
| 6078 | for (int i = 0; i < n; i++) { |
| 6079 | int arg = inst[i].i_oparg; |
| 6080 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6081 | Py_INCREF(constant); |
| 6082 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6083 | } |
| 6084 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6085 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6086 | Py_DECREF(newconst); |
| 6087 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6088 | return -1; |
| 6089 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6090 | if (PyList_Append(consts, newconst)) { |
| 6091 | Py_DECREF(newconst); |
| 6092 | return -1; |
| 6093 | } |
| 6094 | Py_DECREF(newconst); |
| 6095 | for (int i = 0; i < n; i++) { |
| 6096 | inst[i].i_opcode = NOP; |
| 6097 | } |
| 6098 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6099 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6100 | return 0; |
| 6101 | } |
| 6102 | |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6103 | |
| 6104 | static int |
| 6105 | eliminate_jump_to_jump(basicblock *bb, int opcode) { |
| 6106 | assert (bb->b_iused > 0); |
| 6107 | struct instr *inst = &bb->b_instr[bb->b_iused-1]; |
| 6108 | assert (is_jump(inst)); |
| 6109 | assert (inst->i_target->b_iused > 0); |
| 6110 | struct instr *target = &inst->i_target->b_instr[0]; |
| 6111 | if (inst->i_target == target->i_target) { |
| 6112 | /* Nothing to do */ |
| 6113 | return 0; |
| 6114 | } |
| 6115 | int lineno = target->i_lineno; |
| 6116 | if (add_jump_to_block(bb, opcode, lineno, target->i_target) == 0) { |
| 6117 | return -1; |
| 6118 | } |
| 6119 | assert (bb->b_iused >= 2); |
| 6120 | bb->b_instr[bb->b_iused-2].i_opcode = NOP; |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6124 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6125 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6126 | |
| 6127 | /* Optimization */ |
| 6128 | static int |
| 6129 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6130 | { |
| 6131 | assert(PyList_CheckExact(consts)); |
| 6132 | struct instr nop; |
| 6133 | nop.i_opcode = NOP; |
| 6134 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6135 | for (int i = 0; i < bb->b_iused; i++) { |
| 6136 | struct instr *inst = &bb->b_instr[i]; |
| 6137 | int oparg = inst->i_oparg; |
| 6138 | 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] | 6139 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6140 | /* Skip over empty basic blocks. */ |
| 6141 | while (inst->i_target->b_iused == 0) { |
| 6142 | inst->i_target = inst->i_target->b_next; |
| 6143 | } |
| 6144 | target = &inst->i_target->b_instr[0]; |
| 6145 | } |
| 6146 | else { |
| 6147 | target = &nop; |
| 6148 | } |
| 6149 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6150 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6151 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6152 | { |
| 6153 | PyObject* cnt; |
| 6154 | int is_true; |
| 6155 | int jump_if_true; |
| 6156 | switch(nextop) { |
| 6157 | case POP_JUMP_IF_FALSE: |
| 6158 | case POP_JUMP_IF_TRUE: |
| 6159 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6160 | is_true = PyObject_IsTrue(cnt); |
| 6161 | if (is_true == -1) { |
| 6162 | goto error; |
| 6163 | } |
| 6164 | inst->i_opcode = NOP; |
| 6165 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6166 | if (is_true == jump_if_true) { |
| 6167 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6168 | bb->b_nofallthrough = 1; |
| 6169 | } |
| 6170 | else { |
| 6171 | bb->b_instr[i+1].i_opcode = NOP; |
| 6172 | } |
| 6173 | break; |
| 6174 | case JUMP_IF_FALSE_OR_POP: |
| 6175 | case JUMP_IF_TRUE_OR_POP: |
| 6176 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6177 | is_true = PyObject_IsTrue(cnt); |
| 6178 | if (is_true == -1) { |
| 6179 | goto error; |
| 6180 | } |
| 6181 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6182 | if (is_true == jump_if_true) { |
| 6183 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6184 | bb->b_nofallthrough = 1; |
| 6185 | } |
| 6186 | else { |
| 6187 | inst->i_opcode = NOP; |
| 6188 | bb->b_instr[i+1].i_opcode = NOP; |
| 6189 | } |
| 6190 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6191 | } |
| 6192 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6193 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6194 | |
| 6195 | /* Try to fold tuples of constants. |
| 6196 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6197 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6198 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6199 | case BUILD_TUPLE: |
| 6200 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6201 | switch(oparg) { |
| 6202 | case 1: |
| 6203 | inst->i_opcode = NOP; |
| 6204 | bb->b_instr[i+1].i_opcode = NOP; |
| 6205 | break; |
| 6206 | case 2: |
| 6207 | inst->i_opcode = ROT_TWO; |
| 6208 | bb->b_instr[i+1].i_opcode = NOP; |
| 6209 | break; |
| 6210 | case 3: |
| 6211 | inst->i_opcode = ROT_THREE; |
| 6212 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6213 | } |
| 6214 | break; |
| 6215 | } |
| 6216 | if (i >= oparg) { |
| 6217 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6218 | goto error; |
| 6219 | } |
| 6220 | } |
| 6221 | break; |
| 6222 | |
| 6223 | /* Simplify conditional jump to conditional jump where the |
| 6224 | result of the first test implies the success of a similar |
| 6225 | test or the failure of the opposite test. |
| 6226 | Arises in code like: |
| 6227 | "a and b or c" |
| 6228 | "(a and b) and c" |
| 6229 | "(a or b) or c" |
| 6230 | "(a or b) and c" |
| 6231 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6232 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6233 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6234 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6235 | where y+1 is the instruction following the second test. |
| 6236 | */ |
| 6237 | case JUMP_IF_FALSE_OR_POP: |
| 6238 | switch(target->i_opcode) { |
| 6239 | case POP_JUMP_IF_FALSE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6240 | if (inst->i_lineno == target->i_lineno) { |
| 6241 | *inst = *target; |
| 6242 | i--; |
| 6243 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6244 | break; |
| 6245 | case JUMP_ABSOLUTE: |
| 6246 | case JUMP_FORWARD: |
| 6247 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6248 | if (inst->i_lineno == target->i_lineno && |
| 6249 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6250 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6251 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6252 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6253 | break; |
| 6254 | case JUMP_IF_TRUE_OR_POP: |
| 6255 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6256 | if (inst->i_lineno == target->i_lineno) { |
| 6257 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6258 | inst->i_target = inst->i_target->b_next; |
| 6259 | --i; |
| 6260 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6261 | break; |
| 6262 | } |
| 6263 | break; |
| 6264 | |
| 6265 | case JUMP_IF_TRUE_OR_POP: |
| 6266 | switch(target->i_opcode) { |
| 6267 | case POP_JUMP_IF_TRUE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6268 | if (inst->i_lineno == target->i_lineno) { |
| 6269 | *inst = *target; |
| 6270 | i--; |
| 6271 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6272 | break; |
| 6273 | case JUMP_ABSOLUTE: |
| 6274 | case JUMP_FORWARD: |
| 6275 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6276 | if (inst->i_lineno == target->i_lineno && |
| 6277 | inst->i_target != target->i_target) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6278 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6279 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6280 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6281 | break; |
| 6282 | case JUMP_IF_FALSE_OR_POP: |
| 6283 | assert (inst->i_target->b_iused == 1); |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6284 | if (inst->i_lineno == target->i_lineno) { |
| 6285 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 6286 | inst->i_target = inst->i_target->b_next; |
| 6287 | --i; |
| 6288 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6289 | break; |
| 6290 | } |
| 6291 | break; |
| 6292 | |
| 6293 | case POP_JUMP_IF_FALSE: |
| 6294 | switch(target->i_opcode) { |
| 6295 | case JUMP_ABSOLUTE: |
| 6296 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6297 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6298 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6299 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6300 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6301 | break; |
| 6302 | } |
| 6303 | break; |
| 6304 | |
| 6305 | case POP_JUMP_IF_TRUE: |
| 6306 | switch(target->i_opcode) { |
| 6307 | case JUMP_ABSOLUTE: |
| 6308 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6309 | if (inst->i_lineno == target->i_lineno) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6310 | inst->i_target = target->i_target; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6311 | i--; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6312 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6313 | break; |
| 6314 | } |
| 6315 | break; |
| 6316 | |
| 6317 | case JUMP_ABSOLUTE: |
| 6318 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6319 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6320 | switch(target->i_opcode) { |
| 6321 | case JUMP_FORWARD: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6322 | if (eliminate_jump_to_jump(bb, inst->i_opcode)) { |
| 6323 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6324 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6325 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6326 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6327 | case JUMP_ABSOLUTE: |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6328 | if (eliminate_jump_to_jump(bb, JUMP_ABSOLUTE)) { |
| 6329 | goto error; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6330 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6331 | break; |
Mark Shannon | 28b75c8 | 2020-12-23 11:43:10 +0000 | [diff] [blame] | 6332 | default: |
| 6333 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 6334 | basicblock *to_copy = inst->i_target; |
| 6335 | inst->i_opcode = NOP; |
| 6336 | for (i = 0; i < to_copy->b_iused; i++) { |
| 6337 | int index = compiler_next_instr(bb); |
| 6338 | if (index < 0) { |
| 6339 | return -1; |
| 6340 | } |
| 6341 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 6342 | } |
| 6343 | bb->b_exit = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6344 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6345 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6346 | } |
| 6347 | } |
| 6348 | return 0; |
| 6349 | error: |
| 6350 | return -1; |
| 6351 | } |
| 6352 | |
| 6353 | |
| 6354 | static void |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 6355 | clean_basic_block(basicblock *bb, int prev_lineno) { |
| 6356 | /* Remove NOPs when legal to do so. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6357 | int dest = 0; |
| 6358 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6359 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6360 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6361 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6362 | if (lineno < 0) { |
| 6363 | continue; |
| 6364 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6365 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6366 | if (prev_lineno == lineno) { |
| 6367 | continue; |
| 6368 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6369 | /* 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] | 6370 | if (src < bb->b_iused - 1) { |
| 6371 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 6372 | if (next_lineno < 0 || next_lineno == lineno) { |
| 6373 | bb->b_instr[src+1].i_lineno = lineno; |
| 6374 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6375 | } |
| 6376 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6377 | else { |
| 6378 | basicblock* next = bb->b_next; |
| 6379 | while (next && next->b_iused == 0) { |
| 6380 | next = next->b_next; |
| 6381 | } |
| 6382 | /* or if last instruction in BB and next BB has same line number */ |
| 6383 | if (next) { |
| 6384 | if (lineno == next->b_instr[0].i_lineno) { |
| 6385 | continue; |
| 6386 | } |
| 6387 | } |
| 6388 | } |
| 6389 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6390 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6391 | if (dest != src) { |
| 6392 | bb->b_instr[dest] = bb->b_instr[src]; |
| 6393 | } |
| 6394 | dest++; |
| 6395 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6396 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6397 | assert(dest <= bb->b_iused); |
| 6398 | bb->b_iused = dest; |
| 6399 | } |
| 6400 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6401 | static int |
| 6402 | normalize_basic_block(basicblock *bb) { |
| 6403 | /* Mark blocks as exit and/or nofallthrough. |
| 6404 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6405 | for (int i = 0; i < bb->b_iused; i++) { |
| 6406 | switch(bb->b_instr[i].i_opcode) { |
| 6407 | case RETURN_VALUE: |
| 6408 | case RAISE_VARARGS: |
| 6409 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6410 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6411 | bb->b_nofallthrough = 1; |
| 6412 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6413 | case JUMP_ABSOLUTE: |
| 6414 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6415 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6416 | /* fall through */ |
| 6417 | case POP_JUMP_IF_FALSE: |
| 6418 | case POP_JUMP_IF_TRUE: |
| 6419 | case JUMP_IF_FALSE_OR_POP: |
| 6420 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6421 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6422 | if (i != bb->b_iused-1) { |
| 6423 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 6424 | return -1; |
| 6425 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6426 | /* Skip over empty basic blocks. */ |
| 6427 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 6428 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 6429 | } |
| 6430 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6431 | } |
| 6432 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6433 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6436 | static int |
| 6437 | mark_reachable(struct assembler *a) { |
| 6438 | basicblock **stack, **sp; |
| 6439 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 6440 | if (stack == NULL) { |
| 6441 | return -1; |
| 6442 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6443 | a->a_entry->b_predecessors = 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6444 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6445 | while (sp > stack) { |
| 6446 | basicblock *b = *(--sp); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6447 | if (b->b_next && !b->b_nofallthrough) { |
| 6448 | if (b->b_next->b_predecessors == 0) { |
| 6449 | *sp++ = b->b_next; |
| 6450 | } |
| 6451 | b->b_next->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6452 | } |
| 6453 | for (int i = 0; i < b->b_iused; i++) { |
| 6454 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6455 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6456 | target = b->b_instr[i].i_target; |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6457 | if (target->b_predecessors == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6458 | *sp++ = target; |
| 6459 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6460 | target->b_predecessors++; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6461 | } |
| 6462 | } |
| 6463 | } |
| 6464 | PyObject_Free(stack); |
| 6465 | return 0; |
| 6466 | } |
| 6467 | |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6468 | static void |
| 6469 | eliminate_empty_basic_blocks(basicblock *entry) { |
| 6470 | /* Eliminate empty blocks */ |
| 6471 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 6472 | basicblock *next = b->b_next; |
| 6473 | if (next) { |
| 6474 | while (next->b_iused == 0 && next->b_next) { |
| 6475 | next = next->b_next; |
| 6476 | } |
| 6477 | b->b_next = next; |
| 6478 | } |
| 6479 | } |
| 6480 | for (basicblock *b = entry; b != NULL; b = b->b_next) { |
| 6481 | if (b->b_iused == 0) { |
| 6482 | continue; |
| 6483 | } |
| 6484 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 6485 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 6486 | while (target->b_iused == 0) { |
| 6487 | target = target->b_next; |
| 6488 | } |
| 6489 | b->b_instr[b->b_iused-1].i_target = target; |
| 6490 | } |
| 6491 | } |
| 6492 | } |
| 6493 | |
| 6494 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6495 | /* 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] | 6496 | * then copy the line number. If a successor block has no line number, and only |
| 6497 | * one predecessor, then inherit the line number. |
| 6498 | * This ensures that all exit blocks (with one predecessor) receive a line number. |
| 6499 | * Also reduces the size of the line number table, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6500 | * but has no impact on the generated line number events. |
| 6501 | */ |
| 6502 | static void |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6503 | propogate_line_numbers(struct assembler *a) { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6504 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6505 | if (b->b_iused == 0) { |
| 6506 | continue; |
| 6507 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6508 | int prev_lineno = -1; |
| 6509 | for (int i = 0; i < b->b_iused; i++) { |
| 6510 | if (b->b_instr[i].i_lineno < 0) { |
| 6511 | b->b_instr[i].i_lineno = prev_lineno; |
| 6512 | } |
| 6513 | else { |
| 6514 | prev_lineno = b->b_instr[i].i_lineno; |
| 6515 | } |
| 6516 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6517 | if (!b->b_nofallthrough && b->b_next->b_predecessors == 1) { |
| 6518 | assert(b->b_next->b_iused); |
| 6519 | if (b->b_next->b_instr[0].i_lineno < 0) { |
| 6520 | b->b_next->b_instr[0].i_lineno = prev_lineno; |
| 6521 | } |
| 6522 | } |
| 6523 | if (is_jump(&b->b_instr[b->b_iused-1])) { |
| 6524 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 6525 | /* Note: Only actual jumps, not exception handlers */ |
| 6526 | case SETUP_ASYNC_WITH: |
| 6527 | case SETUP_WITH: |
| 6528 | case SETUP_FINALLY: |
| 6529 | continue; |
| 6530 | } |
| 6531 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 6532 | if (target->b_predecessors == 1) { |
| 6533 | if (target->b_instr[0].i_lineno < 0) { |
| 6534 | target->b_instr[0].i_lineno = prev_lineno; |
| 6535 | } |
| 6536 | } |
| 6537 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6538 | } |
| 6539 | } |
| 6540 | |
| 6541 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6542 | The consts object should still be in list form to allow new constants |
| 6543 | to be appended. |
| 6544 | |
| 6545 | All transformations keep the code size the same or smaller. |
| 6546 | For those that reduce size, the gaps are initially filled with |
| 6547 | NOPs. Later those NOPs are removed. |
| 6548 | */ |
| 6549 | |
| 6550 | static int |
| 6551 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 6552 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6553 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6554 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6555 | return -1; |
| 6556 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 6557 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6558 | assert(b->b_predecessors == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6559 | } |
| 6560 | if (mark_reachable(a)) { |
| 6561 | return -1; |
| 6562 | } |
| 6563 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6564 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6565 | if (b->b_predecessors == 0) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6566 | b->b_iused = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 6567 | b->b_nofallthrough = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6568 | } |
| 6569 | } |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 6570 | basicblock *pred = NULL; |
| 6571 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6572 | int prev_lineno = -1; |
| 6573 | if (pred && pred->b_iused) { |
| 6574 | prev_lineno = pred->b_instr[pred->b_iused-1].i_lineno; |
| 6575 | } |
| 6576 | clean_basic_block(b, prev_lineno); |
| 6577 | pred = b->b_nofallthrough ? NULL : b; |
| 6578 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6579 | eliminate_empty_basic_blocks(a->a_entry); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 6580 | /* Delete jump instructions made redundant by previous step. If a non-empty |
| 6581 | block ends with a jump instruction, check if the next non-empty block |
| 6582 | reached through normal flow control is the target of that jump. If it |
| 6583 | is, then the jump instruction is redundant and can be deleted. |
| 6584 | */ |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6585 | int maybe_empty_blocks = 0; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 6586 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6587 | if (b->b_iused > 0) { |
| 6588 | struct instr *b_last_instr = &b->b_instr[b->b_iused - 1]; |
| 6589 | if (b_last_instr->i_opcode == POP_JUMP_IF_FALSE || |
| 6590 | b_last_instr->i_opcode == POP_JUMP_IF_TRUE || |
| 6591 | b_last_instr->i_opcode == JUMP_ABSOLUTE || |
| 6592 | b_last_instr->i_opcode == JUMP_FORWARD) { |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6593 | if (b_last_instr->i_target == b->b_next) { |
| 6594 | assert(b->b_next->b_iused); |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 6595 | b->b_nofallthrough = 0; |
| 6596 | switch(b_last_instr->i_opcode) { |
| 6597 | case POP_JUMP_IF_FALSE: |
| 6598 | case POP_JUMP_IF_TRUE: |
| 6599 | b_last_instr->i_opcode = POP_TOP; |
| 6600 | b_last_instr->i_target = NULL; |
| 6601 | b_last_instr->i_oparg = 0; |
| 6602 | break; |
| 6603 | case JUMP_ABSOLUTE: |
| 6604 | case JUMP_FORWARD: |
| 6605 | b_last_instr->i_opcode = NOP; |
Mark Shannon | 1659ad1 | 2021-01-13 15:05:04 +0000 | [diff] [blame] | 6606 | clean_basic_block(b, -1); |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6607 | maybe_empty_blocks = 1; |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 6608 | break; |
| 6609 | } |
Om G | c71581c | 2020-12-16 17:48:05 +0530 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | } |
| 6613 | } |
Mark Shannon | 3bd6035 | 2021-01-13 12:05:43 +0000 | [diff] [blame] | 6614 | if (maybe_empty_blocks) { |
| 6615 | eliminate_empty_basic_blocks(a->a_entry); |
| 6616 | } |
| 6617 | propogate_line_numbers(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6618 | return 0; |
| 6619 | } |
| 6620 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6621 | static inline int |
| 6622 | is_exit_without_lineno(basicblock *b) { |
| 6623 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 6624 | } |
| 6625 | |
| 6626 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 6627 | * after a frame terminates. It would be prohibitively expensive |
| 6628 | * to continuously update the f_lineno field at runtime, |
| 6629 | * so we make sure that all exiting instruction (raises and returns) |
| 6630 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 6631 | * We can do this by duplicating the exit blocks without line number |
| 6632 | * so that none have more than one predecessor. We can then safely |
| 6633 | * copy the line number from the sole predecessor block. |
| 6634 | */ |
| 6635 | static int |
| 6636 | ensure_exits_have_lineno(struct compiler *c) |
| 6637 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 6638 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6639 | /* Copy all exit blocks without line number that are targets of a jump. |
| 6640 | */ |
| 6641 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6642 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 6643 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 6644 | /* Note: Only actual jumps, not exception handlers */ |
| 6645 | case SETUP_ASYNC_WITH: |
| 6646 | case SETUP_WITH: |
| 6647 | case SETUP_FINALLY: |
| 6648 | continue; |
| 6649 | } |
| 6650 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 6651 | if (is_exit_without_lineno(target)) { |
| 6652 | basicblock *new_target = compiler_copy_block(c, target); |
| 6653 | if (new_target == NULL) { |
| 6654 | return -1; |
| 6655 | } |
| 6656 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 6657 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 6658 | } |
| 6659 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 6660 | entry = b; |
| 6661 | } |
| 6662 | assert(entry != NULL); |
| 6663 | if (is_exit_without_lineno(entry)) { |
| 6664 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6665 | } |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 6666 | /* Eliminate empty blocks */ |
| 6667 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6668 | while (b->b_next && b->b_next->b_iused == 0) { |
| 6669 | b->b_next = b->b_next->b_next; |
| 6670 | } |
| 6671 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6672 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 6673 | * fall through, and thus can only have a single predecessor */ |
| 6674 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6675 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 6676 | if (is_exit_without_lineno(b->b_next)) { |
| 6677 | assert(b->b_next->b_iused > 0); |
| 6678 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 6679 | } |
| 6680 | } |
| 6681 | } |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
| 6685 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6686 | /* Retained for API compatibility. |
| 6687 | * Optimization is now done in optimize_cfg */ |
| 6688 | |
| 6689 | PyObject * |
| 6690 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 6691 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 6692 | { |
| 6693 | Py_INCREF(code); |
| 6694 | return code; |
| 6695 | } |