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 | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 25 | #include "pycore_long.h" // _PyLong_GetZero() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 26 | |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 27 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 28 | #include "ast.h" |
| 29 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 30 | #include "symtable.h" |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 31 | #define NEED_OPCODE_JUMP_TABLES |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 32 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 33 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 34 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 35 | #define DEFAULT_BLOCK_SIZE 16 |
| 36 | #define DEFAULT_BLOCKS 8 |
| 37 | #define DEFAULT_CODE_SIZE 128 |
| 38 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 39 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 40 | #define COMP_GENEXP 0 |
| 41 | #define COMP_LISTCOMP 1 |
| 42 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 43 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 44 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 45 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 46 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 47 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 48 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 49 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | unsigned char i_opcode; |
| 51 | int i_oparg; |
| 52 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 53 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 56 | #define LOG_BITS_PER_INT 5 |
| 57 | #define MASK_LOW_LOG_BITS 31 |
| 58 | |
| 59 | static inline int |
| 60 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 61 | /* Is the relevant bit set in the relevant word? */ |
| 62 | /* 256 bits fit into 8 32-bits words. |
| 63 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 64 | * Bit within word is the low bits of bitindex. |
| 65 | */ |
| 66 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 67 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 68 | } |
| 69 | |
| 70 | static inline int |
| 71 | is_relative_jump(struct instr *i) |
| 72 | { |
| 73 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 74 | } |
| 75 | |
| 76 | static inline int |
| 77 | is_jump(struct instr *i) |
| 78 | { |
| 79 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 80 | } |
| 81 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 82 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 83 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 84 | reverse order that the block are allocated. b_list points to the next |
| 85 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | struct basicblock_ *b_list; |
| 87 | /* number of instructions used */ |
| 88 | int b_iused; |
| 89 | /* length of instruction array (b_instr) */ |
| 90 | int b_ialloc; |
| 91 | /* pointer to an array of instructions, initially NULL */ |
| 92 | struct instr *b_instr; |
| 93 | /* If b_next is non-NULL, it is a pointer to the next |
| 94 | block reached by normal control flow. */ |
| 95 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 97 | unsigned b_return : 1; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 98 | unsigned b_reachable : 1; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 99 | /* Basic block has no fall through (it ends with a return, raise or jump) */ |
| 100 | unsigned b_nofallthrough : 1; |
| 101 | /* Basic block exits scope (it ends with a return or raise) */ |
| 102 | unsigned b_exit : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 104 | int b_startdepth; |
| 105 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 106 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 107 | } basicblock; |
| 108 | |
| 109 | /* fblockinfo tracks the current frame block. |
| 110 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 111 | A frame block is used to handle loops, try/except, and try/finally. |
| 112 | It's called a frame block to distinguish it from a basic block in the |
| 113 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 114 | */ |
| 115 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 116 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
| 117 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 118 | |
| 119 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | enum fblocktype fb_type; |
| 121 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 122 | /* (optional) type-specific exit or cleanup block */ |
| 123 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 124 | /* (optional) additional information required for unwinding */ |
| 125 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 128 | enum { |
| 129 | COMPILER_SCOPE_MODULE, |
| 130 | COMPILER_SCOPE_CLASS, |
| 131 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 132 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 133 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 134 | COMPILER_SCOPE_COMPREHENSION, |
| 135 | }; |
| 136 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 137 | /* The following items change on entry and exit of code blocks. |
| 138 | They must be saved and restored when returning to a block. |
| 139 | */ |
| 140 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 144 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 145 | int u_scope_type; |
| 146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | /* The following fields are dicts that map objects to |
| 148 | the index of them in co_XXX. The index is used as |
| 149 | the argument for opcodes that refer to those collections. |
| 150 | */ |
| 151 | PyObject *u_consts; /* all constants */ |
| 152 | PyObject *u_names; /* all names */ |
| 153 | PyObject *u_varnames; /* local variables */ |
| 154 | PyObject *u_cellvars; /* cell variables */ |
| 155 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 158 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 159 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 160 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 161 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | /* Pointer to the most recently allocated block. By following b_list |
| 163 | members, you can reach all early allocated blocks. */ |
| 164 | basicblock *u_blocks; |
| 165 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | int u_nfblocks; |
| 168 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | int u_firstlineno; /* the first lineno of the block */ |
| 171 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 172 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 176 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 177 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | 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] | 179 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 180 | |
| 181 | Note that we don't track recursion levels during compilation - the |
| 182 | task of detecting and rejecting excessive levels of nesting is |
| 183 | handled by the symbol analysis pass. |
| 184 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | */ |
| 186 | |
| 187 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 188 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | struct symtable *c_st; |
| 190 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 191 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 192 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 193 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | int c_interactive; /* true if in interactive mode */ |
| 195 | int c_nestlevel; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 196 | int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode |
| 197 | if this value is different from zero. |
| 198 | This can be used to temporarily visit |
| 199 | nodes without emitting bytecode to |
| 200 | check only errors. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 201 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 202 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 203 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | struct compiler_unit *u; /* compiler state for current block */ |
| 205 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 206 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 209 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 210 | static void compiler_free(struct compiler *); |
| 211 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 212 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 213 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 214 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 215 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 216 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 217 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 218 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 219 | |
| 220 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 221 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 222 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 223 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 224 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 225 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 226 | static int compiler_subscript(struct compiler *, expr_ty); |
| 227 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 228 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 229 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 230 | 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^] | 231 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 232 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 233 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 234 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 235 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 236 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 237 | asdl_expr_seq *args, |
| 238 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 239 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 240 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 241 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 242 | static int compiler_sync_comprehension_generator( |
| 243 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 244 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 245 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 246 | expr_ty elt, expr_ty val, int type); |
| 247 | |
| 248 | static int compiler_async_comprehension_generator( |
| 249 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 250 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 251 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 252 | expr_ty elt, expr_ty val, int type); |
| 253 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 254 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 255 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 256 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 257 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 258 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 259 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | /* Name mangling: __private becomes _classname__private. |
| 263 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 264 | PyObject *result; |
| 265 | size_t nlen, plen, ipriv; |
| 266 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 268 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 269 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | Py_INCREF(ident); |
| 271 | return ident; |
| 272 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 273 | nlen = PyUnicode_GET_LENGTH(ident); |
| 274 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | The only time a name with a dot can occur is when |
| 278 | we are compiling an import statement that has a |
| 279 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | TODO(jhylton): Decide whether we want to support |
| 282 | mangling of the module name, e.g. __M.X. |
| 283 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 284 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 285 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 286 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | Py_INCREF(ident); |
| 288 | return ident; /* Don't mangle __whatever__ */ |
| 289 | } |
| 290 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 291 | ipriv = 0; |
| 292 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 293 | ipriv++; |
| 294 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | Py_INCREF(ident); |
| 296 | return ident; /* Don't mangle if class is just underscores */ |
| 297 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 298 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 300 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 301 | PyErr_SetString(PyExc_OverflowError, |
| 302 | "private identifier too large to be mangled"); |
| 303 | return NULL; |
| 304 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 305 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 306 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 307 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 308 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 309 | |
| 310 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 311 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 313 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 314 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 315 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 316 | Py_DECREF(result); |
| 317 | return NULL; |
| 318 | } |
| 319 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 320 | Py_DECREF(result); |
| 321 | return NULL; |
| 322 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 323 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 324 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 327 | static int |
| 328 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 331 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 332 | c->c_const_cache = PyDict_New(); |
| 333 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | c->c_stack = PyList_New(0); |
| 338 | if (!c->c_stack) { |
| 339 | Py_CLEAR(c->c_const_cache); |
| 340 | return 0; |
| 341 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 347 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 348 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | struct compiler c; |
| 351 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 352 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | if (!__doc__) { |
| 356 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 357 | if (!__doc__) |
| 358 | return NULL; |
| 359 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 360 | if (!__annotations__) { |
| 361 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 362 | if (!__annotations__) |
| 363 | return NULL; |
| 364 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | if (!compiler_init(&c)) |
| 366 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 367 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | c.c_filename = filename; |
| 369 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 370 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | if (c.c_future == NULL) |
| 372 | goto finally; |
| 373 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | flags = &local_flags; |
| 375 | } |
| 376 | merged = c.c_future->ff_features | flags->cf_flags; |
| 377 | c.c_future->ff_features = merged; |
| 378 | flags->cf_flags = merged; |
| 379 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 380 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | c.c_nestlevel = 0; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 382 | c.c_do_not_emit_bytecode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 383 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 384 | _PyASTOptimizeState state; |
| 385 | state.optimize = c.c_optimize; |
| 386 | state.ff_features = merged; |
| 387 | |
| 388 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 389 | goto finally; |
| 390 | } |
| 391 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 392 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | if (c.c_st == NULL) { |
| 394 | if (!PyErr_Occurred()) |
| 395 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 396 | goto finally; |
| 397 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 400 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 401 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | compiler_free(&c); |
| 403 | assert(co || PyErr_Occurred()); |
| 404 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 408 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 409 | int optimize, PyArena *arena) |
| 410 | { |
| 411 | PyObject *filename; |
| 412 | PyCodeObject *co; |
| 413 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 414 | if (filename == NULL) |
| 415 | return NULL; |
| 416 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 417 | Py_DECREF(filename); |
| 418 | return co; |
| 419 | |
| 420 | } |
| 421 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 422 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 423 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | if (c->c_st) |
| 426 | PySymtable_Free(c->c_st); |
| 427 | if (c->c_future) |
| 428 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 429 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 430 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 434 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 435 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | Py_ssize_t i, n; |
| 438 | PyObject *v, *k; |
| 439 | PyObject *dict = PyDict_New(); |
| 440 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | n = PyList_Size(list); |
| 443 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 444 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | if (!v) { |
| 446 | Py_DECREF(dict); |
| 447 | return NULL; |
| 448 | } |
| 449 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 450 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | Py_DECREF(v); |
| 452 | Py_DECREF(dict); |
| 453 | return NULL; |
| 454 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | Py_DECREF(v); |
| 456 | } |
| 457 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* Return new dict containing names from src that match scope(s). |
| 461 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 462 | 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] | 463 | 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] | 464 | values are integers, starting at offset and increasing by one for |
| 465 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 466 | */ |
| 467 | |
| 468 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 469 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 470 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 471 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 473 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | assert(offset >= 0); |
| 476 | if (dest == NULL) |
| 477 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 478 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 479 | /* Sort the keys so that we have a deterministic order on the indexes |
| 480 | saved in the returned dictionary. These indexes are used as indexes |
| 481 | into the free and cell var storage. Therefore if they aren't |
| 482 | deterministic, then the generated bytecode is not deterministic. |
| 483 | */ |
| 484 | sorted_keys = PyDict_Keys(src); |
| 485 | if (sorted_keys == NULL) |
| 486 | return NULL; |
| 487 | if (PyList_Sort(sorted_keys) != 0) { |
| 488 | Py_DECREF(sorted_keys); |
| 489 | return NULL; |
| 490 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 491 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 492 | |
| 493 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | /* XXX this should probably be a macro in symtable.h */ |
| 495 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 496 | k = PyList_GET_ITEM(sorted_keys, key_i); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 497 | v = PyDict_GetItemWithError(src, k); |
| 498 | assert(v && PyLong_Check(v)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | vi = PyLong_AS_LONG(v); |
| 500 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 503 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 505 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | Py_DECREF(dest); |
| 507 | return NULL; |
| 508 | } |
| 509 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 510 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 511 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | Py_DECREF(item); |
| 513 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | return NULL; |
| 515 | } |
| 516 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 519 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 523 | static void |
| 524 | compiler_unit_check(struct compiler_unit *u) |
| 525 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | basicblock *block; |
| 527 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 528 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 529 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 530 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | if (block->b_instr != NULL) { |
| 532 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 533 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | assert(block->b_ialloc >= block->b_iused); |
| 535 | } |
| 536 | else { |
| 537 | assert (block->b_iused == 0); |
| 538 | assert (block->b_ialloc == 0); |
| 539 | } |
| 540 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void |
| 544 | compiler_unit_free(struct compiler_unit *u) |
| 545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | compiler_unit_check(u); |
| 549 | b = u->u_blocks; |
| 550 | while (b != NULL) { |
| 551 | if (b->b_instr) |
| 552 | PyObject_Free((void *)b->b_instr); |
| 553 | next = b->b_list; |
| 554 | PyObject_Free((void *)b); |
| 555 | b = next; |
| 556 | } |
| 557 | Py_CLEAR(u->u_ste); |
| 558 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 559 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | Py_CLEAR(u->u_consts); |
| 561 | Py_CLEAR(u->u_names); |
| 562 | Py_CLEAR(u->u_varnames); |
| 563 | Py_CLEAR(u->u_freevars); |
| 564 | Py_CLEAR(u->u_cellvars); |
| 565 | Py_CLEAR(u->u_private); |
| 566 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 570 | compiler_enter_scope(struct compiler *c, identifier name, |
| 571 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 574 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 575 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 576 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | struct compiler_unit)); |
| 578 | if (!u) { |
| 579 | PyErr_NoMemory(); |
| 580 | return 0; |
| 581 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 582 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 584 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | u->u_kwonlyargcount = 0; |
| 586 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 587 | if (!u->u_ste) { |
| 588 | compiler_unit_free(u); |
| 589 | return 0; |
| 590 | } |
| 591 | Py_INCREF(name); |
| 592 | u->u_name = name; |
| 593 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 594 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 595 | if (!u->u_varnames || !u->u_cellvars) { |
| 596 | compiler_unit_free(u); |
| 597 | return 0; |
| 598 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 599 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 600 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 601 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 602 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 603 | int res; |
| 604 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 605 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 606 | name = _PyUnicode_FromId(&PyId___class__); |
| 607 | if (!name) { |
| 608 | compiler_unit_free(u); |
| 609 | return 0; |
| 610 | } |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 611 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_GetZero()); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 612 | if (res < 0) { |
| 613 | compiler_unit_free(u); |
| 614 | return 0; |
| 615 | } |
| 616 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | 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] | 619 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | if (!u->u_freevars) { |
| 621 | compiler_unit_free(u); |
| 622 | return 0; |
| 623 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | u->u_blocks = NULL; |
| 626 | u->u_nfblocks = 0; |
| 627 | u->u_firstlineno = lineno; |
| 628 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 629 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | u->u_consts = PyDict_New(); |
| 631 | if (!u->u_consts) { |
| 632 | compiler_unit_free(u); |
| 633 | return 0; |
| 634 | } |
| 635 | u->u_names = PyDict_New(); |
| 636 | if (!u->u_names) { |
| 637 | compiler_unit_free(u); |
| 638 | return 0; |
| 639 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | /* Push the old compiler_unit on the stack. */ |
| 644 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 645 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 647 | Py_XDECREF(capsule); |
| 648 | compiler_unit_free(u); |
| 649 | return 0; |
| 650 | } |
| 651 | Py_DECREF(capsule); |
| 652 | u->u_private = c->u->u_private; |
| 653 | Py_XINCREF(u->u_private); |
| 654 | } |
| 655 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 658 | |
| 659 | block = compiler_new_block(c); |
| 660 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 662 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 663 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 664 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 665 | if (!compiler_set_qualname(c)) |
| 666 | return 0; |
| 667 | } |
| 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 672 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 673 | compiler_exit_scope(struct compiler *c) |
| 674 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 675 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | c->c_nestlevel--; |
| 679 | compiler_unit_free(c->u); |
| 680 | /* Restore c->u to the parent unit. */ |
| 681 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 682 | if (n >= 0) { |
| 683 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 684 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | assert(c->u); |
| 686 | /* we are deleting from a list so this really shouldn't fail */ |
| 687 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 688 | Py_FatalError("compiler_exit_scope()"); |
| 689 | compiler_unit_check(c->u); |
| 690 | } |
| 691 | else |
| 692 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 693 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 696 | static int |
| 697 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 698 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 699 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 700 | _Py_static_string(dot_locals, ".<locals>"); |
| 701 | Py_ssize_t stack_size; |
| 702 | struct compiler_unit *u = c->u; |
| 703 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 704 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 705 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 706 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 707 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 708 | if (stack_size > 1) { |
| 709 | int scope, force_global = 0; |
| 710 | struct compiler_unit *parent; |
| 711 | PyObject *mangled, *capsule; |
| 712 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 713 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 714 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 715 | assert(parent); |
| 716 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 717 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 718 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 719 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 720 | assert(u->u_name); |
| 721 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 722 | if (!mangled) |
| 723 | return 0; |
| 724 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 725 | Py_DECREF(mangled); |
| 726 | assert(scope != GLOBAL_IMPLICIT); |
| 727 | if (scope == GLOBAL_EXPLICIT) |
| 728 | force_global = 1; |
| 729 | } |
| 730 | |
| 731 | if (!force_global) { |
| 732 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 733 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 734 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 735 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 736 | if (dot_locals_str == NULL) |
| 737 | return 0; |
| 738 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 739 | if (base == NULL) |
| 740 | return 0; |
| 741 | } |
| 742 | else { |
| 743 | Py_INCREF(parent->u_qualname); |
| 744 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 745 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 746 | } |
| 747 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 748 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 749 | if (base != NULL) { |
| 750 | dot_str = _PyUnicode_FromId(&dot); |
| 751 | if (dot_str == NULL) { |
| 752 | Py_DECREF(base); |
| 753 | return 0; |
| 754 | } |
| 755 | name = PyUnicode_Concat(base, dot_str); |
| 756 | Py_DECREF(base); |
| 757 | if (name == NULL) |
| 758 | return 0; |
| 759 | PyUnicode_Append(&name, u->u_name); |
| 760 | if (name == NULL) |
| 761 | return 0; |
| 762 | } |
| 763 | else { |
| 764 | Py_INCREF(u->u_name); |
| 765 | name = u->u_name; |
| 766 | } |
| 767 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 768 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 769 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 772 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | /* Allocate a new block and return a pointer to it. |
| 774 | Returns NULL on error. |
| 775 | */ |
| 776 | |
| 777 | static basicblock * |
| 778 | compiler_new_block(struct compiler *c) |
| 779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | basicblock *b; |
| 781 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 784 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | if (b == NULL) { |
| 786 | PyErr_NoMemory(); |
| 787 | return NULL; |
| 788 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | /* Extend the singly linked list of blocks with new block. */ |
| 790 | b->b_list = u->u_blocks; |
| 791 | u->u_blocks = b; |
| 792 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 796 | compiler_next_block(struct compiler *c) |
| 797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | basicblock *block = compiler_new_block(c); |
| 799 | if (block == NULL) |
| 800 | return NULL; |
| 801 | c->u->u_curblock->b_next = block; |
| 802 | c->u->u_curblock = block; |
| 803 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static basicblock * |
| 807 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | assert(block != NULL); |
| 810 | c->u->u_curblock->b_next = block; |
| 811 | c->u->u_curblock = block; |
| 812 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 815 | static basicblock * |
| 816 | compiler_copy_block(struct compiler *c, basicblock *block) |
| 817 | { |
| 818 | /* Cannot copy a block if it has a fallthrough, since |
| 819 | * a block can only have one fallthrough predecessor. |
| 820 | */ |
| 821 | assert(block->b_nofallthrough); |
| 822 | basicblock *result = compiler_next_block(c); |
| 823 | if (result == NULL) { |
| 824 | return NULL; |
| 825 | } |
| 826 | for (int i = 0; i < block->b_iused; i++) { |
| 827 | int n = compiler_next_instr(result); |
| 828 | if (n < 0) { |
| 829 | return NULL; |
| 830 | } |
| 831 | result->b_instr[n] = block->b_instr[i]; |
| 832 | } |
| 833 | result->b_exit = block->b_exit; |
| 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 | */ |
| 905 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 906 | WITH_CLEANUP_FINISH deterministic. */ |
| 907 | static int |
| 908 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 911 | case NOP: |
| 912 | case EXTENDED_ARG: |
| 913 | return 0; |
| 914 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 915 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | case POP_TOP: |
| 917 | return -1; |
| 918 | case ROT_TWO: |
| 919 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 920 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | return 0; |
| 922 | case DUP_TOP: |
| 923 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 924 | case DUP_TOP_TWO: |
| 925 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 926 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 927 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | case UNARY_POSITIVE: |
| 929 | case UNARY_NEGATIVE: |
| 930 | case UNARY_NOT: |
| 931 | case UNARY_INVERT: |
| 932 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | case SET_ADD: |
| 935 | case LIST_APPEND: |
| 936 | return -1; |
| 937 | case MAP_ADD: |
| 938 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 939 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 940 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | case BINARY_POWER: |
| 942 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 943 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | case BINARY_MODULO: |
| 945 | case BINARY_ADD: |
| 946 | case BINARY_SUBTRACT: |
| 947 | case BINARY_SUBSCR: |
| 948 | case BINARY_FLOOR_DIVIDE: |
| 949 | case BINARY_TRUE_DIVIDE: |
| 950 | return -1; |
| 951 | case INPLACE_FLOOR_DIVIDE: |
| 952 | case INPLACE_TRUE_DIVIDE: |
| 953 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | case INPLACE_ADD: |
| 956 | case INPLACE_SUBTRACT: |
| 957 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 958 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | case INPLACE_MODULO: |
| 960 | return -1; |
| 961 | case STORE_SUBSCR: |
| 962 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | case DELETE_SUBSCR: |
| 964 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | case BINARY_LSHIFT: |
| 967 | case BINARY_RSHIFT: |
| 968 | case BINARY_AND: |
| 969 | case BINARY_XOR: |
| 970 | case BINARY_OR: |
| 971 | return -1; |
| 972 | case INPLACE_POWER: |
| 973 | return -1; |
| 974 | case GET_ITER: |
| 975 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | case PRINT_EXPR: |
| 978 | return -1; |
| 979 | case LOAD_BUILD_CLASS: |
| 980 | return 1; |
| 981 | case INPLACE_LSHIFT: |
| 982 | case INPLACE_RSHIFT: |
| 983 | case INPLACE_AND: |
| 984 | case INPLACE_XOR: |
| 985 | case INPLACE_OR: |
| 986 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 989 | /* 1 in the normal flow. |
| 990 | * Restore the stack position and push 6 values before jumping to |
| 991 | * the handler if an exception be raised. */ |
| 992 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | case RETURN_VALUE: |
| 994 | return -1; |
| 995 | case IMPORT_STAR: |
| 996 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 997 | case SETUP_ANNOTATIONS: |
| 998 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | case YIELD_VALUE: |
| 1000 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 1001 | case YIELD_FROM: |
| 1002 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | case POP_BLOCK: |
| 1004 | return 0; |
| 1005 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1006 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | case STORE_NAME: |
| 1009 | return -1; |
| 1010 | case DELETE_NAME: |
| 1011 | return 0; |
| 1012 | case UNPACK_SEQUENCE: |
| 1013 | return oparg-1; |
| 1014 | case UNPACK_EX: |
| 1015 | return (oparg&0xFF) + (oparg>>8); |
| 1016 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1017 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 1018 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | case STORE_ATTR: |
| 1021 | return -2; |
| 1022 | case DELETE_ATTR: |
| 1023 | return -1; |
| 1024 | case STORE_GLOBAL: |
| 1025 | return -1; |
| 1026 | case DELETE_GLOBAL: |
| 1027 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | case LOAD_CONST: |
| 1029 | return 1; |
| 1030 | case LOAD_NAME: |
| 1031 | return 1; |
| 1032 | case BUILD_TUPLE: |
| 1033 | case BUILD_LIST: |
| 1034 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1035 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | return 1-oparg; |
| 1037 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1038 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1039 | case BUILD_CONST_KEY_MAP: |
| 1040 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | case LOAD_ATTR: |
| 1042 | return 0; |
| 1043 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1044 | case IS_OP: |
| 1045 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1047 | case JUMP_IF_NOT_EXC_MATCH: |
| 1048 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case IMPORT_NAME: |
| 1050 | return -1; |
| 1051 | case IMPORT_FROM: |
| 1052 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1053 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1054 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | case JUMP_ABSOLUTE: |
| 1057 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1058 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1059 | case JUMP_IF_TRUE_OR_POP: |
| 1060 | case JUMP_IF_FALSE_OR_POP: |
| 1061 | return jump ? 0 : -1; |
| 1062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | case POP_JUMP_IF_FALSE: |
| 1064 | case POP_JUMP_IF_TRUE: |
| 1065 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | case LOAD_GLOBAL: |
| 1068 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1069 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1070 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1072 | /* 0 in the normal flow. |
| 1073 | * Restore the stack position and push 6 values before jumping to |
| 1074 | * the handler if an exception be raised. */ |
| 1075 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1076 | case RERAISE: |
| 1077 | return -3; |
| 1078 | |
| 1079 | case WITH_EXCEPT_START: |
| 1080 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | case LOAD_FAST: |
| 1083 | return 1; |
| 1084 | case STORE_FAST: |
| 1085 | return -1; |
| 1086 | case DELETE_FAST: |
| 1087 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | case RAISE_VARARGS: |
| 1090 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1094 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1095 | case CALL_METHOD: |
| 1096 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1098 | return -oparg-1; |
| 1099 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1100 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1101 | case MAKE_FUNCTION: |
| 1102 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1103 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | case BUILD_SLICE: |
| 1105 | if (oparg == 3) |
| 1106 | return -2; |
| 1107 | else |
| 1108 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1109 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1110 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | case LOAD_CLOSURE: |
| 1112 | return 1; |
| 1113 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1114 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | return 1; |
| 1116 | case STORE_DEREF: |
| 1117 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1118 | case DELETE_DEREF: |
| 1119 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1120 | |
| 1121 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1122 | case GET_AWAITABLE: |
| 1123 | return 0; |
| 1124 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1125 | /* 0 in the normal flow. |
| 1126 | * Restore the stack position to the position before the result |
| 1127 | * of __aenter__ and push 6 values before jumping to the handler |
| 1128 | * if an exception be raised. */ |
| 1129 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1130 | case BEFORE_ASYNC_WITH: |
| 1131 | return 1; |
| 1132 | case GET_AITER: |
| 1133 | return 0; |
| 1134 | case GET_ANEXT: |
| 1135 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1136 | case GET_YIELD_FROM_ITER: |
| 1137 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1138 | case END_ASYNC_FOR: |
| 1139 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1140 | case FORMAT_VALUE: |
| 1141 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1142 | else 1->1. */ |
| 1143 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1144 | case LOAD_METHOD: |
| 1145 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1146 | case LOAD_ASSERTION_ERROR: |
| 1147 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1148 | case LIST_TO_TUPLE: |
| 1149 | return 0; |
| 1150 | case LIST_EXTEND: |
| 1151 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1152 | case DICT_MERGE: |
| 1153 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1154 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1156 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1158 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1161 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1162 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1163 | { |
| 1164 | return stack_effect(opcode, oparg, jump); |
| 1165 | } |
| 1166 | |
| 1167 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1168 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1169 | { |
| 1170 | return stack_effect(opcode, oparg, -1); |
| 1171 | } |
| 1172 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 | /* Add an opcode with no argument. |
| 1174 | Returns 0 on failure, 1 on success. |
| 1175 | */ |
| 1176 | |
| 1177 | static int |
| 1178 | compiler_addop(struct compiler *c, int opcode) |
| 1179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | basicblock *b; |
| 1181 | struct instr *i; |
| 1182 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1183 | assert(!HAS_ARG(opcode)); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1184 | if (c->c_do_not_emit_bytecode) { |
| 1185 | return 1; |
| 1186 | } |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1187 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | if (off < 0) |
| 1189 | return 0; |
| 1190 | b = c->u->u_curblock; |
| 1191 | i = &b->b_instr[off]; |
| 1192 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1193 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | if (opcode == RETURN_VALUE) |
| 1195 | b->b_return = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1196 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1200 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1201 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1202 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1203 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1205 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1206 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1208 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1210 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1211 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1212 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | return -1; |
| 1215 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1216 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | Py_DECREF(v); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | Py_DECREF(v); |
| 1221 | } |
| 1222 | else |
| 1223 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1224 | return arg; |
| 1225 | } |
| 1226 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1227 | // Merge const *o* recursively and return constant key object. |
| 1228 | static PyObject* |
| 1229 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1230 | { |
| 1231 | // None and Ellipsis are singleton, and key is the singleton. |
| 1232 | // No need to merge object and key. |
| 1233 | if (o == Py_None || o == Py_Ellipsis) { |
| 1234 | Py_INCREF(o); |
| 1235 | return o; |
| 1236 | } |
| 1237 | |
| 1238 | PyObject *key = _PyCode_ConstantKey(o); |
| 1239 | if (key == NULL) { |
| 1240 | return NULL; |
| 1241 | } |
| 1242 | |
| 1243 | // t is borrowed reference |
| 1244 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1245 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1246 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1247 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1248 | Py_DECREF(key); |
| 1249 | return t; |
| 1250 | } |
| 1251 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1252 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1253 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1254 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1255 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1256 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1257 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1258 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1259 | PyObject *u = merge_consts_recursive(c, item); |
| 1260 | if (u == NULL) { |
| 1261 | Py_DECREF(key); |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | |
| 1265 | // See _PyCode_ConstantKey() |
| 1266 | PyObject *v; // borrowed |
| 1267 | if (PyTuple_CheckExact(u)) { |
| 1268 | v = PyTuple_GET_ITEM(u, 1); |
| 1269 | } |
| 1270 | else { |
| 1271 | v = u; |
| 1272 | } |
| 1273 | if (v != item) { |
| 1274 | Py_INCREF(v); |
| 1275 | PyTuple_SET_ITEM(o, i, v); |
| 1276 | Py_DECREF(item); |
| 1277 | } |
| 1278 | |
| 1279 | Py_DECREF(u); |
| 1280 | } |
| 1281 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1282 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1283 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1284 | // constant keys. |
| 1285 | // See _PyCode_ConstantKey() for detail. |
| 1286 | assert(PyTuple_CheckExact(key)); |
| 1287 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1288 | |
| 1289 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1290 | if (len == 0) { // empty frozenset should not be re-created. |
| 1291 | return key; |
| 1292 | } |
| 1293 | PyObject *tuple = PyTuple_New(len); |
| 1294 | if (tuple == NULL) { |
| 1295 | Py_DECREF(key); |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | Py_ssize_t i = 0, pos = 0; |
| 1299 | PyObject *item; |
| 1300 | Py_hash_t hash; |
| 1301 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1302 | PyObject *k = merge_consts_recursive(c, item); |
| 1303 | if (k == NULL) { |
| 1304 | Py_DECREF(tuple); |
| 1305 | Py_DECREF(key); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | PyObject *u; |
| 1309 | if (PyTuple_CheckExact(k)) { |
| 1310 | u = PyTuple_GET_ITEM(k, 1); |
| 1311 | Py_INCREF(u); |
| 1312 | Py_DECREF(k); |
| 1313 | } |
| 1314 | else { |
| 1315 | u = k; |
| 1316 | } |
| 1317 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1318 | i++; |
| 1319 | } |
| 1320 | |
| 1321 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1322 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1323 | PyObject *new = PyFrozenSet_New(tuple); |
| 1324 | Py_DECREF(tuple); |
| 1325 | if (new == NULL) { |
| 1326 | Py_DECREF(key); |
| 1327 | return NULL; |
| 1328 | } |
| 1329 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1330 | Py_DECREF(o); |
| 1331 | PyTuple_SET_ITEM(key, 1, new); |
| 1332 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1333 | |
| 1334 | return key; |
| 1335 | } |
| 1336 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1337 | static Py_ssize_t |
| 1338 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1339 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1340 | if (c->c_do_not_emit_bytecode) { |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1344 | PyObject *key = merge_consts_recursive(c, o); |
| 1345 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1346 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1347 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1348 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1349 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1350 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1355 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1356 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1357 | if (c->c_do_not_emit_bytecode) { |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 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 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1371 | if (c->c_do_not_emit_bytecode) { |
| 1372 | return 1; |
| 1373 | } |
| 1374 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1375 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1376 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1377 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1378 | return compiler_addop_i(c, opcode, arg); |
| 1379 | } |
| 1380 | |
| 1381 | static int |
| 1382 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1384 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1385 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1386 | |
| 1387 | if (c->c_do_not_emit_bytecode) { |
| 1388 | return 1; |
| 1389 | } |
| 1390 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1391 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1392 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1393 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1394 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1395 | Py_DECREF(mangled); |
| 1396 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1397 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1398 | return compiler_addop_i(c, opcode, arg); |
| 1399 | } |
| 1400 | |
| 1401 | /* Add an opcode with an integer argument. |
| 1402 | Returns 0 on failure, 1 on success. |
| 1403 | */ |
| 1404 | |
| 1405 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1406 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | struct instr *i; |
| 1409 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1410 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1411 | if (c->c_do_not_emit_bytecode) { |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1415 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1416 | it in the C code (like Python/ceval.c). |
| 1417 | |
| 1418 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1419 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1420 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1421 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1422 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1423 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1424 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1425 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | if (off < 0) |
| 1427 | return 0; |
| 1428 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1429 | i->i_opcode = opcode; |
| 1430 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1431 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1436 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | struct instr *i; |
| 1439 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1441 | if (c->c_do_not_emit_bytecode) { |
| 1442 | return 1; |
| 1443 | } |
| 1444 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1445 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | assert(b != NULL); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1447 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (off < 0) |
| 1449 | return 0; |
| 1450 | i = &c->u->u_curblock->b_instr[off]; |
| 1451 | i->i_opcode = opcode; |
| 1452 | i->i_target = b; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1453 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1457 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1458 | to the new block. |
| 1459 | |
| 1460 | The returns inside this macro make it impossible to decref objects |
| 1461 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1462 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1463 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | if (compiler_next_block((C)) == NULL) \ |
| 1465 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | if (!compiler_addop((C), (OP))) \ |
| 1470 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 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 | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1527 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1528 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1529 | return 0; \ |
| 1530 | } |
| 1531 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1533 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1534 | */ |
| 1535 | |
| 1536 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1538 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1541 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1543 | compiler_exit_scope(c); \ |
| 1544 | return 0; \ |
| 1545 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1548 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1550 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1554 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1555 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1557 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1558 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1559 | return 0; \ |
| 1560 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1563 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1565 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1567 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1568 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1569 | compiler_exit_scope(c); \ |
| 1570 | return 0; \ |
| 1571 | } \ |
| 1572 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1575 | /* Search if variable annotations are present statically in a block. */ |
| 1576 | |
| 1577 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1578 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1579 | { |
| 1580 | int i, j, res = 0; |
| 1581 | stmt_ty st; |
| 1582 | |
| 1583 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1584 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1585 | switch (st->kind) { |
| 1586 | case AnnAssign_kind: |
| 1587 | return 1; |
| 1588 | case For_kind: |
| 1589 | res = find_ann(st->v.For.body) || |
| 1590 | find_ann(st->v.For.orelse); |
| 1591 | break; |
| 1592 | case AsyncFor_kind: |
| 1593 | res = find_ann(st->v.AsyncFor.body) || |
| 1594 | find_ann(st->v.AsyncFor.orelse); |
| 1595 | break; |
| 1596 | case While_kind: |
| 1597 | res = find_ann(st->v.While.body) || |
| 1598 | find_ann(st->v.While.orelse); |
| 1599 | break; |
| 1600 | case If_kind: |
| 1601 | res = find_ann(st->v.If.body) || |
| 1602 | find_ann(st->v.If.orelse); |
| 1603 | break; |
| 1604 | case With_kind: |
| 1605 | res = find_ann(st->v.With.body); |
| 1606 | break; |
| 1607 | case AsyncWith_kind: |
| 1608 | res = find_ann(st->v.AsyncWith.body); |
| 1609 | break; |
| 1610 | case Try_kind: |
| 1611 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1612 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1613 | st->v.Try.handlers, j); |
| 1614 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1615 | return 1; |
| 1616 | } |
| 1617 | } |
| 1618 | res = find_ann(st->v.Try.body) || |
| 1619 | find_ann(st->v.Try.finalbody) || |
| 1620 | find_ann(st->v.Try.orelse); |
| 1621 | break; |
| 1622 | default: |
| 1623 | res = 0; |
| 1624 | } |
| 1625 | if (res) { |
| 1626 | break; |
| 1627 | } |
| 1628 | } |
| 1629 | return res; |
| 1630 | } |
| 1631 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1632 | /* |
| 1633 | * Frame block handling functions |
| 1634 | */ |
| 1635 | |
| 1636 | static int |
| 1637 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1638 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1639 | { |
| 1640 | struct fblockinfo *f; |
| 1641 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1642 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1643 | } |
| 1644 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1645 | f->fb_type = t; |
| 1646 | f->fb_block = b; |
| 1647 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1648 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1649 | return 1; |
| 1650 | } |
| 1651 | |
| 1652 | static void |
| 1653 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1654 | { |
| 1655 | struct compiler_unit *u = c->u; |
| 1656 | assert(u->u_nfblocks > 0); |
| 1657 | u->u_nfblocks--; |
| 1658 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1659 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1660 | } |
| 1661 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1662 | static int |
| 1663 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1664 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1665 | ADDOP(c, DUP_TOP); |
| 1666 | ADDOP(c, DUP_TOP); |
| 1667 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1668 | return 1; |
| 1669 | } |
| 1670 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1671 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1672 | * popping the blocks will be restored afterwards, unless another |
| 1673 | * return, break or continue is found. In which case, the TOS will |
| 1674 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1675 | */ |
| 1676 | static int |
| 1677 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1678 | int preserve_tos) |
| 1679 | { |
| 1680 | switch (info->fb_type) { |
| 1681 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1682 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1683 | return 1; |
| 1684 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1685 | case FOR_LOOP: |
| 1686 | /* Pop the iterator */ |
| 1687 | if (preserve_tos) { |
| 1688 | ADDOP(c, ROT_TWO); |
| 1689 | } |
| 1690 | ADDOP(c, POP_TOP); |
| 1691 | return 1; |
| 1692 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1693 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1694 | ADDOP(c, POP_BLOCK); |
| 1695 | return 1; |
| 1696 | |
| 1697 | case FINALLY_TRY: |
| 1698 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1699 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1700 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1701 | return 0; |
| 1702 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1703 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1704 | /* Emit the finally block, restoring the line number when done */ |
| 1705 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1706 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1707 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1708 | if (preserve_tos) { |
| 1709 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1710 | } |
| 1711 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1712 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1713 | case FINALLY_END: |
| 1714 | if (preserve_tos) { |
| 1715 | ADDOP(c, ROT_FOUR); |
| 1716 | } |
| 1717 | ADDOP(c, POP_TOP); |
| 1718 | ADDOP(c, POP_TOP); |
| 1719 | ADDOP(c, POP_TOP); |
| 1720 | if (preserve_tos) { |
| 1721 | ADDOP(c, ROT_FOUR); |
| 1722 | } |
| 1723 | ADDOP(c, POP_EXCEPT); |
| 1724 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1725 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1726 | case WITH: |
| 1727 | case ASYNC_WITH: |
| 1728 | ADDOP(c, POP_BLOCK); |
| 1729 | if (preserve_tos) { |
| 1730 | ADDOP(c, ROT_TWO); |
| 1731 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1732 | if(!compiler_call_exit_with_nones(c)) { |
| 1733 | return 0; |
| 1734 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1735 | if (info->fb_type == ASYNC_WITH) { |
| 1736 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1737 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1738 | ADDOP(c, YIELD_FROM); |
| 1739 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1740 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1741 | return 1; |
| 1742 | |
| 1743 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1744 | if (info->fb_datum) { |
| 1745 | ADDOP(c, POP_BLOCK); |
| 1746 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1747 | if (preserve_tos) { |
| 1748 | ADDOP(c, ROT_FOUR); |
| 1749 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1750 | ADDOP(c, POP_EXCEPT); |
| 1751 | if (info->fb_datum) { |
| 1752 | ADDOP_LOAD_CONST(c, Py_None); |
| 1753 | compiler_nameop(c, info->fb_datum, Store); |
| 1754 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1755 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1756 | return 1; |
| 1757 | |
| 1758 | case POP_VALUE: |
| 1759 | if (preserve_tos) { |
| 1760 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1761 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1762 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1763 | return 1; |
| 1764 | } |
| 1765 | Py_UNREACHABLE(); |
| 1766 | } |
| 1767 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1768 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1769 | static int |
| 1770 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1771 | if (c->u->u_nfblocks == 0) { |
| 1772 | return 1; |
| 1773 | } |
| 1774 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1775 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1776 | *loop = top; |
| 1777 | return 1; |
| 1778 | } |
| 1779 | struct fblockinfo copy = *top; |
| 1780 | c->u->u_nfblocks--; |
| 1781 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1782 | return 0; |
| 1783 | } |
| 1784 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1785 | return 0; |
| 1786 | } |
| 1787 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1788 | c->u->u_nfblocks++; |
| 1789 | return 1; |
| 1790 | } |
| 1791 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1792 | /* Compile a sequence of statements, checking for a docstring |
| 1793 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1794 | |
| 1795 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1796 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1798 | int i = 0; |
| 1799 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1800 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1801 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1802 | /* Set current line number to the line number of first statement. |
| 1803 | This way line number for SETUP_ANNOTATIONS will always |
| 1804 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1805 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1806 | 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] | 1807 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1808 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1809 | } |
| 1810 | /* Every annotated class and module should have __annotations__. */ |
| 1811 | if (find_ann(stmts)) { |
| 1812 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1813 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1814 | if (!asdl_seq_LEN(stmts)) |
| 1815 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1816 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1817 | if (c->c_optimize < 2) { |
| 1818 | docstring = _PyAST_GetDocString(stmts); |
| 1819 | if (docstring) { |
| 1820 | i = 1; |
| 1821 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1822 | assert(st->kind == Expr_kind); |
| 1823 | VISIT(c, expr, st->v.Expr.value); |
| 1824 | if (!compiler_nameop(c, __doc__, Store)) |
| 1825 | return 0; |
| 1826 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1828 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1829 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | static PyCodeObject * |
| 1834 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | PyCodeObject *co; |
| 1837 | int addNone = 1; |
| 1838 | static PyObject *module; |
| 1839 | if (!module) { |
| 1840 | module = PyUnicode_InternFromString("<module>"); |
| 1841 | if (!module) |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1845 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | return NULL; |
| 1847 | switch (mod->kind) { |
| 1848 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1849 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | compiler_exit_scope(c); |
| 1851 | return 0; |
| 1852 | } |
| 1853 | break; |
| 1854 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1855 | if (find_ann(mod->v.Interactive.body)) { |
| 1856 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1857 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1859 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | break; |
| 1861 | case Expression_kind: |
| 1862 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1863 | addNone = 0; |
| 1864 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | default: |
| 1866 | PyErr_Format(PyExc_SystemError, |
| 1867 | "module kind %d should not be possible", |
| 1868 | mod->kind); |
| 1869 | return 0; |
| 1870 | } |
| 1871 | co = assemble(c, addNone); |
| 1872 | compiler_exit_scope(c); |
| 1873 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1876 | /* The test for LOCAL must come before the test for FREE in order to |
| 1877 | handle classes where name is both local and free. The local var is |
| 1878 | 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] | 1879 | */ |
| 1880 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1881 | static int |
| 1882 | get_ref_type(struct compiler *c, PyObject *name) |
| 1883 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1884 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1885 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1886 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1887 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1888 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | if (scope == 0) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1890 | _Py_FatalErrorFormat(__func__, |
| 1891 | "unknown scope for %.100s in %.100s(%s)\n" |
| 1892 | "symbols: %s\nlocals: %s\nglobals: %s", |
| 1893 | PyUnicode_AsUTF8(name), |
| 1894 | PyUnicode_AsUTF8(c->u->u_name), |
| 1895 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1896 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1897 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1898 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static int |
| 1905 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1906 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1907 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1908 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1909 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1910 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1911 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1915 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1916 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1917 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1918 | if (qualname == NULL) |
| 1919 | qualname = co->co_name; |
| 1920 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1921 | if (free) { |
| 1922 | for (i = 0; i < free; ++i) { |
| 1923 | /* Bypass com_addop_varname because it will generate |
| 1924 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1925 | */ |
| 1926 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1927 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1928 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1929 | /* Special case: If a class contains a method with a |
| 1930 | free variable that has the same name as a method, |
| 1931 | the name will be considered free *and* local in the |
| 1932 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1933 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1934 | */ |
| 1935 | reftype = get_ref_type(c, name); |
| 1936 | if (reftype == CELL) |
| 1937 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1938 | else /* (reftype == FREE) */ |
| 1939 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1940 | if (arg == -1) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1941 | _Py_FatalErrorFormat(__func__, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1942 | "lookup %s in %s %d %d\n" |
| 1943 | "freevars of %s: %s\n", |
| 1944 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1945 | PyUnicode_AsUTF8(c->u->u_name), |
| 1946 | reftype, arg, |
| 1947 | PyUnicode_AsUTF8(co->co_name), |
| 1948 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1949 | } |
| 1950 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1952 | flags |= 0x08; |
| 1953 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1955 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1956 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1957 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1962 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | if (!decos) |
| 1967 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1970 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1971 | } |
| 1972 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1976 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 1977 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1978 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1979 | /* Push a dict of keyword-only default values. |
| 1980 | |
| 1981 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1982 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1983 | int i; |
| 1984 | PyObject *keys = NULL; |
| 1985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1987 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1988 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1989 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1990 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1991 | if (!mangled) { |
| 1992 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1994 | if (keys == NULL) { |
| 1995 | keys = PyList_New(1); |
| 1996 | if (keys == NULL) { |
| 1997 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1998 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1999 | } |
| 2000 | PyList_SET_ITEM(keys, 0, mangled); |
| 2001 | } |
| 2002 | else { |
| 2003 | int res = PyList_Append(keys, mangled); |
| 2004 | Py_DECREF(mangled); |
| 2005 | if (res == -1) { |
| 2006 | goto error; |
| 2007 | } |
| 2008 | } |
| 2009 | if (!compiler_visit_expr(c, default_)) { |
| 2010 | goto error; |
| 2011 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | } |
| 2013 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2014 | if (keys != NULL) { |
| 2015 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2016 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2017 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2018 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2019 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2020 | assert(default_count > 0); |
| 2021 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2022 | } |
| 2023 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2024 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | error: |
| 2028 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2029 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2033 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2034 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2035 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2036 | return 1; |
| 2037 | } |
| 2038 | |
| 2039 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2040 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2041 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2044 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2045 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2046 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2047 | |
| 2048 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2049 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2050 | VISIT(c, annexpr, annotation); |
| 2051 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2053 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2057 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2058 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2059 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2060 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2062 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2063 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | c, |
| 2065 | arg->arg, |
| 2066 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2067 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2068 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2070 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | static int |
| 2074 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2075 | expr_ty returns) |
| 2076 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2077 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2078 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2079 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2080 | 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] | 2081 | */ |
| 2082 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2083 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2084 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2085 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2086 | return 0; |
| 2087 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2088 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2089 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2090 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2091 | args->vararg->annotation, &annotations_len)) |
| 2092 | return 0; |
| 2093 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2094 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2095 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2096 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2097 | args->kwarg->annotation, &annotations_len)) |
| 2098 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | if (!return_str) { |
| 2101 | return_str = PyUnicode_InternFromString("return"); |
| 2102 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2103 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2105 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2106 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2109 | if (annotations_len) { |
| 2110 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2111 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2113 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2114 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2118 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2119 | { |
| 2120 | VISIT_SEQ(c, expr, args->defaults); |
| 2121 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2122 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2125 | static Py_ssize_t |
| 2126 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2127 | { |
| 2128 | Py_ssize_t funcflags = 0; |
| 2129 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2130 | if (!compiler_visit_defaults(c, args)) |
| 2131 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2132 | funcflags |= 0x01; |
| 2133 | } |
| 2134 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2135 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2136 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2137 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2138 | return -1; |
| 2139 | } |
| 2140 | else if (res > 0) { |
| 2141 | funcflags |= 0x02; |
| 2142 | } |
| 2143 | } |
| 2144 | return funcflags; |
| 2145 | } |
| 2146 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2147 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2148 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2149 | { |
| 2150 | |
| 2151 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2152 | compiler_error(c, "cannot assign to __debug__"); |
| 2153 | return 1; |
| 2154 | } |
| 2155 | return 0; |
| 2156 | } |
| 2157 | |
| 2158 | static int |
| 2159 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2160 | { |
| 2161 | if (arg != NULL) { |
| 2162 | if (forbidden_name(c, arg->arg, Store)) |
| 2163 | return 0; |
| 2164 | } |
| 2165 | return 1; |
| 2166 | } |
| 2167 | |
| 2168 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2169 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2170 | { |
| 2171 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2172 | 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] | 2173 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2174 | return 0; |
| 2175 | } |
| 2176 | } |
| 2177 | return 1; |
| 2178 | } |
| 2179 | |
| 2180 | static int |
| 2181 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2182 | { |
| 2183 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2184 | return 0; |
| 2185 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2186 | return 0; |
| 2187 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2188 | return 0; |
| 2189 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2190 | return 0; |
| 2191 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2192 | return 0; |
| 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2197 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2200 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2201 | arguments_ty args; |
| 2202 | expr_ty returns; |
| 2203 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2204 | asdl_expr_seq* decos; |
| 2205 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2206 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2207 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2208 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2209 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2210 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2211 | if (is_async) { |
| 2212 | assert(s->kind == AsyncFunctionDef_kind); |
| 2213 | |
| 2214 | args = s->v.AsyncFunctionDef.args; |
| 2215 | returns = s->v.AsyncFunctionDef.returns; |
| 2216 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2217 | name = s->v.AsyncFunctionDef.name; |
| 2218 | body = s->v.AsyncFunctionDef.body; |
| 2219 | |
| 2220 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2221 | } else { |
| 2222 | assert(s->kind == FunctionDef_kind); |
| 2223 | |
| 2224 | args = s->v.FunctionDef.args; |
| 2225 | returns = s->v.FunctionDef.returns; |
| 2226 | decos = s->v.FunctionDef.decorator_list; |
| 2227 | name = s->v.FunctionDef.name; |
| 2228 | body = s->v.FunctionDef.body; |
| 2229 | |
| 2230 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2231 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2232 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2233 | if (!compiler_check_debug_args(c, args)) |
| 2234 | return 0; |
| 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | if (!compiler_decorators(c, decos)) |
| 2237 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2238 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2239 | firstlineno = s->lineno; |
| 2240 | if (asdl_seq_LEN(decos)) { |
| 2241 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2242 | } |
| 2243 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2244 | funcflags = compiler_default_arguments(c, args); |
| 2245 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2247 | } |
| 2248 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2249 | annotations = compiler_visit_annotations(c, args, returns); |
| 2250 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2251 | return 0; |
| 2252 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2253 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2254 | funcflags |= 0x04; |
| 2255 | } |
| 2256 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2257 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2258 | return 0; |
| 2259 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2260 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2261 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2262 | if (c->c_optimize < 2) { |
| 2263 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2264 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2265 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | compiler_exit_scope(c); |
| 2267 | return 0; |
| 2268 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2271 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2273 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2274 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2275 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2277 | qualname = c->u->u_qualname; |
| 2278 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2279 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2280 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2281 | Py_XDECREF(qualname); |
| 2282 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2284 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2285 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2286 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2287 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | /* decorators */ |
| 2291 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2292 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2293 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2294 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2295 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | static int |
| 2299 | compiler_class(struct compiler *c, stmt_ty s) |
| 2300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | PyCodeObject *co; |
| 2302 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2303 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2304 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | if (!compiler_decorators(c, decos)) |
| 2307 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2308 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2309 | firstlineno = s->lineno; |
| 2310 | if (asdl_seq_LEN(decos)) { |
| 2311 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2312 | } |
| 2313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | /* ultimately generate code for: |
| 2315 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2316 | where: |
| 2317 | <func> is a function/closure created from the class body; |
| 2318 | it has a single argument (__locals__) where the dict |
| 2319 | (or MutableSequence) representing the locals is passed |
| 2320 | <name> is the class name |
| 2321 | <bases> is the positional arguments and *varargs argument |
| 2322 | <keywords> is the keyword arguments and **kwds argument |
| 2323 | This borrows from compiler_call. |
| 2324 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2327 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2328 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2329 | return 0; |
| 2330 | /* this block represents what we do in the new scope */ |
| 2331 | { |
| 2332 | /* use the class name for name mangling */ |
| 2333 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2334 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | /* load (global) __name__ ... */ |
| 2336 | str = PyUnicode_InternFromString("__name__"); |
| 2337 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2338 | Py_XDECREF(str); |
| 2339 | compiler_exit_scope(c); |
| 2340 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2341 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | Py_DECREF(str); |
| 2343 | /* ... and store it as __module__ */ |
| 2344 | str = PyUnicode_InternFromString("__module__"); |
| 2345 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2346 | Py_XDECREF(str); |
| 2347 | compiler_exit_scope(c); |
| 2348 | return 0; |
| 2349 | } |
| 2350 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2351 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2352 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2353 | str = PyUnicode_InternFromString("__qualname__"); |
| 2354 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2355 | Py_XDECREF(str); |
| 2356 | compiler_exit_scope(c); |
| 2357 | return 0; |
| 2358 | } |
| 2359 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2361 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | compiler_exit_scope(c); |
| 2363 | return 0; |
| 2364 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2365 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2366 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2367 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2368 | str = PyUnicode_InternFromString("__class__"); |
| 2369 | if (str == NULL) { |
| 2370 | compiler_exit_scope(c); |
| 2371 | return 0; |
| 2372 | } |
| 2373 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2374 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2375 | if (i < 0) { |
| 2376 | compiler_exit_scope(c); |
| 2377 | return 0; |
| 2378 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2379 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2382 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2383 | str = PyUnicode_InternFromString("__classcell__"); |
| 2384 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2385 | Py_XDECREF(str); |
| 2386 | compiler_exit_scope(c); |
| 2387 | return 0; |
| 2388 | } |
| 2389 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2391 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2392 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2393 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2394 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2395 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2396 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | /* create the code object */ |
| 2398 | co = assemble(c, 1); |
| 2399 | } |
| 2400 | /* leave the new scope */ |
| 2401 | compiler_exit_scope(c); |
| 2402 | if (co == NULL) |
| 2403 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 | /* 2. load the 'build_class' function */ |
| 2406 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2407 | |
| 2408 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2409 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | Py_DECREF(co); |
| 2411 | |
| 2412 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2413 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | |
| 2415 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2416 | 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] | 2417 | return 0; |
| 2418 | |
| 2419 | /* 6. apply decorators */ |
| 2420 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2421 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2422 | } |
| 2423 | |
| 2424 | /* 7. store into <name> */ |
| 2425 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2426 | return 0; |
| 2427 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2430 | /* Return 0 if the expression is a constant value except named singletons. |
| 2431 | Return 1 otherwise. */ |
| 2432 | static int |
| 2433 | check_is_arg(expr_ty e) |
| 2434 | { |
| 2435 | if (e->kind != Constant_kind) { |
| 2436 | return 1; |
| 2437 | } |
| 2438 | PyObject *value = e->v.Constant.value; |
| 2439 | return (value == Py_None |
| 2440 | || value == Py_False |
| 2441 | || value == Py_True |
| 2442 | || value == Py_Ellipsis); |
| 2443 | } |
| 2444 | |
| 2445 | /* Check operands of identity chacks ("is" and "is not"). |
| 2446 | Emit a warning if any operand is a constant except named singletons. |
| 2447 | Return 0 on error. |
| 2448 | */ |
| 2449 | static int |
| 2450 | check_compare(struct compiler *c, expr_ty e) |
| 2451 | { |
| 2452 | Py_ssize_t i, n; |
| 2453 | int left = check_is_arg(e->v.Compare.left); |
| 2454 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2455 | for (i = 0; i < n; i++) { |
| 2456 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2457 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2458 | if (op == Is || op == IsNot) { |
| 2459 | if (!right || !left) { |
| 2460 | const char *msg = (op == Is) |
| 2461 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2462 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2463 | return compiler_warn(c, msg); |
| 2464 | } |
| 2465 | } |
| 2466 | left = right; |
| 2467 | } |
| 2468 | return 1; |
| 2469 | } |
| 2470 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2471 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2472 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2473 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2474 | switch (op) { |
| 2475 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2476 | cmp = Py_EQ; |
| 2477 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2478 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2479 | cmp = Py_NE; |
| 2480 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2481 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2482 | cmp = Py_LT; |
| 2483 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2484 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2485 | cmp = Py_LE; |
| 2486 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2487 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2488 | cmp = Py_GT; |
| 2489 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2490 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2491 | cmp = Py_GE; |
| 2492 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2493 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2494 | ADDOP_I(c, IS_OP, 0); |
| 2495 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2496 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2497 | ADDOP_I(c, IS_OP, 1); |
| 2498 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2499 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2500 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2501 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2502 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2503 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2504 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2505 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2506 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2507 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2508 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2509 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2510 | } |
| 2511 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2512 | |
| 2513 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2514 | static int |
| 2515 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2516 | { |
| 2517 | switch (e->kind) { |
| 2518 | case UnaryOp_kind: |
| 2519 | if (e->v.UnaryOp.op == Not) |
| 2520 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2521 | /* fallback to general implementation */ |
| 2522 | break; |
| 2523 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2524 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2525 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2526 | assert(n >= 0); |
| 2527 | int cond2 = e->v.BoolOp.op == Or; |
| 2528 | basicblock *next2 = next; |
| 2529 | if (!cond2 != !cond) { |
| 2530 | next2 = compiler_new_block(c); |
| 2531 | if (next2 == NULL) |
| 2532 | return 0; |
| 2533 | } |
| 2534 | for (i = 0; i < n; ++i) { |
| 2535 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2536 | return 0; |
| 2537 | } |
| 2538 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2539 | return 0; |
| 2540 | if (next2 != next) |
| 2541 | compiler_use_next_block(c, next2); |
| 2542 | return 1; |
| 2543 | } |
| 2544 | case IfExp_kind: { |
| 2545 | basicblock *end, *next2; |
| 2546 | end = compiler_new_block(c); |
| 2547 | if (end == NULL) |
| 2548 | return 0; |
| 2549 | next2 = compiler_new_block(c); |
| 2550 | if (next2 == NULL) |
| 2551 | return 0; |
| 2552 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2553 | return 0; |
| 2554 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2555 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2556 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2557 | compiler_use_next_block(c, next2); |
| 2558 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2559 | return 0; |
| 2560 | compiler_use_next_block(c, end); |
| 2561 | return 1; |
| 2562 | } |
| 2563 | case Compare_kind: { |
| 2564 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2565 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2566 | if (!check_compare(c, e)) { |
| 2567 | return 0; |
| 2568 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2569 | basicblock *cleanup = compiler_new_block(c); |
| 2570 | if (cleanup == NULL) |
| 2571 | return 0; |
| 2572 | VISIT(c, expr, e->v.Compare.left); |
| 2573 | for (i = 0; i < n; i++) { |
| 2574 | VISIT(c, expr, |
| 2575 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2576 | ADDOP(c, DUP_TOP); |
| 2577 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2578 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2579 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2580 | NEXT_BLOCK(c); |
| 2581 | } |
| 2582 | 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] | 2583 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2584 | 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] | 2585 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2586 | basicblock *end = compiler_new_block(c); |
| 2587 | if (end == NULL) |
| 2588 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2589 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2590 | compiler_use_next_block(c, cleanup); |
| 2591 | ADDOP(c, POP_TOP); |
| 2592 | if (!cond) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2593 | ADDOP_JUMP(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2594 | } |
| 2595 | compiler_use_next_block(c, end); |
| 2596 | return 1; |
| 2597 | } |
| 2598 | /* fallback to general implementation */ |
| 2599 | break; |
| 2600 | } |
| 2601 | default: |
| 2602 | /* fallback to general implementation */ |
| 2603 | break; |
| 2604 | } |
| 2605 | |
| 2606 | /* general implementation */ |
| 2607 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2608 | 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] | 2609 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2610 | return 1; |
| 2611 | } |
| 2612 | |
| 2613 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2614 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | basicblock *end, *next; |
| 2617 | |
| 2618 | assert(e->kind == IfExp_kind); |
| 2619 | end = compiler_new_block(c); |
| 2620 | if (end == NULL) |
| 2621 | return 0; |
| 2622 | next = compiler_new_block(c); |
| 2623 | if (next == NULL) |
| 2624 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2625 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2626 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2628 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | compiler_use_next_block(c, next); |
| 2630 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2631 | compiler_use_next_block(c, end); |
| 2632 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2636 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2639 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2641 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | arguments_ty args = e->v.Lambda.args; |
| 2643 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2644 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2645 | if (!compiler_check_debug_args(c, args)) |
| 2646 | return 0; |
| 2647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | if (!name) { |
| 2649 | name = PyUnicode_InternFromString("<lambda>"); |
| 2650 | if (!name) |
| 2651 | return 0; |
| 2652 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2653 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2654 | funcflags = compiler_default_arguments(c, args); |
| 2655 | if (funcflags == -1) { |
| 2656 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2658 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2659 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2660 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | /* Make None the first constant, so the lambda can't have a |
| 2664 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2665 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2669 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2671 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2672 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2673 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | } |
| 2675 | else { |
| 2676 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2677 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2679 | qualname = c->u->u_qualname; |
| 2680 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2682 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2684 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2685 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2686 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | Py_DECREF(co); |
| 2688 | |
| 2689 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2693 | compiler_if(struct compiler *c, stmt_ty s) |
| 2694 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | basicblock *end, *next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | assert(s->kind == If_kind); |
| 2697 | end = compiler_new_block(c); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 2698 | if (end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | return 0; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 2700 | } |
| 2701 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2702 | next = compiler_new_block(c); |
| 2703 | if (next == NULL) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2704 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2705 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 2706 | } |
| 2707 | else { |
| 2708 | next = end; |
| 2709 | } |
| 2710 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
| 2711 | return 0; |
| 2712 | } |
| 2713 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2714 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2715 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
| 2716 | compiler_use_next_block(c, next); |
| 2717 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | } |
| 2719 | compiler_use_next_block(c, end); |
| 2720 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | static int |
| 2724 | compiler_for(struct compiler *c, stmt_ty s) |
| 2725 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2726 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2729 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | cleanup = compiler_new_block(c); |
| 2731 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2732 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2734 | } |
| 2735 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2737 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | VISIT(c, expr, s->v.For.iter); |
| 2739 | ADDOP(c, GET_ITER); |
| 2740 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2741 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2742 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | VISIT(c, expr, s->v.For.target); |
| 2744 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | f5e97b7 | 2020-12-14 11:28:39 +0000 | [diff] [blame] | 2745 | /* Mark jump as artificial */ |
| 2746 | c->u->u_lineno = -1; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2747 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2749 | |
| 2750 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2753 | compiler_use_next_block(c, end); |
| 2754 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2757 | |
| 2758 | static int |
| 2759 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2760 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2761 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2762 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2763 | c->u->u_ste->ste_coroutine = 1; |
| 2764 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2765 | return compiler_error(c, "'async for' outside async function"); |
| 2766 | } |
| 2767 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2768 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2769 | except = compiler_new_block(c); |
| 2770 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2771 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2772 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2773 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2774 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2775 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2776 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2777 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2778 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2779 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2780 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2781 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2782 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2783 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2784 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2785 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2786 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2787 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2788 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2789 | /* Success block for __anext__ */ |
| 2790 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2791 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2792 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2793 | |
| 2794 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2795 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2796 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2797 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2798 | |
| 2799 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2800 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2801 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2802 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2803 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2804 | |
| 2805 | compiler_use_next_block(c, end); |
| 2806 | |
| 2807 | return 1; |
| 2808 | } |
| 2809 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2810 | static int |
| 2811 | compiler_while(struct compiler *c, stmt_ty s) |
| 2812 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2813 | basicblock *loop, *body, *end, *anchor = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2815 | body = compiler_new_block(c); |
| 2816 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2817 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2818 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2821 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2822 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | } |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 2825 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2826 | return 0; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 2831 | SET_LOC(c, s); |
| 2832 | if (!compiler_jump_if(c, s->v.While.test, body, 1)) { |
| 2833 | return 0; |
| 2834 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2835 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2836 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2837 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2838 | compiler_use_next_block(c, anchor); |
| 2839 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2841 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2848 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2849 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2850 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2851 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2852 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2853 | return compiler_error(c, "'return' outside function"); |
| 2854 | if (s->v.Return.value != NULL && |
| 2855 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2856 | { |
| 2857 | return compiler_error( |
| 2858 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2860 | if (preserve_tos) { |
| 2861 | VISIT(c, expr, s->v.Return.value); |
| 2862 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2863 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2864 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2865 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2866 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2867 | } |
| 2868 | else if (!preserve_tos) { |
| 2869 | VISIT(c, expr, s->v.Return.value); |
| 2870 | } |
| 2871 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2872 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2877 | static int |
| 2878 | compiler_break(struct compiler *c) |
| 2879 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2880 | struct fblockinfo *loop = NULL; |
| 2881 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2882 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2883 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2884 | if (loop == NULL) { |
| 2885 | return compiler_error(c, "'break' outside loop"); |
| 2886 | } |
| 2887 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2888 | return 0; |
| 2889 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2890 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2891 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2892 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | static int |
| 2896 | compiler_continue(struct compiler *c) |
| 2897 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2898 | struct fblockinfo *loop = NULL; |
| 2899 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2900 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2901 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2902 | if (loop == NULL) { |
| 2903 | return compiler_error(c, "'continue' not properly in loop"); |
| 2904 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2905 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2906 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2907 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2911 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 | |
| 2913 | SETUP_FINALLY L |
| 2914 | <code for body> |
| 2915 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2916 | <code for finalbody> |
| 2917 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2918 | L: |
| 2919 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2920 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | The special instructions use the block stack. Each block |
| 2923 | stack entry contains the instruction that created it (here |
| 2924 | SETUP_FINALLY), the level of the value stack at the time the |
| 2925 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2927 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | Pushes the current value stack level and the label |
| 2929 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2930 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2931 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2933 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2934 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2935 | exceptions are pushed onto the value stack (and the exception |
| 2936 | condition is cleared), and the interpreter jumps to the label |
| 2937 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | */ |
| 2939 | |
| 2940 | static int |
| 2941 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2942 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2943 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | body = compiler_new_block(c); |
| 2946 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2947 | exit = compiler_new_block(c); |
| 2948 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2950 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2951 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2952 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2954 | 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] | 2955 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2956 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2957 | if (!compiler_try_except(c, s)) |
| 2958 | return 0; |
| 2959 | } |
| 2960 | else { |
| 2961 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2962 | } |
Mark Shannon | 56aa20f | 2020-12-14 10:19:10 +0000 | [diff] [blame] | 2963 | /* Mark code as artificial */ |
| 2964 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2966 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 2967 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2968 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2969 | /* `finally` block */ |
| 2970 | compiler_use_next_block(c, end); |
| 2971 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 2972 | return 0; |
| 2973 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2974 | compiler_pop_fblock(c, FINALLY_END, end); |
| 2975 | ADDOP(c, RERAISE); |
| 2976 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2981 | 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] | 2982 | (The contents of the value stack is shown in [], with the top |
| 2983 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2984 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2985 | |
| 2986 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2987 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | [] <code for S> |
| 2989 | [] POP_BLOCK |
| 2990 | [] JUMP_FORWARD L0 |
| 2991 | |
| 2992 | [tb, val, exc] L1: DUP ) |
| 2993 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2994 | [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] | 2995 | [tb, val, exc] POP |
| 2996 | [tb, val] <assign to V1> (or POP if no V1) |
| 2997 | [tb] POP |
| 2998 | [] <code for S1> |
| 2999 | JUMP_FORWARD L0 |
| 3000 | |
| 3001 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3002 | .............................etc....................... |
| 3003 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3004 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | |
| 3006 | [] L0: <next statement> |
| 3007 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3008 | Of course, parts are not generated if Vi or Ei is not present. |
| 3009 | */ |
| 3010 | static int |
| 3011 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3012 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3014 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | body = compiler_new_block(c); |
| 3017 | except = compiler_new_block(c); |
| 3018 | orelse = compiler_new_block(c); |
| 3019 | end = compiler_new_block(c); |
| 3020 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3021 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3022 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3024 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3026 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | ADDOP(c, POP_BLOCK); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3028 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3029 | ADDOP_JUMP(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3030 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3032 | /* Runtime will push a block here, so we need to account for that */ |
| 3033 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3034 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | for (i = 0; i < n; i++) { |
| 3036 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3037 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3039 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3040 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3041 | except = compiler_new_block(c); |
| 3042 | if (except == NULL) |
| 3043 | return 0; |
| 3044 | if (handler->v.ExceptHandler.type) { |
| 3045 | ADDOP(c, DUP_TOP); |
| 3046 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3047 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3048 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | } |
| 3050 | ADDOP(c, POP_TOP); |
| 3051 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3052 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3053 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3054 | cleanup_end = compiler_new_block(c); |
| 3055 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3056 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3057 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3058 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3059 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3060 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3061 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3063 | /* |
| 3064 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3065 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3066 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3067 | try: |
| 3068 | # body |
| 3069 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3070 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3071 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3072 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3074 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3075 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3076 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3077 | 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] | 3078 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3080 | /* second # body */ |
| 3081 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3082 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3083 | ADDOP(c, POP_BLOCK); |
| 3084 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3085 | /* name = None; del name; # Mark as artificial */ |
| 3086 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3087 | ADDOP_LOAD_CONST(c, Py_None); |
| 3088 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3089 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3090 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3092 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3093 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3095 | /* name = None; del name; # Mark as artificial */ |
| 3096 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3097 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3098 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3099 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3100 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3101 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3102 | } |
| 3103 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3104 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3106 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3107 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3108 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3110 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3111 | ADDOP(c, POP_TOP); |
| 3112 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3113 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3114 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3116 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3117 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3118 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3119 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | compiler_use_next_block(c, except); |
| 3121 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3122 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3123 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3125 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | compiler_use_next_block(c, end); |
| 3127 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3131 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3132 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3133 | return compiler_try_finally(c, s); |
| 3134 | else |
| 3135 | return compiler_try_except(c, s); |
| 3136 | } |
| 3137 | |
| 3138 | |
| 3139 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3140 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | /* The IMPORT_NAME opcode was already generated. This function |
| 3143 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | 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] | 3146 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3148 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3149 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3150 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3151 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3152 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3154 | while (1) { |
| 3155 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3157 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3158 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3159 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3160 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3162 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3163 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3164 | if (dot == -1) { |
| 3165 | break; |
| 3166 | } |
| 3167 | ADDOP(c, ROT_TWO); |
| 3168 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3170 | if (!compiler_nameop(c, asname, Store)) { |
| 3171 | return 0; |
| 3172 | } |
| 3173 | ADDOP(c, POP_TOP); |
| 3174 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | } |
| 3176 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | static int |
| 3180 | compiler_import(struct compiler *c, stmt_ty s) |
| 3181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | /* The Import node stores a module name like a.b.c as a single |
| 3183 | string. This is convenient for all cases except |
| 3184 | import a.b.c as d |
| 3185 | where we need to parse that string to extract the individual |
| 3186 | module names. |
| 3187 | XXX Perhaps change the representation to make this case simpler? |
| 3188 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3189 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3190 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3191 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | for (i = 0; i < n; i++) { |
| 3193 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3194 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3195 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3196 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3197 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | if (alias->asname) { |
| 3201 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3202 | if (!r) |
| 3203 | return r; |
| 3204 | } |
| 3205 | else { |
| 3206 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3207 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3208 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3209 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3210 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3211 | if (tmp == NULL) |
| 3212 | return 0; |
| 3213 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3215 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | Py_DECREF(tmp); |
| 3217 | } |
| 3218 | if (!r) |
| 3219 | return r; |
| 3220 | } |
| 3221 | } |
| 3222 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | static int |
| 3226 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3227 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3228 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3229 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3232 | if (!empty_string) { |
| 3233 | empty_string = PyUnicode_FromString(""); |
| 3234 | if (!empty_string) |
| 3235 | return 0; |
| 3236 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3237 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3238 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3239 | |
| 3240 | names = PyTuple_New(n); |
| 3241 | if (!names) |
| 3242 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | /* build up the names */ |
| 3245 | for (i = 0; i < n; i++) { |
| 3246 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3247 | Py_INCREF(alias->name); |
| 3248 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3249 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3252 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | Py_DECREF(names); |
| 3254 | return compiler_error(c, "from __future__ imports must occur " |
| 3255 | "at the beginning of the file"); |
| 3256 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3257 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | if (s->v.ImportFrom.module) { |
| 3260 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3261 | } |
| 3262 | else { |
| 3263 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3264 | } |
| 3265 | for (i = 0; i < n; i++) { |
| 3266 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3267 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3268 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3269 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3270 | assert(n == 1); |
| 3271 | ADDOP(c, IMPORT_STAR); |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
| 3275 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3276 | store_name = alias->name; |
| 3277 | if (alias->asname) |
| 3278 | store_name = alias->asname; |
| 3279 | |
| 3280 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | return 0; |
| 3282 | } |
| 3283 | } |
| 3284 | /* remove imported module */ |
| 3285 | ADDOP(c, POP_TOP); |
| 3286 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | static int |
| 3290 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3292 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3294 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3297 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3298 | { |
| 3299 | if (!compiler_warn(c, "assertion is always true, " |
| 3300 | "perhaps remove parentheses?")) |
| 3301 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3302 | return 0; |
| 3303 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | end = compiler_new_block(c); |
| 3306 | if (end == NULL) |
| 3307 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3308 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3309 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3310 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | if (s->v.Assert.msg) { |
| 3312 | VISIT(c, expr, s->v.Assert.msg); |
| 3313 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3314 | } |
| 3315 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3316 | compiler_use_next_block(c, end); |
| 3317 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3321 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3322 | { |
| 3323 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3324 | VISIT(c, expr, value); |
| 3325 | ADDOP(c, PRINT_EXPR); |
| 3326 | return 1; |
| 3327 | } |
| 3328 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3329 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3330 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3331 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3332 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | VISIT(c, expr, value); |
| 3336 | ADDOP(c, POP_TOP); |
| 3337 | return 1; |
| 3338 | } |
| 3339 | |
| 3340 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3341 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3342 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3343 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3346 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | switch (s->kind) { |
| 3349 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3350 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | case ClassDef_kind: |
| 3352 | return compiler_class(c, s); |
| 3353 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3354 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | case Delete_kind: |
| 3356 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3357 | break; |
| 3358 | case Assign_kind: |
| 3359 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3360 | VISIT(c, expr, s->v.Assign.value); |
| 3361 | for (i = 0; i < n; i++) { |
| 3362 | if (i < n - 1) |
| 3363 | ADDOP(c, DUP_TOP); |
| 3364 | VISIT(c, expr, |
| 3365 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3366 | } |
| 3367 | break; |
| 3368 | case AugAssign_kind: |
| 3369 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3370 | case AnnAssign_kind: |
| 3371 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | case For_kind: |
| 3373 | return compiler_for(c, s); |
| 3374 | case While_kind: |
| 3375 | return compiler_while(c, s); |
| 3376 | case If_kind: |
| 3377 | return compiler_if(c, s); |
| 3378 | case Raise_kind: |
| 3379 | n = 0; |
| 3380 | if (s->v.Raise.exc) { |
| 3381 | VISIT(c, expr, s->v.Raise.exc); |
| 3382 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3383 | if (s->v.Raise.cause) { |
| 3384 | VISIT(c, expr, s->v.Raise.cause); |
| 3385 | n++; |
| 3386 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3387 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3388 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3389 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3391 | case Try_kind: |
| 3392 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | case Assert_kind: |
| 3394 | return compiler_assert(c, s); |
| 3395 | case Import_kind: |
| 3396 | return compiler_import(c, s); |
| 3397 | case ImportFrom_kind: |
| 3398 | return compiler_from_import(c, s); |
| 3399 | case Global_kind: |
| 3400 | case Nonlocal_kind: |
| 3401 | break; |
| 3402 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3403 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3405 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | break; |
| 3407 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3408 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3409 | case Continue_kind: |
| 3410 | return compiler_continue(c); |
| 3411 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3412 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3413 | case AsyncFunctionDef_kind: |
| 3414 | return compiler_function(c, s, 1); |
| 3415 | case AsyncWith_kind: |
| 3416 | return compiler_async_with(c, s, 0); |
| 3417 | case AsyncFor_kind: |
| 3418 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3419 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3421 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | static int |
| 3425 | unaryop(unaryop_ty op) |
| 3426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | switch (op) { |
| 3428 | case Invert: |
| 3429 | return UNARY_INVERT; |
| 3430 | case Not: |
| 3431 | return UNARY_NOT; |
| 3432 | case UAdd: |
| 3433 | return UNARY_POSITIVE; |
| 3434 | case USub: |
| 3435 | return UNARY_NEGATIVE; |
| 3436 | default: |
| 3437 | PyErr_Format(PyExc_SystemError, |
| 3438 | "unary op %d should not be possible", op); |
| 3439 | return 0; |
| 3440 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3444 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | switch (op) { |
| 3447 | case Add: |
| 3448 | return BINARY_ADD; |
| 3449 | case Sub: |
| 3450 | return BINARY_SUBTRACT; |
| 3451 | case Mult: |
| 3452 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3453 | case MatMult: |
| 3454 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | case Div: |
| 3456 | return BINARY_TRUE_DIVIDE; |
| 3457 | case Mod: |
| 3458 | return BINARY_MODULO; |
| 3459 | case Pow: |
| 3460 | return BINARY_POWER; |
| 3461 | case LShift: |
| 3462 | return BINARY_LSHIFT; |
| 3463 | case RShift: |
| 3464 | return BINARY_RSHIFT; |
| 3465 | case BitOr: |
| 3466 | return BINARY_OR; |
| 3467 | case BitXor: |
| 3468 | return BINARY_XOR; |
| 3469 | case BitAnd: |
| 3470 | return BINARY_AND; |
| 3471 | case FloorDiv: |
| 3472 | return BINARY_FLOOR_DIVIDE; |
| 3473 | default: |
| 3474 | PyErr_Format(PyExc_SystemError, |
| 3475 | "binary op %d should not be possible", op); |
| 3476 | return 0; |
| 3477 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3481 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | switch (op) { |
| 3484 | case Add: |
| 3485 | return INPLACE_ADD; |
| 3486 | case Sub: |
| 3487 | return INPLACE_SUBTRACT; |
| 3488 | case Mult: |
| 3489 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3490 | case MatMult: |
| 3491 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | case Div: |
| 3493 | return INPLACE_TRUE_DIVIDE; |
| 3494 | case Mod: |
| 3495 | return INPLACE_MODULO; |
| 3496 | case Pow: |
| 3497 | return INPLACE_POWER; |
| 3498 | case LShift: |
| 3499 | return INPLACE_LSHIFT; |
| 3500 | case RShift: |
| 3501 | return INPLACE_RSHIFT; |
| 3502 | case BitOr: |
| 3503 | return INPLACE_OR; |
| 3504 | case BitXor: |
| 3505 | return INPLACE_XOR; |
| 3506 | case BitAnd: |
| 3507 | return INPLACE_AND; |
| 3508 | case FloorDiv: |
| 3509 | return INPLACE_FLOOR_DIVIDE; |
| 3510 | default: |
| 3511 | PyErr_Format(PyExc_SystemError, |
| 3512 | "inplace binary op %d should not be possible", op); |
| 3513 | return 0; |
| 3514 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | static int |
| 3518 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3519 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3520 | int op, scope; |
| 3521 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | PyObject *dict = c->u->u_names; |
| 3525 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3526 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3527 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3528 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3529 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3530 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3531 | if (forbidden_name(c, name, ctx)) |
| 3532 | return 0; |
| 3533 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3534 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3535 | if (!mangled) |
| 3536 | return 0; |
| 3537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3538 | op = 0; |
| 3539 | optype = OP_NAME; |
| 3540 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3541 | switch (scope) { |
| 3542 | case FREE: |
| 3543 | dict = c->u->u_freevars; |
| 3544 | optype = OP_DEREF; |
| 3545 | break; |
| 3546 | case CELL: |
| 3547 | dict = c->u->u_cellvars; |
| 3548 | optype = OP_DEREF; |
| 3549 | break; |
| 3550 | case LOCAL: |
| 3551 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3552 | optype = OP_FAST; |
| 3553 | break; |
| 3554 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3555 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3556 | optype = OP_GLOBAL; |
| 3557 | break; |
| 3558 | case GLOBAL_EXPLICIT: |
| 3559 | optype = OP_GLOBAL; |
| 3560 | break; |
| 3561 | default: |
| 3562 | /* scope can be 0 */ |
| 3563 | break; |
| 3564 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | /* 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] | 3567 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | switch (optype) { |
| 3570 | case OP_DEREF: |
| 3571 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3572 | case Load: |
| 3573 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3574 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3575 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3576 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3577 | } |
| 3578 | break; |
| 3579 | case OP_FAST: |
| 3580 | switch (ctx) { |
| 3581 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3582 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3584 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3585 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3586 | return 1; |
| 3587 | case OP_GLOBAL: |
| 3588 | switch (ctx) { |
| 3589 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3590 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | } |
| 3593 | break; |
| 3594 | case OP_NAME: |
| 3595 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3596 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3597 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | } |
| 3600 | break; |
| 3601 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3604 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3605 | Py_DECREF(mangled); |
| 3606 | if (arg < 0) |
| 3607 | return 0; |
| 3608 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | static int |
| 3612 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3614 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3615 | int jumpi; |
| 3616 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3617 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3619 | assert(e->kind == BoolOp_kind); |
| 3620 | if (e->v.BoolOp.op == And) |
| 3621 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3622 | else |
| 3623 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3624 | end = compiler_new_block(c); |
| 3625 | if (end == NULL) |
| 3626 | return 0; |
| 3627 | s = e->v.BoolOp.values; |
| 3628 | n = asdl_seq_LEN(s) - 1; |
| 3629 | assert(n >= 0); |
| 3630 | for (i = 0; i < n; ++i) { |
| 3631 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3632 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3633 | basicblock *next = compiler_new_block(c); |
| 3634 | if (next == NULL) { |
| 3635 | return 0; |
| 3636 | } |
| 3637 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | } |
| 3639 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3640 | compiler_use_next_block(c, end); |
| 3641 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3645 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3646 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3647 | { |
| 3648 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3649 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3650 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3651 | PyObject *folded = PyTuple_New(n); |
| 3652 | if (folded == NULL) { |
| 3653 | return 0; |
| 3654 | } |
| 3655 | PyObject *val; |
| 3656 | for (i = 0; i < n; i++) { |
| 3657 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3658 | Py_INCREF(val); |
| 3659 | PyTuple_SET_ITEM(folded, i, val); |
| 3660 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3661 | if (tuple) { |
| 3662 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3663 | } else { |
| 3664 | if (add == SET_ADD) { |
| 3665 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3666 | if (folded == NULL) { |
| 3667 | return 0; |
| 3668 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3669 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3670 | ADDOP_I(c, build, pushed); |
| 3671 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3672 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3673 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3674 | return 1; |
| 3675 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3676 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3677 | for (i = 0; i < n; i++) { |
| 3678 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3679 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3680 | seen_star = 1; |
| 3681 | } |
| 3682 | } |
| 3683 | if (seen_star) { |
| 3684 | seen_star = 0; |
| 3685 | for (i = 0; i < n; i++) { |
| 3686 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3687 | if (elt->kind == Starred_kind) { |
| 3688 | if (seen_star == 0) { |
| 3689 | ADDOP_I(c, build, i+pushed); |
| 3690 | seen_star = 1; |
| 3691 | } |
| 3692 | VISIT(c, expr, elt->v.Starred.value); |
| 3693 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3694 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3695 | else { |
| 3696 | VISIT(c, expr, elt); |
| 3697 | if (seen_star) { |
| 3698 | ADDOP_I(c, add, 1); |
| 3699 | } |
| 3700 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3701 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3702 | assert(seen_star); |
| 3703 | if (tuple) { |
| 3704 | ADDOP(c, LIST_TO_TUPLE); |
| 3705 | } |
| 3706 | } |
| 3707 | else { |
| 3708 | for (i = 0; i < n; i++) { |
| 3709 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3710 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3711 | } |
| 3712 | if (tuple) { |
| 3713 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3714 | } else { |
| 3715 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3716 | } |
| 3717 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3718 | return 1; |
| 3719 | } |
| 3720 | |
| 3721 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3722 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3723 | { |
| 3724 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3725 | Py_ssize_t i; |
| 3726 | int seen_star = 0; |
| 3727 | for (i = 0; i < n; i++) { |
| 3728 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3729 | if (elt->kind == Starred_kind && !seen_star) { |
| 3730 | if ((i >= (1 << 8)) || |
| 3731 | (n-i-1 >= (INT_MAX >> 8))) |
| 3732 | return compiler_error(c, |
| 3733 | "too many expressions in " |
| 3734 | "star-unpacking assignment"); |
| 3735 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3736 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3737 | } |
| 3738 | else if (elt->kind == Starred_kind) { |
| 3739 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3740 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | if (!seen_star) { |
| 3744 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3745 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3746 | for (i = 0; i < n; i++) { |
| 3747 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3748 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3749 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3750 | return 1; |
| 3751 | } |
| 3752 | |
| 3753 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3754 | compiler_list(struct compiler *c, expr_ty e) |
| 3755 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3756 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3757 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3758 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3760 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3761 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3762 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3763 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3764 | else |
| 3765 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3766 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | static int |
| 3770 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3771 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3772 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3773 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3774 | return assignment_helper(c, elts); |
| 3775 | } |
| 3776 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3777 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3778 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3779 | } |
| 3780 | else |
| 3781 | VISIT_SEQ(c, expr, elts); |
| 3782 | return 1; |
| 3783 | } |
| 3784 | |
| 3785 | static int |
| 3786 | compiler_set(struct compiler *c, expr_ty e) |
| 3787 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3788 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3789 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3793 | 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] | 3794 | { |
| 3795 | Py_ssize_t i; |
| 3796 | for (i = begin; i < end; i++) { |
| 3797 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3798 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3799 | return 0; |
| 3800 | } |
| 3801 | return 1; |
| 3802 | } |
| 3803 | |
| 3804 | static int |
| 3805 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3806 | { |
| 3807 | Py_ssize_t i, n = end - begin; |
| 3808 | PyObject *keys, *key; |
| 3809 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3810 | for (i = begin; i < end; i++) { |
| 3811 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3812 | } |
| 3813 | keys = PyTuple_New(n); |
| 3814 | if (keys == NULL) { |
| 3815 | return 0; |
| 3816 | } |
| 3817 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3818 | 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] | 3819 | Py_INCREF(key); |
| 3820 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3821 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3822 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3823 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3824 | } |
| 3825 | else { |
| 3826 | for (i = begin; i < end; i++) { |
| 3827 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3828 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3829 | } |
| 3830 | ADDOP_I(c, BUILD_MAP, n); |
| 3831 | } |
| 3832 | return 1; |
| 3833 | } |
| 3834 | |
| 3835 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3836 | compiler_dict(struct compiler *c, expr_ty e) |
| 3837 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3838 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3839 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3840 | int is_unpacking = 0; |
| 3841 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3842 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3843 | elements = 0; |
| 3844 | for (i = 0; i < n; i++) { |
| 3845 | 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] | 3846 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3847 | if (elements) { |
| 3848 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3849 | return 0; |
| 3850 | } |
| 3851 | if (have_dict) { |
| 3852 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3853 | } |
| 3854 | have_dict = 1; |
| 3855 | elements = 0; |
| 3856 | } |
| 3857 | if (have_dict == 0) { |
| 3858 | ADDOP_I(c, BUILD_MAP, 0); |
| 3859 | have_dict = 1; |
| 3860 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3861 | 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] | 3862 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3863 | } |
| 3864 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3865 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3866 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3867 | return 0; |
| 3868 | } |
| 3869 | if (have_dict) { |
| 3870 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3871 | } |
| 3872 | have_dict = 1; |
| 3873 | elements = 0; |
| 3874 | } |
| 3875 | else { |
| 3876 | elements++; |
| 3877 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | } |
| 3879 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3880 | if (elements) { |
| 3881 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3882 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3883 | } |
| 3884 | if (have_dict) { |
| 3885 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3886 | } |
| 3887 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3888 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3889 | if (!have_dict) { |
| 3890 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | } |
| 3892 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | static int |
| 3896 | compiler_compare(struct compiler *c, expr_ty e) |
| 3897 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3898 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3899 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3900 | if (!check_compare(c, e)) { |
| 3901 | return 0; |
| 3902 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3903 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3904 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3905 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3906 | if (n == 0) { |
| 3907 | 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] | 3908 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3909 | } |
| 3910 | else { |
| 3911 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | if (cleanup == NULL) |
| 3913 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3914 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3915 | VISIT(c, expr, |
| 3916 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3917 | ADDOP(c, DUP_TOP); |
| 3918 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3919 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3920 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3921 | NEXT_BLOCK(c); |
| 3922 | } |
| 3923 | 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] | 3924 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 | basicblock *end = compiler_new_block(c); |
| 3926 | if (end == NULL) |
| 3927 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3928 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3929 | compiler_use_next_block(c, cleanup); |
| 3930 | ADDOP(c, ROT_TWO); |
| 3931 | ADDOP(c, POP_TOP); |
| 3932 | compiler_use_next_block(c, end); |
| 3933 | } |
| 3934 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3937 | static PyTypeObject * |
| 3938 | infer_type(expr_ty e) |
| 3939 | { |
| 3940 | switch (e->kind) { |
| 3941 | case Tuple_kind: |
| 3942 | return &PyTuple_Type; |
| 3943 | case List_kind: |
| 3944 | case ListComp_kind: |
| 3945 | return &PyList_Type; |
| 3946 | case Dict_kind: |
| 3947 | case DictComp_kind: |
| 3948 | return &PyDict_Type; |
| 3949 | case Set_kind: |
| 3950 | case SetComp_kind: |
| 3951 | return &PySet_Type; |
| 3952 | case GeneratorExp_kind: |
| 3953 | return &PyGen_Type; |
| 3954 | case Lambda_kind: |
| 3955 | return &PyFunction_Type; |
| 3956 | case JoinedStr_kind: |
| 3957 | case FormattedValue_kind: |
| 3958 | return &PyUnicode_Type; |
| 3959 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3960 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3961 | default: |
| 3962 | return NULL; |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | static int |
| 3967 | check_caller(struct compiler *c, expr_ty e) |
| 3968 | { |
| 3969 | switch (e->kind) { |
| 3970 | case Constant_kind: |
| 3971 | case Tuple_kind: |
| 3972 | case List_kind: |
| 3973 | case ListComp_kind: |
| 3974 | case Dict_kind: |
| 3975 | case DictComp_kind: |
| 3976 | case Set_kind: |
| 3977 | case SetComp_kind: |
| 3978 | case GeneratorExp_kind: |
| 3979 | case JoinedStr_kind: |
| 3980 | case FormattedValue_kind: |
| 3981 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3982 | "perhaps you missed a comma?", |
| 3983 | infer_type(e)->tp_name); |
| 3984 | default: |
| 3985 | return 1; |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | static int |
| 3990 | check_subscripter(struct compiler *c, expr_ty e) |
| 3991 | { |
| 3992 | PyObject *v; |
| 3993 | |
| 3994 | switch (e->kind) { |
| 3995 | case Constant_kind: |
| 3996 | v = e->v.Constant.value; |
| 3997 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3998 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3999 | PyAnySet_Check(v))) |
| 4000 | { |
| 4001 | return 1; |
| 4002 | } |
| 4003 | /* fall through */ |
| 4004 | case Set_kind: |
| 4005 | case SetComp_kind: |
| 4006 | case GeneratorExp_kind: |
| 4007 | case Lambda_kind: |
| 4008 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4009 | "perhaps you missed a comma?", |
| 4010 | infer_type(e)->tp_name); |
| 4011 | default: |
| 4012 | return 1; |
| 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4017 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4018 | { |
| 4019 | PyObject *v; |
| 4020 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4021 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4022 | if (index_type == NULL |
| 4023 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4024 | || index_type == &PySlice_Type) { |
| 4025 | return 1; |
| 4026 | } |
| 4027 | |
| 4028 | switch (e->kind) { |
| 4029 | case Constant_kind: |
| 4030 | v = e->v.Constant.value; |
| 4031 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4032 | return 1; |
| 4033 | } |
| 4034 | /* fall through */ |
| 4035 | case Tuple_kind: |
| 4036 | case List_kind: |
| 4037 | case ListComp_kind: |
| 4038 | case JoinedStr_kind: |
| 4039 | case FormattedValue_kind: |
| 4040 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4041 | "not %.200s; " |
| 4042 | "perhaps you missed a comma?", |
| 4043 | infer_type(e)->tp_name, |
| 4044 | index_type->tp_name); |
| 4045 | default: |
| 4046 | return 1; |
| 4047 | } |
| 4048 | } |
| 4049 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4050 | // 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] | 4051 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4052 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4053 | { |
| 4054 | Py_ssize_t argsl, i; |
| 4055 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4056 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4057 | |
| 4058 | /* Check that the call node is an attribute access, and that |
| 4059 | the call doesn't have keyword parameters. */ |
| 4060 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4061 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4062 | return -1; |
| 4063 | |
| 4064 | /* Check that there are no *varargs types of arguments. */ |
| 4065 | argsl = asdl_seq_LEN(args); |
| 4066 | for (i = 0; i < argsl; i++) { |
| 4067 | expr_ty elt = asdl_seq_GET(args, i); |
| 4068 | if (elt->kind == Starred_kind) { |
| 4069 | return -1; |
| 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | /* Alright, we can optimize the code. */ |
| 4074 | VISIT(c, expr, meth->v.Attribute.value); |
| 4075 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4076 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4077 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4078 | return 1; |
| 4079 | } |
| 4080 | |
| 4081 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4082 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4083 | { |
| 4084 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4085 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4086 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4087 | if (key->arg == NULL) { |
| 4088 | continue; |
| 4089 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4090 | if (forbidden_name(c, key->arg, Store)) { |
| 4091 | return -1; |
| 4092 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4093 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4094 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4095 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
| 4096 | PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg); |
| 4097 | if (msg == NULL) { |
| 4098 | return -1; |
| 4099 | } |
| 4100 | c->u->u_col_offset = other->col_offset; |
| 4101 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
| 4102 | Py_DECREF(msg); |
| 4103 | return -1; |
| 4104 | } |
| 4105 | } |
| 4106 | } |
| 4107 | return 0; |
| 4108 | } |
| 4109 | |
| 4110 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4111 | compiler_call(struct compiler *c, expr_ty e) |
| 4112 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4113 | int ret = maybe_optimize_method_call(c, e); |
| 4114 | if (ret >= 0) { |
| 4115 | return ret; |
| 4116 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4117 | if (!check_caller(c, e->v.Call.func)) { |
| 4118 | return 0; |
| 4119 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | VISIT(c, expr, e->v.Call.func); |
| 4121 | return compiler_call_helper(c, 0, |
| 4122 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4123 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4126 | static int |
| 4127 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4128 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4129 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4130 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4131 | 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] | 4132 | return 1; |
| 4133 | } |
| 4134 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4135 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4136 | static int |
| 4137 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4138 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4139 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4140 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4141 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4142 | Convert the conversion char to 3 bits: |
| 4143 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4144 | !s : 001 0x1 FVC_STR |
| 4145 | !r : 010 0x2 FVC_REPR |
| 4146 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4147 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4148 | next bit is whether or not we have a format spec: |
| 4149 | yes : 100 0x4 |
| 4150 | no : 000 0x0 |
| 4151 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4152 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4153 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4154 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4155 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4156 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4157 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4158 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4159 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4160 | case 's': oparg = FVC_STR; break; |
| 4161 | case 'r': oparg = FVC_REPR; break; |
| 4162 | case 'a': oparg = FVC_ASCII; break; |
| 4163 | case -1: oparg = FVC_NONE; break; |
| 4164 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4165 | PyErr_Format(PyExc_SystemError, |
| 4166 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4167 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4168 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4169 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4170 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4171 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4172 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4173 | } |
| 4174 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4175 | /* And push our opcode and oparg */ |
| 4176 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4177 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4178 | return 1; |
| 4179 | } |
| 4180 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4181 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4182 | 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] | 4183 | { |
| 4184 | Py_ssize_t i, n = end - begin; |
| 4185 | keyword_ty kw; |
| 4186 | PyObject *keys, *key; |
| 4187 | assert(n > 0); |
| 4188 | if (n > 1) { |
| 4189 | for (i = begin; i < end; i++) { |
| 4190 | kw = asdl_seq_GET(keywords, i); |
| 4191 | VISIT(c, expr, kw->value); |
| 4192 | } |
| 4193 | keys = PyTuple_New(n); |
| 4194 | if (keys == NULL) { |
| 4195 | return 0; |
| 4196 | } |
| 4197 | for (i = begin; i < end; i++) { |
| 4198 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4199 | Py_INCREF(key); |
| 4200 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4201 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4202 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4203 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4204 | } |
| 4205 | else { |
| 4206 | /* a for loop only executes once */ |
| 4207 | for (i = begin; i < end; i++) { |
| 4208 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4209 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4210 | VISIT(c, expr, kw->value); |
| 4211 | } |
| 4212 | ADDOP_I(c, BUILD_MAP, n); |
| 4213 | } |
| 4214 | return 1; |
| 4215 | } |
| 4216 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4217 | /* shared code between compiler_call and compiler_class */ |
| 4218 | static int |
| 4219 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4220 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4221 | asdl_expr_seq *args, |
| 4222 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4223 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4224 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4225 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4226 | if (validate_keywords(c, keywords) == -1) { |
| 4227 | return 0; |
| 4228 | } |
| 4229 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4230 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4231 | nkwelts = asdl_seq_LEN(keywords); |
| 4232 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4233 | for (i = 0; i < nelts; i++) { |
| 4234 | expr_ty elt = asdl_seq_GET(args, i); |
| 4235 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4236 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4237 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4238 | } |
| 4239 | for (i = 0; i < nkwelts; i++) { |
| 4240 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4241 | if (kw->arg == NULL) { |
| 4242 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4243 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4244 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4245 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4246 | /* No * or ** args, so can use faster calling sequence */ |
| 4247 | for (i = 0; i < nelts; i++) { |
| 4248 | expr_ty elt = asdl_seq_GET(args, i); |
| 4249 | assert(elt->kind != Starred_kind); |
| 4250 | VISIT(c, expr, elt); |
| 4251 | } |
| 4252 | if (nkwelts) { |
| 4253 | PyObject *names; |
| 4254 | VISIT_SEQ(c, keyword, keywords); |
| 4255 | names = PyTuple_New(nkwelts); |
| 4256 | if (names == NULL) { |
| 4257 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4258 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4259 | for (i = 0; i < nkwelts; i++) { |
| 4260 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4261 | Py_INCREF(kw->arg); |
| 4262 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4263 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4264 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4265 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4266 | return 1; |
| 4267 | } |
| 4268 | else { |
| 4269 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4270 | return 1; |
| 4271 | } |
| 4272 | |
| 4273 | ex_call: |
| 4274 | |
| 4275 | /* Do positional arguments. */ |
| 4276 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4277 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4278 | } |
| 4279 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4280 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4281 | return 0; |
| 4282 | } |
| 4283 | /* Then keyword arguments */ |
| 4284 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4285 | /* Has a new dict been pushed */ |
| 4286 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4287 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4288 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4289 | for (i = 0; i < nkwelts; i++) { |
| 4290 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4291 | if (kw->arg == NULL) { |
| 4292 | /* A keyword argument unpacking. */ |
| 4293 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4294 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4295 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4296 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4297 | if (have_dict) { |
| 4298 | ADDOP_I(c, DICT_MERGE, 1); |
| 4299 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4300 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4301 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4302 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4303 | if (!have_dict) { |
| 4304 | ADDOP_I(c, BUILD_MAP, 0); |
| 4305 | have_dict = 1; |
| 4306 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4307 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4308 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4309 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4310 | else { |
| 4311 | nseen++; |
| 4312 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4313 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4314 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4315 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4316 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4317 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4318 | } |
| 4319 | if (have_dict) { |
| 4320 | ADDOP_I(c, DICT_MERGE, 1); |
| 4321 | } |
| 4322 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4323 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4324 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4326 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4327 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4330 | |
| 4331 | /* List and set comprehensions and generator expressions work by creating a |
| 4332 | nested function to perform the actual iteration. This means that the |
| 4333 | iteration variables don't leak into the current scope. |
| 4334 | The defined function is called immediately following its definition, with the |
| 4335 | result of that call being the result of the expression. |
| 4336 | The LC/SC version returns the populated container, while the GE version is |
| 4337 | flagged in symtable.c as a generator, so it returns the generator object |
| 4338 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4339 | |
| 4340 | Possible cleanups: |
| 4341 | - iterate over the generator sequence instead of using recursion |
| 4342 | */ |
| 4343 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4344 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4345 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4346 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4347 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4348 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4349 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4350 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4351 | comprehension_ty gen; |
| 4352 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4353 | if (gen->is_async) { |
| 4354 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4355 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4356 | } else { |
| 4357 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4358 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | static int |
| 4363 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4364 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4365 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4366 | expr_ty elt, expr_ty val, int type) |
| 4367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | /* generate code for the iterator, then each of the ifs, |
| 4369 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4371 | comprehension_ty gen; |
| 4372 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4373 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | start = compiler_new_block(c); |
| 4376 | skip = compiler_new_block(c); |
| 4377 | if_cleanup = compiler_new_block(c); |
| 4378 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4380 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4381 | anchor == NULL) |
| 4382 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4384 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4386 | if (gen_index == 0) { |
| 4387 | /* Receive outermost iter as an implicit argument */ |
| 4388 | c->u->u_argcount = 1; |
| 4389 | ADDOP_I(c, LOAD_FAST, 0); |
| 4390 | } |
| 4391 | else { |
| 4392 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4393 | /* Fast path for the temporary variable assignment idiom: |
| 4394 | for y in [f(x)] |
| 4395 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4396 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4397 | switch (gen->iter->kind) { |
| 4398 | case List_kind: |
| 4399 | elts = gen->iter->v.List.elts; |
| 4400 | break; |
| 4401 | case Tuple_kind: |
| 4402 | elts = gen->iter->v.Tuple.elts; |
| 4403 | break; |
| 4404 | default: |
| 4405 | elts = NULL; |
| 4406 | } |
| 4407 | if (asdl_seq_LEN(elts) == 1) { |
| 4408 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4409 | if (elt->kind != Starred_kind) { |
| 4410 | VISIT(c, expr, elt); |
| 4411 | start = NULL; |
| 4412 | } |
| 4413 | } |
| 4414 | if (start) { |
| 4415 | VISIT(c, expr, gen->iter); |
| 4416 | ADDOP(c, GET_ITER); |
| 4417 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4418 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4419 | if (start) { |
| 4420 | depth++; |
| 4421 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4422 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4423 | NEXT_BLOCK(c); |
| 4424 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | /* XXX this needs to be cleaned up...a lot! */ |
| 4428 | n = asdl_seq_LEN(gen->ifs); |
| 4429 | for (i = 0; i < n; i++) { |
| 4430 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4431 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4432 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | NEXT_BLOCK(c); |
| 4434 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4437 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4438 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4439 | elt, val, type)) |
| 4440 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | /* only append after the last for generator */ |
| 4443 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4444 | /* comprehension specific code */ |
| 4445 | switch (type) { |
| 4446 | case COMP_GENEXP: |
| 4447 | VISIT(c, expr, elt); |
| 4448 | ADDOP(c, YIELD_VALUE); |
| 4449 | ADDOP(c, POP_TOP); |
| 4450 | break; |
| 4451 | case COMP_LISTCOMP: |
| 4452 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4453 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | break; |
| 4455 | case COMP_SETCOMP: |
| 4456 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4457 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4458 | break; |
| 4459 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4460 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4463 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4464 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | break; |
| 4466 | default: |
| 4467 | return 0; |
| 4468 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4470 | compiler_use_next_block(c, skip); |
| 4471 | } |
| 4472 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4473 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4474 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4475 | compiler_use_next_block(c, anchor); |
| 4476 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | |
| 4478 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4482 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4483 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4484 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4485 | expr_ty elt, expr_ty val, int type) |
| 4486 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4487 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4488 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4489 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4490 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4491 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4492 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4493 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4494 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4495 | return 0; |
| 4496 | } |
| 4497 | |
| 4498 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4499 | |
| 4500 | if (gen_index == 0) { |
| 4501 | /* Receive outermost iter as an implicit argument */ |
| 4502 | c->u->u_argcount = 1; |
| 4503 | ADDOP_I(c, LOAD_FAST, 0); |
| 4504 | } |
| 4505 | else { |
| 4506 | /* Sub-iter - calculate on the fly */ |
| 4507 | VISIT(c, expr, gen->iter); |
| 4508 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4509 | } |
| 4510 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4511 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4512 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4513 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4514 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4515 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4516 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4517 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4518 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4519 | |
| 4520 | n = asdl_seq_LEN(gen->ifs); |
| 4521 | for (i = 0; i < n; i++) { |
| 4522 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4523 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4524 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4525 | NEXT_BLOCK(c); |
| 4526 | } |
| 4527 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4528 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4529 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4530 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4531 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4532 | elt, val, type)) |
| 4533 | return 0; |
| 4534 | |
| 4535 | /* only append after the last for generator */ |
| 4536 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4537 | /* comprehension specific code */ |
| 4538 | switch (type) { |
| 4539 | case COMP_GENEXP: |
| 4540 | VISIT(c, expr, elt); |
| 4541 | ADDOP(c, YIELD_VALUE); |
| 4542 | ADDOP(c, POP_TOP); |
| 4543 | break; |
| 4544 | case COMP_LISTCOMP: |
| 4545 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4546 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4547 | break; |
| 4548 | case COMP_SETCOMP: |
| 4549 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4550 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4551 | break; |
| 4552 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4553 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4554 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4555 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4556 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4557 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4558 | break; |
| 4559 | default: |
| 4560 | return 0; |
| 4561 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4562 | } |
| 4563 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4564 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4565 | |
| 4566 | compiler_use_next_block(c, except); |
| 4567 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4568 | |
| 4569 | return 1; |
| 4570 | } |
| 4571 | |
| 4572 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4573 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4574 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4575 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4578 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4579 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4580 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4581 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4582 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4583 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4584 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4585 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4586 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4587 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4588 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4594 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4595 | 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] | 4596 | compiler_error(c, "asynchronous comprehension outside of " |
| 4597 | "an asynchronous function"); |
| 4598 | goto error_in_scope; |
| 4599 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | if (type != COMP_GENEXP) { |
| 4602 | int op; |
| 4603 | switch (type) { |
| 4604 | case COMP_LISTCOMP: |
| 4605 | op = BUILD_LIST; |
| 4606 | break; |
| 4607 | case COMP_SETCOMP: |
| 4608 | op = BUILD_SET; |
| 4609 | break; |
| 4610 | case COMP_DICTCOMP: |
| 4611 | op = BUILD_MAP; |
| 4612 | break; |
| 4613 | default: |
| 4614 | PyErr_Format(PyExc_SystemError, |
| 4615 | "unknown comprehension type %d", type); |
| 4616 | goto error_in_scope; |
| 4617 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4619 | ADDOP_I(c, op, 0); |
| 4620 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4621 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4622 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4623 | val, type)) |
| 4624 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | if (type != COMP_GENEXP) { |
| 4627 | ADDOP(c, RETURN_VALUE); |
| 4628 | } |
| 4629 | |
| 4630 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4631 | qualname = c->u->u_qualname; |
| 4632 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4633 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4634 | if (top_level_await && is_async_generator){ |
| 4635 | c->u->u_ste->ste_coroutine = 1; |
| 4636 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4637 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | goto error; |
| 4639 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4640 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4642 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 | Py_DECREF(co); |
| 4644 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4645 | VISIT(c, expr, outermost->iter); |
| 4646 | |
| 4647 | if (outermost->is_async) { |
| 4648 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4649 | } else { |
| 4650 | ADDOP(c, GET_ITER); |
| 4651 | } |
| 4652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4654 | |
| 4655 | if (is_async_generator && type != COMP_GENEXP) { |
| 4656 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4657 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4658 | ADDOP(c, YIELD_FROM); |
| 4659 | } |
| 4660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4661 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4662 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4664 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4665 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | Py_XDECREF(co); |
| 4667 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4671 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | static identifier name; |
| 4674 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4675 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | if (!name) |
| 4677 | return 0; |
| 4678 | } |
| 4679 | assert(e->kind == GeneratorExp_kind); |
| 4680 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4681 | e->v.GeneratorExp.generators, |
| 4682 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4686 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | static identifier name; |
| 4689 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4690 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4691 | if (!name) |
| 4692 | return 0; |
| 4693 | } |
| 4694 | assert(e->kind == ListComp_kind); |
| 4695 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4696 | e->v.ListComp.generators, |
| 4697 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | static int |
| 4701 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | static identifier name; |
| 4704 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4705 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | if (!name) |
| 4707 | return 0; |
| 4708 | } |
| 4709 | assert(e->kind == SetComp_kind); |
| 4710 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4711 | e->v.SetComp.generators, |
| 4712 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | |
| 4716 | static int |
| 4717 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4719 | static identifier name; |
| 4720 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4721 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4722 | if (!name) |
| 4723 | return 0; |
| 4724 | } |
| 4725 | assert(e->kind == DictComp_kind); |
| 4726 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4727 | e->v.DictComp.generators, |
| 4728 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | |
| 4732 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4733 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | VISIT(c, expr, k->value); |
| 4736 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4740 | whether they are true or false. |
| 4741 | |
| 4742 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4743 | */ |
| 4744 | |
| 4745 | static int |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4746 | compiler_with_except_finish(struct compiler *c) { |
| 4747 | basicblock *exit; |
| 4748 | exit = compiler_new_block(c); |
| 4749 | if (exit == NULL) |
| 4750 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4751 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4752 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4753 | ADDOP(c, RERAISE); |
| 4754 | compiler_use_next_block(c, exit); |
| 4755 | ADDOP(c, POP_TOP); |
| 4756 | ADDOP(c, POP_TOP); |
| 4757 | ADDOP(c, POP_TOP); |
| 4758 | ADDOP(c, POP_EXCEPT); |
| 4759 | ADDOP(c, POP_TOP); |
| 4760 | return 1; |
| 4761 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4762 | |
| 4763 | /* |
| 4764 | Implements the async with statement. |
| 4765 | |
| 4766 | The semantics outlined in that PEP are as follows: |
| 4767 | |
| 4768 | async with EXPR as VAR: |
| 4769 | BLOCK |
| 4770 | |
| 4771 | It is implemented roughly as: |
| 4772 | |
| 4773 | context = EXPR |
| 4774 | exit = context.__aexit__ # not calling it |
| 4775 | value = await context.__aenter__() |
| 4776 | try: |
| 4777 | VAR = value # if VAR present in the syntax |
| 4778 | BLOCK |
| 4779 | finally: |
| 4780 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4781 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4782 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4783 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4784 | if not (await exit(*exc)): |
| 4785 | raise |
| 4786 | */ |
| 4787 | static int |
| 4788 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4789 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4790 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4791 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4792 | |
| 4793 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4794 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4795 | c->u->u_ste->ste_coroutine = 1; |
| 4796 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4797 | return compiler_error(c, "'async with' outside async function"); |
| 4798 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4799 | |
| 4800 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4801 | final = compiler_new_block(c); |
| 4802 | exit = compiler_new_block(c); |
| 4803 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4804 | return 0; |
| 4805 | |
| 4806 | /* Evaluate EXPR */ |
| 4807 | VISIT(c, expr, item->context_expr); |
| 4808 | |
| 4809 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4810 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4811 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4812 | ADDOP(c, YIELD_FROM); |
| 4813 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4814 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4815 | |
| 4816 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4817 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4818 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | if (item->optional_vars) { |
| 4823 | VISIT(c, expr, item->optional_vars); |
| 4824 | } |
| 4825 | else { |
| 4826 | /* Discard result from context.__aenter__() */ |
| 4827 | ADDOP(c, POP_TOP); |
| 4828 | } |
| 4829 | |
| 4830 | pos++; |
| 4831 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4832 | /* BLOCK code */ |
| 4833 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4834 | else if (!compiler_async_with(c, s, pos)) |
| 4835 | return 0; |
| 4836 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4837 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4838 | ADDOP(c, POP_BLOCK); |
| 4839 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4840 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4841 | /* For successful outcome: |
| 4842 | * call __exit__(None, None, None) |
| 4843 | */ |
| 4844 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4845 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4846 | ADDOP(c, GET_AWAITABLE); |
| 4847 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4848 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4849 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4850 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4851 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4852 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4853 | |
| 4854 | /* For exceptional outcome: */ |
| 4855 | compiler_use_next_block(c, final); |
| 4856 | |
| 4857 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4858 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4859 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4860 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4861 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4862 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4863 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4864 | return 1; |
| 4865 | } |
| 4866 | |
| 4867 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4868 | /* |
| 4869 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4870 | with EXPR as VAR: |
| 4871 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4872 | is implemented as: |
| 4873 | <code for EXPR> |
| 4874 | SETUP_WITH E |
| 4875 | <code to store to VAR> or POP_TOP |
| 4876 | <code for BLOCK> |
| 4877 | LOAD_CONST (None, None, None) |
| 4878 | CALL_FUNCTION_EX 0 |
| 4879 | JUMP_FORWARD EXIT |
| 4880 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4881 | POP_JUMP_IF_TRUE T: |
| 4882 | RERAISE |
| 4883 | T: POP_TOP * 3 (remove exception from stack) |
| 4884 | POP_EXCEPT |
| 4885 | POP_TOP |
| 4886 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4887 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4888 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4889 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4890 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4891 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4892 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4893 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4894 | |
| 4895 | assert(s->kind == With_kind); |
| 4896 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4897 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4898 | final = compiler_new_block(c); |
| 4899 | exit = compiler_new_block(c); |
| 4900 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4901 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4902 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4903 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4904 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4905 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4906 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4907 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4908 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4909 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4910 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4911 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4914 | if (item->optional_vars) { |
| 4915 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4916 | } |
| 4917 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4918 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4919 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4922 | pos++; |
| 4923 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4924 | /* BLOCK code */ |
| 4925 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4926 | else if (!compiler_with(c, s, pos)) |
| 4927 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4928 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4929 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4930 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4931 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4932 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4933 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4934 | /* For successful outcome: |
| 4935 | * call __exit__(None, None, None) |
| 4936 | */ |
| 4937 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4938 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4939 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4940 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4941 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4942 | /* For exceptional outcome: */ |
| 4943 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4944 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4945 | ADDOP(c, WITH_EXCEPT_START); |
| 4946 | compiler_with_except_finish(c); |
| 4947 | |
| 4948 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4949 | return 1; |
| 4950 | } |
| 4951 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4952 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4953 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4955 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4956 | case NamedExpr_kind: |
| 4957 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4958 | ADDOP(c, DUP_TOP); |
| 4959 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4960 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4961 | case BoolOp_kind: |
| 4962 | return compiler_boolop(c, e); |
| 4963 | case BinOp_kind: |
| 4964 | VISIT(c, expr, e->v.BinOp.left); |
| 4965 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 4966 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4967 | break; |
| 4968 | case UnaryOp_kind: |
| 4969 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4970 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4971 | break; |
| 4972 | case Lambda_kind: |
| 4973 | return compiler_lambda(c, e); |
| 4974 | case IfExp_kind: |
| 4975 | return compiler_ifexp(c, e); |
| 4976 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4977 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4978 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4979 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4980 | case GeneratorExp_kind: |
| 4981 | return compiler_genexp(c, e); |
| 4982 | case ListComp_kind: |
| 4983 | return compiler_listcomp(c, e); |
| 4984 | case SetComp_kind: |
| 4985 | return compiler_setcomp(c, e); |
| 4986 | case DictComp_kind: |
| 4987 | return compiler_dictcomp(c, e); |
| 4988 | case Yield_kind: |
| 4989 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4990 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4991 | if (e->v.Yield.value) { |
| 4992 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4993 | } |
| 4994 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4995 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4997 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4999 | case YieldFrom_kind: |
| 5000 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5001 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5002 | |
| 5003 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5004 | return compiler_error(c, "'yield from' inside async function"); |
| 5005 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5006 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5007 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5008 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5009 | ADDOP(c, YIELD_FROM); |
| 5010 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5011 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5012 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5013 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5014 | return compiler_error(c, "'await' outside function"); |
| 5015 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5016 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5017 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5018 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5019 | return compiler_error(c, "'await' outside async function"); |
| 5020 | } |
| 5021 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5022 | |
| 5023 | VISIT(c, expr, e->v.Await.value); |
| 5024 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5025 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5026 | ADDOP(c, YIELD_FROM); |
| 5027 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | case Compare_kind: |
| 5029 | return compiler_compare(c, e); |
| 5030 | case Call_kind: |
| 5031 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5032 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5033 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5034 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5035 | case JoinedStr_kind: |
| 5036 | return compiler_joined_str(c, e); |
| 5037 | case FormattedValue_kind: |
| 5038 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5039 | /* The following exprs can be assignment targets. */ |
| 5040 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5041 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5043 | case Load: |
| 5044 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5045 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5046 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5047 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5048 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5049 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5050 | break; |
| 5051 | case Del: |
| 5052 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5053 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5054 | } |
| 5055 | break; |
| 5056 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5057 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5058 | case Starred_kind: |
| 5059 | switch (e->v.Starred.ctx) { |
| 5060 | case Store: |
| 5061 | /* In all legitimate cases, the Starred node was already replaced |
| 5062 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5063 | return compiler_error(c, |
| 5064 | "starred assignment target must be in a list or tuple"); |
| 5065 | default: |
| 5066 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5067 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5068 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5069 | break; |
| 5070 | case Slice_kind: |
| 5071 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5072 | case Name_kind: |
| 5073 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5074 | /* child nodes of List and Tuple will have expr_context set */ |
| 5075 | case List_kind: |
| 5076 | return compiler_list(c, e); |
| 5077 | case Tuple_kind: |
| 5078 | return compiler_tuple(c, e); |
| 5079 | } |
| 5080 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5084 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5085 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5086 | int old_lineno = c->u->u_lineno; |
| 5087 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5088 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5089 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5090 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5091 | c->u->u_col_offset = old_col_offset; |
| 5092 | return res; |
| 5093 | } |
| 5094 | |
| 5095 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5096 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5097 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5098 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5099 | expr_ty e = s->v.AugAssign.target; |
| 5100 | |
| 5101 | int old_lineno = c->u->u_lineno; |
| 5102 | int old_col_offset = c->u->u_col_offset; |
| 5103 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | switch (e->kind) { |
| 5106 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5107 | VISIT(c, expr, e->v.Attribute.value); |
| 5108 | ADDOP(c, DUP_TOP); |
| 5109 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5110 | break; |
| 5111 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5112 | VISIT(c, expr, e->v.Subscript.value); |
| 5113 | VISIT(c, expr, e->v.Subscript.slice); |
| 5114 | ADDOP(c, DUP_TOP_TWO); |
| 5115 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | break; |
| 5117 | case Name_kind: |
| 5118 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5119 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5120 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5121 | default: |
| 5122 | PyErr_Format(PyExc_SystemError, |
| 5123 | "invalid node type (%d) for augmented assignment", |
| 5124 | e->kind); |
| 5125 | return 0; |
| 5126 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5127 | |
| 5128 | c->u->u_lineno = old_lineno; |
| 5129 | c->u->u_col_offset = old_col_offset; |
| 5130 | |
| 5131 | VISIT(c, expr, s->v.AugAssign.value); |
| 5132 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5133 | |
| 5134 | SET_LOC(c, e); |
| 5135 | |
| 5136 | switch (e->kind) { |
| 5137 | case Attribute_kind: |
| 5138 | ADDOP(c, ROT_TWO); |
| 5139 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5140 | break; |
| 5141 | case Subscript_kind: |
| 5142 | ADDOP(c, ROT_THREE); |
| 5143 | ADDOP(c, STORE_SUBSCR); |
| 5144 | break; |
| 5145 | case Name_kind: |
| 5146 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5147 | default: |
| 5148 | Py_UNREACHABLE(); |
| 5149 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5150 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5154 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5155 | { |
| 5156 | VISIT(c, expr, e); |
| 5157 | ADDOP(c, POP_TOP); |
| 5158 | return 1; |
| 5159 | } |
| 5160 | |
| 5161 | static int |
| 5162 | check_annotation(struct compiler *c, stmt_ty s) |
| 5163 | { |
| 5164 | /* Annotations are only evaluated in a module or class. */ |
| 5165 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5166 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5167 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5168 | } |
| 5169 | return 1; |
| 5170 | } |
| 5171 | |
| 5172 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5173 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5174 | { |
| 5175 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5176 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5177 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5178 | 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] | 5179 | return 0; |
| 5180 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5181 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5182 | return 0; |
| 5183 | } |
| 5184 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5185 | return 0; |
| 5186 | } |
| 5187 | return 1; |
| 5188 | case Tuple_kind: { |
| 5189 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5190 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5191 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5192 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5193 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5194 | return 0; |
| 5195 | } |
| 5196 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5197 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5198 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5199 | default: |
| 5200 | return check_ann_expr(c, e); |
| 5201 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5202 | } |
| 5203 | |
| 5204 | static int |
| 5205 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5206 | { |
| 5207 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5208 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5209 | |
| 5210 | assert(s->kind == AnnAssign_kind); |
| 5211 | |
| 5212 | /* We perform the actual assignment first. */ |
| 5213 | if (s->v.AnnAssign.value) { |
| 5214 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5215 | VISIT(c, expr, targ); |
| 5216 | } |
| 5217 | switch (targ->kind) { |
| 5218 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5219 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5220 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5221 | /* If we have a simple name in a module or class, store annotation. */ |
| 5222 | if (s->v.AnnAssign.simple && |
| 5223 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5224 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5225 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5226 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5227 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5228 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5229 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5230 | } |
| 5231 | break; |
| 5232 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5233 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5234 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5235 | if (!s->v.AnnAssign.value && |
| 5236 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5237 | return 0; |
| 5238 | } |
| 5239 | break; |
| 5240 | case Subscript_kind: |
| 5241 | if (!s->v.AnnAssign.value && |
| 5242 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5243 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5244 | return 0; |
| 5245 | } |
| 5246 | break; |
| 5247 | default: |
| 5248 | PyErr_Format(PyExc_SystemError, |
| 5249 | "invalid node type (%d) for annotated assignment", |
| 5250 | targ->kind); |
| 5251 | return 0; |
| 5252 | } |
| 5253 | /* Annotation is evaluated last. */ |
| 5254 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5255 | return 0; |
| 5256 | } |
| 5257 | return 1; |
| 5258 | } |
| 5259 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5260 | /* Raises a SyntaxError and returns 0. |
| 5261 | If something goes wrong, a different exception may be raised. |
| 5262 | */ |
| 5263 | |
| 5264 | static int |
| 5265 | compiler_error(struct compiler *c, const char *errstr) |
| 5266 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5267 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5269 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5270 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | if (!loc) { |
| 5272 | Py_INCREF(Py_None); |
| 5273 | loc = Py_None; |
| 5274 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5275 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5276 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5277 | if (!u) |
| 5278 | goto exit; |
| 5279 | v = Py_BuildValue("(zO)", errstr, u); |
| 5280 | if (!v) |
| 5281 | goto exit; |
| 5282 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5283 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5284 | Py_DECREF(loc); |
| 5285 | Py_XDECREF(u); |
| 5286 | Py_XDECREF(v); |
| 5287 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5290 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5291 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5292 | and returns 0. |
| 5293 | */ |
| 5294 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5295 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5296 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5297 | va_list vargs; |
| 5298 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5299 | va_start(vargs, format); |
| 5300 | #else |
| 5301 | va_start(vargs); |
| 5302 | #endif |
| 5303 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5304 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5305 | if (msg == NULL) { |
| 5306 | return 0; |
| 5307 | } |
| 5308 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5309 | c->u->u_lineno, NULL, NULL) < 0) |
| 5310 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5311 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5312 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5313 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5314 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5315 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5316 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5317 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5318 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5319 | return 0; |
| 5320 | } |
| 5321 | Py_DECREF(msg); |
| 5322 | return 1; |
| 5323 | } |
| 5324 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5325 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5326 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5327 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5328 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5330 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5331 | if (ctx == Load) { |
| 5332 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5333 | return 0; |
| 5334 | } |
| 5335 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5336 | return 0; |
| 5337 | } |
| 5338 | } |
| 5339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5340 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5341 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5342 | case Store: op = STORE_SUBSCR; break; |
| 5343 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5345 | assert(op); |
| 5346 | VISIT(c, expr, e->v.Subscript.value); |
| 5347 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5348 | ADDOP(c, op); |
| 5349 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5353 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5355 | int n = 2; |
| 5356 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5358 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5359 | if (s->v.Slice.lower) { |
| 5360 | VISIT(c, expr, s->v.Slice.lower); |
| 5361 | } |
| 5362 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5363 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5364 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5366 | if (s->v.Slice.upper) { |
| 5367 | VISIT(c, expr, s->v.Slice.upper); |
| 5368 | } |
| 5369 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5370 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | if (s->v.Slice.step) { |
| 5374 | n++; |
| 5375 | VISIT(c, expr, s->v.Slice.step); |
| 5376 | } |
| 5377 | ADDOP_I(c, BUILD_SLICE, n); |
| 5378 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5381 | /* End of the compiler section, beginning of the assembler section */ |
| 5382 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5383 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5384 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5385 | |
| 5386 | XXX must handle implicit jumps from one block to next |
| 5387 | */ |
| 5388 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5389 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5391 | int a_offset; /* offset into bytecode */ |
| 5392 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5393 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5394 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5395 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 5396 | int a_lineno; /* lineno of last emitted instruction */ |
| 5397 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5398 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5399 | }; |
| 5400 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5401 | Py_LOCAL_INLINE(void) |
| 5402 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5403 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5404 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5405 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5406 | assert(b->b_startdepth < 0); |
| 5407 | b->b_startdepth = depth; |
| 5408 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5409 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
| 5412 | /* Find the flow path that needs the largest stack. We assume that |
| 5413 | * cycles in the flow graph have no net effect on the stack depth. |
| 5414 | */ |
| 5415 | static int |
| 5416 | stackdepth(struct compiler *c) |
| 5417 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5418 | basicblock *b, *entryblock = NULL; |
| 5419 | basicblock **stack, **sp; |
| 5420 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5421 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5422 | b->b_startdepth = INT_MIN; |
| 5423 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5424 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | } |
| 5426 | if (!entryblock) |
| 5427 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5428 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5429 | if (!stack) { |
| 5430 | PyErr_NoMemory(); |
| 5431 | return -1; |
| 5432 | } |
| 5433 | |
| 5434 | sp = stack; |
| 5435 | stackdepth_push(&sp, entryblock, 0); |
| 5436 | while (sp != stack) { |
| 5437 | b = *--sp; |
| 5438 | int depth = b->b_startdepth; |
| 5439 | assert(depth >= 0); |
| 5440 | basicblock *next = b->b_next; |
| 5441 | for (int i = 0; i < b->b_iused; i++) { |
| 5442 | struct instr *instr = &b->b_instr[i]; |
| 5443 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5444 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 5445 | _Py_FatalErrorFormat(__func__, |
| 5446 | "opcode = %d", instr->i_opcode); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5447 | } |
| 5448 | int new_depth = depth + effect; |
| 5449 | if (new_depth > maxdepth) { |
| 5450 | maxdepth = new_depth; |
| 5451 | } |
| 5452 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5453 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5454 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5455 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5456 | int target_depth = depth + effect; |
| 5457 | if (target_depth > maxdepth) { |
| 5458 | maxdepth = target_depth; |
| 5459 | } |
| 5460 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5461 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5462 | } |
| 5463 | depth = new_depth; |
| 5464 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5465 | instr->i_opcode == JUMP_FORWARD || |
| 5466 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5467 | instr->i_opcode == RAISE_VARARGS || |
| 5468 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5469 | { |
| 5470 | /* remaining code is dead */ |
| 5471 | next = NULL; |
| 5472 | break; |
| 5473 | } |
| 5474 | } |
| 5475 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 5476 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5477 | stackdepth_push(&sp, next, depth); |
| 5478 | } |
| 5479 | } |
| 5480 | PyObject_Free(stack); |
| 5481 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | static int |
| 5485 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5488 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5489 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5490 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5491 | if (a->a_bytecode == NULL) { |
| 5492 | goto error; |
| 5493 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5495 | if (a->a_lnotab == NULL) { |
| 5496 | goto error; |
| 5497 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5498 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5499 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5500 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5501 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5502 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5503 | error: |
| 5504 | Py_XDECREF(a->a_bytecode); |
| 5505 | Py_XDECREF(a->a_lnotab); |
| 5506 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | static void |
| 5510 | assemble_free(struct assembler *a) |
| 5511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | Py_XDECREF(a->a_bytecode); |
| 5513 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5516 | static int |
| 5517 | blocksize(basicblock *b) |
| 5518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | int i; |
| 5520 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5522 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5523 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5524 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5527 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5528 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5529 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5530 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5531 | if (a->a_lnotab_off + 2 >= len) { |
| 5532 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5533 | return 0; |
| 5534 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5535 | unsigned char *lnotab = (unsigned char *) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5538 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5539 | *lnotab++ = bdelta; |
| 5540 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5544 | /* Appends a range to the end of the line number table. See |
| 5545 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 5546 | |
| 5547 | static int |
| 5548 | assemble_line_range(struct assembler *a) |
| 5549 | { |
| 5550 | int ldelta, bdelta; |
| 5551 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 5552 | if (bdelta == 0) { |
| 5553 | return 1; |
| 5554 | } |
| 5555 | if (a->a_lineno < 0) { |
| 5556 | ldelta = -128; |
| 5557 | } |
| 5558 | else { |
| 5559 | ldelta = a->a_lineno - a->a_prevlineno; |
| 5560 | a->a_prevlineno = a->a_lineno; |
| 5561 | while (ldelta > 127) { |
| 5562 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 5563 | return 0; |
| 5564 | } |
| 5565 | ldelta -= 127; |
| 5566 | } |
| 5567 | while (ldelta < -127) { |
| 5568 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 5569 | return 0; |
| 5570 | } |
| 5571 | ldelta += 127; |
| 5572 | } |
| 5573 | } |
| 5574 | assert(-128 <= ldelta && ldelta < 128); |
| 5575 | while (bdelta > 254) { |
| 5576 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 5577 | return 0; |
| 5578 | } |
| 5579 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 5580 | bdelta -= 254; |
| 5581 | } |
| 5582 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 5583 | return 0; |
| 5584 | } |
| 5585 | a->a_lineno_start = a->a_offset; |
| 5586 | return 1; |
| 5587 | } |
| 5588 | |
| 5589 | static int |
| 5590 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 5591 | { |
| 5592 | if (i->i_lineno == a->a_lineno) { |
| 5593 | return 1; |
| 5594 | } |
| 5595 | if (!assemble_line_range(a)) { |
| 5596 | return 0; |
| 5597 | } |
| 5598 | a->a_lineno = i->i_lineno; |
| 5599 | return 1; |
| 5600 | } |
| 5601 | |
| 5602 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5603 | /* assemble_emit() |
| 5604 | Extend the bytecode with a new instruction. |
| 5605 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5606 | */ |
| 5607 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5608 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5609 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5610 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5611 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5613 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5614 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5615 | arg = i->i_oparg; |
| 5616 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5617 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5618 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5619 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5620 | if (len > PY_SSIZE_T_MAX / 2) |
| 5621 | return 0; |
| 5622 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5623 | return 0; |
| 5624 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5625 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5626 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5627 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5628 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5631 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5632 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5634 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5635 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5636 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5638 | /* Compute the size of each block and fixup jump args. |
| 5639 | Replace block pointer with position in bytecode. */ |
| 5640 | do { |
| 5641 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5642 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5643 | bsize = blocksize(b); |
| 5644 | b->b_offset = totsize; |
| 5645 | totsize += bsize; |
| 5646 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5647 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5648 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5649 | bsize = b->b_offset; |
| 5650 | for (i = 0; i < b->b_iused; i++) { |
| 5651 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5652 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5653 | /* Relative jumps are computed relative to |
| 5654 | the instruction pointer after fetching |
| 5655 | the jump instruction. |
| 5656 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5657 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5658 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5660 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5661 | instr->i_oparg -= bsize; |
| 5662 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5663 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5664 | if (instrsize(instr->i_oparg) != isize) { |
| 5665 | extended_arg_recompile = 1; |
| 5666 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5667 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5668 | } |
| 5669 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5671 | /* XXX: This is an awful hack that could hurt performance, but |
| 5672 | on the bright side it should work until we come up |
| 5673 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5675 | The issue is that in the first loop blocksize() is called |
| 5676 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5677 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5681 | The only EXTENDED_ARGs that could be popping up are |
| 5682 | ones in jump instructions. So this should converge |
| 5683 | fairly quickly. |
| 5684 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5685 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5688 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5689 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5691 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5692 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | tuple = PyTuple_New(size); |
| 5695 | if (tuple == NULL) |
| 5696 | return NULL; |
| 5697 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5698 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5699 | Py_INCREF(k); |
| 5700 | assert((i - offset) < size); |
| 5701 | assert((i - offset) >= 0); |
| 5702 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5703 | } |
| 5704 | return tuple; |
| 5705 | } |
| 5706 | |
| 5707 | static PyObject * |
| 5708 | consts_dict_keys_inorder(PyObject *dict) |
| 5709 | { |
| 5710 | PyObject *consts, *k, *v; |
| 5711 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5712 | |
| 5713 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5714 | if (consts == NULL) |
| 5715 | return NULL; |
| 5716 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5717 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5718 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5719 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5720 | * the object we want is always second. */ |
| 5721 | if (PyTuple_CheckExact(k)) { |
| 5722 | k = PyTuple_GET_ITEM(k, 1); |
| 5723 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5724 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5725 | assert(i < size); |
| 5726 | assert(i >= 0); |
| 5727 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5728 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5729 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5732 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5733 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5735 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5736 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5737 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5738 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5739 | if (ste->ste_nested) |
| 5740 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5741 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5742 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5743 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5744 | flags |= CO_COROUTINE; |
| 5745 | if (ste->ste_generator && ste->ste_coroutine) |
| 5746 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5747 | if (ste->ste_varargs) |
| 5748 | flags |= CO_VARARGS; |
| 5749 | if (ste->ste_varkeywords) |
| 5750 | flags |= CO_VARKEYWORDS; |
| 5751 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5754 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5755 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5756 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5757 | ste->ste_coroutine && |
| 5758 | !ste->ste_generator) { |
| 5759 | flags |= CO_COROUTINE; |
| 5760 | } |
| 5761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5762 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5765 | // Merge *tuple* with constant cache. |
| 5766 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5767 | static int |
| 5768 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5769 | { |
| 5770 | assert(PyTuple_CheckExact(*tuple)); |
| 5771 | |
| 5772 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5773 | if (key == NULL) { |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
| 5777 | // t is borrowed reference |
| 5778 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5779 | Py_DECREF(key); |
| 5780 | if (t == NULL) { |
| 5781 | return 0; |
| 5782 | } |
| 5783 | if (t == key) { // tuple is new constant. |
| 5784 | return 1; |
| 5785 | } |
| 5786 | |
| 5787 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5788 | Py_INCREF(u); |
| 5789 | Py_DECREF(*tuple); |
| 5790 | *tuple = u; |
| 5791 | return 1; |
| 5792 | } |
| 5793 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5794 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5795 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5798 | PyObject *names = NULL; |
| 5799 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5800 | PyObject *name = NULL; |
| 5801 | PyObject *freevars = NULL; |
| 5802 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5803 | Py_ssize_t nlocals; |
| 5804 | int nlocals_int; |
| 5805 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5806 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5809 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5810 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5811 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5812 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5813 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5814 | if (!cellvars) |
| 5815 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5816 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5817 | if (!freevars) |
| 5818 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5819 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5820 | if (!merge_const_tuple(c, &names) || |
| 5821 | !merge_const_tuple(c, &varnames) || |
| 5822 | !merge_const_tuple(c, &cellvars) || |
| 5823 | !merge_const_tuple(c, &freevars)) |
| 5824 | { |
| 5825 | goto error; |
| 5826 | } |
| 5827 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5828 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5829 | assert(nlocals < INT_MAX); |
| 5830 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5832 | flags = compute_code_flags(c); |
| 5833 | if (flags < 0) |
| 5834 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5835 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5836 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5837 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5838 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5839 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5840 | if (!merge_const_tuple(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5841 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5842 | goto error; |
| 5843 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5845 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5846 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5847 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5848 | maxdepth = stackdepth(c); |
| 5849 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5850 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5851 | goto error; |
| 5852 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5853 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5854 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5855 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5856 | varnames, freevars, cellvars, c->c_filename, |
| 5857 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5858 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5859 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5860 | Py_XDECREF(names); |
| 5861 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5862 | Py_XDECREF(name); |
| 5863 | Py_XDECREF(freevars); |
| 5864 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5865 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5868 | |
| 5869 | /* For debugging purposes only */ |
| 5870 | #if 0 |
| 5871 | static void |
| 5872 | dump_instr(const struct instr *i) |
| 5873 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5874 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 5875 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5876 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5878 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5879 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5880 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5881 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5883 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | static void |
| 5887 | dump_basicblock(const basicblock *b) |
| 5888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5889 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5890 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 5891 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5892 | if (b->b_instr) { |
| 5893 | int i; |
| 5894 | for (i = 0; i < b->b_iused; i++) { |
| 5895 | fprintf(stderr, " [%02d] ", i); |
| 5896 | dump_instr(b->b_instr + i); |
| 5897 | } |
| 5898 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5899 | } |
| 5900 | #endif |
| 5901 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5902 | |
| 5903 | static int |
| 5904 | normalize_basic_block(basicblock *bb); |
| 5905 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5906 | static int |
| 5907 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 5908 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5909 | static int |
| 5910 | ensure_exits_have_lineno(struct compiler *c); |
| 5911 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5912 | static PyCodeObject * |
| 5913 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5914 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5915 | basicblock *b, *entryblock; |
| 5916 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5917 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5918 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5919 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5921 | /* Make sure every block that falls off the end returns None. |
| 5922 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5923 | block ends with a jump or return b_next shouldn't set. |
| 5924 | */ |
| 5925 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5926 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5927 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5928 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5929 | ADDOP(c, RETURN_VALUE); |
| 5930 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5931 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5932 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5933 | if (normalize_basic_block(b)) { |
| 5934 | goto error; |
| 5935 | } |
| 5936 | } |
| 5937 | |
| 5938 | if (ensure_exits_have_lineno(c)) { |
| 5939 | goto error; |
| 5940 | } |
| 5941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | nblocks = 0; |
| 5943 | entryblock = NULL; |
| 5944 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5945 | nblocks++; |
| 5946 | entryblock = b; |
| 5947 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5949 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5950 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5951 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5952 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5953 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5954 | c->u->u_firstlineno = 1; |
| 5955 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5958 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5959 | a.a_entry = entryblock; |
| 5960 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5961 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5962 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 5963 | if (consts == NULL) { |
| 5964 | goto error; |
| 5965 | } |
| 5966 | if (optimize_cfg(&a, consts)) { |
| 5967 | goto error; |
| 5968 | } |
| 5969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5970 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5971 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5972 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5973 | /* Emit code. */ |
| 5974 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5975 | for (j = 0; j < b->b_iused; j++) |
| 5976 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5977 | goto error; |
| 5978 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5979 | if (!assemble_line_range(&a)) { |
| 5980 | return 0; |
| 5981 | } |
| 5982 | /* Emit sentinel at end of line number table */ |
| 5983 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 5984 | goto error; |
| 5985 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5988 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5989 | 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] | 5990 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5991 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5992 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5993 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5994 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5995 | assemble_free(&a); |
| 5996 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5997 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5998 | |
| 5999 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6000 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6001 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6002 | PyArena *arena) |
| 6003 | { |
| 6004 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6005 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6006 | |
| 6007 | |
| 6008 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6009 | with LOAD_CONST (c1, c2, ... cn). |
| 6010 | The consts table must still be in list form so that the |
| 6011 | new constant (c1, c2, ... cn) can be appended. |
| 6012 | Called with codestr pointing to the first LOAD_CONST. |
| 6013 | */ |
| 6014 | static int |
| 6015 | fold_tuple_on_constants(struct instr *inst, |
| 6016 | int n, PyObject *consts) |
| 6017 | { |
| 6018 | /* Pre-conditions */ |
| 6019 | assert(PyList_CheckExact(consts)); |
| 6020 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6021 | assert(inst[n].i_oparg == n); |
| 6022 | |
| 6023 | for (int i = 0; i < n; i++) { |
| 6024 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6025 | return 0; |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | /* Buildup new tuple of constants */ |
| 6030 | PyObject *newconst = PyTuple_New(n); |
| 6031 | if (newconst == NULL) { |
| 6032 | return -1; |
| 6033 | } |
| 6034 | for (int i = 0; i < n; i++) { |
| 6035 | int arg = inst[i].i_oparg; |
| 6036 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6037 | Py_INCREF(constant); |
| 6038 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6039 | } |
| 6040 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6041 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6042 | Py_DECREF(newconst); |
| 6043 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6044 | return -1; |
| 6045 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6046 | if (PyList_Append(consts, newconst)) { |
| 6047 | Py_DECREF(newconst); |
| 6048 | return -1; |
| 6049 | } |
| 6050 | Py_DECREF(newconst); |
| 6051 | for (int i = 0; i < n; i++) { |
| 6052 | inst[i].i_opcode = NOP; |
| 6053 | } |
| 6054 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6055 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6056 | return 0; |
| 6057 | } |
| 6058 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6059 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6060 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6061 | |
| 6062 | /* Optimization */ |
| 6063 | static int |
| 6064 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6065 | { |
| 6066 | assert(PyList_CheckExact(consts)); |
| 6067 | struct instr nop; |
| 6068 | nop.i_opcode = NOP; |
| 6069 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6070 | for (int i = 0; i < bb->b_iused; i++) { |
| 6071 | struct instr *inst = &bb->b_instr[i]; |
| 6072 | int oparg = inst->i_oparg; |
| 6073 | 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] | 6074 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6075 | /* Skip over empty basic blocks. */ |
| 6076 | while (inst->i_target->b_iused == 0) { |
| 6077 | inst->i_target = inst->i_target->b_next; |
| 6078 | } |
| 6079 | target = &inst->i_target->b_instr[0]; |
| 6080 | } |
| 6081 | else { |
| 6082 | target = &nop; |
| 6083 | } |
| 6084 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6085 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6086 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6087 | { |
| 6088 | PyObject* cnt; |
| 6089 | int is_true; |
| 6090 | int jump_if_true; |
| 6091 | switch(nextop) { |
| 6092 | case POP_JUMP_IF_FALSE: |
| 6093 | case POP_JUMP_IF_TRUE: |
| 6094 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6095 | is_true = PyObject_IsTrue(cnt); |
| 6096 | if (is_true == -1) { |
| 6097 | goto error; |
| 6098 | } |
| 6099 | inst->i_opcode = NOP; |
| 6100 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6101 | if (is_true == jump_if_true) { |
| 6102 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6103 | bb->b_nofallthrough = 1; |
| 6104 | } |
| 6105 | else { |
| 6106 | bb->b_instr[i+1].i_opcode = NOP; |
| 6107 | } |
| 6108 | break; |
| 6109 | case JUMP_IF_FALSE_OR_POP: |
| 6110 | case JUMP_IF_TRUE_OR_POP: |
| 6111 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6112 | is_true = PyObject_IsTrue(cnt); |
| 6113 | if (is_true == -1) { |
| 6114 | goto error; |
| 6115 | } |
| 6116 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6117 | if (is_true == jump_if_true) { |
| 6118 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6119 | bb->b_nofallthrough = 1; |
| 6120 | } |
| 6121 | else { |
| 6122 | inst->i_opcode = NOP; |
| 6123 | bb->b_instr[i+1].i_opcode = NOP; |
| 6124 | } |
| 6125 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6126 | } |
| 6127 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6128 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6129 | |
| 6130 | /* Try to fold tuples of constants. |
| 6131 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6132 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6133 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6134 | case BUILD_TUPLE: |
| 6135 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6136 | switch(oparg) { |
| 6137 | case 1: |
| 6138 | inst->i_opcode = NOP; |
| 6139 | bb->b_instr[i+1].i_opcode = NOP; |
| 6140 | break; |
| 6141 | case 2: |
| 6142 | inst->i_opcode = ROT_TWO; |
| 6143 | bb->b_instr[i+1].i_opcode = NOP; |
| 6144 | break; |
| 6145 | case 3: |
| 6146 | inst->i_opcode = ROT_THREE; |
| 6147 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6148 | } |
| 6149 | break; |
| 6150 | } |
| 6151 | if (i >= oparg) { |
| 6152 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6153 | goto error; |
| 6154 | } |
| 6155 | } |
| 6156 | break; |
| 6157 | |
| 6158 | /* Simplify conditional jump to conditional jump where the |
| 6159 | result of the first test implies the success of a similar |
| 6160 | test or the failure of the opposite test. |
| 6161 | Arises in code like: |
| 6162 | "a and b or c" |
| 6163 | "(a and b) and c" |
| 6164 | "(a or b) or c" |
| 6165 | "(a or b) and c" |
| 6166 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6167 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6168 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6169 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6170 | where y+1 is the instruction following the second test. |
| 6171 | */ |
| 6172 | case JUMP_IF_FALSE_OR_POP: |
| 6173 | switch(target->i_opcode) { |
| 6174 | case POP_JUMP_IF_FALSE: |
| 6175 | *inst = *target; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6176 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6177 | break; |
| 6178 | case JUMP_ABSOLUTE: |
| 6179 | case JUMP_FORWARD: |
| 6180 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6181 | if (inst->i_target != target->i_target) { |
| 6182 | inst->i_target = target->i_target; |
| 6183 | --i; |
| 6184 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6185 | break; |
| 6186 | case JUMP_IF_TRUE_OR_POP: |
| 6187 | assert (inst->i_target->b_iused == 1); |
| 6188 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6189 | inst->i_target = inst->i_target->b_next; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6190 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6191 | break; |
| 6192 | } |
| 6193 | break; |
| 6194 | |
| 6195 | case JUMP_IF_TRUE_OR_POP: |
| 6196 | switch(target->i_opcode) { |
| 6197 | case POP_JUMP_IF_TRUE: |
| 6198 | *inst = *target; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6199 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6200 | break; |
| 6201 | case JUMP_ABSOLUTE: |
| 6202 | case JUMP_FORWARD: |
| 6203 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6204 | if (inst->i_target != target->i_target) { |
| 6205 | inst->i_target = target->i_target; |
| 6206 | --i; |
| 6207 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6208 | break; |
| 6209 | case JUMP_IF_FALSE_OR_POP: |
| 6210 | assert (inst->i_target->b_iused == 1); |
| 6211 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 6212 | inst->i_target = inst->i_target->b_next; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6213 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6214 | break; |
| 6215 | } |
| 6216 | break; |
| 6217 | |
| 6218 | case POP_JUMP_IF_FALSE: |
| 6219 | switch(target->i_opcode) { |
| 6220 | case JUMP_ABSOLUTE: |
| 6221 | case JUMP_FORWARD: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6222 | if (inst->i_target != target->i_target) { |
| 6223 | inst->i_target = target->i_target; |
| 6224 | --i; |
| 6225 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6226 | break; |
| 6227 | } |
| 6228 | break; |
| 6229 | |
| 6230 | case POP_JUMP_IF_TRUE: |
| 6231 | switch(target->i_opcode) { |
| 6232 | case JUMP_ABSOLUTE: |
| 6233 | case JUMP_FORWARD: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6234 | if (inst->i_target != target->i_target) { |
| 6235 | inst->i_target = target->i_target; |
| 6236 | --i; |
| 6237 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6238 | break; |
| 6239 | } |
| 6240 | break; |
| 6241 | |
| 6242 | case JUMP_ABSOLUTE: |
| 6243 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6244 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6245 | switch(target->i_opcode) { |
| 6246 | case JUMP_FORWARD: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6247 | if (inst->i_target != target->i_target) { |
| 6248 | inst->i_target = target->i_target; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 6249 | // --i; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6250 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6251 | break; |
| 6252 | case JUMP_ABSOLUTE: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6253 | if (inst->i_target != target->i_target) { |
| 6254 | inst->i_target = target->i_target; |
| 6255 | inst->i_opcode = target->i_opcode; |
| 6256 | --i; |
| 6257 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6258 | break; |
| 6259 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6260 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 6261 | basicblock *to_copy = inst->i_target; |
Mark Shannon | 8473cf8 | 2020-12-15 11:07:50 +0000 | [diff] [blame^] | 6262 | inst->i_opcode = NOP; |
| 6263 | for (i = 0; i < to_copy->b_iused; i++) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6264 | int index = compiler_next_instr(bb); |
| 6265 | if (index < 0) { |
| 6266 | return -1; |
| 6267 | } |
| 6268 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 6269 | } |
| 6270 | bb->b_exit = 1; |
| 6271 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6272 | break; |
| 6273 | } |
| 6274 | } |
| 6275 | return 0; |
| 6276 | error: |
| 6277 | return -1; |
| 6278 | } |
| 6279 | |
| 6280 | |
| 6281 | static void |
| 6282 | clean_basic_block(basicblock *bb) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6283 | /* Remove NOPs. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6284 | int dest = 0; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6285 | int prev_lineno = -1; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6286 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6287 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6288 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6289 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6290 | if (lineno < 0) { |
| 6291 | continue; |
| 6292 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6293 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6294 | if (prev_lineno == lineno) { |
| 6295 | continue; |
| 6296 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6297 | /* 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] | 6298 | if (src < bb->b_iused - 1) { |
| 6299 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 6300 | if (next_lineno < 0 || next_lineno == lineno) { |
| 6301 | bb->b_instr[src+1].i_lineno = lineno; |
| 6302 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6303 | } |
| 6304 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6305 | else { |
| 6306 | basicblock* next = bb->b_next; |
| 6307 | while (next && next->b_iused == 0) { |
| 6308 | next = next->b_next; |
| 6309 | } |
| 6310 | /* or if last instruction in BB and next BB has same line number */ |
| 6311 | if (next) { |
| 6312 | if (lineno == next->b_instr[0].i_lineno) { |
| 6313 | continue; |
| 6314 | } |
| 6315 | } |
| 6316 | } |
| 6317 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6318 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6319 | if (dest != src) { |
| 6320 | bb->b_instr[dest] = bb->b_instr[src]; |
| 6321 | } |
| 6322 | dest++; |
| 6323 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6324 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6325 | assert(dest <= bb->b_iused); |
| 6326 | bb->b_iused = dest; |
| 6327 | } |
| 6328 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6329 | static int |
| 6330 | normalize_basic_block(basicblock *bb) { |
| 6331 | /* Mark blocks as exit and/or nofallthrough. |
| 6332 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6333 | for (int i = 0; i < bb->b_iused; i++) { |
| 6334 | switch(bb->b_instr[i].i_opcode) { |
| 6335 | case RETURN_VALUE: |
| 6336 | case RAISE_VARARGS: |
| 6337 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6338 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6339 | bb->b_nofallthrough = 1; |
| 6340 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6341 | case JUMP_ABSOLUTE: |
| 6342 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6343 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6344 | /* fall through */ |
| 6345 | case POP_JUMP_IF_FALSE: |
| 6346 | case POP_JUMP_IF_TRUE: |
| 6347 | case JUMP_IF_FALSE_OR_POP: |
| 6348 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6349 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6350 | if (i != bb->b_iused-1) { |
| 6351 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 6352 | return -1; |
| 6353 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6354 | /* Skip over empty basic blocks. */ |
| 6355 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 6356 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 6357 | } |
| 6358 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6359 | } |
| 6360 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6361 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6364 | static int |
| 6365 | mark_reachable(struct assembler *a) { |
| 6366 | basicblock **stack, **sp; |
| 6367 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 6368 | if (stack == NULL) { |
| 6369 | return -1; |
| 6370 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6371 | a->a_entry->b_reachable = 1; |
| 6372 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6373 | while (sp > stack) { |
| 6374 | basicblock *b = *(--sp); |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6375 | if (b->b_next && !b->b_nofallthrough && b->b_next->b_reachable == 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6376 | b->b_next->b_reachable = 1; |
| 6377 | *sp++ = b->b_next; |
| 6378 | } |
| 6379 | for (int i = 0; i < b->b_iused; i++) { |
| 6380 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6381 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6382 | target = b->b_instr[i].i_target; |
| 6383 | if (target->b_reachable == 0) { |
| 6384 | target->b_reachable = 1; |
| 6385 | *sp++ = target; |
| 6386 | } |
| 6387 | } |
| 6388 | } |
| 6389 | } |
| 6390 | PyObject_Free(stack); |
| 6391 | return 0; |
| 6392 | } |
| 6393 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6394 | /* If an instruction has no line number, but it's predecessor in the BB does, |
| 6395 | * then copy the line number. This reduces the size of the line number table, |
| 6396 | * but has no impact on the generated line number events. |
| 6397 | */ |
| 6398 | static void |
| 6399 | minimize_lineno_table(struct assembler *a) { |
| 6400 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6401 | int prev_lineno = -1; |
| 6402 | for (int i = 0; i < b->b_iused; i++) { |
| 6403 | if (b->b_instr[i].i_lineno < 0) { |
| 6404 | b->b_instr[i].i_lineno = prev_lineno; |
| 6405 | } |
| 6406 | else { |
| 6407 | prev_lineno = b->b_instr[i].i_lineno; |
| 6408 | } |
| 6409 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6410 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6411 | } |
| 6412 | } |
| 6413 | |
| 6414 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6415 | The consts object should still be in list form to allow new constants |
| 6416 | to be appended. |
| 6417 | |
| 6418 | All transformations keep the code size the same or smaller. |
| 6419 | For those that reduce size, the gaps are initially filled with |
| 6420 | NOPs. Later those NOPs are removed. |
| 6421 | */ |
| 6422 | |
| 6423 | static int |
| 6424 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 6425 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6426 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6427 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6428 | return -1; |
| 6429 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6430 | clean_basic_block(b); |
| 6431 | assert(b->b_reachable == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6432 | } |
| 6433 | if (mark_reachable(a)) { |
| 6434 | return -1; |
| 6435 | } |
| 6436 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6437 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6438 | if (b->b_reachable == 0) { |
| 6439 | b->b_iused = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6440 | } |
| 6441 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6442 | minimize_lineno_table(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6443 | return 0; |
| 6444 | } |
| 6445 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6446 | static inline int |
| 6447 | is_exit_without_lineno(basicblock *b) { |
| 6448 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 6449 | } |
| 6450 | |
| 6451 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 6452 | * after a frame terminates. It would be prohibitively expensive |
| 6453 | * to continuously update the f_lineno field at runtime, |
| 6454 | * so we make sure that all exiting instruction (raises and returns) |
| 6455 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 6456 | * We can do this by duplicating the exit blocks without line number |
| 6457 | * so that none have more than one predecessor. We can then safely |
| 6458 | * copy the line number from the sole predecessor block. |
| 6459 | */ |
| 6460 | static int |
| 6461 | ensure_exits_have_lineno(struct compiler *c) |
| 6462 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 6463 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6464 | /* Copy all exit blocks without line number that are targets of a jump. |
| 6465 | */ |
| 6466 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6467 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 6468 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 6469 | /* Note: Only actual jumps, not exception handlers */ |
| 6470 | case SETUP_ASYNC_WITH: |
| 6471 | case SETUP_WITH: |
| 6472 | case SETUP_FINALLY: |
| 6473 | continue; |
| 6474 | } |
| 6475 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 6476 | if (is_exit_without_lineno(target)) { |
| 6477 | basicblock *new_target = compiler_copy_block(c, target); |
| 6478 | if (new_target == NULL) { |
| 6479 | return -1; |
| 6480 | } |
| 6481 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 6482 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 6483 | } |
| 6484 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 6485 | entry = b; |
| 6486 | } |
| 6487 | assert(entry != NULL); |
| 6488 | if (is_exit_without_lineno(entry)) { |
| 6489 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6490 | } |
| 6491 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 6492 | * fall through, and thus can only have a single predecessor */ |
| 6493 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6494 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 6495 | if (is_exit_without_lineno(b->b_next)) { |
| 6496 | assert(b->b_next->b_iused > 0); |
| 6497 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 6498 | } |
| 6499 | } |
| 6500 | } |
| 6501 | return 0; |
| 6502 | } |
| 6503 | |
| 6504 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6505 | /* Retained for API compatibility. |
| 6506 | * Optimization is now done in optimize_cfg */ |
| 6507 | |
| 6508 | PyObject * |
| 6509 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 6510 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 6511 | { |
| 6512 | Py_INCREF(code); |
| 6513 | return code; |
| 6514 | } |
| 6515 | |